Passing JSValueRefAndContextRef from one context to another

13 views
Skip to first unread message

Corey

unread,
Sep 30, 2011, 7:11:31 PM9/30/11
to JSCocoa
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!

Thanks,
Corey

Patrick Geiller

unread,
Oct 2, 2011, 4:26:54 AM10/2/11
to jsc...@googlegroups.com

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

Reply all
Reply to author
Forward
0 new messages