Index: jni/JniJsValue.cpp |
=================================================================== |
new file mode 100644 |
--- /dev/null |
+++ b/jni/JniJsValue.cpp |
@@ -0,0 +1,201 @@ |
+/* |
+ * 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" |
+ |
+namespace AdblockPlus |
+{ |
+namespace Android |
+{ |
+ |
+jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue) |
+{ |
+ jclass clazz = env->FindClass(PKG("JsValue")); |
+ jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); |
+ jlong ptr = AdblockPlus::Android::JniPtr2Long(new AdblockPlus::JsValuePtr(jsValue)); |
+ return env->NewObject(clazz, ctor, ptr); |
+} |
+ |
+AdblockPlus::JsValue* JniGetJsValue(jlong ptr) |
+{ |
+ return AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); |
+} |
+ |
+AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) |
+{ |
+ return *AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr); |
+} |
+ |
+jobject JniJsValueList2ArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) |
+{ |
+ jobject arrayList = AdblockPlus::Android::NewJniArrayList(env); |
+ |
+ for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); it != end; ++it) |
+ { |
+ AdblockPlus::Android::JniAddObjectToList(env, arrayList, AdblockPlus::Android::NewJniJsValue(env, *it)); |
+ } |
+ |
+ return arrayList; |
+} |
+ |
+} // namespace Android |
+} // namespace AdblockPlus |
+ |
+static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return AdblockPlus::Android::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 AdblockPlus::Android::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 AdblockPlus::Android::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 AdblockPlus::Android::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 AdblockPlus::Android::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 AdblockPlus::Android::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 AdblockPlus::Android::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(AdblockPlus::Android::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>(AdblockPlus::Android::JniGetJsValue(ptr)->AsInt()); |
+ } |
+ CATCH_THROW_AND_RETURN(env, 0) |
+} |
+ |
+static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ return AdblockPlus::Android::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 AdblockPlus::Android::JniGetJsValue(ptr) ? JNI_TRUE : JNI_FALSE; |
+} |
+ |
+static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ TRY |
+ { |
+ AdblockPlus::JsValueList list = AdblockPlus::Android::JniGetJsValue(ptr)->AsList(); |
+ |
+ return AdblockPlus::Android::JniJsValueList2ArrayList(env, list); |
+ } |
+ CATCH_THROW_AND_RETURN(env, 0) |
+} |
+ |
+static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) |
+{ |
+ delete AdblockPlus::Android::JniLong2TypePtr<AdblockPlus::JsValuePtr>(ptr); |
+} |
+ |
+//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])); |
+} |