From 1ebd9c5deaa453a56e73f4fc34165f08de18bd79 Mon Sep 17 00:00:00 2001 From: Adam Ernst Date: Fri, 8 Jul 2016 09:08:36 -0700 Subject: [PATCH] Create JSContext inside setUp Summary: In practice, it *MUST* be the call to `self.context` within `setUp` that triggers the creation of the context: - It can't come before that point because `_jscWrapper` has not been set up yet - It can't come after that point since `self.context` would create the context there. Just move the creation to be inline, enforced by assert. This makes it easier to reason about where the context is created, and easier to change how it is created later. Reviewed By: javache Differential Revision: D3529843 fbshipit-source-id: 8ed5a9861ebefd4b9e0f7155db8587dcf0442b7a --- React/Executors/RCTJSCExecutor.mm | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/React/Executors/RCTJSCExecutor.mm b/React/Executors/RCTJSCExecutor.mm index c96b81d88..a9d2e60b8 100644 --- a/React/Executors/RCTJSCExecutor.mm +++ b/React/Executors/RCTJSCExecutor.mm @@ -307,19 +307,10 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) - (RCTJavaScriptContext *)context { RCTAssertThread(_javaScriptThread, @"Must be called on JS thread."); - if (!self.isValid) { return nil; } - - if (!_context) { - JSContext *context = [_jscWrapper->JSContext new]; - _context = [[RCTJavaScriptContext alloc] initWithJSContext:context onThread:_javaScriptThread]; - - [[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptContextCreatedNotification - object:context]; - } - + RCTAssert(_context != nil, @"Fetching context while valid, but before it is created"); return _context; } @@ -353,7 +344,11 @@ static void RCTInstallJSCProfiler(RCTBridge *bridge, JSContextRef context) self->_jscWrapper = RCTJSCWrapperCreate(self->_useCustomJSCLibrary); [self->_performanceLogger markStopForTag:RCTPLJSCWrapperOpenLibrary]; - JSContext *context = self.context.context; + RCTAssert(self->_context == nil, @"Didn't expect to set up twice"); + JSContext *context = [self->_jscWrapper->JSContext new]; + self->_context = [[RCTJavaScriptContext alloc] initWithJSContext:context onThread:self->_javaScriptThread]; + [[NSNotificationCenter defaultCenter] postNotificationName:RCTJavaScriptContextCreatedNotification + object:context]; if (self->_jscWrapper->configureJSContextForIOS != NULL) { NSString *cachesPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];