OLD | NEW |
1 /* | 1 /* |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | 2 * This file is part of Adblock Plus <http://adblockplus.org/>, |
3 * Copyright (C) 2006-2014 Eyeo GmbH | 3 * Copyright (C) 2006-2014 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 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | 103 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
104 { | 104 { |
105 try | 105 try |
106 { | 106 { |
107 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; | 107 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; |
108 } | 108 } |
109 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | 109 CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
110 } | 110 } |
111 | 111 |
112 static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr) | |
113 { | |
114 return JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; | |
115 } | |
116 | |
117 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) | 112 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
118 { | 113 { |
119 try | 114 try |
120 { | 115 { |
121 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); | 116 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); |
122 | 117 |
123 return JniJsValueListToArrayList(env, list); | 118 return JniJsValueListToArrayList(env, list); |
124 } | 119 } |
125 CATCH_THROW_AND_RETURN(env, 0) | 120 CATCH_THROW_AND_RETURN(env, 0) |
126 } | 121 } |
127 | 122 |
| 123 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr
ing name) |
| 124 { |
| 125 try |
| 126 { |
| 127 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString
(env, name))); |
| 128 } |
| 129 CATCH_THROW_AND_RETURN(env, 0) |
| 130 } |
| 131 |
128 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | 132 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
129 { | 133 { |
130 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 134 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
131 } | 135 } |
132 | 136 |
133 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue) | 137 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
s jsValueClass) |
134 { | 138 { |
135 jclass clazz = env->FindClass(PKG("JsValue")); | 139 if (!jsValue.get()) |
| 140 { |
| 141 return 0; |
| 142 } |
| 143 |
| 144 jclass clazz = jsValueClass ? jsValueClass : env->FindClass(PKG("JsValue")); |
136 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); | 145 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); |
137 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); | 146 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); |
138 return env->NewObject(clazz, ctor, ptr); | 147 return env->NewObject(clazz, ctor, ptr); |
139 } | 148 } |
140 | 149 |
141 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) | 150 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
142 { | 151 { |
143 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); | 152 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); |
144 } | 153 } |
145 | 154 |
146 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) | 155 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
147 { | 156 { |
148 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | 157 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
149 } | 158 } |
150 | 159 |
151 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) | 160 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) |
152 { | 161 { |
153 jobject arrayList = NewJniArrayList(env); | 162 jobject arrayList = NewJniArrayList(env); |
154 | 163 |
155 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i
t != end; ++it) | 164 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i
t != end; ++it) |
156 { | 165 { |
157 JniAddObjectToList(env, arrayList, NewJniJsValue(env, *it)); | 166 JniAddObjectToList(env, arrayList, NewJniJsValue(env, *it)); |
158 } | 167 } |
159 | 168 |
160 return arrayList; | 169 return arrayList; |
161 } | 170 } |
162 | 171 |
163 // TODO: List of functions that lack JNI bindings | 172 // TODO: List of functions that lack JNI bindings |
164 //std::vector<std::string> GetOwnPropertyNames() const; | 173 //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); | 174 //void SetProperty(const std::string& name, const std::string& val); |
167 //void SetProperty(const std::string& name, int64_t val); | 175 //void SetProperty(const std::string& name, int64_t val); |
168 //void SetProperty(const std::string& name, bool val); | 176 //void SetProperty(const std::string& name, bool val); |
169 //void SetProperty(const std::string& name, JsValuePtr value); | 177 //void SetProperty(const std::string& name, JsValuePtr value); |
170 //void SetProperty(const std::string& name, const char* val); | 178 //void SetProperty(const std::string& name, const char* val); |
171 //inline void SetProperty(const std::string& name, int val); | 179 //inline void SetProperty(const std::string& name, int val); |
172 //std::string GetClass() const; | 180 //std::string GetClass() const; |
173 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu
ePtr thisPtr = AdblockPlus::JsValuePtr()) const; | 181 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu
ePtr thisPtr = AdblockPlus::JsValuePtr()) const; |
174 | 182 |
175 static JNINativeMethod methods[] = | 183 static JNINativeMethod methods[] = |
176 { | 184 { |
177 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, | 185 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, |
178 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, | 186 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, |
179 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, | 187 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, |
180 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, | 188 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, |
181 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, | 189 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, |
182 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, | 190 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, |
183 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, | 191 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, |
184 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, | 192 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, |
185 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, | 193 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, |
186 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, | 194 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
187 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | 195 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
188 { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid }, | 196 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, |
189 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | 197 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
190 }; | 198 }; |
191 | 199 |
192 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) | 200 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) |
193 { | 201 { |
194 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | 202 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
195 } | 203 } |
OLD | NEW |