]> git.saurik.com Git - cycript.git/blobdiff - Execute.cpp
Maintain type flags in Type subclass Copy() logic.
[cycript.git] / Execute.cpp
index 70fce727cdd3362a1bbc04c8bc3a25c11be0b171..3399c86fb644db7d9022db7729245bb0d0056253 100644 (file)
@@ -181,11 +181,12 @@ static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[
 JSObjectRef CYObjectMakeArray(JSContextRef context, size_t length, const JSValueRef values[]) {
     if (JSObjectMakeArray$ != NULL)
         return _jsccall(*JSObjectMakeArray$, context, length, values);
-    else {
-        JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array")));
-        JSValueRef value(CYCallAsFunction(context, Array, NULL, length, values));
-        return CYCastJSObject(context, value);
-    }
+    JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array")));
+    bool wat(length == 1 && JSValueGetType(context, values[0]) == kJSTypeNumber);
+    JSValueRef value(CYCallAsFunction(context, Array, NULL, wat ? 0 : length, values));
+    JSObjectRef object(CYCastJSObject(context, value));
+    if (wat) CYArrayPush(context, object, 1, values);
+    return object;
 }
 
 static JSClassRef All_;
@@ -332,6 +333,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 +718,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 +740,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 +858,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 +880,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,7 +1035,8 @@ static JSValueRef CString_getProperty(JSContextRef context, JSObjectRef object,
     else if (!CYGetOffset(pool, context, property, offset))
         return NULL;
 
-    return CYCastJSValue(context, CYJSString(CYUTF8String(&internal->value_[offset], 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 {
@@ -1018,8 +1049,8 @@ static bool CString_setProperty(JSContextRef context, JSObjectRef object, JSStri
     else if (!CYGetOffset(pool, context, property, offset))
         return false;
 
-    const char *data(CYPoolCString(pool, context, value));
-    internal->value_[offset] = *data;
+    sig::Primitive<char> type;
+    type.PoolFFI(NULL, context, type.GetFFI(pool), internal->value_ + offset, value);
     return true;
 } CYCatch(false) }
 
@@ -1664,7 +1695,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));
@@ -2458,7 +2496,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);
@@ -2525,6 +2573,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);