From: Jay Freeman (saurik) Date: Wed, 4 Nov 2009 20:47:28 +0000 (+0000) Subject: Removed all global cache objects, placing them in a object in the context. X-Git-Tag: v0.9.432~184 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/498c3570ae00dea1f54fb067e1d2c27435863e90?ds=inline Removed all global cache objects, placing them in a object in the context. --- diff --git a/JavaScript.hpp b/JavaScript.hpp index b45e3bf..662dccf 100644 --- a/JavaScript.hpp +++ b/JavaScript.hpp @@ -40,11 +40,8 @@ #ifndef CYCRIPT_JAVASCRIPT_HPP #define CYCRIPT_JAVASCRIPT_HPP -extern JSObjectRef Array_; -extern JSObjectRef Error_; -extern JSObjectRef Function_; -extern JSObjectRef String_; - +extern JSStringRef Array_s; +extern JSStringRef cy_s; extern JSStringRef length_s; extern JSStringRef message_s; extern JSStringRef name_s; @@ -55,8 +52,4 @@ extern JSStringRef splice_s; extern JSStringRef toCYON_s; extern JSStringRef toJSON_s; -extern JSObjectRef Array_prototype_; -extern JSObjectRef Function_prototype_; -extern JSObjectRef Object_prototype_; - #endif/*CYCRIPT_JAVASCRIPT_HPP*/ diff --git a/Library.cpp b/Library.cpp index ca7b1af..bc16967 100644 --- a/Library.cpp +++ b/Library.cpp @@ -301,7 +301,6 @@ bool CYIsKey(CYUTF8String value) { /* }}} */ static JSGlobalContextRef Context_; -static JSObjectRef System_; static JSClassRef Functor_; static JSClassRef Global_; @@ -309,15 +308,8 @@ static JSClassRef Pointer_; static JSClassRef Runtime_; static JSClassRef Struct_; -JSObjectRef Array_; -JSObjectRef Error_; -JSObjectRef Function_; -JSObjectRef String_; - -JSObjectRef Array_prototype_; -JSObjectRef Function_prototype_; -JSObjectRef Object_prototype_; - +JSStringRef Array_s; +JSStringRef cy_s; JSStringRef length_s; JSStringRef message_s; JSStringRef name_s; @@ -892,9 +884,15 @@ static JSObjectRef CYMakeFunctor(JSContextRef context, JSObjectRef function, con return JSObjectMake(context, Functor_, internal); } +JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name) { + return CYCastJSObject(context, CYGetProperty(context, CYCastJSObject(context, CYGetProperty(context, CYGetGlobalObject(context), cy_s)), name)); +} + static JSObjectRef CYMakeFunctor(JSContextRef context, JSValueRef value, const char *type) { + JSObjectRef Function(CYGetCachedObject(context, CYJSString("Function"))); + JSValueRef exception(NULL); - bool function(JSValueIsInstanceOfConstructor(context, value, Function_, &exception)); + bool function(JSValueIsInstanceOfConstructor(context, value, Function, &exception)); CYThrow(context, exception); if (function) { @@ -1359,11 +1357,13 @@ void CYSetArgs(int argc, const char *argv[]) { array = (*JSObjectMakeArray$)(context, argc, args, &exception); CYThrow(context, exception); } else { - JSValueRef value(CYCallAsFunction(context, Array_, NULL, argc, args)); + JSObjectRef Array(CYGetCachedObject(context, CYJSString("Array"))); + JSValueRef value(CYCallAsFunction(context, Array, NULL, argc, args)); array = CYCastJSObject(context, value); } - CYSetProperty(context, System_, CYJSString("args"), array); + JSObjectRef System(CYGetCachedObject(context, CYJSString("System"))); + CYSetProperty(context, System, CYJSString("args"), array); } JSObjectRef CYGetGlobalObject(JSContextRef context) { @@ -1471,6 +1471,8 @@ void CYInitialize() { //definition.getProperty = &Global_getProperty; Global_ = JSClassCreate(&definition); + Array_s = JSStringCreateWithUTF8CString("Array"); + cy_s = JSStringCreateWithUTF8CString("$cy"); length_s = JSStringCreateWithUTF8CString("length"); message_s = JSStringCreateWithUTF8CString("message"); name_s = JSStringCreateWithUTF8CString("name"); @@ -1498,7 +1500,8 @@ void CYThrow(JSContextRef context, JSValueRef value) { } const char *CYJSError::PoolCString(apr_pool_t *pool) const { - return CYPoolCString(pool, context_, value_); + // XXX: this used to be CYPoolCString + return CYPoolCCYON(pool, context_, value_); } JSValueRef CYJSError::CastJSValue(JSContextRef context) const { @@ -1530,10 +1533,12 @@ CYPoolError::CYPoolError(const char *format, va_list args) { } JSValueRef CYCastJSError(JSContextRef context, const char *message) { + JSObjectRef Error(CYGetCachedObject(context, CYJSString("Error"))); + JSValueRef arguments[1] = {CYCastJSValue(context, message)}; JSValueRef exception(NULL); - JSValueRef value(JSObjectCallAsConstructor(context, Error_, 1, arguments, &exception)); + JSValueRef value(JSObjectCallAsConstructor(context, Error, 1, arguments, &exception)); CYThrow(context, exception); return value; @@ -1564,35 +1569,41 @@ JSGlobalContextRef CYGetJSContext(JSContextRef context) { void CYSetupContext(JSGlobalContextRef context) { JSObjectRef global(CYGetGlobalObject(context)); - Array_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array"))); - JSValueProtect(context, Array_); - Array_prototype_ = CYCastJSObject(context, CYGetProperty(context, Array_, prototype_s)); - JSValueProtect(context, Array_prototype_); + JSObjectRef cy(JSObjectMake(context, NULL, NULL)); + CYSetProperty(context, global, cy_s, cy); + + JSObjectRef Array(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Array")))); + CYSetProperty(context, cy, CYJSString("Array"), Array); - Error_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error"))); - JSValueProtect(context, Error_); + JSObjectRef Array_prototype(CYCastJSObject(context, CYGetProperty(context, Array, prototype_s))); + CYSetProperty(context, cy, CYJSString("Array_prototype"), Array_prototype); - Function_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function"))); - JSValueProtect(context, Function_); - Function_prototype_ = (JSObjectRef) CYGetProperty(context, Function_, prototype_s); - JSValueProtect(context, Function_prototype_); + JSObjectRef Error(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Error")))); + CYSetProperty(context, cy, CYJSString("Error"), Error); + + JSObjectRef Function(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Function")))); + CYSetProperty(context, cy, CYJSString("Function"), Function); + + JSObjectRef Function_prototype(CYCastJSObject(context, CYGetProperty(context, Function, prototype_s))); + CYSetProperty(context, cy, CYJSString("Function_prototype"), Function_prototype); JSObjectRef Object(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("Object")))); - Object_prototype_ = CYCastJSObject(context, CYGetProperty(context, Object, prototype_s)); - JSValueProtect(context, Object_prototype_); + CYSetProperty(context, cy, CYJSString("Object"), Object); + + JSObjectRef Object_prototype(CYCastJSObject(context, CYGetProperty(context, Object, prototype_s))); + CYSetProperty(context, cy, CYJSString("Object_prototype"), Object_prototype); - String_ = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String"))); - JSValueProtect(context, String_); + JSObjectRef String(CYCastJSObject(context, CYGetProperty(context, global, CYJSString("String")))); + CYSetProperty(context, cy, CYJSString("String"), String); JSObjectSetPrototype(context, global, JSObjectMake(context, Runtime_, NULL)); - CYSetProperty(context, Array_prototype_, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); + CYSetProperty(context, Array_prototype, toCYON_s, &Array_callAsFunction_toCYON, kJSPropertyAttributeDontEnum); JSObjectRef Functor(JSObjectMakeConstructor(context, Functor_, &Functor_new)); - - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Functor, prototype_s), Function_prototype_); - + JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Functor, prototype_s)), Function_prototype); CYSetProperty(context, global, CYJSString("Functor"), Functor); + CYSetProperty(context, global, CYJSString("Pointer"), JSObjectMakeConstructor(context, Pointer_, &Pointer_new)); CYSetProperty(context, global, CYJSString("Type"), JSObjectMakeConstructor(context, Type_privateData::Class_, &Type_new)); @@ -1602,13 +1613,13 @@ void CYSetupContext(JSGlobalContextRef context) { CYSetProperty(context, global, CYJSString("$cyq"), &$cyq); - System_ = JSObjectMake(context, NULL, NULL); - JSValueProtect(context, System_); + JSObjectRef System(JSObjectMake(context, NULL, NULL)); + CYSetProperty(context, cy, CYJSString("System"), Function); - CYSetProperty(context, global, CYJSString("system"), System_); - CYSetProperty(context, System_, CYJSString("args"), CYJSNull(context)); - //CYSetProperty(context, System_, CYJSString("global"), global); - CYSetProperty(context, System_, CYJSString("print"), &System_print); + CYSetProperty(context, global, CYJSString("system"), System); + CYSetProperty(context, System, CYJSString("args"), CYJSNull(context)); + //CYSetProperty(context, System, CYJSString("global"), global); + CYSetProperty(context, System, CYJSString("print"), &System_print); if (hooks_ != NULL && hooks_->SetupContext != NULL) (*hooks_->SetupContext)(context); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index c5e1772..de56719 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -224,8 +224,6 @@ static JSClassRef ObjectiveC_Image_Classes_; static JSClassRef ObjectiveC_Images_; #endif -static JSObjectRef Instance_prototype_; - #ifdef __APPLE__ static Class NSCFBoolean_; static Class NSCFType_; @@ -253,7 +251,7 @@ Type_privateData *Selector_privateData::GetType() const { // XXX: trick this out with associated objects! JSValueRef CYGetClassPrototype(JSContextRef context, id self) { if (self == nil) - return Instance_prototype_; + return CYGetCachedObject(context, CYJSString("Instance_prototype")); // XXX: I need to think through multi-context typedef std::map CacheMap; @@ -267,9 +265,9 @@ JSValueRef CYGetClassPrototype(JSContextRef context, id self) { JSValueRef prototype; if (self == NSArray_) - prototype = Array_prototype_; + prototype = CYGetCachedObject(context, CYJSString("Array_prototype")); else if (self == NSDictionary_) - prototype = Object_prototype_; + prototype = CYGetCachedObject(context, CYJSString("Object_prototype")); else prototype = CYGetClassPrototype(context, class_getSuperclass(self)); @@ -556,8 +554,9 @@ NSObject *NSCFType$cy$toJSON(id self, SEL sel, NSString *key) { /* }}} */ NSObject *CYCastNSObject_(apr_pool_t *pool, JSContextRef context, JSObjectRef object) { + JSObjectRef Array(CYGetCachedObject(context, Array_s)); JSValueRef exception(NULL); - bool array(JSValueIsInstanceOfConstructor(context, object, Array_, &exception)); + bool array(JSValueIsInstanceOfConstructor(context, object, Array, &exception)); CYThrow(context, exception); id value(array ? [CYJSArray alloc] : [CYJSObject alloc]); return CYPoolRelease(pool, [value initWithJSObject:object inContext:context]); @@ -1156,7 +1155,8 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { JSValueRef exception(NULL); JSValueRef arguments[1]; arguments[0] = CYCastJSValue(context_, (NSObject *) object); - JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array_, push_s)), object_, 1, arguments, &exception); + JSObjectRef Array(CYGetCachedObject(context_, Array_s)); + JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, push_s)), object_, 1, arguments, &exception); CYThrow(context_, exception); } CYObjectiveCatch } @@ -1169,13 +1169,15 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { arguments[0] = CYCastJSValue(context_, index); arguments[1] = CYCastJSValue(context_, 0); arguments[2] = CYCastJSValue(context_, (NSObject *) object); - JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array_, splice_s)), object_, 3, arguments, &exception); + JSObjectRef Array(CYGetCachedObject(context_, Array_s)); + JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 3, arguments, &exception); CYThrow(context_, exception); } CYObjectiveCatch } - (void) removeLastObject { CYObjectiveTry { JSValueRef exception(NULL); - JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array_, pop_s)), object_, 0, NULL, &exception); + JSObjectRef Array(CYGetCachedObject(context_, Array_s)); + JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, pop_s)), object_, 0, NULL, &exception); CYThrow(context_, exception); } CYObjectiveCatch } @@ -1187,7 +1189,8 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { JSValueRef arguments[2]; arguments[0] = CYCastJSValue(context_, index); arguments[1] = CYCastJSValue(context_, 1); - JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array_, splice_s)), object_, 2, arguments, &exception); + JSObjectRef Array(CYGetCachedObject(context_, Array_s)); + JSObjectCallAsFunction(context_, CYCastJSObject(context_, CYGetProperty(context_, Array, splice_s)), object_, 2, arguments, &exception); CYThrow(context_, exception); } CYObjectiveCatch } @@ -2351,6 +2354,7 @@ void CYObjectiveC_Initialize() { /*XXX*/ JSContextRef context(NULL); CYPoolTry { void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { JSObjectRef global(CYGetGlobalObject(context)); + JSObjectRef cy(CYCastJSObject(context, CYGetProperty(context, global, cy_s))); JSObjectRef ObjectiveC(JSObjectMake(context, NULL, NULL)); CYSetProperty(context, global, CYJSString("ObjectiveC"), ObjectiveC); @@ -2367,8 +2371,8 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { JSObjectRef Selector(JSObjectMakeConstructor(context, Selector_, &Selector_new)); JSObjectRef Super(JSObjectMakeConstructor(context, Super_, &Super_new)); - Instance_prototype_ = (JSObjectRef) CYGetProperty(context, Instance, prototype_s); - JSValueProtect(context, Instance_prototype_); + JSObjectRef Instance_prototype(CYCastJSObject(context, CYGetProperty(context, Instance, prototype_s))); + CYSetProperty(context, cy, CYJSString("Instance_prototype"), Instance_prototype); CYSetProperty(context, global, CYJSString("Instance"), Instance); CYSetProperty(context, global, CYJSString("Selector"), Selector); @@ -2380,8 +2384,9 @@ void CYObjectiveC_SetupContext(JSContextRef context) { CYPoolTry { CYSetProperty(context, global, CYJSString("objc_msgSend"), JSObjectMakeFunctionWithCallback(context, CYJSString("objc_msgSend"), &$objc_msgSend)); - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Message, prototype_s), Function_prototype_); - JSObjectSetPrototype(context, (JSObjectRef) CYGetProperty(context, Selector, prototype_s), Function_prototype_); + JSObjectRef Function_prototype(CYGetCachedObject(context, CYJSString("Function_prototype"))); + JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Message, prototype_s)), Function_prototype); + JSObjectSetPrototype(context, CYCastJSObject(context, CYGetProperty(context, Selector, prototype_s)), Function_prototype); } CYPoolCatch() } static CYHooks CYObjectiveCHooks = { diff --git a/cycript.hpp b/cycript.hpp index 9b3c3cd..4412916 100644 --- a/cycript.hpp +++ b/cycript.hpp @@ -78,6 +78,8 @@ bool CYSendAll(int socket, const Type_ *data, size_t size) { JSGlobalContextRef CYGetJSContext(); apr_pool_t *CYGetGlobalPool(); JSObjectRef CYGetGlobalObject(JSContextRef context); + +void CYSetupContext(JSGlobalContextRef context); const char *CYExecute(apr_pool_t *pool, const char *code); void CYSetArgs(int argc, const char *argv[]); @@ -93,6 +95,8 @@ JSValueRef CYGetProperty(JSContextRef context, JSObjectRef object, JSStringRef n void CYSetProperty(JSContextRef context, JSObjectRef object, size_t index, JSValueRef value); void CYSetProperty(JSContextRef context, JSObjectRef object, JSStringRef name, JSValueRef value, JSPropertyAttributes attributes = kJSPropertyAttributeNone); +JSObjectRef CYGetCachedObject(JSContextRef context, JSStringRef name); + JSValueRef CYCastJSValue(JSContextRef context, bool value); JSValueRef CYCastJSValue(JSContextRef context, double value); JSValueRef CYCastJSValue(JSContextRef context, int value);