| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, | |
| 3 * Copyright (C) 2006-2016 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 static jboolean JNICALL JniIsUndefined(JNIEnv* env, jclass clazz, jlong ptr) | |
| 23 { | |
| 24 try | |
| 25 { | |
| 26 return JniGetJsValue(ptr)->IsUndefined() ? JNI_TRUE : JNI_FALSE; | |
| 27 } | |
| 28 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 29 } | |
| 30 | |
| 31 static jboolean JNICALL JniIsNull(JNIEnv* env, jclass clazz, jlong ptr) | |
| 32 { | |
| 33 try | |
| 34 { | |
| 35 return JniGetJsValue(ptr)->IsNull() ? JNI_TRUE : JNI_FALSE; | |
| 36 } | |
| 37 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 38 } | |
| 39 | |
| 40 static jboolean JNICALL JniIsNumber(JNIEnv* env, jclass clazz, jlong ptr) | |
| 41 { | |
| 42 try | |
| 43 { | |
| 44 return JniGetJsValue(ptr)->IsNumber() ? JNI_TRUE : JNI_FALSE; | |
| 45 } | |
| 46 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 47 } | |
| 48 | |
| 49 static jboolean JNICALL JniIsString(JNIEnv* env, jclass clazz, jlong ptr) | |
| 50 { | |
| 51 try | |
| 52 { | |
| 53 return JniGetJsValue(ptr)->IsString() ? JNI_TRUE : JNI_FALSE; | |
| 54 } | |
| 55 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 56 } | |
| 57 | |
| 58 static jboolean JNICALL JniIsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | |
| 59 { | |
| 60 try | |
| 61 { | |
| 62 return JniGetJsValue(ptr)->IsBool() ? JNI_TRUE : JNI_FALSE; | |
| 63 } | |
| 64 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 65 } | |
| 66 | |
| 67 static jboolean JNICALL JniIsObject(JNIEnv* env, jclass clazz, jlong ptr) | |
| 68 { | |
| 69 try | |
| 70 { | |
| 71 return JniGetJsValue(ptr)->IsObject() ? JNI_TRUE : JNI_FALSE; | |
| 72 } | |
| 73 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 74 } | |
| 75 | |
| 76 static jboolean JNICALL JniIsArray(JNIEnv* env, jclass clazz, jlong ptr) | |
| 77 { | |
| 78 try | |
| 79 { | |
| 80 return JniGetJsValue(ptr)->IsArray() ? JNI_TRUE : JNI_FALSE; | |
| 81 } | |
| 82 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 83 } | |
| 84 | |
| 85 static jboolean JNICALL JniIsFunction(JNIEnv* env, jclass clazz, jlong ptr) | |
| 86 { | |
| 87 try | |
| 88 { | |
| 89 return JniGetJsValue(ptr)->IsFunction() ? JNI_TRUE : JNI_FALSE; | |
| 90 } | |
| 91 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 92 } | |
| 93 | |
| 94 static jstring JNICALL JniAsString(JNIEnv* env, jclass clazz, jlong ptr) | |
| 95 { | |
| 96 try | |
| 97 { | |
| 98 return env->NewStringUTF(JniGetJsValue(ptr)->AsString().c_str()); | |
| 99 } | |
| 100 CATCH_THROW_AND_RETURN(env, 0) | |
| 101 } | |
| 102 | |
| 103 static jlong JNICALL JniAsLong(JNIEnv* env, jclass clazz, jlong ptr) | |
| 104 { | |
| 105 try | |
| 106 { | |
| 107 return static_cast<jlong>(JniGetJsValue(ptr)->AsInt()); | |
| 108 } | |
| 109 CATCH_THROW_AND_RETURN(env, 0) | |
| 110 } | |
| 111 | |
| 112 static jboolean JNICALL JniAsBoolean(JNIEnv* env, jclass clazz, jlong ptr) | |
| 113 { | |
| 114 try | |
| 115 { | |
| 116 return JniGetJsValue(ptr)->AsBool() ? JNI_TRUE : JNI_FALSE; | |
| 117 } | |
| 118 CATCH_THROW_AND_RETURN(env, JNI_FALSE) | |
| 119 } | |
| 120 | |
| 121 static jobject JNICALL JniAsList(JNIEnv* env, jclass clazz, jlong ptr) | |
| 122 { | |
| 123 try | |
| 124 { | |
| 125 AdblockPlus::JsValueList list = JniGetJsValue(ptr)->AsList(); | |
| 126 | |
| 127 return JniJsValueListToArrayList(env, list); | |
| 128 } | |
| 129 CATCH_THROW_AND_RETURN(env, 0) | |
| 130 } | |
| 131 | |
| 132 static jobject JNICALL JniGetProperty(JNIEnv* env, jclass clazz, jlong ptr, jstr
ing name) | |
| 133 { | |
| 134 try | |
| 135 { | |
| 136 return NewJniJsValue(env, JniGetJsValue(ptr)->GetProperty(JniJavaToStdString
(env, name))); | |
| 137 } | |
| 138 CATCH_THROW_AND_RETURN(env, 0) | |
| 139 } | |
| 140 | |
| 141 static void JNICALL JniDtor(JNIEnv* env, jclass clazz, jlong ptr) | |
| 142 { | |
| 143 delete JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | |
| 144 } | |
| 145 | |
| 146 jobject NewJniJsValue(JNIEnv* env, const AdblockPlus::JsValuePtr& jsValue, jclas
s jsValueClass) | |
| 147 { | |
| 148 if (!jsValue.get()) | |
| 149 { | |
| 150 return 0; | |
| 151 } | |
| 152 | |
| 153 jclass clazz = jsValueClass ? jsValueClass : env->FindClass(PKG("JsValue")); | |
| 154 jmethodID ctor = env->GetMethodID(clazz, "<init>", "(J)V"); | |
| 155 jlong ptr = JniPtrToLong(new AdblockPlus::JsValuePtr(jsValue)); | |
| 156 jobject ret = env->NewObject(clazz, ctor, ptr); | |
| 157 | |
| 158 if (!jsValueClass) | |
| 159 { | |
| 160 env->DeleteLocalRef(clazz); | |
| 161 } | |
| 162 | |
| 163 return ret; | |
| 164 } | |
| 165 | |
| 166 AdblockPlus::JsValue* JniGetJsValue(jlong ptr) | |
| 167 { | |
| 168 return JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr)->get(); | |
| 169 } | |
| 170 | |
| 171 AdblockPlus::JsValuePtr& JniGetJsValuePtr(jlong ptr) | |
| 172 { | |
| 173 return *JniLongToTypePtr<AdblockPlus::JsValuePtr>(ptr); | |
| 174 } | |
| 175 | |
| 176 jobject JniJsValueListToArrayList(JNIEnv* env, AdblockPlus::JsValueList& list) | |
| 177 { | |
| 178 jobject arrayList = NewJniArrayList(env); | |
| 179 | |
| 180 for (AdblockPlus::JsValueList::iterator it = list.begin(), end = list.end(); i
t != end; ++it) | |
| 181 { | |
| 182 JniAddObjectToList(env, arrayList, | |
| 183 *JniLocalReference<jobject>(env, NewJniJsValue(env, *it))); | |
| 184 } | |
| 185 | |
| 186 return arrayList; | |
| 187 } | |
| 188 | |
| 189 // TODO: List of functions that lack JNI bindings | |
| 190 //std::vector<std::string> GetOwnPropertyNames() const; | |
| 191 //void SetProperty(const std::string& name, const std::string& val); | |
| 192 //void SetProperty(const std::string& name, int64_t val); | |
| 193 //void SetProperty(const std::string& name, bool val); | |
| 194 //void SetProperty(const std::string& name, JsValuePtr value); | |
| 195 //void SetProperty(const std::string& name, const char* val); | |
| 196 //inline void SetProperty(const std::string& name, int val); | |
| 197 //std::string GetClass() const; | |
| 198 //JsValuePtr Call(const JsValueList& params = JsValueList(), AdblockPlus::JsValu
ePtr thisPtr = AdblockPlus::JsValuePtr()) const; | |
| 199 | |
| 200 static JNINativeMethod methods[] = | |
| 201 { | |
| 202 { (char*)"isUndefined", (char*)"(J)Z", (void*)JniIsUndefined }, | |
| 203 { (char*)"isNull", (char*)"(J)Z", (void*)JniIsNull }, | |
| 204 { (char*)"isNumber", (char*)"(J)Z", (void*)JniIsNumber }, | |
| 205 { (char*)"isString", (char*)"(J)Z", (void*)JniIsString }, | |
| 206 { (char*)"isBoolean", (char*)"(J)Z", (void*)JniIsBoolean }, | |
| 207 { (char*)"isObject", (char*)"(J)Z", (void*)JniIsObject }, | |
| 208 { (char*)"isArray", (char*)"(J)Z", (void*)JniIsArray }, | |
| 209 { (char*)"isFunction", (char*)"(J)Z", (void*)JniIsFunction }, | |
| 210 { (char*)"asString", (char*)"(J)Ljava/lang/String;", (void*)JniAsString }, | |
| 211 { (char*)"asLong", (char*)"(J)J", (void*)JniAsLong }, | |
| 212 { (char*)"asBoolean", (char*)"(J)Z", (void*)JniAsBoolean }, | |
| 213 { (char*)"asList", (char*)"(J)Ljava/util/List;", (void*)JniAsList }, | |
| 214 { (char*)"getProperty", (char*)"(JLjava/lang/String;)" TYP("JsValue"), (void*)
JniGetProperty }, | |
| 215 { (char*)"dtor", (char*)"(J)V", (void*)JniDtor } | |
| 216 }; | |
| 217 | |
| 218 extern "C" JNIEXPORT void JNICALL Java_org_adblockplus_libadblockplus_JsValue_re
gisterNatives(JNIEnv *env, jclass clazz) | |
| 219 { | |
| 220 env->RegisterNatives(clazz, methods, sizeof(methods) / sizeof(methods[0])); | |
| 221 } | |
| OLD | NEW |