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/>.
27 #include <JavaVM/jni.h>
32 #include "cycript.hpp"
34 #include "Execute.hpp"
35 #include "Internal.hpp"
36 #include "JavaScript.hpp"
37 #include "Pooling.hpp"
39 #define _jnicall(expr) ({ \
41 if (_value != JNI_OK) \
42 CYThrow("_jnicall(%s) == %d", #expr, _value); \
45 #define _envcall(jni, expr) ({ \
46 __typeof__(jni->expr) _value(jni->expr); \
47 if (jthrowable _error = jni->ExceptionOccurred()) { \
48 jni->ExceptionClear(); \
49 throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \
53 #define _envcallv(jni, expr) do { \
55 if (jthrowable _error = jni->ExceptionOccurred()) { \
56 jni->ExceptionClear(); \
57 throw CYJavaError(CYJavaLocal<jthrowable>(jni, _error)); \
63 auto &protect(*reinterpret_cast<CYProtect *>(jprotect)); \
64 _disused JSContextRef context(protect); \
65 _disused JSObjectRef object(protect); \
67 #define CYJavaCatch(value) \
68 catch (const CYException &error) { \
69 jni->Throw(CYCastJavaObject(jni, context, error.CastJSValue(context, "Error")).cast<jthrowable>()); \
74 // Android's jni.h seriously doesn't declare these :/
75 jint
JNI_CreateJavaVM(JavaVM
**, void **, void *);
76 jint
JNI_GetCreatedJavaVMs(JavaVM
**, jsize
, jsize
*);
79 JNIEnv
*GetJNI(JSContextRef context
);
81 #define CYJavaForEachPrimitive \
82 CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \
83 CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \
84 CYJavaForEachPrimitive_(C, c, Char, Character, char) \
85 CYJavaForEachPrimitive_(S, s, Short, Short, short) \
86 CYJavaForEachPrimitive_(I, i, Int, Integer, int) \
87 CYJavaForEachPrimitive_(J, j, Long, Long, long) \
88 CYJavaForEachPrimitive_(F, f, Float, Float, float) \
89 CYJavaForEachPrimitive_(D, d, Double, Double, double)
91 enum CYJavaPrimitive
: char {
92 CYJavaPrimitiveObject
,
94 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
95 CYJavaPrimitive ## Type,
96 CYJavaForEachPrimitive
97 #undef CYJavaForEachPrimitive_
100 // Java References {{{
101 template <typename Value_
>
106 _finline
CYJavaRef(JNIEnv
*jni
, Value_ value
) :
112 _finline
operator Value_() const {
116 _finline JNIEnv
*jni() const {
120 _finline Value_
get() const {
124 template <typename Other_
>
125 _finline CYJavaRef
<Other_
> cast() const {
126 return {jni_
, static_cast<Other_
>(value_
)};
130 Value_
value(value_
);
136 template <typename Value_
, void (JNIEnv::*Delete_
)(jobject
)>
137 struct CYJavaDelete
:
140 _finline
CYJavaDelete(JNIEnv
*jni
, Value_ value
) :
141 CYJavaRef
<Value_
>(jni
, value
)
146 if (this->value_
!= NULL
)
147 (this->jni_
->*Delete_
)(this->value_
);
151 template <typename Value_
>
152 struct CYJavaGlobal
:
153 CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
>
155 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
> CYJavaBase
;
158 CYJavaBase(NULL
, NULL
)
162 template <typename Other_
>
163 CYJavaGlobal(const CYJavaRef
<Other_
> &other
) :
164 CYJavaBase(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewGlobalRef(other
.value_
)))
168 CYJavaGlobal(const CYJavaGlobal
<Value_
> &other
) :
169 CYJavaGlobal(static_cast<const CYJavaRef
<Value_
> &>(other
))
173 CYJavaGlobal(CYJavaGlobal
&&value
) :
174 CYJavaBase(value
.jni_
, value
.value_
)
180 template <typename Value_
>
182 CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
>
184 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
> CYJavaBase
;
187 CYJavaBase(NULL
, NULL
)
191 CYJavaLocal(JNIEnv
*jni
, Value_ value
) :
192 CYJavaBase(jni
, value
)
196 template <typename Other_
>
197 CYJavaLocal(const CYJavaRef
<Other_
> &other
) :
198 CYJavaLocal(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewLocalRef(other
.value_
)))
202 CYJavaLocal(CYJavaLocal
&&value
) :
203 CYJavaLocal(value
.jni_
, value
.value_
)
208 CYJavaLocal
&operator =(CYJavaLocal
<Value_
> &&other
) {
209 std::swap(this->jni_
, other
.jni_
);
210 std::swap(this->value_
, other
.value_
);
216 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaRef
<jobject
> &value
);
218 class CYJavaUTF8String
:
222 const CYJavaRef
<jstring
> *value_
;
225 CYJavaUTF8String(const CYJavaRef
<jstring
> &value
) :
229 JNIEnv
*jni(value
.jni());
230 size
= jni
->GetStringUTFLength(value
);
231 data
= jni
->GetStringUTFChars(value
, NULL
);
234 CYJavaUTF8String(const CYJavaRef
<jobject
> &value
) :
235 CYJavaUTF8String(CYCastJavaString(value
))
239 ~CYJavaUTF8String() {
240 if (value_
!= NULL
) {
241 JNIEnv
*jni(value_
->jni());
242 jni
->ReleaseStringUTFChars(*value_
, data
);
246 CYJavaUTF8String(const CYJavaUTF8String
&) = delete;
248 CYJavaUTF8String(CYJavaUTF8String
&&rhs
) :
255 CYJavaUTF8String
CYCastUTF8String(const CYJavaRef
<jstring
> &value
) {
256 return CYJavaUTF8String(value
);
259 JSStringRef
CYCopyJSString(const CYJavaRef
<jstring
> &value
) {
260 return CYCopyJSString(CYCastUTF8String(value
));
267 CYJavaGlobal
<jthrowable
> value_
;
269 CYJavaError(const CYJavaRef
<jthrowable
> &value
) :
274 virtual const char *PoolCString(CYPool
&pool
) const {
275 return CYPoolCString(pool
, CYJavaUTF8String(value_
.cast
<jobject
>()));
278 virtual JSValueRef
CastJSValue(JSContextRef context
, const char *name
) const;
285 CYJavaFrame(JNIEnv
*jni
, jint capacity
) :
288 _assert(jni
->PushLocalFrame(capacity
) == 0);
295 jobject
operator ()(jobject object
) {
298 return jni
->PopLocalFrame(object
);
307 CYJavaEnv(JNIEnv
*jni
) :
312 template <typename Other_
>
313 CYJavaEnv(const CYJavaRef
<Other_
> &value
) :
318 operator JNIEnv
*() const {
322 JNIEnv
*operator ->() const {
326 CYJavaLocal
<jclass
> FindClass(const char *name
) const {
327 return {jni
, _envcall(jni
, FindClass(name
))};
330 CYJavaLocal
<jclass
> GetObjectClass(jobject object
) const {
331 return {jni
, _envcall(jni
, GetObjectClass(object
))};
334 CYJavaLocal
<jclass
> GetSuperclass(jclass _class
) const {
335 return {jni
, _envcall(jni
, GetSuperclass(_class
))};
338 CYJavaLocal
<jobject
> NewObject(jclass _class
, jmethodID method
, ...) const {
340 va_start(args
, method
);
341 jobject
object(_envcall(jni
, NewObjectV(_class
, method
, args
)));
343 return {jni
, object
};
346 CYJavaLocal
<jobject
> NewObjectA(jclass _class
, jmethodID method
, jvalue
*args
) const {
347 return {jni
, _envcall(jni
, NewObjectA(_class
, method
, args
))};
350 CYJavaLocal
<jstring
> NewString(const jchar
*data
, jsize size
) const {
351 return {jni
, _envcall(jni
, NewString(data
, size
))};
354 #define CYJavaEnv_(Code) \
355 template <typename... Args_> \
356 void Code(Args_ &&... args) const { \
357 _envcallv(jni, Code(cy::Forward<Args_>(args)...)); \
360 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
361 CYJavaEnv_(Get ## Typ ## ArrayRegion) \
362 CYJavaEnv_(Set ## Typ ## Field) \
363 CYJavaEnv_(SetStatic ## Typ ## Field)
364 CYJavaForEachPrimitive
365 #undef CYJavaForEachPrimitive_
367 CYJavaEnv_(CallVoidMethod
)
368 CYJavaEnv_(CallStaticVoidMethod
)
369 CYJavaEnv_(CallVoidMethodA
)
370 CYJavaEnv_(CallStaticVoidMethodA
)
371 CYJavaEnv_(SetObjectArrayElement
)
372 CYJavaEnv_(SetObjectField
)
373 CYJavaEnv_(SetStaticObjectField
)
376 #define CYJavaEnv_(Code) \
377 template <typename... Args_> \
378 auto Code(Args_ &&... args) const -> decltype(jni->Code(args...)) { \
379 return _envcall(jni, Code(cy::Forward<Args_>(args)...)); \
382 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
383 CYJavaEnv_(Call ## Typ ## Method) \
384 CYJavaEnv_(CallStatic ## Typ ## Method) \
385 CYJavaEnv_(Call ## Typ ## MethodA) \
386 CYJavaEnv_(CallStatic ## Typ ## MethodA) \
387 CYJavaEnv_(Get ## Typ ## Field) \
388 CYJavaEnv_(GetStatic ## Typ ## Field)
389 CYJavaForEachPrimitive
390 #undef CYJavaForEachPrimitive_
392 CYJavaEnv_(FromReflectedField
)
393 CYJavaEnv_(FromReflectedMethod
)
394 CYJavaEnv_(GetArrayLength
)
395 CYJavaEnv_(GetMethodID
)
396 CYJavaEnv_(GetStaticMethodID
)
397 CYJavaEnv_(IsSameObject
)
400 #define CYJavaEnv_(Code) \
401 template <typename Other_, typename... Args_> \
402 auto Code(Args_ &&... args) const -> CYJavaLocal<Other_> { \
403 return {jni, static_cast<Other_>(_envcall(jni, Code(cy::Forward<Args_>(args)...)))}; \
406 CYJavaEnv_(CallObjectMethod
)
407 CYJavaEnv_(CallStaticObjectMethod
)
408 CYJavaEnv_(CallObjectMethodA
)
409 CYJavaEnv_(CallStaticObjectMethodA
)
410 CYJavaEnv_(GetObjectArrayElement
)
411 CYJavaEnv_(GetObjectField
)
412 CYJavaEnv_(GetStaticObjectField
)
416 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaRef
<jobject
> &value
) {
417 CYJavaEnv
jni(value
);
418 auto Object$
(jni
.FindClass("java/lang/Object"));
419 auto Object$
toString(jni
.GetMethodID(Object$
, "toString", "()Ljava/lang/String;"));
420 return jni
.CallObjectMethod
<jstring
>(value
, Object$toString
);
423 template <typename Internal_
, typename Value_
>
427 CYJavaGlobal
<Value_
> value_
;
429 CYJavaValue(const CYJavaRef
<Value_
> &value
) :
434 CYJavaValue(const CYJavaValue
&) = delete;
437 static JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<jobject
> &value
);
439 template <typename Other_
>
440 static _finline JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<Other_
> &value
) {
441 return CYCastJSValue(context
, value
.template cast
<jobject
>());
444 template <typename Type_
>
445 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, Type_ value
) {
446 return CYCastJSValue(context
, value
);
449 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, jboolean value
) {
450 return CYCastJSValue(context
, static_cast<bool>(value
));
453 JSValueRef
CYJavaError::CastJSValue(JSContextRef context
, const char *name
) const {
454 return CYCastJSValue(context
, value_
);
457 static std::map
<std::string
, CYJavaPrimitive
> Primitives_
;
459 static CYJavaPrimitive
CYJavaGetPrimitive(JSContextRef context
, const CYJavaRef
<jclass
> &type
, jmethodID Class$get$$Name
) {
461 auto string(jni
.CallObjectMethod
<jstring
>(type
, Class$get$$Name
));
464 CYJavaUTF8String
name(string
);
465 auto primitive(Primitives_
.find(name
));
466 return primitive
!= Primitives_
.end() ? primitive
->second
: CYJavaPrimitiveObject
;
469 typedef std::vector
<CYJavaPrimitive
> CYJavaShorty
;
471 static CYJavaShorty
CYJavaGetShorty(JSContextRef context
, const CYJavaRef
<jobjectArray
> &types
, jmethodID Class$get$$Name
) {
472 CYJavaEnv
jni(types
);
473 size_t count(jni
.GetArrayLength(types
));
474 CYJavaShorty
shorty(count
);
475 for (size_t index(0); index
!= count
; ++index
)
476 shorty
[index
] = CYJavaGetPrimitive(context
, jni
.GetObjectArrayElement
<jclass
>(types
, index
), Class$get$$Name
);
482 CYJavaPrimitive primitive_
;
485 typedef std::map
<std::string
, CYJavaField
> CYJavaFieldMap
;
487 struct CYJavaSignature
{
488 CYJavaGlobal
<jobject
> reflected_
;
490 CYJavaPrimitive primitive_
;
491 CYJavaShorty shorty_
;
493 CYJavaSignature(const CYJavaRef
<jobject
> &reflected
, jmethodID method
, CYJavaPrimitive primitive
, const CYJavaShorty
&shorty
) :
494 reflected_(reflected
),
496 primitive_(primitive
),
501 CYJavaSignature(unsigned count
) :
506 bool operator <(const CYJavaSignature
&rhs
) const {
507 return shorty_
.size() < rhs
.shorty_
.size();
511 typedef std::multiset
<CYJavaSignature
> CYJavaOverload
;
513 struct CYJavaMethod
:
514 CYPrivate
<CYJavaMethod
>
516 CYJavaOverload overload_
;
518 CYJavaMethod(const CYJavaOverload
&overload
) :
524 struct CYJavaStaticMethod
:
525 CYPrivate
<CYJavaStaticMethod
>
527 CYJavaOverload overload_
;
529 CYJavaStaticMethod(const CYJavaOverload
&overload
) :
536 CYJavaValue
<CYJavaClass
, jclass
>
540 CYJavaFieldMap static_
;
541 CYJavaFieldMap instance_
;
542 CYJavaOverload overload_
;
544 CYJavaClass(const CYJavaRef
<jclass
> &value
, bool interface
) :
546 interface_(interface
)
551 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &_class
);
553 struct CYJavaObject
:
554 CYJavaValue
<CYJavaObject
, jobject
>
558 CYJavaObject(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
564 JSValueRef
GetPrototype(JSContextRef context
) const;
567 struct CYJavaInterior
:
568 CYJavaValue
<CYJavaInterior
, jobject
>
572 CYJavaInterior(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
579 struct CYJavaStaticInterior
:
580 CYJavaValue
<CYJavaStaticInterior
, jclass
>
584 CYJavaStaticInterior(const CYJavaRef
<jclass
> &value
, CYJavaClass
*table
) :
592 CYJavaValue
<CYJavaArray
, jarray
>
594 CYJavaPrimitive primitive_
;
596 CYJavaArray(const CYJavaRef
<jarray
> &value
, CYJavaPrimitive primitive
) :
598 primitive_(primitive
)
602 JSValueRef
GetPrototype(JSContextRef context
) const;
605 struct CYJavaPackage
:
606 CYPrivate
<CYJavaPackage
>
608 typedef std::vector
<std::string
> Path
;
611 _finline
CYJavaPackage(const Path
&package
) :
617 JSValueRef
CYJavaObject::GetPrototype(JSContextRef context
) const {
618 CYJavaEnv
jni(value_
);
619 return CYGetProperty(context
, CYGetJavaClass(context
, jni
.GetObjectClass(value_
)), prototype_s
);
622 JSValueRef
CYJavaArray::GetPrototype(JSContextRef context
) const {
623 return CYGetCachedObject(context
, CYJSString("Array_prototype"));
626 static JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
628 return CYJSNull(context
);
629 CYJavaEnv
jni(value
);
631 auto _class(jni
.GetObjectClass(value
));
632 if (jni
.IsSameObject(_class
, jni
.FindClass("java/lang/String")))
633 return CYCastJSValue(context
, CYJSString(value
.cast
<jstring
>()));
635 auto Class$
(jni
.FindClass("java/lang/Class"));
636 auto Class$
isArray(jni
.GetMethodID(Class$
, "isArray", "()Z"));
637 if (jni
.CallBooleanMethod(_class
, Class$isArray
)) {
638 auto Class$
getComponentType(jni
.GetMethodID(Class$
, "getComponentType", "()Ljava/lang/Class;"));
639 auto component(jni
.CallObjectMethod
<jclass
>(_class
, Class$getComponentType
));
640 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
641 return CYJavaArray::Make(context
, value
.cast
<jarray
>(), CYJavaGetPrimitive(context
, component
, Class$getName
));
644 auto Wrapper$
(jni
.FindClass("Cycript$Wrapper"));
645 if (jni
.IsSameObject(_class
, Wrapper$
)) {
646 auto Wrapper$
getProtect(jni
.GetMethodID(Wrapper$
, "getProtect", "()J"));
647 auto &protect(*reinterpret_cast<CYProtect
*>(jni
.CallLongMethod(value
, Wrapper$getProtect
)));
651 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(CYGetJavaClass(context
, _class
))));
652 return CYJavaObject::Make(context
, value
, table
);
655 static _finline JSObjectRef
CYCastJSObject(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
656 return CYCastJSObject(context
, CYCastJSValue(context
, value
));
659 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, JSContextRef context
, CYUTF16String value
) {
660 return jni
.NewString(value
.data
, value
.size
);
663 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, JSContextRef context
, JSStringRef value
) {
664 return CYCastJavaString(jni
, context
, CYCastUTF16String(value
));
667 #define CYCastJava$(T, Type, jtype, Cast) \
668 _disused static CYJavaLocal<jobject> CYCastJava ## Type(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { \
669 auto Type$(jni.FindClass("java/lang/" #Type)); \
670 auto Type$init$(jni.GetMethodID(Type$, "<init>", "(" #T ")V")); \
671 return jni.NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value))); \
674 CYCastJava$
(Z
, Boolean
, jboolean
, CYCastBool
)
675 CYCastJava$
(B
, Byte
, jbyte
, CYCastDouble
)
676 CYCastJava$
(C
, Character
, jchar
, CYCastDouble
)
677 CYCastJava$
(S
, Short
, jshort
, CYCastDouble
)
678 CYCastJava$
(I
, Integer
, jint
, CYCastDouble
)
679 CYCastJava$
(J
, Long
, jlong
, CYCastDouble
)
680 CYCastJava$
(F
, Float
, jfloat
, CYCastDouble
)
681 CYCastJava$
(D
, Double
, jdouble
, CYCastDouble
)
683 static CYJavaClass
*CYGetJavaTable(JSContextRef context
, JSObjectRef object
) {
684 if (!JSValueIsObjectOfClass(context
, object
, CYJavaClass::Class_
))
686 return reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
));
689 static CYJavaObject
*CYGetJavaObject(JSContextRef context
, JSObjectRef object
) {
690 if (!JSValueIsObjectOfClass(context
, object
, CYJavaObject::Class_
))
692 return reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
));
695 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSObjectRef value
) {
696 if (CYJavaObject
*internal
= CYGetJavaObject(context
, value
))
697 return internal
->value_
;
699 auto Wrapper$
(jni
.FindClass("Cycript$Wrapper"));
700 auto Wrapper$$init$
(jni
.GetMethodID(Wrapper$
, "<init>", "(J)V"));
701 CYProtect
*protect(new CYProtect(context
, value
));
702 return jni
.NewObject(Wrapper$
, Wrapper$$init$
, reinterpret_cast<jlong
>(protect
));
705 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSValueRef value
) {
706 switch (JSValueGetType(context
, value
)) {
710 return CYCastJavaBoolean(jni
, context
, value
);
712 return CYCastJavaDouble(jni
, context
, value
);
714 return CYCastJavaString(jni
, context
, CYJSString(context
, value
));
716 return CYCastJavaObject(jni
, context
, CYCastJSObject(context
, value
));
718 case kJSTypeUndefined
:
719 // XXX: I am currently relying on this for dynamic proxy of void method
726 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &value
) {
727 CYJavaEnv
jni(value
);
728 CYJavaFrame
frame(jni
, 64);
730 JSObjectRef
global(CYGetGlobalObject(context
));
731 JSObjectRef
cy(CYCastJSObject(context
, CYGetProperty(context
, global
, cy_s
)));
733 auto Class$
(jni
.FindClass("java/lang/Class"));
734 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
736 CYJSString
name(jni
.CallObjectMethod
<jstring
>(value
, Class$getName
));
737 JSValueRef
cached(CYGetProperty(context
, cy
, name
));
738 if (!JSValueIsUndefined(context
, cached
))
739 return CYCastJSObject(context
, cached
);
741 JSObjectRef constructor
;
742 JSObjectRef prototype
;
746 auto Class$
isInterface(jni
.GetMethodID(Class$
, "isInterface", "()Z"));
748 auto Class$
getDeclaredConstructors(jni
.GetMethodID(Class$
, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;"));
749 auto Class$
getDeclaredFields(jni
.GetMethodID(Class$
, "getDeclaredFields", "()[Ljava/lang/reflect/Field;"));
750 auto Class$
getDeclaredMethods(jni
.GetMethodID(Class$
, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;"));
752 auto Constructor$
(jni
.FindClass("java/lang/reflect/Constructor"));
753 //auto Constructor$getModifiers(jni.GetMethodID(Constructor$, "getModifiers", "()I"));
754 auto Constructor$
getParameterTypes(jni
.GetMethodID(Constructor$
, "getParameterTypes", "()[Ljava/lang/Class;"));
756 auto Field$
(jni
.FindClass("java/lang/reflect/Field"));
757 auto Field$
getModifiers(jni
.GetMethodID(Field$
, "getModifiers", "()I"));
758 auto Field$
getName(jni
.GetMethodID(Field$
, "getName", "()Ljava/lang/String;"));
759 auto Field$
getType(jni
.GetMethodID(Field$
, "getType", "()Ljava/lang/Class;"));
761 auto Method$
(jni
.FindClass("java/lang/reflect/Method"));
762 auto Method$
getModifiers(jni
.GetMethodID(Method$
, "getModifiers", "()I"));
763 auto Method$
getName(jni
.GetMethodID(Method$
, "getName", "()Ljava/lang/String;"));
764 auto Method$
getParameterTypes(jni
.GetMethodID(Method$
, "getParameterTypes", "()[Ljava/lang/Class;"));
765 auto Method$
getReturnType(jni
.GetMethodID(Method$
, "getReturnType", "()Ljava/lang/Class;"));
767 auto Modifier$
(jni
.FindClass("java/lang/reflect/Modifier"));
768 auto Modifier$
isStatic(jni
.GetStaticMethodID(Modifier$
, "isStatic", "(I)Z"));
770 auto interface(jni
.CallBooleanMethod(value
, Class$isInterface
));
771 auto table(new CYJavaClass(value
, interface
));
773 for (CYJavaLocal
<jclass
> prototype(value
); prototype
; prototype
= jni
.GetSuperclass(prototype
)) {
774 auto fields(jni
.CallObjectMethod
<jobjectArray
>(prototype
, Class$getDeclaredFields
));
776 for (jsize
i(0), e(jni
.GetArrayLength(fields
)); i
!= e
; ++i
) {
777 auto field(jni
.GetObjectArrayElement
<jobject
>(fields
, e
- i
- 1));
778 auto modifiers(jni
.CallIntMethod(field
, Field$getModifiers
));
779 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
780 auto &map(instance
? table
->instance_
: table
->static_
);
781 CYJavaUTF8String
name(jni
.CallObjectMethod
<jstring
>(field
, Field$getName
));
782 auto id(jni
.FromReflectedField(field
));
783 auto type(jni
.CallObjectMethod
<jclass
>(field
, Field$getType
));
784 map
.insert(std::make_pair(std::string(name
), CYJavaField
{id
, CYJavaGetPrimitive(context
, type
, Class$getName
)}));
788 constructor
= JSObjectMake(context
, CYJavaClass::Class_
, table
);
790 prototype
= JSObjectMake(context
, NULL
, NULL
);
791 CYSetProperty(context
, constructor
, prototype_s
, prototype
, kJSPropertyAttributeDontEnum
);
793 auto constructors(jni
.CallObjectMethod
<jobjectArray
>(value
, Class$getDeclaredConstructors
));
795 for (jsize
i(0), e(jni
.GetArrayLength(constructors
)); i
!= e
; ++i
) {
796 auto constructor(jni
.GetObjectArrayElement
<jobject
>(constructors
, i
));
797 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(constructor
, Constructor$getParameterTypes
));
798 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
799 auto id(jni
.FromReflectedMethod(constructor
));
800 table
->overload_
.insert(CYJavaSignature(constructor
, id
, CYJavaPrimitiveObject
, shorty
));
803 auto methods(jni
.CallObjectMethod
<jobjectArray
>(value
, Class$getDeclaredMethods
));
805 std::map
<std::pair
<bool, std::string
>, CYJavaOverload
> entries
;
807 for (jsize
i(0), e(jni
.GetArrayLength(methods
)); i
!= e
; ++i
) {
808 auto method(jni
.GetObjectArrayElement
<jobject
>(methods
, i
));
809 auto modifiers(jni
.CallIntMethod(method
, Method$getModifiers
));
810 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
811 CYJavaUTF8String
name(jni
.CallObjectMethod
<jstring
>(method
, Method$getName
));
812 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(method
, Method$getParameterTypes
));
813 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
814 auto type(jni
.CallObjectMethod
<jclass
>(method
, Method$getReturnType
));
815 auto primitive(CYJavaGetPrimitive(context
, type
, Class$getName
));
816 auto id(jni
.FromReflectedMethod(method
));
817 entries
[std::make_pair(instance
, std::string(name
))].insert(CYJavaSignature(method
, id
, primitive
, shorty
));
820 for (const auto &entry
: entries
) {
821 bool instance(entry
.first
.first
);
822 CYJSString
name(entry
.first
.second
);
823 auto &overload(entry
.second
);
825 CYSetProperty(context
, prototype
, name
, CYJavaMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
827 CYSetProperty(context
, constructor
, name
, CYJavaStaticMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
832 // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name
833 // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :(
835 if (auto super
= jni
.GetSuperclass(value
)) {
836 JSObjectRef
parent(CYGetJavaClass(context
, super
));
837 CYSetPrototype(context
, constructor
, parent
);
838 CYSetPrototype(context
, prototype
, CYGetProperty(context
, parent
, prototype_s
));
841 CYSetProperty(context
, cy
, name
, constructor
);
845 static void CYCastJavaNumeric(jvalue
&value
, CYJavaPrimitive primitive
, JSContextRef context
, JSValueRef argument
) {
847 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
848 case CYJavaPrimitive ## Type: \
849 value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \
851 CYJavaForEachPrimitive
852 #undef CYJavaForEachPrimitive_
858 static bool CYCastJavaArguments(const CYJavaEnv
&jni
, const CYJavaShorty
&shorty
, JSContextRef context
, const JSValueRef arguments
[], jvalue
*array
) {
859 for (size_t index(0); index
!= shorty
.size(); ++index
) {
860 JSValueRef
argument(arguments
[index
]);
861 JSType
type(JSValueGetType(context
, argument
));
862 jvalue
&value(array
[index
]);
864 switch (CYJavaPrimitive primitive
= shorty
[index
]) {
865 case CYJavaPrimitiveObject
:
866 value
.l
= CYCastJavaObject(jni
, context
, argument
).leak();
869 case CYJavaPrimitiveBoolean
:
870 if (type
!= kJSTypeBoolean
)
872 value
.z
= CYCastBool(context
, argument
);
875 case CYJavaPrimitiveCharacter
:
876 if (type
== kJSTypeNumber
)
877 CYCastJavaNumeric(value
, primitive
, context
, argument
);
878 else if (type
!= kJSTypeString
)
881 CYJSString
string(context
, argument
);
882 if (JSStringGetLength(string
) != 1)
885 value
.c
= JSStringGetCharactersPtr(string
)[0];
889 case CYJavaPrimitiveByte
:
890 case CYJavaPrimitiveShort
:
891 case CYJavaPrimitiveInteger
:
892 case CYJavaPrimitiveLong
:
893 case CYJavaPrimitiveFloat
:
894 case CYJavaPrimitiveDouble
:
895 if (type
!= kJSTypeNumber
)
897 CYCastJavaNumeric(value
, primitive
, context
, argument
);
908 static JSValueRef
JavaMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
909 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
910 CYJavaObject
*self(CYGetJavaObject(context
, _this
));
911 CYJavaEnv
jni(self
->value_
);
913 CYJavaSignature
bound(count
);
914 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
915 CYJavaFrame(jni
, count
+ 16);
917 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
919 jvalue
*values(array
);
920 switch (overload
->primitive_
) {
921 case CYJavaPrimitiveObject
:
922 return CYCastJSValue(context
, jni
.CallObjectMethodA
<jobject
>(self
->value_
, overload
->method_
, values
));
923 case CYJavaPrimitiveVoid
:
924 jni
.CallVoidMethodA(self
->value_
, overload
->method_
, values
);
925 return CYJSUndefined(context
);
926 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
927 case CYJavaPrimitive ## Type: \
928 return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, overload->method_, values));
929 CYJavaForEachPrimitive
930 #undef CYJavaForEachPrimitive_
931 default: _assert(false);
935 CYThrow("invalid method call");
938 static JSValueRef
JavaStaticMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
939 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
940 CYJavaClass
*table(CYGetJavaTable(context
, _this
));
941 CYJavaEnv
jni(table
->value_
);
943 CYJavaSignature
bound(count
);
944 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
945 CYJavaFrame(jni
, count
+ 16);
947 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
949 jvalue
*values(array
);
950 switch (overload
->primitive_
) {
951 case CYJavaPrimitiveObject
:
952 return CYCastJSValue(context
, jni
.CallStaticObjectMethodA
<jobject
>(table
->value_
, overload
->method_
, values
));
953 case CYJavaPrimitiveVoid
:
954 jni
.CallStaticVoidMethodA(table
->value_
, overload
->method_
, values
);
955 return CYJSUndefined(context
);
956 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
957 case CYJavaPrimitive ## Type: \
958 return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, overload->method_, values));
959 CYJavaForEachPrimitive
960 #undef CYJavaForEachPrimitive_
961 default: _assert(false);
965 CYThrow("invalid method call");
968 static JSObjectRef
JavaClass_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
969 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
970 CYJavaEnv
jni(table
->value_
);
971 jclass
_class(table
->value_
);
973 if (table
->interface_
&& count
== 1) {
974 JSObjectRef
target(CYCastJSObject(context
, arguments
[0]));
975 auto Cycript$
(jni
.FindClass("Cycript"));
976 auto Cycript$
Make(jni
.GetStaticMethodID(Cycript$
, "proxy", "(Ljava/lang/Class;J)Ljava/lang/Object;"));
977 auto protect(new CYProtect(context
, target
));
978 return CYCastJSObject(context
, jni
.CallObjectMethod
<jobject
>(Cycript$
, Cycript$Make
, _class
, reinterpret_cast<jlong
>(protect
)));
981 CYJavaSignature
bound(count
);
982 for (auto overload(table
->overload_
.lower_bound(bound
)), e(table
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
983 CYJavaFrame(jni
, count
+ 16);
985 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
987 jvalue
*values(array
);
988 auto object(jni
.NewObjectA(_class
, overload
->method_
, values
));
989 return CYCastJSObject(context
, object
);
992 CYThrow("invalid constructor call");
995 static bool JavaStaticInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
996 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
997 CYJavaClass
*table(internal
->table_
);
999 auto name(CYPoolUTF8String(pool
, context
, property
));
1000 auto field(table
->static_
.find(name
));
1001 if (field
== table
->static_
.end())
1006 static JSValueRef
JavaStaticInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1007 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
1008 CYJavaClass
*table(internal
->table_
);
1009 CYJavaEnv
jni(table
->value_
);
1011 auto name(CYPoolUTF8String(pool
, context
, property
));
1012 auto field(table
->static_
.find(name
));
1013 if (field
== table
->static_
.end())
1016 switch (field
->second
.primitive_
) {
1017 case CYJavaPrimitiveObject
:
1018 return CYCastJSValue(context
, jni
.GetStaticObjectField
<jobject
>(table
->value_
, field
->second
.field_
));
1019 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1020 case CYJavaPrimitive ## Type: \
1021 return CYJavaCastJSValue(context, jni.GetStatic ## Typ ## Field(table->value_, field->second.field_));
1022 CYJavaForEachPrimitive
1023 #undef CYJavaForEachPrimitive_
1024 default: _assert(false);
1028 static bool JavaStaticInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1029 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
1030 CYJavaClass
*table(internal
->table_
);
1031 CYJavaEnv
jni(table
->value_
);
1033 auto name(CYPoolUTF8String(pool
, context
, property
));
1034 auto field(table
->static_
.find(name
));
1035 if (field
== table
->static_
.end())
1038 switch (field
->second
.primitive_
) {
1039 case CYJavaPrimitiveObject
:
1040 jni
.SetStaticObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1041 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1042 case CYJavaPrimitive ## Type: \
1043 jni.SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1045 CYJavaForEachPrimitive
1046 #undef CYJavaForEachPrimitive_
1047 default: _assert(false);
1053 static void JavaStaticInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1054 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
1055 CYJavaClass
*table(internal
->table_
);
1056 for (const auto &field
: table
->static_
)
1057 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1060 static JSValueRef
JavaClass_getProperty_class(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1061 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
1062 return CYCastJSValue(context
, table
->value_
);
1065 static bool JavaInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1066 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1067 CYJavaClass
*table(internal
->table_
);
1069 auto name(CYPoolUTF8String(pool
, context
, property
));
1070 auto field(table
->instance_
.find(name
));
1071 if (field
== table
->instance_
.end())
1076 static JSValueRef
JavaInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1077 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1078 CYJavaEnv
jni(internal
->value_
);
1079 CYJavaClass
*table(internal
->table_
);
1081 auto name(CYPoolUTF8String(pool
, context
, property
));
1082 auto field(table
->instance_
.find(name
));
1083 if (field
== table
->instance_
.end())
1086 switch (field
->second
.primitive_
) {
1087 case CYJavaPrimitiveObject
:
1088 return CYCastJSValue(context
, jni
.GetObjectField
<jobject
>(internal
->value_
, field
->second
.field_
));
1089 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1090 case CYJavaPrimitive ## Type: \
1091 return CYJavaCastJSValue(context, jni.Get ## Typ ## Field(internal->value_, field->second.field_));
1092 CYJavaForEachPrimitive
1093 #undef CYJavaForEachPrimitive_
1094 default: _assert(false);
1098 static bool JavaInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1099 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1100 CYJavaEnv
jni(internal
->value_
);
1101 CYJavaClass
*table(internal
->table_
);
1103 auto name(CYPoolUTF8String(pool
, context
, property
));
1104 auto field(table
->instance_
.find(name
));
1105 if (field
== table
->instance_
.end())
1108 switch (field
->second
.primitive_
) {
1109 case CYJavaPrimitiveObject
:
1110 jni
.SetObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1111 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1112 case CYJavaPrimitive ## Type: \
1113 jni.Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1115 CYJavaForEachPrimitive
1116 #undef CYJavaForEachPrimitive_
1117 default: _assert(false);
1123 static void JavaInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1124 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1125 CYJavaClass
*table(internal
->table_
);
1126 for (const auto &field
: table
->instance_
)
1127 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1130 static JSValueRef
JavaObject_getProperty_constructor(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1131 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
1132 CYJavaEnv
jni(internal
->value_
);
1133 return CYGetJavaClass(context
, jni
.GetObjectClass(internal
->value_
));
1136 static JSValueRef JavaClass_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1137 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
1138 return CYJavaStaticInterior::Make(context
, internal
->value_
, internal
);
1141 static JSValueRef JavaObject_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1142 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
1143 return CYJavaInterior::Make(context
, internal
->value_
, internal
->table_
);
1146 static JSValueRef
JavaClass_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1147 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(_this
)));
1148 CYJavaEnv
jni(internal
->value_
);
1149 auto Class$
(jni
.FindClass("java/lang/Class"));
1150 auto Class$
getCanonicalName(jni
.GetMethodID(Class$
, "getCanonicalName", "()Ljava/lang/String;"));
1151 return CYCastJSValue(context
, CYJSString(jni
.CallObjectMethod
<jstring
>(internal
->value_
, Class$getCanonicalName
)));
1154 static JSValueRef
JavaMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1155 std::ostringstream cyon
;
1156 return CYCastJSValue(context
, CYJSString(cyon
.str()));
1159 static JSValueRef
JavaStaticMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1160 std::ostringstream cyon
;
1161 return CYCastJSValue(context
, CYJSString(cyon
.str()));
1164 static JSValueRef
JavaArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1165 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
1166 CYJavaEnv
jni(internal
->value_
);
1167 if (JSStringIsEqual(property
, length_s
))
1168 return CYCastJSValue(context
, jni
.GetArrayLength(internal
->value_
));
1172 if (!CYGetOffset(pool
, context
, property
, offset
))
1175 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1176 return CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
));
1177 else switch (internal
->primitive_
) {
1178 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1179 case CYJavaPrimitive ## Type: { \
1180 j ## type element; \
1181 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1182 return CYJavaCastJSValue(context, element); \
1184 CYJavaForEachPrimitive
1185 #undef CYJavaForEachPrimitive_
1186 default: _assert(false);
1190 static bool JavaArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1191 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
1192 CYJavaEnv
jni(internal
->value_
);
1196 if (!CYGetOffset(pool
, context
, property
, offset
))
1199 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1200 jni
.SetObjectArrayElement(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
, CYCastJavaObject(jni
, context
, value
));
1201 else switch (internal
->primitive_
) {
1202 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1203 case CYJavaPrimitive ## Type: { \
1204 j ## type element; \
1205 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1206 return CYJavaCastJSValue(context, element); \
1208 CYJavaForEachPrimitive
1209 #undef CYJavaForEachPrimitive_
1210 default: _assert(false);
1216 static JSValueRef
JavaPackage_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1217 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(_this
)));
1218 std::ostringstream name
;
1219 for (auto &package
: internal
->package_
)
1220 name
<< package
<< '.';
1222 return CYCastJSValue(context
, CYJSString(name
.str()));
1225 static bool CYJavaPackage_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1229 static JSValueRef
CYJavaPackage_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1230 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(object
)));
1231 CYJavaPackage::Path
package(internal
->package_
);
1234 const char *next(CYPoolCString(pool
, context
, property
));
1236 std::ostringstream name
;
1237 for (auto &package
: internal
->package_
)
1238 name
<< package
<< '/';
1241 JNIEnv
*jni(GetJNI(context
));
1242 if (auto _class
= jni
->FindClass(name
.str().c_str()))
1243 return CYGetJavaClass(context
, CYJavaLocal
<jclass
>(jni
, _class
));
1244 jni
->ExceptionClear();
1246 package
.push_back(next
);
1247 return CYJavaPackage::Make(context
, package
);
1250 static void Cycript_delete(JNIEnv
*env
, jclass api
, jlong jprotect
) { CYJavaTry
{
1254 static jobject
Cycript_handle(JNIEnv
*env
, jclass api
, jlong jprotect
, jstring property
, jobjectArray jarguments
) { CYJavaTry
{
1255 JSValueRef
function(CYGetProperty(context
, object
, CYJSString(CYJavaRef
<jstring
>(jni
, property
))));
1256 if (JSValueIsUndefined(context
, function
))
1259 size_t count(jarguments
== NULL
? 0 : jni
.GetArrayLength(jarguments
));
1260 JSValueRef arguments
[count
];
1261 for (size_t index(0); index
!= count
; ++index
) {
1262 arguments
[index
] = CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(jarguments
, index
));
1265 return CYCastJavaObject(jni
, context
, CYCallAsFunction(context
, CYCastJSObject(context
, function
), object
, count
, arguments
));
1266 } CYJavaCatch(NULL
) }
1268 static JNINativeMethod Cycript_
[] = {
1269 {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete
},
1270 {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle
},
1273 JNIEnv
*GetJNI(JSContextRef context
) {
1274 static JavaVM
*jvm(NULL
);
1275 static JNIEnv
*jni(NULL
);
1279 jint
version(JNI_VERSION_1_4
);
1282 JavaVM
*jvms
[capacity
];
1284 _jnicall(JNI_GetCreatedJavaVMs(jvms
, capacity
, &size
));
1288 _jnicall(jvm
->GetEnv(reinterpret_cast<void **>(&jni
), version
));
1291 std::vector
<JavaVMOption
> options
;
1294 std::ostringstream option
;
1295 option
<< "-Djava.class.path=";
1296 option
<< CYPoolLibraryPath(pool
) << "/libcycript.jar";
1297 if (const char *classpath
= getenv("CLASSPATH"))
1298 option
<< ':' << classpath
;
1299 options
.push_back(JavaVMOption
{pool
.strdup(option
.str().c_str()), NULL
});
1302 JavaVMInitArgs args
;
1303 memset(&args
, 0, sizeof(args
));
1304 args
.version
= version
;
1305 args
.nOptions
= options
.size();
1306 args
.options
= options
.data();
1307 _jnicall(JNI_CreateJavaVM(&jvm
, reinterpret_cast<void **>(&jni
), &args
));
1310 auto Cycript$
(CYJavaEnv(jni
).FindClass("Cycript"));
1311 _envcall(jni
, RegisterNatives(Cycript$
, Cycript_
, sizeof(Cycript_
) / sizeof(Cycript_
[0])));
1316 static JSStaticValue JavaClass_staticValues
[3] = {
1317 {"class", &JavaClass_getProperty_class
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1318 {"$cyi", &JavaClass_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1319 {NULL
, NULL
, NULL
, 0}
1322 static JSStaticFunction JavaClass_staticFunctions
[2] = {
1323 {"toCYON", &JavaClass_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1327 static JSStaticValue JavaObject_staticValues
[3] = {
1328 {"constructor", &JavaObject_getProperty_constructor
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1329 {"$cyi", &JavaObject_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1330 {NULL
, NULL
, NULL
, 0}
1333 static JSStaticFunction JavaMethod_staticFunctions
[2] = {
1334 {"toCYON", &JavaMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1338 static JSStaticFunction JavaStaticMethod_staticFunctions
[2] = {
1339 {"toCYON", &JavaStaticMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1343 static JSStaticFunction JavaPackage_staticFunctions
[2] = {
1344 {"toCYON", &JavaPackage_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1348 void CYJava_Initialize() {
1349 Primitives_
.insert(std::make_pair("void", CYJavaPrimitiveVoid
));
1350 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1351 Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type));
1352 CYJavaForEachPrimitive
1353 #undef CYJavaForEachPrimitive_
1355 JSClassDefinition definition
;
1357 definition
= kJSClassDefinitionEmpty
;
1358 definition
.className
= "JavaClass";
1359 definition
.staticValues
= JavaClass_staticValues
;
1360 definition
.staticFunctions
= JavaClass_staticFunctions
;
1361 definition
.callAsConstructor
= &JavaClass_callAsConstructor
;
1362 definition
.finalize
= &CYFinalize
;
1363 CYJavaClass::Class_
= JSClassCreate(&definition
);
1365 definition
= kJSClassDefinitionEmpty
;
1366 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1367 definition
.className
= "JavaInterior";
1368 definition
.hasProperty
= &JavaInterior_hasProperty
;
1369 definition
.getProperty
= &JavaInterior_getProperty
;
1370 definition
.setProperty
= &JavaInterior_setProperty
;
1371 definition
.getPropertyNames
= &JavaInterior_getPropertyNames
;
1372 definition
.finalize
= &CYFinalize
;
1373 CYJavaInterior::Class_
= JSClassCreate(&definition
);
1375 definition
= kJSClassDefinitionEmpty
;
1376 definition
.className
= "JavaMethod";
1377 definition
.staticFunctions
= JavaMethod_staticFunctions
;
1378 definition
.callAsFunction
= &JavaMethod_callAsFunction
;
1379 definition
.finalize
= &CYFinalize
;
1380 CYJavaMethod::Class_
= JSClassCreate(&definition
);
1382 definition
= kJSClassDefinitionEmpty
;
1383 definition
.className
= "JavaStaticMethod";
1384 definition
.staticFunctions
= JavaStaticMethod_staticFunctions
;
1385 definition
.callAsFunction
= &JavaStaticMethod_callAsFunction
;
1386 definition
.finalize
= &CYFinalize
;
1387 CYJavaStaticMethod::Class_
= JSClassCreate(&definition
);
1389 definition
= kJSClassDefinitionEmpty
;
1390 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1391 definition
.className
= "JavaObject";
1392 definition
.staticValues
= JavaObject_staticValues
;
1393 definition
.finalize
= &CYFinalize
;
1394 CYJavaObject::Class_
= JSClassCreate(&definition
);
1396 definition
= kJSClassDefinitionEmpty
;
1397 definition
.className
= "JavaArray";
1398 definition
.getProperty
= &JavaArray_getProperty
;
1399 definition
.setProperty
= &JavaArray_setProperty
;
1400 definition
.finalize
= &CYFinalize
;
1401 CYJavaArray::Class_
= JSClassCreate(&definition
);
1403 definition
= kJSClassDefinitionEmpty
;
1404 definition
.className
= "JavaPackage";
1405 definition
.staticFunctions
= JavaPackage_staticFunctions
;
1406 definition
.hasProperty
= &CYJavaPackage_hasProperty
;
1407 definition
.getProperty
= &CYJavaPackage_getProperty
;
1408 definition
.finalize
= &CYFinalize
;
1409 CYJavaPackage::Class_
= JSClassCreate(&definition
);
1411 definition
= kJSClassDefinitionEmpty
;
1412 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1413 definition
.className
= "JavaStaticInterior";
1414 definition
.hasProperty
= &JavaStaticInterior_hasProperty
;
1415 definition
.getProperty
= &JavaStaticInterior_getProperty
;
1416 definition
.setProperty
= &JavaStaticInterior_setProperty
;
1417 definition
.getPropertyNames
= &JavaStaticInterior_getPropertyNames
;
1418 definition
.finalize
= &CYFinalize
;
1419 CYJavaStaticInterior::Class_
= JSClassCreate(&definition
);
1422 void CYJava_SetupContext(JSContextRef context
) {
1423 JSObjectRef
global(CYGetGlobalObject(context
));
1424 //JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
1425 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1426 JSObjectRef
all(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("all"))));
1427 //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
1429 JSObjectRef
Java(JSObjectMake(context
, NULL
, NULL
));
1430 CYSetProperty(context
, cycript
, CYJSString("Java"), Java
);
1432 JSObjectRef
Packages(CYJavaPackage::Make(context
, CYJavaPackage::Path()));
1433 CYSetProperty(context
, all
, CYJSString("Packages"), Packages
);
1435 for (auto name
: (const char *[]) {"java", "javax", "android", "com", "net", "org"}) {
1436 CYJSString
js(name
);
1437 CYSetProperty(context
, all
, js
, CYGetProperty(context
, Packages
, js
));
1441 static CYHook CYJavaHook
= {
1446 &CYJava_SetupContext
,
1450 CYRegisterHook
CYJava(&CYJavaHook
);