From 20ded97a3fd17e970ee44a9a2ae273098e27c3cb Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Thu, 4 Mar 2010 07:03:21 +0000 Subject: [PATCH] Add a concept of .toPointer(). --- Execute.cpp | 17 +++++++++++++---- ObjectiveC/Library.mm | 14 +++++++++++++- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Execute.cpp b/Execute.cpp index b781a39..8ac1b14 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -165,6 +165,7 @@ JSStringRef push_s; JSStringRef splice_s; JSStringRef toCYON_s; JSStringRef toJSON_s; +JSStringRef toPointer_s; static JSStringRef Result_; @@ -514,12 +515,19 @@ void *CYCastPointer_(JSContextRef context, JSValueRef value) { switch (JSValueGetType(context, value)) { case kJSTypeNull: return NULL; - /*case kJSTypeObject: + case kJSTypeObject: { + JSObjectRef object((JSObjectRef) value); if (JSValueIsObjectOfClass(context, value, Pointer_)) { - Pointer *internal(reinterpret_cast(JSObjectGetPrivate((JSObjectRef) value))); + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); return internal->value_; - }*/ - default: + } + JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); + if (CYIsCallable(context, toPointer)) { + JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); + _assert(value != NULL); + return CYCastPointer_(context, value); + } + } default: double number(CYCastDouble(context, value)); if (std::isnan(number)) throw CYJSError(context, "cannot convert value to pointer"); @@ -1308,6 +1316,7 @@ void CYInitializeDynamic() { splice_s = JSStringCreateWithUTF8CString("splice"); toCYON_s = JSStringCreateWithUTF8CString("toCYON"); toJSON_s = JSStringCreateWithUTF8CString("toJSON"); + toPointer_s = JSStringCreateWithUTF8CString("toPointer"); Result_ = JSStringCreateWithUTF8CString("_"); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index f67fac2..e8777b4 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -2179,6 +2179,7 @@ static JSValueRef Instance_callAsFunction_toJSON(JSContextRef context, JSObjectR } CYPoolCatch(NULL) } CYCatch return /*XXX*/ NULL; } +#if 0 static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (!JSValueIsObjectOfClass(context, _this, Instance_)) return NULL; @@ -2186,6 +2187,16 @@ static JSValueRef Instance_callAsFunction_valueOf(JSContextRef context, JSObject Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); return CYCastJSValue(context, reinterpret_cast(internal->GetValue())); } CYCatch return /*XXX*/ NULL; } +#endif + +static JSValueRef Instance_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + if (!JSValueIsObjectOfClass(context, _this, Instance_)) + return NULL; + + Instance *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + // XXX: but... but... THIS ISN'T A POINTER! :( + return CYCastJSValue(context, reinterpret_cast(internal->GetValue())); +} CYCatch return /*XXX*/ NULL; } static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (!JSValueIsObjectOfClass(context, _this, Instance_)) @@ -2259,7 +2270,8 @@ static JSStaticFunction Instance_staticFunctions[6] = { {"$cya", &CYValue_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &Instance_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &Instance_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, - {"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + //{"valueOf", &Instance_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; -- 2.45.2