X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/95be164556de14834c2040ac7bea1632b63e5764..9ec2b9397cc7a924bb0aa9a44ba3747fd32fa36d:/Java/Execute.cpp diff --git a/Java/Execute.cpp b/Java/Execute.cpp index f450a5d..98bf103 100644 --- a/Java/Execute.cpp +++ b/Java/Execute.cpp @@ -25,7 +25,7 @@ #include -#ifdef __APPLE__ +#if defined(__APPLE__) && !defined(__arm__) #include #else #include @@ -77,8 +77,6 @@ _value; }) return value; \ } -static JNIEnv *GetJNI(JSContextRef context); - #define CYJavaForEachPrimitive \ CYJavaForEachPrimitive_(Z, z, Boolean, Boolean, boolean) \ CYJavaForEachPrimitive_(B, b, Byte, Byte, byte) \ @@ -245,11 +243,11 @@ class CYJavaUTF8String : public CYUTF8String { private: - const CYJavaRef *value_; + const CYJavaRef &value_; public: CYJavaUTF8String(const CYJavaRef &value) : - value_(&value) + value_(value) { _assert(value); JNIEnv *jni(value.jni()); @@ -257,29 +255,18 @@ class CYJavaUTF8String : data = jni->GetStringUTFChars(value, NULL); } - CYJavaUTF8String(const CYJavaRef &value) : - CYJavaUTF8String(CYCastJavaString(value)) - { - } + CYJavaUTF8String(CYJavaRef &&) = delete; ~CYJavaUTF8String() { - if (value_ != NULL) { - JNIEnv *jni(value_->jni()); - jni->ReleaseStringUTFChars(*value_, data); - } + JNIEnv *jni(value_.jni()); + jni->ReleaseStringUTFChars(value_, data); } CYJavaUTF8String(const CYJavaUTF8String &) = delete; - - CYJavaUTF8String(CYJavaUTF8String &&rhs) : - value_(rhs.value_) - { - rhs.value_ = NULL; - } }; CYJavaUTF8String CYCastUTF8String(const CYJavaRef &value) { - return CYJavaUTF8String(value); + return {value}; } JSStringRef CYCopyJSString(const CYJavaRef &value) { @@ -298,7 +285,8 @@ struct CYJavaError : } virtual const char *PoolCString(CYPool &pool) const { - return CYPoolCString(pool, CYJavaUTF8String(value_.cast())); + auto string(CYCastJavaString(value_.cast())); + return CYPoolCString(pool, CYJavaUTF8String(string)); } virtual JSValueRef CastJSValue(JSContextRef context, const char *name) const; @@ -451,20 +439,6 @@ static CYJavaLocal CYCastJavaString(const CYJavaRef &value) { return jni.CallObjectMethod(value, Object$toString); } -template -struct CYJavaValue : - CYPrivate -{ - CYJavaGlobal value_; - - CYJavaValue(const CYJavaRef &value) : - value_(value) - { - } - - CYJavaValue(const CYJavaValue &) = delete; -}; - static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef &value); template @@ -529,51 +503,54 @@ struct CYJavaSignature { { } - CYJavaSignature(unsigned count) : - shorty_(count) - { - } - + // XXX: the shorty doesn't store enough information bool operator <(const CYJavaSignature &rhs) const { - return shorty_.size() < rhs.shorty_.size(); + return shorty_ < rhs.shorty_; } }; -typedef std::multiset CYJavaOverload; +typedef std::set CYJavaOverload; +typedef std::map CYJavaOverloads; struct CYJavaMethod : - CYPrivate + CYRoot { - CYJavaOverload overload_; - - CYJavaMethod(const CYJavaOverload &overload) : - overload_(overload) + CYUTF8String type_; + CYUTF8String name_; + CYJavaOverloads overloads_; + + CYJavaMethod(CYUTF8String type, CYUTF8String name, const CYJavaOverloads &overloads) : + type_(CYPoolUTF8String(*pool_, type)), + name_(CYPoolUTF8String(*pool_, name)), + overloads_(overloads) { } }; -struct CYJavaStaticMethod : - CYPrivate +struct CYJavaInstanceMethod : + CYJavaMethod { - CYJavaOverload overload_; + using CYJavaMethod::CYJavaMethod; +}; - CYJavaStaticMethod(const CYJavaOverload &overload) : - overload_(overload) - { - } +struct CYJavaStaticMethod : + CYJavaMethod +{ + using CYJavaMethod::CYJavaMethod; }; struct CYJavaClass : - CYJavaValue + CYRoot { + CYJavaGlobal value_; bool interface_; CYJavaFieldMap static_; CYJavaFieldMap instance_; - CYJavaOverload overload_; + CYJavaOverloads overloads_; CYJavaClass(const CYJavaRef &value, bool interface) : - CYJavaValue(value), + value_(value), interface_(interface) { } @@ -582,12 +559,13 @@ struct CYJavaClass : static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef &_class); struct CYJavaObject : - CYJavaValue + CYRoot { + CYJavaGlobal value_; CYJavaClass *table_; CYJavaObject(const CYJavaRef &value, CYJavaClass *table) : - CYJavaValue(value), + value_(value), table_(table) { } @@ -596,36 +574,39 @@ struct CYJavaObject : }; struct CYJavaInterior : - CYJavaValue + CYRoot { + CYJavaGlobal value_; CYJavaClass *table_; CYJavaInterior(const CYJavaRef &value, CYJavaClass *table) : - CYJavaValue(value), + value_(value), table_(table) { } }; struct CYJavaStaticInterior : - CYJavaValue + CYRoot { + CYJavaGlobal value_; CYJavaClass *table_; CYJavaStaticInterior(const CYJavaRef &value, CYJavaClass *table) : - CYJavaValue(value), + value_(value), table_(table) { } }; struct CYJavaArray : - CYJavaValue + CYRoot { + CYJavaGlobal value_; CYJavaPrimitive primitive_; CYJavaArray(const CYJavaRef &value, CYJavaPrimitive primitive) : - CYJavaValue(value), + value_(value), primitive_(primitive) { } @@ -634,12 +615,15 @@ struct CYJavaArray : }; struct CYJavaPackage : - CYPrivate + CYRoot { + JNIEnv *jni_; + typedef std::vector Path; Path package_; - _finline CYJavaPackage(const Path &package) : + _finline CYJavaPackage(JNIEnv *jni, const Path &package) : + jni_(jni), package_(package) { } @@ -669,7 +653,7 @@ static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef & auto Class$getComponentType(jni.GetMethodID(Class$, "getComponentType", "()Ljava/lang/Class;")); auto component(jni.CallObjectMethod(_class, Class$getComponentType)); auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); - return CYJavaArray::Make(context, value.cast(), CYJavaGetPrimitive(context, component, Class$getName)); + return CYPrivate::Make(context, value.cast(), CYJavaGetPrimitive(context, component, Class$getName)); } auto Wrapper$(jni.FindClass("Cycript$Wrapper")); @@ -680,7 +664,7 @@ static JSValueRef CYCastJSValue(JSContextRef context, const CYJavaRef & } CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(CYGetJavaClass(context, _class)))); - return CYJavaObject::Make(context, value, table); + return CYPrivate::Make(context, value, table); } static _finline JSObjectRef CYCastJSObject(JSContextRef context, const CYJavaRef &value) { @@ -712,13 +696,13 @@ CYCastJava$(F, Float, jfloat, CYCastDouble) CYCastJava$(D, Double, jdouble, CYCastDouble) static CYJavaClass *CYGetJavaTable(JSContextRef context, JSObjectRef object) { - if (!JSValueIsObjectOfClass(context, object, CYJavaClass::Class_)) + if (!JSValueIsObjectOfClass(context, object, CYPrivate::Class_)) return NULL; return reinterpret_cast(JSObjectGetPrivate(object)); } static CYJavaObject *CYGetJavaObject(JSContextRef context, JSObjectRef object) { - if (!JSValueIsObjectOfClass(context, object, CYJavaObject::Class_)) + if (!JSValueIsObjectOfClass(context, object, CYPrivate::Class_)) return NULL; return reinterpret_cast(JSObjectGetPrivate(object)); } @@ -764,7 +748,10 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef auto Class$(jni.FindClass("java/lang/Class")); auto Class$getName(jni.GetMethodID(Class$, "getName", "()Ljava/lang/String;")); - CYJSString name(jni.CallObjectMethod(value, Class$getName)); + auto string(jni.CallObjectMethod(value, Class$getName)); + CYJavaUTF8String utf8(string); + CYJSString name(utf8); + JSValueRef cached(CYGetProperty(context, cy, name)); if (!JSValueIsUndefined(context, cached)) return CYCastJSObject(context, cached); @@ -809,14 +796,15 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef auto modifiers(jni.CallIntMethod(field, Field$getModifiers)); auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); auto &map(instance ? table->instance_ : table->static_); - CYJavaUTF8String name(jni.CallObjectMethod(field, Field$getName)); + auto string(jni.CallObjectMethod(field, Field$getName)); + CYJavaUTF8String name(string); auto id(jni.FromReflectedField(field)); auto type(jni.CallObjectMethod(field, Field$getType)); map.insert(std::make_pair(std::string(name), CYJavaField{id, CYJavaGetPrimitive(context, type, Class$getName)})); } } - constructor = JSObjectMake(context, CYJavaClass::Class_, table); + constructor = JSObjectMake(context, CYPrivate::Class_, table); prototype = JSObjectMake(context, NULL, NULL); CYSetProperty(context, constructor, prototype_s, prototype, kJSPropertyAttributeDontEnum); @@ -828,34 +816,52 @@ static JSObjectRef CYGetJavaClass(JSContextRef context, const CYJavaRef auto parameters(jni.CallObjectMethod(constructor, Constructor$getParameterTypes)); CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); auto id(jni.FromReflectedMethod(constructor)); - table->overload_.insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty)); + table->overloads_[shorty.size()].insert(CYJavaSignature(constructor, id, CYJavaPrimitiveObject, shorty)); } - auto methods(jni.CallObjectMethod(value, Class$getDeclaredMethods)); + std::map, CYJavaOverloads> entries; + + bool base(false); + for (CYJavaLocal prototype(value); prototype; prototype = jni.GetSuperclass(prototype)) { + auto methods(jni.CallObjectMethod(prototype, Class$getDeclaredMethods)); - std::map, CYJavaOverload> entries; + for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) { + auto method(jni.GetObjectArrayElement(methods, i)); + auto modifiers(jni.CallIntMethod(method, Method$getModifiers)); + auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); + auto string(jni.CallObjectMethod(method, Method$getName)); + CYJavaUTF8String name(string); + + auto parameters(jni.CallObjectMethod(method, Method$getParameterTypes)); + CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); + + CYJavaOverload *overload; + if (!base) + overload = &entries[std::make_pair(instance, std::string(name))][shorty.size()]; + else { + auto entry(entries.find(std::make_pair(instance, std::string(name)))); + if (entry == entries.end()) + continue; + overload = &entry->second[shorty.size()]; + } - for (jsize i(0), e(jni.GetArrayLength(methods)); i != e; ++i) { - auto method(jni.GetObjectArrayElement(methods, i)); - auto modifiers(jni.CallIntMethod(method, Method$getModifiers)); - auto instance(!jni.CallStaticBooleanMethod(Modifier$, Modifier$isStatic, modifiers)); - CYJavaUTF8String name(jni.CallObjectMethod(method, Method$getName)); - auto parameters(jni.CallObjectMethod(method, Method$getParameterTypes)); - CYJavaShorty shorty(CYJavaGetShorty(context, parameters, Class$getName)); - auto type(jni.CallObjectMethod(method, Method$getReturnType)); - auto primitive(CYJavaGetPrimitive(context, type, Class$getName)); - auto id(jni.FromReflectedMethod(method)); - entries[std::make_pair(instance, std::string(name))].insert(CYJavaSignature(method, id, primitive, shorty)); + auto type(jni.CallObjectMethod(method, Method$getReturnType)); + auto primitive(CYJavaGetPrimitive(context, type, Class$getName)); + auto id(jni.FromReflectedMethod(method)); + overload->insert(CYJavaSignature(method, id, primitive, shorty)); + } + + base = true; } for (const auto &entry : entries) { bool instance(entry.first.first); - CYJSString name(entry.first.second); + CYJSString method(entry.first.second); auto &overload(entry.second); if (instance) - CYSetProperty(context, prototype, name, CYJavaMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + CYSetProperty(context, prototype, method, CYPrivate::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); else - CYSetProperty(context, constructor, name, CYJavaStaticMethod::Make(context, overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + CYSetProperty(context, constructor, method, CYPrivate::Make(context, utf8, entry.first.second.c_str(), overload), kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); } } @@ -939,27 +945,29 @@ static bool CYCastJavaArguments(const CYJavaFrame &frame, const CYJavaShorty &sh 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))); +static JSValueRef JavaInstanceMethod_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + auto internal(CYPrivate::Get(context, object)); CYJavaObject *self(CYGetJavaObject(context, _this)); + _assert(self != NULL); CYJavaEnv jni(self->value_); - CYJavaSignature bound(count); - for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { + auto overload(internal->overloads_.find(count)); + if (overload != internal->overloads_.end()) + for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { CYJavaFrame frame(jni, count + 16); jvalue array[count]; - if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) + if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) continue; jvalue *values(array); - switch (overload->primitive_) { + switch (signature->primitive_) { case CYJavaPrimitiveObject: - return CYCastJSValue(context, jni.CallObjectMethodA(self->value_, overload->method_, values)); + return CYCastJSValue(context, jni.CallObjectMethodA(self->value_, signature->method_, values)); case CYJavaPrimitiveVoid: - jni.CallVoidMethodA(self->value_, overload->method_, values); + jni.CallVoidMethodA(self->value_, signature->method_, values); return CYJSUndefined(context); #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ - return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, overload->method_, values)); + return CYJavaCastJSValue(context, jni.Call ## Typ ## MethodA(self->value_, signature->method_, values)); CYJavaForEachPrimitive #undef CYJavaForEachPrimitive_ default: _assert(false); @@ -970,26 +978,27 @@ CYJavaForEachPrimitive } CYCatch(NULL) } 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))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(CYGetJavaTable(context, _this)); CYJavaEnv jni(table->value_); - CYJavaSignature bound(count); - for (auto overload(internal->overload_.lower_bound(bound)), e(internal->overload_.upper_bound(bound)); overload != e; ++overload) { + auto overload(internal->overloads_.find(count)); + if (overload != internal->overloads_.end()) + for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { CYJavaFrame frame(jni, count + 16); jvalue array[count]; - if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) + if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) continue; jvalue *values(array); - switch (overload->primitive_) { + switch (signature->primitive_) { case CYJavaPrimitiveObject: - return CYCastJSValue(context, jni.CallStaticObjectMethodA(table->value_, overload->method_, values)); + return CYCastJSValue(context, jni.CallStaticObjectMethodA(table->value_, signature->method_, values)); case CYJavaPrimitiveVoid: - jni.CallStaticVoidMethodA(table->value_, overload->method_, values); + jni.CallStaticVoidMethodA(table->value_, signature->method_, values); return CYJSUndefined(context); #define CYJavaForEachPrimitive_(T, t, Typ, Type, type) \ case CYJavaPrimitive ## Type: \ - return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, overload->method_, values)); + return CYJavaCastJSValue(context, jni.CallStatic ## Typ ## MethodA(table->value_, signature->method_, values)); CYJavaForEachPrimitive #undef CYJavaForEachPrimitive_ default: _assert(false); @@ -1000,7 +1009,7 @@ CYJavaForEachPrimitive } CYCatch(NULL) } static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); + auto table(CYPrivate::Get(context, object)); CYJavaEnv jni(table->value_); jclass _class(table->value_); @@ -1010,22 +1019,23 @@ static JSObjectRef JavaClass_callAsConstructor(JSContextRef context, JSObjectRef return CYCastJSObject(context, jni.CallObjectMethod(Cycript$, Cycript$Make, _class, CYCastJavaObject(jni, context, CYCastJSObject(context, arguments[0])).get())); } - CYJavaSignature bound(count); - for (auto overload(table->overload_.lower_bound(bound)), e(table->overload_.upper_bound(bound)); overload != e; ++overload) { + auto overload(table->overloads_.find(count)); + if (overload != table->overloads_.end()) + for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { CYJavaFrame frame(jni, count + 16); jvalue array[count]; - if (!CYCastJavaArguments(frame, overload->shorty_, context, arguments, array)) + if (!CYCastJavaArguments(frame, signature->shorty_, context, arguments, array)) continue; jvalue *values(array); - auto object(jni.NewObjectA(_class, overload->method_, values)); + auto object(jni.NewObjectA(_class, signature->method_, values)); return CYCastJSObject(context, object); } CYThrow("invalid constructor call"); -} CYCatch(NULL) } +} CYCatchObject() } static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); @@ -1036,7 +1046,7 @@ static bool JavaStaticInterior_hasProperty(JSContextRef context, JSObjectRef obj } static JSValueRef JavaStaticInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); CYJavaEnv jni(table->value_); CYPool pool; @@ -1058,7 +1068,7 @@ CYJavaForEachPrimitive } CYCatch(NULL) } static bool JavaStaticInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); CYJavaEnv jni(table->value_); CYPool pool; @@ -1083,19 +1093,19 @@ CYJavaForEachPrimitive } CYCatch(false) } static void JavaStaticInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - CYJavaStaticInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); for (const auto &field : table->static_) JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); } static JSValueRef JavaClass_getProperty_class(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaClass *table(reinterpret_cast(JSObjectGetPrivate(object))); + auto table(CYPrivate::Get(context, object)); return CYCastJSValue(context, table->value_); } CYCatch(NULL) } static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); CYPool pool; auto name(CYPoolUTF8String(pool, context, property)); @@ -1106,7 +1116,7 @@ static bool JavaInterior_hasProperty(JSContextRef context, JSObjectRef object, J } static JSValueRef JavaInterior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaEnv jni(internal->value_); CYJavaClass *table(internal->table_); CYPool pool; @@ -1128,7 +1138,7 @@ CYJavaForEachPrimitive } CYCatch(NULL) } static bool JavaInterior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaEnv jni(internal->value_); CYJavaClass *table(internal->table_); CYPool pool; @@ -1153,30 +1163,30 @@ CYJavaForEachPrimitive } CYCatch(false) } static void JavaInterior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - CYJavaInterior *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaClass *table(internal->table_); for (const auto &field : table->instance_) JSPropertyNameAccumulatorAddName(names, CYJSString(field.first)); } static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaObject *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaEnv jni(internal->value_); return CYGetJavaClass(context, 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))); - return CYJavaStaticInterior::Make(context, internal->value_, internal); + auto internal(CYPrivate::Get(context, object)); + return CYPrivate::Make(context, 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))); - return CYJavaInterior::Make(context, internal->value_, internal->table_); + auto internal(CYPrivate::Get(context, object)); + return CYPrivate::Make(context, 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))); + auto internal(CYPrivate::Get(context, _this)); CYJavaEnv jni(internal->value_); auto Class$(jni.FindClass("java/lang/Class")); auto Class$getCanonicalName(jni.GetMethodID(Class$, "getCanonicalName", "()Ljava/lang/String;")); @@ -1184,17 +1194,27 @@ static JSValueRef JavaClass_callAsFunction_toCYON(JSContextRef context, JSObject } CYCatch(NULL) } static JSValueRef JavaMethod_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + auto internal(CYPrivate::Get(context, _this)); 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; + if (false) + cyon << internal->type_ << "." << internal->name_; + else { + bool comma(false); + for (auto overload(internal->overloads_.begin()); overload != internal->overloads_.end(); ++overload) + for (auto signature(overload->second.begin()); signature != overload->second.end(); ++signature) { + if (comma) + cyon << std::endl; + else + comma = true; + auto string(CYCastJavaString(signature->reflected_)); + cyon << CYJavaUTF8String(string); + } + } 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))); + auto internal(CYPrivate::Get(context, object)); CYJavaEnv jni(internal->value_); if (JSStringIsEqual(property, length_s)) return CYCastJSValue(context, jni.GetArrayLength(internal->value_)); @@ -1220,7 +1240,7 @@ CYJavaForEachPrimitive } CYCatch(NULL) } static bool JavaArray_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - CYJavaArray *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaEnv jni(internal->value_); CYPool pool; @@ -1245,8 +1265,10 @@ CYJavaForEachPrimitive return true; } CYCatch(false) } +static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env); + 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))); + auto internal(CYPrivate::Get(context, _this)); std::ostringstream name; for (auto &package : internal->package_) name << package << '.'; @@ -1259,7 +1281,7 @@ static bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object, } static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - CYJavaPackage *internal(reinterpret_cast(JSObjectGetPrivate(object))); + auto internal(CYPrivate::Get(context, object)); CYJavaPackage::Path package(internal->package_); CYPool pool; @@ -1270,13 +1292,16 @@ static JSValueRef CYJavaPackage_getProperty(JSContextRef context, JSObjectRef ob name << package << '/'; name << next; - JNIEnv *jni(GetJNI(context)); + if (internal->jni_ == NULL) + GetJNI(context, internal->jni_); + JNIEnv *jni(internal->jni_); + if (auto _class = jni->FindClass(name.str().c_str())) return CYGetJavaClass(context, CYJavaLocal(jni, _class)); jni->ExceptionClear(); package.push_back(next); - return CYJavaPackage::Make(context, package); + return CYPrivate::Make(context, jni, package); } CYCatch(NULL) } static void Cycript_delete(JNIEnv *env, jclass api, jlong jprotect) { CYJavaTry { @@ -1308,26 +1333,17 @@ static _finline void dlset(Type_ &function, const char *name, void *handle) { jint CYJavaVersion(JNI_VERSION_1_4); -static JNIEnv *CYGetCreatedJava(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) { +static JavaVM *CYGetJavaVM(jint (*$JNI_GetCreatedJavaVMs)(JavaVM **, jsize, jsize *)) { jsize capacity(16); JavaVM *jvms[capacity]; jsize size; _jnicall($JNI_GetCreatedJavaVMs(jvms, capacity, &size)); if (size == 0) return NULL; - JavaVM *jvm(jvms[0]); - JNIEnv *jni; - _jnicall(jvm->GetEnv(reinterpret_cast(&jni), CYJavaVersion)); - return jni; + return jvms[0]; } -static JNIEnv *GetJNI_(JSContextRef context) { - static JavaVM *jvm(NULL); - static JNIEnv *jni(NULL); - - if (jni != NULL) - return jni; - +static JavaVM *CYGetJavaVM(JSContextRef context) { CYPool pool; void *handle(RTLD_DEFAULT); std::string library; @@ -1336,8 +1352,8 @@ static JNIEnv *GetJNI_(JSContextRef context) { dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); if ($JNI_GetCreatedJavaVMs != NULL) { - if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs)) - return jni; + if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) + return jvm; } else { std::vector guesses; @@ -1347,6 +1363,10 @@ static JNIEnv *GetJNI_(JSContextRef context) { guesses.push_back(android); #endif + guesses.push_back("/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/jli/libjli.dylib"); + //guesses.push_back("/System/Library/Frameworks/JavaVM.framework/JavaVM"); + guesses.push_back("libjvm.dylib"); + guesses.push_back("libart.so"); guesses.push_back("libdvm.so"); guesses.push_back("libjvm.so"); @@ -1359,11 +1379,12 @@ static JNIEnv *GetJNI_(JSContextRef context) { } } - _assert(library.size() != 0); + if (library.size() == 0) + return NULL; dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", handle); - if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs)) - return jni; + if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) + return jvm; } std::vector options; @@ -1406,16 +1427,19 @@ static JNIEnv *GetJNI_(JSContextRef context) { JniInvocation$Init(invocation, NULL); dlset($JNI_GetCreatedJavaVMs, "JNI_GetCreatedJavaVMs", libnativehelper); - if (JNIEnv *jni = CYGetCreatedJava($JNI_GetCreatedJavaVMs)) - return jni; + if (JavaVM *jvm = CYGetJavaVM($JNI_GetCreatedJavaVMs)) + return jvm; } } + JavaVM *jvm; + JNIEnv *env; + if (void *libandroid_runtime = dlopen("libandroid_runtime.so", RTLD_LAZY | RTLD_GLOBAL)) { class AndroidRuntime$; AndroidRuntime$ *(*AndroidRuntime$$init$)(AndroidRuntime$ *self, char *args, unsigned int size)(NULL); - int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **jni)(NULL); - int (*AndroidRuntime$startReg)(JNIEnv *jni)(NULL); + int (*AndroidRuntime$startVm)(AndroidRuntime$ *self, JavaVM **jvm, JNIEnv **env)(NULL); + int (*AndroidRuntime$startReg)(JNIEnv *env)(NULL); int (*AndroidRuntime$addOption)(AndroidRuntime$ *self, const char *option, void *extra)(NULL); int (*AndroidRuntime$addVmArguments)(AndroidRuntime$ *self, int, const char *const argv[])(NULL); AndroidRuntime$ *(*AndroidRuntime$finalize)(AndroidRuntime$ *self)(NULL); @@ -1445,14 +1469,14 @@ static JNIEnv *GetJNI_(JSContextRef context) { int failure; _assert(AndroidRuntime$startVm != NULL); - failure = AndroidRuntime$startVm(runtime, &jvm, &jni); + failure = AndroidRuntime$startVm(runtime, &jvm, &env); _assert(failure == 0); _assert(AndroidRuntime$startReg != NULL); - failure = AndroidRuntime$startReg(jni); + failure = AndroidRuntime$startReg(env); _assert(failure == 0); - return jni; + return jvm; } jint (*$JNI_CreateJavaVM)(JavaVM **jvm, void **, void *); @@ -1463,15 +1487,27 @@ static JNIEnv *GetJNI_(JSContextRef context) { args.version = CYJavaVersion; args.nOptions = options.size(); args.options = options.data(); - _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast(&jni), &args)); - return jni; + _jnicall($JNI_CreateJavaVM(&jvm, reinterpret_cast(&env), &args)); + return jvm; } -static JNIEnv *GetJNI(JSContextRef context) { - CYJavaEnv jni(GetJNI_(context)); +static JNIEnv *GetJNI(JSContextRef context, JNIEnv *&env) { + auto jvm(CYGetJavaVM(context)); + _assert(jvm != NULL); + _jnicall(jvm->GetEnv(reinterpret_cast(&env), CYJavaVersion)); + CYJavaEnv jni(env); + + // XXX: this happens once per stub :/ + auto Cycript$(jni.FindClass("Cycript")); jni.RegisterNatives(Cycript$, Cycript_, sizeof(Cycript_) / sizeof(Cycript_[0])); - return jni; + + JSObjectRef java(CYGetCachedObject(context, CYJSString("Java"))); + JSValueRef arguments[1]; + arguments[0] = CYCastJSValue(context, CYJSString("setup")); + CYCallAsFunction(context, CYCastJSObject(context, CYGetProperty(context, java, CYJSString("emit"))), java, 1, arguments); + + return env; } static JSStaticValue JavaClass_staticValues[3] = { @@ -1491,13 +1527,13 @@ static JSStaticValue JavaObject_staticValues[3] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction JavaMethod_staticFunctions[2] = { +static JSStaticFunction JavaInstanceMethod_staticFunctions[2] = { {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; static JSStaticFunction JavaStaticMethod_staticFunctions[2] = { - {"toCYON", &JavaStaticMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toCYON", &JavaMethod_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; @@ -1521,7 +1557,7 @@ CYJavaForEachPrimitive definition.staticFunctions = JavaClass_staticFunctions; definition.callAsConstructor = &JavaClass_callAsConstructor; definition.finalize = &CYFinalize; - CYJavaClass::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.attributes = kJSClassAttributeNoAutomaticPrototype; @@ -1531,35 +1567,40 @@ CYJavaForEachPrimitive definition.setProperty = &JavaInterior_setProperty; definition.getPropertyNames = &JavaInterior_getPropertyNames; definition.finalize = &CYFinalize; - CYJavaInterior::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "JavaMethod"; - definition.staticFunctions = JavaMethod_staticFunctions; - definition.callAsFunction = &JavaMethod_callAsFunction; definition.finalize = &CYFinalize; - CYJavaMethod::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); + + definition = kJSClassDefinitionEmpty; + definition.className = "JavaInstanceMethod"; + definition.parentClass = CYPrivate::Class_; + definition.staticFunctions = JavaInstanceMethod_staticFunctions; + definition.callAsFunction = &JavaInstanceMethod_callAsFunction; + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "JavaStaticMethod"; + definition.parentClass = CYPrivate::Class_; definition.staticFunctions = JavaStaticMethod_staticFunctions; definition.callAsFunction = &JavaStaticMethod_callAsFunction; - definition.finalize = &CYFinalize; - CYJavaStaticMethod::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.attributes = kJSClassAttributeNoAutomaticPrototype; definition.className = "JavaObject"; definition.staticValues = JavaObject_staticValues; definition.finalize = &CYFinalize; - CYJavaObject::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "JavaArray"; definition.getProperty = &JavaArray_getProperty; definition.setProperty = &JavaArray_setProperty; definition.finalize = &CYFinalize; - CYJavaArray::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "JavaPackage"; @@ -1567,7 +1608,7 @@ CYJavaForEachPrimitive definition.hasProperty = &CYJavaPackage_hasProperty; definition.getProperty = &CYJavaPackage_getProperty; definition.finalize = &CYFinalize; - CYJavaPackage::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.attributes = kJSClassAttributeNoAutomaticPrototype; @@ -1577,25 +1618,26 @@ CYJavaForEachPrimitive definition.setProperty = &JavaStaticInterior_setProperty; definition.getPropertyNames = &JavaStaticInterior_getPropertyNames; definition.finalize = &CYFinalize; - CYJavaStaticInterior::Class_ = JSClassCreate(&definition); + CYPrivate::Class_ = JSClassCreate(&definition); } void CYJava_SetupContext(JSContextRef context) { JSObjectRef global(CYGetGlobalObject(context)); - //JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); + JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); JSObjectRef all(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("all")))); //JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); JSObjectRef Java(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, cycript, CYJSString("Java"), Java); + CYSetProperty(context, cy, CYJSString("Java"), Java); - JSObjectRef Packages(CYJavaPackage::Make(context, CYJavaPackage::Path())); - CYSetProperty(context, all, CYJSString("Packages"), Packages); + JSObjectRef Packages(CYPrivate::Make(context, nullptr, CYJavaPackage::Path())); + CYSetProperty(context, all, CYJSString("Packages"), Packages, kJSPropertyAttributeDontEnum); for (auto name : (const char *[]) {"java", "javax", "android", "com", "net", "org"}) { CYJSString js(name); - CYSetProperty(context, all, js, CYGetProperty(context, Packages, js)); + CYSetProperty(context, all, js, CYPrivate::Make(context, nullptr, CYJavaPackage::Path(1, name)), kJSPropertyAttributeDontEnum); } }