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 bool() const {
113 return value_
!= NULL
;
116 _finline
operator JNIEnv
*() const {
120 _finline
operator Value_() const {
124 _finline Value_
get() const {
128 template <typename Other_
>
129 _finline CYJavaRef
<Other_
> cast() const {
130 return {jni_
, static_cast<Other_
>(value_
)};
134 Value_
value(value_
);
140 template <typename Value_
, void (JNIEnv::*Delete_
)(jobject
)>
141 struct CYJavaDelete
:
144 _finline
CYJavaDelete(JNIEnv
*jni
, Value_ value
) :
145 CYJavaRef
<Value_
>(jni
, value
)
150 if (this->value_
!= NULL
)
151 (this->jni_
->*Delete_
)(this->value_
);
155 template <typename Value_
>
156 struct CYJavaGlobal
:
157 CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
>
159 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteGlobalRef
> CYJavaBase
;
162 CYJavaBase(NULL
, NULL
)
166 template <typename Other_
>
167 CYJavaGlobal(const CYJavaRef
<Other_
> &other
) :
168 CYJavaBase(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewGlobalRef(other
.value_
)))
172 CYJavaGlobal(const CYJavaGlobal
<Value_
> &other
) :
173 CYJavaGlobal(static_cast<const CYJavaRef
<Value_
> &>(other
))
177 CYJavaGlobal(CYJavaGlobal
&&value
) :
178 CYJavaBase(value
.jni_
, value
.value_
)
184 template <typename Value_
>
186 CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
>
188 typedef CYJavaDelete
<Value_
, &JNIEnv::DeleteLocalRef
> CYJavaBase
;
191 CYJavaBase(NULL
, NULL
)
195 CYJavaLocal(JNIEnv
*jni
, Value_ value
) :
196 CYJavaBase(jni
, value
)
200 template <typename Other_
>
201 CYJavaLocal(const CYJavaRef
<Other_
> &other
) :
202 CYJavaLocal(other
.jni_
, static_cast<Other_
>(other
.jni_
->NewLocalRef(other
.value_
)))
206 CYJavaLocal(CYJavaLocal
&&value
) :
207 CYJavaLocal(value
.jni_
, value
.value_
)
212 CYJavaLocal
&operator =(CYJavaLocal
<Value_
> &&other
) {
213 std::swap(this->jni_
, other
.jni_
);
214 std::swap(this->value_
, other
.value_
);
220 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaRef
<jobject
> &value
);
222 class CYJavaUTF8String
:
226 const CYJavaRef
<jstring
> *value_
;
229 CYJavaUTF8String(const CYJavaRef
<jstring
> &value
) :
234 size
= jni
->GetStringUTFLength(value
);
235 data
= jni
->GetStringUTFChars(value
, NULL
);
238 CYJavaUTF8String(const CYJavaRef
<jobject
> &value
) :
239 CYJavaUTF8String(CYCastJavaString(value
))
243 ~CYJavaUTF8String() {
244 if (value_
!= NULL
) {
245 JNIEnv
*jni(*value_
);
246 jni
->ReleaseStringUTFChars(*value_
, data
);
250 CYJavaUTF8String(const CYJavaUTF8String
&) = delete;
252 CYJavaUTF8String(CYJavaUTF8String
&&rhs
) :
259 CYJavaUTF8String
CYCastUTF8String(const CYJavaRef
<jstring
> &value
) {
260 return CYJavaUTF8String(value
);
263 JSStringRef
CYCopyJSString(const CYJavaRef
<jstring
> &value
) {
264 return CYCopyJSString(CYCastUTF8String(value
));
271 CYJavaGlobal
<jthrowable
> value_
;
273 CYJavaError(const CYJavaRef
<jthrowable
> &value
) :
278 virtual const char *PoolCString(CYPool
&pool
) const {
279 return CYPoolCString(pool
, CYJavaUTF8String(value_
.cast
<jobject
>()));
282 virtual JSValueRef
CastJSValue(JSContextRef context
, const char *name
) const {
283 return CYCastJSValue(context
, value_
);
291 CYJavaFrame(JNIEnv
*jni
, jint capacity
) :
294 _assert(jni
->PushLocalFrame(capacity
) == 0);
301 jobject
operator ()(jobject object
) {
304 return jni
->PopLocalFrame(object
);
313 CYJavaEnv(JNIEnv
*jni
) :
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 template <typename Type_
>
438 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, Type_ value
) {
439 return CYCastJSValue(context
, value
);
442 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, jboolean value
) {
443 return CYCastJSValue(context
, static_cast<bool>(value
));
446 static std::map
<std::string
, CYJavaPrimitive
> Primitives_
;
448 static CYJavaPrimitive
CYJavaGetPrimitive(JSContextRef context
, const CYJavaRef
<jclass
> &type
, jmethodID Class$get$$Name
) {
450 auto string(jni
.CallObjectMethod
<jstring
>(type
, Class$get$$Name
));
453 CYJavaUTF8String
name(string
);
454 auto primitive(Primitives_
.find(name
));
455 return primitive
!= Primitives_
.end() ? primitive
->second
: CYJavaPrimitiveObject
;
458 typedef std::vector
<CYJavaPrimitive
> CYJavaShorty
;
460 static CYJavaShorty
CYJavaGetShorty(JSContextRef context
, const CYJavaRef
<jobjectArray
> &types
, jmethodID Class$get$$Name
) {
461 CYJavaEnv
jni(types
);
462 size_t count(jni
.GetArrayLength(types
));
463 CYJavaShorty
shorty(count
);
464 for (size_t index(0); index
!= count
; ++index
)
465 shorty
[index
] = CYJavaGetPrimitive(context
, jni
.GetObjectArrayElement
<jclass
>(types
, index
), Class$get$$Name
);
471 CYJavaPrimitive primitive_
;
474 typedef std::map
<std::string
, CYJavaField
> CYJavaFieldMap
;
476 struct CYJavaSignature
{
477 CYJavaGlobal
<jobject
> reflected_
;
479 CYJavaPrimitive primitive_
;
480 CYJavaShorty shorty_
;
482 CYJavaSignature(const CYJavaRef
<jobject
> &reflected
, jmethodID method
, CYJavaPrimitive primitive
, const CYJavaShorty
&shorty
) :
483 reflected_(reflected
),
485 primitive_(primitive
),
490 CYJavaSignature(unsigned count
) :
495 bool operator <(const CYJavaSignature
&rhs
) const {
496 return shorty_
.size() < rhs
.shorty_
.size();
500 typedef std::multiset
<CYJavaSignature
> CYJavaOverload
;
502 struct CYJavaMethod
:
503 CYPrivate
<CYJavaMethod
>
505 CYJavaOverload overload_
;
507 CYJavaMethod(const CYJavaOverload
&overload
) :
513 struct CYJavaStaticMethod
:
514 CYPrivate
<CYJavaStaticMethod
>
516 CYJavaOverload overload_
;
518 CYJavaStaticMethod(const CYJavaOverload
&overload
) :
525 CYJavaValue
<CYJavaClass
, jclass
>
529 CYJavaFieldMap static_
;
530 CYJavaFieldMap instance_
;
531 CYJavaOverload overload_
;
533 CYJavaClass(const CYJavaRef
<jclass
> &value
, bool interface
) :
535 interface_(interface
)
540 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &_class
);
542 struct CYJavaObject
:
543 CYJavaValue
<CYJavaObject
, jobject
>
547 CYJavaObject(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
553 JSValueRef
GetPrototype(JSContextRef context
) const;
556 struct CYJavaInterior
:
557 CYJavaValue
<CYJavaInterior
, jobject
>
561 CYJavaInterior(const CYJavaRef
<jobject
> &value
, CYJavaClass
*table
) :
568 struct CYJavaStaticInterior
:
569 CYJavaValue
<CYJavaStaticInterior
, jclass
>
573 CYJavaStaticInterior(const CYJavaRef
<jclass
> &value
, CYJavaClass
*table
) :
581 CYJavaValue
<CYJavaArray
, jarray
>
583 CYJavaPrimitive primitive_
;
585 CYJavaArray(const CYJavaRef
<jarray
> &value
, CYJavaPrimitive primitive
) :
587 primitive_(primitive
)
591 JSValueRef
GetPrototype(JSContextRef context
) const;
594 struct CYJavaPackage
:
595 CYPrivate
<CYJavaPackage
>
597 typedef std::vector
<std::string
> Path
;
600 _finline
CYJavaPackage(const Path
&package
) :
606 JSValueRef
CYJavaObject::GetPrototype(JSContextRef context
) const {
607 CYJavaEnv
jni(value_
);
608 return CYGetProperty(context
, CYGetJavaClass(context
, jni
.GetObjectClass(value_
)), prototype_s
);
611 JSValueRef
CYJavaArray::GetPrototype(JSContextRef context
) const {
612 return CYGetCachedObject(context
, CYJSString("Array_prototype"));
615 static JSValueRef
CYCastJSValue(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
617 return CYJSNull(context
);
618 CYJavaEnv
jni(value
);
620 auto _class(jni
.GetObjectClass(value
));
621 if (jni
.IsSameObject(_class
, jni
.FindClass("java/lang/String")))
622 return CYCastJSValue(context
, CYJSString(value
.cast
<jstring
>()));
624 auto Class$
(jni
.FindClass("java/lang/Class"));
625 auto Class$
isArray(jni
.GetMethodID(Class$
, "isArray", "()Z"));
626 if (jni
.CallBooleanMethod(_class
, Class$isArray
)) {
627 auto Class$
getComponentType(jni
.GetMethodID(Class$
, "getComponentType", "()Ljava/lang/Class;"));
628 auto component(jni
.CallObjectMethod
<jclass
>(_class
, Class$getComponentType
));
629 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
630 return CYJavaArray::Make(context
, value
.cast
<jarray
>(), CYJavaGetPrimitive(context
, component
, Class$getName
));
633 auto Wrapper$
(jni
.FindClass("Cycript$Wrapper"));
634 if (jni
.IsSameObject(_class
, Wrapper$
)) {
635 auto Wrapper$
getProtect(jni
.GetMethodID(Wrapper$
, "getProtect", "()J"));
636 auto &protect(*reinterpret_cast<CYProtect
*>(jni
.CallLongMethod(value
, Wrapper$getProtect
)));
640 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(CYGetJavaClass(context
, _class
))));
641 return CYJavaObject::Make(context
, value
, table
);
644 static _finline JSObjectRef
CYCastJSObject(JSContextRef context
, const CYJavaRef
<jobject
> &value
) {
645 return CYCastJSObject(context
, CYCastJSValue(context
, value
));
648 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, JSContextRef context
, CYUTF16String value
) {
649 return jni
.NewString(value
.data
, value
.size
);
652 static CYJavaLocal
<jstring
> CYCastJavaString(const CYJavaEnv
&jni
, JSContextRef context
, JSStringRef value
) {
653 return CYCastJavaString(jni
, context
, CYCastUTF16String(value
));
656 #define CYCastJava$(T, Type, jtype, Cast) \
657 _disused static CYJavaLocal<jobject> CYCastJava ## Type(const CYJavaEnv &jni, JSContextRef context, JSValueRef value) { \
658 auto Type$(jni.FindClass("java/lang/" #Type)); \
659 auto Type$init$(jni.GetMethodID(Type$, "<init>", "(" #T ")V")); \
660 return jni.NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value))); \
663 CYCastJava$
(Z
, Boolean
, jboolean
, CYCastBool
)
664 CYCastJava$
(B
, Byte
, jbyte
, CYCastDouble
)
665 CYCastJava$
(C
, Character
, jchar
, CYCastDouble
)
666 CYCastJava$
(S
, Short
, jshort
, CYCastDouble
)
667 CYCastJava$
(I
, Integer
, jint
, CYCastDouble
)
668 CYCastJava$
(J
, Long
, jlong
, CYCastDouble
)
669 CYCastJava$
(F
, Float
, jfloat
, CYCastDouble
)
670 CYCastJava$
(D
, Double
, jdouble
, CYCastDouble
)
672 static CYJavaClass
*CYGetJavaTable(JSContextRef context
, JSObjectRef object
) {
673 if (!JSValueIsObjectOfClass(context
, object
, CYJavaClass::Class_
))
675 return reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
));
678 static CYJavaObject
*CYGetJavaObject(JSContextRef context
, JSObjectRef object
) {
679 if (!JSValueIsObjectOfClass(context
, object
, CYJavaObject::Class_
))
681 return reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
));
684 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSObjectRef value
) {
685 if (CYJavaObject
*internal
= CYGetJavaObject(context
, value
))
686 return internal
->value_
;
688 auto Wrapper$
(jni
.FindClass("Cycript$Wrapper"));
689 auto Wrapper$$init$
(jni
.GetMethodID(Wrapper$
, "<init>", "(J)V"));
690 CYProtect
*protect(new CYProtect(context
, value
));
691 return jni
.NewObject(Wrapper$
, Wrapper$$init$
, reinterpret_cast<jlong
>(protect
));
694 static CYJavaLocal
<jobject
> CYCastJavaObject(const CYJavaEnv
&jni
, JSContextRef context
, JSValueRef value
) {
695 switch (JSValueGetType(context
, value
)) {
699 return CYCastJavaBoolean(jni
, context
, value
);
701 return CYCastJavaDouble(jni
, context
, value
);
703 return CYCastJavaString(jni
, context
, CYJSString(context
, value
));
705 return CYCastJavaObject(jni
, context
, CYCastJSObject(context
, value
));
707 case kJSTypeUndefined
:
708 // XXX: I am currently relying on this for dynamic proxy of void method
715 static JSObjectRef
CYGetJavaClass(JSContextRef context
, const CYJavaRef
<jclass
> &value
) {
716 CYJavaEnv
jni(value
);
717 CYJavaFrame
frame(jni
, 64);
719 JSObjectRef
global(CYGetGlobalObject(context
));
720 JSObjectRef
cy(CYCastJSObject(context
, CYGetProperty(context
, global
, cy_s
)));
722 auto Class$
(jni
.FindClass("java/lang/Class"));
723 auto Class$
getName(jni
.GetMethodID(Class$
, "getName", "()Ljava/lang/String;"));
725 CYJSString
name(jni
.CallObjectMethod
<jstring
>(value
, Class$getName
));
726 JSValueRef
cached(CYGetProperty(context
, cy
, name
));
727 if (!JSValueIsUndefined(context
, cached
))
728 return CYCastJSObject(context
, cached
);
730 JSObjectRef constructor
;
731 JSObjectRef prototype
;
735 auto Class$
isInterface(jni
.GetMethodID(Class$
, "isInterface", "()Z"));
737 auto Class$
getDeclaredConstructors(jni
.GetMethodID(Class$
, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;"));
738 auto Class$
getDeclaredFields(jni
.GetMethodID(Class$
, "getDeclaredFields", "()[Ljava/lang/reflect/Field;"));
739 auto Class$
getDeclaredMethods(jni
.GetMethodID(Class$
, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;"));
741 auto Constructor$
(jni
.FindClass("java/lang/reflect/Constructor"));
742 //auto Constructor$getModifiers(jni.GetMethodID(Constructor$, "getModifiers", "()I"));
743 auto Constructor$
getParameterTypes(jni
.GetMethodID(Constructor$
, "getParameterTypes", "()[Ljava/lang/Class;"));
745 auto Field$
(jni
.FindClass("java/lang/reflect/Field"));
746 auto Field$
getModifiers(jni
.GetMethodID(Field$
, "getModifiers", "()I"));
747 auto Field$
getName(jni
.GetMethodID(Field$
, "getName", "()Ljava/lang/String;"));
748 auto Field$
getType(jni
.GetMethodID(Field$
, "getType", "()Ljava/lang/Class;"));
750 auto Method$
(jni
.FindClass("java/lang/reflect/Method"));
751 auto Method$
getModifiers(jni
.GetMethodID(Method$
, "getModifiers", "()I"));
752 auto Method$
getName(jni
.GetMethodID(Method$
, "getName", "()Ljava/lang/String;"));
753 auto Method$
getParameterTypes(jni
.GetMethodID(Method$
, "getParameterTypes", "()[Ljava/lang/Class;"));
754 auto Method$
getReturnType(jni
.GetMethodID(Method$
, "getReturnType", "()Ljava/lang/Class;"));
756 auto Modifier$
(jni
.FindClass("java/lang/reflect/Modifier"));
757 auto Modifier$
isStatic(jni
.GetStaticMethodID(Modifier$
, "isStatic", "(I)Z"));
759 auto interface(jni
.CallBooleanMethod(value
, Class$isInterface
));
760 auto table(new CYJavaClass(value
, interface
));
762 for (CYJavaLocal
<jclass
> prototype(value
); prototype
; prototype
= jni
.GetSuperclass(prototype
)) {
763 auto fields(jni
.CallObjectMethod
<jobjectArray
>(prototype
, Class$getDeclaredFields
));
765 for (jsize
i(0), e(jni
.GetArrayLength(fields
)); i
!= e
; ++i
) {
766 auto field(jni
.GetObjectArrayElement
<jobject
>(fields
, e
- i
- 1));
767 auto modifiers(jni
.CallIntMethod(field
, Field$getModifiers
));
768 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
769 auto &map(instance
? table
->instance_
: table
->static_
);
770 CYJavaUTF8String
name(jni
.CallObjectMethod
<jstring
>(field
, Field$getName
));
771 auto id(jni
.FromReflectedField(field
));
772 auto type(jni
.CallObjectMethod
<jclass
>(field
, Field$getType
));
773 map
.insert(std::make_pair(std::string(name
), CYJavaField
{id
, CYJavaGetPrimitive(context
, type
, Class$getName
)}));
777 constructor
= JSObjectMake(context
, CYJavaClass::Class_
, table
);
779 prototype
= JSObjectMake(context
, NULL
, NULL
);
780 CYSetProperty(context
, constructor
, prototype_s
, prototype
, kJSPropertyAttributeDontEnum
);
782 auto constructors(jni
.CallObjectMethod
<jobjectArray
>(value
, Class$getDeclaredConstructors
));
784 for (jsize
i(0), e(jni
.GetArrayLength(constructors
)); i
!= e
; ++i
) {
785 auto constructor(jni
.GetObjectArrayElement
<jobject
>(constructors
, i
));
786 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(constructor
, Constructor$getParameterTypes
));
787 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
788 auto id(jni
.FromReflectedMethod(constructor
));
789 table
->overload_
.insert(CYJavaSignature(constructor
, id
, CYJavaPrimitiveObject
, shorty
));
792 auto methods(jni
.CallObjectMethod
<jobjectArray
>(value
, Class$getDeclaredMethods
));
794 std::map
<std::pair
<bool, std::string
>, CYJavaOverload
> entries
;
796 for (jsize
i(0), e(jni
.GetArrayLength(methods
)); i
!= e
; ++i
) {
797 auto method(jni
.GetObjectArrayElement
<jobject
>(methods
, i
));
798 auto modifiers(jni
.CallIntMethod(method
, Method$getModifiers
));
799 auto instance(!jni
.CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
));
800 CYJavaUTF8String
name(jni
.CallObjectMethod
<jstring
>(method
, Method$getName
));
801 auto parameters(jni
.CallObjectMethod
<jobjectArray
>(method
, Method$getParameterTypes
));
802 CYJavaShorty
shorty(CYJavaGetShorty(context
, parameters
, Class$getName
));
803 auto type(jni
.CallObjectMethod
<jclass
>(method
, Method$getReturnType
));
804 auto primitive(CYJavaGetPrimitive(context
, type
, Class$getName
));
805 auto id(jni
.FromReflectedMethod(method
));
806 entries
[std::make_pair(instance
, std::string(name
))].insert(CYJavaSignature(method
, id
, primitive
, shorty
));
809 for (const auto &entry
: entries
) {
810 bool instance(entry
.first
.first
);
811 CYJSString
name(entry
.first
.second
);
812 auto &overload(entry
.second
);
814 CYSetProperty(context
, prototype
, name
, CYJavaMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
816 CYSetProperty(context
, constructor
, name
, CYJavaStaticMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
821 // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name
822 // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :(
824 if (auto super
= jni
.GetSuperclass(value
)) {
825 JSObjectRef
parent(CYGetJavaClass(context
, super
));
826 CYSetPrototype(context
, constructor
, parent
);
827 CYSetPrototype(context
, prototype
, CYGetProperty(context
, parent
, prototype_s
));
830 CYSetProperty(context
, cy
, name
, constructor
);
834 static void CYCastJavaNumeric(jvalue
&value
, CYJavaPrimitive primitive
, JSContextRef context
, JSValueRef argument
) {
836 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
837 case CYJavaPrimitive ## Type: \
838 value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \
840 CYJavaForEachPrimitive
841 #undef CYJavaForEachPrimitive_
847 static bool CYCastJavaArguments(const CYJavaEnv
&jni
, const CYJavaShorty
&shorty
, JSContextRef context
, const JSValueRef arguments
[], jvalue
*array
) {
848 for (size_t index(0); index
!= shorty
.size(); ++index
) {
849 JSValueRef
argument(arguments
[index
]);
850 JSType
type(JSValueGetType(context
, argument
));
851 jvalue
&value(array
[index
]);
853 switch (CYJavaPrimitive primitive
= shorty
[index
]) {
854 case CYJavaPrimitiveObject
:
855 value
.l
= CYCastJavaObject(jni
, context
, argument
).leak();
858 case CYJavaPrimitiveBoolean
:
859 if (type
!= kJSTypeBoolean
)
861 value
.z
= CYCastBool(context
, argument
);
864 case CYJavaPrimitiveCharacter
:
865 if (type
== kJSTypeNumber
)
866 CYCastJavaNumeric(value
, primitive
, context
, argument
);
867 else if (type
!= kJSTypeString
)
870 CYJSString
string(context
, argument
);
871 if (JSStringGetLength(string
) != 1)
874 value
.c
= JSStringGetCharactersPtr(string
)[0];
878 case CYJavaPrimitiveByte
:
879 case CYJavaPrimitiveShort
:
880 case CYJavaPrimitiveInteger
:
881 case CYJavaPrimitiveLong
:
882 case CYJavaPrimitiveFloat
:
883 case CYJavaPrimitiveDouble
:
884 if (type
!= kJSTypeNumber
)
886 CYCastJavaNumeric(value
, primitive
, context
, argument
);
897 static JSValueRef
JavaMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
898 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
899 CYJavaObject
*self(CYGetJavaObject(context
, _this
));
900 CYJavaEnv
jni(self
->value_
);
902 CYJavaSignature
bound(count
);
903 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
904 CYJavaFrame(jni
, count
+ 16);
906 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
908 jvalue
*values(array
);
909 switch (overload
->primitive_
) {
910 case CYJavaPrimitiveObject
:
911 return CYCastJSValue(context
, jni
.CallObjectMethodA
<jobject
>(self
->value_
, overload
->method_
, values
));
912 case CYJavaPrimitiveVoid
:
913 jni
.CallVoidMethodA(self
->value_
, overload
->method_
, values
);
914 return CYJSUndefined(context
);
915 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
916 case CYJavaPrimitive ## Type: \
917 return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, overload->method_, values));
918 CYJavaForEachPrimitive
919 #undef CYJavaForEachPrimitive_
920 default: _assert(false);
924 CYThrow("invalid method call");
927 static JSValueRef
JavaStaticMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
928 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
929 CYJavaClass
*table(CYGetJavaTable(context
, _this
));
930 CYJavaEnv
jni(table
->value_
);
932 CYJavaSignature
bound(count
);
933 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
934 CYJavaFrame(jni
, count
+ 16);
936 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
938 jvalue
*values(array
);
939 switch (overload
->primitive_
) {
940 case CYJavaPrimitiveObject
:
941 return CYCastJSValue(context
, jni
.CallStaticObjectMethodA
<jobject
>(table
->value_
, overload
->method_
, values
));
942 case CYJavaPrimitiveVoid
:
943 jni
.CallStaticVoidMethodA(table
->value_
, overload
->method_
, values
);
944 return CYJSUndefined(context
);
945 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
946 case CYJavaPrimitive ## Type: \
947 return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, overload->method_, values));
948 CYJavaForEachPrimitive
949 #undef CYJavaForEachPrimitive_
950 default: _assert(false);
954 CYThrow("invalid method call");
957 static JSObjectRef
JavaClass_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
958 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
959 CYJavaEnv
jni(table
->value_
);
960 jclass
_class(table
->value_
);
962 if (table
->interface_
&& count
== 1) {
963 JSObjectRef
target(CYCastJSObject(context
, arguments
[0]));
964 auto Cycript$
(jni
.FindClass("Cycript"));
965 auto Cycript$
Make(jni
.GetStaticMethodID(Cycript$
, "proxy", "(Ljava/lang/Class;J)Ljava/lang/Object;"));
966 auto protect(new CYProtect(context
, target
));
967 return CYCastJSObject(context
, jni
.CallObjectMethod
<jobject
>(Cycript$
, Cycript$Make
, _class
, reinterpret_cast<jlong
>(protect
)));
970 CYJavaSignature
bound(count
);
971 for (auto overload(table
->overload_
.lower_bound(bound
)), e(table
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
972 CYJavaFrame(jni
, count
+ 16);
974 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
976 jvalue
*values(array
);
977 auto object(jni
.NewObjectA(_class
, overload
->method_
, values
));
978 return CYCastJSObject(context
, object
);
981 CYThrow("invalid constructor call");
984 static bool JavaStaticInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
985 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
986 CYJavaClass
*table(internal
->table_
);
988 auto name(CYPoolUTF8String(pool
, context
, property
));
989 auto field(table
->static_
.find(name
));
990 if (field
== table
->static_
.end())
995 static JSValueRef
JavaStaticInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
996 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
997 CYJavaClass
*table(internal
->table_
);
998 CYJavaEnv
jni(table
->value_
);
1000 auto name(CYPoolUTF8String(pool
, context
, property
));
1001 auto field(table
->static_
.find(name
));
1002 if (field
== table
->static_
.end())
1005 switch (field
->second
.primitive_
) {
1006 case CYJavaPrimitiveObject
:
1007 return CYCastJSValue(context
, jni
.GetStaticObjectField
<jobject
>(table
->value_
, field
->second
.field_
));
1008 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1009 case CYJavaPrimitive ## Type: \
1010 return CYJavaCastJSValue(context, jni.GetStatic ## Typ ## Field(table->value_, field->second.field_));
1011 CYJavaForEachPrimitive
1012 #undef CYJavaForEachPrimitive_
1013 default: _assert(false);
1017 static bool JavaStaticInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1018 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
1019 CYJavaClass
*table(internal
->table_
);
1020 CYJavaEnv
jni(table
->value_
);
1022 auto name(CYPoolUTF8String(pool
, context
, property
));
1023 auto field(table
->static_
.find(name
));
1024 if (field
== table
->static_
.end())
1027 switch (field
->second
.primitive_
) {
1028 case CYJavaPrimitiveObject
:
1029 jni
.SetStaticObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1030 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1031 case CYJavaPrimitive ## Type: \
1032 jni.SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1034 CYJavaForEachPrimitive
1035 #undef CYJavaForEachPrimitive_
1036 default: _assert(false);
1042 static void JavaStaticInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1043 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
1044 CYJavaClass
*table(internal
->table_
);
1045 for (const auto &field
: table
->static_
)
1046 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1049 static JSValueRef
JavaClass_getProperty_class(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1050 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
1051 return CYCastJSValue(context
, table
->value_
);
1054 static bool JavaInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1055 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1056 CYJavaClass
*table(internal
->table_
);
1058 auto name(CYPoolUTF8String(pool
, context
, property
));
1059 auto field(table
->instance_
.find(name
));
1060 if (field
== table
->instance_
.end())
1065 static JSValueRef
JavaInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1066 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1067 CYJavaEnv
jni(internal
->value_
);
1068 CYJavaClass
*table(internal
->table_
);
1070 auto name(CYPoolUTF8String(pool
, context
, property
));
1071 auto field(table
->instance_
.find(name
));
1072 if (field
== table
->instance_
.end())
1075 switch (field
->second
.primitive_
) {
1076 case CYJavaPrimitiveObject
:
1077 return CYCastJSValue(context
, jni
.GetObjectField
<jobject
>(internal
->value_
, field
->second
.field_
));
1078 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1079 case CYJavaPrimitive ## Type: \
1080 return CYJavaCastJSValue(context, jni.Get ## Typ ## Field(internal->value_, field->second.field_));
1081 CYJavaForEachPrimitive
1082 #undef CYJavaForEachPrimitive_
1083 default: _assert(false);
1087 static bool JavaInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1088 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1089 CYJavaEnv
jni(internal
->value_
);
1090 CYJavaClass
*table(internal
->table_
);
1092 auto name(CYPoolUTF8String(pool
, context
, property
));
1093 auto field(table
->instance_
.find(name
));
1094 if (field
== table
->instance_
.end())
1097 switch (field
->second
.primitive_
) {
1098 case CYJavaPrimitiveObject
:
1099 jni
.SetObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
));
1100 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1101 case CYJavaPrimitive ## Type: \
1102 jni.Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value)); \
1104 CYJavaForEachPrimitive
1105 #undef CYJavaForEachPrimitive_
1106 default: _assert(false);
1112 static void JavaInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
1113 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
1114 CYJavaClass
*table(internal
->table_
);
1115 for (const auto &field
: table
->instance_
)
1116 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
1119 static JSValueRef
JavaObject_getProperty_constructor(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1120 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
1121 CYJavaEnv
jni(internal
->value_
);
1122 return CYGetJavaClass(context
, jni
.GetObjectClass(internal
->value_
));
1125 static JSValueRef JavaClass_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1126 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
1127 return CYJavaStaticInterior::Make(context
, internal
->value_
, internal
);
1130 static JSValueRef JavaObject_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1131 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
1132 return CYJavaInterior::Make(context
, internal
->value_
, internal
->table_
);
1135 static JSValueRef
JavaClass_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1136 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(_this
)));
1137 CYJavaEnv
jni(internal
->value_
);
1138 auto Class$
(jni
.FindClass("java/lang/Class"));
1139 auto Class$
getCanonicalName(jni
.GetMethodID(Class$
, "getCanonicalName", "()Ljava/lang/String;"));
1140 return CYCastJSValue(context
, CYJSString(jni
.CallObjectMethod
<jstring
>(internal
->value_
, Class$getCanonicalName
)));
1143 static JSValueRef
JavaMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1144 std::ostringstream cyon
;
1145 return CYCastJSValue(context
, CYJSString(cyon
.str()));
1148 static JSValueRef
JavaStaticMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1149 std::ostringstream cyon
;
1150 return CYCastJSValue(context
, CYJSString(cyon
.str()));
1153 static JSValueRef
JavaArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1154 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
1155 CYJavaEnv
jni(internal
->value_
);
1156 if (JSStringIsEqual(property
, length_s
))
1157 return CYCastJSValue(context
, jni
.GetArrayLength(internal
->value_
));
1161 if (!CYGetOffset(pool
, context
, property
, offset
))
1164 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1165 return CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
));
1166 else switch (internal
->primitive_
) {
1167 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1168 case CYJavaPrimitive ## Type: { \
1169 j ## type element; \
1170 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1171 return CYJavaCastJSValue(context, element); \
1173 CYJavaForEachPrimitive
1174 #undef CYJavaForEachPrimitive_
1175 default: _assert(false);
1179 static bool JavaArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
1180 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
1181 CYJavaEnv
jni(internal
->value_
);
1185 if (!CYGetOffset(pool
, context
, property
, offset
))
1188 if (internal
->primitive_
== CYJavaPrimitiveObject
)
1189 jni
.SetObjectArrayElement(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
, CYCastJavaObject(jni
, context
, value
));
1190 else switch (internal
->primitive_
) {
1191 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1192 case CYJavaPrimitive ## Type: { \
1193 j ## type element; \
1194 jni.Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element); \
1195 return CYJavaCastJSValue(context, element); \
1197 CYJavaForEachPrimitive
1198 #undef CYJavaForEachPrimitive_
1199 default: _assert(false);
1205 static JSValueRef
JavaPackage_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
1206 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(_this
)));
1207 std::ostringstream name
;
1208 for (auto &package
: internal
->package_
)
1209 name
<< package
<< '.';
1211 return CYCastJSValue(context
, CYJSString(name
.str()));
1214 static bool CYJavaPackage_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1218 static JSValueRef
CYJavaPackage_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1219 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(object
)));
1220 CYJavaPackage::Path
package(internal
->package_
);
1223 const char *next(CYPoolCString(pool
, context
, property
));
1225 std::ostringstream name
;
1226 for (auto &package
: internal
->package_
)
1227 name
<< package
<< '/';
1230 JNIEnv
*jni(GetJNI(context
));
1231 if (auto _class
= jni
->FindClass(name
.str().c_str()))
1232 return CYGetJavaClass(context
, CYJavaLocal
<jclass
>(jni
, _class
));
1233 jni
->ExceptionClear();
1235 package
.push_back(next
);
1236 return CYJavaPackage::Make(context
, package
);
1239 static void Cycript_delete(JNIEnv
*env
, jclass api
, jlong jprotect
) { CYJavaTry
{
1243 static jobject
Cycript_handle(JNIEnv
*env
, jclass api
, jlong jprotect
, jstring property
, jobjectArray jarguments
) { CYJavaTry
{
1244 JSValueRef
function(CYGetProperty(context
, object
, CYJSString(CYJavaRef
<jstring
>(jni
, property
))));
1245 if (JSValueIsUndefined(context
, function
))
1248 size_t count(jarguments
== NULL
? 0 : jni
.GetArrayLength(jarguments
));
1249 JSValueRef arguments
[count
];
1250 for (size_t index(0); index
!= count
; ++index
) {
1251 arguments
[index
] = CYCastJSValue(context
, jni
.GetObjectArrayElement
<jobject
>(jarguments
, index
));
1254 return CYCastJavaObject(jni
, context
, CYCallAsFunction(context
, CYCastJSObject(context
, function
), object
, count
, arguments
));
1255 } CYJavaCatch(NULL
) }
1257 static JNINativeMethod Cycript_
[] = {
1258 {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete
},
1259 {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle
},
1262 JNIEnv
*GetJNI(JSContextRef context
) {
1263 static JavaVM
*jvm(NULL
);
1264 static JNIEnv
*jni(NULL
);
1268 jint
version(JNI_VERSION_1_4
);
1271 JavaVM
*jvms
[capacity
];
1273 _jnicall(JNI_GetCreatedJavaVMs(jvms
, capacity
, &size
));
1277 _jnicall(jvm
->GetEnv(reinterpret_cast<void **>(&jni
), version
));
1280 std::vector
<JavaVMOption
> options
;
1283 std::ostringstream option
;
1284 option
<< "-Djava.class.path=";
1285 option
<< CYPoolLibraryPath(pool
) << "/libcycript.jar";
1286 if (const char *classpath
= getenv("CLASSPATH"))
1287 option
<< ':' << classpath
;
1288 options
.push_back(JavaVMOption
{pool
.strdup(option
.str().c_str()), NULL
});
1291 JavaVMInitArgs args
;
1292 memset(&args
, 0, sizeof(args
));
1293 args
.version
= version
;
1294 args
.nOptions
= options
.size();
1295 args
.options
= options
.data();
1296 _jnicall(JNI_CreateJavaVM(&jvm
, reinterpret_cast<void **>(&jni
), &args
));
1299 auto Cycript$
(CYJavaEnv(jni
).FindClass("Cycript"));
1300 _envcall(jni
, RegisterNatives(Cycript$
, Cycript_
, sizeof(Cycript_
) / sizeof(Cycript_
[0])));
1305 static JSStaticValue JavaClass_staticValues
[3] = {
1306 {"class", &JavaClass_getProperty_class
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1307 {"$cyi", &JavaClass_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1308 {NULL
, NULL
, NULL
, 0}
1311 static JSStaticFunction JavaClass_staticFunctions
[2] = {
1312 {"toCYON", &JavaClass_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1316 static JSStaticValue JavaObject_staticValues
[3] = {
1317 {"constructor", &JavaObject_getProperty_constructor
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1318 {"$cyi", &JavaObject_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1319 {NULL
, NULL
, NULL
, 0}
1322 static JSStaticFunction JavaMethod_staticFunctions
[2] = {
1323 {"toCYON", &JavaMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1327 static JSStaticFunction JavaStaticMethod_staticFunctions
[2] = {
1328 {"toCYON", &JavaStaticMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1332 static JSStaticFunction JavaPackage_staticFunctions
[2] = {
1333 {"toCYON", &JavaPackage_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1337 void CYJava_Initialize() {
1338 Primitives_
.insert(std::make_pair("void", CYJavaPrimitiveVoid
));
1339 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1340 Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type));
1341 CYJavaForEachPrimitive
1342 #undef CYJavaForEachPrimitive_
1344 JSClassDefinition definition
;
1346 definition
= kJSClassDefinitionEmpty
;
1347 definition
.className
= "JavaClass";
1348 definition
.staticValues
= JavaClass_staticValues
;
1349 definition
.staticFunctions
= JavaClass_staticFunctions
;
1350 definition
.callAsConstructor
= &JavaClass_callAsConstructor
;
1351 definition
.finalize
= &CYFinalize
;
1352 CYJavaClass::Class_
= JSClassCreate(&definition
);
1354 definition
= kJSClassDefinitionEmpty
;
1355 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1356 definition
.className
= "JavaInterior";
1357 definition
.hasProperty
= &JavaInterior_hasProperty
;
1358 definition
.getProperty
= &JavaInterior_getProperty
;
1359 definition
.setProperty
= &JavaInterior_setProperty
;
1360 definition
.getPropertyNames
= &JavaInterior_getPropertyNames
;
1361 definition
.finalize
= &CYFinalize
;
1362 CYJavaInterior::Class_
= JSClassCreate(&definition
);
1364 definition
= kJSClassDefinitionEmpty
;
1365 definition
.className
= "JavaMethod";
1366 definition
.staticFunctions
= JavaMethod_staticFunctions
;
1367 definition
.callAsFunction
= &JavaMethod_callAsFunction
;
1368 definition
.finalize
= &CYFinalize
;
1369 CYJavaMethod::Class_
= JSClassCreate(&definition
);
1371 definition
= kJSClassDefinitionEmpty
;
1372 definition
.className
= "JavaStaticMethod";
1373 definition
.staticFunctions
= JavaStaticMethod_staticFunctions
;
1374 definition
.callAsFunction
= &JavaStaticMethod_callAsFunction
;
1375 definition
.finalize
= &CYFinalize
;
1376 CYJavaStaticMethod::Class_
= JSClassCreate(&definition
);
1378 definition
= kJSClassDefinitionEmpty
;
1379 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1380 definition
.className
= "JavaObject";
1381 definition
.staticValues
= JavaObject_staticValues
;
1382 definition
.finalize
= &CYFinalize
;
1383 CYJavaObject::Class_
= JSClassCreate(&definition
);
1385 definition
= kJSClassDefinitionEmpty
;
1386 definition
.className
= "JavaArray";
1387 definition
.getProperty
= &JavaArray_getProperty
;
1388 definition
.setProperty
= &JavaArray_setProperty
;
1389 definition
.finalize
= &CYFinalize
;
1390 CYJavaArray::Class_
= JSClassCreate(&definition
);
1392 definition
= kJSClassDefinitionEmpty
;
1393 definition
.className
= "JavaPackage";
1394 definition
.staticFunctions
= JavaPackage_staticFunctions
;
1395 definition
.hasProperty
= &CYJavaPackage_hasProperty
;
1396 definition
.getProperty
= &CYJavaPackage_getProperty
;
1397 definition
.finalize
= &CYFinalize
;
1398 CYJavaPackage::Class_
= JSClassCreate(&definition
);
1400 definition
= kJSClassDefinitionEmpty
;
1401 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1402 definition
.className
= "JavaStaticInterior";
1403 definition
.hasProperty
= &JavaStaticInterior_hasProperty
;
1404 definition
.getProperty
= &JavaStaticInterior_getProperty
;
1405 definition
.setProperty
= &JavaStaticInterior_setProperty
;
1406 definition
.getPropertyNames
= &JavaStaticInterior_getPropertyNames
;
1407 definition
.finalize
= &CYFinalize
;
1408 CYJavaStaticInterior::Class_
= JSClassCreate(&definition
);
1411 void CYJava_SetupContext(JSContextRef context
) {
1412 JSObjectRef
global(CYGetGlobalObject(context
));
1413 //JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
1414 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1415 JSObjectRef
all(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("all"))));
1416 //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
1418 JSObjectRef
Java(JSObjectMake(context
, NULL
, NULL
));
1419 CYSetProperty(context
, cycript
, CYJSString("Java"), Java
);
1421 JSObjectRef
Packages(CYJavaPackage::Make(context
, CYJavaPackage::Path()));
1422 CYSetProperty(context
, all
, CYJSString("Packages"), Packages
);
1424 for (auto name
: (const char *[]) {"java", "javax", "android", "com", "net", "org"}) {
1425 CYJSString
js(name
);
1426 CYSetProperty(context
, all
, js
, CYGetProperty(context
, Packages
, js
));
1430 static CYHook CYJavaHook
= {
1435 &CYJava_SetupContext
,
1439 CYRegisterHook
CYJava(&CYJavaHook
);