1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2015 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 CYJSError(context, CYCastJSValue(context, jni, _error)); \
53 #define _envcallv(jni, expr) do { \
55 if (jthrowable _error = jni->ExceptionOccurred()) { \
56 jni->ExceptionClear(); \
57 throw CYJSError(context, CYCastJSValue(context, jni, _error)); \
63 #define CYJavaCatch(value) \
64 catch (const CYException &error) { \
66 jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), error.PoolCString(pool)); \
71 // Android's jni.h seriously doesn't declare these :/
72 jint
JNI_CreateJavaVM(JavaVM
**, void **, void *);
73 jint
JNI_GetCreatedJavaVMs(JavaVM
**, jsize
, jsize
*);
76 static JSValueRef
CYCastJSValue(JSContextRef context
, JNIEnv
*jni
, jobject value
);
78 static void CYRegisterNatives(JSContextRef context
, JNIEnv
*jni
);
80 JNIEnv
*GetJNI(JSContextRef context
) {
81 static JavaVM
*jvm(NULL
);
82 static JNIEnv
*jni(NULL
);
86 jint
version(JNI_VERSION_1_4
);
89 JavaVM
*jvms
[capacity
];
91 _jnicall(JNI_GetCreatedJavaVMs(jvms
, capacity
, &size
));
95 _jnicall(jvm
->GetEnv(reinterpret_cast<void **>(&jni
), version
));
98 std::vector
<JavaVMOption
> options
;
101 std::ostringstream option
;
102 option
<< "-Djava.class.path=";
103 option
<< CYPoolLibraryPath(pool
) << "/libcycript.jar";
104 if (const char *classpath
= getenv("CLASSPATH"))
105 option
<< ':' << classpath
;
106 options
.push_back(JavaVMOption
{pool
.strdup(option
.str().c_str()), NULL
});
110 memset(&args
, 0, sizeof(args
));
111 args
.version
= version
;
112 args
.nOptions
= options
.size();
113 args
.options
= options
.data();
114 _jnicall(JNI_CreateJavaVM(&jvm
, reinterpret_cast<void **>(&jni
), &args
));
117 CYRegisterNatives(context
, jni
);
122 class CYJavaUTF8String
:
130 CYJavaUTF8String(JNIEnv
*jni
, jstring value
) :
134 _assert(value
!= NULL
);
135 size
= jni_
->GetStringUTFLength(value_
);
136 data
= jni_
->GetStringUTFChars(value_
, NULL
);
139 ~CYJavaUTF8String() {
141 jni_
->ReleaseStringUTFChars(value_
, data
);
144 CYJavaUTF8String(const CYJavaUTF8String
&) = delete;
146 CYJavaUTF8String(CYJavaUTF8String
&&rhs
) :
154 CYJavaUTF8String
CYCastUTF8String(JNIEnv
*jni
, jstring value
) {
155 return CYJavaUTF8String(jni
, value
);
158 JSStringRef
CYCopyJSString(JNIEnv
*jni
, jstring value
) {
159 return CYCopyJSString(CYCastUTF8String(jni
, value
));
162 template <typename Value_
>
163 struct CYJavaGlobal
{
173 CYJavaGlobal(JNIEnv
*jni
, Value_ value
) :
175 value_(static_cast<Value_
>(jni_
->NewGlobalRef(value
)))
179 CYJavaGlobal(const CYJavaGlobal
&value
) :
180 CYJavaGlobal(value
.jni_
, value
.value_
)
184 CYJavaGlobal(CYJavaGlobal
&&value
) :
193 jni_
->DeleteGlobalRef(value_
);
196 operator bool() const {
197 return value_
!= NULL
;
200 operator JNIEnv
*() const {
204 operator Value_() const {
209 template <typename Internal_
, typename Value_
>
213 CYJavaGlobal
<Value_
> value_
;
215 CYJavaValue(JNIEnv
*jni
, Value_ value
) :
220 CYJavaValue(const CYJavaValue
&) = delete;
223 #define CYJavaForEachPrimitive \
224 CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \
225 CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \
226 CYJavaForEachPrimitive_(C, c, Char, Character, char) \
227 CYJavaForEachPrimitive_(S, s, Short, Short, short) \
228 CYJavaForEachPrimitive_(I, i, Int, Integer, int) \
229 CYJavaForEachPrimitive_(J, j, Long, Long, long) \
230 CYJavaForEachPrimitive_(F, f, Float, Float, float) \
231 CYJavaForEachPrimitive_(D, d, Double, Double, double)
233 enum CYJavaPrimitive
: char {
234 CYJavaPrimitiveObject
,
236 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
237 CYJavaPrimitive ## Type,
238 CYJavaForEachPrimitive
239 #undef CYJavaForEachPrimitive_
242 template <typename Type_
>
243 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, Type_ value
) {
244 return CYCastJSValue(context
, value
);
247 static _finline JSValueRef
CYJavaCastJSValue(JSContextRef context
, jboolean value
) {
248 return CYCastJSValue(context
, static_cast<bool>(value
));
251 static std::map
<std::string
, CYJavaPrimitive
> Primitives_
;
253 static CYJavaPrimitive
CYJavaGetPrimitive(JSContextRef context
, JNIEnv
*jni
, jobject type
, jmethodID Class$get$$Name
) {
254 jstring
string(static_cast<jstring
>(_envcall(jni
, CallObjectMethod(type
, Class$get$$Name
))));
255 _assert(string
!= NULL
);
256 CYJavaUTF8String
name(jni
, string
);
257 auto primitive(Primitives_
.find(name
));
258 return primitive
!= Primitives_
.end() ? primitive
->second
: CYJavaPrimitiveObject
;
261 typedef std::vector
<CYJavaPrimitive
> CYJavaShorty
;
263 static CYJavaShorty
CYJavaGetShorty(JSContextRef context
, JNIEnv
*jni
, jobjectArray types
, jmethodID Class$get$$Name
) {
264 size_t count(_envcall(jni
, GetArrayLength(types
)));
265 CYJavaShorty
shorty(count
);
266 for (size_t index(0); index
!= count
; ++index
)
267 shorty
[index
] = CYJavaGetPrimitive(context
, jni
, _envcall(jni
, GetObjectArrayElement(types
, index
)), Class$get$$Name
);
273 CYJavaPrimitive primitive_
;
276 typedef std::map
<std::string
, CYJavaField
> CYJavaFieldMap
;
278 struct CYJavaSignature
{
280 CYJavaGlobal
<jobject
> reflected_
;
281 CYJavaPrimitive primitive_
;
282 CYJavaShorty shorty_
;
284 CYJavaSignature(JNIEnv
*jni
, jmethodID method
, jobject reflected
, CYJavaPrimitive primitive
, const CYJavaShorty
&shorty
) :
286 reflected_(jni
, reflected
),
287 primitive_(primitive
),
292 CYJavaSignature(unsigned count
) :
297 bool operator <(const CYJavaSignature
&rhs
) const {
298 return shorty_
.size() < rhs
.shorty_
.size();
302 typedef std::multiset
<CYJavaSignature
> CYJavaOverload
;
304 struct CYJavaMethod
:
305 CYPrivate
<CYJavaMethod
>
307 CYJavaOverload overload_
;
309 CYJavaMethod(const CYJavaOverload
&overload
) :
315 struct CYJavaStaticMethod
:
316 CYPrivate
<CYJavaStaticMethod
>
318 CYJavaOverload overload_
;
320 CYJavaStaticMethod(const CYJavaOverload
&overload
) :
327 CYJavaValue
<CYJavaClass
, jclass
>
331 CYJavaFieldMap static_
;
332 CYJavaFieldMap instance_
;
333 CYJavaOverload overload_
;
335 CYJavaClass(JNIEnv
*jni
, jclass value
, bool interface
) :
336 CYJavaValue(jni
, value
),
337 interface_(interface
)
342 static JSObjectRef
CYGetJavaClass(JSContextRef context
, JNIEnv
*jni
, jclass _class
);
344 struct CYJavaObject
:
345 CYJavaValue
<CYJavaObject
, jobject
>
349 CYJavaObject(JNIEnv
*jni
, jobject value
, CYJavaClass
*table
) :
350 CYJavaValue(jni
, value
),
355 JSValueRef
GetPrototype(JSContextRef context
) const;
358 struct CYJavaInterior
:
359 CYJavaValue
<CYJavaInterior
, jobject
>
363 CYJavaInterior(JNIEnv
*jni
, jobject value
, CYJavaClass
*table
) :
364 CYJavaValue(jni
, value
),
370 struct CYJavaStaticInterior
:
371 CYJavaValue
<CYJavaStaticInterior
, jobject
>
375 CYJavaStaticInterior(JNIEnv
*jni
, jobject value
, CYJavaClass
*table
) :
376 CYJavaValue(jni
, value
),
383 CYJavaValue
<CYJavaArray
, jarray
>
385 CYJavaPrimitive primitive_
;
387 CYJavaArray(JNIEnv
*jni
, jarray value
, CYJavaPrimitive primitive
) :
388 CYJavaValue(jni
, value
),
389 primitive_(primitive
)
393 JSValueRef
GetPrototype(JSContextRef context
) const;
396 struct CYJavaPackage
:
397 CYPrivate
<CYJavaPackage
>
399 typedef std::vector
<std::string
> Path
;
402 _finline
CYJavaPackage(const Path
&package
) :
408 JSValueRef
CYJavaObject::GetPrototype(JSContextRef context
) const {
410 return CYGetProperty(context
, CYGetJavaClass(context
, jni
, _envcall(jni
, GetObjectClass(value_
))), prototype_s
);
413 JSValueRef
CYJavaArray::GetPrototype(JSContextRef context
) const {
414 return CYGetCachedObject(context
, CYJSString("Array_prototype"));
417 static JSValueRef
CYCastJSValue(JSContextRef context
, JNIEnv
*jni
, jobject value
) {
419 return CYJSNull(context
);
421 jclass
_class(_envcall(jni
, GetObjectClass(value
)));
422 if (_envcall(jni
, IsSameObject(_class
, _envcall(jni
, FindClass("java/lang/String")))))
423 return CYCastJSValue(context
, CYJSString(jni
, static_cast<jstring
>(value
)));
425 jclass Class$
(_envcall(jni
, FindClass("java/lang/Class")));
426 jmethodID Class$
isArray(_envcall(jni
, GetMethodID(Class$
, "isArray", "()Z")));
427 if (_envcall(jni
, CallBooleanMethod(_class
, Class$isArray
))) {
428 jmethodID Class$
getComponentType(_envcall(jni
, GetMethodID(Class$
, "getComponentType", "()Ljava/lang/Class;")));
429 jclass
component(static_cast<jclass
>(_envcall(jni
, CallObjectMethod(_class
, Class$getComponentType
))));
430 jmethodID Class$
getName(_envcall(jni
, GetMethodID(Class$
, "getName", "()Ljava/lang/String;")));
431 return CYJavaArray::Make(context
, jni
, static_cast<jarray
>(value
), CYJavaGetPrimitive(context
, jni
, component
, Class$getName
));
434 jclass Wrapper$
(_envcall(jni
, FindClass("Cycript$Wrapper")));
435 if (_envcall(jni
, IsSameObject(_class
, Wrapper$
))) {
436 jmethodID Wrapper$
getProtect(_envcall(jni
, GetMethodID(Wrapper$
, "getProtect", "()J")));
437 auto &protect(*reinterpret_cast<CYProtect
*>(_envcall(jni
, CallLongMethod(value
, Wrapper$getProtect
))));
441 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(CYGetJavaClass(context
, jni
, _class
))));
442 return CYJavaObject::Make(context
, jni
, value
, table
);
445 static _finline JSObjectRef
CYCastJSObject(JSContextRef context
, JNIEnv
*jni
, jobject value
) {
446 return CYCastJSObject(context
, CYCastJSValue(context
, jni
, value
));
449 static jstring
CYCastJavaString(JNIEnv
*jni
, JSContextRef context
, CYUTF16String value
) {
450 return _envcall(jni
, NewString(value
.data
, value
.size
));
453 static jstring
CYCastJavaString(JNIEnv
*jni
, JSContextRef context
, JSStringRef value
) {
454 return CYCastJavaString(jni
, context
, CYCastUTF16String(value
));
457 #define CYCastJava$(T, Type, jtype, Cast) \
458 _disused static jobject CYCastJava ## Type(JNIEnv *jni, JSContextRef context, JSValueRef value) { \
459 jclass Type$(_envcall(jni, FindClass("java/lang/" #Type))); \
460 jmethodID Type$init$(_envcall(jni, GetMethodID(Type$, "<init>", "(" #T ")V"))); \
461 return _envcall(jni, NewObject(Type$, Type$init$, static_cast<jtype>(Cast(context, value)))); \
464 CYCastJava$
(Z
, Boolean
, jboolean
, CYCastBool
)
465 CYCastJava$
(B
, Byte
, jbyte
, CYCastDouble
)
466 CYCastJava$
(C
, Character
, jchar
, CYCastDouble
)
467 CYCastJava$
(S
, Short
, jshort
, CYCastDouble
)
468 CYCastJava$
(I
, Integer
, jint
, CYCastDouble
)
469 CYCastJava$
(J
, Long
, jlong
, CYCastDouble
)
470 CYCastJava$
(F
, Float
, jfloat
, CYCastDouble
)
471 CYCastJava$
(D
, Double
, jdouble
, CYCastDouble
)
473 static CYJavaClass
*CYGetJavaTable(JSContextRef context
, JSObjectRef object
) {
474 if (!JSValueIsObjectOfClass(context
, object
, CYJavaClass::Class_
))
476 return reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
));
479 static CYJavaObject
*CYGetJavaObject(JSContextRef context
, JSObjectRef object
) {
480 if (!JSValueIsObjectOfClass(context
, object
, CYJavaObject::Class_
))
482 return reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
));
485 static jobject
CYCastJavaObject(JNIEnv
*jni
, JSContextRef context
, JSObjectRef value
) {
486 if (CYJavaObject
*internal
= CYGetJavaObject(context
, value
))
487 return internal
->value_
;
489 jclass Wrapper$
(_envcall(jni
, FindClass("Cycript$Wrapper")));
490 jmethodID Wrapper$$init$
(_envcall(jni
, GetMethodID(Wrapper$
, "<init>", "(J)V")));
491 CYProtect
*protect(new CYProtect(context
, value
));
492 return _envcall(jni
, NewObject(Wrapper$
, Wrapper$$init$
, reinterpret_cast<jlong
>(protect
)));
495 static jobject
CYCastJavaObject(JNIEnv
*jni
, JSContextRef context
, JSValueRef value
) {
496 switch (JSValueGetType(context
, value
)) {
500 return CYCastJavaBoolean(jni
, context
, value
);
502 return CYCastJavaDouble(jni
, context
, value
);
504 return CYCastJavaString(jni
, context
, CYJSString(context
, value
));
506 return CYCastJavaObject(jni
, context
, CYCastJSObject(context
, value
));
508 case kJSTypeUndefined
:
509 // XXX: I am currently relying on this for dynamic proxy of void method
516 static JSObjectRef
CYGetJavaClass(JSContextRef context
, JNIEnv
*jni
, jclass value
) {
517 JSObjectRef
global(CYGetGlobalObject(context
));
518 JSObjectRef
cy(CYCastJSObject(context
, CYGetProperty(context
, global
, cy_s
)));
520 jclass Class$
(_envcall(jni
, FindClass("java/lang/Class")));
521 jmethodID Class$
getName(_envcall(jni
, GetMethodID(Class$
, "getName", "()Ljava/lang/String;")));
523 CYJSString
name(jni
, static_cast<jstring
>(_envcall(jni
, CallObjectMethod(value
, Class$getName
))));
524 JSValueRef
cached(CYGetProperty(context
, cy
, name
));
525 if (!JSValueIsUndefined(context
, cached
))
526 return CYCastJSObject(context
, cached
);
528 jmethodID Class$
isInterface(_envcall(jni
, GetMethodID(Class$
, "isInterface", "()Z")));
530 jmethodID Class$
getDeclaredConstructors(_envcall(jni
, GetMethodID(Class$
, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;")));
531 jmethodID Class$
getDeclaredFields(_envcall(jni
, GetMethodID(Class$
, "getDeclaredFields", "()[Ljava/lang/reflect/Field;")));
532 jmethodID Class$
getDeclaredMethods(_envcall(jni
, GetMethodID(Class$
, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;")));
534 jclass Constructor$
(_envcall(jni
, FindClass("java/lang/reflect/Constructor")));
535 //jmethodID Constructor$getModifiers(_envcall(jni, GetMethodID(Constructor$, "getModifiers", "()I")));
536 jmethodID Constructor$
getParameterTypes(_envcall(jni
, GetMethodID(Constructor$
, "getParameterTypes", "()[Ljava/lang/Class;")));
538 jclass Field$
(_envcall(jni
, FindClass("java/lang/reflect/Field")));
539 jmethodID Field$
getModifiers(_envcall(jni
, GetMethodID(Field$
, "getModifiers", "()I")));
540 jmethodID Field$
getName(_envcall(jni
, GetMethodID(Field$
, "getName", "()Ljava/lang/String;")));
541 jmethodID Field$
getType(_envcall(jni
, GetMethodID(Field$
, "getType", "()Ljava/lang/Class;")));
543 jclass Method$
(_envcall(jni
, FindClass("java/lang/reflect/Method")));
544 jmethodID Method$
getModifiers(_envcall(jni
, GetMethodID(Method$
, "getModifiers", "()I")));
545 jmethodID Method$
getName(_envcall(jni
, GetMethodID(Method$
, "getName", "()Ljava/lang/String;")));
546 jmethodID Method$
getParameterTypes(_envcall(jni
, GetMethodID(Method$
, "getParameterTypes", "()[Ljava/lang/Class;")));
547 jmethodID Method$
getReturnType(_envcall(jni
, GetMethodID(Method$
, "getReturnType", "()Ljava/lang/Class;")));
549 jclass Modifier$
(_envcall(jni
, FindClass("java/lang/reflect/Modifier")));
550 jmethodID Modifier$
isStatic(_envcall(jni
, GetStaticMethodID(Modifier$
, "isStatic", "(I)Z")));
552 bool interface(_envcall(jni
, CallBooleanMethod(value
, Class$isInterface
)));
553 CYJavaClass
*table(new CYJavaClass(jni
, value
, interface
));
555 for (jclass
prototype(value
); prototype
!= NULL
; prototype
= _envcall(jni
, GetSuperclass(prototype
))) {
556 jobjectArray
fields(static_cast<jobjectArray
>(_envcall(jni
, CallObjectMethod(prototype
, Class$getDeclaredFields
))));
558 for (jsize
i(0), e(_envcall(jni
, GetArrayLength(fields
))); i
!= e
; ++i
) {
559 jobject
field(_envcall(jni
, GetObjectArrayElement(fields
, e
- i
- 1)));
560 jint
modifiers(_envcall(jni
, CallIntMethod(field
, Field$getModifiers
)));
561 bool instance(!_envcall(jni
, CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
)));
562 auto &map(instance
? table
->instance_
: table
->static_
);
563 CYJavaUTF8String
name(jni
, static_cast<jstring
>(_envcall(jni
, CallObjectMethod(field
, Field$getName
))));
564 jfieldID
id(_envcall(jni
, FromReflectedField(field
)));
565 jobject
type(_envcall(jni
, CallObjectMethod(field
, Field$getType
)));
566 map
.insert(std::make_pair(std::string(name
), CYJavaField
{id
, CYJavaGetPrimitive(context
, jni
, type
, Class$getName
)}));
570 JSObjectRef
constructor(JSObjectMake(context
, CYJavaClass::Class_
, table
));
572 JSObjectRef
prototype(JSObjectMake(context
, NULL
, NULL
));
573 CYSetProperty(context
, constructor
, prototype_s
, prototype
, kJSPropertyAttributeDontEnum
);
575 jobjectArray
constructors(static_cast<jobjectArray
>(_envcall(jni
, CallObjectMethod(value
, Class$getDeclaredConstructors
))));
577 for (jsize
i(0), e(_envcall(jni
, GetArrayLength(constructors
))); i
!= e
; ++i
) {
578 jobject
constructor(_envcall(jni
, GetObjectArrayElement(constructors
, i
)));
579 jobjectArray
parameters(static_cast<jobjectArray
>(_envcall(jni
, CallObjectMethod(constructor
, Constructor$getParameterTypes
))));
580 CYJavaShorty
shorty(CYJavaGetShorty(context
, jni
, parameters
, Class$getName
));
581 jmethodID
id(_envcall(jni
, FromReflectedMethod(constructor
)));
582 table
->overload_
.insert(CYJavaSignature(jni
, id
, constructor
, CYJavaPrimitiveObject
, shorty
));
585 jobjectArray
methods(static_cast<jobjectArray
>(_envcall(jni
, CallObjectMethod(value
, Class$getDeclaredMethods
))));
587 std::map
<std::pair
<bool, std::string
>, CYJavaOverload
> entries
;
589 for (jsize
i(0), e(_envcall(jni
, GetArrayLength(methods
))); i
!= e
; ++i
) {
590 jobject
method(_envcall(jni
, GetObjectArrayElement(methods
, i
)));
591 jint
modifiers(_envcall(jni
, CallIntMethod(method
, Method$getModifiers
)));
592 bool instance(!_envcall(jni
, CallStaticBooleanMethod(Modifier$
, Modifier$isStatic
, modifiers
)));
593 CYJavaUTF8String
name(jni
, static_cast<jstring
>(_envcall(jni
, CallObjectMethod(method
, Method$getName
))));
594 jobjectArray
parameters(static_cast<jobjectArray
>(_envcall(jni
, CallObjectMethod(method
, Method$getParameterTypes
))));
595 CYJavaShorty
shorty(CYJavaGetShorty(context
, jni
, parameters
, Class$getName
));
596 jobject
type(_envcall(jni
, CallObjectMethod(method
, Method$getReturnType
)));
597 auto primitive(CYJavaGetPrimitive(context
, jni
, type
, Class$getName
));
598 jmethodID
id(_envcall(jni
, FromReflectedMethod(method
)));
599 entries
[std::make_pair(instance
, std::string(name
))].insert(CYJavaSignature(jni
, id
, method
, primitive
, shorty
));
602 for (const auto &entry
: entries
) {
603 bool instance(entry
.first
.first
);
604 CYJSString
name(entry
.first
.second
);
605 auto &overload(entry
.second
);
607 CYSetProperty(context
, prototype
, name
, CYJavaMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
609 CYSetProperty(context
, constructor
, name
, CYJavaStaticMethod::Make(context
, overload
), kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
);
612 // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name
613 // by not linking the prototypes until after we set the properties, we hide the parent property from this issue :(
615 if (jclass super
= _envcall(jni
, GetSuperclass(value
))) {
616 JSObjectRef
parent(CYGetJavaClass(context
, jni
, super
));
617 CYSetPrototype(context
, constructor
, parent
);
618 CYSetPrototype(context
, prototype
, CYGetProperty(context
, parent
, prototype_s
));
621 CYSetProperty(context
, cy
, name
, constructor
);
625 static void CYCastJavaNumeric(jvalue
&value
, CYJavaPrimitive primitive
, JSContextRef context
, JSValueRef argument
) {
627 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
628 case CYJavaPrimitive ## Type: \
629 value.t = static_cast<j ## type>(CYCastDouble(context, argument)); \
631 CYJavaForEachPrimitive
632 #undef CYJavaForEachPrimitive_
638 static bool CYCastJavaArguments(JNIEnv
*jni
, const CYJavaShorty
&shorty
, JSContextRef context
, const JSValueRef arguments
[], jvalue
*array
) {
639 for (size_t index(0); index
!= shorty
.size(); ++index
) {
640 JSValueRef
argument(arguments
[index
]);
641 JSType
type(JSValueGetType(context
, argument
));
642 jvalue
&value(array
[index
]);
644 switch (CYJavaPrimitive primitive
= shorty
[index
]) {
645 case CYJavaPrimitiveObject
:
646 value
.l
= CYCastJavaObject(jni
, context
, argument
);
649 case CYJavaPrimitiveBoolean
:
650 if (type
!= kJSTypeBoolean
)
652 value
.z
= CYCastBool(context
, argument
);
655 case CYJavaPrimitiveCharacter
:
656 if (type
== kJSTypeNumber
)
657 CYCastJavaNumeric(value
, primitive
, context
, argument
);
658 else if (type
!= kJSTypeString
)
661 CYJSString
string(context
, argument
);
662 if (JSStringGetLength(string
) != 1)
665 value
.c
= JSStringGetCharactersPtr(string
)[0];
669 case CYJavaPrimitiveByte
:
670 case CYJavaPrimitiveShort
:
671 case CYJavaPrimitiveInteger
:
672 case CYJavaPrimitiveLong
:
673 case CYJavaPrimitiveFloat
:
674 case CYJavaPrimitiveDouble
:
675 if (type
!= kJSTypeNumber
)
677 CYCastJavaNumeric(value
, primitive
, context
, argument
);
688 static JSValueRef
JavaMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
689 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
690 CYJavaObject
*self(CYGetJavaObject(context
, _this
));
691 JNIEnv
*jni(self
->value_
);
693 CYJavaSignature
bound(count
);
694 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
696 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
698 switch (overload
->primitive_
) {
699 case CYJavaPrimitiveObject
:
700 return CYCastJSValue(context
, jni
, _envcall(jni
, CallObjectMethodA(self
->value_
, overload
->method_
, array
)));
701 case CYJavaPrimitiveVoid
:
702 _envcallv(jni
, CallVoidMethodA(self
->value_
, overload
->method_
, array
));
703 return CYJSUndefined(context
);
704 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
705 case CYJavaPrimitive ## Type: \
706 return CYJavaCastJSValue(context, _envcall(jni, Call ## Typ ## MethodA(self->value_, overload->method_, array)));
707 CYJavaForEachPrimitive
708 #undef CYJavaForEachPrimitive_
709 default: _assert(false);
713 CYThrow("invalid method call");
716 static JSValueRef
JavaStaticMethod_callAsFunction(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
717 CYJavaMethod
*internal(reinterpret_cast<CYJavaMethod
*>(JSObjectGetPrivate(object
)));
718 CYJavaClass
*table(CYGetJavaTable(context
, _this
));
719 JNIEnv
*jni(table
->value_
);
721 CYJavaSignature
bound(count
);
722 for (auto overload(internal
->overload_
.lower_bound(bound
)), e(internal
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
724 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
726 switch (overload
->primitive_
) {
727 case CYJavaPrimitiveObject
:
728 return CYCastJSValue(context
, jni
, _envcall(jni
, CallStaticObjectMethodA(table
->value_
, overload
->method_
, array
)));
729 case CYJavaPrimitiveVoid
:
730 _envcallv(jni
, CallStaticVoidMethodA(table
->value_
, overload
->method_
, array
));
731 return CYJSUndefined(context
);
732 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
733 case CYJavaPrimitive ## Type: \
734 return CYJavaCastJSValue(context, _envcall(jni, CallStatic ## Typ ## MethodA(table->value_, overload->method_, array)));
735 CYJavaForEachPrimitive
736 #undef CYJavaForEachPrimitive_
737 default: _assert(false);
741 CYThrow("invalid method call");
744 static JSObjectRef
JavaClass_callAsConstructor(JSContextRef context
, JSObjectRef object
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
745 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
746 JNIEnv
*jni(table
->value_
);
747 jclass
_class(table
->value_
);
749 if (table
->interface_
&& count
== 1) {
750 JSObjectRef
target(CYCastJSObject(context
, arguments
[0]));
751 jclass Cycript$
(_envcall(jni
, FindClass("Cycript")));
752 jmethodID Cycript$
Make(_envcall(jni
, GetStaticMethodID(Cycript$
, "proxy", "(Ljava/lang/Class;J)Ljava/lang/Object;")));
753 CYProtect
*protect(new CYProtect(context
, target
));
754 return CYCastJSObject(context
, jni
, _envcall(jni
, CallObjectMethod(Cycript$
, Cycript$Make
, _class
, reinterpret_cast<jlong
>(protect
))));
757 CYJavaSignature
bound(count
);
758 for (auto overload(table
->overload_
.lower_bound(bound
)), e(table
->overload_
.upper_bound(bound
)); overload
!= e
; ++overload
) {
760 if (!CYCastJavaArguments(jni
, overload
->shorty_
, context
, arguments
, array
))
762 jobject
object(_envcall(jni
, NewObjectA(_class
, overload
->method_
, array
)));
763 return CYCastJSObject(context
, jni
, object
);
766 CYThrow("invalid constructor call");
769 static bool JavaStaticInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
770 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
771 CYJavaClass
*table(internal
->table_
);
773 auto name(CYPoolUTF8String(pool
, context
, property
));
774 auto field(table
->static_
.find(name
));
775 if (field
== table
->static_
.end())
780 static JSValueRef
JavaStaticInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
781 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
782 CYJavaClass
*table(internal
->table_
);
783 JNIEnv
*jni(table
->value_
);
785 auto name(CYPoolUTF8String(pool
, context
, property
));
786 auto field(table
->static_
.find(name
));
787 if (field
== table
->static_
.end())
790 switch (field
->second
.primitive_
) {
791 case CYJavaPrimitiveObject
:
792 return CYCastJSValue(context
, jni
, _envcall(jni
, GetStaticObjectField(table
->value_
, field
->second
.field_
)));
793 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
794 case CYJavaPrimitive ## Type: \
795 return CYJavaCastJSValue(context, _envcall(jni, GetStatic ## Typ ## Field(table->value_, field->second.field_)));
796 CYJavaForEachPrimitive
797 #undef CYJavaForEachPrimitive_
798 default: _assert(false);
802 static bool JavaStaticInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
803 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
804 CYJavaClass
*table(internal
->table_
);
805 JNIEnv
*jni(table
->value_
);
807 auto name(CYPoolUTF8String(pool
, context
, property
));
808 auto field(table
->static_
.find(name
));
809 if (field
== table
->static_
.end())
812 switch (field
->second
.primitive_
) {
813 case CYJavaPrimitiveObject
:
814 _envcallv(jni
, SetStaticObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
)));
815 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
816 case CYJavaPrimitive ## Type: \
817 _envcallv(jni, SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value))); \
819 CYJavaForEachPrimitive
820 #undef CYJavaForEachPrimitive_
821 default: _assert(false);
827 static void JavaStaticInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
828 CYJavaStaticInterior
*internal(reinterpret_cast<CYJavaStaticInterior
*>(JSObjectGetPrivate(object
)));
829 CYJavaClass
*table(internal
->table_
);
830 for (const auto &field
: table
->static_
)
831 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
834 static JSValueRef
JavaClass_getProperty_class(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
835 CYJavaClass
*table(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
836 return CYCastJSValue(context
, table
->value_
, table
->value_
);
839 static bool JavaInterior_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
840 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
841 CYJavaClass
*table(internal
->table_
);
843 auto name(CYPoolUTF8String(pool
, context
, property
));
844 auto field(table
->instance_
.find(name
));
845 if (field
== table
->instance_
.end())
850 static JSValueRef
JavaInterior_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
851 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
852 JNIEnv
*jni(internal
->value_
);
853 CYJavaClass
*table(internal
->table_
);
855 auto name(CYPoolUTF8String(pool
, context
, property
));
856 auto field(table
->instance_
.find(name
));
857 if (field
== table
->instance_
.end())
860 switch (field
->second
.primitive_
) {
861 case CYJavaPrimitiveObject
:
862 return CYCastJSValue(context
, jni
, _envcall(jni
, GetObjectField(internal
->value_
, field
->second
.field_
)));
863 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
864 case CYJavaPrimitive ## Type: \
865 return CYJavaCastJSValue(context, _envcall(jni, Get ## Typ ## Field(internal->value_, field->second.field_)));
866 CYJavaForEachPrimitive
867 #undef CYJavaForEachPrimitive_
868 default: _assert(false);
872 static bool JavaInterior_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
873 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
874 JNIEnv
*jni(internal
->value_
);
875 CYJavaClass
*table(internal
->table_
);
877 auto name(CYPoolUTF8String(pool
, context
, property
));
878 auto field(table
->instance_
.find(name
));
879 if (field
== table
->instance_
.end())
882 switch (field
->second
.primitive_
) {
883 case CYJavaPrimitiveObject
:
884 _envcallv(jni
, SetObjectField(table
->value_
, field
->second
.field_
, CYCastJavaObject(jni
, context
, value
)));
885 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
886 case CYJavaPrimitive ## Type: \
887 _envcallv(jni, Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value))); \
889 CYJavaForEachPrimitive
890 #undef CYJavaForEachPrimitive_
891 default: _assert(false);
897 static void JavaInterior_getPropertyNames(JSContextRef context
, JSObjectRef object
, JSPropertyNameAccumulatorRef names
) {
898 CYJavaInterior
*internal(reinterpret_cast<CYJavaInterior
*>(JSObjectGetPrivate(object
)));
899 CYJavaClass
*table(internal
->table_
);
900 for (const auto &field
: table
->instance_
)
901 JSPropertyNameAccumulatorAddName(names
, CYJSString(field
.first
));
904 static JSValueRef
JavaObject_getProperty_constructor(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
905 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
906 JNIEnv
*jni(internal
->value_
);
907 return CYGetJavaClass(context
, jni
, _envcall(jni
, GetObjectClass(internal
->value_
)));
910 static JSValueRef JavaClass_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
911 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(object
)));
912 JNIEnv
*jni(internal
->value_
);
913 return CYJavaStaticInterior::Make(context
, jni
, internal
->value_
, internal
);
916 static JSValueRef JavaObject_getProperty_$
cyi(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
917 CYJavaObject
*internal(reinterpret_cast<CYJavaObject
*>(JSObjectGetPrivate(object
)));
918 JNIEnv
*jni(internal
->value_
);
919 return CYJavaInterior::Make(context
, jni
, internal
->value_
, internal
->table_
);
922 static JSValueRef
JavaClass_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
923 CYJavaClass
*internal(reinterpret_cast<CYJavaClass
*>(JSObjectGetPrivate(_this
)));
924 JNIEnv
*jni(internal
->value_
);
925 jclass Class$
(_envcall(jni
, FindClass("java/lang/Class")));
926 jmethodID Class$
getCanonicalName(_envcall(jni
, GetMethodID(Class$
, "getCanonicalName", "()Ljava/lang/String;")));
927 return CYCastJSValue(context
, CYJSString(jni
, static_cast<jstring
>(_envcall(jni
, CallObjectMethod(internal
->value_
, Class$getCanonicalName
)))));
930 static JSValueRef
JavaMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
931 std::ostringstream cyon
;
932 return CYCastJSValue(context
, CYJSString(cyon
.str()));
935 static JSValueRef
JavaStaticMethod_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
936 std::ostringstream cyon
;
937 return CYCastJSValue(context
, CYJSString(cyon
.str()));
940 static JSValueRef
JavaArray_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
941 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
942 JNIEnv
*jni(internal
->value_
);
943 if (JSStringIsEqual(property
, length_s
))
944 return CYCastJSValue(context
, _envcall(jni
, GetArrayLength(internal
->value_
)));
948 if (!CYGetOffset(pool
, context
, property
, offset
))
951 if (internal
->primitive_
== CYJavaPrimitiveObject
)
952 return CYCastJSValue(context
, jni
, _envcall(jni
, GetObjectArrayElement(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
)));
953 else switch (internal
->primitive_
) {
954 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
955 case CYJavaPrimitive ## Type: { \
957 _envcallv(jni, Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element)); \
958 return CYJavaCastJSValue(context, element); \
960 CYJavaForEachPrimitive
961 #undef CYJavaForEachPrimitive_
962 default: _assert(false);
966 static bool JavaArray_setProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef value
, JSValueRef
*exception
) { CYTry
{
967 CYJavaArray
*internal(reinterpret_cast<CYJavaArray
*>(JSObjectGetPrivate(object
)));
968 JNIEnv
*jni(internal
->value_
);
972 if (!CYGetOffset(pool
, context
, property
, offset
))
975 if (internal
->primitive_
== CYJavaPrimitiveObject
)
976 _envcallv(jni
, SetObjectArrayElement(static_cast<jobjectArray
>(internal
->value_
.value_
), offset
, CYCastJavaObject(jni
, context
, value
)));
977 else switch (internal
->primitive_
) {
978 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
979 case CYJavaPrimitive ## Type: { \
981 _envcallv(jni, Get ## Typ ## ArrayRegion(static_cast<j ## type ## Array>(internal->value_.value_), offset, 1, &element)); \
982 return CYJavaCastJSValue(context, element); \
984 CYJavaForEachPrimitive
985 #undef CYJavaForEachPrimitive_
986 default: _assert(false);
992 static JSValueRef
JavaPackage_callAsFunction_toCYON(JSContextRef context
, JSObjectRef object
, JSObjectRef _this
, size_t count
, const JSValueRef arguments
[], JSValueRef
*exception
) { CYTry
{
993 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(_this
)));
994 std::ostringstream name
;
995 for (auto &package
: internal
->package_
)
996 name
<< package
<< '.';
998 return CYCastJSValue(context
, CYJSString(name
.str()));
1001 static bool CYJavaPackage_hasProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
) {
1005 static JSValueRef
CYJavaPackage_getProperty(JSContextRef context
, JSObjectRef object
, JSStringRef property
, JSValueRef
*exception
) { CYTry
{
1006 CYJavaPackage
*internal(reinterpret_cast<CYJavaPackage
*>(JSObjectGetPrivate(object
)));
1007 CYJavaPackage::Path
package(internal
->package_
);
1010 const char *next(CYPoolCString(pool
, context
, property
));
1012 std::ostringstream name
;
1013 for (auto &package
: internal
->package_
)
1014 name
<< package
<< '/';
1017 JNIEnv
*jni(GetJNI(context
));
1018 if (jclass _class
= jni
->FindClass(name
.str().c_str()))
1019 return CYGetJavaClass(context
, jni
, _class
);
1020 jni
->ExceptionClear();
1022 package
.push_back(next
);
1023 return CYJavaPackage::Make(context
, package
);
1026 static void Cycript_delete(JNIEnv
*jni
, jclass api
, jlong jprotect
) { CYJavaTry
{
1027 delete reinterpret_cast<CYProtect
*>(jprotect
);
1030 static jobject
Cycript_handle(JNIEnv
*jni
, jclass api
, jlong jprotect
, jstring property
, jobjectArray jarguments
) { CYJavaTry
{
1031 auto &protect(*reinterpret_cast<CYProtect
*>(jprotect
));
1032 JSContextRef
context(protect
);
1033 JSValueRef
function(CYGetProperty(context
, protect
, CYJSString(jni
, property
)));
1034 if (JSValueIsUndefined(context
, function
))
1037 size_t count(jarguments
== NULL
? 0 : _envcall(jni
, GetArrayLength(jarguments
)));
1038 JSValueRef arguments
[count
];
1039 for (size_t index(0); index
!= count
; ++index
)
1040 arguments
[index
] = CYCastJSValue(context
, jni
, _envcall(jni
, GetObjectArrayElement(jarguments
, index
)));
1042 return CYCastJavaObject(jni
, context
, CYCallAsFunction(context
, CYCastJSObject(context
, function
), protect
, count
, arguments
));
1043 } CYJavaCatch(NULL
) }
1045 static JNINativeMethod Cycript_
[] = {
1046 {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete
},
1047 {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle
},
1050 static void CYRegisterNatives(JSContextRef context
, JNIEnv
*jni
) {
1051 jclass Cycript$
(_envcall(jni
, FindClass("Cycript")));
1052 _envcall(jni
, RegisterNatives(Cycript$
, Cycript_
, sizeof(Cycript_
) / sizeof(Cycript_
[0])));
1055 static JSStaticValue JavaClass_staticValues
[3] = {
1056 {"class", &JavaClass_getProperty_class
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1057 {"$cyi", &JavaClass_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1058 {NULL
, NULL
, NULL
, 0}
1061 static JSStaticFunction JavaClass_staticFunctions
[2] = {
1062 {"toCYON", &JavaClass_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1066 static JSStaticValue JavaObject_staticValues
[3] = {
1067 {"constructor", &JavaObject_getProperty_constructor
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1068 {"$cyi", &JavaObject_getProperty_$cyi
, NULL
, kJSPropertyAttributeReadOnly
| kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1069 {NULL
, NULL
, NULL
, 0}
1072 static JSStaticFunction JavaMethod_staticFunctions
[2] = {
1073 {"toCYON", &JavaMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1077 static JSStaticFunction JavaStaticMethod_staticFunctions
[2] = {
1078 {"toCYON", &JavaStaticMethod_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1082 static JSStaticFunction JavaPackage_staticFunctions
[2] = {
1083 {"toCYON", &JavaPackage_callAsFunction_toCYON
, kJSPropertyAttributeDontEnum
| kJSPropertyAttributeDontDelete
},
1087 void CYJava_Initialize() {
1088 Primitives_
.insert(std::make_pair("void", CYJavaPrimitiveVoid
));
1089 #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \
1090 Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type));
1091 CYJavaForEachPrimitive
1092 #undef CYJavaForEachPrimitive_
1094 JSClassDefinition definition
;
1096 definition
= kJSClassDefinitionEmpty
;
1097 definition
.className
= "JavaClass";
1098 definition
.staticValues
= JavaClass_staticValues
;
1099 definition
.staticFunctions
= JavaClass_staticFunctions
;
1100 definition
.callAsConstructor
= &JavaClass_callAsConstructor
;
1101 definition
.finalize
= &CYFinalize
;
1102 CYJavaClass::Class_
= JSClassCreate(&definition
);
1104 definition
= kJSClassDefinitionEmpty
;
1105 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1106 definition
.className
= "JavaInterior";
1107 definition
.hasProperty
= &JavaInterior_hasProperty
;
1108 definition
.getProperty
= &JavaInterior_getProperty
;
1109 definition
.setProperty
= &JavaInterior_setProperty
;
1110 definition
.getPropertyNames
= &JavaInterior_getPropertyNames
;
1111 definition
.finalize
= &CYFinalize
;
1112 CYJavaInterior::Class_
= JSClassCreate(&definition
);
1114 definition
= kJSClassDefinitionEmpty
;
1115 definition
.className
= "JavaMethod";
1116 definition
.staticFunctions
= JavaMethod_staticFunctions
;
1117 definition
.callAsFunction
= &JavaMethod_callAsFunction
;
1118 definition
.finalize
= &CYFinalize
;
1119 CYJavaMethod::Class_
= JSClassCreate(&definition
);
1121 definition
= kJSClassDefinitionEmpty
;
1122 definition
.className
= "JavaStaticMethod";
1123 definition
.staticFunctions
= JavaStaticMethod_staticFunctions
;
1124 definition
.callAsFunction
= &JavaStaticMethod_callAsFunction
;
1125 definition
.finalize
= &CYFinalize
;
1126 CYJavaStaticMethod::Class_
= JSClassCreate(&definition
);
1128 definition
= kJSClassDefinitionEmpty
;
1129 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1130 definition
.className
= "JavaObject";
1131 definition
.staticValues
= JavaObject_staticValues
;
1132 definition
.finalize
= &CYFinalize
;
1133 CYJavaObject::Class_
= JSClassCreate(&definition
);
1135 definition
= kJSClassDefinitionEmpty
;
1136 definition
.className
= "JavaArray";
1137 definition
.getProperty
= &JavaArray_getProperty
;
1138 definition
.setProperty
= &JavaArray_setProperty
;
1139 definition
.finalize
= &CYFinalize
;
1140 CYJavaArray::Class_
= JSClassCreate(&definition
);
1142 definition
= kJSClassDefinitionEmpty
;
1143 definition
.className
= "JavaPackage";
1144 definition
.staticFunctions
= JavaPackage_staticFunctions
;
1145 definition
.hasProperty
= &CYJavaPackage_hasProperty
;
1146 definition
.getProperty
= &CYJavaPackage_getProperty
;
1147 definition
.finalize
= &CYFinalize
;
1148 CYJavaPackage::Class_
= JSClassCreate(&definition
);
1150 definition
= kJSClassDefinitionEmpty
;
1151 definition
.attributes
= kJSClassAttributeNoAutomaticPrototype
;
1152 definition
.className
= "JavaStaticInterior";
1153 definition
.hasProperty
= &JavaStaticInterior_hasProperty
;
1154 definition
.getProperty
= &JavaStaticInterior_getProperty
;
1155 definition
.setProperty
= &JavaStaticInterior_setProperty
;
1156 definition
.getPropertyNames
= &JavaStaticInterior_getPropertyNames
;
1157 definition
.finalize
= &CYFinalize
;
1158 CYJavaStaticInterior::Class_
= JSClassCreate(&definition
);
1161 void CYJava_SetupContext(JSContextRef context
) {
1162 JSObjectRef
global(CYGetGlobalObject(context
));
1163 //JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
1164 JSObjectRef
cycript(CYCastJSObject(context
, CYGetProperty(context
, global
, CYJSString("Cycript"))));
1165 JSObjectRef
all(CYCastJSObject(context
, CYGetProperty(context
, cycript
, CYJSString("all"))));
1166 //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls"))));
1168 JSObjectRef
Java(JSObjectMake(context
, NULL
, NULL
));
1169 CYSetProperty(context
, cycript
, CYJSString("Java"), Java
);
1171 JSObjectRef
Packages(CYJavaPackage::Make(context
, CYJavaPackage::Path()));
1172 CYSetProperty(context
, all
, CYJSString("Packages"), Packages
);
1174 for (auto name
: (const char *[]) {"java", "javax", "android", "com", "net", "org"}) {
1175 CYJSString
js(name
);
1176 CYSetProperty(context
, all
, js
, CYGetProperty(context
, Packages
, js
));
1180 static CYHook CYJavaHook
= {
1185 &CYJava_SetupContext
,
1189 CYRegisterHook
CYJava(&CYJavaHook
);