From c4481e4036f73f230903a9521612bbca5ff32359 Mon Sep 17 00:00:00 2001 From: "Jay Freeman (saurik)" Date: Sat, 11 Oct 2014 15:15:16 -0700 Subject: [PATCH] Support having multiple language hooks registered. --- Execute.cpp | 64 ++++++++++++++++++++++++++++--------------- JavaScript.hpp | 6 ++-- ObjectiveC/Library.mm | 10 ++----- 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/Execute.cpp b/Execute.cpp index 98105be..9a2eb95 100644 --- a/Execute.cpp +++ b/Execute.cpp @@ -50,7 +50,11 @@ #include "JavaScript.hpp" #include "String.hpp" -struct CYHooks *hooks_; +std::vector hooks_; + +CYRegisterHook::CYRegisterHook(CYHook *hook) { + hooks_.push_back(hook); +} /* JavaScript Properties {{{ */ bool CYHasProperty(JSContextRef context, JSObjectRef object, JSStringRef name) { @@ -670,9 +674,10 @@ void CYPoolFFI(CYPool *pool, JSContextRef context, sig::Type *type, ffi_type *ff break; default: - if (hooks_ != NULL && hooks_->PoolFFI != NULL) - if ((*hooks_->PoolFFI)(pool, context, type, ffi, data, value)) - return; + for (CYHook *hook : hooks_) + if (hook->PoolFFI != NULL) + if ((*hook->PoolFFI)(pool, context, type, ffi, data, value)) + return; CYThrow("unimplemented signature code: '%c''\n", type->primitive); } @@ -723,9 +728,10 @@ JSValueRef CYFromFFI(JSContextRef context, sig::Type *type, ffi_type *ffi, void null: return CYJSNull(context); default: - if (hooks_ != NULL && hooks_->FromFFI != NULL) - if (JSValueRef value = (*hooks_->FromFFI)(context, type, ffi, data, initialize, owner)) - return value; + for (CYHook *hook : hooks_) + if (hook->FromFFI != NULL) + if (JSValueRef value = (*hook->FromFFI)(context, type, ffi, data, initialize, owner)) + return value; CYThrow("unimplemented signature code: '%c''\n", type->primitive); } @@ -987,11 +993,15 @@ JSValueRef CYCallFunction(CYPool &pool, JSContextRef context, size_t setups, voi uint8_t value[cif->rtype->size]; - if (hooks_ != NULL && hooks_->CallFunction != NULL) - (*hooks_->CallFunction)(context, cif, function, value, values); - else - ffi_call(cif, function, value, values); + for (CYHook *hook : hooks_) + if (hook->CallFunction != NULL) { + // XXX: this only supports one hook, but it is a bad idea anyway + (*hook->CallFunction)(context, cif, function, value, values); + goto from; + } + ffi_call(cif, function, value, values); + from: return CYFromFFI(context, signature->elements[0].type, cif->rtype, value, initialize); } @@ -1534,24 +1544,32 @@ JSObjectRef CYGetGlobalObject(JSContextRef context) { return JSContextGetGlobalObject(context); } +// XXX: this is neither exceptin safe nor even terribly sane class ExecutionHandle { private: JSContextRef context_; - void *handle_; + std::vector handles_; public: ExecutionHandle(JSContextRef context) : context_(context) { - if (hooks_ != NULL && hooks_->ExecuteStart != NULL) - handle_ = (*hooks_->ExecuteStart)(context_); - else - handle_ = NULL; + handles_.resize(hooks_.size()); + for (size_t i(0); i != hooks_.size(); ++i) { + CYHook *hook(hooks_[i]); + if (hook->ExecuteStart != NULL) + handles_[i] = (*hook->ExecuteStart)(context_); + else + handles_[i] = NULL; + } } ~ExecutionHandle() { - if (hooks_ != NULL && hooks_->ExecuteEnd != NULL) - (*hooks_->ExecuteEnd)(context_, handle_); + for (size_t i(hooks_.size()); i != 0; --i) { + CYHook *hook(hooks_[i-1]); + if (hook->ExecuteEnd != NULL) + (*hook->ExecuteEnd)(context_, handles_[i-1]); + } } }; @@ -1668,8 +1686,9 @@ void CYInitializeDynamic() { Result_ = JSStringCreateWithUTF8CString("_"); - if (hooks_ != NULL && hooks_->Initialize != NULL) - (*hooks_->Initialize)(); + for (CYHook *hook : hooks_) + if (hook->Initialize != NULL) + (*hook->Initialize)(); } void CYThrow(JSContextRef context, JSValueRef value) { @@ -1898,8 +1917,9 @@ extern "C" void CYSetupContext(JSGlobalContextRef context) { if (CYBridgeEntry *entry = CYBridgeHash("1dlerror", 8)) entry->cache_ = new cy::Functor(entry->value_, reinterpret_cast(&dlerror)); - if (hooks_ != NULL && hooks_->SetupContext != NULL) - (*hooks_->SetupContext)(context); + for (CYHook *hook : hooks_) + if (hook->SetupContext != NULL) + (*hook->SetupContext)(context); CYArrayPush(context, alls, cycript); } diff --git a/JavaScript.hpp b/JavaScript.hpp index 2a08072..80599e6 100644 --- a/JavaScript.hpp +++ b/JavaScript.hpp @@ -115,7 +115,7 @@ JSValueRef CYCallAsFunction(JSContextRef context, JSObjectRef function, JSObject const char *CYPoolCCYON(CYPool &pool, JSContextRef context, JSObjectRef object, std::set &objects); std::set *CYCastObjects(JSContextRef context, JSObjectRef _this, size_t count, const JSValueRef arguments[]); -struct CYHooks { +struct CYHook { void *(*ExecuteStart)(JSContextRef); void (*ExecuteEnd)(JSContextRef, void *); @@ -128,7 +128,9 @@ struct CYHooks { JSValueRef (*FromFFI)(JSContextRef, sig::Type *, ffi_type *, void *, bool, JSObjectRef); }; -extern struct CYHooks *hooks_; +struct CYRegisterHook { + CYRegisterHook(CYHook *hook); +}; JSObjectRef CYMakePointer(JSContextRef context, void *pointer, size_t length, sig::Type *type, ffi_type *ffi, JSObjectRef owner); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index 3ddb873..a5a99a4 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -3092,7 +3092,7 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { CYSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype); } CYPoolCatch() } -static CYHooks CYObjectiveCHooks = { +static CYHook CYObjectiveCHook = { &CYObjectiveC_ExecuteStart, &CYObjectiveC_ExecuteEnd, &CYObjectiveC_CallFunction, @@ -3102,13 +3102,7 @@ static CYHooks CYObjectiveCHooks = { &CYObjectiveC_FromFFI, }; -struct CYObjectiveC { - CYObjectiveC() { - hooks_ = &CYObjectiveCHooks; - // XXX: evil magic juju to make this actually take effect on a Mac when compiled with autoconf/libtool doom! - _assert(hooks_ != NULL); - } -} CYObjectiveC; +CYRegisterHook CYObjectiveC(&CYObjectiveCHook); extern "C" void CydgetSetupContext(JSGlobalContextRef context) { CYObjectiveTry_ { CYSetupContext(context); -- 2.45.2