X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/dbf05bfd2ab79568fe706daa9112037192d4d9dd..c9c16dde5f5c8469610e727748875806a29906a6:/Java/Execute.cpp diff --git a/Java/Execute.cpp b/Java/Execute.cpp index c972562..fe6d5a9 100644 --- a/Java/Execute.cpp +++ b/Java/Execute.cpp @@ -30,6 +30,7 @@ #endif #include "cycript.hpp" +#include "Error.hpp" #include "Execute.hpp" #include "Internal.hpp" #include "JavaScript.hpp" @@ -45,7 +46,7 @@ __typeof__(jni->expr) _value(jni->expr); \ if (jthrowable _error = jni->ExceptionOccurred()) { \ jni->ExceptionClear(); \ - CYThrow("_envcall(%s): %p", #expr, _error); \ + throw CYJSError(context, CYCastJSValue(context, jni, _error)); \ } \ _value; }) @@ -53,17 +54,30 @@ _value; }) jni->expr; \ if (jthrowable _error = jni->ExceptionOccurred()) { \ jni->ExceptionClear(); \ - CYThrow("_envcall(%s): %p", #expr, _error); \ + throw CYJSError(context, CYCastJSValue(context, jni, _error)); \ } \ } while (false) +#define CYJavaTry \ + try +#define CYJavaCatch(value) \ + catch (const CYException &error) { \ + CYPool pool; \ + jni->ThrowNew(jni->FindClass("java/lang/RuntimeException"), error.PoolCString(pool)); \ + return value; \ + } + extern "C" { // Android's jni.h seriously doesn't declare these :/ jint JNI_CreateJavaVM(JavaVM **, void **, void *); jint JNI_GetCreatedJavaVMs(JavaVM **, jsize, jsize *); } -JNIEnv *GetJNI() { +static JSValueRef CYCastJSValue(JSContextRef context, JNIEnv *jni, jobject value); + +static void CYRegisterNatives(JSContextRef context, JNIEnv *jni); + +JNIEnv *GetJNI(JSContextRef context) { static JavaVM *jvm(NULL); static JNIEnv *jni(NULL); @@ -80,12 +94,28 @@ JNIEnv *GetJNI() { jvm = jvms[0]; _jnicall(jvm->GetEnv(reinterpret_cast(&jni), version)); } else { + CYPool pool; + std::vector options; + + { + std::ostringstream option; + option << "-Djava.class.path="; + option << CYPoolLibraryPath(pool) << "/libcycript.jar"; + if (const char *classpath = getenv("CLASSPATH")) + option << ':' << classpath; + options.push_back(JavaVMOption{pool.strdup(option.str().c_str()), NULL}); + } + JavaVMInitArgs args; memset(&args, 0, sizeof(args)); args.version = version; + args.nOptions = options.size(); + args.options = options.data(); _jnicall(JNI_CreateJavaVM(&jvm, reinterpret_cast(&jni), &args)); } + CYRegisterNatives(context, jni); + return jni; } @@ -101,6 +131,7 @@ class CYJavaUTF8String : jni_(jni), value_(value) { + _assert(value != NULL); size = jni_->GetStringUTFLength(value_); data = jni_->GetStringUTFChars(value_, NULL); } @@ -141,7 +172,7 @@ struct CYJavaGlobal { CYJavaGlobal(JNIEnv *jni, Value_ value) : jni_(jni), - value_(static_cast(_envcall(jni_, NewGlobalRef(value)))) + value_(static_cast(jni_->NewGlobalRef(value))) { } @@ -159,7 +190,7 @@ struct CYJavaGlobal { ~CYJavaGlobal() { if (value_ != NULL) - _envcallv(jni_, DeleteGlobalRef(value_)); + jni_->DeleteGlobalRef(value_); } operator bool() const { @@ -190,19 +221,19 @@ struct CYJavaValue : }; #define CYJavaForEachPrimitive \ - CYJavaForEachPrimitive_(Z, Boolean, Boolean) \ - CYJavaForEachPrimitive_(B, Byte, Byte) \ - CYJavaForEachPrimitive_(C, Char, Character) \ - CYJavaForEachPrimitive_(S, Short, Short) \ - CYJavaForEachPrimitive_(I, Int, Integer) \ - CYJavaForEachPrimitive_(J, Long, Long) \ - CYJavaForEachPrimitive_(F, Float, Float) \ - CYJavaForEachPrimitive_(D, Double, Double) + CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \ + CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \ + CYJavaForEachPrimitive_(C, c, Char, Character, char) \ + CYJavaForEachPrimitive_(S, s, Short, Short, short) \ + CYJavaForEachPrimitive_(I, i, Int, Integer, int) \ + CYJavaForEachPrimitive_(J, j, Long, Long, long) \ + CYJavaForEachPrimitive_(F, f, Float, Float, float) \ + CYJavaForEachPrimitive_(D, d, Double, Double, double) enum CYJavaPrimitive : char { CYJavaPrimitiveObject, CYJavaPrimitiveVoid, -#define CYJavaForEachPrimitive_(T, Typ, Type) \ +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ CYJavaPrimitive ## Type, CYJavaForEachPrimitive #undef CYJavaForEachPrimitive_ @@ -219,19 +250,21 @@ static _finline JSValueRef CYJavaCastJSValue(JSContextRef context, jboolean valu static std::map Primitives_; -static CYJavaPrimitive CYJavaGetPrimitive(JNIEnv *jni, jobject type, jmethodID Class$get$$Name) { - CYJavaUTF8String name(jni, static_cast(_envcall(jni, CallObjectMethod(type, Class$get$$Name)))); +static CYJavaPrimitive CYJavaGetPrimitive(JSContextRef context, JNIEnv *jni, jobject type, jmethodID Class$get$$Name) { + jstring string(static_cast(_envcall(jni, CallObjectMethod(type, Class$get$$Name)))); + _assert(string != NULL); + CYJavaUTF8String name(jni, string); auto primitive(Primitives_.find(name)); return primitive != Primitives_.end() ? primitive->second : CYJavaPrimitiveObject; } typedef std::vector CYJavaShorty; -static CYJavaShorty CYJavaGetShorty(JNIEnv *jni, jobjectArray types, jmethodID Class$get$$Name) { +static CYJavaShorty CYJavaGetShorty(JSContextRef context, JNIEnv *jni, jobjectArray types, jmethodID Class$get$$Name) { size_t count(_envcall(jni, GetArrayLength(types))); CYJavaShorty shorty(count); for (size_t index(0); index != count; ++index) - shorty[index] = CYJavaGetPrimitive(jni, _envcall(jni, GetObjectArrayElement(types, index)), Class$get$$Name); + shorty[index] = CYJavaGetPrimitive(context, jni, _envcall(jni, GetObjectArrayElement(types, index)), Class$get$$Name); return shorty; } @@ -243,24 +276,26 @@ struct CYJavaField { typedef std::map CYJavaFieldMap; struct CYJavaSignature { - CYJavaGlobal method; - CYJavaPrimitive primitive; - CYJavaShorty shorty; - - CYJavaSignature(JNIEnv *jni, jobject method, CYJavaPrimitive primitive, const CYJavaShorty &shorty) : - method(jni, method), - primitive(primitive), - shorty(shorty) + jmethodID method_; + CYJavaGlobal reflected_; + CYJavaPrimitive primitive_; + CYJavaShorty shorty_; + + CYJavaSignature(JNIEnv *jni, jmethodID method, jobject reflected, CYJavaPrimitive primitive, const CYJavaShorty &shorty) : + method_(method), + reflected_(jni, reflected), + primitive_(primitive), + shorty_(shorty) { } CYJavaSignature(unsigned count) : - shorty(count) + shorty_(count) { } bool operator <(const CYJavaSignature &rhs) const { - return shorty.size() < rhs.shorty.size(); + return shorty_.size() < rhs.shorty_.size(); } }; @@ -269,12 +304,20 @@ typedef std::multiset CYJavaOverload; struct CYJavaMethod : CYPrivate { - JNIEnv *jni_; CYJavaOverload overload_; - // XXX: figure out move constructors on Apple's crappy toolchain - CYJavaMethod(JNIEnv *jni, const CYJavaOverload &overload) : - jni_(jni), + CYJavaMethod(const CYJavaOverload &overload) : + overload_(overload) + { + } +}; + +struct CYJavaStaticMethod : + CYPrivate +{ + CYJavaOverload overload_; + + CYJavaStaticMethod(const CYJavaOverload &overload) : overload_(overload) { } @@ -283,21 +326,67 @@ struct CYJavaMethod : struct CYJavaClass : CYJavaValue { + bool interface_; + CYJavaFieldMap static_; CYJavaFieldMap instance_; CYJavaOverload overload_; - CYJavaClass(JNIEnv *jni, jclass value) : - CYJavaValue(jni, value) + CYJavaClass(JNIEnv *jni, jclass value, bool interface) : + CYJavaValue(jni, value), + interface_(interface) { } }; +static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass _class); + struct CYJavaObject : CYJavaValue { - CYJavaObject(JNIEnv *jni, jobject value) : - CYJavaValue(jni, value) + CYJavaClass *table_; + + CYJavaObject(JNIEnv *jni, jobject value, CYJavaClass *table) : + CYJavaValue(jni, value), + table_(table) + { + } + + JSValueRef GetPrototype(JSContextRef context) const; +}; + +struct CYJavaInterior : + CYJavaValue +{ + CYJavaClass *table_; + + CYJavaInterior(JNIEnv *jni, jobject value, CYJavaClass *table) : + CYJavaValue(jni, value), + table_(table) + { + } +}; + +struct CYJavaStaticInterior : + CYJavaValue +{ + CYJavaClass *table_; + + CYJavaStaticInterior(JNIEnv *jni, jobject value, CYJavaClass *table) : + CYJavaValue(jni, value), + table_(table) + { + } +}; + +struct CYJavaArray : + CYJavaValue +{ + CYJavaPrimitive primitive_; + + CYJavaArray(JNIEnv *jni, jarray value, CYJavaPrimitive primitive) : + CYJavaValue(jni, value), + primitive_(primitive) { } @@ -316,26 +405,53 @@ struct CYJavaPackage : } }; -static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass _class); - JSValueRef CYJavaObject::GetPrototype(JSContextRef context) const { JNIEnv *jni(value_); return CYGetProperty(context, CYGetJavaClass(context, jni, _envcall(jni, GetObjectClass(value_))), prototype_s); } +JSValueRef CYJavaArray::GetPrototype(JSContextRef context) const { + return CYGetCachedObject(context, CYJSString("Array_prototype")); +} + static JSValueRef CYCastJSValue(JSContextRef context, JNIEnv *jni, jobject value) { if (value == NULL) return CYJSNull(context); - return CYJavaObject::Make(context, jni, value); + jclass _class(_envcall(jni, GetObjectClass(value))); + if (_envcall(jni, IsSameObject(_class, _envcall(jni, FindClass("java/lang/String"))))) + return CYCastJSValue(context, CYJSString(jni, static_cast(value))); + + jclass Class$(_envcall(jni, FindClass("java/lang/Class"))); + jmethodID Class$isArray(_envcall(jni, GetMethodID(Class$, "isArray", "()Z"))); + if (_envcall(jni, CallBooleanMethod(_class, Class$isArray))) { + jmethodID Class$getComponentType(_envcall(jni, GetMethodID(Class$, "getComponentType", "()Ljava/lang/Class;"))); + jclass component(static_cast(_envcall(jni, CallObjectMethod(_class, Class$getComponentType)))); + jmethodID Class$getName(_envcall(jni, GetMethodID(Class$, "getName", "()Ljava/lang/String;"))); + return CYJavaArray::Make(context, jni, static_cast(value), CYJavaGetPrimitive(context, jni, component, Class$getName)); + } + + jclass Wrapper$(_envcall(jni, FindClass("Cycript$Wrapper"))); + if (_envcall(jni, IsSameObject(_class, Wrapper$))) { + jmethodID Wrapper$getProtect(_envcall(jni, GetMethodID(Wrapper$, "getProtect", "()J"))); + auto &protect(*reinterpret_cast(_envcall(jni, CallLongMethod(value, Wrapper$getProtect)))); + return protect; + } + + CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(CYGetJavaClass(context, jni, _class)))); + return CYJavaObject::Make(context, jni, value, table); } -static jstring CYCastJavaString(JNIEnv *jni, CYUTF16String value) { +static _finline JSObjectRef CYCastJSObject(JSContextRef context, JNIEnv *jni, jobject value) { + return CYCastJSObject(context, CYCastJSValue(context, jni, value)); +} + +static jstring CYCastJavaString(JNIEnv *jni, JSContextRef context, CYUTF16String value) { return _envcall(jni, NewString(value.data, value.size)); } -static jstring CYCastJavaString(JNIEnv *jni, JSStringRef value) { - return CYCastJavaString(jni, CYCastUTF16String(value)); +static jstring CYCastJavaString(JNIEnv *jni, JSContextRef context, JSStringRef value) { + return CYCastJavaString(jni, context, CYCastUTF16String(value)); } #define CYCastJava$(T, Type, jtype, Cast) \ @@ -354,14 +470,26 @@ CYCastJava$(J, Long, jlong, CYCastDouble) CYCastJava$(F, Float, jfloat, CYCastDouble) CYCastJava$(D, Double, jdouble, CYCastDouble) +static CYJavaClass *CYGetJavaTable(JSContextRef context, JSObjectRef object) { + if (!JSValueIsObjectOfClass(context, object, CYJavaClass::Class_)) + return NULL; + return reinterpret_cast(JSObjectGetPrivate(object)); +} + +static CYJavaObject *CYGetJavaObject(JSContextRef context, JSObjectRef object) { + if (!JSValueIsObjectOfClass(context, object, CYJavaObject::Class_)) + return NULL; + return reinterpret_cast(JSObjectGetPrivate(object)); +} + static jobject CYCastJavaObject(JNIEnv *jni, JSContextRef context, JSObjectRef value) { - JSObjectRef object(CYCastJSObject(context, value)); - if (JSValueIsObjectOfClass(context, value, CYJavaObject::Class_)) { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); + if (CYJavaObject *internal = CYGetJavaObject(context, value)) return internal->value_; - } - _assert(false); + jclass Wrapper$(_envcall(jni, FindClass("Cycript$Wrapper"))); + jmethodID Wrapper$$init$(_envcall(jni, GetMethodID(Wrapper$, "", "(J)V"))); + CYProtect *protect(new CYProtect(context, value)); + return _envcall(jni, NewObject(Wrapper$, Wrapper$$init$, reinterpret_cast(protect))); } static jobject CYCastJavaObject(JNIEnv *jni, JSContextRef context, JSValueRef value) { @@ -373,33 +501,32 @@ static jobject CYCastJavaObject(JNIEnv *jni, JSContextRef context, JSValueRef va case kJSTypeNumber: return CYCastJavaDouble(jni, context, value); case kJSTypeString: - return CYCastJavaString(jni, CYJSString(context, value)); + return CYCastJavaString(jni, context, CYJSString(context, value)); case kJSTypeObject: return CYCastJavaObject(jni, context, CYCastJSObject(context, value)); case kJSTypeUndefined: + // XXX: I am currently relying on this for dynamic proxy of void method + return NULL; default: _assert(false); } } -CYJavaClass *CYGetJavaTable(JSContextRef context, JNIEnv *jni, jobject object) { - // XXX: this implementation is absolutely unacceptable :/ - return reinterpret_cast(JSObjectGetPrivate(CYGetJavaClass(context, jni, _envcall(jni, GetObjectClass(object))))); -} - static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass value) { JSObjectRef global(CYGetGlobalObject(context)); JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); jclass Class$(_envcall(jni, FindClass("java/lang/Class"))); - jmethodID Class$getCanonicalName(_envcall(jni, GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;"))); + jmethodID Class$getName(_envcall(jni, GetMethodID(Class$, "getName", "()Ljava/lang/String;"))); - CYJSString name(jni, static_cast(_envcall(jni, CallObjectMethod(value, Class$getCanonicalName)))); + CYJSString name(jni, static_cast(_envcall(jni, CallObjectMethod(value, Class$getName)))); JSValueRef cached(CYGetProperty(context, cy, name)); if (!JSValueIsUndefined(context, cached)) return CYCastJSObject(context, cached); + jmethodID Class$isInterface(_envcall(jni, GetMethodID(Class$, "isInterface", "()Z"))); + jmethodID Class$getDeclaredConstructors(_envcall(jni, GetMethodID(Class$, "getDeclaredConstructors", "()[Ljava/lang/reflect/Constructor;"))); jmethodID Class$getDeclaredFields(_envcall(jni, GetMethodID(Class$, "getDeclaredFields", "()[Ljava/lang/reflect/Field;"))); jmethodID Class$getDeclaredMethods(_envcall(jni, GetMethodID(Class$, "getDeclaredMethods", "()[Ljava/lang/reflect/Method;"))); @@ -422,7 +549,8 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu jclass Modifier$(_envcall(jni, FindClass("java/lang/reflect/Modifier"))); jmethodID Modifier$isStatic(_envcall(jni, GetStaticMethodID(Modifier$, "isStatic", "(I)Z"))); - CYJavaClass *table(new CYJavaClass(jni, value)); + bool interface(_envcall(jni, CallBooleanMethod(value, Class$isInterface))); + CYJavaClass *table(new CYJavaClass(jni, value, interface)); for (jclass prototype(value); prototype != NULL; prototype = _envcall(jni, GetSuperclass(prototype))) { jobjectArray fields(static_cast(_envcall(jni, CallObjectMethod(prototype, Class$getDeclaredFields)))); @@ -435,13 +563,11 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu CYJavaUTF8String name(jni, static_cast(_envcall(jni, CallObjectMethod(field, Field$getName)))); jfieldID id(_envcall(jni, FromReflectedField(field))); jobject type(_envcall(jni, CallObjectMethod(field, Field$getType))); - map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(jni, type, Class$getCanonicalName)})); + map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(context, jni, type, Class$getName)})); } } JSObjectRef constructor(JSObjectMake(context, CYJavaClass::Class_, table)); - JSObjectRef indirect(JSObjectMake(context, NULL, NULL)); - CYSetPrototype(context, constructor, indirect); JSObjectRef prototype(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, constructor, prototype_s, prototype, kJSPropertyAttributeDontEnum); @@ -451,8 +577,9 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu for (jsize i(0), e(_envcall(jni, GetArrayLength(constructors))); i != e; ++i) { jobject constructor(_envcall(jni, GetObjectArrayElement(constructors, i))); jobjectArray parameters(static_cast(_envcall(jni, CallObjectMethod(constructor, Constructor$getParameterTypes)))); - CYJavaShorty shorty(CYJavaGetShorty(jni, parameters, Class$getCanonicalName)); - table->overload_.insert(CYJavaSignature(jni, constructor, CYJavaPrimitiveObject, shorty)); + CYJavaShorty shorty(CYJavaGetShorty(context, jni, parameters, Class$getName)); + jmethodID id(_envcall(jni, FromReflectedMethod(constructor))); + table->overload_.insert(CYJavaSignature(jni, id, constructor, CYJavaPrimitiveObject, shorty)); } jobjectArray methods(static_cast(_envcall(jni, CallObjectMethod(value, Class$getDeclaredMethods)))); @@ -465,19 +592,21 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu bool instance(!_envcall(jni, CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers))); CYJavaUTF8String name(jni, static_cast(_envcall(jni, CallObjectMethod(method, Method$getName)))); jobjectArray parameters(static_cast(_envcall(jni, CallObjectMethod(method, Method$getParameterTypes)))); - CYJavaShorty shorty(CYJavaGetShorty(jni, parameters, Class$getCanonicalName)); + CYJavaShorty shorty(CYJavaGetShorty(context, jni, parameters, Class$getName)); jobject type(_envcall(jni, CallObjectMethod(method, Method$getReturnType))); - auto primitive(CYJavaGetPrimitive(jni, type, Class$getCanonicalName)); - entries[std::make_pair(instance, std::string(name))].insert(CYJavaSignature(jni, method, primitive, shorty)); + auto primitive(CYJavaGetPrimitive(context, jni, type, Class$getName)); + jmethodID id(_envcall(jni, FromReflectedMethod(method))); + entries[std::make_pair(instance, std::string(name))].insert(CYJavaSignature(jni, id, method, primitive, shorty)); } for (const auto &entry : entries) { bool instance(entry.first.first); CYJSString name(entry.first.second); auto &overload(entry.second); - auto target(instance ? prototype : indirect); - JSValueRef wrapper(CYJavaMethod::Make(context, jni, overload)); - CYSetProperty(context, target, name, wrapper, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + if (instance) + CYSetProperty(context, prototype, name, CYJavaMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + else + CYSetProperty(context, constructor, name, CYJavaStaticMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); } // XXX: for some reason kJSPropertyAttributeDontEnum doesn't work if there's already a property with the same name @@ -485,7 +614,7 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu if (jclass super = _envcall(jni, GetSuperclass(value))) { JSObjectRef parent(CYGetJavaClass(context, jni, super)); - CYSetPrototype(context, indirect, CYGetPrototype(context, parent)); + CYSetPrototype(context, constructor, parent); CYSetPrototype(context, prototype, CYGetProperty(context, parent, prototype_s)); } @@ -493,45 +622,120 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, JNIEnv *jni, jclass valu return constructor; } -static jobjectArray CYCastJavaArguments(JNIEnv *jni, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jclass Object$) { - jobjectArray array(_envcall(jni, NewObjectArray(shorty.size(), Object$, NULL))); - for (size_t index(0); index != shorty.size(); ++index) { - jobject argument; - switch (shorty[index]) { - case CYJavaPrimitiveObject: - argument = CYCastJavaObject(jni, context, arguments[index]); - break; -#define CYJavaForEachPrimitive_(T, Typ, Type) \ - case CYJavaPrimitive ## Type: \ - argument = CYCastJava ## Type(jni, context, arguments[index]); \ - break; +static void CYCastJavaNumeric(jvalue &value, CYJavaPrimitive primitive, JSContextRef context, JSValueRef argument) { + switch (primitive) { +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + case CYJavaPrimitive ## Type: \ + value.t = static_cast(CYCastDouble(context, argument)); \ + break; CYJavaForEachPrimitive #undef CYJavaForEachPrimitive_ + default: + _assert(false); + } +} + +static bool CYCastJavaArguments(JNIEnv *jni, const CYJavaShorty &shorty, JSContextRef context, const JSValueRef arguments[], jvalue *array) { + for (size_t index(0); index != shorty.size(); ++index) { + JSValueRef argument(arguments[index]); + JSType type(JSValueGetType(context, argument)); + jvalue &value(array[index]); + + switch (CYJavaPrimitive primitive = shorty[index]) { + case CYJavaPrimitiveObject: + value.l = CYCastJavaObject(jni, context, argument); + break; + + case CYJavaPrimitiveBoolean: + if (type != kJSTypeBoolean) + return false; + value.z = CYCastBool(context, argument); + break; + + case CYJavaPrimitiveCharacter: + if (type == kJSTypeNumber) + CYCastJavaNumeric(value, primitive, context, argument); + else if (type != kJSTypeString) + return false; + else { + CYJSString string(context, argument); + if (JSStringGetLength(string) != 1) + return false; + else + value.c = JSStringGetCharactersPtr(string)[0]; + } + break; + + case CYJavaPrimitiveByte: + case CYJavaPrimitiveShort: + case CYJavaPrimitiveInteger: + case CYJavaPrimitiveLong: + case CYJavaPrimitiveFloat: + case CYJavaPrimitiveDouble: + if (type != kJSTypeNumber) + return false; + CYCastJavaNumeric(value, primitive, context, argument); + break; + default: _assert(false); } - _envcallv(jni, SetObjectArrayElement(array, index, argument)); } - return array; + return true; } static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYJavaMethod *internal(reinterpret_cast(JSObjectGetPrivate(object))); - JNIEnv *jni(internal->jni_); + CYJavaObject *self(CYGetJavaObject(context, _this)); + JNIEnv *jni(self->value_); - jclass Object$(_envcall(jni, FindClass("java/lang/Object"))); + CYJavaSignature bound(count); + for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { + jvalue array[count]; + if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array)) + continue; + switch (overload->primitive_) { + case CYJavaPrimitiveObject: + return CYCastJSValue(context, jni, _envcall(jni, CallObjectMethodA(self->value_, overload->method_, array))); + case CYJavaPrimitiveVoid: + _envcallv(jni, CallVoidMethodA(self->value_, overload->method_, array)); + return CYJSUndefined(context); +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + case CYJavaPrimitive ## Type: \ + return CYJavaCastJSValue(context, _envcall(jni, Call ## Typ ## MethodA(self->value_, overload->method_, array))); +CYJavaForEachPrimitive +#undef CYJavaForEachPrimitive_ + default: _assert(false); + } + } - jclass Method$(_envcall(jni, FindClass("java/lang/reflect/Method"))); - jmethodID Method$invoke(_envcall(jni, GetMethodID(Method$, "invoke", "(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;"))); + CYThrow("invalid method call"); +} CYCatch(NULL) } - jobject self(CYCastJavaObject(jni, context, _this)); +static JSValueRef JavaStaticMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYJavaMethod *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(CYGetJavaTable(context, _this)); + JNIEnv *jni(table->value_); CYJavaSignature bound(count); for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { - jobjectArray array(CYCastJavaArguments(jni, overload->shorty, context, arguments, Object$)); - jobject object(_envcall(jni, CallObjectMethod(overload->method, Method$invoke, self, array))); - return CYCastJSValue(context, jni, object); + jvalue array[count]; + if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array)) + continue; + switch (overload->primitive_) { + case CYJavaPrimitiveObject: + return CYCastJSValue(context, jni, _envcall(jni, CallStaticObjectMethodA(table->value_, overload->method_, array))); + case CYJavaPrimitiveVoid: + _envcallv(jni, CallStaticVoidMethodA(table->value_, overload->method_, array)); + return CYJSUndefined(context); +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + case CYJavaPrimitive ## Type: \ + return CYJavaCastJSValue(context, _envcall(jni, CallStatic ## Typ ## MethodA(table->value_, overload->method_, array))); +CYJavaForEachPrimitive +#undef CYJavaForEachPrimitive_ + default: _assert(false); + } } CYThrow("invalid method call"); @@ -540,24 +744,31 @@ static JSValueRef JavaMethod_callAsFunction(JSContextRef context, JSObjectRef ob static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); JNIEnv *jni(table->value_); - - jclass Object$(_envcall(jni, FindClass("java/lang/Object"))); - - jclass Constructor$(_envcall(jni, FindClass("java/lang/reflect/Constructor"))); - jmethodID Constructor$newInstance(_envcall(jni, GetMethodID(Constructor$, "newInstance", "([Ljava/lang/Object;)Ljava/lang/Object;"))); + jclass _class(table->value_); + + if (table->interface_ && count == 1) { + JSObjectRef target(CYCastJSObject(context, arguments[0])); + jclass Cycript$(_envcall(jni, FindClass("Cycript"))); + jmethodID Cycript$Make(_envcall(jni, GetStaticMethodID(Cycript$, "proxy", "(Ljava/lang/Class;J)Ljava/lang/Object;"))); + CYProtect *protect(new CYProtect(context, target)); + return CYCastJSObject(context, jni, _envcall(jni, CallObjectMethod(Cycript$, Cycript$Make, _class, reinterpret_cast(protect)))); + } CYJavaSignature bound(count); for (auto overload(table->overload_.lower_bound(bound)), e(table->overload_.upper_bound(bound)); overload != e; ++overload) { - jobjectArray array(CYCastJavaArguments(jni, overload->shorty, context, arguments, Object$)); - jobject object(_envcall(jni, CallObjectMethod(overload->method, Constructor$newInstance, array))); - return CYCastJSObject(context, CYCastJSValue(context, jni, object)); + jvalue array[count]; + if (!CYCastJavaArguments(jni, overload->shorty_, context, arguments, array)) + continue; + jobject object(_envcall(jni, NewObjectA(_class, overload->method_, array))); + return CYCastJSObject(context, jni, object); } CYThrow("invalid constructor call"); } CYCatch(NULL) } -static bool JavaClass_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); +static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); auto field(table->static_.find(name)); @@ -566,8 +777,9 @@ static bool JavaClass_hasProperty(JSContextRef context, JSObjectRef object, JSSt return true; } -static JSValueRef JavaClass_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); +static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); JNIEnv *jni(table->value_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); @@ -578,7 +790,7 @@ static JSValueRef JavaClass_getProperty(JSContextRef context, JSObjectRef object switch (field->second.primitive_) { case CYJavaPrimitiveObject: return CYCastJSValue(context, jni, _envcall(jni, GetStaticObjectField(table->value_, field->second.field_))); -#define CYJavaForEachPrimitive_(T, Typ, Type) \ +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ return CYJavaCastJSValue(context, _envcall(jni, GetStatic ## Typ ## Field(table->value_, field->second.field_))); CYJavaForEachPrimitive @@ -587,8 +799,9 @@ CYJavaForEachPrimitive } } CYCatch(NULL) } -static bool JavaClass_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); +static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); JNIEnv *jni(table->value_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); @@ -599,7 +812,7 @@ static bool JavaClass_setProperty(JSContextRef context, JSObjectRef object, JSSt switch (field->second.primitive_) { case CYJavaPrimitiveObject: _envcallv(jni, SetStaticObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value))); -#define CYJavaForEachPrimitive_(T, Typ, Type) \ +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ _envcallv(jni, SetStatic ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value))); \ break; @@ -611,8 +824,9 @@ CYJavaForEachPrimitive return true; } CYCatch(false) } -static void JavaClass_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); +static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); for (const auto &field : table->static_) JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); } @@ -622,10 +836,9 @@ static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef return CYCastJSValue(context, table->value_, table->value_); } CYCatch(NULL) } -static bool JavaObject_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); - JNIEnv *jni(internal->value_); - CYJavaClass *table(CYGetJavaTable(context, jni, internal->value_)); +static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); auto field(table->instance_.find(name)); @@ -634,10 +847,10 @@ static bool JavaObject_hasProperty(JSContextRef context, JSObjectRef object, JSS return true; } -static JSValueRef JavaObject_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); JNIEnv *jni(internal->value_); - CYJavaClass *table(CYGetJavaTable(context, jni, internal->value_)); + CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); auto field(table->instance_.find(name)); @@ -647,7 +860,7 @@ static JSValueRef JavaObject_getProperty(JSContextRef context, JSObjectRef objec switch (field->second.primitive_) { case CYJavaPrimitiveObject: return CYCastJSValue(context, jni, _envcall(jni, GetObjectField(internal->value_, field->second.field_))); -#define CYJavaForEachPrimitive_(T, Typ, Type) \ +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ return CYJavaCastJSValue(context, _envcall(jni, Get ## Typ ## Field(internal->value_, field->second.field_))); CYJavaForEachPrimitive @@ -656,10 +869,10 @@ CYJavaForEachPrimitive } } CYCatch(NULL) } -static bool JavaObject_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); JNIEnv *jni(internal->value_); - CYJavaClass *table(CYGetJavaTable(context, jni, internal->value_)); + CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); auto field(table->instance_.find(name)); @@ -669,7 +882,7 @@ static bool JavaObject_setProperty(JSContextRef context, JSObjectRef object, JSS switch (field->second.primitive_) { case CYJavaPrimitiveObject: _envcallv(jni, SetObjectField(table->value_, field->second.field_, CYCastJavaObject(jni, context, value))); -#define CYJavaForEachPrimitive_(T, Typ, Type) \ +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ _envcallv(jni, Set ## Typ ## Field(table->value_, field->second.field_, CYCastDouble(context, value))); \ break; @@ -681,10 +894,9 @@ CYJavaForEachPrimitive return true; } CYCatch(false) } -static void JavaObject_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); - JNIEnv *jni(internal->value_); - CYJavaClass *table(CYGetJavaTable(context, jni, internal->value_)); +static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + CYJavaClass *table(internal->table_); for (const auto &field : table->instance_) JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); } @@ -695,6 +907,97 @@ static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObj return CYGetJavaClass(context, jni, _envcall(jni, GetObjectClass(internal->value_))); } CYCatch(NULL) } +static JSValueRef JavaClass_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYJavaClass *internal(reinterpret_cast(JSObjectGetPrivate(object))); + JNIEnv *jni(internal->value_); + return CYJavaStaticInterior::Make(context, jni, internal->value_, internal); +} CYCatch(NULL) } + +static JSValueRef JavaObject_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); + JNIEnv *jni(internal->value_); + return CYJavaInterior::Make(context, jni, internal->value_, internal->table_); +} CYCatch(NULL) } + +static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYJavaClass *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + JNIEnv *jni(internal->value_); + jclass Class$(_envcall(jni, FindClass("java/lang/Class"))); + jmethodID Class$getCanonicalName(_envcall(jni, GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;"))); + return CYCastJSValue(context, CYJSString(jni, static_cast(_envcall(jni, CallObjectMethod(internal->value_, Class$getCanonicalName))))); +} CYCatch(NULL) } + +static JSValueRef JavaMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + std::ostringstream cyon; + return CYCastJSValue(context, CYJSString(cyon.str())); +} CYCatch(NULL) } + +static JSValueRef JavaStaticMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + std::ostringstream cyon; + return CYCastJSValue(context, CYJSString(cyon.str())); +} CYCatch(NULL) } + +static JSValueRef JavaArray_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + CYJavaArray *internal(reinterpret_cast(JSObjectGetPrivate(object))); + JNIEnv *jni(internal->value_); + if (JSStringIsEqual(property, length_s)) + return CYCastJSValue(context, _envcall(jni, GetArrayLength(internal->value_))); + + CYPool pool; + ssize_t offset; + if (!CYGetOffset(pool, context, property, offset)) + return NULL; + + if (internal->primitive_ == CYJavaPrimitiveObject) + return CYCastJSValue(context, jni, _envcall(jni, GetObjectArrayElement(static_cast(internal->value_.value_), offset))); + else switch (internal->primitive_) { +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + case CYJavaPrimitive ## Type: { \ + j ## type element; \ + _envcallv(jni, Get ## Typ ## ArrayRegion(static_cast(internal->value_.value_), offset, 1, &element)); \ + return CYJavaCastJSValue(context, element); \ + } break; +CYJavaForEachPrimitive +#undef CYJavaForEachPrimitive_ + default: _assert(false); + } +} CYCatch(NULL) } + +static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + CYJavaArray *internal(reinterpret_cast(JSObjectGetPrivate(object))); + JNIEnv *jni(internal->value_); + + CYPool pool; + ssize_t offset; + if (!CYGetOffset(pool, context, property, offset)) + return false; + + if (internal->primitive_ == CYJavaPrimitiveObject) + _envcallv(jni, SetObjectArrayElement(static_cast(internal->value_.value_), offset, CYCastJavaObject(jni, context, value))); + else switch (internal->primitive_) { +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + case CYJavaPrimitive ## Type: { \ + j ## type element; \ + _envcallv(jni, Get ## Typ ## ArrayRegion(static_cast(internal->value_.value_), offset, 1, &element)); \ + return CYJavaCastJSValue(context, element); \ + } break; +CYJavaForEachPrimitive +#undef CYJavaForEachPrimitive_ + default: _assert(false); + } + + return true; +} CYCatch(false) } + +static JSValueRef JavaPackage_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYJavaPackage *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + std::ostringstream name; + for (auto &package : internal->package_) + name << package << '.'; + name << '*'; + return CYCastJSValue(context, CYJSString(name.str())); +} CYCatch(NULL) } + static bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { return true; } @@ -711,7 +1014,7 @@ static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef ob name << package << '/'; name << next; - JNIEnv *jni(GetJNI()); + JNIEnv *jni(GetJNI(context)); if (jclass _class = jni->FindClass(name.str().c_str())) return CYGetJavaClass(context, jni, _class); jni->ExceptionClear(); @@ -720,61 +1023,122 @@ static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef ob return CYJavaPackage::Make(context, package); } CYCatch(NULL) } -static JSStaticValue JavaClass_staticValues[2] = { +static void Cycript_delete(JNIEnv *jni, jclass api, jlong jprotect) { CYJavaTry { + delete reinterpret_cast(jprotect); +} CYJavaCatch() } + +static jobject Cycript_handle(JNIEnv *jni, jclass api, jlong jprotect, jstring property, jobjectArray jarguments) { CYJavaTry { + auto &protect(*reinterpret_cast(jprotect)); + JSContextRef context(protect); + JSValueRef function(CYGetProperty(context, protect, CYJSString(jni, property))); + if (JSValueIsUndefined(context, function)) + return NULL; + + size_t count(jarguments == NULL ? 0 : _envcall(jni, GetArrayLength(jarguments))); + JSValueRef arguments[count]; + for (size_t index(0); index != count; ++index) + arguments[index] = CYCastJSValue(context, jni, _envcall(jni, GetObjectArrayElement(jarguments, index))); + + return CYCastJavaObject(jni, context, CYCallAsFunction(context, CYCastJSObject(context, function), protect, count, arguments)); +} CYJavaCatch(NULL) } + +static JNINativeMethod Cycript_[] = { + {(char *) "delete", (char *) "(J)V", (void *) &Cycript_delete}, + {(char *) "handle", (char *) "(JLjava/lang/String;[Ljava/lang/Object;)Ljava/lang/Object;", (void *) &Cycript_handle}, +}; + +static void CYRegisterNatives(JSContextRef context, JNIEnv *jni) { + jclass Cycript$(_envcall(jni, FindClass("Cycript"))); + _envcall(jni, RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0]))); +} + +static JSStaticValue JavaClass_staticValues[3] = { {"class", &JavaClass_getProperty_class, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"$cyi", &JavaClass_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; -static JSStaticValue JavaObject_staticValues[2] = { +static JSStaticFunction JavaClass_staticFunctions[2] = { + {"toCYON", &JavaClass_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + +static JSStaticValue JavaObject_staticValues[3] = { {"constructor", &JavaObject_getProperty_constructor, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"$cyi", &JavaObject_getProperty_$cyi, NULL, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, NULL, 0} }; -static JSStaticFunction JavaPackage_staticFunctions[1] = { +static JSStaticFunction JavaMethod_staticFunctions[2] = { + {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + +static JSStaticFunction JavaStaticMethod_staticFunctions[2] = { + {"toCYON", &JavaStaticMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + +static JSStaticFunction JavaPackage_staticFunctions[2] = { + {"toCYON", &JavaPackage_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; void CYJava_Initialize() { Primitives_.insert(std::make_pair("void", CYJavaPrimitiveVoid)); - Primitives_.insert(std::make_pair("boolean", CYJavaPrimitiveBoolean)); - Primitives_.insert(std::make_pair("byte", CYJavaPrimitiveByte)); - Primitives_.insert(std::make_pair("char", CYJavaPrimitiveCharacter)); - Primitives_.insert(std::make_pair("short", CYJavaPrimitiveShort)); - Primitives_.insert(std::make_pair("int", CYJavaPrimitiveInteger)); - Primitives_.insert(std::make_pair("long", CYJavaPrimitiveLong)); - Primitives_.insert(std::make_pair("float", CYJavaPrimitiveFloat)); - Primitives_.insert(std::make_pair("double", CYJavaPrimitiveDouble)); +#define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ + Primitives_.insert(std::make_pair(#type, CYJavaPrimitive ## Type)); +CYJavaForEachPrimitive +#undef CYJavaForEachPrimitive_ JSClassDefinition definition; definition = kJSClassDefinitionEmpty; definition.className = "JavaClass"; definition.staticValues = JavaClass_staticValues; - definition.hasProperty = &JavaClass_hasProperty; - definition.getProperty = &JavaClass_getProperty; - definition.setProperty = &JavaClass_setProperty; - definition.getPropertyNames = &JavaClass_getPropertyNames; + definition.staticFunctions = JavaClass_staticFunctions; definition.callAsConstructor = &JavaClass_callAsConstructor; definition.finalize = &CYFinalize; CYJavaClass::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.attributes = kJSClassAttributeNoAutomaticPrototype; + definition.className = "JavaInterior"; + definition.hasProperty = &JavaInterior_hasProperty; + definition.getProperty = &JavaInterior_getProperty; + definition.setProperty = &JavaInterior_setProperty; + definition.getPropertyNames = &JavaInterior_getPropertyNames; + definition.finalize = &CYFinalize; + CYJavaInterior::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "JavaMethod"; + definition.staticFunctions = JavaMethod_staticFunctions; definition.callAsFunction = &JavaMethod_callAsFunction; definition.finalize = &CYFinalize; CYJavaMethod::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "JavaStaticMethod"; + definition.staticFunctions = JavaStaticMethod_staticFunctions; + definition.callAsFunction = &JavaStaticMethod_callAsFunction; + definition.finalize = &CYFinalize; + CYJavaStaticMethod::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.attributes = kJSClassAttributeNoAutomaticPrototype; definition.className = "JavaObject"; definition.staticValues = JavaObject_staticValues; - definition.hasProperty = &JavaObject_hasProperty; - definition.getProperty = &JavaObject_getProperty; - definition.setProperty = &JavaObject_setProperty; - definition.getPropertyNames = &JavaObject_getPropertyNames; definition.finalize = &CYFinalize; CYJavaObject::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; + definition.className = "JavaArray"; + definition.getProperty = &JavaArray_getProperty; + definition.setProperty = &JavaArray_setProperty; + definition.finalize = &CYFinalize; + CYJavaArray::Class_ = JSClassCreate(&definition); + definition = kJSClassDefinitionEmpty; definition.className = "JavaPackage"; definition.staticFunctions = JavaPackage_staticFunctions; @@ -782,6 +1146,16 @@ void CYJava_Initialize() { definition.getProperty = &CYJavaPackage_getProperty; definition.finalize = &CYFinalize; CYJavaPackage::Class_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.attributes = kJSClassAttributeNoAutomaticPrototype; + definition.className = "JavaStaticInterior"; + definition.hasProperty = &JavaStaticInterior_hasProperty; + definition.getProperty = &JavaStaticInterior_getProperty; + definition.setProperty = &JavaStaticInterior_setProperty; + definition.getPropertyNames = &JavaStaticInterior_getPropertyNames; + definition.finalize = &CYFinalize; + CYJavaStaticInterior::Class_ = JSClassCreate(&definition); } void CYJava_SetupContext(JSContextRef context) {