]>
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 | ||
6bbc110e | 516 | // XXX: the shorty doesn't store enough information |
dbf05bfd | 517 | bool operator <(const CYJavaSignature &rhs) const { |
6bbc110e | 518 | return shorty_ < rhs.shorty_; |
dbf05bfd JF |
519 | } |
520 | }; | |
521 | ||
6bbc110e JF |
522 | typedef std::set<CYJavaSignature> CYJavaOverload; |
523 | typedef std::map<unsigned, CYJavaOverload> CYJavaOverloads; | |
dbf05bfd JF |
524 | |
525 | struct CYJavaMethod : | |
d4222ffb | 526 | CYRoot |
dbf05bfd | 527 | { |
6bbc110e | 528 | CYJavaOverloads overloads_; |
dbf05bfd | 529 | |
6bbc110e JF |
530 | CYJavaMethod(const CYJavaOverloads &overloads) : |
531 | overloads_(overloads) | |
c9c16dde JF |
532 | { |
533 | } | |
534 | }; | |
535 | ||
536 | struct CYJavaStaticMethod : | |
d4222ffb | 537 | CYRoot |
c9c16dde | 538 | { |
6bbc110e | 539 | CYJavaOverloads overloads_; |
c9c16dde | 540 | |
6bbc110e JF |
541 | CYJavaStaticMethod(const CYJavaOverloads &overloads) : |
542 | overloads_(overloads) | |
dbf05bfd JF |
543 | { |
544 | } | |
545 | }; | |
546 | ||
547 | struct CYJavaClass : | |
d4222ffb | 548 | CYRoot |
dbf05bfd | 549 | { |
d4222ffb | 550 | CYJavaGlobal<jclass> value_; |
c9c16dde JF |
551 | bool interface_; |
552 | ||
dbf05bfd JF |
553 | CYJavaFieldMap static_; |
554 | CYJavaFieldMap instance_; | |
6bbc110e | 555 | CYJavaOverloads overloads_; |
dbf05bfd | 556 | |
fc664744 | 557 | CYJavaClass(const CYJavaRef<jclass> &value, bool interface) : |
d4222ffb | 558 | value_(value), |
c9c16dde | 559 | interface_(interface) |
dbf05bfd JF |
560 | { |
561 | } | |
562 | }; | |
563 | ||
fc664744 | 564 | static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef<jclass> &_class); |
8effd381 | 565 | |
dbf05bfd | 566 | struct CYJavaObject : |
d4222ffb | 567 | CYRoot |
dbf05bfd | 568 | { |
d4222ffb | 569 | CYJavaGlobal<jobject> value_; |
8effd381 JF |
570 | CYJavaClass *table_; |
571 | ||
fc664744 | 572 | CYJavaObject(const CYJavaRef<jobject> &value, CYJavaClass *table) : |
d4222ffb | 573 | value_(value), |
4b645e23 | 574 | table_(table) |
dbf05bfd JF |
575 | { |
576 | } | |
577 | ||
578 | JSValueRef GetPrototype(JSContextRef context) const; | |
579 | }; | |
580 | ||
8effd381 | 581 | struct CYJavaInterior : |
d4222ffb | 582 | CYRoot |
8effd381 | 583 | { |
d4222ffb | 584 | CYJavaGlobal<jobject> value_; |
8effd381 JF |
585 | CYJavaClass *table_; |
586 | ||
fc664744 | 587 | CYJavaInterior(const CYJavaRef<jobject> &value, CYJavaClass *table) : |
d4222ffb | 588 | value_(value), |
8effd381 JF |
589 | table_(table) |
590 | { | |
591 | } | |
592 | }; | |
593 | ||
c9c16dde | 594 | struct CYJavaStaticInterior : |
d4222ffb | 595 | CYRoot |
4b645e23 | 596 | { |
d4222ffb | 597 | CYJavaGlobal<jclass> value_; |
4b645e23 JF |
598 | CYJavaClass *table_; |
599 | ||
fc664744 | 600 | CYJavaStaticInterior(const CYJavaRef<jclass> &value, CYJavaClass *table) : |
d4222ffb | 601 | value_(value), |
4b645e23 JF |
602 | table_(table) |
603 | { | |
604 | } | |
605 | }; | |
606 | ||
607 | struct CYJavaArray : | |
d4222ffb | 608 | CYRoot |
4b645e23 | 609 | { |
d4222ffb | 610 | CYJavaGlobal<jarray> value_; |
4b645e23 JF |
611 | CYJavaPrimitive primitive_; |
612 | ||
fc664744 | 613 | CYJavaArray(const CYJavaRef<jarray> &value, CYJavaPrimitive primitive) : |
d4222ffb | 614 | value_(value), |
4b645e23 JF |
615 | primitive_(primitive) |
616 | { | |
617 | } | |
618 | ||
619 | JSValueRef GetPrototype(JSContextRef context) const; | |
620 | }; | |
621 | ||
dbf05bfd | 622 | struct CYJavaPackage : |
d4222ffb | 623 | CYRoot |
dbf05bfd | 624 | { |
f3e85e94 JF |
625 | JNIEnv *jni_; |
626 | ||
dbf05bfd JF |
627 | typedef std::vector<std::string> Path; |
628 | Path package_; | |
629 | ||
f3e85e94 JF |
630 | _finline CYJavaPackage(JNIEnv *jni, const Path &package) : |
631 | jni_(jni), | |
dbf05bfd JF |
632 | package_(package) |
633 | { | |
634 | } | |
635 | }; | |
636 | ||
dbf05bfd | 637 | JSValueRef CYJavaObject::GetPrototype(JSContextRef context) const { |
fc664744 JF |
638 | CYJavaEnv jni(value_); |
639 | return CYGetProperty(context, CYGetJavaClass(context, jni.GetObjectClass(value_)), prototype_s); | |
dbf05bfd JF |
640 | } |
641 | ||
4b645e23 JF |
642 | JSValueRef CYJavaArray::GetPrototype(JSContextRef context) const { |
643 | return CYGetCachedObject(context, CYJSString("Array_prototype")); | |
644 | } | |
645 | ||
fc664744 JF |
646 | static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef<jobject> &value) { |
647 | if (!value) | |
dbf05bfd | 648 | return CYJSNull(context); |
fc664744 JF |
649 | CYJavaEnv jni(value); |
650 | ||
651 | auto _class(jni.GetObjectClass(value)); | |
652 | if (jni.IsSameObject(_class, jni.FindClass("java/lang/String"))) | |
653 | return CYCastJSValue(context, CYJSString(value.cast<jstring>())); | |
654 | ||
655 | auto Class$(jni.FindClass("java/lang/Class")); | |
656 | auto Class$isArray(jni.GetMethodID(Class$, "isArray", "()Z")); | |
657 | if (jni.CallBooleanMethod(_class, Class$isArray)) { | |
658 | auto Class$getComponentType(jni.GetMethodID(Class$, "getComponentType", "()Ljava/lang/Class;")); | |
659 | auto component(jni.CallObjectMethod<jclass>(_class, Class$getComponentType)); | |
660 | auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); | |
d4222ffb | 661 | return CYPrivate<CYJavaArray>::Make(context, value.cast<jarray>(), CYJavaGetPrimitive(context, component, Class$getName)); |
c9c16dde JF |
662 | } |
663 | ||
fc664744 JF |
664 | auto Wrapper$(jni.FindClass("Cycript$Wrapper")); |
665 | if (jni.IsSameObject(_class, Wrapper$)) { | |
666 | auto Wrapper$getProtect(jni.GetMethodID(Wrapper$, "getProtect", "()J")); | |
667 | auto &protect(*reinterpret_cast<CYProtect *>(jni.CallLongMethod(value, Wrapper$getProtect))); | |
c9c16dde | 668 | return protect; |
4b645e23 JF |
669 | } |
670 | ||
fc664744 | 671 | CYJavaClass *table(reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(CYGetJavaClass(context, _class)))); |
d4222ffb | 672 | return CYPrivate<CYJavaObject>::Make(context, value, table); |
dbf05bfd JF |
673 | } |
674 | ||
fc664744 JF |
675 | static _finline JSObjectRef CYCastJSObject(JSContextRef context, const CYJavaRef<jobject> &value) { |
676 | return CYCastJSObject(context, CYCastJSValue(context, value)); | |
c9c16dde JF |
677 | } |
678 | ||
fc664744 JF |
679 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaEnv &jni, JSContextRef context, CYUTF16String value) { |
680 | return jni.NewString(value.data, value.size); | |
dbf05bfd JF |
681 | } |
682 | ||
fc664744 | 683 | static CYJavaLocal<jstring> CYCastJavaString(const CYJavaEnv &jni, JSContextRef context, JSStringRef value) { |
c9c16dde | 684 | return CYCastJavaString(jni, context, CYCastUTF16String(value)); |
dbf05bfd JF |
685 | } |
686 | ||
687 | #define CYCastJava$(T, Type, jtype, Cast) \ | |
fc664744 JF |
688 | _disused static CYJavaLocal<jobject> CYCastJava ## Type(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { \ |
689 | auto Type$(jni.FindClass("java/lang/" #Type)); \ | |
690 | auto Type$init$(jni.GetMethodID(Type$, "<init>", "(" #T ")V")); \ | |
691 | return jni.NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value))); \ | |
dbf05bfd JF |
692 | } |
693 | ||
694 | CYCastJava$(Z, Boolean, jboolean, CYCastBool) | |
695 | CYCastJava$(B, Byte, jbyte, CYCastDouble) | |
696 | CYCastJava$(C, Character, jchar, CYCastDouble) | |
697 | CYCastJava$(S, Short, jshort, CYCastDouble) | |
698 | CYCastJava$(I, Integer, jint, CYCastDouble) | |
699 | CYCastJava$(J, Long, jlong, CYCastDouble) | |
700 | CYCastJava$(F, Float, jfloat, CYCastDouble) | |
701 | CYCastJava$(D, Double, jdouble, CYCastDouble) | |
702 | ||
c9c16dde | 703 | static CYJavaClass *CYGetJavaTable(JSContextRef context, JSObjectRef object) { |
d4222ffb | 704 | if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaClass>::Class_)) |
c9c16dde JF |
705 | return NULL; |
706 | return reinterpret_cast<CYJavaClass *>(JSObjectGetPrivate(object)); | |
707 | } | |
708 | ||
709 | static CYJavaObject *CYGetJavaObject(JSContextRef context, JSObjectRef object) { | |
d4222ffb | 710 | if (!JSValueIsObjectOfClass(context, object, CYPrivate<CYJavaObject>::Class_)) |
c9c16dde JF |
711 | return NULL; |
712 | return reinterpret_cast<CYJavaObject *>(JSObjectGetPrivate(object)); | |
713 | } | |
714 | ||
fc664744 | 715 | static CYJavaLocal<jobject> CYCastJavaObject(const CYJavaEnv &jni, JSContextRef context, JSObjectRef value) { |
c9c16dde | 716 | if (CYJavaObject *internal = CYGetJavaObject(context, value)) |
dbf05bfd | 717 | return internal->value_; |
dbf05bfd | 718 | |
fc664744 JF |
719 | auto Wrapper$(jni.FindClass("Cycript$Wrapper")); |
720 | auto Wrapper$$init$(jni.GetMethodID(Wrapper$, "<init>", "(J)V")); | |
c9c16dde | 721 | CYProtect *protect(new CYProtect(context, value)); |
fc664744 | 722 | return jni.NewObject(Wrapper$, Wrapper$$init$, reinterpret_cast<jlong>(protect)); |
dbf05bfd JF |
723 | } |
724 | ||
fc664744 | 725 | static CYJavaLocal<jobject> CYCastJavaObject(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { |
dbf05bfd JF |
726 | switch (JSValueGetType(context, value)) { |
727 | case kJSTypeNull: | |
fc664744 | 728 | return {jni, NULL}; |
dbf05bfd JF |
729 | case kJSTypeBoolean: |
730 | return CYCastJavaBoolean(jni, context, value); | |
731 | case kJSTypeNumber: | |
732 | return CYCastJavaDouble(jni, context, value); | |
733 | case kJSTypeString: | |
c9c16dde | 734 | return CYCastJavaString(jni, context, CYJSString(context, value)); |
dbf05bfd JF |
735 | case kJSTypeObject: |
736 | return CYCastJavaObject(jni, context, CYCastJSObject(context, value)); | |
737 | ||
738 | case kJSTypeUndefined: | |
c9c16dde | 739 | // XXX: I am currently relying on this for dynamic proxy of void method |
fc664744 | 740 | return {jni, NULL}; |
dbf05bfd JF |
741 | default: |
742 | _assert(false); | |
743 | } | |
744 | } | |
745 | ||
fc664744 JF |
746 | static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef<jclass> &value) { |
747 | CYJavaEnv jni(value); | |
748 | CYJavaFrame frame(jni, 64); | |
749 | ||
dbf05bfd JF |
750 | JSObjectRef global(CYGetGlobalObject(context)); |
751 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); | |
752 | ||
fc664744 JF |
753 | auto Class$(jni.FindClass("java/lang/Class")); |
754 | auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); | |
dbf05bfd | 755 | |
fc664744 | 756 | CYJSString name(jni.CallObjectMethod<jstring>(value, Class$getName)); |
dbf05bfd JF |
757 | JSValueRef cached(CYGetProperty(context, cy, name)); |
758 | if (!JSValueIsUndefined(context, cached)) | |
759 | return CYCastJSObject(context, cached); | |
760 | ||
fc664744 JF |
761 | JSObjectRef constructor; |
762 | JSObjectRef prototype; | |
c9c16dde | 763 | |
fc664744 | 764 | { |
dbf05bfd | 765 | |
fc664744 | 766 | auto Class$isInterface(jni.GetMethodID(Class$, "isInterface", "()Z")); |
dbf05bfd | 767 | |
fc664744 JF |
768 | auto Class$getDeclaredConstructors(jni.GetMethodID(Class$, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;")); |
769 | auto Class$getDeclaredFields(jni.GetMethodID(Class$, "getDeclaredFields", "()[Ljava/lang/reflect/Field;")); | |
770 | auto Class$getDeclaredMethods(jni.GetMethodID(Class$, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;")); | |
dbf05bfd | 771 | |
fc664744 JF |
772 | auto Constructor$(jni.FindClass("java/lang/reflect/Constructor")); |
773 | //auto Constructor$getModifiers(jni.GetMethodID(Constructor$, "getModifiers", "()I")); | |
774 | auto Constructor$getParameterTypes(jni.GetMethodID(Constructor$, "getParameterTypes", "()[Ljava/lang/Class;")); | |
dbf05bfd | 775 | |
fc664744 JF |
776 | auto Field$(jni.FindClass("java/lang/reflect/Field")); |
777 | auto Field$getModifiers(jni.GetMethodID(Field$, "getModifiers", "()I")); | |
778 | auto Field$getName(jni.GetMethodID(Field$, "getName", "()Ljava/lang/String;")); | |
779 | auto Field$getType(jni.GetMethodID(Field$, "getType", "()Ljava/lang/Class;")); | |
dbf05bfd | 780 | |
fc664744 JF |
781 | auto Method$(jni.FindClass("java/lang/reflect/Method")); |
782 | auto Method$getModifiers(jni.GetMethodID(Method$, "getModifiers", "()I")); | |
783 | auto Method$getName(jni.GetMethodID(Method$, "getName", "()Ljava/lang/String;")); | |
784 | auto Method$getParameterTypes(jni.GetMethodID(Method$, "getParameterTypes", "()[Ljava/lang/Class;")); | |
785 | auto Method$getReturnType(jni.GetMethodID(Method$, "getReturnType", "()Ljava/lang/Class;")); | |
dbf05bfd | 786 | |
fc664744 JF |
787 | auto Modifier$(jni.FindClass("java/lang/reflect/Modifier")); |
788 | auto Modifier$isStatic(jni.GetStaticMethodID(Modifier$, "isStatic", "(I)Z")); | |
dbf05bfd | 789 | |
fc664744 JF |
790 | auto interface(jni.CallBooleanMethod(value, Class$isInterface)); |
791 | auto table(new CYJavaClass(value, interface)); | |
792 | ||
793 | for (CYJavaLocal<jclass> prototype(value); prototype; prototype = jni.GetSuperclass(prototype)) { | |
794 | auto fields(jni.CallObjectMethod<jobjectArray>(prototype, Class$getDeclaredFields)); | |
795 | ||
796 | for (jsize i(0), e(jni.GetArrayLength(fields)); i != e; ++i) { | |
797 | auto field(jni.GetObjectArrayElement<jobject>(fields, e - i - 1)); | |
798 | auto modifiers(jni.CallIntMethod(field, Field$getModifiers)); | |
799 | auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); | |
dbf05bfd | 800 | auto &map(instance ? table->instance_ : table->static_); |
fc664744 JF |
801 | CYJavaUTF8String name(jni.CallObjectMethod<jstring>(field, Field$getName)); |
802 | auto id(jni.FromReflectedField(field)); | |
803 | auto type(jni.CallObjectMethod<jclass>(field, Field$getType)); | |
804 | map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(context, type, Class$getName)})); | |
dbf05bfd JF |
805 | } |
806 | } | |
807 | ||
d4222ffb | 808 | constructor = JSObjectMake(context, CYPrivate<CYJavaClass>::Class_, table); |
dbf05bfd | 809 | |
fc664744 | 810 | prototype = JSObjectMake(context, NULL, NULL); |
dbf05bfd JF |
811 | CYSetProperty(context, constructor, prototype_s, prototype, kJSPropertyAttributeDontEnum); |
812 | ||
fc664744 | 813 | auto constructors(jni.CallObjectMethod<jobjectArray>(value, Class$getDeclaredConstructors)); |
dbf05bfd | 814 | |
fc664744 JF |
815 | for (jsize i(0), e(jni.GetArrayLength(constructors)); i != e; ++i) { |
816 | auto constructor(jni.GetObjectArrayElement<jobject>(constructors, i)); | |
817 | auto parameters(jni.CallObjectMethod<jobjectArray>(constructor, Constructor$getParameterTypes)); | |
818 | CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); | |
819 | auto id(jni.FromReflectedMethod(constructor)); | |
6bbc110e | 820 | table->overloads_[shorty.size()].insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty)); |
dbf05bfd JF |
821 | } |
822 | ||
6bbc110e JF |
823 | std::map<std::pair<bool, std::string>, CYJavaOverloads> entries; |
824 | ||
825 | bool base(false); | |
826 | for (CYJavaLocal<jclass> prototype(value); prototype; prototype = jni.GetSuperclass(prototype)) { | |
827 | auto methods(jni.CallObjectMethod<jobjectArray>(prototype, Class$getDeclaredMethods)); | |
dbf05bfd | 828 | |
6bbc110e JF |
829 | for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) { |
830 | auto method(jni.GetObjectArrayElement<jobject>(methods, i)); | |
831 | auto modifiers(jni.CallIntMethod(method, Method$getModifiers)); | |
832 | auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); | |
833 | CYJavaUTF8String name(jni.CallObjectMethod<jstring>(method, Method$getName)); | |
834 | ||
835 | auto parameters(jni.CallObjectMethod<jobjectArray>(method, Method$getParameterTypes)); | |
836 | CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); | |
837 | ||
838 | CYJavaOverload *overload; | |
839 | if (!base) | |
840 | overload = &entries[std::make_pair(instance, std::string(name))][shorty.size()]; | |
841 | else { | |
842 | auto entry(entries.find(std::make_pair(instance, std::string(name)))); | |
843 | if (entry == entries.end()) | |
844 | continue; | |
845 | overload = &entry->second[shorty.size()]; | |
846 | } | |
847 | ||
848 | auto type(jni.CallObjectMethod<jclass>(method, Method$getReturnType)); | |
849 | auto primitive(CYJavaGetPrimitive(context, type, Class$getName)); | |
850 | auto id(jni.FromReflectedMethod(method)); | |
851 | overload->insert(CYJavaSignature(method, id, primitive, shorty)); | |
852 | } | |
dbf05bfd | 853 | |
6bbc110e | 854 | base = true; |
dbf05bfd JF |
855 | } |
856 | ||
857 | for (const auto &entry : entries) { | |
858 | bool instance(entry.first.first); | |
859 | CYJSString name(entry.first.second); | |
860 | auto &overload(entry.second); | |
c9c16dde | 861 | if (instance) |
d4222ffb | 862 | CYSetProperty(context, prototype, name, CYPrivate<CYJavaMethod>::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); |
c9c16dde | 863 | else |
d4222ffb | 864 | CYSetProperty(context, constructor, name, CYPrivate<CYJavaStaticMethod>::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); |
dbf05bfd JF |
865 | } |
866 | ||
fc664744 JF |
867 | } |
868 | ||
dbf05bfd JF |
869 | // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name |
870 | // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :( | |
871 | ||
fc664744 JF |
872 | if (auto super = jni.GetSuperclass(value)) { |
873 | JSObjectRef parent(CYGetJavaClass(context, super)); | |
4b645e23 | 874 | CYSetPrototype(context, constructor, parent); |
dbf05bfd JF |
875 | CYSetPrototype(context, prototype, CYGetProperty(context, parent, prototype_s)); |
876 | } | |
877 | ||
878 | CYSetProperty(context, cy, name, constructor); | |
879 | return constructor; | |
880 | } | |
881 | ||
4b645e23 JF |
882 | static void CYCastJavaNumeric(jvalue &value, CYJavaPrimitive primitive, JSContextRef context, JSValueRef argument) { |
883 | switch (primitive) { | |
884 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
885 | case CYJavaPrimitive ## Type: \ | |
886 | value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \ | |
887 | break; | |
dbf05bfd JF |
888 | CYJavaForEachPrimitive |
889 | #undef CYJavaForEachPrimitive_ | |
4b645e23 JF |
890 | default: |
891 | _assert(false); | |
892 | } | |
893 | } | |
894 | ||
4e2cc9d3 JF |
895 | static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jvalue *array) { |
896 | CYJavaEnv jni(frame); | |
897 | ||
4b645e23 JF |
898 | for (size_t index(0); index != shorty.size(); ++index) { |
899 | JSValueRef argument(arguments[index]); | |
900 | JSType type(JSValueGetType(context, argument)); | |
901 | jvalue &value(array[index]); | |
902 | ||
903 | switch (CYJavaPrimitive primitive = shorty[index]) { | |
904 | case CYJavaPrimitiveObject: | |
4e2cc9d3 | 905 | // XXX: figure out a way to tie this in to the CYJavaFrame |
fc664744 | 906 | value.l = CYCastJavaObject(jni, context, argument).leak(); |
4b645e23 JF |
907 | break; |
908 | ||
909 | case CYJavaPrimitiveBoolean: | |
910 | if (type != kJSTypeBoolean) | |
911 | return false; | |
912 | value.z = CYCastBool(context, argument); | |
913 | break; | |
914 | ||
915 | case CYJavaPrimitiveCharacter: | |
916 | if (type == kJSTypeNumber) | |
917 | CYCastJavaNumeric(value, primitive, context, argument); | |
918 | else if (type != kJSTypeString) | |
919 | return false; | |
920 | else { | |
921 | CYJSString string(context, argument); | |
922 | if (JSStringGetLength(string) != 1) | |
923 | return false; | |
924 | else | |
925 | value.c = JSStringGetCharactersPtr(string)[0]; | |
926 | } | |
927 | break; | |
928 | ||
929 | case CYJavaPrimitiveByte: | |
930 | case CYJavaPrimitiveShort: | |
931 | case CYJavaPrimitiveInteger: | |
932 | case CYJavaPrimitiveLong: | |
933 | case CYJavaPrimitiveFloat: | |
934 | case CYJavaPrimitiveDouble: | |
935 | if (type != kJSTypeNumber) | |
936 | return false; | |
937 | CYCastJavaNumeric(value, primitive, context, argument); | |
938 | break; | |
939 | ||
dbf05bfd JF |
940 | default: |
941 | _assert(false); | |
942 | } | |
dbf05bfd JF |
943 | } |
944 | ||
4b645e23 | 945 | return true; |
dbf05bfd JF |
946 | } |
947 | ||
948 | static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 949 | auto internal(CYPrivate<CYJavaMethod>::Get(context, object)); |
c9c16dde | 950 | CYJavaObject *self(CYGetJavaObject(context, _this)); |
447b698c | 951 | _assert(self != NULL); |
fc664744 | 952 | CYJavaEnv jni(self->value_); |
c9c16dde | 953 | |
6bbc110e JF |
954 | auto overload(internal->overloads_.find(count)); |
955 | if (overload != internal->overloads_.end()) | |
956 | for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { | |
4e2cc9d3 | 957 | CYJavaFrame frame(jni, count + 16); |
c9c16dde | 958 | jvalue array[count]; |
6bbc110e | 959 | if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) |
c9c16dde | 960 | continue; |
fc664744 | 961 | jvalue *values(array); |
6bbc110e | 962 | switch (signature->primitive_) { |
c9c16dde | 963 | case CYJavaPrimitiveObject: |
6bbc110e | 964 | return CYCastJSValue(context, jni.CallObjectMethodA<jobject>(self->value_, signature->method_, values)); |
c9c16dde | 965 | case CYJavaPrimitiveVoid: |
6bbc110e | 966 | jni.CallVoidMethodA(self->value_, signature->method_, values); |
c9c16dde JF |
967 | return CYJSUndefined(context); |
968 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
969 | case CYJavaPrimitive ## Type: \ | |
6bbc110e | 970 | return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, signature->method_, values)); |
c9c16dde JF |
971 | CYJavaForEachPrimitive |
972 | #undef CYJavaForEachPrimitive_ | |
973 | default: _assert(false); | |
974 | } | |
975 | } | |
976 | ||
977 | CYThrow("invalid method call"); | |
978 | } CYCatch(NULL) } | |
979 | ||
980 | static JSValueRef JavaStaticMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 981 | auto internal(CYPrivate<CYJavaStaticMethod>::Get(context, object)); |
c9c16dde | 982 | CYJavaClass *table(CYGetJavaTable(context, _this)); |
fc664744 | 983 | CYJavaEnv jni(table->value_); |
dbf05bfd | 984 | |
6bbc110e JF |
985 | auto overload(internal->overloads_.find(count)); |
986 | if (overload != internal->overloads_.end()) | |
987 | for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { | |
4e2cc9d3 | 988 | CYJavaFrame frame(jni, count + 16); |
4b645e23 | 989 | jvalue array[count]; |
6bbc110e | 990 | if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) |
4b645e23 | 991 | continue; |
fc664744 | 992 | jvalue *values(array); |
6bbc110e | 993 | switch (signature->primitive_) { |
4b645e23 | 994 | case CYJavaPrimitiveObject: |
6bbc110e | 995 | return CYCastJSValue(context, jni.CallStaticObjectMethodA<jobject>(table->value_, signature->method_, values)); |
c9c16dde | 996 | case CYJavaPrimitiveVoid: |
6bbc110e | 997 | jni.CallStaticVoidMethodA(table->value_, signature->method_, values); |
c9c16dde | 998 | return CYJSUndefined(context); |
4b645e23 JF |
999 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
1000 | case CYJavaPrimitive ## Type: \ | |
6bbc110e | 1001 | return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, signature->method_, values)); |
4b645e23 JF |
1002 | CYJavaForEachPrimitive |
1003 | #undef CYJavaForEachPrimitive_ | |
1004 | default: _assert(false); | |
1005 | } | |
dbf05bfd JF |
1006 | } |
1007 | ||
1008 | CYThrow("invalid method call"); | |
1009 | } CYCatch(NULL) } | |
1010 | ||
1011 | static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
d4222ffb | 1012 | auto table(CYPrivate<CYJavaClass>::Get(context, object)); |
fc664744 | 1013 | CYJavaEnv jni(table->value_); |
c9c16dde JF |
1014 | jclass _class(table->value_); |
1015 | ||
1016 | if (table->interface_ && count == 1) { | |
fc664744 | 1017 | auto Cycript$(jni.FindClass("Cycript")); |
0e832de4 JF |
1018 | auto Cycript$Make(jni.GetStaticMethodID(Cycript$, "proxy", "(Ljava/lang/Class;LCycript$Wrapper;)Ljava/lang/Object;")); |
1019 | return CYCastJSObject(context, jni.CallObjectMethod<jobject>(Cycript$, Cycript$Make, _class, CYCastJavaObject(jni, context, CYCastJSObject(context, arguments[0])).get())); | |
c9c16dde | 1020 | } |
dbf05bfd | 1021 | |
6bbc110e JF |
1022 | auto overload(table->overloads_.find(count)); |
1023 | if (overload != table->overloads_.end()) | |
1024 | for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { | |
4e2cc9d3 | 1025 | CYJavaFrame frame(jni, count + 16); |
4b645e23 | 1026 | jvalue array[count]; |
6bbc110e | 1027 | if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) |
4b645e23 | 1028 | continue; |
fc664744 | 1029 | jvalue *values(array); |
6bbc110e | 1030 | auto object(jni.NewObjectA(_class, signature->method_, values)); |
fc664744 | 1031 | return CYCastJSObject(context, object); |
dbf05bfd JF |
1032 | } |
1033 | ||
1034 | CYThrow("invalid constructor call"); | |
77dd5db9 | 1035 | } CYCatchObject() } |
dbf05bfd | 1036 | |
c9c16dde | 1037 | static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
d4222ffb | 1038 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1039 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1040 | CYPool pool; |
1041 | auto name(CYPoolUTF8String(pool, context, property)); | |
1042 | auto field(table->static_.find(name)); | |
1043 | if (field == table->static_.end()) | |
1044 | return false; | |
1045 | return true; | |
1046 | } | |
1047 | ||
c9c16dde | 1048 | static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1049 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1050 | CYJavaClass *table(internal->table_); |
fc664744 | 1051 | CYJavaEnv jni(table->value_); |
dbf05bfd JF |
1052 | CYPool pool; |
1053 | auto name(CYPoolUTF8String(pool, context, property)); | |
1054 | auto field(table->static_.find(name)); | |
1055 | if (field == table->static_.end()) | |
1056 | return NULL; | |
1057 | ||
1058 | switch (field->second.primitive_) { | |
1059 | case CYJavaPrimitiveObject: | |
fc664744 | 1060 | return CYCastJSValue(context, jni.GetStaticObjectField<jobject>(table->value_, field->second.field_)); |
4b645e23 | 1061 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1062 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1063 | return CYJavaCastJSValue(context, jni.GetStatic ## Typ ## Field(table->value_, field->second.field_)); |
dbf05bfd JF |
1064 | CYJavaForEachPrimitive |
1065 | #undef CYJavaForEachPrimitive_ | |
1066 | default: _assert(false); | |
1067 | } | |
1068 | } CYCatch(NULL) } | |
1069 | ||
c9c16dde | 1070 | static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { |
d4222ffb | 1071 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1072 | CYJavaClass *table(internal->table_); |
fc664744 | 1073 | CYJavaEnv jni(table->value_); |
dbf05bfd JF |
1074 | CYPool pool; |
1075 | auto name(CYPoolUTF8String(pool, context, property)); | |
1076 | auto field(table->static_.find(name)); | |
1077 | if (field == table->static_.end()) | |
1078 | return false; | |
1079 | ||
1080 | switch (field->second.primitive_) { | |
1081 | case CYJavaPrimitiveObject: | |
fc664744 | 1082 | jni.SetStaticObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value)); |
4b645e23 | 1083 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1084 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1085 | jni.SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \ |
dbf05bfd JF |
1086 | break; |
1087 | CYJavaForEachPrimitive | |
1088 | #undef CYJavaForEachPrimitive_ | |
1089 | default: _assert(false); | |
1090 | } | |
1091 | ||
1092 | return true; | |
1093 | } CYCatch(false) } | |
1094 | ||
c9c16dde | 1095 | static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
d4222ffb | 1096 | auto internal(CYPrivate<CYJavaStaticInterior>::Get(context, object)); |
4b645e23 | 1097 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1098 | for (const auto &field : table->static_) |
1099 | JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); | |
1100 | } | |
1101 | ||
1102 | static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1103 | auto table(CYPrivate<CYJavaClass>::Get(context, object)); |
fc664744 | 1104 | return CYCastJSValue(context, table->value_); |
dbf05bfd JF |
1105 | } CYCatch(NULL) } |
1106 | ||
8effd381 | 1107 | static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
d4222ffb | 1108 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
8effd381 | 1109 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1110 | CYPool pool; |
1111 | auto name(CYPoolUTF8String(pool, context, property)); | |
1112 | auto field(table->instance_.find(name)); | |
1113 | if (field == table->instance_.end()) | |
1114 | return false; | |
1115 | return true; | |
1116 | } | |
1117 | ||
8effd381 | 1118 | static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1119 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
fc664744 | 1120 | CYJavaEnv jni(internal->value_); |
8effd381 | 1121 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1122 | CYPool pool; |
1123 | auto name(CYPoolUTF8String(pool, context, property)); | |
1124 | auto field(table->instance_.find(name)); | |
1125 | if (field == table->instance_.end()) | |
1126 | return NULL; | |
1127 | ||
1128 | switch (field->second.primitive_) { | |
1129 | case CYJavaPrimitiveObject: | |
fc664744 | 1130 | return CYCastJSValue(context, jni.GetObjectField<jobject>(internal->value_, field->second.field_)); |
4b645e23 | 1131 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1132 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1133 | return CYJavaCastJSValue(context, jni.Get ## Typ ## Field(internal->value_, field->second.field_)); |
dbf05bfd JF |
1134 | CYJavaForEachPrimitive |
1135 | #undef CYJavaForEachPrimitive_ | |
1136 | default: _assert(false); | |
1137 | } | |
1138 | } CYCatch(NULL) } | |
1139 | ||
8effd381 | 1140 | static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { |
d4222ffb | 1141 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
fc664744 | 1142 | CYJavaEnv jni(internal->value_); |
8effd381 | 1143 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1144 | CYPool pool; |
1145 | auto name(CYPoolUTF8String(pool, context, property)); | |
1146 | auto field(table->instance_.find(name)); | |
1147 | if (field == table->instance_.end()) | |
1148 | return false; | |
1149 | ||
1150 | switch (field->second.primitive_) { | |
1151 | case CYJavaPrimitiveObject: | |
fc664744 | 1152 | jni.SetObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value)); |
4b645e23 | 1153 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
dbf05bfd | 1154 | case CYJavaPrimitive ## Type: \ |
fc664744 | 1155 | jni.Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \ |
dbf05bfd JF |
1156 | break; |
1157 | CYJavaForEachPrimitive | |
1158 | #undef CYJavaForEachPrimitive_ | |
1159 | default: _assert(false); | |
1160 | } | |
1161 | ||
1162 | return true; | |
1163 | } CYCatch(false) } | |
1164 | ||
8effd381 | 1165 | static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { |
d4222ffb | 1166 | auto internal(CYPrivate<CYJavaInterior>::Get(context, object)); |
8effd381 | 1167 | CYJavaClass *table(internal->table_); |
dbf05bfd JF |
1168 | for (const auto &field : table->instance_) |
1169 | JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); | |
1170 | } | |
1171 | ||
1172 | static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1173 | auto internal(CYPrivate<CYJavaObject>::Get(context, object)); |
fc664744 JF |
1174 | CYJavaEnv jni(internal->value_); |
1175 | return CYGetJavaClass(context, jni.GetObjectClass(internal->value_)); | |
dbf05bfd JF |
1176 | } CYCatch(NULL) } |
1177 | ||
4b645e23 | 1178 | static JSValueRef JavaClass_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb JF |
1179 | auto internal(CYPrivate<CYJavaClass>::Get(context, object)); |
1180 | return CYPrivate<CYJavaStaticInterior>::Make(context, internal->value_, internal); | |
4b645e23 JF |
1181 | } CYCatch(NULL) } |
1182 | ||
8effd381 | 1183 | static JSValueRef JavaObject_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb JF |
1184 | auto internal(CYPrivate<CYJavaObject>::Get(context, object)); |
1185 | return CYPrivate<CYJavaInterior>::Make(context, internal->value_, internal->table_); | |
8effd381 JF |
1186 | } CYCatch(NULL) } |
1187 | ||
4b645e23 | 1188 | static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
d4222ffb | 1189 | auto internal(CYPrivate<CYJavaClass>::Get(context, _this)); |
fc664744 JF |
1190 | CYJavaEnv jni(internal->value_); |
1191 | auto Class$(jni.FindClass("java/lang/Class")); | |
1192 | auto Class$getCanonicalName(jni.GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;")); | |
1193 | return CYCastJSValue(context, CYJSString(jni.CallObjectMethod<jstring>(internal->value_, Class$getCanonicalName))); | |
4b645e23 JF |
1194 | } CYCatch(NULL) } |
1195 | ||
1196 | static JSValueRef JavaMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { | |
1197 | std::ostringstream cyon; | |
1198 | return CYCastJSValue(context, CYJSString(cyon.str())); | |
1199 | } CYCatch(NULL) } | |
1200 | ||
c9c16dde JF |
1201 | static JSValueRef JavaStaticMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
1202 | std::ostringstream cyon; | |
1203 | return CYCastJSValue(context, CYJSString(cyon.str())); | |
1204 | } CYCatch(NULL) } | |
1205 | ||
4b645e23 | 1206 | static JSValueRef JavaArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { |
d4222ffb | 1207 | auto internal(CYPrivate<CYJavaArray>::Get(context, object)); |
fc664744 | 1208 | CYJavaEnv jni(internal->value_); |
4b645e23 | 1209 | if (JSStringIsEqual(property, length_s)) |
fc664744 | 1210 | return CYCastJSValue(context, jni.GetArrayLength(internal->value_)); |
4b645e23 JF |
1211 | |
1212 | CYPool pool; | |
1213 | ssize_t offset; | |
1214 | if (!CYGetOffset(pool, context, property, offset)) | |
1215 | return NULL; | |
1216 | ||
1217 | if (internal->primitive_ == CYJavaPrimitiveObject) | |
fc664744 | 1218 | return CYCastJSValue(context, jni.GetObjectArrayElement<jobject>(static_cast<jobjectArray>(internal->value_.value_), offset)); |
4b645e23 JF |
1219 | else switch (internal->primitive_) { |
1220 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
1221 | case CYJavaPrimitive ## Type: { \ | |
1222 | j ## type element; \ | |
fc664744 | 1223 | jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \ |
4b645e23 JF |
1224 | return CYJavaCastJSValue(context, element); \ |
1225 | } break; | |
1226 | CYJavaForEachPrimitive | |
1227 | #undef CYJavaForEachPrimitive_ | |
1228 | default: _assert(false); | |
1229 | } | |
1230 | } CYCatch(NULL) } | |
1231 | ||
1232 | static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { | |
d4222ffb | 1233 | auto internal(CYPrivate<CYJavaArray>::Get(context, object)); |
fc664744 | 1234 | CYJavaEnv jni(internal->value_); |
4b645e23 JF |
1235 | |
1236 | CYPool pool; | |
1237 | ssize_t offset; | |
1238 | if (!CYGetOffset(pool, context, property, offset)) | |
1239 | return false; | |
1240 | ||
1241 | if (internal->primitive_ == CYJavaPrimitiveObject) | |
fc664744 | 1242 | jni.SetObjectArrayElement(static_cast<jobjectArray>(internal->value_.value_), offset, CYCastJavaObject(jni, context, value)); |
4b645e23 JF |
1243 | else switch (internal->primitive_) { |
1244 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ | |
1245 | case CYJavaPrimitive ## Type: { \ | |
1246 | j ## type element; \ | |
fc664744 | 1247 | jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \ |
4b645e23 JF |
1248 | return CYJavaCastJSValue(context, element); \ |
1249 | } break; | |
1250 | CYJavaForEachPrimitive | |
1251 | #undef CYJavaForEachPrimitive_ | |
1252 | default: _assert(false); | |
1253 | } | |
1254 | ||
1255 | return true; | |
1256 | } CYCatch(false) } | |
1257 | ||
f45374f4 JF |
1258 | static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env); |
1259 | ||
4b645e23 | 1260 | static JSValueRef JavaPackage_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { |
d4222ffb | 1261 | auto internal(CYPrivate<CYJavaPackage>::Get(context, _this)); |
4b645e23 JF |
1262 | std::ostringstream name; |
1263 | for (auto &package : internal->package_) | |
1264 | name << package << '.'; | |
1265 | name << '*'; | |
1266 | return CYCastJSValue(context, CYJSString(name.str())); | |
1267 | } CYCatch(NULL) } | |
1268 | ||
dbf05bfd JF |
1269 | static bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { |
1270 | return true; | |
1271 | } | |
1272 | ||
1273 | static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { | |
d4222ffb | 1274 | auto internal(CYPrivate<CYJavaPackage>::Get(context, object)); |
dbf05bfd JF |
1275 | CYJavaPackage::Path package(internal->package_); |
1276 | ||
1277 | CYPool pool; | |
1278 | const char *next(CYPoolCString(pool, context, property)); | |
1279 | ||
1280 | std::ostringstream name; | |
1281 | for (auto &package : internal->package_) | |
1282 | name << package << '/'; | |
1283 | name << next; | |
1284 | ||
f45374f4 JF |
1285 | if (internal->jni_ == NULL) |
1286 | GetJNI(context, internal->jni_); | |
f3e85e94 | 1287 | JNIEnv *jni(internal->jni_); |
f45374f4 | 1288 | |
fc664744 JF |
1289 | if (auto _class = jni->FindClass(name.str().c_str())) |
1290 | return CYGetJavaClass(context, CYJavaLocal<jclass>(jni, _class)); | |
dbf05bfd JF |
1291 | jni->ExceptionClear(); |
1292 | ||
1293 | package.push_back(next); | |
d4222ffb | 1294 | return CYPrivate<CYJavaPackage>::Make(context, jni, package); |
dbf05bfd JF |
1295 | } CYCatch(NULL) } |
1296 | ||
fc664744 | 1297 | static void Cycript_delete(JNIEnv *env, jclass api, jlong jprotect) { CYJavaTry { |
824bc1ec | 1298 | delete &protect; |
c9c16dde JF |
1299 | } CYJavaCatch() } |
1300 | ||
fc664744 JF |
1301 | static jobject Cycript_handle(JNIEnv *env, jclass api, jlong jprotect, jstring property, jobjectArray jarguments) { CYJavaTry { |
1302 | JSValueRef function(CYGetProperty(context, object, CYJSString(CYJavaRef<jstring>(jni, property)))); | |
c9c16dde JF |
1303 | if (JSValueIsUndefined(context, function)) |
1304 | return NULL; | |
1305 | ||
fc664744 | 1306 | size_t count(jarguments == NULL ? 0 : jni.GetArrayLength(jarguments)); |
c9c16dde | 1307 | JSValueRef arguments[count]; |
4e2cc9d3 | 1308 | for (size_t index(0); index != count; ++index) |
fc664744 | 1309 | arguments[index] = CYCastJSValue(context, jni.GetObjectArrayElement<jobject>(jarguments, index)); |
c9c16dde | 1310 | |
4e2cc9d3 | 1311 | return CYCastJavaObject(jni, context, CYCallAsFunction(context, CYCastJSObject(context, function), object, count, arguments)).leak(); |
c9c16dde JF |
1312 | } CYJavaCatch(NULL) } |
1313 | ||
1314 | static JNINativeMethod Cycript_[] = { | |
1315 | {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete}, | |
1316 | {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle}, | |
1317 | }; | |
1318 | ||
f2c357f9 JF |
1319 | template <typename Type_> |
1320 | static _finline void dlset(Type_ &function, const char *name, void *handle) { | |
1321 | function = reinterpret_cast<Type_>(dlsym(handle, name)); | |
1322 | } | |
1323 | ||
1324 | jint CYJavaVersion(JNI_VERSION_1_4); | |
1325 | ||
f3e85e94 | 1326 | static JavaVM *CYGetJavaVM(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) { |
f2c357f9 JF |
1327 | jsize capacity(16); |
1328 | JavaVM *jvms[capacity]; | |
1329 | jsize size; | |
1330 | _jnicall($JNI_GetCreatedJavaVMs(jvms, capacity, &size)); | |
1331 | if (size == 0) | |
1332 | return NULL; | |
f3e85e94 | 1333 | return jvms[0]; |
f2c357f9 JF |
1334 | } |
1335 | ||
f3e85e94 | 1336 | static JavaVM *CYGetJavaVM(JSContextRef context) { |
f2c357f9 JF |
1337 | CYPool pool; |
1338 | void *handle(RTLD_DEFAULT); | |
1339 | std::string library; | |
fc664744 | 1340 | |
f2c357f9 JF |
1341 | jint (*$JNI_GetCreatedJavaVMs)(JavaVM **jvms, jsize capacity, jsize *size); |
1342 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); | |
1343 | ||
1344 | if ($JNI_GetCreatedJavaVMs != NULL) { | |
f3e85e94 JF |
1345 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1346 | return jvm; | |
fc664744 | 1347 | } else { |
f2c357f9 JF |
1348 | std::vector<const char *> guesses; |
1349 | ||
1350 | #ifdef __ANDROID__ | |
1351 | char android[PROP_VALUE_MAX]; | |
1352 | if (__system_property_get("persist.sys.dalvik.vm.lib", android) != 0) | |
1353 | guesses.push_back(android); | |
1354 | #endif | |
1355 | ||
f3e85e94 JF |
1356 | guesses.push_back("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib"); |
1357 | //guesses.push_back("/System/Library/Frameworks/JavaVM.framework/JavaVM"); | |
4b5dc402 | 1358 | guesses.push_back("libjvm.dylib"); |
f3e85e94 | 1359 | |
f2c357f9 JF |
1360 | guesses.push_back("libart.so"); |
1361 | guesses.push_back("libdvm.so"); | |
1362 | guesses.push_back("libjvm.so"); | |
1363 | ||
1364 | for (const char *guess : guesses) { | |
1365 | handle = dlopen(guess, RTLD_LAZY | RTLD_GLOBAL); | |
1366 | if (handle != NULL) { | |
1367 | library = guess; | |
1368 | break; | |
1369 | } | |
fc664744 JF |
1370 | } |
1371 | ||
f3e85e94 JF |
1372 | if (library.size() == 0) |
1373 | return NULL; | |
f2c357f9 JF |
1374 | |
1375 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); | |
f3e85e94 JF |
1376 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1377 | return jvm; | |
fc664744 JF |
1378 | } |
1379 | ||
f2c357f9 JF |
1380 | std::vector<JavaVMOption> options; |
1381 | ||
1382 | { | |
1383 | std::ostringstream option; | |
1384 | option << "-Djava.class.path="; | |
1385 | option << CYPoolLibraryPath(pool) << "/libcycript.jar"; | |
1386 | if (const char *classpath = getenv("CLASSPATH")) | |
1387 | option << ':' << classpath; | |
1388 | options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL}); | |
1389 | } | |
1390 | ||
1391 | // To use libnativehelper to access JNI_GetCreatedJavaVMs, you need JniInvocation. | |
1392 | // ...but there can only be one JniInvocation, and assuradely the other VM has it. | |
1393 | // Essentially, this API makes no sense. We need it for AndroidRuntime, though :/. | |
1394 | ||
1395 | if (void *libnativehelper = dlopen("libnativehelper.so", RTLD_LAZY | RTLD_GLOBAL)) { | |
1396 | class JniInvocation$; | |
1397 | JniInvocation$ *(*JniInvocation$$init$)(JniInvocation$ *self)(NULL); | |
1398 | bool (*JniInvocation$Init)(JniInvocation$ *self, const char *library)(NULL); | |
1399 | JniInvocation$ *(*JniInvocation$finalize)(JniInvocation$ *self)(NULL); | |
1400 | ||
1401 | dlset(JniInvocation$$init$, "_ZN13JniInvocationC1Ev", libnativehelper); | |
1402 | dlset(JniInvocation$Init, "_ZN13JniInvocation4InitEPKc", libnativehelper); | |
1403 | dlset(JniInvocation$finalize, "_ZN13JniInvocationD1Ev", libnativehelper); | |
1404 | ||
95be1645 JF |
1405 | if (JniInvocation$$init$ == NULL) |
1406 | dlclose(libnativehelper); | |
1407 | else { | |
1408 | // XXX: we should attach a pool to the VM itself and deallocate this there | |
1409 | //auto invocation(pool.calloc<JniInvocation$>(1, 1024)); | |
1410 | //_assert(JniInvocation$finalize != NULL); | |
1411 | //pool.atexit(reinterpret_cast<void (*)(void *)>(JniInvocation$finalize), invocation); | |
1412 | ||
1413 | auto invocation(static_cast<JniInvocation$ *>(calloc(1, 1024))); | |
1414 | JniInvocation$$init$(invocation); | |
1415 | ||
1416 | _assert(JniInvocation$Init != NULL); | |
1417 | JniInvocation$Init(invocation, NULL); | |
1418 | ||
1419 | dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", libnativehelper); | |
f3e85e94 JF |
1420 | if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) |
1421 | return jvm; | |
95be1645 | 1422 | } |
f2c357f9 JF |
1423 | } |
1424 | ||
f3e85e94 JF |
1425 | JavaVM *jvm; |
1426 | JNIEnv *env; | |
1427 | ||
f2c357f9 JF |
1428 | if (void *libandroid_runtime = dlopen("libandroid_runtime.so", RTLD_LAZY | RTLD_GLOBAL)) { |
1429 | class AndroidRuntime$; | |
1430 | AndroidRuntime$ *(*AndroidRuntime$$init$)(AndroidRuntime$ *self, char *args, unsigned int size)(NULL); | |
f3e85e94 JF |
1431 | int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **env)(NULL); |
1432 | int (*AndroidRuntime$startReg)(JNIEnv *env)(NULL); | |
f2c357f9 JF |
1433 | int (*AndroidRuntime$addOption)(AndroidRuntime$ *self, const char *option, void *extra)(NULL); |
1434 | int (*AndroidRuntime$addVmArguments)(AndroidRuntime$ *self, int, const char *const argv[])(NULL); | |
1435 | AndroidRuntime$ *(*AndroidRuntime$finalize)(AndroidRuntime$ *self)(NULL); | |
1436 | ||
1437 | dlset(AndroidRuntime$$init$, "_ZN7android14AndroidRuntimeC1EPcj", libandroid_runtime); | |
1438 | dlset(AndroidRuntime$startVm, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnv", libandroid_runtime); | |
1439 | dlset(AndroidRuntime$startReg, "_ZN7android14AndroidRuntime8startRegEP7_JNIEnv", libandroid_runtime); | |
1440 | dlset(AndroidRuntime$addOption, "_ZN7android14AndroidRuntime9addOptionEPKcPv", libandroid_runtime); | |
1441 | dlset(AndroidRuntime$addVmArguments, "_ZN7android14AndroidRuntime14addVmArgumentsEiPKPKc", libandroid_runtime); | |
1442 | dlset(AndroidRuntime$finalize, "_ZN7android14AndroidRuntimeD1Ev", libandroid_runtime); | |
1443 | ||
1444 | // XXX: it would also be interesting to attach this to a global pool | |
1445 | AndroidRuntime$ *runtime(pool.calloc<AndroidRuntime$>(1, 1024)); | |
1446 | ||
1447 | _assert(AndroidRuntime$$init$ != NULL); | |
1448 | AndroidRuntime$$init$(runtime, NULL, 0); | |
1449 | ||
1450 | if (AndroidRuntime$addOption == NULL) { | |
1451 | _assert(AndroidRuntime$addVmArguments != NULL); | |
1452 | std::vector<const char *> arguments; | |
1453 | for (const auto &option : options) | |
1454 | arguments.push_back(option.optionString); | |
1455 | AndroidRuntime$addVmArguments(runtime, arguments.size(), arguments.data()); | |
1456 | } else for (const auto &option : options) | |
1457 | AndroidRuntime$addOption(runtime, option.optionString, option.extraInfo); | |
1458 | ||
1459 | int failure; | |
1460 | ||
1461 | _assert(AndroidRuntime$startVm != NULL); | |
f3e85e94 | 1462 | failure = AndroidRuntime$startVm(runtime, &jvm, &env); |
f2c357f9 JF |
1463 | _assert(failure == 0); |
1464 | ||
1465 | _assert(AndroidRuntime$startReg != NULL); | |
f3e85e94 | 1466 | failure = AndroidRuntime$startReg(env); |
f2c357f9 JF |
1467 | _assert(failure == 0); |
1468 | ||
f3e85e94 | 1469 | return jvm; |
f2c357f9 JF |
1470 | } |
1471 | ||
1472 | jint (*$JNI_CreateJavaVM)(JavaVM **jvm, void **, void *); | |
1473 | dlset($JNI_CreateJavaVM, "JNI_CreateJavaVM", handle); | |
1474 | ||
1475 | JavaVMInitArgs args; | |
1476 | memset(&args, 0, sizeof(args)); | |
1477 | args.version = CYJavaVersion; | |
1478 | args.nOptions = options.size(); | |
1479 | args.options = options.data(); | |
f3e85e94 JF |
1480 | _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast<void **>(&env), &args)); |
1481 | return jvm; | |
f2c357f9 | 1482 | } |
fc664744 | 1483 | |
f45374f4 | 1484 | static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env) { |
f3e85e94 | 1485 | auto jvm(CYGetJavaVM(context)); |
f45374f4 | 1486 | _assert(jvm != NULL); |
f3e85e94 JF |
1487 | _jnicall(jvm->GetEnv(reinterpret_cast<void **>(&env), CYJavaVersion)); |
1488 | CYJavaEnv jni(env); | |
1489 | ||
f45374f4 JF |
1490 | // XXX: this happens once per stub :/ |
1491 | ||
f2c357f9 JF |
1492 | auto Cycript$(jni.FindClass("Cycript")); |
1493 | jni.RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0])); | |
f3e85e94 | 1494 | |
f45374f4 JF |
1495 | JSObjectRef java(CYGetCachedObject(context, CYJSString("Java"))); |
1496 | JSValueRef arguments[1]; | |
1497 | arguments[0] = CYCastJSValue(context, CYJSString("setup")); | |
1498 | CYCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, java, CYJSString("emit"))), java, 1, arguments); | |
1499 | ||
f3e85e94 | 1500 | return env; |
c9c16dde JF |
1501 | } |
1502 | ||
4b645e23 | 1503 | static JSStaticValue JavaClass_staticValues[3] = { |
dbf05bfd | 1504 | {"class", &JavaClass_getProperty_class, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
4b645e23 | 1505 | {"$cyi", &JavaClass_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
dbf05bfd JF |
1506 | {NULL, NULL, NULL, 0} |
1507 | }; | |
1508 | ||
4b645e23 JF |
1509 | static JSStaticFunction JavaClass_staticFunctions[2] = { |
1510 | {"toCYON", &JavaClass_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1511 | {NULL, NULL, 0} | |
1512 | }; | |
1513 | ||
8effd381 | 1514 | static JSStaticValue JavaObject_staticValues[3] = { |
dbf05bfd | 1515 | {"constructor", &JavaObject_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
8effd381 | 1516 | {"$cyi", &JavaObject_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, |
dbf05bfd JF |
1517 | {NULL, NULL, NULL, 0} |
1518 | }; | |
1519 | ||
4b645e23 JF |
1520 | static JSStaticFunction JavaMethod_staticFunctions[2] = { |
1521 | {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1522 | {NULL, NULL, 0} | |
1523 | }; | |
1524 | ||
c9c16dde JF |
1525 | static JSStaticFunction JavaStaticMethod_staticFunctions[2] = { |
1526 | {"toCYON", &JavaStaticMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
1527 | {NULL, NULL, 0} | |
1528 | }; | |
1529 | ||
4b645e23 JF |
1530 | static JSStaticFunction JavaPackage_staticFunctions[2] = { |
1531 | {"toCYON", &JavaPackage_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, | |
dbf05bfd JF |
1532 | {NULL, NULL, 0} |
1533 | }; | |
1534 | ||
1535 | void CYJava_Initialize() { | |
1536 | Primitives_.insert(std::make_pair("void", CYJavaPrimitiveVoid)); | |
4b645e23 | 1537 | #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ |
42619b59 JF |
1538 | Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type)); |
1539 | CYJavaForEachPrimitive | |
1540 | #undef CYJavaForEachPrimitive_ | |
dbf05bfd JF |
1541 | |
1542 | JSClassDefinition definition; | |
1543 | ||
1544 | definition = kJSClassDefinitionEmpty; | |
1545 | definition.className = "JavaClass"; | |
1546 | definition.staticValues = JavaClass_staticValues; | |
4b645e23 | 1547 | definition.staticFunctions = JavaClass_staticFunctions; |
dbf05bfd JF |
1548 | definition.callAsConstructor = &JavaClass_callAsConstructor; |
1549 | definition.finalize = &CYFinalize; | |
d4222ffb | 1550 | CYPrivate<CYJavaClass>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1551 | |
8effd381 JF |
1552 | definition = kJSClassDefinitionEmpty; |
1553 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
1554 | definition.className = "JavaInterior"; | |
1555 | definition.hasProperty = &JavaInterior_hasProperty; | |
1556 | definition.getProperty = &JavaInterior_getProperty; | |
1557 | definition.setProperty = &JavaInterior_setProperty; | |
1558 | definition.getPropertyNames = &JavaInterior_getPropertyNames; | |
1559 | definition.finalize = &CYFinalize; | |
d4222ffb | 1560 | CYPrivate<CYJavaInterior>::Class_ = JSClassCreate(&definition); |
8effd381 | 1561 | |
dbf05bfd JF |
1562 | definition = kJSClassDefinitionEmpty; |
1563 | definition.className = "JavaMethod"; | |
4b645e23 | 1564 | definition.staticFunctions = JavaMethod_staticFunctions; |
dbf05bfd JF |
1565 | definition.callAsFunction = &JavaMethod_callAsFunction; |
1566 | definition.finalize = &CYFinalize; | |
d4222ffb | 1567 | CYPrivate<CYJavaMethod>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1568 | |
c9c16dde JF |
1569 | definition = kJSClassDefinitionEmpty; |
1570 | definition.className = "JavaStaticMethod"; | |
1571 | definition.staticFunctions = JavaStaticMethod_staticFunctions; | |
1572 | definition.callAsFunction = &JavaStaticMethod_callAsFunction; | |
1573 | definition.finalize = &CYFinalize; | |
d4222ffb | 1574 | CYPrivate<CYJavaStaticMethod>::Class_ = JSClassCreate(&definition); |
c9c16dde | 1575 | |
dbf05bfd JF |
1576 | definition = kJSClassDefinitionEmpty; |
1577 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
1578 | definition.className = "JavaObject"; | |
1579 | definition.staticValues = JavaObject_staticValues; | |
dbf05bfd | 1580 | definition.finalize = &CYFinalize; |
d4222ffb | 1581 | CYPrivate<CYJavaObject>::Class_ = JSClassCreate(&definition); |
dbf05bfd | 1582 | |
4b645e23 JF |
1583 | definition = kJSClassDefinitionEmpty; |
1584 | definition.className = "JavaArray"; | |
1585 | definition.getProperty = &JavaArray_getProperty; | |
1586 | definition.setProperty = &JavaArray_setProperty; | |
1587 | definition.finalize = &CYFinalize; | |
d4222ffb | 1588 | CYPrivate<CYJavaArray>::Class_ = JSClassCreate(&definition); |
4b645e23 | 1589 | |
dbf05bfd JF |
1590 | definition = kJSClassDefinitionEmpty; |
1591 | definition.className = "JavaPackage"; | |
1592 | definition.staticFunctions = JavaPackage_staticFunctions; | |
1593 | definition.hasProperty = &CYJavaPackage_hasProperty; | |
1594 | definition.getProperty = &CYJavaPackage_getProperty; | |
1595 | definition.finalize = &CYFinalize; | |
d4222ffb | 1596 | CYPrivate<CYJavaPackage>::Class_ = JSClassCreate(&definition); |
4b645e23 JF |
1597 | |
1598 | definition = kJSClassDefinitionEmpty; | |
1599 | definition.attributes = kJSClassAttributeNoAutomaticPrototype; | |
c9c16dde JF |
1600 | definition.className = "JavaStaticInterior"; |
1601 | definition.hasProperty = &JavaStaticInterior_hasProperty; | |
1602 | definition.getProperty = &JavaStaticInterior_getProperty; | |
1603 | definition.setProperty = &JavaStaticInterior_setProperty; | |
1604 | definition.getPropertyNames = &JavaStaticInterior_getPropertyNames; | |
4b645e23 | 1605 | definition.finalize = &CYFinalize; |
d4222ffb | 1606 | CYPrivate<CYJavaStaticInterior>::Class_ = JSClassCreate(&definition); |
dbf05bfd JF |
1607 | } |
1608 | ||
1609 | void CYJava_SetupContext(JSContextRef context) { | |
1610 | JSObjectRef global(CYGetGlobalObject(context)); | |
f45374f4 | 1611 | JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); |
dbf05bfd JF |
1612 | JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); |
1613 | JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all")))); | |
1614 | //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); | |
1615 | ||
1616 | JSObjectRef Java(JSObjectMake(context, NULL, NULL)); | |
1617 | CYSetProperty(context, cycript, CYJSString("Java"), Java); | |
f45374f4 | 1618 | CYSetProperty(context, cy, CYJSString("Java"), Java); |
dbf05bfd | 1619 | |
d4222ffb | 1620 | JSObjectRef Packages(CYPrivate<CYJavaPackage>::Make(context, nullptr, CYJavaPackage::Path())); |
f45374f4 | 1621 | CYSetProperty(context, all, CYJSString("Packages"), Packages, kJSPropertyAttributeDontEnum); |
dbf05bfd JF |
1622 | |
1623 | for (auto name : (const char *[]) {"java", "javax", "android", "com", "net", "org"}) { | |
1624 | CYJSString js(name); | |
d4222ffb | 1625 | CYSetProperty(context, all, js, CYPrivate<CYJavaPackage>::Make(context, nullptr, CYJavaPackage::Path(1, name)), kJSPropertyAttributeDontEnum); |
dbf05bfd JF |
1626 | } |
1627 | } | |
1628 | ||
1629 | static CYHook CYJavaHook = { | |
1630 | NULL, | |
1631 | NULL, | |
1632 | NULL, | |
1633 | &CYJava_Initialize, | |
1634 | &CYJava_SetupContext, | |
1635 | NULL, | |
1636 | }; | |
1637 | ||
1638 | CYRegisterHook CYJava(&CYJavaHook); |