]> git.saurik.com Git - cycript.git/commitdiff
Add String::toCYON, toString_s, and bridge NSString via String.prototype.
authorJay Freeman (saurik) <saurik@saurik.com>
Tue, 27 Apr 2010 22:07:57 +0000 (22:07 +0000)
committerJay Freeman (saurik) <saurik@saurik.com>
Tue, 27 Apr 2010 22:07:57 +0000 (22:07 +0000)
Execute.cpp
JavaScript.hpp
ObjectiveC/Library.mm
todo.txt

index e8ea4ae006674d8f348818c6805ee4431df3212c..99dbda86c94db2188f2422d900b28982f2dc4ed5 100644 (file)
@@ -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);
index 4dba607d77611b50077c852b7d7df76a2b0cd958..7cead8e57f1f92f83330278a507ffda6aaa779c6 100644 (file)
@@ -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();
index 817fa1a39503a5db2f96fcf285617bd2f0e02b15..c1cacf86062bcea5c4123c0cf8e658f1d562914b 100644 (file)
@@ -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;
index 76e8de8f3b41df8785f2223703bc65ed52de8e1c..27707b719910dd4999b033d51b808c058218c2fb 100644 (file)
--- 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