]>
Commit | Line | Data |
---|---|---|
7341eedb JF |
1 | /* Cycript - The Truly Universal Scripting Language |
2 | * Copyright (C) 2009-2016 Jay Freeman (saurik) | |
e17ad658 JF |
3 | */ |
4 | ||
f95d2598 | 5 | /* GNU Affero General Public License, Version 3 {{{ */ |
e17ad658 | 6 | /* |
f95d2598 JF |
7 | * This program is free software: you can redistribute it and/or modify |
8 | * it under the terms of the GNU Affero General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | ||
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
e17ad658 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
f95d2598 JF |
15 | * GNU Affero General Public License for more details. |
16 | ||
17 | * You should have received a copy of the GNU Affero General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
e17ad658 JF |
19 | **/ |
20 | /* }}} */ | |
21 | ||
dbf05bfd JF |
22 | #include <map> |
23 | #include <sstream> | |
24 | #include <vector> | |
25 | ||
f2c357f9 JF |
26 | #include <dlfcn.h> |
27 | ||
4b5dc402 | 28 | #if defined(__APPLE__) && !defined(__arm__) |
def90846 JF |
29 | #include <JavaVM/jni.h> |
30 | #else | |
e17ad658 | 31 | #include <jni.h> |
def90846 | 32 | #endif |
dbf05bfd | 33 | |
f2c357f9 JF |
34 | #ifdef __ANDROID__ |
35 | // XXX: this is deprecated?!?!?!?!?!?! | |
36 | #include <sys/system_properties.h> | |
37 | #endif | |
38 | ||
dbf05bfd | 39 | #include "cycript.hpp" |
c9c16dde | 40 | #include "Error.hpp" |
dbf05bfd JF |
41 | #include "Execute.hpp" |
42 | #include "Internal.hpp" | |
43 | #include "JavaScript.hpp" | |
44 | #include "Pooling.hpp" | |
45 | ||
46 | #define _jnicall(expr) ({ \ | |
47 | jint _value(expr); \ | |
48 | if (_value != JNI_OK) \ | |
49 | CYThrow("_jnicall(%s) == %d", #expr, _value); \ | |
50 | }) | |
51 | ||
52 | #define _envcall(jni, expr) ({ \ | |
53 | __typeof__(jni->expr) _value(jni->expr); \ | |
54 | if (jthrowable _error = jni->ExceptionOccurred()) { \ | |
55 | jni->ExceptionClear(); \ | |
fc664744 | 56 | throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \ |
dbf05bfd JF |
57 | } \ |
58 | _value; }) | |
59 | ||
60 | #define _envcallv(jni, expr) do { \ | |
61 | jni->expr; \ | |
62 | if (jthrowable _error = jni->ExceptionOccurred()) { \ | |
63 | jni->ExceptionClear(); \ | |
fc664744 | 64 | throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \ |
dbf05bfd JF |
65 | } \ |
66 | } while (false) | |
67 | ||
c9c16dde | 68 | #define CYJavaTry \ |
fc664744 | 69 | CYJavaEnv jni(env); \ |
824bc1ec JF |
70 | auto &protect(*reinterpret_cast<CYProtect *>(jprotect)); \ |
71 | _disused JSContextRef context(protect); \ | |
72 | _disused JSObjectRef object(protect); \ | |
c9c16dde JF |
73 | try |
74 | #define CYJavaCatch(value) \ | |
75 | catch (const CYException &error) { \ | |
fc664744 | 76 | jni->Throw(CYCastJavaObject(jni, context, error.CastJSValue(context, "Error")).cast<jthrowable>()); \ |
c9c16dde JF |
77 | return value; \ |
78 | } | |
79 | ||
fc664744 JF |
80 | #define CYJavaForEachPrimitive \ |
81 | CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \ | |
82 | CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \ | |
83 | CYJavaForEachPrimitive_(C, c, Char, Character, char) \ | |
84 | CYJavaForEachPrimitive_(S, s, Short, Short, short) \ | |
85 | CYJavaForEachPrimitive_(I, i, Int, Integer, int) \ | |
86 | CYJavaForEachPrimitive_(J, j, Long, Long, long) \ | |
87 | CYJavaForEachPrimitive_(F, f, Float, Float, float) \ | |
88 | CYJavaForEachPrimitive_(D, d, Double, Double, double) | |
c9c16dde | 89 | |
fc664744 JF |
90 | enum CYJavaPrimitive : char { |
91 | CYJavaPrimitiveObject, | |
92 | CYJavaPrimitiveVoid, | |
93 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
94 | CYJavaPrimitive ## Type, | |
95 | CYJavaForEachPrimitive | |
96 | #undef CYJavaForEachPrimitive_ | |
97 | }; | |
dbf05bfd | 98 | |
0e832de4 JF |
99 | template <typename Type_> |
100 | struct IsJavaPrimitive { static const bool value = false; }; | |
101 | ||
102 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
103 | template <> \ | |
104 | struct IsJavaPrimitive<j ## type> { static const bool value = true; }; | |
105 | CYJavaForEachPrimitive | |
106 | #undef CYJavaForEachPrimitive_ | |
107 | ||
fc664744 JF |
108 | // Java References {{{ |
109 | template <typename Value_> | |
110 | struct CYJavaRef { | |
111 | JNIEnv *jni_; | |
112 | Value_ value_; | |
dbf05bfd | 113 | |
fc664744 JF |
114 | _finline CYJavaRef(JNIEnv *jni, Value_ value) : |
115 | jni_(jni), | |
116 | value_(value) | |
117 | { | |
118 | } | |
dbf05bfd | 119 | |
477549f5 JF |
120 | _finline operator Value_() const { |
121 | return value_; | |
fc664744 | 122 | } |
c9c16dde | 123 | |
477549f5 | 124 | _finline JNIEnv *jni() const { |
fc664744 JF |
125 | return jni_; |
126 | } | |
c9c16dde | 127 | |
0e832de4 | 128 | // XXX: this is only needed to support CYJavaEnv relying on C variadics |
fc664744 JF |
129 | _finline Value_ get() const { |
130 | return value_; | |
131 | } | |
c9c16dde | 132 | |
fc664744 JF |
133 | template <typename Other_> |
134 | _finline CYJavaRef<Other_> cast() const { | |
135 | return {jni_, static_cast<Other_>(value_)}; | |
136 | } | |
137 | ||
4e2cc9d3 | 138 | // XXX: this should be tied into CYJavaFrame |
fc664744 JF |
139 | Value_ leak() { |
140 | Value_ value(value_); | |
141 | value_ = NULL; | |
142 | return value; | |
143 | } | |
144 | }; | |
145 | ||
146 | template <typename Value_, void (JNIEnv::*Delete_)(jobject)> | |
147 | struct CYJavaDelete : | |
148 | CYJavaRef<Value_> | |
149 | { | |
150 | _finline CYJavaDelete(JNIEnv *jni, Value_ value) : | |
151 | CYJavaRef<Value_>(jni, value) | |
152 | { | |
153 | } | |
154 | ||
4e2cc9d3 | 155 | void clear() { |
fc664744 JF |
156 | if (this->value_ != NULL) |
157 | (this->jni_->*Delete_)(this->value_); | |
4e2cc9d3 JF |
158 | this->value_ = NULL; |
159 | } | |
160 | ||
161 | ~CYJavaDelete() { | |
162 | clear(); | |
fc664744 JF |
163 | } |
164 | }; | |
165 | ||
166 | template <typename Value_> | |
167 | struct CYJavaGlobal : | |
168 | CYJavaDelete<Value_, &JNIEnv::DeleteGlobalRef> | |
169 | { | |
170 | typedef CYJavaDelete<Value_, &JNIEnv::DeleteGlobalRef> CYJavaBase; | |
171 | ||
172 | CYJavaGlobal() : | |
173 | CYJavaBase(NULL, NULL) | |
174 | { | |
175 | } | |
176 | ||
177 | template <typename Other_> | |
178 | CYJavaGlobal(const CYJavaRef<Other_> &other) : | |
179 | CYJavaBase(other.jni_, static_cast<Other_>(other.jni_->NewGlobalRef(other.value_))) | |
180 | { | |
181 | } | |
182 | ||
183 | CYJavaGlobal(const CYJavaGlobal<Value_> &other) : | |
184 | CYJavaGlobal(static_cast<const CYJavaRef<Value_> &>(other)) | |
185 | { | |
186 | } | |
187 | ||
188 | CYJavaGlobal(CYJavaGlobal &&value) : | |
189 | CYJavaBase(value.jni_, value.value_) | |
190 | { | |
191 | value.value_ = NULL; | |
192 | } | |
193 | }; | |
194 | ||
195 | template <typename Value_> | |
196 | struct CYJavaLocal : | |
197 | CYJavaDelete<Value_, &JNIEnv::DeleteLocalRef> | |
198 | { | |
199 | typedef CYJavaDelete<Value_, &JNIEnv::DeleteLocalRef> CYJavaBase; | |
200 | ||
201 | CYJavaLocal() : | |
202 | CYJavaBase(NULL, NULL) | |
203 | { | |
204 | } | |
205 | ||
206 | CYJavaLocal(JNIEnv *jni, Value_ value) : | |
207 | CYJavaBase(jni, value) | |
208 | { | |
209 | } | |
210 | ||
211 | template <typename Other_> | |
212 | CYJavaLocal(const CYJavaRef<Other_> &other) : | |
213 | CYJavaLocal(other.jni_, static_cast<Other_>(other.jni_->NewLocalRef(other.value_))) | |
214 | { | |
215 | } | |
216 | ||
4e2cc9d3 JF |
217 | template <typename Other_> |
218 | CYJavaLocal(CYJavaRef<Other_> &&other) : | |
219 | CYJavaLocal(other.jni_, other.value_) | |
220 | { | |
221 | other.value_ = NULL; | |
222 | } | |
223 | ||
224 | CYJavaLocal(CYJavaLocal &&other) : | |
225 | CYJavaLocal(static_cast<CYJavaRef<Value_> &&>(other)) | |
fc664744 | 226 | { |
fc664744 JF |
227 | } |
228 | ||
4e2cc9d3 JF |
229 | template <typename Other_> |
230 | CYJavaLocal &operator =(CYJavaLocal<Other_> &&other) { | |
231 | this->clear(); | |
232 | this->jni_ = other.jni_; | |
233 | this->value_ = other.value_; | |
234 | other.value_ = NULL; | |
fc664744 JF |
235 | return *this; |
236 | } | |
237 | }; | |
238 | // }}} | |
239 | // Java Strings {{{ | |
240 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaRef<jobject> &value); | |
dbf05bfd JF |
241 | |
242 | class CYJavaUTF8String : | |
243 | public CYUTF8String | |
244 | { | |
245 | private: | |
fc664744 | 246 | const CYJavaRef<jstring> *value_; |
dbf05bfd JF |
247 | |
248 | public: | |
fc664744 JF |
249 | CYJavaUTF8String(const CYJavaRef<jstring> &value) : |
250 | value_(&value) | |
251 | { | |
252 | _assert(value); | |
477549f5 | 253 | JNIEnv *jni(value.jni()); |
fc664744 JF |
254 | size = jni->GetStringUTFLength(value); |
255 | data = jni->GetStringUTFChars(value, NULL); | |
256 | } | |
257 | ||
258 | CYJavaUTF8String(const CYJavaRef<jobject> &value) : | |
259 | CYJavaUTF8String(CYCastJavaString(value)) | |
dbf05bfd | 260 | { |
dbf05bfd JF |
261 | } |
262 | ||
263 | ~CYJavaUTF8String() { | |
fc664744 | 264 | if (value_ != NULL) { |
477549f5 | 265 | JNIEnv *jni(value_->jni()); |
fc664744 JF |
266 | jni->ReleaseStringUTFChars(*value_, data); |
267 | } | |
dbf05bfd JF |
268 | } |
269 | ||
270 | CYJavaUTF8String(const CYJavaUTF8String &) = delete; | |
271 | ||
272 | CYJavaUTF8String(CYJavaUTF8String &&rhs) : | |
dbf05bfd JF |
273 | value_(rhs.value_) |
274 | { | |
275 | rhs.value_ = NULL; | |
276 | } | |
277 | }; | |
278 | ||
fc664744 JF |
279 | CYJavaUTF8String CYCastUTF8String(const CYJavaRef<jstring> &value) { |
280 | return CYJavaUTF8String(value); | |
dbf05bfd JF |
281 | } |
282 | ||
fc664744 JF |
283 | JSStringRef CYCopyJSString(const CYJavaRef<jstring> &value) { |
284 | return CYCopyJSString(CYCastUTF8String(value)); | |
dbf05bfd | 285 | } |
fc664744 JF |
286 | // }}} |
287 | // Java Error {{{ | |
288 | struct CYJavaError : | |
289 | CYException | |
290 | { | |
291 | CYJavaGlobal<jthrowable> value_; | |
dbf05bfd | 292 | |
fc664744 JF |
293 | CYJavaError(const CYJavaRef<jthrowable> &value) : |
294 | value_(value) | |
295 | { | |
296 | } | |
297 | ||
298 | virtual const char *PoolCString(CYPool &pool) const { | |
299 | return CYPoolCString(pool, CYJavaUTF8String(value_.cast<jobject>())); | |
300 | } | |
301 | ||
477549f5 | 302 | virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const; |
fc664744 JF |
303 | }; |
304 | // }}} | |
305 | ||
306 | struct CYJavaFrame { | |
dbf05bfd | 307 | JNIEnv *jni_; |
dbf05bfd | 308 | |
fc664744 JF |
309 | CYJavaFrame(JNIEnv *jni, jint capacity) : |
310 | jni_(jni) | |
dbf05bfd | 311 | { |
fc664744 | 312 | _assert(jni->PushLocalFrame(capacity) == 0); |
dbf05bfd JF |
313 | } |
314 | ||
fc664744 JF |
315 | ~CYJavaFrame() { |
316 | operator ()(NULL); | |
dbf05bfd JF |
317 | } |
318 | ||
4e2cc9d3 JF |
319 | operator JNIEnv *() const { |
320 | return jni_; | |
321 | } | |
322 | ||
fc664744 JF |
323 | jobject operator ()(jobject object) { |
324 | JNIEnv *jni(jni_); | |
325 | jni_ = NULL; | |
326 | return jni->PopLocalFrame(object); | |
dbf05bfd | 327 | } |
fc664744 | 328 | }; |
dbf05bfd | 329 | |
fc664744 JF |
330 | struct CYJavaEnv { |
331 | private: | |
332 | JNIEnv *jni; | |
333 | ||
334 | public: | |
335 | CYJavaEnv(JNIEnv *jni) : | |
336 | jni(jni) | |
dbf05bfd | 337 | { |
dbf05bfd JF |
338 | } |
339 | ||
477549f5 JF |
340 | template <typename Other_> |
341 | CYJavaEnv(const CYJavaRef<Other_> &value) : | |
342 | jni(value.jni()) | |
343 | { | |
344 | } | |
345 | ||
fc664744 JF |
346 | operator JNIEnv *() const { |
347 | return jni; | |
dbf05bfd JF |
348 | } |
349 | ||
fc664744 JF |
350 | JNIEnv *operator ->() const { |
351 | return jni; | |
dbf05bfd JF |
352 | } |
353 | ||
fc664744 JF |
354 | CYJavaLocal<jclass> FindClass(const char *name) const { |
355 | return {jni, _envcall(jni, FindClass(name))}; | |
dbf05bfd JF |
356 | } |
357 | ||
fc664744 JF |
358 | CYJavaLocal<jclass> GetObjectClass(jobject object) const { |
359 | return {jni, _envcall(jni, GetObjectClass(object))}; | |
360 | } | |
361 | ||
362 | CYJavaLocal<jclass> GetSuperclass(jclass _class) const { | |
363 | return {jni, _envcall(jni, GetSuperclass(_class))}; | |
364 | } | |
365 | ||
366 | CYJavaLocal<jobject> NewObject(jclass _class, jmethodID method, ...) const { | |
367 | va_list args; | |
368 | va_start(args, method); | |
369 | jobject object(_envcall(jni, NewObjectV(_class, method, args))); | |
370 | va_end(args); | |
371 | return {jni, object}; | |
372 | } | |
373 | ||
374 | CYJavaLocal<jobject> NewObjectA(jclass _class, jmethodID method, jvalue *args) const { | |
375 | return {jni, _envcall(jni, NewObjectA(_class, method, args))}; | |
dbf05bfd | 376 | } |
fc664744 JF |
377 | |
378 | CYJavaLocal<jstring> NewString(const jchar *data, jsize size) const { | |
379 | return {jni, _envcall(jni, NewString(data, size))}; | |
380 | } | |
381 | ||
382 | #define CYJavaEnv_(Code) \ | |
383 | template <typename... Args_> \ | |
384 | void Code(Args_ &&... args) const { \ | |
385 | _envcallv(jni, Code(cy::Forward<Args_>(args)...)); \ | |
386 | } | |
387 | ||
388 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
389 | CYJavaEnv_(Get ## Typ ## ArrayRegion) \ | |
390 | CYJavaEnv_(Set ## Typ ## Field) \ | |
391 | CYJavaEnv_(SetStatic ## Typ ## Field) | |
392 | CYJavaForEachPrimitive | |
393 | #undef CYJavaForEachPrimitive_ | |
394 | ||
395 | CYJavaEnv_(CallVoidMethod) | |
396 | CYJavaEnv_(CallStaticVoidMethod) | |
397 | CYJavaEnv_(CallVoidMethodA) | |
398 | CYJavaEnv_(CallStaticVoidMethodA) | |
399 | CYJavaEnv_(SetObjectArrayElement) | |
400 | CYJavaEnv_(SetObjectField) | |
401 | CYJavaEnv_(SetStaticObjectField) | |
402 | #undef CYJavaEnv_ | |
403 | ||
404 | #define CYJavaEnv_(Code) \ | |
405 | template <typename... Args_> \ | |
0e832de4 | 406 | auto Code(Args_ &&... args) const -> decltype(jni->Code(cy::Forward<Args_>(args)...)) { \ |
fc664744 JF |
407 | return _envcall(jni, Code(cy::Forward<Args_>(args)...)); \ |
408 | } | |
409 | ||
410 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
411 | CYJavaEnv_(Call ## Typ ## Method) \ | |
412 | CYJavaEnv_(CallStatic ## Typ ## Method) \ | |
413 | CYJavaEnv_(Call ## Typ ## MethodA) \ | |
414 | CYJavaEnv_(CallStatic ## Typ ## MethodA) \ | |
415 | CYJavaEnv_(Get ## Typ ## Field) \ | |
416 | CYJavaEnv_(GetStatic ## Typ ## Field) | |
417 | CYJavaForEachPrimitive | |
418 | #undef CYJavaForEachPrimitive_ | |
419 | ||
420 | CYJavaEnv_(FromReflectedField) | |
421 | CYJavaEnv_(FromReflectedMethod) | |
422 | CYJavaEnv_(GetArrayLength) | |
423 | CYJavaEnv_(GetMethodID) | |
424 | CYJavaEnv_(GetStaticMethodID) | |
425 | CYJavaEnv_(IsSameObject) | |
f2c357f9 | 426 | CYJavaEnv_(RegisterNatives) |
fc664744 JF |
427 | #undef CYJavaEnv_ |
428 | ||
429 | #define CYJavaEnv_(Code) \ | |
430 | template <typename Other_, typename... Args_> \ | |
431 | auto Code(Args_ &&... args) const -> CYJavaLocal<Other_> { \ | |
432 | return {jni, static_cast<Other_>(_envcall(jni, Code(cy::Forward<Args_>(args)...)))}; \ | |
433 | } | |
434 | ||
435 | CYJavaEnv_(CallObjectMethod) | |
436 | CYJavaEnv_(CallStaticObjectMethod) | |
437 | CYJavaEnv_(CallObjectMethodA) | |
438 | CYJavaEnv_(CallStaticObjectMethodA) | |
439 | CYJavaEnv_(GetObjectArrayElement) | |
440 | CYJavaEnv_(GetObjectField) | |
441 | CYJavaEnv_(GetStaticObjectField) | |
442 | #undef CYJavaEnv_ | |
dbf05bfd JF |
443 | }; |
444 | ||
fc664744 JF |
445 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaRef<jobject> &value) { |
446 | CYJavaEnv jni(value); | |
447 | auto Object$(jni.FindClass("java/lang/Object")); | |
448 | auto Object$toString(jni.GetMethodID(Object$, "toString", "()Ljava/lang/String;")); | |
449 | return jni.CallObjectMethod<jstring>(value, Object$toString); | |
450 | } | |
451 | ||
477549f5 JF |
452 | static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef<jobject> &value); |
453 | ||
454 | template <typename Other_> | |
455 | static _finline JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef<Other_> &value) { | |
456 | return CYCastJSValue(context, value.template cast<jobject>()); | |
457 | } | |
458 | ||
dbf05bfd JF |
459 | template <typename Type_> |
460 | static _finline JSValueRef CYJavaCastJSValue(JSContextRef context, Type_ value) { | |
461 | return CYCastJSValue(context, value); | |
462 | } | |
463 | ||
464 | static _finline JSValueRef CYJavaCastJSValue(JSContextRef context, jboolean value) { | |
465 | return CYCastJSValue(context, static_cast<bool>(value)); | |
466 | } | |
467 | ||
477549f5 JF |
468 | JSValueRef CYJavaError::CastJSValue(JSContextRef context, const char *name) const { |
469 | return CYCastJSValue(context, value_); | |
470 | } | |
471 | ||
dbf05bfd JF |
472 | static std::map<std::string, CYJavaPrimitive> Primitives_; |
473 | ||
fc664744 JF |
474 | static CYJavaPrimitive CYJavaGetPrimitive(JSContextRef context, const CYJavaRef<jclass> &type, jmethodID Class$get$$Name) { |
475 | CYJavaEnv jni(type); | |
476 | auto string(jni.CallObjectMethod<jstring>(type, Class$get$$Name)); | |
477 | _assert(string); | |
478 | ||
479 | CYJavaUTF8String name(string); | |
dbf05bfd JF |
480 | auto primitive(Primitives_.find(name)); |
481 | return primitive != Primitives_.end() ? primitive->second : CYJavaPrimitiveObject; | |
482 | } | |
483 | ||
484 | typedef std::vector<CYJavaPrimitive> CYJavaShorty; | |
485 | ||
fc664744 JF |
486 | static CYJavaShorty CYJavaGetShorty(JSContextRef context, const CYJavaRef<jobjectArray> &types, jmethodID Class$get$$Name) { |
487 | CYJavaEnv jni(types); | |
488 | size_t count(jni.GetArrayLength(types)); | |
dbf05bfd JF |
489 | CYJavaShorty shorty(count); |
490 | for (size_t index(0); index != count; ++index) | |
fc664744 | 491 | shorty[index] = CYJavaGetPrimitive(context, jni.GetObjectArrayElement<jclass>(types, index), Class$get$$Name); |
dbf05bfd JF |
492 | return shorty; |
493 | } | |
494 | ||
495 | struct CYJavaField { | |
496 | jfieldID field_; | |
497 | CYJavaPrimitive primitive_; | |
498 | }; | |
499 | ||
500 | typedef std::map<std::string, CYJavaField> CYJavaFieldMap; | |
501 | ||
502 | struct CYJavaSignature { | |
4b645e23 | 503 | CYJavaGlobal<jobject> reflected_; |
fc664744 | 504 | jmethodID method_; |
4b645e23 JF |
505 | CYJavaPrimitive primitive_; |
506 | CYJavaShorty shorty_; | |
507 | ||
fc664744 JF |
508 | CYJavaSignature(const CYJavaRef<jobject> &reflected, jmethodID method, CYJavaPrimitive primitive, const CYJavaShorty &shorty) : |
509 | reflected_(reflected), | |
4b645e23 | 510 | method_(method), |
4b645e23 JF |
511 | primitive_(primitive), |
512 | shorty_(shorty) | |
dbf05bfd JF |
513 | { |
514 | } | |
515 | ||
516 | CYJavaSignature(unsigned count) : | |
4b645e23 | 517 | shorty_(count) |
dbf05bfd JF |
518 | { |
519 | } | |
520 | ||
521 | bool operator <(const CYJavaSignature &rhs) const { | |
4b645e23 | 522 | return shorty_.size() < rhs.shorty_.size(); |
dbf05bfd JF |
523 | } |
524 | }; | |
525 | ||
526 | typedef std::multiset<CYJavaSignature> CYJavaOverload; | |
527 | ||
528 | struct CYJavaMethod : | |
d4222ffb | 529 | CYRoot |
dbf05bfd | 530 | { |
dbf05bfd JF |
531 | CYJavaOverload overload_; |
532 | ||
c9c16dde JF |
533 | CYJavaMethod(const CYJavaOverload &overload) : |
534 | overload_(overload) | |
535 | { | |
536 | } | |
537 | }; | |
538 | ||
539 | struct CYJavaStaticMethod : | |
d4222ffb | 540 | CYRoot |
c9c16dde JF |
541 | { |
542 | CYJavaOverload overload_; | |
543 | ||
544 | CYJavaStaticMethod(const CYJavaOverload &overload) : | |
dbf05bfd JF |
545 | overload_(overload) |
546 | { | |
547 | } | |
548 | }; | |
549 | ||
550 | struct CYJavaClass : | |
d4222ffb | 551 | CYRoot |
dbf05bfd | 552 | { |
d4222ffb | 553 | CYJavaGlobal<jclass> value_; |
c9c16dde JF |
554 | bool interface_; |
555 | ||
dbf05bfd JF |
556 | CYJavaFieldMap static_; |
557 | CYJavaFieldMap instance_; | |
558 | CYJavaOverload overload_; | |
559 | ||
fc664744 | 560 | CYJavaClass(const CYJavaRef<jclass> &value, bool interface) : |
d4222ffb | 561 | value_(value), |
c9c16dde | 562 | interface_(interface) |
dbf05bfd JF |
563 | { |
564 | } | |
565 | }; | |
566 | ||
fc664744 | 567 | static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef<jclass> &_class); |
8effd381 | 568 | |
dbf05bfd | 569 | struct CYJavaObject : |
d4222ffb | 570 | CYRoot |
dbf05bfd | 571 | { |
d4222ffb | 572 | CYJavaGlobal<jobject> value_; |
8effd381 JF |
573 | CYJavaClass *table_; |
574 | ||
fc664744 | 575 | CYJavaObject(const CYJavaRef<jobject> &value, CYJavaClass *table) : |
d4222ffb | 576 | value_(value), |
4b645e23 | 577 | table_(table) |
dbf05bfd JF |
578 | { |
579 | } | |
580 | ||
581 | JSValueRef GetPrototype(JSContextRef context) const; | |
582 | }; | |
583 | ||
8effd381 | 584 | struct CYJavaInterior : |
d4222ffb | 585 | CYRoot |
8effd381 | 586 | { |
d4222ffb | 587 | CYJavaGlobal<jobject> value_; |
8effd381 JF |
588 | CYJavaClass *table_; |
589 | ||
fc664744 | 590 | CYJavaInterior(const CYJavaRef<jobject> &value, CYJavaClass *table) : |
d4222ffb | 591 | value_(value), |
8effd381 JF |
592 | table_(table) |
593 | { | |
594 | } | |
595 | }; | |
596 | ||
c9c16dde | 597 | struct CYJavaStaticInterior : |
d4222ffb | 598 | CYRoot |
4b645e23 | 599 | { |
d4222ffb | 600 | CYJavaGlobal<jclass> value_; |
4b645e23 JF |
601 | CYJavaClass *table_; |
602 | ||
fc664744 | 603 | CYJavaStaticInterior(const CYJavaRef<jclass> &value, CYJavaClass *table) : |
d4222ffb | 604 | value_(value), |
4b645e23 JF |
605 | table_(table) |
606 | { | |
607 | } | |
608 | }; | |
609 | ||
610 | struct CYJavaArray : | |
d4222ffb | 611 | CYRoot |
4b645e23 | 612 | { |
d4222ffb | 613 | CYJavaGlobal<jarray> value_; |
4b645e23 JF |
614 | CYJavaPrimitive primitive_; |
615 | ||
fc664744 | 616 | CYJavaArray(const CYJavaRef<jarray> &value, CYJavaPrimitive primitive) : |
d4222ffb | 617 | value_(value), |
4b645e23 JF |
618 | primitive_(primitive) |
619 | { | |
620 | } | |
621 | ||
622 | JSValueRef GetPrototype(JSContextRef context) const; | |
623 | }; | |
624 | ||
dbf05bfd | 625 | struct CYJavaPackage : |
d4222ffb | 626 | CYRoot |
dbf05bfd | 627 | { |
f3e85e94 JF |
628 | JNIEnv *jni_; |
629 | ||
dbf05bfd JF |
630 | typedef std::vector<std::string> Path; |
631 | Path package_; | |
632 | ||
f3e85e94 JF |
633 | _finline CYJavaPackage(JNIEnv *jni, const Path &package) : |
634 | jni_(jni), | |
dbf05bfd JF |
635 | package_(package) |
636 | { | |
637 | } | |
638 | }; | |
639 | ||
dbf05bfd | 640 | JSValueRef CYJavaObject::GetPrototype(JSContextRef context) const { |
fc664744 JF |
641 | CYJavaEnv jni(value_); |
642 | return CYGetProperty(context, CYGetJavaClass(context, jni.GetObjectClass(value_)), prototype_s); | |
dbf05bfd JF |
643 | } |
644 | ||
4b645e23 JF |
645 | JSValueRef CYJavaArray::GetPrototype(JSContextRef context) const { |
646 | return CYGetCachedObject(context, CYJSString("Array_prototype")); | |
647 | } | |
648 | ||
fc664744 JF |
649 | static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef<jobject> &value) { |
650 | if (!value) | |
dbf05bfd | 651 | return CYJSNull(context); |
fc664744 JF |
652 | CYJavaEnv jni(value); |
653 | ||
654 | auto _class(jni.GetObjectClass(value)); | |
655 | if (jni.IsSameObject(_class, jni.FindClass("java/lang/String"))) | |
656 | return CYCastJSValue(context, CYJSString(value.cast<jstring>())); | |
657 | ||
658 | auto Class$(jni.FindClass("java/lang/Class")); | |
659 | auto Class$isArray(jni.GetMethodID(Class$, "isArray", "()Z")); | |
660 | if (jni.CallBooleanMethod(_class, Class$isArray)) { | |
661 | auto Class$getComponentType(jni.GetMethodID(Class$, "getComponentType", "()Ljava/lang/Class;")); | |
662 | auto component(jni.CallObjectMethod<jclass>(_class, Class$getComponentType)); | |
663 | auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); | |
d4222ffb | 664 | return CYPrivate<CYJavaArray>::Make(context, value.cast<jarray>(), CYJavaGetPrimitive(context, component, Class$getName)); |
c9c16dde JF |
665 | } |
666 | ||
fc664744 JF |
667 | auto Wrapper$(jni.FindClass("Cycript$Wrapper")); |
668 | if (jni.IsSameObject(_class, Wrapper$)) { | |
669 | auto Wrapper$getProtect(jni.GetMethodID(Wrapper$, "getProtect", "()J")); | |
670 | auto &protect(*reinterpret_cast<CYProtect *>(jni.CallLongMethod(value, Wrapper$getProtect))); | |
c9c16dde | 671 | return protect; |
4b645e23 JF |
672 | } |
673 | ||
fc664744 | 674 | CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(CYGetJavaClass(context, _class)))); |
d4222ffb | 675 | return CYPrivate<CYJavaObject>::Make(context, value, table); |
dbf05bfd JF |
676 | } |
677 | ||
fc664744 JF |
678 | static _finline JSObjectRef CYCastJSObject(JSContextRef context, const CYJavaRef<jobject> &value) { |
679 | return CYCastJSObject(context, CYCastJSValue(context, value)); | |
c9c16dde JF |
680 | } |
681 | ||
fc664744 JF |
682 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaEnv &jni, JSContextRef context, CYUTF16String value) { |
683 | return jni.NewString(value.data, value.size); | |
dbf05bfd JF |
684 | } |
685 | ||
fc664744 | 686 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaEnv &jni, JSContextRef context, JSStringRef value) { |
c9c16dde | 687 | return CYCastJavaString(jni, context, CYCastUTF16String(value)); |
dbf05bfd JF |
688 | } |
689 | ||
690 | #define CYCastJava$(T, Type, jtype, Cast) \ | |
fc664744 JF |
691 | _disused static CYJavaLocal<jobject> CYCastJava ## Type(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { \ |
692 | auto Type$(jni.FindClass("java/lang/" #Type)); \ | |
693 | auto Type$init$(jni.GetMethodID(Type$, "<init>", "(" #T ")V")); \ | |
694 | return jni.NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value))); \ | |
dbf05bfd JF |
695 | } |
696 | ||
697 | CYCastJava$(Z, Boolean, jboolean, CYCastBool) | |
698 | CYCastJava$(B, Byte, jbyte, CYCastDouble) | |
699 | CYCastJava$(C, Character, jchar, CYCastDouble) | |
700 | CYCastJava$(S, Short, jshort, CYCastDouble) | |
701 | CYCastJava$(I, Integer, jint, CYCastDouble) | |
702 | CYCastJava$(J, Long, jlong, CYCastDouble) | |
703 | CYCastJava$(F, Float, jfloat, CYCastDouble) | |
704 | CYCastJava$(D, Double, jdouble, CYCastDouble) | |
705 | ||
c9c16dde | 706 | static CYJavaClass *CYGetJavaTable(JSContextRef context, JSObjectRef object) { |
d4222ffb | 707 | if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaClass>::Class_)) |
c9c16dde JF |
708 | return NULL; |
709 | return reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)); | |
710 | } | |
711 | ||
712 | static CYJavaObject *CYGetJavaObject(JSContextRef context, JSObjectRef object) { | |
d4222ffb | 713 | if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaObject>::Class_)) |
c9c16dde JF |
714 | return NULL; |
715 | return reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)); | |
716 | } | |
717 | ||
fc664744 | 718 | static CYJavaLocal<jobject> CYCastJavaObject(const CYJavaEnv &jni, JSContextRef context, JSObjectRef value) { |
c9c16dde | 719 | if (CYJavaObject *internal = CYGetJavaObject(context, value)) |
dbf05bfd | 720 | return internal->value_; |
dbf05bfd | 721 | |
fc664744 JF |
722 | auto Wrapper$(jni.FindClass("Cycript$Wrapper")); |
723 | auto Wrapper$$init$(jni.GetMethodID(Wrapper$, "<init>", "(J)V")); | |
c9c16dde | 724 | CYProtect *protect(new CYProtect(context, value)); |
fc664744 | 725 | return jni.NewObject(Wrapper$, Wrapper$$init$, reinterpret_cast<jlong>(protect)); |
dbf05bfd JF |
726 | } |
727 | ||
fc664744 | 728 | static CYJavaLocal<jobject> CYCastJavaObject(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { |
dbf05bfd JF |
729 | switch (JSValueGetType(context, value)) { |
730 | case kJSTypeNull: | |
fc664744 | 731 | return {jni, NULL}; |
dbf05bfd JF |
732 | case kJSTypeBoolean: |
733 | return CYCastJavaBoolean(jni, context, value); | |
734 | case kJSTypeNumber: | |
735 | return CYCastJavaDouble(jni, context, value); | |
736 | case kJSTypeString: | |
c9c16dde | 737 | return CYCastJavaString(jni, context, CYJSString(context, value)); |
dbf05bfd JF |
738 | case kJSTypeObject: |
739 | return CYCastJavaObject(jni, context, CYCastJSObject(context, value)); | |
740 | ||
741 | case kJSTypeUndefined: | |
c9c16dde | 742 | // XXX: I am currently relying on this for dynamic proxy of void method |
fc664744 | 743 | return {jni, NULL}; |
dbf05bfd JF |
744 | default: |
745 | _assert(false); | |
746 | } | |
747 | } | |
748 | ||
fc664744 JF |
749 | static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef<jclass> &value) { |
750 | CYJavaEnv jni(value); | |
751 | CYJavaFrame frame(jni, 64); | |
752 | ||
dbf05bfd JF |
753 | JSObjectRef global(CYGetGlobalObject(context)); |
754 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
755 | ||
fc664744 JF |
756 | auto Class$(jni.FindClass("java/lang/Class")); |
757 | auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); | |
dbf05bfd | 758 | |
fc664744 | 759 | CYJSString name(jni.CallObjectMethod<jstring>(value, Class$getName)); |
dbf05bfd JF |
760 | JSValueRef cached(CYGetProperty(context, cy, name)); |
761 | if (!JSValueIsUndefined(context, cached)) | |
762 | return CYCastJSObject(context, cached); | |
763 | ||
fc664744 JF |
764 | JSObjectRef constructor; |
765 | JSObjectRef prototype; | |
c9c16dde | 766 | |
fc664744 | 767 | { |
dbf05bfd | 768 | |
fc664744 | 769 | auto Class$isInterface(jni.GetMethodID(Class$, "isInterface", "()Z")); |
dbf05bfd | 770 | |
fc664744 JF |
771 | auto Class$getDeclaredConstructors(jni.GetMethodID(Class$, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;")); |
772 | auto Class$getDeclaredFields(jni.GetMethodID(Class$, "getDeclaredFields", "()[Ljava/lang/reflect/Field;")); | |
773 | auto Class$getDeclaredMethods(jni.GetMethodID(Class$, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;")); | |
dbf05bfd | 774 | |
fc664744 JF |
775 | auto Constructor$(jni.FindClass("java/lang/reflect/Constructor")); |
776 | //auto Constructor$getModifiers(jni.GetMethodID(Constructor$, "getModifiers", "()I")); | |
777 | auto Constructor$getParameterTypes(jni.GetMethodID(Constructor$, "getParameterTypes", "()[Ljava/lang/Class;")); | |
dbf05bfd | 778 | |
fc664744 JF |
779 | auto Field$(jni.FindClass("java/lang/reflect/Field")); |
780 | auto Field$getModifiers(jni.GetMethodID(Field$, "getModifiers", "()I")); | |
781 | auto Field$getName(jni.GetMethodID(Field$, "getName", "()Ljava/lang/String;")); | |
782 | auto Field$getType(jni.GetMethodID(Field$, "getType", "()Ljava/lang/Class;")); | |
dbf05bfd | 783 | |
fc664744 JF |
784 | auto Method$(jni.FindClass("java/lang/reflect/Method")); |
785 | auto Method$getModifiers(jni.GetMethodID(Method$, "getModifiers", "()I")); | |
786 | auto Method$getName(jni.GetMethodID(Method$, "getName", "()Ljava/lang/String;")); | |
787 | auto Method$getParameterTypes(jni.GetMethodID(Method$, "getParameterTypes", "()[Ljava/lang/Class;")); | |
788 | auto Method$getReturnType(jni.GetMethodID(Method$, "getReturnType", "()Ljava/lang/Class;")); | |
dbf05bfd | 789 | |
fc664744 JF |
790 | auto Modifier$(jni.FindClass("java/lang/reflect/Modifier")); |
791 | auto Modifier$isStatic(jni.GetStaticMethodID(Modifier$, "isStatic", "(I)Z")); | |
dbf05bfd | 792 | |
fc664744 JF |
793 | auto interface(jni.CallBooleanMethod(value, Class$isInterface)); |
794 | auto table(new CYJavaClass(value, interface)); | |
795 | ||
796 | for (CYJavaLocal<jclass> prototype(value); prototype; prototype = jni.GetSuperclass(prototype)) { | |
797 | auto fields(jni.CallObjectMethod<jobjectArray>(prototype, Class$getDeclaredFields)); | |
798 | ||
799 | for (jsize i(0), e(jni.GetArrayLength(fields)); i != e; ++i) { | |
800 | auto field(jni.GetObjectArrayElement<jobject>(fields, e - i - 1)); | |
801 | auto modifiers(jni.CallIntMethod(field, Field$getModifiers)); | |
802 | auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); | |
dbf05bfd | 803 | auto &map(instance ? table->instance_ : table->static_); |
fc664744 JF |
804 | CYJavaUTF8String name(jni.CallObjectMethod<jstring>(field, Field$getName)); |
805 | auto id(jni.FromReflectedField(field)); | |
806 | auto type(jni.CallObjectMethod<jclass>(field, Field$getType)); | |
807 | map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(context, type, Class$getName)})); | |
dbf05bfd JF |
808 | } |
809 | } | |
810 | ||
d4222ffb | 811 | constructor = JSObjectMake(context, CYPrivate<CYJavaClass>::Class_, table); |
dbf05bfd | 812 | |
fc664744 | 813 | prototype = JSObjectMake(context, NULL, NULL); |
dbf05bfd JF |
814 | CYSetProperty(context, constructor, prototype_s, prototype, kJSPropertyAttributeDontEnum); |
815 | ||
fc664744 | 816 | auto constructors(jni.CallObjectMethod<jobjectArray>(value, Class$getDeclaredConstructors)); |
dbf05bfd | 817 | |
fc664744 JF |
818 | for (jsize i(0), e(jni.GetArrayLength(constructors)); i != e; ++i) { |
819 | auto constructor(jni.GetObjectArrayElement<jobject>(constructors, i)); | |
820 | auto parameters(jni.CallObjectMethod<jobjectArray>(constructor, Constructor$getParameterTypes)); | |
821 | CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); | |
822 | auto id(jni.FromReflectedMethod(constructor)); | |
823 | table->overload_.insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty)); | |
dbf05bfd JF |
824 | } |
825 | ||
fc664744 | 826 | auto methods(jni.CallObjectMethod<jobjectArray>(value, Class$getDeclaredMethods)); |
dbf05bfd JF |
827 | |
828 | std::map<std::pair<bool, std::string>, CYJavaOverload> entries; | |
829 | ||
fc664744 JF |
830 | for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) { |
831 | auto method(jni.GetObjectArrayElement<jobject>(methods, i)); | |
832 | auto modifiers(jni.CallIntMethod(method, Method$getModifiers)); | |
833 | auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); | |
834 | CYJavaUTF8String name(jni.CallObjectMethod<jstring>(method, Method$getName)); | |
835 | auto parameters(jni.CallObjectMethod<jobjectArray>(method, Method$getParameterTypes)); | |
836 | CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); | |
837 | auto type(jni.CallObjectMethod<jclass>(method, Method$getReturnType)); | |
838 | auto primitive(CYJavaGetPrimitive(context, type, Class$getName)); | |
839 | auto id(jni.FromReflectedMethod(method)); | |
840 | entries[std::make_pair(instance, std::string(name))].insert(CYJavaSignature(method, id, primitive, shorty)); | |
dbf05bfd JF |
841 | } |
842 | ||
843 | for (const auto &entry : entries) { | |
844 | bool instance(entry.first.first); | |
845 | CYJSString name(entry.first.second); | |
846 | auto &overload(entry.second); | |
c9c16dde | 847 | if (instance) |
d4222ffb | 848 | CYSetProperty(context, prototype, name, CYPrivate<CYJavaMethod>::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); |
c9c16dde | 849 | else |
d4222ffb | 850 | CYSetProperty(context, constructor, name, CYPrivate<CYJavaStaticMethod>::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); |
dbf05bfd JF |
851 | } |
852 | ||
fc664744 JF |
853 | } |
854 | ||
dbf05bfd JF |
855 | // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name |
856 | // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :( | |
857 | ||
fc664744 JF |
858 | if (auto super = jni.GetSuperclass(value)) { |
859 | JSObjectRef parent(CYGetJavaClass(context, super)); | |
4b645e23 | 860 | CYSetPrototype(context, constructor, parent); |
dbf05bfd JF |
861 | CYSetPrototype(context, prototype, CYGetProperty(context, parent, prototype_s)); |
862 | } | |
863 | ||
864 | CYSetProperty(context, cy, name, constructor); | |
865 | return constructor; | |
866 | } | |
867 | ||
4b645e23 JF |
868 | static void CYCastJavaNumeric(jvalue &value, CYJavaPrimitive primitive, JSContextRef context, JSValueRef argument) { |
869 | switch (primitive) { | |
870 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
871 | case CYJavaPrimitive ## Type: \ | |
872 | value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \ | |
873 | break; | |
dbf05bfd JF |
874 | CYJavaForEachPrimitive |
875 | #undef CYJavaForEachPrimitive_ | |
4b645e23 JF |
876 | default: |
877 | _assert(false); | |
878 | } | |
879 | } | |
880 | ||
4e2cc9d3 JF |
881 | static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jvalue *array) { |
882 | CYJavaEnv jni(frame); | |
883 | ||
4b645e23 JF |
884 | for (size_t index(0); index != shorty.size(); ++index) { |
885 | JSValueRef argument(arguments[index]); | |
886 | JSType type(JSValueGetType(context, argument)); | |
887 | jvalue &value(array[index]); | |
888 | ||
889 | switch (CYJavaPrimitive primitive = shorty[index]) { | |
890 | case CYJavaPrimitiveObject: | |
4e2cc9d3 | 891 | // XXX: figure out a way to tie this in to the CYJavaFrame |
fc664744 | 892 | value.l = CYCastJavaObject(jni, context, argument).leak(); |
4b645e23 JF |
893 | break; |
894 | ||
895 | case CYJavaPrimitiveBoolean: | |
896 | if (type != kJSTypeBoolean) | |
897 | return false; | |
898 | value.z = CYCastBool(context, argument); | |
899 | break; | |
900 | ||
901 | case CYJavaPrimitiveCharacter: | |
902 | if (type == kJSTypeNumber) | |
903 | CYCastJavaNumeric(value, primitive, context, argument); | |
904 | else if (type != kJSTypeString) | |
905 | return false; | |
906 | else { | |
907 | CYJSString string(context, argument); | |
908 | if (JSStringGetLength(string) != 1) | |
909 | return false; | |
910 | else | |
911 | value.c = JSStringGetCharactersPtr(string)[0]; | |
912 | } | |
913 | break; | |
914 | ||
915 | case CYJavaPrimitiveByte: | |
916 | case CYJavaPrimitiveShort: | |
917 | case CYJavaPrimitiveInteger: | |
918 | case CYJavaPrimitiveLong: | |
919 | case CYJavaPrimitiveFloat: | |
920 | case CYJavaPrimitiveDouble: | |
921 | if (type != kJSTypeNumber) | |
922 | return false; | |
923 | CYCastJavaNumeric(value, primitive, context, argument); | |
924 | break; | |
925 | ||
dbf05bfd JF |
926 | default: |
927 | _assert(false); | |
928 | } | |
dbf05bfd JF |
929 | } |
930 | ||
4b645e23 | 931 | return true; |
dbf05bfd JF |
932 | } |
933 | ||
934 | static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 935 | auto internal(CYPrivate<CYJavaMethod>::Get(context, object)); |
c9c16dde | 936 | CYJavaObject *self(CYGetJavaObject(context, _this)); |
447b698c | 937 | _assert(self != NULL); |
fc664744 | 938 | CYJavaEnv jni(self->value_); |
c9c16dde JF |
939 | |
940 | CYJavaSignature bound(count); | |
941 | for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { | |
4e2cc9d3 | 942 | CYJavaFrame frame(jni, count + 16); |
c9c16dde | 943 | jvalue array[count]; |
4e2cc9d3 | 944 | if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) |
c9c16dde | 945 | continue; |
fc664744 | 946 | jvalue *values(array); |
c9c16dde JF |
947 | switch (overload->primitive_) { |
948 | case CYJavaPrimitiveObject: | |
fc664744 | 949 | return CYCastJSValue(context, jni.CallObjectMethodA<jobject>(self->value_, overload->method_, values)); |
c9c16dde | 950 | case CYJavaPrimitiveVoid: |
fc664744 | 951 | jni.CallVoidMethodA(self->value_, overload->method_, values); |
c9c16dde JF |
952 | return CYJSUndefined(context); |
953 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
954 | case CYJavaPrimitive ## Type: \ | |
fc664744 | 955 | return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, overload->method_, values)); |
c9c16dde JF |
956 | CYJavaForEachPrimitive |
957 | #undef CYJavaForEachPrimitive_ | |
958 | default: _assert(false); | |
959 | } | |
960 | } | |
961 | ||
962 | CYThrow("invalid method call"); | |
963 | } CYCatch(NULL) } | |
964 | ||
965 | static JSValueRef JavaStaticMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 966 | auto internal(CYPrivate<CYJavaStaticMethod>::Get(context, object)); |
c9c16dde | 967 | CYJavaClass *table(CYGetJavaTable(context, _this)); |
fc664744 | 968 | CYJavaEnv jni(table->value_); |
dbf05bfd JF |
969 | |
970 | CYJavaSignature bound(count); | |
971 | for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { | |
4e2cc9d3 | 972 | CYJavaFrame frame(jni, count + 16); |
4b645e23 | 973 | jvalue array[count]; |
4e2cc9d3 | 974 | if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) |
4b645e23 | 975 | continue; |
fc664744 | 976 | jvalue *values(array); |
4b645e23 JF |
977 | switch (overload->primitive_) { |
978 | case CYJavaPrimitiveObject: | |
fc664744 | 979 | return CYCastJSValue(context, jni.CallStaticObjectMethodA<jobject>(table->value_, overload->method_, values)); |
c9c16dde | 980 | case CYJavaPrimitiveVoid: |
fc664744 | 981 | jni.CallStaticVoidMethodA(table->value_, overload->method_, values); |
c9c16dde | 982 | return CYJSUndefined(context); |
4b645e23 JF |
983 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
984 | case CYJavaPrimitive ## Type: \ | |
fc664744 | 985 | return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, overload->method_, values)); |
4b645e23 JF |
986 | CYJavaForEachPrimitive |
987 | #undef CYJavaForEachPrimitive_ | |
988 | default: _assert(false); | |
989 | } | |
dbf05bfd JF |
990 | } |
991 | ||
992 | CYThrow("invalid method call"); | |
993 | } CYCatch(NULL) } | |
994 | ||
995 | static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 996 | auto table(CYPrivate<CYJavaClass>::Get(context, object)); |
fc664744 | 997 | CYJavaEnv jni(table->value_); |
c9c16dde JF |
998 | jclass _class(table->value_); |
999 | ||
1000 | if (table->interface_ && count == 1) { | |
fc664744 | 1001 | auto Cycript$(jni.FindClass("Cycript")); |
0e832de4 JF |
1002 | auto Cycript$Make(jni.GetStaticMethodID(Cycript$, "proxy", "(Ljava/lang/Class;LCycript$Wrapper;)Ljava/lang/Object;")); |
1003 | return CYCastJSObject(context, jni.CallObjectMethod<jobject>(Cycript$, Cycript$Make, _class, CYCastJavaObject(jni, context, CYCastJSObject(context, arguments[0])).get())); | |
c9c16dde | 1004 | } |
dbf05bfd | 1005 | |
dbf05bfd JF |
1006 | CYJavaSignature bound(count); |
1007 | for (auto overload(table->overload_.lower_bound(bound)), e(table->overload_.upper_bound(bound)); overload != e; ++overload) { | |
4e2cc9d3 | 1008 | CYJavaFrame frame(jni, count + 16); |
4b645e23 | 1009 | jvalue array[count]; |
4e2cc9d3 | 1010 | if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) |
4b645e23 | 1011 | continue; |
fc664744 JF |
1012 | jvalue *values(array); |
1013 | auto object(jni.NewObjectA(_class, overload->method_, values)); | |
1014 | return CYCastJSObject(context, object); | |
dbf05bfd JF |
1015 | } |
1016 | ||
1017 | CYThrow("invalid constructor call"); | |
77dd5db9 | 1018 | } CYCatchObject() } |
dbf05bfd | 1019 | |
c9c16dde | 1020 | static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
d4222ffb | 1021 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1022 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1023 | CYPool pool; |
1024 | auto name(CYPoolUTF8String(pool, context, property)); | |
1025 | auto field(table->static_.find(name)); | |
1026 | if (field == table->static_.end()) | |
1027 | return false; | |
1028 | return true; | |
1029 | } | |
1030 | ||
c9c16dde | 1031 | static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1032 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1033 | CYJavaClass *table(internal->table_); |
fc664744 | 1034 | CYJavaEnv jni(table->value_); |
dbf05bfd JF |
1035 | CYPool pool; |
1036 | auto name(CYPoolUTF8String(pool, context, property)); | |
1037 | auto field(table->static_.find(name)); | |
1038 | if (field == table->static_.end()) | |
1039 | return NULL; | |
1040 | ||
1041 | switch (field->second.primitive_) { | |
1042 | case CYJavaPrimitiveObject: | |
fc664744 | 1043 | return CYCastJSValue(context, jni.GetStaticObjectField<jobject>(table->value_, field->second.field_)); |
4b645e23 | 1044 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1045 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1046 | return CYJavaCastJSValue(context, jni.GetStatic ## Typ ## Field(table->value_, field->second.field_)); |
dbf05bfd JF |
1047 | CYJavaForEachPrimitive |
1048 | #undef CYJavaForEachPrimitive_ | |
1049 | default: _assert(false); | |
1050 | } | |
1051 | } CYCatch(NULL) } | |
1052 | ||
c9c16dde | 1053 | static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { |
d4222ffb | 1054 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1055 | CYJavaClass *table(internal->table_); |
fc664744 | 1056 | CYJavaEnv jni(table->value_); |
dbf05bfd JF |
1057 | CYPool pool; |
1058 | auto name(CYPoolUTF8String(pool, context, property)); | |
1059 | auto field(table->static_.find(name)); | |
1060 | if (field == table->static_.end()) | |
1061 | return false; | |
1062 | ||
1063 | switch (field->second.primitive_) { | |
1064 | case CYJavaPrimitiveObject: | |
fc664744 | 1065 | jni.SetStaticObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value)); |
4b645e23 | 1066 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1067 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1068 | jni.SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \ |
dbf05bfd JF |
1069 | break; |
1070 | CYJavaForEachPrimitive | |
1071 | #undef CYJavaForEachPrimitive_ | |
1072 | default: _assert(false); | |
1073 | } | |
1074 | ||
1075 | return true; | |
1076 | } CYCatch(false) } | |
1077 | ||
c9c16dde | 1078 | static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
d4222ffb | 1079 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1080 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1081 | for (const auto &field : table->static_) |
1082 | JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); | |
1083 | } | |
1084 | ||
1085 | static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1086 | auto table(CYPrivate<CYJavaClass>::Get(context, object)); |
fc664744 | 1087 | return CYCastJSValue(context, table->value_); |
dbf05bfd JF |
1088 | } CYCatch(NULL) } |
1089 | ||
8effd381 | 1090 | static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
d4222ffb | 1091 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
8effd381 | 1092 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1093 | CYPool pool; |
1094 | auto name(CYPoolUTF8String(pool, context, property)); | |
1095 | auto field(table->instance_.find(name)); | |
1096 | if (field == table->instance_.end()) | |
1097 | return false; | |
1098 | return true; | |
1099 | } | |
1100 | ||
8effd381 | 1101 | static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1102 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
fc664744 | 1103 | CYJavaEnv jni(internal->value_); |
8effd381 | 1104 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1105 | CYPool pool; |
1106 | auto name(CYPoolUTF8String(pool, context, property)); | |
1107 | auto field(table->instance_.find(name)); | |
1108 | if (field == table->instance_.end()) | |
1109 | return NULL; | |
1110 | ||
1111 | switch (field->second.primitive_) { | |
1112 | case CYJavaPrimitiveObject: | |
fc664744 | 1113 | return CYCastJSValue(context, jni.GetObjectField<jobject>(internal->value_, field->second.field_)); |
4b645e23 | 1114 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1115 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1116 | return CYJavaCastJSValue(context, jni.Get ## Typ ## Field(internal->value_, field->second.field_)); |
dbf05bfd JF |
1117 | CYJavaForEachPrimitive |
1118 | #undef CYJavaForEachPrimitive_ | |
1119 | default: _assert(false); | |
1120 | } | |
1121 | } CYCatch(NULL) } | |
1122 | ||
8effd381 | 1123 | static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { |
d4222ffb | 1124 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
fc664744 | 1125 | CYJavaEnv jni(internal->value_); |
8effd381 | 1126 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1127 | CYPool pool; |
1128 | auto name(CYPoolUTF8String(pool, context, property)); | |
1129 | auto field(table->instance_.find(name)); | |
1130 | if (field == table->instance_.end()) | |
1131 | return false; | |
1132 | ||
1133 | switch (field->second.primitive_) { | |
1134 | case CYJavaPrimitiveObject: | |
fc664744 | 1135 | jni.SetObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value)); |
4b645e23 | 1136 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1137 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1138 | jni.Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \ |
dbf05bfd JF |
1139 | break; |
1140 | CYJavaForEachPrimitive | |
1141 | #undef CYJavaForEachPrimitive_ | |
1142 | default: _assert(false); | |
1143 | } | |
1144 | ||
1145 | return true; | |
1146 | } CYCatch(false) } | |
1147 | ||
8effd381 | 1148 | static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
d4222ffb | 1149 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
8effd381 | 1150 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1151 | for (const auto &field : table->instance_) |
1152 | JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); | |
1153 | } | |
1154 | ||
1155 | static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1156 | auto internal(CYPrivate<CYJavaObject>::Get(context, object)); |
fc664744 JF |
1157 | CYJavaEnv jni(internal->value_); |
1158 | return CYGetJavaClass(context, jni.GetObjectClass(internal->value_)); | |
dbf05bfd JF |
1159 | } CYCatch(NULL) } |
1160 | ||
4b645e23 | 1161 | static JSValueRef JavaClass_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb JF |
1162 | auto internal(CYPrivate<CYJavaClass>::Get(context, object)); |
1163 | return CYPrivate<CYJavaStaticInterior>::Make(context, internal->value_, internal); | |
4b645e23 JF |
1164 | } CYCatch(NULL) } |
1165 | ||
8effd381 | 1166 | static JSValueRef JavaObject_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb JF |
1167 | auto internal(CYPrivate<CYJavaObject>::Get(context, object)); |
1168 | return CYPrivate<CYJavaInterior>::Make(context, internal->value_, internal->table_); | |
8effd381 JF |
1169 | } CYCatch(NULL) } |
1170 | ||
4b645e23 | 1171 | static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
d4222ffb | 1172 | auto internal(CYPrivate<CYJavaClass>::Get(context, _this)); |
fc664744 JF |
1173 | CYJavaEnv jni(internal->value_); |
1174 | auto Class$(jni.FindClass("java/lang/Class")); | |
1175 | auto Class$getCanonicalName(jni.GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;")); | |
1176 | return CYCastJSValue(context, CYJSString(jni.CallObjectMethod<jstring>(internal->value_, Class$getCanonicalName))); | |
4b645e23 JF |
1177 | } CYCatch(NULL) } |
1178 | ||
1179 | static JSValueRef JavaMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1180 | std::ostringstream cyon; | |
1181 | return CYCastJSValue(context, CYJSString(cyon.str())); | |
1182 | } CYCatch(NULL) } | |
1183 | ||
c9c16dde JF |
1184 | static JSValueRef JavaStaticMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1185 | std::ostringstream cyon; | |
1186 | return CYCastJSValue(context, CYJSString(cyon.str())); | |
1187 | } CYCatch(NULL) } | |
1188 | ||
4b645e23 | 1189 | static JSValueRef JavaArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1190 | auto internal(CYPrivate<CYJavaArray>::Get(context, object)); |
fc664744 | 1191 | CYJavaEnv jni(internal->value_); |
4b645e23 | 1192 | if (JSStringIsEqual(property, length_s)) |
fc664744 | 1193 | return CYCastJSValue(context, jni.GetArrayLength(internal->value_)); |
4b645e23 JF |
1194 | |
1195 | CYPool pool; | |
1196 | ssize_t offset; | |
1197 | if (!CYGetOffset(pool, context, property, offset)) | |
1198 | return NULL; | |
1199 | ||
1200 | if (internal->primitive_ == CYJavaPrimitiveObject) | |
fc664744 | 1201 | return CYCastJSValue(context, jni.GetObjectArrayElement<jobject>(static_cast<jobjectArray>(internal->value_.value_), offset)); |
4b645e23 JF |
1202 | else switch (internal->primitive_) { |
1203 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
1204 | case CYJavaPrimitive ## Type: { \ | |
1205 | j ## type element; \ | |
fc664744 | 1206 | jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \ |
4b645e23 JF |
1207 | return CYJavaCastJSValue(context, element); \ |
1208 | } break; | |
1209 | CYJavaForEachPrimitive | |
1210 | #undef CYJavaForEachPrimitive_ | |
1211 | default: _assert(false); | |
1212 | } | |
1213 | } CYCatch(NULL) } | |
1214 | ||
1215 | static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
d4222ffb | 1216 | auto internal(CYPrivate<CYJavaArray>::Get(context, object)); |
fc664744 | 1217 | CYJavaEnv jni(internal->value_); |
4b645e23 JF |
1218 | |
1219 | CYPool pool; | |
1220 | ssize_t offset; | |
1221 | if (!CYGetOffset(pool, context, property, offset)) | |
1222 | return false; | |
1223 | ||
1224 | if (internal->primitive_ == CYJavaPrimitiveObject) | |
fc664744 | 1225 | jni.SetObjectArrayElement(static_cast<jobjectArray>(internal->value_.value_), offset, CYCastJavaObject(jni, context, value)); |
4b645e23 JF |
1226 | else switch (internal->primitive_) { |
1227 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
1228 | case CYJavaPrimitive ## Type: { \ | |
1229 | j ## type element; \ | |
fc664744 | 1230 | jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \ |
4b645e23 JF |
1231 | return CYJavaCastJSValue(context, element); \ |
1232 | } break; | |
1233 | CYJavaForEachPrimitive | |
1234 | #undef CYJavaForEachPrimitive_ | |
1235 | default: _assert(false); | |
1236 | } | |
1237 | ||
1238 | return true; | |
1239 | } CYCatch(false) } | |
1240 | ||
f45374f4 JF |
1241 | static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env); |
1242 | ||
4b645e23 | 1243 | static JSValueRef JavaPackage_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
d4222ffb | 1244 | auto internal(CYPrivate<CYJavaPackage>::Get(context, _this)); |
4b645e23 JF |
1245 | std::ostringstream name; |
1246 | for (auto &package : internal->package_) | |
1247 | name << package << '.'; | |
1248 | name << '*'; | |
1249 | return CYCastJSValue(context, CYJSString(name.str())); | |
1250 | } CYCatch(NULL) } | |
1251 | ||
dbf05bfd JF |
1252 | static bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
1253 | return true; | |
1254 | } | |
1255 | ||
1256 | static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1257 | auto internal(CYPrivate<CYJavaPackage>::Get(context, object)); |
dbf05bfd JF |
1258 | CYJavaPackage::Path package(internal->package_); |
1259 | ||
1260 | CYPool pool; | |
1261 | const char *next(CYPoolCString(pool, context, property)); | |
1262 | ||
1263 | std::ostringstream name; | |
1264 | for (auto &package : internal->package_) | |
1265 | name << package << '/'; | |
1266 | name << next; | |
1267 | ||
f45374f4 JF |
1268 | if (internal->jni_ == NULL) |
1269 | GetJNI(context, internal->jni_); | |
f3e85e94 | 1270 | JNIEnv *jni(internal->jni_); |
f45374f4 | 1271 | |
fc664744 JF |
1272 | if (auto _class = jni->FindClass(name.str().c_str())) |
1273 | return CYGetJavaClass(context, CYJavaLocal<jclass>(jni, _class)); | |
dbf05bfd JF |
1274 | jni->ExceptionClear(); |
1275 | ||
1276 | package.push_back(next); | |
d4222ffb | 1277 | return CYPrivate<CYJavaPackage>::Make(context, jni, package); |
dbf05bfd JF |
1278 | } CYCatch(NULL) } |
1279 | ||
fc664744 | 1280 | static void Cycript_delete(JNIEnv *env, jclass api, jlong jprotect) { CYJavaTry { |
824bc1ec | 1281 | delete &protect; |
c9c16dde JF |
1282 | } CYJavaCatch() } |
1283 | ||
fc664744 JF |
1284 | static jobject Cycript_handle(JNIEnv *env, jclass api, jlong jprotect, jstring property, jobjectArray jarguments) { CYJavaTry { |
1285 | JSValueRef function(CYGetProperty(context, object, CYJSString(CYJavaRef<jstring>(jni, property)))); | |
c9c16dde JF |
1286 | if (JSValueIsUndefined(context, function)) |
1287 | return NULL; | |
1288 | ||
fc664744 | 1289 | size_t count(jarguments == NULL ? 0 : jni.GetArrayLength(jarguments)); |
c9c16dde | 1290 | JSValueRef arguments[count]; |
4e2cc9d3 | 1291 | for (size_t index(0); index != count; ++index) |
fc664744 | 1292 | arguments[index] = CYCastJSValue(context, jni.GetObjectArrayElement<jobject>(jarguments, index)); |
c9c16dde | 1293 | |
4e2cc9d3 | 1294 | return CYCastJavaObject(jni, context, CYCallAsFunction(context, CYCastJSObject(context, function), object, count, arguments)).leak(); |
c9c16dde JF |
1295 | } CYJavaCatch(NULL) } |
1296 | ||
1297 | static JNINativeMethod Cycript_[] = { | |
1298 | {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete}, | |
1299 | {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle}, | |
1300 | }; | |
1301 | ||
f2c357f9 JF |
1302 | template <typename Type_> |
1303 | static _finline void dlset(Type_ &function, const char *name, void *handle) { | |
1304 | function = reinterpret_cast<Type_>(dlsym(handle, name)); | |
1305 | } | |
1306 | ||
1307 | jint CYJavaVersion(JNI_VERSION_1_4); | |
1308 | ||
f3e85e94 | 1309 | static JavaVM *CYGetJavaVM(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) { |
f2c357f9 JF |
1310 | jsize capacity(16); |
1311 | JavaVM *jvms[capacity]; | |
1312 | jsize size; | |
1313 | _jnicall($JNI_GetCreatedJavaVMs(jvms, capacity, &size)); | |
1314 | if (size == 0) | |
1315 | return NULL; | |
f3e85e94 | 1316 | return jvms[0]; |
f2c357f9 JF |
1317 | } |
1318 | ||
f3e85e94 | 1319 | static JavaVM *CYGetJavaVM(JSContextRef context) { |
f2c357f9 JF |
1320 | CYPool pool; |
1321 | void *handle(RTLD_DEFAULT); | |
1322 | std::string library; | |
fc664744 | 1323 | |
f2c357f9 JF |
1324 | jint (*$JNI_GetCreatedJavaVMs)(JavaVM **jvms, jsize capacity, jsize *size); |
1325 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); | |
1326 | ||
1327 | if ($JNI_GetCreatedJavaVMs != NULL) { | |
f3e85e94 JF |
1328 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1329 | return jvm; | |
fc664744 | 1330 | } else { |
f2c357f9 JF |
1331 | std::vector<const char *> guesses; |
1332 | ||
1333 | #ifdef __ANDROID__ | |
1334 | char android[PROP_VALUE_MAX]; | |
1335 | if (__system_property_get("persist.sys.dalvik.vm.lib", android) != 0) | |
1336 | guesses.push_back(android); | |
1337 | #endif | |
1338 | ||
f3e85e94 JF |
1339 | guesses.push_back("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib"); |
1340 | //guesses.push_back("/System/Library/Frameworks/JavaVM.framework/JavaVM"); | |
4b5dc402 | 1341 | guesses.push_back("libjvm.dylib"); |
f3e85e94 | 1342 | |
f2c357f9 JF |
1343 | guesses.push_back("libart.so"); |
1344 | guesses.push_back("libdvm.so"); | |
1345 | guesses.push_back("libjvm.so"); | |
1346 | ||
1347 | for (const char *guess : guesses) { | |
1348 | handle = dlopen(guess, RTLD_LAZY | RTLD_GLOBAL); | |
1349 | if (handle != NULL) { | |
1350 | library = guess; | |
1351 | break; | |
1352 | } | |
fc664744 JF |
1353 | } |
1354 | ||
f3e85e94 JF |
1355 | if (library.size() == 0) |
1356 | return NULL; | |
f2c357f9 JF |
1357 | |
1358 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); | |
f3e85e94 JF |
1359 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1360 | return jvm; | |
fc664744 JF |
1361 | } |
1362 | ||
f2c357f9 JF |
1363 | std::vector<JavaVMOption> options; |
1364 | ||
1365 | { | |
1366 | std::ostringstream option; | |
1367 | option << "-Djava.class.path="; | |
1368 | option << CYPoolLibraryPath(pool) << "/libcycript.jar"; | |
1369 | if (const char *classpath = getenv("CLASSPATH")) | |
1370 | option << ':' << classpath; | |
1371 | options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL}); | |
1372 | } | |
1373 | ||
1374 | // To use libnativehelper to access JNI_GetCreatedJavaVMs, you need JniInvocation. | |
1375 | // ...but there can only be one JniInvocation, and assuradely the other VM has it. | |
1376 | // Essentially, this API makes no sense. We need it for AndroidRuntime, though :/. | |
1377 | ||
1378 | if (void *libnativehelper = dlopen("libnativehelper.so", RTLD_LAZY | RTLD_GLOBAL)) { | |
1379 | class JniInvocation$; | |
1380 | JniInvocation$ *(*JniInvocation$$init$)(JniInvocation$ *self)(NULL); | |
1381 | bool (*JniInvocation$Init)(JniInvocation$ *self, const char *library)(NULL); | |
1382 | JniInvocation$ *(*JniInvocation$finalize)(JniInvocation$ *self)(NULL); | |
1383 | ||
1384 | dlset(JniInvocation$$init$, "_ZN13JniInvocationC1Ev", libnativehelper); | |
1385 | dlset(JniInvocation$Init, "_ZN13JniInvocation4InitEPKc", libnativehelper); | |
1386 | dlset(JniInvocation$finalize, "_ZN13JniInvocationD1Ev", libnativehelper); | |
1387 | ||
95be1645 JF |
1388 | if (JniInvocation$$init$ == NULL) |
1389 | dlclose(libnativehelper); | |
1390 | else { | |
1391 | // XXX: we should attach a pool to the VM itself and deallocate this there | |
1392 | //auto invocation(pool.calloc<JniInvocation$>(1, 1024)); | |
1393 | //_assert(JniInvocation$finalize != NULL); | |
1394 | //pool.atexit(reinterpret_cast<void (*)(void *)>(JniInvocation$finalize), invocation); | |
1395 | ||
1396 | auto invocation(static_cast<JniInvocation$ *>(calloc(1, 1024))); | |
1397 | JniInvocation$$init$(invocation); | |
1398 | ||
1399 | _assert(JniInvocation$Init != NULL); | |
1400 | JniInvocation$Init(invocation, NULL); | |
1401 | ||
1402 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", libnativehelper); | |
f3e85e94 JF |
1403 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1404 | return jvm; | |
95be1645 | 1405 | } |
f2c357f9 JF |
1406 | } |
1407 | ||
f3e85e94 JF |
1408 | JavaVM *jvm; |
1409 | JNIEnv *env; | |
1410 | ||
f2c357f9 JF |
1411 | if (void *libandroid_runtime = dlopen("libandroid_runtime.so", RTLD_LAZY | RTLD_GLOBAL)) { |
1412 | class AndroidRuntime$; | |
1413 | AndroidRuntime$ *(*AndroidRuntime$$init$)(AndroidRuntime$ *self, char *args, unsigned int size)(NULL); | |
f3e85e94 JF |
1414 | int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **env)(NULL); |
1415 | int (*AndroidRuntime$startReg)(JNIEnv *env)(NULL); | |
f2c357f9 JF |
1416 | int (*AndroidRuntime$addOption)(AndroidRuntime$ *self, const char *option, void *extra)(NULL); |
1417 | int (*AndroidRuntime$addVmArguments)(AndroidRuntime$ *self, int, const char *const argv[])(NULL); | |
1418 | AndroidRuntime$ *(*AndroidRuntime$finalize)(AndroidRuntime$ *self)(NULL); | |
1419 | ||
1420 | dlset(AndroidRuntime$$init$, "_ZN7android14AndroidRuntimeC1EPcj", libandroid_runtime); | |
1421 | dlset(AndroidRuntime$startVm, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnv", libandroid_runtime); | |
1422 | dlset(AndroidRuntime$startReg, "_ZN7android14AndroidRuntime8startRegEP7_JNIEnv", libandroid_runtime); | |
1423 | dlset(AndroidRuntime$addOption, "_ZN7android14AndroidRuntime9addOptionEPKcPv", libandroid_runtime); | |
1424 | dlset(AndroidRuntime$addVmArguments, "_ZN7android14AndroidRuntime14addVmArgumentsEiPKPKc", libandroid_runtime); | |
1425 | dlset(AndroidRuntime$finalize, "_ZN7android14AndroidRuntimeD1Ev", libandroid_runtime); | |
1426 | ||
1427 | // XXX: it would also be interesting to attach this to a global pool | |
1428 | AndroidRuntime$ *runtime(pool.calloc<AndroidRuntime$>(1, 1024)); | |
1429 | ||
1430 | _assert(AndroidRuntime$$init$ != NULL); | |
1431 | AndroidRuntime$$init$(runtime, NULL, 0); | |
1432 | ||
1433 | if (AndroidRuntime$addOption == NULL) { | |
1434 | _assert(AndroidRuntime$addVmArguments != NULL); | |
1435 | std::vector<const char *> arguments; | |
1436 | for (const auto &option : options) | |
1437 | arguments.push_back(option.optionString); | |
1438 | AndroidRuntime$addVmArguments(runtime, arguments.size(), arguments.data()); | |
1439 | } else for (const auto &option : options) | |
1440 | AndroidRuntime$addOption(runtime, option.optionString, option.extraInfo); | |
1441 | ||
1442 | int failure; | |
1443 | ||
1444 | _assert(AndroidRuntime$startVm != NULL); | |
f3e85e94 | 1445 | failure = AndroidRuntime$startVm(runtime, &jvm, &env); |
f2c357f9 JF |
1446 | _assert(failure == 0); |
1447 | ||
1448 | _assert(AndroidRuntime$startReg != NULL); | |
f3e85e94 | 1449 | failure = AndroidRuntime$startReg(env); |
f2c357f9 JF |
1450 | _assert(failure == 0); |
1451 | ||
f3e85e94 | 1452 | return jvm; |
f2c357f9 JF |
1453 | } |
1454 | ||
1455 | jint (*$JNI_CreateJavaVM)(JavaVM **jvm, void **, void *); | |
1456 | dlset($JNI_CreateJavaVM, "JNI_CreateJavaVM", handle); | |
1457 | ||
1458 | JavaVMInitArgs args; | |
1459 | memset(&args, 0, sizeof(args)); | |
1460 | args.version = CYJavaVersion; | |
1461 | args.nOptions = options.size(); | |
1462 | args.options = options.data(); | |
f3e85e94 JF |
1463 | _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&env), &args)); |
1464 | return jvm; | |
f2c357f9 | 1465 | } |
fc664744 | 1466 | |
f45374f4 | 1467 | static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env) { |
f3e85e94 | 1468 | auto jvm(CYGetJavaVM(context)); |
f45374f4 | 1469 | _assert(jvm != NULL); |
f3e85e94 JF |
1470 | _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&env), CYJavaVersion)); |
1471 | CYJavaEnv jni(env); | |
1472 | ||
f45374f4 JF |
1473 | // XXX: this happens once per stub :/ |
1474 | ||
f2c357f9 JF |
1475 | auto Cycript$(jni.FindClass("Cycript")); |
1476 | jni.RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0])); | |
f3e85e94 | 1477 | |
f45374f4 JF |
1478 | JSObjectRef java(CYGetCachedObject(context, CYJSString("Java"))); |
1479 | JSValueRef arguments[1]; | |
1480 | arguments[0] = CYCastJSValue(context, CYJSString("setup")); | |
1481 | CYCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, java, CYJSString("emit"))), java, 1, arguments); | |
1482 | ||
f3e85e94 | 1483 | return env; |
c9c16dde JF |
1484 | } |
1485 | ||
4b645e23 | 1486 | static JSStaticValue JavaClass_staticValues[3] = { |
dbf05bfd | 1487 | {"class", &JavaClass_getProperty_class, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
4b645e23 | 1488 | {"$cyi", &JavaClass_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
dbf05bfd JF |
1489 | {NULL, NULL, NULL, 0} |
1490 | }; | |
1491 | ||
4b645e23 JF |
1492 | static JSStaticFunction JavaClass_staticFunctions[2] = { |
1493 | {"toCYON", &JavaClass_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1494 | {NULL, NULL, 0} | |
1495 | }; | |
1496 | ||
8effd381 | 1497 | static JSStaticValue JavaObject_staticValues[3] = { |
dbf05bfd | 1498 | {"constructor", &JavaObject_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
8effd381 | 1499 | {"$cyi", &JavaObject_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
dbf05bfd JF |
1500 | {NULL, NULL, NULL, 0} |
1501 | }; | |
1502 | ||
4b645e23 JF |
1503 | static JSStaticFunction JavaMethod_staticFunctions[2] = { |
1504 | {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1505 | {NULL, NULL, 0} | |
1506 | }; | |
1507 | ||
c9c16dde JF |
1508 | static JSStaticFunction JavaStaticMethod_staticFunctions[2] = { |
1509 | {"toCYON", &JavaStaticMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1510 | {NULL, NULL, 0} | |
1511 | }; | |
1512 | ||
4b645e23 JF |
1513 | static JSStaticFunction JavaPackage_staticFunctions[2] = { |
1514 | {"toCYON", &JavaPackage_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
dbf05bfd JF |
1515 | {NULL, NULL, 0} |
1516 | }; | |
1517 | ||
1518 | void CYJava_Initialize() { | |
1519 | Primitives_.insert(std::make_pair("void", CYJavaPrimitiveVoid)); | |
4b645e23 | 1520 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
42619b59 JF |
1521 | Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type)); |
1522 | CYJavaForEachPrimitive | |
1523 | #undef CYJavaForEachPrimitive_ | |
dbf05bfd JF |
1524 | |
1525 | JSClassDefinition definition; | |
1526 | ||
1527 | definition = kJSClassDefinitionEmpty; | |
1528 | definition.className = "JavaClass"; | |
1529 | definition.staticValues = JavaClass_staticValues; | |
4b645e23 | 1530 | definition.staticFunctions = JavaClass_staticFunctions; |
dbf05bfd JF |
1531 | definition.callAsConstructor = &JavaClass_callAsConstructor; |
1532 | definition.finalize = &CYFinalize; | |
d4222ffb | 1533 | CYPrivate<CYJavaClass>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1534 | |
8effd381 JF |
1535 | definition = kJSClassDefinitionEmpty; |
1536 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
1537 | definition.className = "JavaInterior"; | |
1538 | definition.hasProperty = &JavaInterior_hasProperty; | |
1539 | definition.getProperty = &JavaInterior_getProperty; | |
1540 | definition.setProperty = &JavaInterior_setProperty; | |
1541 | definition.getPropertyNames = &JavaInterior_getPropertyNames; | |
1542 | definition.finalize = &CYFinalize; | |
d4222ffb | 1543 | CYPrivate<CYJavaInterior>::Class_ = JSClassCreate(&definition); |
8effd381 | 1544 | |
dbf05bfd JF |
1545 | definition = kJSClassDefinitionEmpty; |
1546 | definition.className = "JavaMethod"; | |
4b645e23 | 1547 | definition.staticFunctions = JavaMethod_staticFunctions; |
dbf05bfd JF |
1548 | definition.callAsFunction = &JavaMethod_callAsFunction; |
1549 | definition.finalize = &CYFinalize; | |
d4222ffb | 1550 | CYPrivate<CYJavaMethod>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1551 | |
c9c16dde JF |
1552 | definition = kJSClassDefinitionEmpty; |
1553 | definition.className = "JavaStaticMethod"; | |
1554 | definition.staticFunctions = JavaStaticMethod_staticFunctions; | |
1555 | definition.callAsFunction = &JavaStaticMethod_callAsFunction; | |
1556 | definition.finalize = &CYFinalize; | |
d4222ffb | 1557 | CYPrivate<CYJavaStaticMethod>::Class_ = JSClassCreate(&definition); |
c9c16dde | 1558 | |
dbf05bfd JF |
1559 | definition = kJSClassDefinitionEmpty; |
1560 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
1561 | definition.className = "JavaObject"; | |
1562 | definition.staticValues = JavaObject_staticValues; | |
dbf05bfd | 1563 | definition.finalize = &CYFinalize; |
d4222ffb | 1564 | CYPrivate<CYJavaObject>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1565 | |
4b645e23 JF |
1566 | definition = kJSClassDefinitionEmpty; |
1567 | definition.className = "JavaArray"; | |
1568 | definition.getProperty = &JavaArray_getProperty; | |
1569 | definition.setProperty = &JavaArray_setProperty; | |
1570 | definition.finalize = &CYFinalize; | |
d4222ffb | 1571 | CYPrivate<CYJavaArray>::Class_ = JSClassCreate(&definition); |
4b645e23 | 1572 | |
dbf05bfd JF |
1573 | definition = kJSClassDefinitionEmpty; |
1574 | definition.className = "JavaPackage"; | |
1575 | definition.staticFunctions = JavaPackage_staticFunctions; | |
1576 | definition.hasProperty = &CYJavaPackage_hasProperty; | |
1577 | definition.getProperty = &CYJavaPackage_getProperty; | |
1578 | definition.finalize = &CYFinalize; | |
d4222ffb | 1579 | CYPrivate<CYJavaPackage>::Class_ = JSClassCreate(&definition); |
4b645e23 JF |
1580 | |
1581 | definition = kJSClassDefinitionEmpty; | |
1582 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
c9c16dde JF |
1583 | definition.className = "JavaStaticInterior"; |
1584 | definition.hasProperty = &JavaStaticInterior_hasProperty; | |
1585 | definition.getProperty = &JavaStaticInterior_getProperty; | |
1586 | definition.setProperty = &JavaStaticInterior_setProperty; | |
1587 | definition.getPropertyNames = &JavaStaticInterior_getPropertyNames; | |
4b645e23 | 1588 | definition.finalize = &CYFinalize; |
d4222ffb | 1589 | CYPrivate<CYJavaStaticInterior>::Class_ = JSClassCreate(&definition); |
dbf05bfd JF |
1590 | } |
1591 | ||
1592 | void CYJava_SetupContext(JSContextRef context) { | |
1593 | JSObjectRef global(CYGetGlobalObject(context)); | |
f45374f4 | 1594 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); |
dbf05bfd JF |
1595 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); |
1596 | JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all")))); | |
1597 | //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1598 | ||
1599 | JSObjectRef Java(JSObjectMake(context, NULL, NULL)); | |
1600 | CYSetProperty(context, cycript, CYJSString("Java"), Java); | |
f45374f4 | 1601 | CYSetProperty(context, cy, CYJSString("Java"), Java); |
dbf05bfd | 1602 | |
d4222ffb | 1603 | JSObjectRef Packages(CYPrivate<CYJavaPackage>::Make(context, nullptr, CYJavaPackage::Path())); |
f45374f4 | 1604 | CYSetProperty(context, all, CYJSString("Packages"), Packages, kJSPropertyAttributeDontEnum); |
dbf05bfd JF |
1605 | |
1606 | for (auto name : (const char *[]) {"java", "javax", "android", "com", "net", "org"}) { | |
1607 | CYJSString js(name); | |
d4222ffb | 1608 | CYSetProperty(context, all, js, CYPrivate<CYJavaPackage>::Make(context, nullptr, CYJavaPackage::Path(1, name)), kJSPropertyAttributeDontEnum); |
dbf05bfd JF |
1609 | } |
1610 | } | |
1611 | ||
1612 | static CYHook CYJavaHook = { | |
1613 | NULL, | |
1614 | NULL, | |
1615 | NULL, | |
1616 | &CYJava_Initialize, | |
1617 | &CYJava_SetupContext, | |
1618 | NULL, | |
1619 | }; | |
1620 | ||
1621 | CYRegisterHook CYJava(&CYJavaHook); |