X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/f1b5a47ffa92e12d17571d350a567e2035d75039..84759b5be9e3925e184005b0be902a8be25b4584:/Execute.cpp diff --git a/Execute.cpp b/Execute.cpp index 87e7de2..eb1970e 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -22,6 +22,9 @@ #include "Internal.hpp" #include +#include +#include +#include #include "cycript.hpp" @@ -32,6 +35,7 @@ #include "Execute.hpp" #include +#include #include #include @@ -42,6 +46,7 @@ #include "Parser.hpp" +#include "Decode.hpp" #include "Error.hpp" #include "JavaScript.hpp" #include "String.hpp" @@ -451,18 +456,21 @@ static JSValueRef Array_callAsFunction_toCYON(JSContextRef context, JSObjectRef bool comma(false); for (size_t index(0), count(CYCastDouble(context, length)); index != count; ++index) { - JSValueRef value(CYGetProperty(context, _this, index)); - if (comma) str << ','; else comma = true; - if (!JSValueIsUndefined(context, value)) - str << CYPoolCCYON(pool, context, value); - else { - str << ','; - comma = false; + try { + JSValueRef value(CYGetProperty(context, _this, index)); + if (!JSValueIsUndefined(context, value)) + str << CYPoolCCYON(pool, context, value); + else { + str << ','; + comma = false; + } + } catch (const CYException &error) { + str << "@error"; } } @@ -951,8 +959,8 @@ static JSValueRef Functor_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue()); } CYCatch(NULL) } -JSObjectRef CYMakeType(JSContextRef context, const char *type) { - Type_privateData *internal(new Type_privateData(type)); +JSObjectRef CYMakeType(JSContextRef context, const char *encoding) { + Type_privateData *internal(new Type_privateData(encoding)); return JSObjectMake(context, Type_privateData::Class_, internal); } @@ -961,6 +969,19 @@ JSObjectRef CYMakeType(JSContextRef context, sig::Type *type) { return JSObjectMake(context, Type_privateData::Class_, internal); } +JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature) { + CYPool pool; + + sig::Type type; + type.name = NULL; + type.flags = 0; + + type.primitive = sig::function_P; + sig::Copy(pool, type.data.signature, *signature); + + return CYMakeType(context, &type); +} + static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { JSObjectRef global(CYGetGlobalObject(context)); JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); @@ -1079,6 +1100,38 @@ static JSValueRef Type_callAsFunction_constant(JSContextRef context, JSObjectRef return CYMakeType(context, &type); } CYCatch(NULL) } +static JSValueRef Type_callAsFunction_functionWith(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); + + CYPool pool; + + sig::Type type; + type.name = NULL; + type.flags = 0; + + type.primitive = sig::function_P; + type.data.signature.elements = new(pool) sig::Element[1 + count]; + type.data.signature.count = 1 + count; + + type.data.signature.elements[0].name = NULL; + type.data.signature.elements[0].type = internal->type_; + type.data.signature.elements[0].offset = _not(size_t); + + for (size_t i(0); i != count; ++i) { + sig::Element &element(type.data.signature.elements[i + 1]); + element.name = NULL; + element.offset = _not(size_t); + + JSObjectRef object(CYCastJSObject(context, arguments[i])); + _assert(JSValueIsObjectOfClass(context, object, Type_privateData::Class_)); + Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(object))); + + element.type = internal->type_; + } + + return CYMakeType(context, &type); +} CYCatch(NULL) } + static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { if (count != 0) throw CYJSError(context, "incorrect number of arguments to Type.pointerTo"); @@ -1086,11 +1139,18 @@ static JSValueRef Type_callAsFunction_pointerTo(JSContextRef context, JSObjectRe sig::Type type; type.name = NULL; - type.flags = 0; - type.primitive = sig::pointer_P; - type.data.data.type = internal->type_; - type.data.data.size = 0; + if (internal->type_->primitive == sig::char_P) { + type.flags = internal->type_->flags; + type.primitive = sig::string_P; + type.data.data.type = NULL; + type.data.data.size = 0; + } else { + type.flags = 0; + type.primitive = sig::pointer_P; + type.data.data.type = internal->type_; + type.data.data.size = 0; + } return CYMakeType(context, &type); } CYCatch(NULL) } @@ -1180,8 +1240,7 @@ static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRe static JSValueRef Functor_getProperty_type(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { cy::Functor *internal(reinterpret_cast(JSObjectGetPrivate(object))); - CYPool pool; - return CYCastJSValue(context, Unparse(pool, &internal->signature_)); + return CYMakeType(context, &internal->signature_); } CYCatch(NULL) } static JSValueRef Type_getProperty_alignment(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { @@ -1208,12 +1267,12 @@ static JSValueRef Type_callAsFunction_toString(JSContextRef context, JSObjectRef static JSValueRef Type_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { Type_privateData *internal(reinterpret_cast(JSObjectGetPrivate(_this))); - CYPool pool; - const char *type(sig::Unparse(pool, internal->type_)); - std::ostringstream str; - CYStringify(str, type, strlen(type)); - char *cyon(pool.strcat("new Type(", str.str().c_str(), ")", NULL)); - return CYCastJSValue(context, CYJSString(cyon)); + CYLocalPool pool; + std::ostringstream out; + CYOptions options; + CYOutput output(out, options); + (new(pool) CYEncodedType(Decode(pool, internal->type_)))->Output(output, CYNoFlags); + return CYCastJSValue(context, CYJSString(out.str().c_str())); } CYCatch(NULL) } static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { @@ -1255,9 +1314,10 @@ static JSStaticValue Type_staticValues[4] = { {NULL, NULL, NULL, 0} }; -static JSStaticFunction Type_staticFunctions[8] = { +static JSStaticFunction Type_staticFunctions[9] = { {"arrayOf", &Type_callAsFunction_arrayOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"constant", &Type_callAsFunction_constant, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {"functionWith", &Type_callAsFunction_functionWith, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"pointerTo", &Type_callAsFunction_pointerTo, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"withName", &Type_callAsFunction_withName, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toCYON", &Type_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, @@ -1291,9 +1351,8 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) { return JSContextGetGlobalObject(context); } -const char *CYExecute(CYPool &pool, CYUTF8String code) { - JSContextRef context(CYGetJSContext()); - JSValueRef exception(NULL), result; +const char *CYExecute(JSContextRef context, CYPool &pool, CYUTF8String code) { + JSValueRef exception(NULL); void *handle; if (hooks_ != NULL && hooks_->ExecuteStart != NULL) @@ -1301,8 +1360,9 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) { else handle = NULL; - const char *json; + try { + JSValueRef result; try { result = JSEvaluateScript(context, CYJSString(code), NULL, NULL, 0, &exception); } catch (const char *error) { @@ -1315,6 +1375,7 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) { if (JSValueIsUndefined(context, result)) return NULL; + const char *json; try { json = CYPoolCCYON(pool, context, result, &exception); } catch (const char *error) { @@ -1326,9 +1387,13 @@ const char *CYExecute(CYPool &pool, CYUTF8String code) { CYSetProperty(context, CYGetGlobalObject(context), Result_, result); - if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) - (*hooks_->ExecuteEnd)(context, handle); return json; + + } catch (...) { + if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) + (*hooks_->ExecuteEnd)(context, handle); + throw; + } } extern "C" void CydgetSetupContext(JSGlobalContextRef context) { @@ -1459,6 +1524,66 @@ JSGlobalContextRef CYGetJSContext(JSContextRef context) { return reinterpret_cast(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; } +extern "C" bool CydgetMemoryParse(const uint16_t **data, size_t *size); + +void *CYMapFile(const char *path, size_t *psize) { + int fd; + _syscall(fd = open(path, O_RDONLY)); + + struct stat stat; + _syscall(fstat(fd, &stat)); + size_t size(stat.st_size); + + *psize = size; + + void *base; + _syscall(base = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0)); + + _syscall(close(fd)); + return base; +} + +static void CYRunSetups(JSContextRef context) { + std::string folder("/etc/cycript/setup.d"); + DIR *setups(opendir(folder.c_str())); + if (setups == NULL) + return; + + for (;;) { + dirent setup; + dirent *result; + _syscall(readdir_r(setups, &setup, &result)); + + if (result == NULL) + break; + _assert(result == &setup); + + const char *name(setup.d_name); + size_t length(strlen(name)); + if (length < 4) + continue; + + if (name[0] == '.') + continue; + if (memcmp(name + length - 3, ".cy", 3) != 0) + continue; + + std::string script(folder + "/" + name); + CYUTF8String utf8; + utf8.data = reinterpret_cast(CYMapFile(script.c_str(), &utf8.size)); + + CYPool pool; + CYUTF16String utf16(CYPoolUTF16String(pool, utf8)); + munmap(const_cast(utf8.data), utf8.size); + + if (CydgetMemoryParse(&utf16.data, &utf16.size)) + CYExecute(context, pool, CYPoolUTF8String(pool, utf16)); + free(const_cast(utf16.data)); + } + + _syscall(closedir(setups)); +} + extern "C" void CYSetupContext(JSGlobalContextRef context) { CYInitializeDynamic(); @@ -1548,6 +1673,8 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { (*hooks_->SetupContext)(context); CYArrayPush(context, alls, cycript); + + CYRunSetups(context); } JSGlobalContextRef CYGetJSContext() {