Index: jni/JniJsValue.cpp |
diff --git a/jni/JniJsValue.cpp b/jni/JniJsValue.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..0eae830e8264cab2d0c51df7c6a630238a3950d7 |
--- /dev/null |
+++ b/jni/JniJsValue.cpp |
@@ -0,0 +1,195 @@ |
+/* |
+ * This file is part of Adblock Plus <http://adblockplus.org/>, |
+ * Copyright (C) 2006-2014 Eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+#include <AdblockPlus.h> |
+#include "Utils.h" |
+#include "JniJsValue.h" |
+ |
+jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue) |
+{ |
+ jclass clazz = env->FindClass(PKG("JsValue")); |
+ jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); |
+ jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); |
+ return env->NewObject(clazz, ctor, ptr); |
+} |
+ |
+AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
+{ |
+ return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); |
+} |
+ |
+AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
+{ |
+ return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
+} |
+ |
+jobject JniJsValueList2ArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) |
+{ |
+ jobject arrayList = NewJniArrayList(env); |
+ |
+ for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); it != end; ++it) |
+ { |
+ JniAddObjectToList(env, arrayList, NewJniJsValue(env, *it)); |
+ } |
+ |
+ return arrayList; |
+} |
+ |
+static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
Felix Dahlke
2014/03/28 17:28:41
Static stuff to the top here as well please.
René Jeschke
2014/04/11 12:25:53
Done.
|
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str()); |
+ } |
+ CATCH_THROW_AND_RETURN(env, 0) |
+} |
+ |
+static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return static_cast<jlong>(JniGetJsValue(ptr)->AsInt()); |
+ } |
+ CATCH_THROW_AND_RETURN(env, 0) |
+} |
+ |
+static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; |
+ } |
+ CATCH_THROW_AND_RETURN(env, JNI_FALSE) |
+} |
+ |
+static jboolean JNICALL JniIsValid(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ return JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; |
+} |
+ |
+static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); |
+ |
+ return JniJsValueList2ArrayList(env, list); |
+ } |
+ CATCH_THROW_AND_RETURN(env, 0) |
+} |
+ |
+static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); |
+} |
+ |
+// TODO: List of functions that lack JNI bindings |
+//std::vector<std::string> GetOwnPropertyNames() const; |
+//JsValuePtr GetProperty(const std::string& name) const; |
+//void SetProperty(const std::string& name, const std::string& val); |
+//void SetProperty(const std::string& name, int64_t val); |
+//void SetProperty(const std::string& name, bool val); |
+//void SetProperty(const std::string& name, JsValuePtr value); |
+//void SetProperty(const std::string& name, const char* val); |
+//inline void SetProperty(const std::string& name, int val); |
+//std::string GetClass() const; |
+//JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValuePtr thisPtr = AdblockPlus::JsValuePtr()) const; |
+ |
+static JNINativeMethod methods[] = |
+{ |
+ { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, |
+ { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, |
+ { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, |
+ { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, |
+ { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, |
+ { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, |
+ { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, |
+ { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, |
+ { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, |
+ { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, |
+ { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, |
+ { (char*)"isValid", (char*)"(J)Z", (void*)JniIsValid }, |
+ { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } |
+}; |
+ |
+extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_android_api_JsValue_registerNatives(JNIEnv *env, jclass clazz) |
+{ |
+ env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); |
+} |