> If I have two JSCocoa instances (JS1 and JS2) is it possible to send
> JSValueRefAndContextRef's from JS1 to JS2?
> For example, can I pass a function from JS1 to JS2. Everything I've
> read seems to indicate no, but I'm hoping I missed something!
There is a bridge to pass values from context to context, and a test that manipulates a WebView (which has its own context) from the existing JSCocoa test context.
ObjC part In JSCocoaController.m :
JSValueRef valueFromExternalContext(JSContextRef externalCtx, JSValueRef value, JSContextRef ctx)
JSValueRef valueToExternalContext(JSContextRef ctx, JSValueRef value, JSContextRef externalCtx)
JSValueRef boxedValueFromExternalContext(JSContextRef externalCtx, JSValueRef value, JSContextRef ctx)
JS part in Tests/35 webview.js. The test creates a Javascript WebView subclass and then manipulates its external javascript context :
- (void)webView:(WebView *)sender didFinishLoadForFrame:(WebFrame *)frame
{
// Global context is frame's window object
var w = sender.mainFrame.globalContext
...
w.document.body.firstChild.nextSibling.style.backgroundColor = 'lime'
// Add a function to the external context
w['eval']('function addMe(a, b) { return a+b}')
...
The bridge does not let functions pass from context to context, because they might be closures. You can do this manually by using eval.
-Patrick