Decoupling initialisation and synchronous execution.

Reviewed By: javache

Differential Revision: D4117471

fbshipit-source-id: b00de532c99f041ebba8b9d74972a36827a1a3f4
This commit is contained in:
Ashok Menon 2016-11-03 10:45:06 -07:00 committed by Facebook Github Bot
parent e7dc71ba84
commit 227f1b0ea8
2 changed files with 29 additions and 34 deletions

View File

@ -87,15 +87,21 @@ RCT_EXTERN NSString *const RCTFBJSValueClassKey;
/**
* @experimental
* Pass a RCTJSContextProvider object to use an NSThread/JSContext pair that have already been created.
* The returned executor has already executed the supplied application script synchronously.
* The underlying JSContext will be returned in the JSContext pointer if it is non-NULL and there was no error.
* If an error occurs, this method will return nil and specify the error in the error pointer if it is non-NULL.
* The underlying JSContext will be returned in the JSContext pointer if it is non-NULL.
*/
+ (instancetype)initializedExecutorWithContextProvider:(RCTJSContextProvider *)JSContextProvider
applicationScript:(NSData *)applicationScript
sourceURL:(NSURL *)sourceURL
JSContext:(JSContext **)JSContext
error:(NSError **)error;
JSContext:(JSContext **)JSContext;
/**
* @experimental
* synchronouslyExecuteApplicationScript:sourceURL:JSContext:error:
*
* Run the provided JS Script/Bundle, blocking the caller until it finishes.
* If there is an error during execution, it is returned, otherwise `NULL` is
* returned.
*/
- (NSError *)synchronouslyExecuteApplicationScript:(NSData *)script
sourceURL:(NSURL *)sourceURL;
/**
* Invokes the given module/method directly. The completion block will be called with the

View File

@ -252,20 +252,13 @@ static NSThread *newJavaScriptThread(void)
}
+ (instancetype)initializedExecutorWithContextProvider:(RCTJSContextProvider *)JSContextProvider
applicationScript:(NSData *)applicationScript
sourceURL:(NSURL *)sourceURL
JSContext:(JSContext **)JSContext
error:(NSError **)error
{
const RCTJSContextData data = JSContextProvider.data;
if (JSContext) {
*JSContext = data.context;
}
RCTJSCExecutor *executor = [[RCTJSCExecutor alloc] initWithJSContextData:data];
if (applicationScript && ![executor _synchronouslyExecuteApplicationScript:applicationScript sourceURL:sourceURL JSContext:data.context error:error]) {
return nil; // error has been set by _synchronouslyExecuteApplicationScript:
}
return executor;
return [[RCTJSCExecutor alloc] initWithJSContextData:data];
}
- (instancetype)initWithJSContextData:(const RCTJSContextData &)data
@ -280,33 +273,29 @@ static NSThread *newJavaScriptThread(void)
return self;
}
- (BOOL)_synchronouslyExecuteApplicationScript:(NSData *)script
- (NSError *)synchronouslyExecuteApplicationScript:(NSData *)script
sourceURL:(NSURL *)sourceURL
JSContext:(JSContext *)context
error:(NSError **)error
{
TaggedScript taggedScript = loadTaggedScript(script, sourceURL, _performanceLogger, _randomAccessBundle, error);
NSError *loadError;
TaggedScript taggedScript = loadTaggedScript(script, sourceURL, _performanceLogger, _randomAccessBundle, &loadError);
if (!taggedScript.script) {
return NO;
if (loadError) {
return loadError;
}
if (taggedScript.tag == RCTScriptRAMBundle) {
registerNativeRequire(context, self);
registerNativeRequire(_context.context, self);
}
NSError *returnedError = executeApplicationScript(taggedScript, sourceURL,
NSError *execError = executeApplicationScript(taggedScript, sourceURL,
_jscWrapper,
_performanceLogger,
_context.context.JSGlobalContextRef);
if (returnedError) {
if (error) {
*error = returnedError;
}
return NO;
} else {
return YES;
if (execError) {
return execError;
}
return NULL;
}
- (RCTJavaScriptContext *)context