From 4cb8aa43e7ccadcc4b7868ae45974ffc5f272c3d Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Tue, 27 Apr 2010 22:07:57 +0000 Subject: [PATCH] Add String::toCYON, toString_s, and bridge NSString via String.prototype. --- Execute.cpp | 17 +++++++++++++++++ JavaScript.hpp | 2 ++ ObjectiveC/Library.mm | 14 ++++++++++++-- todo.txt | 1 + 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Execute.cpp b/Execute.cpp index e8ea4ae..99dbda8 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -166,6 +166,7 @@ JSStringRef splice_s; JSStringRef toCYON_s; JSStringRef toJSON_s; JSStringRef toPointer_s; +JSStringRef toString_s; static JSStringRef Result_; @@ -484,6 +485,17 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); } CYCatch } +static JSValueRef String_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + CYPool pool; + std::ostringstream str; + + CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this))); + CYStringify(str, string.data, string.size); + + std::string value(str.str()); + return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size()))); +} CYCatch } + JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) { Pointer *internal(new Pointer(pointer, context, owner, length, type)); return JSObjectMake(context, Pointer_, internal); @@ -1318,6 +1330,7 @@ void CYInitializeDynamic() { toCYON_s = JSStringCreateWithUTF8CString("toCYON"); toJSON_s = JSStringCreateWithUTF8CString("toJSON"); toPointer_s = JSStringCreateWithUTF8CString("toPointer"); + toString_s = JSStringCreateWithUTF8CString("toString"); Result_ = JSStringCreateWithUTF8CString("_"); @@ -1405,9 +1418,13 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")))); CYSetProperty(context, cy, CYJSString("String"), String); + + JSObjectRef String_prototype(CYCastJSObject(context, CYGetProperty(context, String, prototype_s))); + CYSetProperty(context, cy, CYJSString("String_prototype"), String_prototype); /* }}} */ CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); + CYSetProperty(context, String_prototype, toCYON_s, &String_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); JSObjectRef cycript(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, global, CYJSString("Cycript"), cycript); diff --git a/JavaScript.hpp b/JavaScript.hpp index 4dba607..7cead8e 100644 --- a/JavaScript.hpp +++ b/JavaScript.hpp @@ -63,6 +63,8 @@ extern JSStringRef push_s; extern JSStringRef splice_s; extern JSStringRef toCYON_s; extern JSStringRef toJSON_s; +extern JSStringRef toPointer_s; +extern JSStringRef toString_s; void CYInitializeDynamic(); JSGlobalContextRef CYGetJSContext(); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 817fa1a..c1cacf8 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -281,6 +281,7 @@ static Class NSBoolNumber_; static Class NSArray_; static Class NSDictionary_; +static Class NSString_; static Class Object_; static Type_privateData *Object_type; @@ -294,6 +295,8 @@ Type_privateData *Selector_privateData::GetType() const { return Selector_type; } +static JSValueRef Instance_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception); + JSValueRef CYGetClassPrototype(JSContextRef context, id self) { if (self == nil) return CYGetCachedObject(context, CYJSString("Instance_prototype")); @@ -312,16 +315,22 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) { JSClassRef _class(NULL); JSValueRef prototype; + JSObjectRef object(JSObjectMake(context, _class, NULL)); + if (self == NSArray_) prototype = CYGetCachedObject(context, CYJSString("Array_prototype")); else if (self == NSDictionary_) prototype = CYGetCachedObject(context, CYJSString("Object_prototype")); - else + else if (self == NSString_) { + prototype = CYGetCachedObject(context, CYJSString("String_prototype")); + + CYSetProperty(context, object, toString_s, &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete); + } else prototype = CYGetClassPrototype(context, class_getSuperclass(self)); - JSObjectRef object(JSObjectMake(context, _class, NULL)); JSObjectSetPrototype(context, object, prototype); CYSetProperty(context, cy, name, object); + return object; } @@ -2403,6 +2412,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { NSArray_ = objc_getClass("NSArray"); NSDictionary_ = objc_getClass("NSDictionary"); + NSString_ = objc_getClass("NSString"); Object_ = objc_getClass("Object"); JSClassDefinition definition; diff --git a/todo.txt b/todo.txt index 76e8de8..27707b7 100644 --- a/todo.txt +++ b/todo.txt @@ -26,3 +26,4 @@ special work needs to be done to correctly handle the "arguments" symbol: Declar at the Program level I seem to be eating away all of the var statements function pointers are ?; note that blocks are currently block_P = '?' I should probably attempt to use the auto_ flag somehow to not do contexts_ push when compiling +Object_callAsFunction_toCYON should be implemented -- 2.47.2