From: Jay Freeman (saurik) Date: Wed, 4 Nov 2009 09:28:34 +0000 (+0000) Subject: Fixed the GC crash of doom that kennytm reported: apparently local contexts come... X-Git-Tag: v0.9.432~188 X-Git-Url: https://git.saurik.com/cycript.git/commitdiff_plain/bc60fb46c3973f1c8d84994d9f98a6270f9ae803?ds=sidebyside Fixed the GC crash of doom that kennytm reported: apparently local contexts come from the ECMA spec, and are rather forcibly deallocated. --- diff --git a/Internal.hpp b/Internal.hpp index 13987d4..ed0f874 100644 --- a/Internal.hpp +++ b/Internal.hpp @@ -43,12 +43,14 @@ #include "Pooling.hpp" #include +#include #include #include #include #include +JSGlobalContextRef CYGetJSContext(JSContextRef context); void Structor_(apr_pool_t *pool, sig::Type *&type); struct Type_privateData : @@ -144,15 +146,16 @@ struct CYOwned : CYValue { private: - JSContextRef context_; + JSGlobalContextRef context_; JSObjectRef owner_; public: CYOwned(void *value, JSContextRef context, JSObjectRef owner) : CYValue(value), - context_(context), + context_(CYGetJSContext(context)), owner_(owner) { + //XXX:JSGlobalContextRetain(context_); if (owner_ != NULL) JSValueProtect(context_, owner_); } @@ -160,6 +163,7 @@ struct CYOwned : virtual ~CYOwned() { if (owner_ != NULL) JSValueUnprotect(context_, owner_); + //XXX:JSGlobalContextRelease(context_); } JSObjectRef GetOwner() const { @@ -191,19 +195,21 @@ struct Functor : struct Closure_privateData : cy::Functor { - JSContextRef context_; + JSGlobalContextRef context_; JSObjectRef function_; Closure_privateData(JSContextRef context, JSObjectRef function, const char *type) : cy::Functor(type, NULL), - context_(context), + context_(CYGetJSContext(context)), function_(function) { + //XXX:JSGlobalContextRetain(context_); JSValueProtect(context_, function_); } virtual ~Closure_privateData() { JSValueUnprotect(context_, function_); + //XXX:JSGlobalContextRelease(context_); } }; diff --git a/Library.cpp b/Library.cpp index 029797d..d9a7626 100644 --- a/Library.cpp +++ b/Library.cpp @@ -1494,6 +1494,11 @@ CYJSError::CYJSError(JSContextRef context, const char *format, ...) { value_ = CYCastJSError(context, message); } +JSGlobalContextRef CYGetJSContext(JSContextRef context) { + // XXX: do something better + return Context_; +} + JSGlobalContextRef CYGetJSContext() { CYInitialize(); diff --git a/ObjectiveC/Library.mm b/ObjectiveC/Library.mm index c3f073a..a3c1d66 100644 --- a/ObjectiveC/Library.mm +++ b/ObjectiveC/Library.mm @@ -1053,13 +1053,15 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { if ((self = [super init]) != nil) { object_ = object; - context_ = context; + context_ = CYGetJSContext(context); + //XXX:JSGlobalContextRetain(context_); JSValueProtect(context_, object_); } return self; } CYObjectiveCatch } - (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); + //XXX:JSGlobalContextRelease(context_); [super dealloc]; } CYObjectiveCatch } @@ -1127,13 +1129,15 @@ JSValueRef CYCastJSValue(JSContextRef context, NSObject *value) { CYPoolTry { - (id) initWithJSObject:(JSObjectRef)object inContext:(JSContextRef)context { CYObjectiveTry { if ((self = [super init]) != nil) { object_ = object; - context_ = context; + context_ = CYGetJSContext(context); + //XXX:JSGlobalContextRetain(context_); JSValueProtect(context_, object_); } return self; } CYObjectiveCatch } - (void) dealloc { CYObjectiveTry { JSValueUnprotect(context_, object_); + //XXX:JSGlobalContextRelease(context_); [super dealloc]; } CYObjectiveCatch } diff --git a/todo.txt b/todo.txt index 1c9c3d8..60e8c15 100644 --- a/todo.txt +++ b/todo.txt @@ -21,3 +21,4 @@ b = []; for (x in a) b.push(x); <- crashes errors in another process aren't displayed; to fix this, parse errors should get converted to exceptions and thrown CYPoolTry/Catch now carefully save the exception after it /no longer needs the exception/... uhh... wtf? throw CYJSError should probably be replaced with CYThrow() across the board +figure out what to do about global context refs: I really really want to retain the bastards