From: Jay Freeman (saurik) Date: Fri, 1 Jan 2016 19:15:49 +0000 (-0800) Subject: Use * and -> syntax to unify Java and Objective-C. X-Git-Tag: v0.9.590~102 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/8effd3814c3f69bc2628345eef1accd89e936ddc Use * and -> syntax to unify Java and Objective-C. --- diff --git a/Java/Execute.cpp b/Java/Execute.cpp index c972562..227afdf 100644 --- a/Java/Execute.cpp +++ b/Java/Execute.cpp @@ -293,17 +293,34 @@ struct CYJavaClass : } }; +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, JSContextRef context) : + CYJavaValue(jni, value), + table_(reinterpret_cast(JSObjectGetPrivate(CYGetJavaClass(context, jni, _envcall(jni, GetObjectClass(value)))))) { } JSValueRef GetPrototype(JSContextRef context) const; }; +struct CYJavaInterior : + CYJavaValue +{ + CYJavaClass *table_; + + CYJavaInterior(JNIEnv *jni, jobject value, CYJavaClass *table) : + CYJavaValue(jni, value), + table_(table) + { + } +}; + struct CYJavaPackage : CYPrivate { @@ -316,8 +333,6 @@ 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); @@ -326,8 +341,7 @@ JSValueRef CYJavaObject::GetPrototype(JSContextRef context) const { static JSValueRef CYCastJSValue(JSContextRef context, JNIEnv *jni, jobject value) { if (value == NULL) return CYJSNull(context); - - return CYJavaObject::Make(context, jni, value); + return CYJavaObject::Make(context, jni, value, context); } static jstring CYCastJavaString(JNIEnv *jni, CYUTF16String value) { @@ -383,11 +397,6 @@ static jobject CYCastJavaObject(JNIEnv *jni, JSContextRef context, JSValueRef va } } -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))); @@ -622,10 +631,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 +642,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)); @@ -656,10 +664,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)); @@ -681,10 +689,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 +702,12 @@ static JSValueRef JavaObject_getProperty_constructor(JSContextRef context, JSObj return CYGetJavaClass(context, jni, _envcall(jni, GetObjectClass(internal->value_))); } 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 bool CYJavaPackage_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { return true; } @@ -725,8 +738,9 @@ static JSStaticValue JavaClass_staticValues[2] = { {NULL, NULL, NULL, 0} }; -static JSStaticValue JavaObject_staticValues[2] = { +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} }; @@ -758,6 +772,16 @@ void CYJava_Initialize() { 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.callAsFunction = &JavaMethod_callAsFunction; @@ -768,10 +792,6 @@ void CYJava_Initialize() { 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); diff --git a/ObjectiveC/Internal.hpp b/ObjectiveC/Internal.hpp index a19b076..759c875 100644 --- a/ObjectiveC/Internal.hpp +++ b/ObjectiveC/Internal.hpp @@ -82,12 +82,12 @@ struct Messages : JSValueRef GetPrototype(JSContextRef context) const; }; -struct Internal : - CYValue +struct Interior : + CYValue { CYProtect owner_; - _finline Internal(id value, JSContextRef context, JSObjectRef owner) : + _finline Interior(id value, JSContextRef context, JSObjectRef owner) : CYValue(value), owner_(context, owner) { diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 11a1305..6777868 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -1745,7 +1745,7 @@ static JSValueRef Instance_getProperty(JSContextRef context, JSObjectRef object, id self(internal->value_); if (JSStringIsEqualToUTF8CString(property, "$cyi")) - return Internal::Make(context, self, context, object); + return Interior::Make(context, self, context, object); CYPool pool; NSString *name(CYCastNSString(&pool, context, property)); @@ -1978,8 +1978,8 @@ static JSValueRef Instance_box_callAsFunction(JSContextRef context, JSObjectRef return CYCastJSValue(context, [value cy$box]); } CYCatch(NULL) } -static bool Internal_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { - Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool Interior_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + Interior *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; id self(internal->value_); @@ -2009,8 +2009,8 @@ static void CYBitField(unsigned &length, unsigned &shift, id self, Ivar ivar, co free(ivars); } -static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { - Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static JSValueRef Interior_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + Interior *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; id self(internal->value_); @@ -2045,8 +2045,8 @@ static JSValueRef Internal_getProperty(JSContextRef context, JSObjectRef object, return NULL; } CYCatch(NULL) } -static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { - Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static bool Interior_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry { + Interior *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; id self(internal->value_); @@ -2075,9 +2075,9 @@ static bool Internal_setProperty(JSContextRef context, JSObjectRef object, JSStr return false; } CYCatch(false) } -static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { +static void Interior_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRef names) { if (Class super = class_getSuperclass(_class)) - Internal_getPropertyNames_(super, names); + Interior_getPropertyNames_(super, names); unsigned int size; objc_ivar **data(class_copyIvarList(_class, &size)); @@ -2086,18 +2086,18 @@ static void Internal_getPropertyNames_(Class _class, JSPropertyNameAccumulatorRe free(data); } -static void Internal_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { - Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static void Interior_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { + Interior *internal(reinterpret_cast(JSObjectGetPrivate(object))); CYPool pool; id self(internal->value_); Class _class(object_getClass(self)); - Internal_getPropertyNames_(_class, names); + Interior_getPropertyNames_(_class, names); } -static JSValueRef Internal_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { - Internal *internal(reinterpret_cast(JSObjectGetPrivate(object))); +static JSValueRef Interior_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + Interior *internal(reinterpret_cast(JSObjectGetPrivate(object))); return internal->owner_; } CYCatch(NULL) } @@ -2693,8 +2693,8 @@ static JSStaticFunction Class_staticFunctions[2] = { {NULL, NULL, 0} }; -static JSStaticFunction Internal_staticFunctions[2] = { - {"$cya", &Internal_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, +static JSStaticFunction Interior_staticFunctions[2] = { + {"$cya", &Interior_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; @@ -2778,14 +2778,14 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { ClassInstance_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; - definition.className = "Internal"; - definition.staticFunctions = Internal_staticFunctions; - definition.hasProperty = &Internal_hasProperty; - definition.getProperty = &Internal_getProperty; - definition.setProperty = &Internal_setProperty; - definition.getPropertyNames = &Internal_getPropertyNames; + definition.className = "Interior"; + definition.staticFunctions = Interior_staticFunctions; + definition.hasProperty = &Interior_hasProperty; + definition.getProperty = &Interior_getProperty; + definition.setProperty = &Interior_setProperty; + definition.getPropertyNames = &Interior_getPropertyNames; definition.finalize = &CYFinalize; - Internal::Class_ = JSClassCreate(&definition); + Interior::Class_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; definition.className = "Message"; diff --git a/libcycript.cy b/libcycript.cy index 578f924..f5f63ea 100644 --- a/libcycript.cy +++ b/libcycript.cy @@ -86,49 +86,49 @@ $cy_set(RegExp.prototype, { if ("Java" in Cycript) { $cy_set(java.lang.Boolean.prototype, { toCYON: function() { - return `new java.lang.Boolean(${this.value})`; + return `new java.lang.Boolean(${this->value})`; }, }); $cy_set(java.lang.Byte.prototype, { toCYON: function() { - return `new java.lang.Byte(${this.value})`; + return `new java.lang.Byte(${this->value})`; }, }); $cy_set(java.lang.Character.prototype, { toCYON: function() { - return `new java.lang.Character(${this.value})`; + return `new java.lang.Character(${this->value})`; }, }); $cy_set(java.lang.Short.prototype, { toCYON: function() { - return `new java.lang.Short(${this.value})`; + return `new java.lang.Short(${this->value})`; }, }); $cy_set(java.lang.Integer.prototype, { toCYON: function() { - return `new java.lang.Integer(${this.value})`; + return `new java.lang.Integer(${this->value})`; }, }); $cy_set(java.lang.Long.prototype, { toCYON: function() { - return `new java.lang.Long(${this.value})`; + return `new java.lang.Long(${this->value})`; }, }); $cy_set(java.lang.Float.prototype, { toCYON: function() { - return `new java.lang.Float(${this.value})`; + return `new java.lang.Float(${this->value})`; }, }); $cy_set(java.lang.Double.prototype, { toCYON: function() { - return `new java.lang.Double(${this.value})`; + return `new java.lang.Double(${this->value})`; }, });