X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/0f557ff24f280bfb7c04372baa53172eb576071d..fddfdb6f5afd415d4f8b9b6f74aad2cd1180094a:/Execute.cpp diff --git a/Execute.cpp b/Execute.cpp index e575f94..d27183c 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -36,6 +36,8 @@ #include #include +#include + #include "sig/parse.hpp" #include "sig/ffi_type.hpp" @@ -48,6 +50,16 @@ #include "Pooling.hpp" #include "String.hpp" +const char *sqlite3_column_string(sqlite3_stmt *stmt, int n) { + return reinterpret_cast(sqlite3_column_text(stmt, n)); +} + +char *sqlite3_column_pooled(CYPool &pool, sqlite3_stmt *stmt, int n) { + if (const char *value = sqlite3_column_string(stmt, n)) + return pool.strdup(value); + else return NULL; +} + static std::vector &GetHooks() { static std::vector hooks; return hooks; @@ -142,6 +154,18 @@ size_t CYGetIndex(CYPool &pool, JSContextRef context, JSStringRef value) { } /* }}} */ +static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); + +static 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); + } +} + static JSClassRef All_; static JSClassRef Context_; static JSClassRef CString_; @@ -166,6 +190,8 @@ JSStringRef toPointer_s; JSStringRef toString_s; JSStringRef weak_s; +static sqlite3 *database_; + static JSStringRef Result_; void CYFinalize(JSObjectRef object) { @@ -190,28 +216,7 @@ void Structor_(CYPool &pool, sig::Type *&type) { if (type->primitive != sig::struct_P || type->name == NULL) return; - size_t length(strlen(type->name)); - char keyed[length + 2]; - memcpy(keyed + 1, type->name, length + 1); - - static const char *modes = "34"; - for (size_t i(0); i != 2; ++i) { - char mode(modes[i]); - keyed[0] = mode; - - if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) - switch (mode) { - case '3': - sig::Parse(pool, &type->data.signature, entry->value_, &Structor_); - break; - - case '4': { - sig::Signature signature; - sig::Parse(pool, &signature, entry->value_, &Structor_); - type = signature.elements[0].type; - } break; - } - } + //_assert(false); } JSClassRef Type_privateData::Class_; @@ -248,6 +253,13 @@ struct Pointer : length_(length) { } + + Pointer(void *value, JSContextRef context, JSObjectRef owner, size_t length, const char *encoding) : + CYOwned(value, context, owner), + type_(new(*pool_) Type_privateData(encoding)), + length_(length) + { + } }; struct Struct_privateData : @@ -360,13 +372,52 @@ JSValueRef CYArrayGet(JSContextRef context, JSObjectRef array, size_t index) { return _jsccall(JSObjectGetPropertyAtIndex, context, array, index); } -void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { - JSValueRef arguments[1]; - arguments[0] = value; +void CYArrayPush(JSContextRef context, JSObjectRef array, size_t length, const JSValueRef arguments[]) { JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array_prototype"))); - _jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, 1, arguments); + _jsccall(JSObjectCallAsFunction, context, CYCastJSObject(context, CYGetProperty(context, Array, push_s)), array, length, arguments); +} + +void CYArrayPush(JSContextRef context, JSObjectRef array, JSValueRef value) { + return CYArrayPush(context, array, 1, &value); } +template +class CYArrayBuilder { + private: + JSContextRef context_; + JSObjectRef &array_; + size_t size_; + JSValueRef values_[Size_]; + + void flush() { + if (array_ == NULL) + array_ = CYObjectMakeArray(context_, size_, values_); + else + CYArrayPush(context_, array_, size_, values_); + } + + public: + CYArrayBuilder(JSContextRef context, JSObjectRef &array) : + context_(context), + array_(array), + size_(0) + { + } + + ~CYArrayBuilder() { + flush(); + } + + void operator ()(JSValueRef value) { + if (size_ == Size_) { + flush(); + size_ = 0; + } + + values_[size_++] = value; + } +}; + static JSValueRef System_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { FILE *file(stdout); @@ -576,6 +627,11 @@ JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, si return JSObjectMake(context, Pointer_, internal); } +JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, const char *encoding, JSObjectRef owner) { + Pointer *internal(new Pointer(pointer, context, owner, length, encoding)); + return JSObjectMake(context, Pointer_, internal); +} + JSObjectRef CYMakeCString(JSContextRef context, char *pointer, JSObjectRef owner) { CString *internal(new CString(pointer, context, owner)); return JSObjectMake(context, CString_, internal); @@ -585,19 +641,12 @@ static JSObjectRef CYMakeFunctor(JSContextRef context, void (*function)(), const return JSObjectMake(context, Functor_, new cy::Functor(signature, function)); } -static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *encoding, void **cache) { - cy::Functor *internal; - if (*cache != NULL) - internal = reinterpret_cast(*cache); - else { - void (*function)()(reinterpret_cast(CYCastSymbol(symbol))); - if (function == NULL) - return NULL; - - internal = new cy::Functor(encoding, function); - *cache = internal; - } +static JSObjectRef CYMakeFunctor(JSContextRef context, const char *symbol, const char *encoding) { + void (*function)()(reinterpret_cast(CYCastSymbol(symbol))); + if (function == NULL) + return NULL; + cy::Functor *internal(new cy::Functor(encoding, function)); ++internal->count_; return JSObjectMake(context, Functor_, internal); } @@ -652,7 +701,7 @@ void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ff break; CYPoolFFI_(uchar, unsigned char) - CYPoolFFI_(char, char) + CYPoolFFI_(schar, signed char) CYPoolFFI_(ushort, unsigned short) CYPoolFFI_(short, short) CYPoolFFI_(ulong, unsigned long) @@ -727,6 +776,9 @@ void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ff case sig::void_P: break; + // XXX: implement a conversion from a single character string? + CYPoolFFI_(char, char) + default: for (CYHook *hook : GetHooks()) if (hook->PoolFFI != NULL) @@ -747,7 +799,7 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void return CYCastJSValue(context, *reinterpret_cast(data)); \ CYFromFFI_(uchar, unsigned char) - CYFromFFI_(char, char) + CYFromFFI_(schar, signed char) CYFromFFI_(ushort, unsigned short) CYFromFFI_(short, short) CYFromFFI_(ulong, unsigned long) @@ -779,6 +831,8 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void case sig::void_P: return CYJSUndefined(context); + CYFromFFI_(char, char) + null: return CYJSNull(context); default: @@ -932,10 +986,13 @@ static bool Index_(CYPool &pool, JSContextRef context, Struct_privateData *inter base: ffi_type **elements(typical->GetFFI()->elements); - base = reinterpret_cast(internal->value_); - for (ssize_t local(0); local != index; ++local) - base += elements[local]->size; + size_t offset(0); + for (ssize_t local(0); local != index; ++local) { + offset += elements[local]->size; + CYAlign(offset, elements[local + 1]->alignment); + } + base = reinterpret_cast(internal->value_) + offset; return true; } @@ -1101,8 +1158,8 @@ static JSValueRef Pointer_callAsFunction(JSContextRef context, JSObjectRef objec return CYCallAsFunction(context, functor, _this, count, arguments); } CYCatch(NULL) } -JSObjectRef CYMakeType(JSContextRef context, const char *encoding) { - Type_privateData *internal(new Type_privateData(encoding)); +JSObjectRef CYMakeType(JSContextRef context, sig::Primitive primitive) { + Type_privateData *internal(new Type_privateData(primitive)); return JSObjectMake(context, Type_privateData::Class_, internal); } @@ -1124,7 +1181,39 @@ JSObjectRef CYMakeType(JSContextRef context, sig::Signature *signature) { return CYMakeType(context, &type); } +extern "C" bool CYBridgeHash(CYPool &pool, CYUTF8String name, const char *&code, unsigned &flags) { + sqlite3_stmt *statement; + + _sqlcall(sqlite3_prepare(database_, + "select " + "\"cache\".\"code\", " + "\"cache\".\"flags\" " + "from \"cache\" " + "where" + " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM " and" + " \"cache\".\"name\" = ?" + " limit 1" + , -1, &statement, NULL)); + + _sqlcall(sqlite3_bind_text(statement, 1, name.data, name.size, SQLITE_STATIC)); + + bool success; + if (_sqlcall(sqlite3_step(statement)) == SQLITE_DONE) + success = false; + else { + success = true; + code = sqlite3_column_pooled(pool, statement, 0); + flags = sqlite3_column_int(statement, 1); + } + + _sqlcall(sqlite3_finalize(statement)); + return success; +} + static bool All_hasProperty(JSContextRef context, JSObjectRef object, JSStringRef property) { + if (JSStringIsEqualToUTF8CString(property, "errno")) + return true; + JSObjectRef global(CYGetGlobalObject(context)); JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); @@ -1135,23 +1224,18 @@ static bool All_hasProperty(JSContextRef context, JSObjectRef object, JSStringRe return true; CYPool pool; - CYUTF8String name(CYPoolUTF8String(pool, context, property)); - - size_t length(name.size); - char keyed[length + 2]; - memcpy(keyed + 1, name.data, length + 1); - - static const char *modes = "0124"; - for (size_t i(0); i != 4; ++i) { - keyed[0] = modes[i]; - if (CYBridgeHash(keyed, length + 1) != NULL) - return true; - } + const char *code; + unsigned flags; + if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags)) + return true; return false; } static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSStringRef property, JSValueRef *exception) { CYTry { + if (JSStringIsEqualToUTF8CString(property, "errno")) + return CYCastJSValue(context, errno); + JSObjectRef global(CYGetGlobalObject(context)); JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); JSObjectRef alls(CYCastJSObject(context, CYGetProperty(context, cycript, CYJSString("alls")))); @@ -1163,44 +1247,76 @@ static JSValueRef All_getProperty(JSContextRef context, JSObjectRef object, JSSt return value; CYPool pool; - CYUTF8String name(CYPoolUTF8String(pool, context, property)); - - size_t length(name.size); - char keyed[length + 2]; - memcpy(keyed + 1, name.data, length + 1); - - static const char *modes = "0124"; - for (size_t i(0); i != 4; ++i) { - char mode(modes[i]); - keyed[0] = mode; - - if (CYBridgeEntry *entry = CYBridgeHash(keyed, length + 1)) - switch (mode) { - case '0': - return JSEvaluateScript(CYGetJSContext(context), CYJSString(entry->value_), NULL, NULL, 0, NULL); - - case '1': - return CYMakeFunctor(context, name.data, entry->value_, &entry->cache_); - - case '2': - if (void *symbol = CYCastSymbol(name.data)) { - // XXX: this is horrendously inefficient - sig::Signature signature; - sig::Parse(pool, &signature, entry->value_, &Structor_); - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); - return CYFromFFI(context, signature.elements[0].type, cif.rtype, symbol); - } else return NULL; - - // XXX: implement case 3 - case '4': - return CYMakeType(context, entry->value_); - } + const char *code; + unsigned flags; + if (CYBridgeHash(pool, CYPoolUTF8String(pool, context, property), code, flags)) { + CYUTF8String parsed; + + try { + parsed = CYPoolCode(pool, code); + } catch (const CYException &error) { + CYThrow("%s", pool.strcat("error caching ", CYPoolCString(pool, context, property), ": ", error.PoolCString(pool), NULL)); + } + + JSValueRef result(_jsccall(JSEvaluateScript, context, CYJSString(parsed), NULL, NULL, 0)); + + if (flags == 0) { + JSObjectRef cache(CYGetCachedObject(context, CYJSString("cache"))); + CYSetProperty(context, cache, property, result); + } + + return result; } return NULL; } CYCatch(NULL) } +static JSValueRef All_complete_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { + _assert(count == 1); + CYPool pool; + CYUTF8String prefix(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0]))); + + JSObjectRef array(NULL); + + { + CYArrayBuilder<1024> values(context, array); + + sqlite3_stmt *statement; + + if (prefix.size == 0) + _sqlcall(sqlite3_prepare(database_, + "select " + "\"cache\".\"name\" " + "from \"cache\" " + "where" + " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM + , -1, &statement, NULL)); + else { + _sqlcall(sqlite3_prepare(database_, + "select " + "\"cache\".\"name\" " + "from \"cache\" " + "where" + " \"cache\".\"name\" >= ? and \"cache\".\"name\" < ? and " + " \"cache\".\"system\" & " CY_SYSTEM " == " CY_SYSTEM + , -1, &statement, NULL)); + + _sqlcall(sqlite3_bind_text(statement, 1, prefix.data, prefix.size, SQLITE_STATIC)); + + char *after(pool.strndup(prefix.data, prefix.size)); + ++after[prefix.size - 1]; + _sqlcall(sqlite3_bind_text(statement, 2, after, prefix.size, SQLITE_STATIC)); + } + + while (_sqlcall(sqlite3_step(statement)) != SQLITE_DONE) + values(CYCastJSValue(context, CYJSString(sqlite3_column_string(statement, 0)))); + + _sqlcall(sqlite3_finalize(statement)); + } + + return array; +} CYCatch(NULL) } + static void All_getPropertyNames(JSContextRef context, JSObjectRef object, JSPropertyNameAccumulatorRef names) { JSObjectRef global(CYGetGlobalObject(context)); JSObjectRef cycript(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Cycript")))); @@ -1242,8 +1358,10 @@ static JSObjectRef Type_new(JSContextRef context, JSObjectRef object, size_t cou if (false) { } else if (count == 1) { - const char *type(CYPoolCString(pool, context, arguments[0])); - return CYMakeType(context, type); + const char *encoding(CYPoolCString(pool, context, arguments[0])); + sig::Signature signature; + sig::Parse(pool, &signature, encoding, &Structor_); + return CYMakeType(context, signature.elements[0].type); } else if (count == 2) { JSObjectRef types(CYCastJSObject(context, arguments[0])); size_t count(CYArrayLength(context, types)); @@ -1389,7 +1507,7 @@ static JSValueRef Type_callAsFunction_signed(JSContextRef context, JSObjectRef o sig::Type type(*internal->type_); switch (type.primitive) { - case sig::char_P: case sig::uchar_P: type.primitive = sig::char_P; break; + case sig::char_P: case sig::schar_P: case sig::uchar_P: type.primitive = sig::schar_P; break; case sig::short_P: case sig::ushort_P: type.primitive = sig::short_P; break; case sig::int_P: case sig::uint_P: type.primitive = sig::int_P; break; case sig::long_P: case sig::ulong_P: type.primitive = sig::long_P; break; @@ -1408,7 +1526,7 @@ static JSValueRef Type_callAsFunction_unsigned(JSContextRef context, JSObjectRef sig::Type type(*internal->type_); switch (type.primitive) { - case sig::char_P: case sig::uchar_P: type.primitive = sig::uchar_P; break; + case sig::char_P: case sig::schar_P: case sig::uchar_P: type.primitive = sig::uchar_P; break; case sig::short_P: case sig::ushort_P: type.primitive = sig::ushort_P; break; case sig::int_P: case sig::uint_P: type.primitive = sig::uint_P; break; case sig::long_P: case sig::ulong_P: type.primitive = sig::ulong_P; break; @@ -1491,8 +1609,11 @@ static JSObjectRef Type_callAsConstructor(JSContextRef context, JSObjectRef obje type = type->data.data.type; } - void *value(calloc(1, internal->GetFFI()->size)); - return CYMakePointer(context, value, length, type, NULL, NULL); + JSObjectRef pointer(CYMakePointer(context, NULL, length, type, NULL, NULL)); + Pointer *value(reinterpret_cast(JSObjectGetPrivate(pointer))); + value->value_ = value->pool_->malloc(internal->GetFFI()->size); + memset(value->value_, 0, internal->GetFFI()->size); + return pointer; } CYCatch(NULL) } static JSObjectRef Functor_new(JSContextRef context, JSObjectRef object, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry { @@ -1682,6 +1803,11 @@ static JSValueRef Type_callAsFunction_toJSON(JSContextRef context, JSObjectRef o return Type_callAsFunction_toString(context, object, _this, count, arguments, exception); } +static JSStaticFunction All_staticFunctions[2] = { + {"cy$complete", &All_complete_callAsFunction, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, + {NULL, NULL, 0} +}; + static JSStaticFunction CString_staticFunctions[6] = { {"toCYON", &CString_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, {"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete}, @@ -1760,23 +1886,13 @@ static JSStaticFunction Type_staticFunctions[14] = { {NULL, NULL, 0} }; -static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); - _visible void CYSetArgs(int argc, const char *argv[]) { JSContextRef context(CYGetJSContext()); JSValueRef args[argc]; for (int i(0); i != argc; ++i) args[i] = CYCastJSValue(context, argv[i]); - JSObjectRef array; - if (JSObjectMakeArray$ != NULL) - array = _jsccall(*JSObjectMakeArray$, context, argc, args); - else { - JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array"))); - JSValueRef value(CYCallAsFunction(context, Array, NULL, argc, args)); - array = CYCastJSObject(context, value); - } - + JSObjectRef array(CYObjectMakeArray(context, argc, args)); JSObjectRef System(CYGetCachedObject(context, CYJSString("System"))); CYSetProperty(context, System, CYJSString("args"), array); } @@ -1846,6 +1962,8 @@ _visible void CYCancel() { cancel_ = true; } +static const char *CYPoolLibraryPath(CYPool &pool); + static bool initialized_ = false; void CYInitializeDynamic() { @@ -1853,6 +1971,10 @@ void CYInitializeDynamic() { initialized_ = true; else return; + CYPool pool; + const char *db(pool.strcat(CYPoolLibraryPath(pool), "/libcycript.db", NULL)); + _sqlcall(sqlite3_open_v2(db, &database_, SQLITE_OPEN_READONLY, NULL)); + JSObjectMakeArray$ = reinterpret_cast(dlsym(RTLD_DEFAULT, "JSObjectMakeArray")); JSSynchronousGarbageCollectForDebugging$ = reinterpret_cast(dlsym(RTLD_DEFAULT, "JSSynchronousGarbageCollectForDebugging")); @@ -1860,6 +1982,7 @@ void CYInitializeDynamic() { definition = kJSClassDefinitionEmpty; definition.className = "All"; + definition.staticFunctions = All_staticFunctions; definition.hasProperty = &All_hasProperty; definition.getProperty = &All_getProperty; definition.getPropertyNames = &All_getPropertyNames; @@ -1987,49 +2110,6 @@ JSGlobalContextRef CYGetJSContext(JSContextRef context) { return reinterpret_cast(JSObjectGetPrivate(CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s))))->context_; } -struct CYFile { - void *data_; - size_t size_; - - CYFile(void *data, size_t size) : - data_(data), - size_(size) - { - } -}; - -static void CYFileExit(void *data) { - CYFile *file(reinterpret_cast(data)); - _syscall(munmap(file->data_, file->size_)); -} - -static void *CYPoolFile(CYPool &pool, const char *path, size_t *psize) { - int fd(_syscall_(open(path, O_RDONLY), 1, ENOENT)); - if (fd == -1) - return NULL; - - 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)); - - CYFile *file(new (pool) CYFile(base, size)); - pool.atexit(&CYFileExit, file); - - _syscall(close(fd)); - return base; -} - -static CYUTF8String CYPoolFileUTF8String(CYPool &pool, const char *path) { - CYUTF8String data; - data.data = reinterpret_cast(CYPoolFile(pool, path, &data.size)); - return data; -} - static const char *CYPoolLibraryPath(CYPool &pool) { Dl_info addr; _assert(dladdr(reinterpret_cast(&CYPoolLibraryPath), &addr) != 0); @@ -2039,6 +2119,10 @@ static const char *CYPoolLibraryPath(CYPool &pool) { _assert(slash != NULL); *slash = '\0'; + slash = strrchr(lib, '/'); + if (slash != NULL && strcmp(slash, "/.libs") == 0) + *slash = '\0'; + return lib; } @@ -2102,12 +2186,11 @@ static JSValueRef require_callAsFunction(JSContextRef context, JSObjectRef objec static bool CYRunScript(JSGlobalContextRef context, const char *path) { CYPool pool; - CYUTF8String code(CYPoolFileUTF8String(pool, path)); + CYUTF8String code(CYPoolFileUTF8String(pool, pool.strcat(CYPoolLibraryPath(pool), path, NULL))); if (code.data == NULL) return false; - CYStream stream(code.data, code.data + code.size); - code = CYPoolCode(pool, stream); + code = CYPoolCode(pool, code); _jsccall(JSEvaluateScript, context, CYJSString(code), NULL, NULL, 0); return true; } @@ -2195,6 +2278,10 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { JSObjectRef all(JSObjectMake(context, All_, NULL)); CYSetProperty(context, cycript, CYJSString("all"), all); + JSObjectRef cache(JSObjectMake(context, NULL, NULL)); + CYSetProperty(context, cy, CYJSString("cache"), cache); + CYSetPrototype(context, cache, all); + JSObjectRef alls(_jsccall(JSObjectCallAsConstructor, context, Array, 0, NULL)); CYSetProperty(context, cycript, CYJSString("alls"), alls); @@ -2210,13 +2297,13 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { next = JSObjectGetPrototype(context, curr); } - CYSetPrototype(context, last, all); + CYSetPrototype(context, last, cache); } JSObjectRef System(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, cy, CYJSString("System"), System); - CYSetProperty(context, all, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum); + CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum); CYSetProperty(context, global, CYJSString("system"), System); CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); @@ -2231,16 +2318,27 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { } #endif - if (CYBridgeEntry *entry = CYBridgeHash("1dlerror", 8)) - entry->cache_ = new cy::Functor(entry->value_, reinterpret_cast(&dlerror)); + CYSetProperty(context, cache, CYJSString("dlerror"), CYMakeFunctor(context, "dlerror", "*"), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("RTLD_DEFAULT"), CYCastJSValue(context, reinterpret_cast(RTLD_DEFAULT)), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("dlsym"), CYMakeFunctor(context, "dlsym", "^v^v*"), kJSPropertyAttributeDontEnum); - CYRunScript(context, "libcycript.cy"); + CYSetProperty(context, cache, CYJSString("NULL"), CYJSNull(context), kJSPropertyAttributeDontEnum); + + CYSetProperty(context, cache, CYJSString("bool"), CYMakeType(context, sig::boolean_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("char"), CYMakeType(context, sig::char_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("short"), CYMakeType(context, sig::short_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("int"), CYMakeType(context, sig::int_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("long"), CYMakeType(context, sig::long_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("float"), CYMakeType(context, sig::float_P), kJSPropertyAttributeDontEnum); + CYSetProperty(context, cache, CYJSString("double"), CYMakeType(context, sig::double_P), kJSPropertyAttributeDontEnum); for (CYHook *hook : GetHooks()) if (hook->SetupContext != NULL) (*hook->SetupContext)(context); CYArrayPush(context, alls, cycript); + + CYRunScript(context, "/libcycript.cy"); } static JSGlobalContextRef context_;