]> git.saurik.com Git - cycript.git/blobdiff - Library.cpp
Setup Pointer to emulate an Array.
[cycript.git] / Library.cpp
index 7bc867bd43f9c7c5009094ceb3b1a2ad2a9852b8..01e38018ce7801efe4e3f40a88761f32fd90cda6 100644 (file)
@@ -340,8 +340,19 @@ struct CStringMapLess :
     }
 };
 
-void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) {
-    if (name == NULL)
+void Structor_(apr_pool_t *pool, sig::Type *&type) {
+    if (
+        type->primitive == sig::pointer_P &&
+        type->data.data.type != NULL &&
+        type->data.data.type->primitive == sig::struct_P &&
+        strcmp(type->data.data.type->name, "_objc_class") == 0
+    ) {
+        type->primitive = sig::typename_P;
+        type->data.data.type = NULL;
+        return;
+    }
+
+    if (type->primitive != sig::struct_P || type->name == NULL)
         return;
 
     sqlite3_stmt *statement;
@@ -357,7 +368,7 @@ void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type
         " limit 1"
     , -1, &statement, NULL));
 
-    _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC));
+    _sqlcall(sqlite3_bind_text(statement, 1, type->name, -1, SQLITE_STATIC));
 
     int mode;
     const char *value;
@@ -396,10 +407,12 @@ struct Pointer :
     CYOwned
 {
     Type_privateData *type_;
+    size_t length_;
 
-    Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type) :
+    Pointer(void *value, JSContextRef context, JSObjectRef owner, sig::Type *type, size_t length = _not(size_t)) :
         CYOwned(value, context, owner),
-        type_(new(pool_) Type_privateData(type))
+        type_(new(pool_) Type_privateData(type)),
+        length_(length)
     {
     }
 };
@@ -528,7 +541,7 @@ static size_t Nonce_(0);
 
 static JSValueRef $cyq(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     CYPool pool;
-    const char *name(apr_psprintf(pool, "%s%zu", CYPoolCString(pool, context, arguments[0]), Nonce_++));
+    const char *name(apr_psprintf(pool, "%s%"APR_SIZE_T_FMT"", CYPoolCString(pool, context, arguments[0]), Nonce_++));
     return CYCastJSValue(context, name);
 }
 
@@ -657,7 +670,7 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef
     return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
 } CYCatch }
 
-JSObjectRef CYMakePointer(JSContextRef context, void *pointer, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
+JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner) {
     Pointer *internal(new Pointer(pointer, context, owner, type));
     return JSObjectMake(context, Pointer_, internal);
 }
@@ -783,9 +796,14 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void
         CYFromFFI_(float, float)
         CYFromFFI_(double, double)
 
+        case sig::array_P:
+            if (void *pointer = data)
+                return CYMakePointer(context, pointer, type->data.data.size, type->data.data.type, NULL, owner);
+            else goto null;
+
         case sig::pointer_P:
             if (void *pointer = *reinterpret_cast<void **>(data))
-                return CYMakePointer(context, pointer, type->data.data.type, ffi, owner);
+                return CYMakePointer(context, pointer, _not(size_t), type->data.data.type, NULL, owner);
             else goto null;
 
         case sig::string_P:
@@ -805,8 +823,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void
                 if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner))
                     return value;
 
-            fprintf(stderr, "CYFromFFI(%c)\n", type->primitive);
-            _assert(false);
+            CYThrow("failed conversion from FFI format: '%c'\n", type->primitive);
     }
 }
 
@@ -908,52 +925,34 @@ static bool Index_(apr_pool_t *pool, JSContextRef context, Struct_privateData *i
     return true;
 }
 
-static JSValueRef Pointer_getIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef *exception) { CYTry {
+static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
+    CYPool pool;
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
-    Type_privateData *typical(internal->type_);
-
-    ffi_type *ffi(typical->GetFFI());
 
-    uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
-    base += ffi->size * index;
-
-    JSObjectRef owner(internal->GetOwner() ?: object);
-    return CYFromFFI(context, typical->type_, ffi, base, false, owner);
-} CYCatch }
+    if (JSStringIsEqual(property, length_))
+        return internal->length_ == _not(size_t) ? CYJSUndefined(context) : CYCastJSValue(context, internal->length_);
 
-static JSValueRef Pointer_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
-    CYPool pool;
-    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
     Type_privateData *typical(internal->type_);
 
     if (typical->type_ == NULL)
         return NULL;
 
     ssize_t offset;
-    if (!CYGetOffset(pool, context, property, offset))
+    if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+        offset = 0;
+    else if (!CYGetOffset(pool, context, property, offset))
         return NULL;
 
-    return Pointer_getIndex(context, object, offset, exception);
-}
-
-static JSValueRef Pointer_getProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) {
-    return Pointer_getIndex(context, object, 0, exception);
-}
-
-static bool Pointer_setIndex(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value, JSValueRef *exception) { CYTry {
-    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
-    Type_privateData *typical(internal->type_);
-
     ffi_type *ffi(typical->GetFFI());
 
     uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
-    base += ffi->size * index;
+    base += ffi->size * offset;
 
-    CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
-    return true;
+    JSObjectRef owner(internal->GetOwner() ?: object);
+    return CYFromFFI(context, typical->type_, ffi, base, false, owner);
 } CYCatch }
 
-static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
+static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
     CYPool pool;
     Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
     Type_privateData *typical(internal->type_);
@@ -962,20 +961,24 @@ static bool Pointer_setProperty(JSContextRef context, JSObjectRef object, JSStri
         return NULL;
 
     ssize_t offset;
-    if (!CYGetOffset(pool, context, property, offset))
+    if (JSStringIsEqualToUTF8CString(property, "$cyi"))
+        offset = 0;
+    else if (!CYGetOffset(pool, context, property, offset))
         return NULL;
 
-    return Pointer_setIndex(context, object, offset, value, exception);
-}
+    ffi_type *ffi(typical->GetFFI());
 
-static bool Pointer_setProperty_$cyi(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) {
-    return Pointer_setIndex(context, object, 0, value, exception);
-}
+    uint8_t *base(reinterpret_cast<uint8_t *>(internal->value_));
+    base += ffi->size * offset;
+
+    CYPoolFFI(NULL, context, typical->type_, ffi, base, value);
+    return true;
+} CYCatch }
 
 static JSValueRef Struct_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) {
     Struct_privateData *internal(reinterpret_cast<Struct_privateData *>(JSObjectGetPrivate(_this)));
     Type_privateData *typical(internal->type_);
-    return CYMakePointer(context, internal->value_, typical->type_, typical->ffi_, _this);
+    return CYMakePointer(context, internal->value_, _not(size_t), typical->type_, typical->ffi_, _this);
 }
 
 static JSValueRef Struct_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry {
@@ -1068,7 +1071,7 @@ static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef objec
 }
 
 static JSObjectRef CYMakeType(JSContextRef context, const char *type) {
-    Type_privateData *internal(new Type_privateData(NULL, type));
+    Type_privateData *internal(new Type_privateData(type));
     return JSObjectMake(context, Type_privateData::Class_, internal);
 }
 
@@ -1158,7 +1161,7 @@ static JSObjectRef Pointer_new(JSContextRef context, JSObjectRef object, size_t
     sig::Signature signature;
     sig::Parse(pool, &signature, type, &Structor_);
 
-    return CYMakePointer(context, value, signature.elements[0].type, NULL, NULL);
+    return CYMakePointer(context, value, _not(size_t), signature.elements[0].type, NULL, NULL);
 } CYCatch }
 
 static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
@@ -1224,7 +1227,7 @@ static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef obje
     }
 
     void *value(malloc(internal->GetFFI()->size));
-    return CYMakePointer(context, value, type, NULL, NULL);
+    return CYMakePointer(context, value, _not(size_t), type, NULL, NULL);
 } CYCatch }
 
 static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
@@ -1245,13 +1248,24 @@ static JSValueRef CYValue_callAsFunction_toJSON(JSContextRef context, JSObjectRe
 }
 
 static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
-CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
+    CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
     char string[32];
     sprintf(string, "%p", internal->value_);
-
     return CYCastJSValue(context, string);
 } CYCatch }
 
+static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+    Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(_this)));
+    if (internal->length_ != _not(size_t))
+        // XXX: maybe dynamically look up Array.toCYON?
+        return Array_callAsFunction_toCYON(context, object, _this, count, arguments, exception);
+    else {
+        char string[32];
+        sprintf(string, "%p", internal->value_);
+        return CYCastJSValue(context, string);
+    }
+} CYCatch }
+
 static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
     Type_privateData *internal(reinterpret_cast<Type_privateData *>(JSObjectGetPrivate(_this)));
     CYPool pool;
@@ -1277,13 +1291,8 @@ static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef o
     return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
 }
 
-static JSStaticValue Pointer_staticValues[2] = {
-    {"$cyi", &Pointer_getProperty_$cyi, &Pointer_setProperty_$cyi, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
-    {NULL, NULL, NULL, 0}
-};
-
 static JSStaticFunction Pointer_staticFunctions[4] = {
-    {"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+    {"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
     {NULL, NULL, 0}
@@ -1437,8 +1446,18 @@ CYPoolError::CYPoolError(const char *format, va_list args) {
     message_ = apr_pvsprintf(pool_, format, args);
 }
 
+JSValueRef CYCastJSError(JSContextRef context, const char *message) {
+    JSValueRef arguments[1] = {CYCastJSValue(context, message)};
+
+    JSValueRef exception(NULL);
+    JSValueRef value(JSObjectCallAsConstructor(context, Error_, 1, arguments, &exception));
+    CYThrow(context, exception);
+
+    return value;
+}
+
 JSValueRef CYPoolError::CastJSValue(JSContextRef context) const {
-    return CYCastJSValue(context, message_);
+    return CYCastJSError(context, message_);
 }
 
 CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
@@ -1452,11 +1471,7 @@ CYJSError::CYJSError(JSContextRef context, const char *format, ...) {
     const char *message(apr_pvsprintf(pool, format, args));
     va_end (args);
 
-    JSValueRef arguments[1] = {CYCastJSValue(context, CYJSString(message))};
-
-    JSValueRef exception(NULL);
-    value_ = JSObjectCallAsConstructor(context, Error_, 1, arguments, &exception);
-    CYThrow(context, exception);
+    value_ = CYCastJSError(context, message);
 }
 
 JSGlobalContextRef CYGetJSContext() {
@@ -1474,7 +1489,6 @@ JSGlobalContextRef CYGetJSContext() {
 
         definition = kJSClassDefinitionEmpty;
         definition.className = "Pointer";
-        definition.staticValues = Pointer_staticValues;
         definition.staticFunctions = Pointer_staticFunctions;
         definition.getProperty = &Pointer_getProperty;
         definition.setProperty = &Pointer_setProperty;