X-Git-Url: https://git.saurik.com/cycript.git/blobdiff_plain/37954781d9756ece500551055562183a1e28e943..9814ec39ae60db89cf3f65487e71a48c55bbfa3c:/Library.cpp diff --git a/Library.cpp b/Library.cpp index d22d423..0f91128 100644 --- a/Library.cpp +++ b/Library.cpp @@ -1,4 +1,4 @@ -/* Cycript - Remote Execution Server and Disassembler +/* Cycript - Inlining/Optimizing JavaScript Compiler * Copyright (C) 2009 Jay Freeman (saurik) */ @@ -140,20 +140,29 @@ static CYUTF16String CYCastUTF16String(JSStringRef value) { return CYUTF16String(JSStringGetCharactersPtr(value), JSStringGetLength(value)); } -static CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) { +template +_finline size_t iconv_(size_t (*iconv)(iconv_t, Type_, size_t *, char **, size_t *), iconv_t cd, char **inbuf, size_t *inbytesleft, char **outbuf, size_t *outbytesleft) { + return iconv(cd, const_cast(inbuf), inbytesleft, outbuf, outbytesleft); +} + +CYUTF8String CYPoolUTF8String(apr_pool_t *pool, JSContextRef context, JSStringRef value) { _assert(pool != NULL); CYUTF16String utf16(CYCastUTF16String(value)); const char *in(reinterpret_cast(utf16.data)); +#ifdef __GLIBC__ + iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2"))); +#else iconv_t conversion(_syscall(iconv_open("UTF-8", "UCS-2-INTERNAL"))); +#endif size_t size(JSStringGetMaximumUTF8CStringSize(value)); char *out(new(pool) char[size]); CYUTF8String utf8(out, size); size = utf16.size * 2; - _syscall(iconv(conversion, const_cast(&in), &size, &out, &utf8.size)); + _syscall(iconv_(&iconv, conversion, const_cast(&in), &size, &out, &utf8.size)); *out = '\0'; utf8.size = out - utf8.data; @@ -177,19 +186,25 @@ const char *CYPoolCString(apr_pool_t *pool, JSContextRef context, JSValueRef val /* Index Offsets {{{ */ size_t CYGetIndex(const CYUTF8String &value) { if (value.data[0] != '0') { - char *end; - size_t index(strtoul(value.data, &end, 10)); - if (value.data + value.size == end) - return index; - } else if (value.data[1] == '\0') + size_t index(0); + for (size_t i(0); i != value.size; ++i) { + if (!DigitRange_[value.data[i]]) + return _not(size_t); + index *= 10; + index += value.data[i] - '0'; + } + return index; + } else if (value.size == 1) return 0; - return _not(size_t); + else + return _not(size_t); } size_t CYGetIndex(apr_pool_t *pool, JSContextRef context, JSStringRef value) { return CYGetIndex(CYPoolUTF8String(pool, context, value)); } +// XXX: this isn't actually right bool CYGetOffset(const char *value, ssize_t &index) { if (value[0] != '0') { char *end; @@ -241,8 +256,9 @@ void CYStringify(std::ostringstream &str, const char *data, size_t size) { break; default: + // this test is designed to be "awewsome", generating neither warnings nor incorrect results if (*value < 0x20 || *value >= 0x7f) - str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(*value); + str << "\\x" << std::setbase(16) << std::setw(2) << std::setfill('0') << unsigned(uint8_t(*value)); else simple: str << *value; } @@ -324,8 +340,19 @@ struct CStringMapLess : } }; -void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type *&type) { - if (name == NULL) +void Structor_(apr_pool_t *pool, sig::Type *&type) { + if ( + type->primitive == sig::pointer_P && + type->data.data.type != NULL && + type->data.data.type->primitive == sig::struct_P && + strcmp(type->data.data.type->name, "_objc_class") == 0 + ) { + type->primitive = sig::typename_P; + type->data.data.type = NULL; + return; + } + + if (type->primitive != sig::struct_P || type->name == NULL) return; sqlite3_stmt *statement; @@ -341,7 +368,7 @@ void Structor_(apr_pool_t *pool, const char *name, const char *types, sig::Type " limit 1" , -1, &statement, NULL)); - _sqlcall(sqlite3_bind_text(statement, 1, name, -1, SQLITE_STATIC)); + _sqlcall(sqlite3_bind_text(statement, 1, type->name, -1, SQLITE_STATIC)); int mode; const char *value; @@ -1011,7 +1038,7 @@ static void Struct_getPropertyNames(JSContextRef context, JSObjectRef object, JS name = elements[index].name; if (name == NULL) { - sprintf(number, "%lu", index); + sprintf(number, "%zu", index); name = number; } @@ -1036,7 +1063,11 @@ JSValueRef CYCallFunction(apr_pool_t *pool, JSContextRef context, size_t setups, } uint8_t value[cif->rtype->size]; - ffi_call(cif, function, value, values); + + if (hooks_ != NULL && hooks_->CallFunction != NULL) + (*hooks_->CallFunction)(context, cif, function, value, values); + else + ffi_call(cif, function, value, values); return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); } CYCatch } @@ -1104,17 +1135,21 @@ static JSValueRef Runtime_getProperty(JSContextRef context, JSObjectRef object, case 0: return JSEvaluateScript(CYGetJSContext(), CYJSString(value), NULL, NULL, 0, NULL); - case 1: - return CYMakeFunctor(context, reinterpret_cast(CYCastSymbol(name.data)), value); - case 2: { - // XXX: this is horrendously inefficient - sig::Signature signature; - sig::Parse(pool, &signature, value, &Structor_); - ffi_cif cif; - sig::sig_ffi_cif(pool, &sig::ObjectiveC, &signature, &cif); - return CYFromFFI(context, signature.elements[0].type, cif.rtype, CYCastSymbol(name.data)); - } + case 1: + if (void (*symbol)() = reinterpret_cast(CYCastSymbol(name.data))) + return CYMakeFunctor(context, symbol, value); + else return NULL; + + case 2: + if (void *symbol = CYCastSymbol(name.data)) { + // XXX: this is horrendously inefficient + sig::Signature signature; + sig::Parse(pool, &signature, 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: @@ -1288,14 +1323,24 @@ static JSStaticFunction Type_staticFunctions[4] = { {NULL, NULL, 0} }; +static JSObjectRef (*JSObjectMakeArray$)(JSContextRef, size_t, const JSValueRef[], JSValueRef *); + 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]); - JSValueRef exception(NULL); - JSObjectRef array(JSObjectMakeArray(context, argc, args, &exception)); - CYThrow(context, exception); + + JSObjectRef array; + if (JSObjectMakeArray$ != NULL) { + JSValueRef exception(NULL); + array = (*JSObjectMakeArray$)(context, argc, args, &exception); + CYThrow(context, exception); + } else { + JSValueRef value(CYCallAsFunction(context, Array_, NULL, argc, args)); + array = CYCastJSObject(context, value); + } + CYSetProperty(context, System_, CYJSString("args"), array); } @@ -1309,7 +1354,7 @@ const char *CYExecute(apr_pool_t *pool, const char *code) { void *handle; if (hooks_ != NULL && hooks_->ExecuteStart != NULL) - handle = (*hooks_->ExecuteStart)(); + handle = (*hooks_->ExecuteStart)(context); else handle = NULL; @@ -1341,7 +1386,7 @@ const char *CYExecute(apr_pool_t *pool, const char *code) { CYSetProperty(context, CYGetGlobalObject(context), Result_, result); if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) - (*hooks_->ExecuteEnd)(handle); + (*hooks_->ExecuteEnd)(context, handle); return json; } @@ -1357,6 +1402,8 @@ void CYInitialize() { _aprcall(apr_initialize()); _aprcall(apr_pool_create(&Pool_, NULL)); _sqlcall(sqlite3_open("/usr/lib/libcycript.db", &Bridge_)); + + JSObjectMakeArray$ = reinterpret_cast(dlsym(RTLD_DEFAULT, "JSObjectMakeArray")); } apr_pool_t *CYGetGlobalPool() { @@ -1423,8 +1470,6 @@ CYJSError::CYJSError(JSContextRef context, const char *format, ...) { CYThrow(context, exception); } -void CYObjectiveC(JSContextRef context, JSObjectRef global); - JSGlobalContextRef CYGetJSContext() { CYInitialize(); @@ -1543,7 +1588,8 @@ JSGlobalContextRef CYGetJSContext() { Result_ = JSStringCreateWithUTF8CString("_"); - CYObjectiveC(context, global); + if (hooks_ != NULL && hooks_->SetupContext != NULL) + (*hooks_->SetupContext)(context); } return Context_;