]> git.saurik.com Git - cycript.git/blobdiff - ObjectiveC/Library.mm
Fix a regression in iconv detection; cleaner clean.
[cycript.git] / ObjectiveC / Library.mm
index 548a2b2268de699d5e24850a3e7e6dc7fe95da77..e8777b48a51571527404ca55fdb9f748bfd4a282 100644 (file)
@@ -66,6 +66,7 @@
 #include "Error.hpp"
 #include "JavaScript.hpp"
 #include "String.hpp"
+#include "Execute.hpp"
 
 #include <cmath>
 #include <map>
     } \
 }
 
+#define CYSadTry { \
+    @try
+#define CYSadCatch(value) \
+    @catch (NSException *error ) { \
+        throw CYJSError(context, CYCastJSValue(context, error)); \
+    } return value; \
+}
+
 #ifndef __APPLE__
 #define class_getSuperclass GSObjCSuper
 #define class_getInstanceVariable GSCGetInstanceVariableDefinition
 
 JSValueRef CYSendMessage(apr_pool_t *pool, JSContextRef context, id self, Class super, SEL _cmd, size_t count, const JSValueRef arguments[], bool initialize, JSValueRef *exception);
 
-extern sqlite3 *Bridge_;
-
 /* Objective-C Pool Release {{{ */
 apr_status_t CYPoolRelease_(void *data) {
     id object(reinterpret_cast<id>(data));
@@ -287,17 +294,19 @@ Type_privateData *Selector_privateData::GetType() const {
     return Selector_type;
 }
 
-// XXX: trick this out with associated objects!
 JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
     if (self == nil)
         return CYGetCachedObject(context, CYJSString("Instance_prototype"));
 
-    // XXX: I need to think through multi-context
-    typedef std::map<id, JSValueRef> CacheMap;
-    static CacheMap cache_;
+    JSObjectRef global(CYGetGlobalObject(context));
+    JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s)));
+
+    char label[32];
+    sprintf(label, "i%p", self);
+    CYJSString name(label);
 
-    JSValueRef &value(cache_[self]);
-    if (value != NULL)
+    JSValueRef value(CYGetProperty(context, cy, name));
+    if (!JSValueIsUndefined(context, value))
         return value;
 
     JSClassRef _class(NULL);
@@ -312,9 +321,7 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) {
 
     JSObjectRef object(JSObjectMake(context, _class, NULL));
     JSObjectSetPrototype(context, object, prototype);
-
-    JSValueProtect(context, object);
-    value = object;
+    CYSetProperty(context, cy, name, object);
     return object;
 }
 
@@ -1312,31 +1319,29 @@ static SEL CYCastSEL(JSContextRef context, JSValueRef value) {
         return CYCastPointer<SEL>(context, value);
 }
 
-void *CYObjectiveC_ExecuteStart(JSContextRef context) {
-    // XXX: deal with exceptions!
+void *CYObjectiveC_ExecuteStart(JSContextRef context) { CYSadTry {
     return (void *) [[NSAutoreleasePool alloc] init];
-}
+} CYSadCatch(NULL) }
 
-void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) {
-    // XXX: deal with exceptions!
+void CYObjectiveC_ExecuteEnd(JSContextRef context, void *handle) { CYSadTry {
     return [(NSAutoreleasePool *) handle release];
-}
+} CYSadCatch() }
 
 JSValueRef CYObjectiveC_RuntimeProperty(JSContextRef context, CYUTF8String name) { CYPoolTry {
     if (name == "nil")
         return Instance::Make(context, nil);
     if (Class _class = objc_getClass(name.data))
         return CYMakeInstance(context, _class, true);
+    if (Protocol *protocol = objc_getProtocol(name.data))
+        return CYMakeInstance(context, protocol, true);
     return NULL;
 } CYPoolCatch(NULL) return /*XXX*/ NULL; }
 
-static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { @try {
+static void CYObjectiveC_CallFunction(JSContextRef context, ffi_cif *cif, void (*function)(), uint8_t *value, void **values) { CYSadTry {
     ffi_call(cif, function, value, values);
-} @catch (NSException *error ) {
-    throw CYJSError(context, CYCastJSValue(context, error));
-} }
+} CYSadCatch() }
 
-static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { @try  {
+static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, JSValueRef value) { CYSadTry {
     switch (type->primitive) {
         case sig::object_P:
         case sig::typename_P:
@@ -1352,9 +1357,7 @@ static bool CYObjectiveC_PoolFFI(apr_pool_t *pool, JSContextRef context, sig::Ty
     }
 
     return true;
-} @catch (NSException *error ) {
-    throw CYJSError(context, CYCastJSValue(context, error));
-} }
+} CYSadCatch(false) }
 
 static JSValueRef CYObjectiveC_FromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) { CYPoolTry {
     switch (type->primitive) {
@@ -1404,36 +1407,16 @@ static const char *CYPoolTypeEncoding(apr_pool_t *pool, JSContextRef context, SE
         return method_getTypeEncoding(method);
 
     const char *name(sel_getName(sel));
+    size_t length(strlen(name));
 
-    sqlite3_stmt *statement;
-
-    _sqlcall(sqlite3_prepare(Bridge_,
-        "select "
-            "\"bridge\".\"value\" "
-        "from \"bridge\" "
-        "where"
-            " \"bridge\".\"mode\" = -1 and"
-            " \"bridge\".\"name\" = ?"
-        " limit 1"
-    , -1, &statement, NULL));
-
-    _trace();
-    _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
-
-    const char *value;
-    if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) {
-    _trace();
-        value = NULL;}
-    else {
-    _trace();
-        value = sqlite3_column_pooled(pool, statement, 0);
-        }
+    char keyed[length + 2];
+    keyed[0] = '6';
+    keyed[length + 1] = '\0';
+    memcpy(keyed + 1, name, length);
 
-    _sqlcall(sqlite3_finalize(statement));
+    if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1))
+        return entry->value_;
 
-    if (value != NULL)
-        return value;
-_trace();
     return NULL;
 }
 
@@ -2196,12 +2179,35 @@ 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;
+
+    Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
+    return CYCastJSValue(context, reinterpret_cast<uintptr_t>(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<Instance *>(JSObjectGetPrivate(_this)));
+    // XXX: but... but... THIS ISN'T A POINTER! :(
+    return CYCastJSValue(context, reinterpret_cast<uintptr_t>(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_))
         return NULL;
 
     Instance *internal(reinterpret_cast<Instance *>(JSObjectGetPrivate(_this)));
 
+    id value(internal->GetValue());
+    if (value == nil)
+        return CYCastJSValue(context, "nil");
+
     CYPoolTry {
         // XXX: this seems like a stupid implementation; what if it crashes? why not use the CYONifier backend?
         return CYCastJSValue(context, CYJSString(context, [internal->GetValue() description]));
@@ -2260,10 +2266,12 @@ static JSStaticValue Instance_staticValues[5] = {
     {NULL, NULL, NULL, 0}
 };
 
-static JSStaticFunction Instance_staticFunctions[5] = {
+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},
+    {"toPointer", &Instance_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toString", &Instance_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
 };
@@ -2397,9 +2405,11 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry {
 void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
     JSObjectRef global(CYGetGlobalObject(context));
     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 ObjectiveC(JSObjectMake(context, NULL, NULL));
-    CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC);
+    CYSetProperty(context, cycript, CYJSString("ObjectiveC"), ObjectiveC);
 
     CYSetProperty(context, ObjectiveC, CYJSString("classes"), JSObjectMake(context, ObjectiveC_Classes_, NULL));
     CYSetProperty(context, ObjectiveC, CYJSString("protocols"), JSObjectMake(context, ObjectiveC_Protocols_, NULL));
@@ -2416,15 +2426,15 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry {
     JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s)));
     CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype);
 
-    CYSetProperty(context, global, CYJSString("Instance"), Instance);
-    CYSetProperty(context, global, CYJSString("Selector"), Selector);
-    CYSetProperty(context, global, CYJSString("Super"), Super);
+    CYSetProperty(context, cycript, CYJSString("Instance"), Instance);
+    CYSetProperty(context, cycript, CYJSString("Selector"), Selector);
+    CYSetProperty(context, cycript, CYJSString("Super"), Super);
 
 #if defined(__APPLE__) && defined(__arm__)
-    CYSetProperty(context, global, CYJSString("objc_registerClassPair"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_registerClassPair"), &objc_registerClassPair_));
+    CYSetProperty(context, all, CYJSString("objc_registerClassPair"), &objc_registerClassPair_, kJSPropertyAttributeDontEnum);
 #endif
 
-    CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend));
+    CYSetProperty(context, all, CYJSString("objc_msgSend"), &$objc_msgSend, kJSPropertyAttributeDontEnum);
 
     JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype")));
     JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype);