Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bridge] is it possible to get JavaScriptCore's JSContext somehow? #1618

Closed
plandem opened this issue Jun 14, 2015 · 2 comments
Closed

[Bridge] is it possible to get JavaScriptCore's JSContext somehow? #1618

plandem opened this issue Jun 14, 2015 · 2 comments
Labels
Resolution: Locked This issue was locked by the bot.

Comments

@plandem
Copy link

plandem commented Jun 14, 2015

Why do i need it? Sometimes i just want to use javascript directly and don't create wrappers to emit events to/from JS to get values.

class SimpleApp extends React.Component {
    render() {
        return (
...view
        )
    }

    validate(data) {
        return -100;
    }
}

For example, i would like to get value from 'SimpleApp.prototype.validate'. Just want to call it directly.

In my case, i have one bundle with visuals and some functions that has no any connection to visual part. Right now we have only:

React.AppRegistry.registerRunnable('validate', function(data) { return -100; });

and call it from objective-c:

[_bridge enqueueJSCall:@"AppRegistry.runApplication" args:@[@"validate", @[@2, @2]]]

But current implementation does not allow to get value from js. So my question again - is it possible to get JSContext to do next:

JSValue *validator = context[@"SimpleApp.prototype.validate"];
JSValue *result = [validator callWithArguments:@[@5] ];
@brentvatne brentvatne changed the title is it possible to get JavaScriptCore's JSContext somehow? [Bridge] is it possible to get JavaScriptCore's JSContext somehow? Jun 15, 2015
@ide
Copy link
Contributor

ide commented Jun 16, 2015

@plandem It's not very well supported at the moment. You will have issues with React's event loop if the JS functions you invoke directly from Obj-C do any kind of async work. Directly using the JSContext will bypass the code path that React takes to start an iteration of its event loop. Also if your JS throws an unhandled error you may have to handle it yourself. And even though JSC is mostly thread-safe, you should make sure to use the JSContext only on the same JS thread as the one that React will use.

But since you asked... currently the best approach is to subclass RCTContextExecutor and define init to return -[super initWithJavaScriptThread:globalContextRef:], which allows you to inject your own JSContextRef as the second argument. Expose this JSContext/JSContextRef to the rest of your app via NSNotificationCenter or a global variable. Then set up an RCTBridge like so:

RCTBridge *bridge = [RCTBridge alloc];  // warranty not included
bridge.executorClass = [YourCustomExecutor class];
bridge = [bridge initWithBundleURL:url moduleProvider:nil launchOptions:launchOptions];

RCTRootView *root = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"YourAppKey"];

I'm going to close this because as you can see it's really not well-supported but it does work if you know what you're doing and are willing to dive into the full stack.

@ide ide closed this as completed Jun 16, 2015
@plandem
Copy link
Author

plandem commented Jun 16, 2015

Thanks, for help. Yes, i have reasons to use it. It's not for any heavy tasks/async and etc. In my case i just need to move some 'value formatters' to JS side, because some visual parts will be there. But in other cases i will need just 'value formatters' without any visual parts. So i want to use same way - formatters from JS. It's mostly native app and only some parts ("modules") in React

@facebook facebook locked as resolved and limited conversation to collaborators Jun 16, 2018
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jul 22, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

No branches or pull requests

3 participants