// valueAndContext gives the js object and its associated context (the current call stack).
// This context might be soon destroyed, therefore we must use the main context to protect the value
- (id)setJSObject:(JSValueRefAndContextRef)valueAndContext {
// Get the main context
JSContextRef mainContext = [[JSCocoa controllerFromContext:valueAndContext.ctx] ctx];
// Protect the object
JSValueProtect(mainContext, valueAndContext.value);
}
// Use JSValueRefAndContextRef also when returning your object to js
- (JSValueRefAndContextRef)jsObject
{
JSValueRefAndContextRef returnValue = { NULL, NULL };
returnValue.value = /* your previously stored object */
return returnValue;
}
> --
> JSCocoa: http://inexdo.com/JSCocoa
> Source: http://github.com/parmanoir/jscocoa/tree/master
> Docs: http://code.google.com/p/jscocoa/
> Group: http://groups.google.com/group/jscocoa
> Unsubscribe: jscocoa+u...@googlegroups.com
I've had JSValueProtect fail while protecting values from webViews and ended up using a global array that stored the values. Where JSValueProtect fails, the array is a root variable of the context and JavascriptCore does keep it around. Look for __gcprotect in JSCocoaPrivateObject.
Ian