OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-2017 eyeo GmbH | 3 * Copyright (C) 2006-2017 eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 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 | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 29 matching lines...) Expand all Loading... |
40 | 40 |
41 jclass GetJsValueClass() | 41 jclass GetJsValueClass() |
42 { | 42 { |
43 return (jsValueClass != NULL ? jsValueClass->Get() : NULL); | 43 return (jsValueClass != NULL ? jsValueClass->Get() : NULL); |
44 } | 44 } |
45 | 45 |
46 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) | 46 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
47 { | 47 { |
48 try | 48 try |
49 { | 49 { |
50 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; | 50 return JniGetJsValuePtr(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; |
51 } | 51 } |
52 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 52 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
53 } | 53 } |
54 | 54 |
55 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) | 55 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) |
56 { | 56 { |
57 try | 57 try |
58 { | 58 { |
59 return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; | 59 return JniGetJsValuePtr(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; |
60 } | 60 } |
61 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 61 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
62 } | 62 } |
63 | 63 |
64 static jboolean JNICALL JniIsNumber(JNIEnv* env, jclass clazz, jlong ptr) | 64 static jboolean JNICALL JniIsNumber(JNIEnv* env, jclass clazz, jlong ptr) |
65 { | 65 { |
66 try | 66 try |
67 { | 67 { |
68 return JniGetJsValue(ptr)->IsNumber() ? JNI_TRUE : JNI_FALSE; | 68 return JniGetJsValuePtr(ptr)->IsNumber() ? JNI_TRUE : JNI_FALSE; |
69 } | 69 } |
70 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 70 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
71 } | 71 } |
72 | 72 |
73 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) | 73 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) |
74 { | 74 { |
75 try | 75 try |
76 { | 76 { |
77 return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; | 77 return JniGetJsValuePtr(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; |
78 } | 78 } |
79 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 79 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
80 } | 80 } |
81 | 81 |
82 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | 82 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
83 { | 83 { |
84 try | 84 try |
85 { | 85 { |
86 return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; | 86 return JniGetJsValuePtr(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; |
87 } | 87 } |
88 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 88 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
89 } | 89 } |
90 | 90 |
91 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) | 91 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) |
92 { | 92 { |
93 try | 93 try |
94 { | 94 { |
95 return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; | 95 return JniGetJsValuePtr(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; |
96 } | 96 } |
97 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 97 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
98 } | 98 } |
99 | 99 |
100 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) | 100 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) |
101 { | 101 { |
102 try | 102 try |
103 { | 103 { |
104 return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; | 104 return JniGetJsValuePtr(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; |
105 } | 105 } |
106 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 106 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
107 } | 107 } |
108 | 108 |
109 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) | 109 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) |
110 { | 110 { |
111 try | 111 try |
112 { | 112 { |
113 return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; | 113 return JniGetJsValuePtr(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; |
114 } | 114 } |
115 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 115 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
116 } | 116 } |
117 | 117 |
118 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) | 118 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) |
119 { | 119 { |
120 try | 120 try |
121 { | 121 { |
122 return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str()); | 122 AdblockPlus::JsValue* jsValue = JniGetJsValuePtr(ptr); |
| 123 return env->NewStringUTF(jsValue->AsString().c_str()); |
123 } | 124 } |
124 CATCH_THROW_AND_RETURN(env, 0) | 125 CATCH_THROW_AND_RETURN(env, 0) |
125 } | 126 } |
126 | 127 |
127 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) | 128 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) |
128 { | 129 { |
129 try | 130 try |
130 { | 131 { |
131 return static_cast<jlong>(JniGetJsValue(ptr)->AsInt()); | 132 return static_cast<jlong>(JniGetJsValuePtr(ptr)->AsInt()); |
132 } | 133 } |
133 CATCH_THROW_AND_RETURN(env, 0) | 134 CATCH_THROW_AND_RETURN(env, 0) |
134 } | 135 } |
135 | 136 |
136 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | 137 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
137 { | 138 { |
138 try | 139 try |
139 { | 140 { |
140 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; | 141 return JniGetJsValuePtr(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; |
141 } | 142 } |
142 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 143 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
143 } | 144 } |
144 | 145 |
145 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) | 146 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
146 { | 147 { |
147 try | 148 try |
148 { | 149 { |
149 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); | 150 AdblockPlus::JsValueList list = JniGetJsValuePtr(ptr)->AsList(); |
150 | 151 |
151 return JniJsValueListToArrayList(env, list); | 152 return JniJsValueListToArrayList(env, list); |
152 } | 153 } |
153 CATCH_THROW_AND_RETURN(env, 0) | 154 CATCH_THROW_AND_RETURN(env, 0) |
154 } | 155 } |
155 | 156 |
156 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr
ing name) | 157 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr
ing name) |
157 { | 158 { |
158 try | 159 try |
159 { | 160 { |
160 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString
(env, name))); | 161 return NewJniJsValue(env, JniGetJsValuePtr(ptr)->GetProperty(JniJavaToStdStr
ing(env, name))); |
161 } | 162 } |
162 CATCH_THROW_AND_RETURN(env, 0) | 163 CATCH_THROW_AND_RETURN(env, 0) |
163 } | 164 } |
164 | 165 |
165 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 166 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
166 { | 167 { |
167 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 168 delete JniLongToTypePtr<AdblockPlus::JsValue>(ptr); |
168 } | 169 } |
169 | 170 |
170 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
s jsValueClassArg) | 171 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValue& jsValue, jclass j
sValueClassArg) |
171 { | 172 { |
172 if (!jsValue.get()) | 173 return env->NewObject(jsValueClass->Get(), jsValueCtor, new AdblockPlus::JsVal
ue(std::move(jsValue))); |
173 { | |
174 return 0; | |
175 } | |
176 | |
177 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); | |
178 return env->NewObject(jsValueClass->Get(), jsValueCtor, ptr); | |
179 } | 174 } |
180 | 175 |
181 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) | 176 AdblockPlus::JsValue* JniGetJsValuePtr(jlong ptr) |
182 { | 177 { |
183 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); | 178 return JniLongToTypePtr<AdblockPlus::JsValue>(ptr); |
184 } | 179 } |
185 | 180 |
186 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) | 181 AdblockPlus::JsValue& JniGetJsValue(jlong ptr) |
187 { | 182 { |
188 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 183 return *JniLongToTypePtr<AdblockPlus::JsValue>(ptr); |
189 } | 184 } |
190 | 185 |
191 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) | 186 jobject JniJsValueListToArrayList(JNIEnv* env, const AdblockPlus::JsValueList& l
ist) |
192 { | 187 { |
193 jobject arrayList = NewJniArrayList(env); | 188 jobject arrayList = NewJniArrayList(env); |
194 | 189 |
195 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i
t != end; ++it) | 190 for (AdblockPlus::JsValueList::const_iterator it = list.begin(), end = list.en
d(); it != end; ++it) |
196 { | 191 { |
197 JniAddObjectToList(env, arrayList, | 192 JniAddObjectToList(env, arrayList, *JniLocalReference<jobject>(env, NewJniJs
Value(env, *it))); |
198 *JniLocalReference<jobject>(env, NewJniJsValue(env, *it))); | |
199 } | 193 } |
200 | 194 |
201 return arrayList; | 195 return arrayList; |
202 } | 196 } |
203 | 197 |
204 // TODO: List of functions that lack JNI bindings | 198 // TODO: List of functions that lack JNI bindings |
205 //std::vector<std::string> GetOwnPropertyNames() const; | 199 //std::vector<std::string> GetOwnPropertyNames() const; |
206 //void SetProperty(const std::string& name, const std::string& val); | 200 //void SetProperty(const std::string& name, const std::string& val); |
207 //void SetProperty(const std::string& name, int64_t val); | 201 //void SetProperty(const std::string& name, int64_t val); |
208 //void SetProperty(const std::string& name, bool val); | 202 //void SetProperty(const std::string& name, bool val); |
209 //void SetProperty(const std::string& name, JsValuePtr value); | 203 //void SetProperty(const std::string& name, JsValue value); |
210 //void SetProperty(const std::string& name, const char* val); | 204 //void SetProperty(const std::string& name, const char* val); |
211 //inline void SetProperty(const std::string& name, int val); | 205 //inline void SetProperty(const std::string& name, int val); |
212 //std::string GetClass() const; | 206 //std::string GetClass() const; |
213 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu
ePtr thisPtr = AdblockPlus::JsValuePtr()) const; | 207 //JsValue Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValue t
hisPtr = AdblockPlus::JsValue()) const; |
214 | 208 |
215 static JNINativeMethod methods[] = | 209 static JNINativeMethod methods[] = |
216 { | 210 { |
217 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, | 211 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, |
218 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, | 212 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, |
219 { (char*)"isNumber", (char*)"(J)Z", (void*)JniIsNumber }, | 213 { (char*)"isNumber", (char*)"(J)Z", (void*)JniIsNumber }, |
220 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, | 214 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, |
221 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, | 215 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, |
222 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, | 216 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, |
223 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, | 217 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, |
224 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, | 218 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, |
225 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, | 219 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, |
226 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, | 220 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, |
227 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, | 221 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
228 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | 222 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
229 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, | 223 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, |
230 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 224 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
231 }; | 225 }; |
232 | 226 |
233 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) | 227 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) |
234 { | 228 { |
235 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 229 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
236 } | 230 } |
OLD | NEW |