]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Allow the size of array typedefs to be a variable.
[cycript.git] / Execute.cpp
index 11b54f97e2902d87b31dbd7f1380578a211838de..48dc4780d8924538f8ac01af90bef4ac5647a57b 100644 (file)
@@ -332,6 +332,7 @@ JSValueRef CYCastJSValue(JSContextRef context, double value) {
         return JSValueMakeNumber(context, static_cast<double>(value)); \
     }
 
+CYCastJSValue_(long double)
 CYCastJSValue_(signed short int)
 CYCastJSValue_(unsigned short int)
 CYCastJSValue_(signed int)
@@ -716,14 +717,17 @@ void Primitive<Type_>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi
     *reinterpret_cast<Type_ *>(data) = CYCastDouble(context, value); \
 }
 
-CYPoolFFI_(char)
-CYPoolFFI_(double)
+CYPoolFFI_(wchar_t)
 CYPoolFFI_(float)
+CYPoolFFI_(double)
+CYPoolFFI_(long double)
+
 CYPoolFFI_(signed char)
 CYPoolFFI_(signed int)
 CYPoolFFI_(signed long int)
 CYPoolFFI_(signed long long int)
 CYPoolFFI_(signed short int)
+
 CYPoolFFI_(unsigned char)
 CYPoolFFI_(unsigned int)
 CYPoolFFI_(unsigned long int)
@@ -735,6 +739,18 @@ CYPoolFFI_(signed __int128)
 CYPoolFFI_(unsigned __int128)
 #endif
 
+template <>
+void Primitive<char>::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
+    if (JSValueGetType(context, value) != kJSTypeString)
+        *reinterpret_cast<char *>(data) = CYCastDouble(context, value);
+    else {
+        CYJSString script(context, value);
+        auto string(CYCastUTF16String(script));
+        _assert(string.size == 1);
+        *reinterpret_cast<char *>(data) = string.data[0];
+    }
+}
+
 void Void::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const {
     _assert(false);
 }
@@ -841,14 +857,17 @@ JSValueRef Primitive<Type_>::FromFFI(JSContextRef context, ffi_type *ffi, void *
 }
 
 CYFromFFI_(bool)
-CYFromFFI_(char)
-CYFromFFI_(double)
+CYFromFFI_(wchar_t)
 CYFromFFI_(float)
+CYFromFFI_(double)
+CYFromFFI_(long double)
+
 CYFromFFI_(signed char)
 CYFromFFI_(signed int)
 CYFromFFI_(signed long int)
 CYFromFFI_(signed long long int)
 CYFromFFI_(signed short int)
+
 CYFromFFI_(unsigned char)
 CYFromFFI_(unsigned int)
 CYFromFFI_(unsigned long int)
@@ -860,6 +879,16 @@ CYFromFFI_(signed __int128)
 CYFromFFI_(unsigned __int128)
 #endif
 
+template <>
+JSValueRef Primitive<char>::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
+    uint16_t string(uint8_t(*reinterpret_cast<char *>(data)));
+    JSValueRef value(CYCastJSValue(context, CYJSString(CYUTF16String(&string, 1))));
+    JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("String")), 1, &value));
+    CYSetProperty(context, typed, cyt_s, CYMakeType(context, sig::Primitive<char>()), kJSPropertyAttributeDontEnum);
+    CYSetPrototype(context, typed, CYGetCachedValue(context, CYJSString("Character_prototype")));
+    return typed;
+}
+
 JSValueRef Void::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const {
     return CYJSUndefined(context);
 }
@@ -1005,8 +1034,8 @@ static JSValueRef CString_getProperty(JSContextRef context, JSObjectRef object,
     else if (!CYGetOffset(pool, context, property, offset))
         return NULL;
 
-    uint16_t value(uint8_t(internal->value_[offset]));
-    return CYCastJSValue(context, CYJSString(CYUTF16String(&value, 1)));
+    sig::Primitive<char> type;
+    return type.FromFFI(context, type.GetFFI(pool), internal->value_ + offset, false, NULL);
 } CYCatch(NULL) }
 
 static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef value, JSValueRef *exception) { CYTry {
@@ -1019,9 +1048,8 @@ static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStri
     else if (!CYGetOffset(pool, context, property, offset))
         return false;
 
-    auto data(CYCastUTF16String(CYJSString(context, value)));
-    _assert(data.size == 1);
-    internal->value_[offset] = data.data[0];
+    sig::Primitive<char> type;
+    type.PoolFFI(NULL, context, type.GetFFI(pool), internal->value_ + offset, value);
     return true;
 } CYCatch(false) }
 
@@ -1666,7 +1694,14 @@ static JSValueRef Type_callAsFunction(JSContextRef context, JSObjectRef object,
 
     sig::Type *type(internal->type_);
     ffi_type *ffi(internal->GetFFI());
-    void *data(buffer->malloc<void>(ffi->size, ffi->alignment));
+
+    void *data;
+    if (_this == NULL || CYIsStrictEqual(context, _this, CYGetGlobalObject(context)))
+        data = buffer->malloc<void>(ffi->size, ffi->alignment);
+    else {
+        CYSetProperty(context, buffer, CYJSString("$cyo"), _this, kJSPropertyAttributeDontEnum);
+        data = CYCastPointer<void *>(context, _this);
+    }
 
     type->PoolFFI(buffer, context, ffi, data, arguments[0]);
     JSValueRef value(type->FromFFI(context, ffi, data, false, buffer));
@@ -2460,7 +2495,17 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
     CYSetProperty(context, cycript, CYJSString("Functor"), Functor);
 
     CYSetProperty(context, cycript, CYJSString("Pointer"), JSObjectMakeConstructor(context, CYPrivate<Pointer>::Class_, &Pointer_new));
-    CYSetProperty(context, cycript, CYJSString("Type"), JSObjectMakeConstructor(context, CYPrivate<Type_privateData>::Class_, &Type_new));
+
+    JSObjectRef Type(JSObjectMakeConstructor(context, CYPrivate<Type_privateData>::Class_, &Type_new));
+    JSObjectRef Type_prototype(CYCastJSObject(context, CYGetProperty(context, Type, prototype_s)));
+    CYSetPrototype(context, Type_prototype, Function_prototype);
+    CYSetProperty(context, cy, CYJSString("Type_prototype"), Type_prototype);
+    CYSetProperty(context, cycript, CYJSString("Type"), Type);
+
+    JSObjectRef Character_prototype(JSObjectMake(context, NULL, NULL));
+    CYSetPrototype(context, Character_prototype, String_prototype);
+    CYSetProperty(context, cy, CYJSString("Character_prototype"), Character_prototype);
+    CYSetProperty(context, Character_prototype, CYJSString("valueOf"), _jsccall(JSEvaluateScript, context, CYJSString("(function(){return this.charCodeAt(0);})"), NULL, NULL, 0));
 
     JSObjectRef modules(JSObjectMake(context, NULL, NULL));
     CYSetProperty(context, cy, CYJSString("modules"), modules);
@@ -2527,6 +2572,7 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) {
 
     CYSetProperty(context, cache, CYJSString("float"), CYMakeType(context, sig::Primitive<float>()), kJSPropertyAttributeDontEnum);
     CYSetProperty(context, cache, CYJSString("double"), CYMakeType(context, sig::Primitive<double>()), kJSPropertyAttributeDontEnum);
+    CYSetProperty(context, cache, CYJSString("longdouble"), CYMakeType(context, sig::Primitive<long double>()), kJSPropertyAttributeDontEnum);
 
     CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum);