Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: jni/JniJsValue.cpp

Issue 6606493159784448: New JNI bindings (Closed)
Patch Set: Bug fix for global references (shadowing) Created March 31, 2014, 2:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « jni/JniJsValue.h ('k') | jni/JniLogSystem.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <http://adblockplus.org/>,
3 * Copyright (C) 2006-2014 Eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <AdblockPlus.h>
19 #include "Utils.h"
20 #include "JniJsValue.h"
21
22 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue)
23 {
24 jclass clazz = env->FindClass(PKG("JsValue"));
25 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V");
26 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue));
27 return env->NewObject(clazz, ctor, ptr);
28 }
29
30 AdblockPlus::JsValue* JniGetJsValue(jlong ptr)
31 {
32 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get();
33 }
34
35 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr)
36 {
37 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr);
38 }
39
40 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list)
41 {
42 jobject arrayList = NewJniArrayList(env);
43
44 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i t != end; ++it)
45 {
46 JniAddObjectToList(env, arrayList, NewJniJsValue(env, *it));
47 }
48
49 return arrayList;
50 }
51
52 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr)
53 {
54 TRY
55 {
56 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE;
57 }
58 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
59 }
60
61 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr)
62 {
63 TRY
64 {
65 return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE;
66 }
67 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
68 }
69
70 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr)
71 {
72 TRY
73 {
74 return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE;
75 }
76 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
77 }
78
79 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr)
80 {
81 TRY
82 {
83 return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE;
84 }
85 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
86 }
87
88 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr)
89 {
90 TRY
91 {
92 return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE;
93 }
94 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
95 }
96
97 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr)
98 {
99 TRY
100 {
101 return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE;
102 }
103 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
104 }
105
106 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr)
107 {
108 TRY
109 {
110 return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE;
111 }
112 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
113 }
114
115 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr)
116 {
117 TRY
118 {
119 return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str());
120 }
121 CATCH_THROW_AND_RETURN(env, 0)
122 }
123
124 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr)
125 {
126 TRY
127 {
128 return static_cast<jlong>(JniGetJsValue(ptr)->AsInt());
129 }
130 CATCH_THROW_AND_RETURN(env, 0)
131 }
132
133 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr)
134 {
135 TRY
136 {
137 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE;
138 }
139 CATCH_THROW_AND_RETURN(env, JNI_FALSE)
140 }
141
142 static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr)
143 {
144 return JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE;
145 }
146
147 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr)
148 {
149 TRY
150 {
151 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList();
152
153 return JniJsValueListToArrayList(env, list);
154 }
155 CATCH_THROW_AND_RETURN(env, 0)
156 }
157
158 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr)
159 {
160 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr);
161 }
162
163 // TODO: List of functions that lack JNI bindings
164 //std::vector<std::string> GetOwnPropertyNames() const;
165 //JsValuePtr GetProperty(const std::string& name) const;
166 //void SetProperty(const std::string& name, const std::string& val);
167 //void SetProperty(const std::string& name, int64_t val);
168 //void SetProperty(const std::string& name, bool val);
169 //void SetProperty(const std::string& name, JsValuePtr value);
170 //void SetProperty(const std::string& name, const char* val);
171 //inline void SetProperty(const std::string& name, int val);
172 //std::string GetClass() const;
173 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu ePtr thisPtr = AdblockPlus::JsValuePtr()) const;
174
175 static JNINativeMethod methods[] =
176 {
177 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined },
178 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull },
179 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString },
180 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean },
181 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject },
182 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray },
183 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction },
184 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString },
185 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong },
186 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean },
187 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList },
188 { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid },
189 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor }
190 };
191
192 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re gisterNatives(JNIEnv *env, jclass clazz)
193 {
194 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0]));
195 }
OLDNEW
« no previous file with comments | « jni/JniJsValue.h ('k') | jni/JniLogSystem.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld