1 /* Cycript - The Truly Universal Scripting Language
2 * Copyright (C) 2009-2016 Jay Freeman (saurik)
5 /* GNU Affero General Public License, Version 3 {{{ */
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.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
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/>.
28 #if defined(__APPLE__) && !defined(__arm__)
29 #include <JavaVM/jni.h>
35 // XXX: this is deprecated?!?!?!?!?!?!
36 #include <sys/system_properties.h>
39 #include "cycript.hpp"
41 #include "Execute.hpp"
42 #include "Internal.hpp"
43 #include "JavaScript.hpp"
44 #include "Pooling.hpp"
46 #define _jnicall(expr) ({ \
48 if (_value != JNI_OK) \
49 CYThrow("_jnicall(%s) == %d", #expr, _value); \
52 #define _envcall(jni, expr) ({ \
53 __typeof__(jni->expr) _value(jni->expr); \
54 if (jthrowable _error = jni->ExceptionOccurred()) { \
55 jni->ExceptionClear(); \
56 throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \
60 #define _envcallv(jni, expr) do { \
62 if (jthrowable _error = jni->ExceptionOccurred()) { \
63 jni->ExceptionClear(); \
64 throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \
70 auto &protect(*reinterpret_cast<CYProtect *>(jprotect)); \
71 _disused JSContextRef context(protect); \
72 _disused JSObjectRef object(protect); \
74 #define CYJavaCatch(value) \
75 catch (const CYException &error) { \
76 jni->Throw(CYCastJavaObject(jni, context, error.CastJSValue(context, "Error")).cast<jthrowable>()); \
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)
90 enum CYJavaPrimitive
: char {
91 CYJavaPrimitiveObject
,
93 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
94 CYJavaPrimitive ## Type,
95 CYJavaForEachPrimitive
96 #undef CYJavaForEachPrimitive_
99 template <typename Type_
>
100 struct IsJavaPrimitive
{ static const bool value
= false; };
102 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
104 struct IsJavaPrimitive<j ## type> { static const bool value = true; };
105 CYJavaForEachPrimitive
106 #undef CYJavaForEachPrimitive_
108 // Java References {{{
109 template <typename Value_
>
114 _finline
CYJavaRef(JNIEnv
*jni
, Value_ value
) :
120 _finline
operator Value_() const {
124 _finline JNIEnv
*jni() const {
128 // XXX: this is only needed to support CYJavaEnv relying on C variadics
129 _finline Value_
get() const {
133 template <typename Other_
>
134 _finline CYJavaRef
<Other_
> cast() const {
135 return {jni_
, static_cast<Other_
>(value_
)};
138 // XXX: this should be tied into CYJavaFrame
140 Value_
value(value_
);
146 template <typename Value_
, void (JNIEnv::*Delete_
)(jobject
)>
147 struct CYJavaDelete
:
150 _finline
CYJavaDelete(JNIEnv
*jni
, Value_ value
) :
151 CYJavaRef
<Value_
>(jni
, value
)
156 if (this->value_
!= NULL
)
157 (this->jni_
->*Delete_
)(this->value_
);
166 template <typename Value_
>
167 struct CYJavaGlobal
:
168 CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
>
170 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
> CYJavaBase
;
173 CYJavaBase(NULL
, NULL
)
177 template <typename Other_
>
178 CYJavaGlobal(const CYJavaRef
<Other_
> &other
) :
179 CYJavaBase(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewGlobalRef(other
.value_
)))
183 CYJavaGlobal(const CYJavaGlobal
<Value_
> &other
) :
184 CYJavaGlobal(static_cast<const CYJavaRef
<Value_
> &>(other
))
188 CYJavaGlobal(CYJavaGlobal
&&value
) :
189 CYJavaBase(value
.jni_
, value
.value_
)
194 template <typename Other_
>
195 CYJavaGlobal
&operator =(const CYJavaRef
<Other_
> &other
) {
196 this->~CYJavaGlobal();
197 new (this) CYJavaGlobal(other
);
202 template <typename Value_
>
204 CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
>
206 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
> CYJavaBase
;
209 CYJavaBase(NULL
, NULL
)
213 CYJavaLocal(JNIEnv
*jni
, Value_ value
) :
214 CYJavaBase(jni
, value
)
218 template <typename Other_
>
219 CYJavaLocal(const CYJavaRef
<Other_
> &other
) :
220 CYJavaLocal(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewLocalRef(other
.value_
)))
224 template <typename Other_
>
225 CYJavaLocal(CYJavaRef
<Other_
> &&other
) :
226 CYJavaLocal(other
.jni_
, other
.value_
)
231 CYJavaLocal(CYJavaLocal
&&other
) :
232 CYJavaLocal(static_cast<CYJavaRef
<Value_
> &&>(other
))
236 template <typename Other_
>
237 CYJavaLocal
&operator =(CYJavaLocal
<Other_
> &&other
) {
239 this->jni_
= other
.jni_
;
240 this->value_
= other
.value_
;
247 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaRef
<jobject
> &value
);
249 class CYJavaUTF8String
:
253 const CYJavaRef
<jstring
> &value_
;
256 CYJavaUTF8String(const CYJavaRef
<jstring
> &value
) :
260 JNIEnv
*jni(value
.jni());
261 size
= jni
->GetStringUTFLength(value
);
262 data
= jni
->GetStringUTFChars(value
, NULL
);
265 CYJavaUTF8String(CYJavaRef
<jstring
> &&) = delete;
267 ~CYJavaUTF8String() {
268 JNIEnv
*jni(value_
.jni());
269 jni
->ReleaseStringUTFChars(value_
, data
);
272 CYJavaUTF8String(const CYJavaUTF8String
&) = delete;
275 CYJavaUTF8String
CYCastUTF8String(const CYJavaRef
<jstring
> &value
) {
279 JSStringRef
CYCopyJSString(const CYJavaRef
<jstring
> &value
) {
280 return CYCopyJSString(CYCastUTF8String(value
));
287 CYJavaGlobal
<jthrowable
> value_
;
289 CYJavaError(const CYJavaRef
<jthrowable
> &value
) :
294 virtual const char *PoolCString(CYPool
&pool
) const {
295 auto string(CYCastJavaString(value_
.cast
<jobject
>()));
296 return CYPoolCString(pool
, CYJavaUTF8String(string
));
299 virtual JSValueRef
CastJSValue(JSContextRef context
, const char *name
) const;
306 CYJavaFrame(JNIEnv
*jni
, jint capacity
) :
309 _assert(jni
->PushLocalFrame(capacity
) == 0);
316 operator JNIEnv
*() const {
320 jobject
operator ()(jobject object
) {
323 return jni
->PopLocalFrame(object
);
332 CYJavaEnv(JNIEnv
*jni
) :
337 template <typename Other_
>
338 CYJavaEnv(const CYJavaRef
<Other_
> &value
) :
343 operator JNIEnv
*() const {
347 JNIEnv
*operator ->() const {
351 CYJavaLocal
<jclass
> FindClass(const char *name
) const {
352 return {jni
, _envcall(jni
, FindClass(name
))};
355 CYJavaLocal
<jclass
> FindClass$
(const char *name
) const {
356 jclass
value(jni
->FindClass(name
));
357 jni
->ExceptionClear();
361 CYJavaLocal
<jclass
> GetObjectClass(jobject object
) const {
362 return {jni
, _envcall(jni
, GetObjectClass(object
))};
365 CYJavaLocal
<jclass
> GetSuperclass(jclass _class
) const {
366 return {jni
, _envcall(jni
, GetSuperclass(_class
))};
369 CYJavaLocal
<jobject
> NewObject(jclass _class
, jmethodID method
, ...) const {
371 va_start(args
, method
);
372 jobject
object(_envcall(jni
, NewObjectV(_class
, method
, args
)));
374 return {jni
, object
};
377 CYJavaLocal
<jobject
> NewObjectA(jclass _class
, jmethodID method
, jvalue
*args
) const {
378 return {jni
, _envcall(jni
, NewObjectA(_class
, method
, args
))};
381 CYJavaLocal
<jobjectArray
> NewObjectArray(jsize length
, jclass _class
, jobject initial
) const {
382 return {jni
, _envcall(jni
, NewObjectArray(length
, _class
, initial
))};
385 CYJavaLocal
<jstring
> NewString(const jchar
*data
, jsize size
) const {
386 return {jni
, _envcall(jni
, NewString(data
, size
))};
389 #define CYJavaEnv_(Code) \
390 template <typename... Args_> \
391 void Code(Args_ &&... args) const { \
392 _envcallv(jni, Code(cy::Forward<Args_>(args)...)); \
395 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
396 CYJavaEnv_(Get ## Typ ## ArrayRegion) \
397 CYJavaEnv_(Set ## Typ ## Field) \
398 CYJavaEnv_(SetStatic ## Typ ## Field)
399 CYJavaForEachPrimitive
400 #undef CYJavaForEachPrimitive_
402 CYJavaEnv_(CallVoidMethod
)
403 CYJavaEnv_(CallStaticVoidMethod
)
404 CYJavaEnv_(CallVoidMethodA
)
405 CYJavaEnv_(CallStaticVoidMethodA
)
406 CYJavaEnv_(SetObjectArrayElement
)
407 CYJavaEnv_(SetObjectField
)
408 CYJavaEnv_(SetStaticObjectField
)
411 #define CYJavaEnv_(Code) \
412 template <typename... Args_> \
413 auto Code(Args_ &&... args) const -> decltype(jni->Code(cy::Forward<Args_>(args)...)) { \
414 return _envcall(jni, Code(cy::Forward<Args_>(args)...)); \
417 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
418 CYJavaEnv_(Call ## Typ ## Method) \
419 CYJavaEnv_(CallStatic ## Typ ## Method) \
420 CYJavaEnv_(Call ## Typ ## MethodA) \
421 CYJavaEnv_(CallStatic ## Typ ## MethodA) \
422 CYJavaEnv_(Get ## Typ ## Field) \
423 CYJavaEnv_(GetStatic ## Typ ## Field)
424 CYJavaForEachPrimitive
425 #undef CYJavaForEachPrimitive_
427 CYJavaEnv_(FromReflectedField
)
428 CYJavaEnv_(FromReflectedMethod
)
429 CYJavaEnv_(GetArrayLength
)
430 CYJavaEnv_(GetMethodID
)
431 CYJavaEnv_(GetStaticMethodID
)
432 CYJavaEnv_(IsSameObject
)
433 CYJavaEnv_(RegisterNatives
)
436 #define CYJavaEnv_(Code) \
437 template <typename Other_, typename... Args_> \
438 auto Code(Args_ &&... args) const -> CYJavaLocal<Other_> { \
439 return {jni, static_cast<Other_>(_envcall(jni, Code(cy::Forward<Args_>(args)...)))}; \
442 CYJavaEnv_(CallObjectMethod
)
443 CYJavaEnv_(CallStaticObjectMethod
)
444 CYJavaEnv_(CallObjectMethodA
)
445 CYJavaEnv_(CallStaticObjectMethodA
)
446 CYJavaEnv_(GetObjectArrayElement
)
447 CYJavaEnv_(GetObjectField
)
448 CYJavaEnv_(GetStaticObjectField
)
452 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaRef
<jobject
> &value
) {
453 CYJavaEnv
jni(value
);
454 auto Object$
(jni
.FindClass("java/lang/Object"));
455 auto Object$
toString(jni
.GetMethodID(Object$
, "toString", "()Ljava/lang/String;"));
456 return jni
.CallObjectMethod
<jstring
>(value
, Object$toString
);
459 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, CYUTF16String value
) {
460 return jni
.NewString(value
.data
, value
.size
);
463 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, CYUTF8String value
) {
464 // XXX: if there are no nulls then you can do this faster
466 return CYCastJavaString(jni
, CYPoolUTF16String(pool
, value
));
469 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, const char *value
) {
470 return CYCastJavaString(jni
, CYUTF8String(value
));
473 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, JSContextRef context
, JSStringRef value
) {
474 return CYCastJavaString(jni
, CYCastUTF16String(value
));
477 static JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<jobject
> &value
);
479 template <typename Other_
>
480 static _finline JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<Other_
> &value
) {
481 return CYCastJSValue(context
, value
.template cast
<jobject
>());
484 template <typename Type_
>
485 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, Type_ value
) {
486 return CYCastJSValue(context
, value
);
489 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, jboolean value
) {
490 return CYCastJSValue(context
, static_cast<bool>(value
));
493 JSValueRef
CYJavaError::CastJSValue(JSContextRef context
, const char *name
) const {
494 return CYCastJSValue(context
, value_
);
500 CYJavaGlobal
<jobject
> loader_
;
501 jmethodID ClassLoader$loadClass
;
506 bool Setup(JNIEnv
*env
) {
513 auto ClassLoader$
(jni
.FindClass("java/lang/ClassLoader"));
514 ClassLoader$loadClass
= jni
.GetMethodID(ClassLoader$
, "loadClass", "(Ljava/lang/String;)Ljava/lang/Class;");
516 auto ClassLoader$
getSystemClassLoader(jni
.GetStaticMethodID(ClassLoader$
, "getSystemClassLoader", "()Ljava/lang/ClassLoader;"));
517 auto parent(jni
.CallStaticObjectMethod
<jobject
>(ClassLoader$
, ClassLoader$getSystemClassLoader
));
519 auto path(CYCastJavaString(jni
, pool
.strcat("file://", CYPoolLibraryPath(pool
), "/libcycript.jar", NULL
)));
521 auto PathClassLoader$
(jni
.FindClass$
("dalvik/system/PathClassLoader"));
522 if (PathClassLoader$
) {
523 auto PathClassLoader$$init$
(jni
.GetMethodID(PathClassLoader$
, "<init>", "(Ljava/lang/String;Ljava/lang/ClassLoader;)V"));
524 loader_
= jni
.NewObject(PathClassLoader$
, PathClassLoader$$init$
, path
.get(), parent
.get());
526 auto URL$
(jni
.FindClass("java/net/URL"));
527 auto URL$$init$
(jni
.GetMethodID(URL$
, "<init>", "(Ljava/lang/String;)V"));
529 auto URLClassLoader$
(jni
.FindClass("java/net/URLClassLoader"));
530 auto URLClassLoader$$init$
(jni
.GetMethodID(URLClassLoader$
, "<init>", "([Ljava/net/URL;Ljava/lang/ClassLoader;)V"));
532 auto url(jni
.NewObject(URL$
, URL$$init$
, path
.get()));
533 auto urls(jni
.NewObjectArray(1, URL$
, url
));
534 // XXX: .cast().get() <- this is required :/
535 loader_
= jni
.NewObject(URLClassLoader$
, URLClassLoader$$init$
, urls
.cast
<jobject
>().get(), parent
.get());
541 CYJavaLocal
<jclass
> LoadClass(const char *name
) {
543 CYJavaEnv
jni(loader_
);
544 return jni
.CallObjectMethod
<jclass
>(loader_
, ClassLoader$loadClass
, CYCastJavaString(jni
, name
).get());
548 static std::map
<std::string
, CYJavaPrimitive
> Primitives_
;
550 static CYJavaPrimitive
CYJavaGetPrimitive(JSContextRef context
, const CYJavaRef
<jclass
> &type
, jmethodID Class$get$$Name
) {
552 auto string(jni
.CallObjectMethod
<jstring
>(type
, Class$get$$Name
));
555 CYJavaUTF8String
name(string
);
556 auto primitive(Primitives_
.find(name
));
557 return primitive
!= Primitives_
.end() ? primitive
->second
: CYJavaPrimitiveObject
;
560 typedef std::vector
<CYJavaPrimitive
> CYJavaShorty
;
562 static CYJavaShorty
CYJavaGetShorty(JSContextRef context
, const CYJavaRef
<jobjectArray
> &types
, jmethodID Class$get$$Name
) {
563 CYJavaEnv
jni(types
);
564 size_t count(jni
.GetArrayLength(types
));
565 CYJavaShorty
shorty(count
);
566 for (size_t index(0); index
!= count
; ++index
)
567 shorty
[index
] = CYJavaGetPrimitive(context
, jni
.GetObjectArrayElement
<jclass
>(types
, index
), Class$get$$Name
);
573 CYJavaPrimitive primitive_
;
576 typedef std::map
<std::string
, CYJavaField
> CYJavaFieldMap
;
578 struct CYJavaSignature
{
579 CYJavaGlobal
<jobject
> reflected_
;
581 CYJavaPrimitive primitive_
;
582 CYJavaShorty shorty_
;
584 CYJavaSignature(const CYJavaRef
<jobject
> &reflected
, jmethodID method
, CYJavaPrimitive primitive
, const CYJavaShorty
&shorty
) :
585 reflected_(reflected
),
587 primitive_(primitive
),
592 // XXX: the shorty doesn't store enough information
593 bool operator <(const CYJavaSignature
&rhs
) const {
594 return shorty_
< rhs
.shorty_
;
598 typedef std::set
<CYJavaSignature
> CYJavaOverload
;
599 typedef std::map
<unsigned, CYJavaOverload
> CYJavaOverloads
;
601 struct CYJavaMethod
:
606 CYJavaOverloads overloads_
;
608 CYJavaMethod(CYUTF8String type
, CYUTF8String name
, const CYJavaOverloads
&overloads
) :
609 type_(CYPoolUTF8String(*pool_
, type
)),
610 name_(CYPoolUTF8String(*pool_
, name
)),
611 overloads_(overloads
)
616 struct CYJavaInstanceMethod
:
619 using CYJavaMethod::CYJavaMethod
;
622 struct CYJavaStaticMethod
:
625 using CYJavaMethod::CYJavaMethod
;
631 CYJavaGlobal
<jclass
> value_
;
634 CYJavaFieldMap static_
;
635 CYJavaFieldMap instance_
;
636 CYJavaOverloads overloads_
;
638 CYJavaClass(const CYJavaRef
<jclass
> &value
, bool interface
) :
640 interface_(interface
)
645 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &_class
);
647 struct CYJavaObject
:
650 CYJavaGlobal
<jobject
> value_
;
653 CYJavaObject(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
659 JSValueRef
GetPrototype(JSContextRef context
) const;
662 struct CYJavaInterior
:
665 CYJavaGlobal
<jobject
> value_
;
668 CYJavaInterior(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
675 struct CYJavaStaticInterior
:
678 CYJavaGlobal
<jclass
> value_
;
681 CYJavaStaticInterior(const CYJavaRef
<jclass
> &value
, CYJavaClass
*table
) :
691 CYJavaGlobal
<jarray
> value_
;
692 CYJavaPrimitive primitive_
;
694 CYJavaArray(const CYJavaRef
<jarray
> &value
, CYJavaPrimitive primitive
) :
696 primitive_(primitive
)
700 JSValueRef
GetPrototype(JSContextRef context
) const;
703 struct CYJavaPackage
:
708 typedef std::vector
<std::string
> Path
;
711 _finline
CYJavaPackage(JNIEnv
*jni
, const Path
&package
) :
718 JSValueRef
CYJavaObject::GetPrototype(JSContextRef context
) const {
719 CYJavaEnv
jni(value_
);
720 return CYGetProperty(context
, CYGetJavaClass(context
, jni
.GetObjectClass(value_
)), prototype_s
);
723 JSValueRef
CYJavaArray::GetPrototype(JSContextRef context
) const {
724 return CYGetCachedObject(context
, CYJSString("Array_prototype"));
727 static JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
729 return CYJSNull(context
);
730 CYJavaEnv
jni(value
);
732 auto _class(jni
.GetObjectClass(value
));
733 if (jni
.IsSameObject(_class
, jni
.FindClass("java/lang/String")))
734 return CYCastJSValue(context
, CYJSString(value
.cast
<jstring
>()));
736 auto Class$
(jni
.FindClass("java/lang/Class"));
737 auto Class$
isArray(jni
.GetMethodID(Class$
, "isArray", "()Z"));
738 if (jni
.CallBooleanMethod(_class
, Class$isArray
)) {
739 auto Class$
getComponentType(jni
.GetMethodID(Class$
, "getComponentType", "()Ljava/lang/Class;"));
740 auto component(jni
.CallObjectMethod
<jclass
>(_class
, Class$getComponentType
));
741 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
742 return CYPrivate
<CYJavaArray
>::Make(context
, value
.cast
<jarray
>(), CYJavaGetPrimitive(context
, component
, Class$getName
));
745 auto java(reinterpret_cast<CYJava
*>(JSObjectGetPrivate(CYGetCachedObject(context
, CYJSString("Java")))));
746 auto Wrapper$
(java
->LoadClass("Cycript$Wrapper"));
748 if (jni
.IsSameObject(_class
, Wrapper$
)) {
749 auto Wrapper$
getProtect(jni
.GetMethodID(Wrapper$
, "getProtect", "()J"));
750 auto &protect(*reinterpret_cast<CYProtect
*>(jni
.CallLongMethod(value
, Wrapper$getProtect
)));
754 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(CYGetJavaClass(context
, _class
))));
755 return CYPrivate
<CYJavaObject
>::Make(context
, value
, table
);
758 static _finline JSObjectRef
CYCastJSObject(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
759 return CYCastJSObject(context
, CYCastJSValue(context
, value
));
762 #define CYCastJava$(T, Type, jtype, Cast) \
763 _disused static CYJavaLocal<jobject> CYCastJava ## Type(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { \
764 auto Type$(jni.FindClass("java/lang/" #Type)); \
765 auto Type$init$(jni.GetMethodID(Type$, "<init>", "(" #T ")V")); \
766 return jni.NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value))); \
769 CYCastJava$
(Z
, Boolean
, jboolean
, CYCastBool
)
770 CYCastJava$
(B
, Byte
, jbyte
, CYCastDouble
)
771 CYCastJava$
(C
, Character
, jchar
, CYCastDouble
)
772 CYCastJava$
(S
, Short
, jshort
, CYCastDouble
)
773 CYCastJava$
(I
, Integer
, jint
, CYCastDouble
)
774 CYCastJava$
(J
, Long
, jlong
, CYCastDouble
)
775 CYCastJava$
(F
, Float
, jfloat
, CYCastDouble
)
776 CYCastJava$
(D
, Double
, jdouble
, CYCastDouble
)
778 static CYJavaClass
*CYGetJavaTable(JSContextRef context
, JSObjectRef object
) {
779 if (!JSValueIsObjectOfClass(context
, object
, CYPrivate
<CYJavaClass
>::Class_
))
781 return reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
));
784 static CYJavaObject
*CYGetJavaObject(JSContextRef context
, JSObjectRef object
) {
785 if (!JSValueIsObjectOfClass(context
, object
, CYPrivate
<CYJavaObject
>::Class_
))
787 return reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
));
790 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSObjectRef value
) {
791 if (CYJavaObject
*internal
= CYGetJavaObject(context
, value
))
792 return internal
->value_
;
794 auto java(reinterpret_cast<CYJava
*>(JSObjectGetPrivate(CYGetCachedObject(context
, CYJSString("Java")))));
795 auto Wrapper$
(java
->LoadClass("Cycript$Wrapper"));
797 auto Wrapper$$init$
(jni
.GetMethodID(Wrapper$
, "<init>", "(J)V"));
798 CYProtect
*protect(new CYProtect(context
, value
));
799 return jni
.NewObject(Wrapper$
, Wrapper$$init$
, reinterpret_cast<jlong
>(protect
));
802 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSValueRef value
) {
803 switch (JSValueGetType(context
, value
)) {
807 return CYCastJavaBoolean(jni
, context
, value
);
809 return CYCastJavaDouble(jni
, context
, value
);
811 return CYCastJavaString(jni
, context
, CYJSString(context
, value
));
813 return CYCastJavaObject(jni
, context
, CYCastJSObject(context
, value
));
815 case kJSTypeUndefined
:
816 // XXX: I am currently relying on this for dynamic proxy of void method
823 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &value
) {
824 CYJavaEnv
jni(value
);
825 CYJavaFrame
frame(jni
, 64);
827 JSObjectRef
global(CYGetGlobalObject(context
));
828 JSObjectRef
cy(CYCastJSObject(context
, CYGetProperty(context
, global
, cy_s
)));
830 auto Class$
(jni
.FindClass("java/lang/Class"));
831 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
833 auto string(jni
.CallObjectMethod
<jstring
>(value
, Class$getName
));
834 CYJavaUTF8String
utf8(string
);
835 CYJSString
name(utf8
);
837 JSValueRef
cached(CYGetProperty(context
, cy
, name
));
838 if (!JSValueIsUndefined(context
, cached
))
839 return CYCastJSObject(context
, cached
);
841 JSObjectRef constructor
;
842 JSObjectRef prototype
;
846 auto Class$
isInterface(jni
.GetMethodID(Class$
, "isInterface", "()Z"));
848 auto Class$
getDeclaredConstructors(jni
.GetMethodID(Class$
, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;"));
849 auto Class$
getDeclaredFields(jni
.GetMethodID(Class$
, "getDeclaredFields", "()[Ljava/lang/reflect/Field;"));
850 auto Class$
getDeclaredMethods(jni
.GetMethodID(Class$
, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;"));
852 auto Constructor$
(jni
.FindClass("java/lang/reflect/Constructor"));
853 //auto Constructor$getModifiers(jni.GetMethodID(Constructor$, "getModifiers", "()I"));
854 auto Constructor$
getParameterTypes(jni
.GetMethodID(Constructor$
, "getParameterTypes", "()[Ljava/lang/Class;"));
856 auto Field$
(jni
.FindClass("java/lang/reflect/Field"));
857 auto Field$
getModifiers(jni
.GetMethodID(Field$
, "getModifiers", "()I"));
858 auto Field$
getName(jni
.GetMethodID(Field$
, "getName", "()Ljava/lang/String;"));
859 auto Field$
getType(jni
.GetMethodID(Field$
, "getType", "()Ljava/lang/Class;"));
861 auto Method$
(jni
.FindClass("java/lang/reflect/Method"));
862 auto Method$
getModifiers(jni
.GetMethodID(Method$
, "getModifiers", "()I"));
863 auto Method$
getName(jni
.GetMethodID(Method$
, "getName", "()Ljava/lang/String;"));
864 auto Method$
getParameterTypes(jni
.GetMethodID(Method$
, "getParameterTypes", "()[Ljava/lang/Class;"));
865 auto Method$
getReturnType(jni
.GetMethodID(Method$
, "getReturnType", "()Ljava/lang/Class;"));
867 auto Modifier$
(jni
.FindClass("java/lang/reflect/Modifier"));
868 auto Modifier$
isStatic(jni
.GetStaticMethodID(Modifier$
, "isStatic", "(I)Z"));
870 auto interface(jni
.CallBooleanMethod(value
, Class$isInterface
));
871 auto table(new CYJavaClass(value
, interface
));
873 for (CYJavaLocal
<jclass
> prototype(value
); prototype
; prototype
= jni
.GetSuperclass(prototype
)) {
874 auto fields(jni
.CallObjectMethod
<jobjectArray
>(prototype
, Class$getDeclaredFields
));
876 for (jsize
i(0), e(jni
.GetArrayLength(fields
)); i
!= e
; ++i
) {
877 auto field(jni
.GetObjectArrayElement
<jobject
>(fields
, e
- i
- 1));
878 auto modifiers(jni
.CallIntMethod(field
, Field$getModifiers
));
879 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
880 auto &map(instance
? table
->instance_
: table
->static_
);
881 auto string(jni
.CallObjectMethod
<jstring
>(field
, Field$getName
));
882 CYJavaUTF8String
name(string
);
883 auto id(jni
.FromReflectedField(field
));
884 auto type(jni
.CallObjectMethod
<jclass
>(field
, Field$getType
));
885 map
.insert(std::make_pair(std::string(name
), CYJavaField
{id
, CYJavaGetPrimitive(context
, type
, Class$getName
)}));
889 constructor
= JSObjectMake(context
, CYPrivate
<CYJavaClass
>::Class_
, table
);
891 prototype
= JSObjectMake(context
, NULL
, NULL
);
892 CYSetProperty(context
, constructor
, prototype_s
, prototype
, kJSPropertyAttributeDontEnum
);
894 auto constructors(jni
.CallObjectMethod
<jobjectArray
>(value
, Class$getDeclaredConstructors
));
896 for (jsize
i(0), e(jni
.GetArrayLength(constructors
)); i
!= e
; ++i
) {
897 auto constructor(jni
.GetObjectArrayElement
<jobject
>(constructors
, i
));
898 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(constructor
, Constructor$getParameterTypes
));
899 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
900 auto id(jni
.FromReflectedMethod(constructor
));
901 table
->overloads_
[shorty
.size()].insert(CYJavaSignature(constructor
, id
, CYJavaPrimitiveObject
, shorty
));
904 std::map
<std::pair
<bool, std::string
>, CYJavaOverloads
> entries
;
907 for (CYJavaLocal
<jclass
> prototype(value
); prototype
; prototype
= jni
.GetSuperclass(prototype
)) {
908 auto methods(jni
.CallObjectMethod
<jobjectArray
>(prototype
, Class$getDeclaredMethods
));
910 for (jsize
i(0), e(jni
.GetArrayLength(methods
)); i
!= e
; ++i
) {
911 auto method(jni
.GetObjectArrayElement
<jobject
>(methods
, i
));
912 auto modifiers(jni
.CallIntMethod(method
, Method$getModifiers
));
913 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
914 auto string(jni
.CallObjectMethod
<jstring
>(method
, Method$getName
));
915 CYJavaUTF8String
name(string
);
917 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(method
, Method$getParameterTypes
));
918 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
920 CYJavaOverload
*overload
;
922 overload
= &entries
[std::make_pair(instance
, std::string(name
))][shorty
.size()];
924 auto entry(entries
.find(std::make_pair(instance
, std::string(name
))));
925 if (entry
== entries
.end())
927 overload
= &entry
->second
[shorty
.size()];
930 auto type(jni
.CallObjectMethod
<jclass
>(method
, Method$getReturnType
));
931 auto primitive(CYJavaGetPrimitive(context
, type
, Class$getName
));
932 auto id(jni
.FromReflectedMethod(method
));
933 overload
->insert(CYJavaSignature(method
, id
, primitive
, shorty
));
939 for (const auto &entry
: entries
) {
940 bool instance(entry
.first
.first
);
941 CYJSString
method(entry
.first
.second
);
942 auto &overload(entry
.second
);
944 CYSetProperty(context
, prototype
, method
, CYPrivate
<CYJavaInstanceMethod
>::Make(context
, utf8
, entry
.first
.second
.c_str(), overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
946 CYSetProperty(context
, constructor
, method
, CYPrivate
<CYJavaStaticMethod
>::Make(context
, utf8
, entry
.first
.second
.c_str(), overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
951 // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name
952 // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :(
954 if (auto super
= jni
.GetSuperclass(value
)) {
955 JSObjectRef
parent(CYGetJavaClass(context
, super
));
956 CYSetPrototype(context
, constructor
, parent
);
957 CYSetPrototype(context
, prototype
, CYGetProperty(context
, parent
, prototype_s
));
960 CYSetProperty(context
, cy
, name
, constructor
);
964 static void CYCastJavaNumeric(jvalue
&value
, CYJavaPrimitive primitive
, JSContextRef context
, JSValueRef argument
) {
966 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
967 case CYJavaPrimitive ## Type: \
968 value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \
970 CYJavaForEachPrimitive
971 #undef CYJavaForEachPrimitive_
977 static bool CYCastJavaArguments(const CYJavaFrame
&frame
, const CYJavaShorty
&shorty
, JSContextRef context
, const JSValueRef arguments
[], jvalue
*array
) {
978 CYJavaEnv
jni(frame
);
980 for (size_t index(0); index
!= shorty
.size(); ++index
) {
981 JSValueRef
argument(arguments
[index
]);
982 JSType
type(JSValueGetType(context
, argument
));
983 jvalue
&value(array
[index
]);
985 switch (CYJavaPrimitive primitive
= shorty
[index
]) {
986 case CYJavaPrimitiveObject
:
987 // XXX: figure out a way to tie this in to the CYJavaFrame
988 value
.l
= CYCastJavaObject(jni
, context
, argument
).leak();
991 case CYJavaPrimitiveBoolean
:
992 if (type
!= kJSTypeBoolean
)
994 value
.z
= CYCastBool(context
, argument
);
997 case CYJavaPrimitiveCharacter
:
998 if (type
== kJSTypeNumber
)
999 CYCastJavaNumeric(value
, primitive
, context
, argument
);
1000 else if (type
!= kJSTypeString
)
1003 CYJSString
string(context
, argument
);
1004 if (JSStringGetLength(string
) != 1)
1007 value
.c
= JSStringGetCharactersPtr(string
)[0];
1011 case CYJavaPrimitiveByte
:
1012 case CYJavaPrimitiveShort
:
1013 case CYJavaPrimitiveInteger
:
1014 case CYJavaPrimitiveLong
:
1015 case CYJavaPrimitiveFloat
:
1016 case CYJavaPrimitiveDouble
:
1017 if (type
!= kJSTypeNumber
)
1019 CYCastJavaNumeric(value
, primitive
, context
, argument
);
1030 static JSValueRef
JavaInstanceMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1031 auto internal(CYPrivate
<CYJavaInstanceMethod
>::Get(context
, object
));
1032 CYJavaObject
*self(CYGetJavaObject(context
, _this
));
1033 _assert(self
!= NULL
);
1034 CYJavaEnv
jni(self
->value_
);
1036 auto overload(internal
->overloads_
.find(count
));
1037 if (overload
!= internal
->overloads_
.end())
1038 for (auto signature(overload
->second
.begin()); signature
!= overload
->second
.end(); ++signature
) {
1039 CYJavaFrame
frame(jni
, count
+ 16);
1040 jvalue array
[count
];
1041 if (!CYCastJavaArguments(frame
, signature
->shorty_
, context
, arguments
, array
))
1043 jvalue
*values(array
);
1044 switch (signature
->primitive_
) {
1045 case CYJavaPrimitiveObject
:
1046 return CYCastJSValue(context
, jni
.CallObjectMethodA
<jobject
>(self
->value_
, signature
->method_
, values
));
1047 case CYJavaPrimitiveVoid
:
1048 jni
.CallVoidMethodA(self
->value_
, signature
->method_
, values
);
1049 return CYJSUndefined(context
);
1050 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1051 case CYJavaPrimitive ## Type: \
1052 return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, signature->method_, values));
1053 CYJavaForEachPrimitive
1054 #undef CYJavaForEachPrimitive_
1055 default: _assert(false);
1059 CYThrow("invalid method call");
1062 static JSValueRef
JavaStaticMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1063 auto internal(CYPrivate
<CYJavaStaticMethod
>::Get(context
, object
));
1064 CYJavaClass
*table(CYGetJavaTable(context
, _this
));
1065 CYJavaEnv
jni(table
->value_
);
1067 auto overload(internal
->overloads_
.find(count
));
1068 if (overload
!= internal
->overloads_
.end())
1069 for (auto signature(overload
->second
.begin()); signature
!= overload
->second
.end(); ++signature
) {
1070 CYJavaFrame
frame(jni
, count
+ 16);
1071 jvalue array
[count
];
1072 if (!CYCastJavaArguments(frame
, signature
->shorty_
, context
, arguments
, array
))
1074 jvalue
*values(array
);
1075 switch (signature
->primitive_
) {
1076 case CYJavaPrimitiveObject
:
1077 return CYCastJSValue(context
, jni
.CallStaticObjectMethodA
<jobject
>(table
->value_
, signature
->method_
, values
));
1078 case CYJavaPrimitiveVoid
:
1079 jni
.CallStaticVoidMethodA(table
->value_
, signature
->method_
, values
);
1080 return CYJSUndefined(context
);
1081 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1082 case CYJavaPrimitive ## Type: \
1083 return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, signature->method_, values));
1084 CYJavaForEachPrimitive
1085 #undef CYJavaForEachPrimitive_
1086 default: _assert(false);
1090 CYThrow("invalid method call");
1093 static JSObjectRef
JavaClass_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1094 auto table(CYPrivate
<CYJavaClass
>::Get(context
, object
));
1095 CYJavaEnv
jni(table
->value_
);
1096 jclass
_class(table
->value_
);
1098 if (table
->interface_
&& count
== 1) {
1099 auto java(reinterpret_cast<CYJava
*>(JSObjectGetPrivate(CYGetCachedObject(context
, CYJSString("Java")))));
1100 auto Cycript$
(java
->LoadClass("Cycript"));
1101 auto Cycript$
Make(jni
.GetStaticMethodID(Cycript$
, "proxy", "(Ljava/lang/Class;LCycript$Wrapper;)Ljava/lang/Object;"));
1102 return CYCastJSObject(context
, jni
.CallObjectMethod
<jobject
>(Cycript$
, Cycript$Make
, _class
, CYCastJavaObject(jni
, context
, CYCastJSObject(context
, arguments
[0])).get()));
1105 auto overload(table
->overloads_
.find(count
));
1106 if (overload
!= table
->overloads_
.end())
1107 for (auto signature(overload
->second
.begin()); signature
!= overload
->second
.end(); ++signature
) {
1108 CYJavaFrame
frame(jni
, count
+ 16);
1109 jvalue array
[count
];
1110 if (!CYCastJavaArguments(frame
, signature
->shorty_
, context
, arguments
, array
))
1112 jvalue
*values(array
);
1113 auto object(jni
.NewObjectA(_class
, signature
->method_
, values
));
1114 return CYCastJSObject(context
, object
);
1117 CYThrow("invalid constructor call");
1120 static bool JavaStaticInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1121 auto internal(CYPrivate
<CYJavaStaticInterior
>::Get(context
, object
));
1122 CYJavaClass
*table(internal
->table_
);
1124 auto name(CYPoolUTF8String(pool
, context
, property
));
1125 auto field(table
->static_
.find(name
));
1126 if (field
== table
->static_
.end())
1131 static JSValueRef
JavaStaticInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1132 auto internal(CYPrivate
<CYJavaStaticInterior
>::Get(context
, object
));
1133 CYJavaClass
*table(internal
->table_
);
1134 CYJavaEnv
jni(table
->value_
);
1136 auto name(CYPoolUTF8String(pool
, context
, property
));
1137 auto field(table
->static_
.find(name
));
1138 if (field
== table
->static_
.end())
1141 switch (field
->second
.primitive_
) {
1142 case CYJavaPrimitiveObject
:
1143 return CYCastJSValue(context
, jni
.GetStaticObjectField
<jobject
>(table
->value_
, field
->second
.field_
));
1144 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1145 case CYJavaPrimitive ## Type: \
1146 return CYJavaCastJSValue(context, jni.GetStatic ## Typ ## Field(table->value_, field->second.field_));
1147 CYJavaForEachPrimitive
1148 #undef CYJavaForEachPrimitive_
1149 default: _assert(false);
1153 static bool JavaStaticInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1154 auto internal(CYPrivate
<CYJavaStaticInterior
>::Get(context
, object
));
1155 CYJavaClass
*table(internal
->table_
);
1156 CYJavaEnv
jni(table
->value_
);
1158 auto name(CYPoolUTF8String(pool
, context
, property
));
1159 auto field(table
->static_
.find(name
));
1160 if (field
== table
->static_
.end())
1163 switch (field
->second
.primitive_
) {
1164 case CYJavaPrimitiveObject
:
1165 jni
.SetStaticObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1166 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1167 case CYJavaPrimitive ## Type: \
1168 jni.SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1170 CYJavaForEachPrimitive
1171 #undef CYJavaForEachPrimitive_
1172 default: _assert(false);
1178 static void JavaStaticInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1179 auto internal(CYPrivate
<CYJavaStaticInterior
>::Get(context
, object
));
1180 CYJavaClass
*table(internal
->table_
);
1181 for (const auto &field
: table
->static_
)
1182 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1185 static JSValueRef
JavaClass_getProperty_class(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1186 auto table(CYPrivate
<CYJavaClass
>::Get(context
, object
));
1187 return CYCastJSValue(context
, table
->value_
);
1190 static bool JavaInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1191 auto internal(CYPrivate
<CYJavaInterior
>::Get(context
, object
));
1192 CYJavaClass
*table(internal
->table_
);
1194 auto name(CYPoolUTF8String(pool
, context
, property
));
1195 auto field(table
->instance_
.find(name
));
1196 if (field
== table
->instance_
.end())
1201 static JSValueRef
JavaInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1202 auto internal(CYPrivate
<CYJavaInterior
>::Get(context
, object
));
1203 CYJavaEnv
jni(internal
->value_
);
1204 CYJavaClass
*table(internal
->table_
);
1206 auto name(CYPoolUTF8String(pool
, context
, property
));
1207 auto field(table
->instance_
.find(name
));
1208 if (field
== table
->instance_
.end())
1211 switch (field
->second
.primitive_
) {
1212 case CYJavaPrimitiveObject
:
1213 return CYCastJSValue(context
, jni
.GetObjectField
<jobject
>(internal
->value_
, field
->second
.field_
));
1214 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1215 case CYJavaPrimitive ## Type: \
1216 return CYJavaCastJSValue(context, jni.Get ## Typ ## Field(internal->value_, field->second.field_));
1217 CYJavaForEachPrimitive
1218 #undef CYJavaForEachPrimitive_
1219 default: _assert(false);
1223 static bool JavaInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1224 auto internal(CYPrivate
<CYJavaInterior
>::Get(context
, object
));
1225 CYJavaEnv
jni(internal
->value_
);
1226 CYJavaClass
*table(internal
->table_
);
1228 auto name(CYPoolUTF8String(pool
, context
, property
));
1229 auto field(table
->instance_
.find(name
));
1230 if (field
== table
->instance_
.end())
1233 switch (field
->second
.primitive_
) {
1234 case CYJavaPrimitiveObject
:
1235 jni
.SetObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1236 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1237 case CYJavaPrimitive ## Type: \
1238 jni.Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1240 CYJavaForEachPrimitive
1241 #undef CYJavaForEachPrimitive_
1242 default: _assert(false);
1248 static void JavaInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1249 auto internal(CYPrivate
<CYJavaInterior
>::Get(context
, object
));
1250 CYJavaClass
*table(internal
->table_
);
1251 for (const auto &field
: table
->instance_
)
1252 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1255 static JSValueRef
JavaObject_getProperty_constructor(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1256 auto internal(CYPrivate
<CYJavaObject
>::Get(context
, object
));
1257 CYJavaEnv
jni(internal
->value_
);
1258 return CYGetJavaClass(context
, jni
.GetObjectClass(internal
->value_
));
1261 static JSValueRef JavaClass_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1262 auto internal(CYPrivate
<CYJavaClass
>::Get(context
, object
));
1263 return CYPrivate
<CYJavaStaticInterior
>::Make(context
, internal
->value_
, internal
);
1266 static JSValueRef JavaObject_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1267 auto internal(CYPrivate
<CYJavaObject
>::Get(context
, object
));
1268 return CYPrivate
<CYJavaInterior
>::Make(context
, internal
->value_
, internal
->table_
);
1271 static JSValueRef
JavaClass_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1272 auto internal(CYPrivate
<CYJavaClass
>::Get(context
, _this
));
1273 CYJavaEnv
jni(internal
->value_
);
1274 auto Class$
(jni
.FindClass("java/lang/Class"));
1275 auto Class$
getCanonicalName(jni
.GetMethodID(Class$
, "getCanonicalName", "()Ljava/lang/String;"));
1276 return CYCastJSValue(context
, CYJSString(jni
.CallObjectMethod
<jstring
>(internal
->value_
, Class$getCanonicalName
)));
1279 static JSValueRef
JavaMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1280 auto internal(CYPrivate
<CYJavaMethod
>::Get(context
, _this
));
1281 std::ostringstream cyon
;
1283 cyon
<< internal
->type_
<< "." << internal
->name_
;
1286 for (auto overload(internal
->overloads_
.begin()); overload
!= internal
->overloads_
.end(); ++overload
)
1287 for (auto signature(overload
->second
.begin()); signature
!= overload
->second
.end(); ++signature
) {
1292 auto string(CYCastJavaString(signature
->reflected_
));
1293 cyon
<< CYJavaUTF8String(string
);
1296 return CYCastJSValue(context
, CYJSString(cyon
.str()));
1299 static JSValueRef
JavaArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1300 auto internal(CYPrivate
<CYJavaArray
>::Get(context
, object
));
1301 CYJavaEnv
jni(internal
->value_
);
1302 if (JSStringIsEqual(property
, length_s
))
1303 return CYCastJSValue(context
, jni
.GetArrayLength(internal
->value_
));
1307 if (!CYGetOffset(pool
, context
, property
, offset
))
1310 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1311 return CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
));
1312 else switch (internal
->primitive_
) {
1313 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1314 case CYJavaPrimitive ## Type: { \
1315 j ## type element; \
1316 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1317 return CYJavaCastJSValue(context, element); \
1319 CYJavaForEachPrimitive
1320 #undef CYJavaForEachPrimitive_
1321 default: _assert(false);
1325 static bool JavaArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1326 auto internal(CYPrivate
<CYJavaArray
>::Get(context
, object
));
1327 CYJavaEnv
jni(internal
->value_
);
1331 if (!CYGetOffset(pool
, context
, property
, offset
))
1334 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1335 jni
.SetObjectArrayElement(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
, CYCastJavaObject(jni
, context
, value
));
1336 else switch (internal
->primitive_
) {
1337 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1338 case CYJavaPrimitive ## Type: { \
1339 j ## type element; \
1340 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1341 return CYJavaCastJSValue(context, element); \
1343 CYJavaForEachPrimitive
1344 #undef CYJavaForEachPrimitive_
1345 default: _assert(false);
1351 static JNIEnv
*GetJNI(JSContextRef context
, JNIEnv
*&env
);
1353 static JSValueRef
JavaPackage_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1354 auto internal(CYPrivate
<CYJavaPackage
>::Get(context
, _this
));
1355 std::ostringstream name
;
1356 for (auto &package
: internal
->package_
)
1357 name
<< package
<< '.';
1359 return CYCastJSValue(context
, CYJSString(name
.str()));
1362 static bool CYJavaPackage_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1366 static JSValueRef
CYJavaPackage_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1367 auto internal(CYPrivate
<CYJavaPackage
>::Get(context
, object
));
1368 CYJavaPackage::Path
package(internal
->package_
);
1371 const char *next(CYPoolCString(pool
, context
, property
));
1373 std::ostringstream name
;
1374 for (auto &package
: internal
->package_
)
1375 name
<< package
<< '/';
1378 if (internal
->jni_
== NULL
)
1379 GetJNI(context
, internal
->jni_
);
1380 JNIEnv
*jni(internal
->jni_
);
1382 if (auto _class
= jni
->FindClass(name
.str().c_str()))
1383 return CYGetJavaClass(context
, CYJavaLocal
<jclass
>(jni
, _class
));
1384 jni
->ExceptionClear();
1386 package
.push_back(next
);
1387 return CYPrivate
<CYJavaPackage
>::Make(context
, jni
, package
);
1390 static void Cycript_delete(JNIEnv
*env
, jclass api
, jlong jprotect
) { CYJavaTry
{
1394 static jobject
Cycript_handle(JNIEnv
*env
, jclass api
, jlong jprotect
, jstring property
, jobjectArray jarguments
) { CYJavaTry
{
1395 JSValueRef
function(CYGetProperty(context
, object
, CYJSString(CYJavaRef
<jstring
>(jni
, property
))));
1396 if (JSValueIsUndefined(context
, function
))
1399 size_t count(jarguments
== NULL
? 0 : jni
.GetArrayLength(jarguments
));
1400 JSValueRef arguments
[count
];
1401 for (size_t index(0); index
!= count
; ++index
)
1402 arguments
[index
] = CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(jarguments
, index
));
1404 return CYCastJavaObject(jni
, context
, CYCallAsFunction(context
, CYCastJSObject(context
, function
), object
, count
, arguments
)).leak();
1405 } CYJavaCatch(NULL
) }
1407 static JNINativeMethod Cycript_
[] = {
1408 {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete
},
1409 {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle
},
1412 template <typename Type_
>
1413 static _finline
void dlset(Type_
&function
, const char *name
, void *handle
) {
1414 function
= reinterpret_cast<Type_
>(dlsym(handle
, name
));
1417 jint
CYJavaVersion(JNI_VERSION_1_4
);
1419 static JavaVM
*CYGetJavaVM(jint (*$JNI_GetCreatedJavaVMs
)(JavaVM
**, jsize
, jsize
*)) {
1421 JavaVM
*jvms
[capacity
];
1423 _jnicall($
JNI_GetCreatedJavaVMs(jvms
, capacity
, &size
));
1429 static JavaVM
*CYGetJavaVM(JSContextRef context
) {
1431 void *handle(RTLD_DEFAULT
);
1432 std::string library
;
1434 jint (*$JNI_GetCreatedJavaVMs
)(JavaVM
**jvms
, jsize capacity
, jsize
*size
);
1435 dlset($JNI_GetCreatedJavaVMs
, "JNI_GetCreatedJavaVMs", handle
);
1437 if ($JNI_GetCreatedJavaVMs
!= NULL
) {
1438 if (JavaVM
*jvm
= CYGetJavaVM($JNI_GetCreatedJavaVMs
))
1441 std::vector
<const char *> guesses
;
1444 char android
[PROP_VALUE_MAX
];
1445 if (__system_property_get("persist.sys.dalvik.vm.lib", android
) != 0)
1446 guesses
.push_back(android
);
1449 guesses
.push_back("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib");
1450 //guesses.push_back("/System/Library/Frameworks/JavaVM.framework/JavaVM");
1451 guesses
.push_back("libjvm.dylib");
1453 guesses
.push_back("libart.so");
1454 guesses
.push_back("libdvm.so");
1455 guesses
.push_back("libjvm.so");
1457 for (const char *guess
: guesses
) {
1458 handle
= dlopen(guess
, RTLD_LAZY
| RTLD_GLOBAL
);
1459 if (handle
!= NULL
) {
1465 if (library
.size() == 0)
1468 dlset($JNI_GetCreatedJavaVMs
, "JNI_GetCreatedJavaVMs", handle
);
1469 if (JavaVM
*jvm
= CYGetJavaVM($JNI_GetCreatedJavaVMs
))
1473 std::vector
<JavaVMOption
> options
;
1475 if (const char *classpath
= getenv("CLASSPATH"))
1476 options
.push_back(JavaVMOption
{pool
.strcat("-Djava.class.path=", classpath
, NULL
), NULL
});
1478 // To use libnativehelper to access JNI_GetCreatedJavaVMs, you need JniInvocation.
1479 // ...but there can only be one JniInvocation, and assuradely the other VM has it.
1480 // Essentially, this API makes no sense. We need it for AndroidRuntime, though :/.
1482 if (void *libnativehelper
= dlopen("libnativehelper.so", RTLD_LAZY
| RTLD_GLOBAL
)) {
1483 class JniInvocation$
;
1484 JniInvocation$
*(*JniInvocation$$init$
)(JniInvocation$
*self
)(NULL
);
1485 bool (*JniInvocation$Init
)(JniInvocation$
*self
, const char *library
)(NULL
);
1486 JniInvocation$
*(*JniInvocation$finalize
)(JniInvocation$
*self
)(NULL
);
1488 dlset(JniInvocation$$init$
, "_ZN13JniInvocationC1Ev", libnativehelper
);
1489 dlset(JniInvocation$Init
, "_ZN13JniInvocation4InitEPKc", libnativehelper
);
1490 dlset(JniInvocation$finalize
, "_ZN13JniInvocationD1Ev", libnativehelper
);
1492 if (JniInvocation$$init$
== NULL
)
1493 dlclose(libnativehelper
);
1495 // XXX: we should attach a pool to the VM itself and deallocate this there
1496 //auto invocation(pool.calloc<JniInvocation$>(1, 1024));
1497 //_assert(JniInvocation$finalize != NULL);
1498 //pool.atexit(reinterpret_cast<void (*)(void *)>(JniInvocation$finalize), invocation);
1500 auto invocation(static_cast<JniInvocation$
*>(calloc(1, 1024)));
1501 JniInvocation$$init$
(invocation
);
1503 _assert(JniInvocation$Init
!= NULL
);
1504 JniInvocation$
Init(invocation
, NULL
);
1506 dlset($JNI_GetCreatedJavaVMs
, "JNI_GetCreatedJavaVMs", libnativehelper
);
1507 if (JavaVM
*jvm
= CYGetJavaVM($JNI_GetCreatedJavaVMs
))
1515 if (void *libandroid_runtime
= dlopen("libandroid_runtime.so", RTLD_LAZY
| RTLD_GLOBAL
)) {
1516 class AndroidRuntime$
;
1517 AndroidRuntime$
*(*AndroidRuntime$$init$
)(AndroidRuntime$
*self
, char *args
, unsigned int size
)(NULL
);
1518 int (*AndroidRuntime$startVm1
)(AndroidRuntime$
*self
, JavaVM
**jvm
, JNIEnv
**env
)(NULL
);
1519 int (*AndroidRuntime$startVm2
)(AndroidRuntime$
*self
, JavaVM
**jvm
, JNIEnv
**env
, bool)(NULL
);
1520 int (*AndroidRuntime$startReg
)(JNIEnv
*env
)(NULL
);
1521 int (*AndroidRuntime$addOption
)(AndroidRuntime$
*self
, const char *option
, void *extra
)(NULL
);
1522 int (*AndroidRuntime$addVmArguments
)(AndroidRuntime$
*self
, int, const char *const argv
[])(NULL
);
1523 AndroidRuntime$
*(*AndroidRuntime$finalize
)(AndroidRuntime$
*self
)(NULL
);
1525 dlset(AndroidRuntime$$init$
, "_ZN7android14AndroidRuntimeC1EPcj", libandroid_runtime
);
1526 dlset(AndroidRuntime$startVm1
, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnv", libandroid_runtime
);
1527 dlset(AndroidRuntime$startVm2
, "_ZN7android14AndroidRuntime7startVmEPP7_JavaVMPP7_JNIEnvb", libandroid_runtime
);
1528 dlset(AndroidRuntime$startReg
, "_ZN7android14AndroidRuntime8startRegEP7_JNIEnv", libandroid_runtime
);
1529 dlset(AndroidRuntime$addOption
, "_ZN7android14AndroidRuntime9addOptionEPKcPv", libandroid_runtime
);
1530 dlset(AndroidRuntime$addVmArguments
, "_ZN7android14AndroidRuntime14addVmArgumentsEiPKPKc", libandroid_runtime
);
1531 dlset(AndroidRuntime$finalize
, "_ZN7android14AndroidRuntimeD1Ev", libandroid_runtime
);
1533 // XXX: it would also be interesting to attach this to a global pool
1534 AndroidRuntime$
*runtime(pool
.calloc
<AndroidRuntime$
>(1, 1024));
1536 _assert(AndroidRuntime$$init$
!= NULL
);
1537 AndroidRuntime$$init$
(runtime
, NULL
, 0);
1539 if (AndroidRuntime$addOption
== NULL
) {
1540 _assert(AndroidRuntime$addVmArguments
!= NULL
);
1541 std::vector
<const char *> arguments
;
1542 for (const auto &option
: options
)
1543 arguments
.push_back(option
.optionString
);
1544 AndroidRuntime$
addVmArguments(runtime
, arguments
.size(), arguments
.data());
1545 } else for (const auto &option
: options
)
1546 AndroidRuntime$
addOption(runtime
, option
.optionString
, option
.extraInfo
);
1551 else if (AndroidRuntime$startVm1
!= NULL
)
1552 failure
= AndroidRuntime$
startVm1(runtime
, &jvm
, &env
);
1553 else if (AndroidRuntime$startVm2
!= NULL
)
1554 failure
= AndroidRuntime$
startVm2(runtime
, &jvm
, &env
, false);
1555 else _assert(false);
1557 _assert(failure
== 0);
1559 _assert(AndroidRuntime$startReg
!= NULL
);
1560 failure
= AndroidRuntime$
startReg(env
);
1561 _assert(failure
== 0);
1566 jint (*$JNI_CreateJavaVM
)(JavaVM
**jvm
, void **, void *);
1567 dlset($JNI_CreateJavaVM
, "JNI_CreateJavaVM", handle
);
1569 JavaVMInitArgs args
;
1570 memset(&args
, 0, sizeof(args
));
1571 args
.version
= CYJavaVersion
;
1572 args
.nOptions
= options
.size();
1573 args
.options
= options
.data();
1574 _jnicall($
JNI_CreateJavaVM(&jvm
, reinterpret_cast<void **>(&env
), &args
));
1578 static JNIEnv
*GetJNI(JSContextRef context
, JNIEnv
*&env
) {
1579 auto jvm(CYGetJavaVM(context
));
1580 _assert(jvm
!= NULL
);
1582 switch (jvm
->GetEnv(reinterpret_cast<void **>(&env
), CYJavaVersion
)) {
1584 _jnicall(jvm
->AttachCurrentThreadAsDaemon(
1586 reinterpret_cast<void **>
1598 auto java(reinterpret_cast<CYJava
*>(JSObjectGetPrivate(CYGetCachedObject(context
, CYJSString("Java")))));
1599 if (java
->Setup(jni
)) {
1600 auto Cycript$
(java
->LoadClass("Cycript"));
1601 jni
.RegisterNatives(Cycript$
, Cycript_
, sizeof(Cycript_
) / sizeof(Cycript_
[0]));
1603 JSObjectRef
java(CYGetCachedObject(context
, CYJSString("Java")));
1604 JSValueRef arguments
[1];
1605 arguments
[0] = CYCastJSValue(context
, CYJSString("setup"));
1606 CYCallAsFunction(context
, CYCastJSObject(context
, CYGetProperty(context
, java
, CYJSString("emit"))), java
, 1, arguments
);
1612 static JSStaticValue JavaClass_staticValues
[3] = {
1613 {"class", &JavaClass_getProperty_class
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1614 {"$cyi", &JavaClass_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1615 {NULL
, NULL
, NULL
, 0}
1618 static JSStaticFunction JavaClass_staticFunctions
[2] = {
1619 {"toCYON", &JavaClass_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1623 static JSStaticValue JavaObject_staticValues
[3] = {
1624 {"constructor", &JavaObject_getProperty_constructor
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1625 {"$cyi", &JavaObject_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1626 {NULL
, NULL
, NULL
, 0}
1629 static JSStaticFunction JavaInstanceMethod_staticFunctions
[2] = {
1630 {"toCYON", &JavaMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1634 static JSStaticFunction JavaStaticMethod_staticFunctions
[2] = {
1635 {"toCYON", &JavaMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1639 static JSStaticFunction JavaPackage_staticFunctions
[2] = {
1640 {"toCYON", &JavaPackage_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1644 void CYJava_Initialize() {
1645 Primitives_
.insert(std::make_pair("void", CYJavaPrimitiveVoid
));
1646 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1647 Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type));
1648 CYJavaForEachPrimitive
1649 #undef CYJavaForEachPrimitive_
1651 JSClassDefinition definition
;
1653 definition
= kJSClassDefinitionEmpty
;
1654 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1655 definition
.className
= "Java";
1656 definition
.finalize
= &CYFinalize
;
1657 CYPrivate
<CYJava
>::Class_
= JSClassCreate(&definition
);
1659 definition
= kJSClassDefinitionEmpty
;
1660 definition
.className
= "JavaClass";
1661 definition
.staticValues
= JavaClass_staticValues
;
1662 definition
.staticFunctions
= JavaClass_staticFunctions
;
1663 definition
.callAsConstructor
= &JavaClass_callAsConstructor
;
1664 definition
.finalize
= &CYFinalize
;
1665 CYPrivate
<CYJavaClass
>::Class_
= JSClassCreate(&definition
);
1667 definition
= kJSClassDefinitionEmpty
;
1668 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1669 definition
.className
= "JavaInterior";
1670 definition
.hasProperty
= &JavaInterior_hasProperty
;
1671 definition
.getProperty
= &JavaInterior_getProperty
;
1672 definition
.setProperty
= &JavaInterior_setProperty
;
1673 definition
.getPropertyNames
= &JavaInterior_getPropertyNames
;
1674 definition
.finalize
= &CYFinalize
;
1675 CYPrivate
<CYJavaInterior
>::Class_
= JSClassCreate(&definition
);
1677 definition
= kJSClassDefinitionEmpty
;
1678 definition
.className
= "JavaMethod";
1679 definition
.finalize
= &CYFinalize
;
1680 CYPrivate
<CYJavaMethod
>::Class_
= JSClassCreate(&definition
);
1682 definition
= kJSClassDefinitionEmpty
;
1683 definition
.className
= "JavaInstanceMethod";
1684 definition
.parentClass
= CYPrivate
<CYJavaMethod
>::Class_
;
1685 definition
.staticFunctions
= JavaInstanceMethod_staticFunctions
;
1686 definition
.callAsFunction
= &JavaInstanceMethod_callAsFunction
;
1687 CYPrivate
<CYJavaInstanceMethod
>::Class_
= JSClassCreate(&definition
);
1689 definition
= kJSClassDefinitionEmpty
;
1690 definition
.className
= "JavaStaticMethod";
1691 definition
.parentClass
= CYPrivate
<CYJavaMethod
>::Class_
;
1692 definition
.staticFunctions
= JavaStaticMethod_staticFunctions
;
1693 definition
.callAsFunction
= &JavaStaticMethod_callAsFunction
;
1694 CYPrivate
<CYJavaStaticMethod
>::Class_
= JSClassCreate(&definition
);
1696 definition
= kJSClassDefinitionEmpty
;
1697 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1698 definition
.className
= "JavaObject";
1699 definition
.staticValues
= JavaObject_staticValues
;
1700 definition
.finalize
= &CYFinalize
;
1701 CYPrivate
<CYJavaObject
>::Class_
= JSClassCreate(&definition
);
1703 definition
= kJSClassDefinitionEmpty
;
1704 definition
.className
= "JavaArray";
1705 definition
.getProperty
= &JavaArray_getProperty
;
1706 definition
.setProperty
= &JavaArray_setProperty
;
1707 definition
.finalize
= &CYFinalize
;
1708 CYPrivate
<CYJavaArray
>::Class_
= JSClassCreate(&definition
);
1710 definition
= kJSClassDefinitionEmpty
;
1711 definition
.className
= "JavaPackage";
1712 definition
.staticFunctions
= JavaPackage_staticFunctions
;
1713 definition
.hasProperty
= &CYJavaPackage_hasProperty
;
1714 definition
.getProperty
= &CYJavaPackage_getProperty
;
1715 definition
.finalize
= &CYFinalize
;
1716 CYPrivate
<CYJavaPackage
>::Class_
= JSClassCreate(&definition
);
1718 definition
= kJSClassDefinitionEmpty
;
1719 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1720 definition
.className
= "JavaStaticInterior";
1721 definition
.hasProperty
= &JavaStaticInterior_hasProperty
;
1722 definition
.getProperty
= &JavaStaticInterior_getProperty
;
1723 definition
.setProperty
= &JavaStaticInterior_setProperty
;
1724 definition
.getPropertyNames
= &JavaStaticInterior_getPropertyNames
;
1725 definition
.finalize
= &CYFinalize
;
1726 CYPrivate
<CYJavaStaticInterior
>::Class_
= JSClassCreate(&definition
);
1729 void CYJava_SetupContext(JSContextRef context
) {
1730 JSObjectRef
global(CYGetGlobalObject(context
));
1731 JSObjectRef
cy(CYCastJSObject(context
, CYGetProperty(context
, global
, cy_s
)));
1732 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1733 JSObjectRef
all(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("all"))));
1734 //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
1736 JSObjectRef
Java(CYPrivate
<CYJava
>::Make(context
));
1737 CYSetProperty(context
, cycript
, CYJSString("Java"), Java
);
1738 CYSetProperty(context
, cy
, CYJSString("Java"), Java
);
1740 JSObjectRef
Packages(CYPrivate
<CYJavaPackage
>::Make(context
, nullptr, CYJavaPackage::Path()));
1741 CYSetProperty(context
, all
, CYJSString("Packages"), Packages
, kJSPropertyAttributeDontEnum
);
1743 for (auto name
: (const char *[]) {"java", "javax", "android", "com", "net", "org"}) {
1744 CYJSString
js(name
);
1745 CYSetProperty(context
, all
, js
, CYPrivate
<CYJavaPackage
>::Make(context
, nullptr, CYJavaPackage::Path(1, name
)), kJSPropertyAttributeDontEnum
);
1749 static CYHook CYJavaHook
= {
1754 &CYJava_SetupContext
,
1758 CYRegisterHook
CYJava(&CYJavaHook
);