X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/284f434e679db9480ba276b2e98d3fe5aeffa23b..96d36aaa89e579a53bd5848c22a462ca8eb13f80:/Execute.cpp diff --git a/Execute.cpp b/Execute.cpp index 3f00a87..b92b478 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -144,18 +144,6 @@ CYUTF16String CYCastUTF16String(JSStringRef value) { return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); } -const char *CYPoolCString(CYPool &pool, CYUTF8String utf8) { - return pool.strndup(utf8.data, utf8.size); -} - -CYUTF8String CYPoolUTF8String(CYPool &pool, CYUTF8String utf8) { - return {pool.strndup(utf8.data, utf8.size), utf8.size}; -} - -_visible CYUTF8String CYPoolUTF8String(CYPool &pool, const std::string &value) { - return {pool.strndup(value.data(), value.size()), value.size()}; -} - CYUTF8String CYPoolUTF8String(CYPool &pool, JSContextRef context, JSStringRef value) { return CYPoolUTF8String(pool, CYCastUTF16String(value)); } @@ -198,6 +186,7 @@ JSStringRef constructor_s; JSStringRef cy_s; JSStringRef cyi_s; JSStringRef cyt_s; +JSStringRef cyt__s; JSStringRef length_s; JSStringRef message_s; JSStringRef name_s; @@ -217,6 +206,8 @@ static JSStringRef Result_; void CYFinalize(JSObjectRef object) { CYData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + if (internal == NULL) + return; _assert(internal->count_ != _not(unsigned)); if (--internal->count_ == 0) delete internal; @@ -669,6 +660,24 @@ bool CYGetOffset(CYPool &pool, JSContextRef context, JSStringRef value, ssize_t return CYGetOffset(CYPoolCString(pool, context, value), index); } +// XXX: this is a horrible back added for CFType +void *CYCastPointerEx_(JSContextRef context, JSObjectRef value) { + JSObjectRef object((JSObjectRef) value); + if (JSValueIsObjectOfClass(context, value, CYPrivate::Class_)) { + Pointer *internal(reinterpret_cast(JSObjectGetPrivate(object))); + return internal->value_; + } + + JSValueRef toPointer(CYGetProperty(context, object, toPointer_s)); + if (CYIsCallable(context, toPointer)) { + JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL)); + _assert(value != NULL); + return CYCastPointer_(context, value); + } + + return NULL; +} + void *CYCastPointer_(JSContextRef context, JSValueRef value, bool *guess) { if (value == NULL) return NULL; @@ -750,12 +759,13 @@ void Primitive::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, CYJSString script(context, value); auto string(CYCastUTF16String(script)); _assert(string.size == 1); + _assert((string.data[0] & 0xff) == string.data[0]); *reinterpret_cast(data) = string.data[0]; } } void Void::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { - _assert(false); + _assert(JSValueIsUndefined(context, value)); } void Unknown::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void *data, JSValueRef value) const { @@ -853,31 +863,39 @@ void Function::PoolFFI(CYPool *pool, JSContextRef context, ffi_type *ffi, void * _assert(false); } +// XXX: this code is getting worse, not better :/ + #define CYFromFFI_(Type_) \ template <> \ JSValueRef Primitive::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \ JSValueRef value(CYCastJSValue(context, *reinterpret_cast(data))); \ JSObjectRef typed(_jsccall(JSObjectCallAsConstructor, context, CYGetCachedObject(context, CYJSString("Number")), 1, &value)); \ - CYSetProperty(context, typed, cyt_s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \ + CYSetProperty(context, typed, cyt__s, CYMakeType(context, *this), kJSPropertyAttributeDontEnum); \ return typed; \ } +#define CYFromFFI_2(Type_) \ +template <> \ +JSValueRef Primitive::FromFFI(JSContextRef context, ffi_type *ffi, void *data, bool initialize, JSObjectRef owner) const { \ + return CYCastJSValue(context, *reinterpret_cast(data)); \ +} + CYFromFFI_(wchar_t) CYFromFFI_(float) CYFromFFI_(double) CYFromFFI_(long double) -CYFromFFI_(signed char) -CYFromFFI_(signed int) +CYFromFFI_2(signed char) +CYFromFFI_2(signed int) CYFromFFI_(signed long int) CYFromFFI_(signed long long int) -CYFromFFI_(signed short int) +CYFromFFI_2(signed short int) -CYFromFFI_(unsigned char) -CYFromFFI_(unsigned int) +CYFromFFI_2(unsigned char) +CYFromFFI_2(unsigned int) CYFromFFI_(unsigned long int) CYFromFFI_(unsigned long long int) -CYFromFFI_(unsigned short int) +CYFromFFI_2(unsigned short int) #ifdef __SIZEOF_INT128__ CYFromFFI_(signed __int128) @@ -955,8 +973,7 @@ void CYExecuteClosure(ffi_cif *cif, void *result, void **arguments, void *arg) { values[index] = internal->signature_.elements[1 + index].type->FromFFI(context, internal->cif_.arg_types[index], arguments[index]); JSValueRef value(internal->adapter_(context, count, values, internal->function_)); - if (internal->cif_.rtype != &ffi_type_void) - internal->signature_.elements[0].type->PoolFFI(NULL, context, internal->cif_.rtype, result, value); + internal->signature_.elements[0].type->PoolFFI(NULL, context, internal->cif_.rtype, result, value); } static JSValueRef FunctionAdapter_(JSContextRef context, size_t count, JSValueRef values[], JSObjectRef function) { @@ -1253,7 +1270,10 @@ static sig::Type *CYGetType(CYPool &pool, JSContextRef context, JSValueRef value if (JSValueIsNull(context, value)) return &PointerToVoid_; JSObjectRef object(CYCastJSObject(context, value)); - JSObjectRef type(CYCastJSObject(context, CYGetProperty(context, object, cyt_s))); + JSValueRef check(CYGetProperty(context, object, cyt_s)); + if (JSValueIsUndefined(context, check)) + CYThrow("could not infer type of argument '%s'", CYPoolCString(pool, context, value)); + JSObjectRef type(CYCastJSObject(context, check)); _assert(JSValueIsObjectOfClass(context, type, CYPrivate::Class_)); Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(type))); return internal->type_; @@ -1987,9 +2007,10 @@ static JSStaticValue Struct_staticValues[2] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction Functor_staticFunctions[4] = { +static JSStaticFunction Functor_staticFunctions[5] = { {"$cya", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &Functor_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"toPointer", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"valueOf", &Functor_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {NULL, NULL, 0} }; @@ -2104,11 +2125,11 @@ _visible const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String } } -#ifndef __ANDROID__ _visible void CYCancel() { +#ifndef __ANDROID__ cancel_ = true; -} #endif +} const char *CYPoolLibraryPath(CYPool &pool); @@ -2129,6 +2150,7 @@ void CYInitializeDynamic() { JSClassDefinition definition; definition = kJSClassDefinitionEmpty; + definition.attributes = kJSClassAttributeNoAutomaticPrototype; definition.className = "All"; definition.staticFunctions = All_staticFunctions; definition.hasProperty = &All_hasProperty; @@ -2137,6 +2159,7 @@ void CYInitializeDynamic() { All_ = JSClassCreate(&definition); definition = kJSClassDefinitionEmpty; + definition.attributes = kJSClassAttributeNoAutomaticPrototype; definition.className = "Context"; definition.finalize = &CYFinalize; CYPrivate::Class_ = JSClassCreate(&definition); @@ -2211,6 +2234,7 @@ void CYInitializeDynamic() { cy_s = JSStringCreateWithUTF8CString("$cy"); cyi_s = JSStringCreateWithUTF8CString("$cyi"); cyt_s = JSStringCreateWithUTF8CString("$cyt"); + cyt__s = JSStringCreateWithUTF8CString("$cyt_"); length_s = JSStringCreateWithUTF8CString("length"); message_s = JSStringCreateWithUTF8CString("message"); name_s = JSStringCreateWithUTF8CString("name");