X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/6447ef4983dec9dee9ce857d78822a2229edd449..278e0646badec22a6a49e09cee07078b70e3017c:/Execute.cpp diff --git a/Execute.cpp b/Execute.cpp index 1644bb1..a145ee0 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -44,8 +44,7 @@ #include #include -#include "Parser.hpp" - +#include "Code.hpp" #include "Decode.hpp" #include "Error.hpp" #include "JavaScript.hpp" @@ -1686,8 +1685,9 @@ JSGlobalContextRef CYGetJSContext(JSContextRef 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)); + int fd(_syscall_(open(path, O_RDONLY), 1, {ENOENT})); + if (fd == -1) + return NULL; struct stat stat; _syscall(fstat(fd, &stat)); @@ -1717,7 +1717,9 @@ static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef CYJSString property("exports"); JSObjectRef module; - const char *path(pool.strcat(lib, "/cycript0.9/", CYPoolCString(pool, context, arguments[0]), ".cy", NULL)); + const char *name(CYPoolCString(pool, context, arguments[0])); + const char *path(pool.strcat(lib, "/cycript0.9/", name, ".cy", NULL)); + CYJSString key(path); JSObjectRef modules(CYGetCachedObject(context, CYJSString("modules"))); JSValueRef cache(CYGetProperty(context, modules, key)); @@ -1725,18 +1727,28 @@ static JSValueRef require(JSContextRef context, JSObjectRef object, JSObjectRef if (!JSValueIsUndefined(context, cache)) module = CYCastJSObject(context, cache); else { + CYUTF8String code; + code.data = reinterpret_cast(CYMapFile(path, &code.size)); + + if (code.data == NULL) { + if (strchr(name, '/') == NULL && ( + dlopen(pool.strcat("/System/Library/Frameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL || + dlopen(pool.strcat("/System/Library/PrivateFrameworks/", name, ".framework/", name, NULL), RTLD_LAZY | RTLD_GLOBAL) != NULL || + false)) + return CYJSUndefined(NULL); + + CYThrow("Can't find module: %s", name); + } + module = JSObjectMake(context, NULL, NULL); CYSetProperty(context, modules, key, module); JSObjectRef exports(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, module, property, exports); - CYUTF8String code; - code.data = reinterpret_cast(CYMapFile(path, &code.size)); - std::stringstream wrap; wrap << "(function (exports, require, module) { " << code << "\n});"; - code = CYPoolCode(pool, wrap.str().c_str()); + code = CYPoolCode(pool, wrap); JSValueRef value(_jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0)); JSObjectRef function(CYCastJSObject(context, value));