OLD | NEW |
| (Empty) |
1 /* | |
2 * This file is part of Adblock Plus <http://adblockplus.org/>, | |
3 * Copyright (C) 2006-2013 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 <math.h> | |
19 #include <android/log.h> | |
20 #include <jni.h> | |
21 #include <v8.h> | |
22 | |
23 jobject NewBoolean(JNIEnv *pEnv, jboolean value) | |
24 { | |
25 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/Boolean"))); | |
26 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "(Z)V"); | |
27 return pEnv->NewObject(cls, cid, value); | |
28 } | |
29 | |
30 jobject NewInt(JNIEnv *pEnv, jint value) | |
31 { | |
32 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/Integer"))); | |
33 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "(I)V"); | |
34 return pEnv->NewObject(cls, cid, value); | |
35 } | |
36 | |
37 jobject NewLong(JNIEnv *pEnv, jlong value) | |
38 { | |
39 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/Long"))); | |
40 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "(J)V"); | |
41 return pEnv->NewObject(cls, cid, value); | |
42 } | |
43 | |
44 jobject NewDouble(JNIEnv *pEnv, jdouble value) | |
45 { | |
46 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/Double"))); | |
47 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "(D)V"); | |
48 return pEnv->NewObject(cls, cid, value); | |
49 } | |
50 | |
51 jstring NewString(JNIEnv *pEnv, v8::Handle<v8::String> str) | |
52 { | |
53 return pEnv->NewStringUTF(*v8::String::Utf8Value(str)); | |
54 } | |
55 | |
56 jobject NewDate(JNIEnv *pEnv, v8::Handle<v8::Date> date) | |
57 { | |
58 static jclass cls = reinterpret_cast<jclass>(pEnv->NewGlobalRef(pEnv->FindClas
s("java/lang/Double"))); | |
59 static jmethodID cid = pEnv->GetMethodID(cls, "<init>", "(J)V"); | |
60 jlong value = floor(date->NumberValue()); | |
61 return pEnv->NewObject(cls, cid, value); | |
62 } | |
63 | |
64 jobject wrapJSObject(JNIEnv *pEnv, v8::Handle<v8::Value> value) | |
65 { | |
66 v8::HandleScope handle_scope; | |
67 | |
68 if (value.IsEmpty() || value->IsNull() || value->IsUndefined()) return NULL; | |
69 if (value->IsTrue()) return NewBoolean(pEnv, JNI_TRUE); | |
70 if (value->IsFalse()) return NewBoolean(pEnv, JNI_FALSE); | |
71 | |
72 if (value->IsInt32()) return NewInt(pEnv, value->Int32Value()); | |
73 if (value->IsUint32()) return NewLong(pEnv, value->IntegerValue()); | |
74 if (value->IsString()) return NewString(pEnv, v8::Handle<v8::String>::Cast(val
ue)); | |
75 if (value->IsDate()) return NewDate(pEnv, v8::Handle<v8::Date>::Cast(value)); | |
76 if (value->IsNumber()) return NewDouble(pEnv, value->NumberValue()); | |
77 | |
78 // return wrap(value->ToObject()); | |
79 return NULL; | |
80 } | |
81 | |
82 v8::Handle<v8::Value> wrapJavaObject(JNIEnv *pEnv, jobject value) | |
83 { | |
84 v8::HandleScope handle_scope; | |
85 v8::TryCatch try_catch; | |
86 | |
87 if (value == NULL) | |
88 return handle_scope.Close(v8::Null()); | |
89 | |
90 v8::Handle<v8::Value> result; | |
91 | |
92 jclass cls = pEnv->GetObjectClass(value); | |
93 | |
94 if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/String")) == JNI_TR
UE) | |
95 { | |
96 jstring str = (jstring) value; | |
97 const char *p = pEnv->GetStringUTFChars(str, NULL); | |
98 | |
99 result = v8::String::New(p, pEnv->GetStringUTFLength(str)); | |
100 | |
101 pEnv->ReleaseStringUTFChars(str, p); | |
102 } | |
103 else if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Long")) == JNI
_TRUE | |
104 || pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Integer")) == JN
I_TRUE | |
105 || pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Short")) == JNI_
TRUE | |
106 || pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Byte")) == JNI_T
RUE) | |
107 { | |
108 static jmethodID mid = pEnv->GetMethodID(pEnv->FindClass("java/lang/Number")
, "intValue", "()I"); | |
109 | |
110 result = v8::Integer::New(pEnv->CallIntMethod(value, mid)); | |
111 } | |
112 else if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Double")) == J
NI_TRUE | |
113 || pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Float")) == JNI_
TRUE) | |
114 { | |
115 static jmethodID mid = pEnv->GetMethodID(pEnv->FindClass("java/lang/Number")
, "doubleValue", "()D"); | |
116 | |
117 result = v8::Number::New(pEnv->CallDoubleMethod(value, mid)); | |
118 } | |
119 else if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/Boolean")) ==
JNI_TRUE) | |
120 { | |
121 static jmethodID mid = pEnv->GetMethodID(pEnv->FindClass("java/lang/Boolean"
), "booleanValue", "()Z"); | |
122 | |
123 result = v8::Boolean::New(pEnv->CallBooleanMethod(value, mid)); | |
124 } | |
125 else if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/util/Date")) == JNI
_TRUE) | |
126 { | |
127 static jmethodID mid = pEnv->GetMethodID(pEnv->FindClass("java/util/Date"),
"getTime", "()J"); | |
128 | |
129 result = v8::Date::New(pEnv->CallLongMethod(value, mid)); | |
130 } | |
131 /* | |
132 else if (pEnv->IsAssignableFrom(cls, pEnv->FindClass("java/lang/reflect/Metho
d")) == JNI_TRUE) | |
133 { | |
134 result = jni::CJavaFunction::Wrap(pEnv, value); | |
135 } | |
136 */ | |
137 else | |
138 { | |
139 static jmethodID mid = pEnv->GetMethodID(pEnv->FindClass("java/lang/Class"),
"isArray", "()Z"); | |
140 | |
141 if (pEnv->CallBooleanMethod(pEnv->GetObjectClass(value), mid)) | |
142 { | |
143 size_t len = pEnv->GetArrayLength((jarray) value); | |
144 v8::Handle<v8::Array> items = v8::Array::New(len); | |
145 | |
146 for (size_t i=0; i<len; i++) | |
147 { | |
148 jobject item = pEnv->GetObjectArrayElement((jobjectArray) value, i); | |
149 items->Set(i, wrapJavaObject(pEnv, item)); | |
150 pEnv->DeleteLocalRef(item); | |
151 } | |
152 | |
153 result = items; | |
154 } | |
155 } | |
156 | |
157 return try_catch.HasCaught() ? v8::Handle<v8::Value>() : handle_scope.Close(re
sult); | |
158 // return ThrowIf(try_catch) ? v8::Handle<v8::Value>() : handle_scope.Close(res
ult); | |
159 } | |
OLD | NEW |