Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
Fix multicontext recreate (#1818)
Browse files Browse the repository at this point in the history
* [iOS] Remove multicontext switch.

* [iOS] Remove multicontext switch.

* [iOS] For normal instance, do not create default jscontext which will be deallocated soon.
  • Loading branch information
wqyfavor authored and cxfeng1 committed Nov 26, 2018
1 parent 78db8a6 commit adfa157
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 120 deletions.
6 changes: 0 additions & 6 deletions ios/sdk/WeexSDK.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -923,7 +923,6 @@
DCA445C41EFA57E300D0CFA8 /* WXAppConfiguration.h in Headers */ = {isa = PBXBuildFile; fileRef = 74A4BA941CB365D100195969 /* WXAppConfiguration.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCA445C51EFA57E800D0CFA8 /* WXAComponent.h in Headers */ = {isa = PBXBuildFile; fileRef = 2AE5B7501CAB7DBD0082FDDB /* WXAComponent.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCA445C61EFA57EE00D0CFA8 /* NSObject+WXSwizzle.h in Headers */ = {isa = PBXBuildFile; fileRef = 74896F2E1D1AC79400D1D593 /* NSObject+WXSwizzle.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCA445C91EFA58CE00D0CFA8 /* native-bundle-main.js in Resources */ = {isa = PBXBuildFile; fileRef = DCF0CD9D1EAF3A6B0062CA8F /* native-bundle-main.js */; };
DCA445CA1EFA58CE00D0CFA8 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 59AC02501D2A7E6E00355112 /* [email protected] */; };
DCA445CB1EFA590600D0CFA8 /* WXComponent+Layout.h in Headers */ = {isa = PBXBuildFile; fileRef = 744BEA571D0520F300452B5D /* WXComponent+Layout.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCA445CC1EFA592800D0CFA8 /* WXResourceLoader.h in Headers */ = {isa = PBXBuildFile; fileRef = 742AD7381DF98C8B007DC46C /* WXResourceLoader.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1030,7 +1029,6 @@
DCEA54631F2B7DBA000ECB23 /* WXTracingManager.m in Sources */ = {isa = PBXBuildFile; fileRef = C4E97D321F1EF46D00ABC314 /* WXTracingManager.m */; };
DCF087611DCAE161005CD6EB /* WXInvocationConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = DCF0875F1DCAE161005CD6EB /* WXInvocationConfig.h */; };
DCF087621DCAE161005CD6EB /* WXInvocationConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = DCF087601DCAE161005CD6EB /* WXInvocationConfig.m */; };
DCF0CD9E1EAF3A6B0062CA8F /* native-bundle-main.js in Resources */ = {isa = PBXBuildFile; fileRef = DCF0CD9D1EAF3A6B0062CA8F /* native-bundle-main.js */; };
DCF343671E49CAEE00A2FB34 /* WXJSExceptionInfo.h in Headers */ = {isa = PBXBuildFile; fileRef = DCF343651E49CAEE00A2FB34 /* WXJSExceptionInfo.h */; settings = {ATTRIBUTES = (Public, ); }; };
DCF343681E49CAEE00A2FB34 /* WXJSExceptionInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = DCF343661E49CAEE00A2FB34 /* WXJSExceptionInfo.m */; };
ED053500207F4DEB007B4568 /* JSContext+Weex.h in Headers */ = {isa = PBXBuildFile; fileRef = ED0534FE207F4DEB007B4568 /* JSContext+Weex.h */; settings = {ATTRIBUTES = (Public, ); }; };
Expand Down Expand Up @@ -1599,7 +1597,6 @@
DCE7F1EE20AD358A00D471E7 /* WXPageEventNotifyEvent.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = WXPageEventNotifyEvent.h; path = Protocol/WXPageEventNotifyEvent.h; sourceTree = "<group>"; };
DCF0875F1DCAE161005CD6EB /* WXInvocationConfig.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXInvocationConfig.h; sourceTree = "<group>"; };
DCF087601DCAE161005CD6EB /* WXInvocationConfig.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WXInvocationConfig.m; sourceTree = "<group>"; };
DCF0CD9D1EAF3A6B0062CA8F /* native-bundle-main.js */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.javascript; name = "native-bundle-main.js"; path = "../../../../pre-build/native-bundle-main.js"; sourceTree = "<group>"; };
DCF343651E49CAEE00A2FB34 /* WXJSExceptionInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = WXJSExceptionInfo.h; sourceTree = "<group>"; };
DCF343661E49CAEE00A2FB34 /* WXJSExceptionInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = WXJSExceptionInfo.m; sourceTree = "<group>"; };
ED0534FE207F4DEB007B4568 /* JSContext+Weex.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "JSContext+Weex.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1926,7 +1923,6 @@
DCD8D0F22073392A0002C420 /* weex-polyfill.js */,
DC15A3D92010BC93009C8977 /* weex-main-jsfm.js */,
DC15A3DA2010BC93009C8977 /* weex-rax-api.js */,
DCF0CD9D1EAF3A6B0062CA8F /* native-bundle-main.js */,
59AC02501D2A7E6E00355112 /* [email protected] */,
);
path = Resources;
Expand Down Expand Up @@ -3409,7 +3405,6 @@
buildActionMask = 2147483647;
files = (
DC15A3DC2010BC93009C8977 /* weex-rax-api.js in Resources */,
DCF0CD9E1EAF3A6B0062CA8F /* native-bundle-main.js in Resources */,
DCD8D0F32073392A0002C420 /* weex-polyfill.js in Resources */,
DC15A3DB2010BC93009C8977 /* weex-main-jsfm.js in Resources */,
59AC02511D2A7E6E00355112 /* [email protected] in Resources */,
Expand All @@ -3421,7 +3416,6 @@
buildActionMask = 2147483647;
files = (
DCD8D0F52073393B0002C420 /* weex-main-jsfm.js in Resources */,
DCA445C91EFA58CE00D0CFA8 /* native-bundle-main.js in Resources */,
DCD8D0F42073392A0002C420 /* weex-polyfill.js in Resources */,
DCD8D0F62073393B0002C420 /* weex-rax-api.js in Resources */,
DCA445CA1EFA58CE00D0CFA8 /* [email protected] in Resources */,
Expand Down
49 changes: 21 additions & 28 deletions ios/sdk/WeexSDK/Sources/Bridge/WXBridgeContext.m
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ - (void)registerGlobalFunctions
NSMutableDictionary * newOptions = options ? [options mutableCopy] : [NSMutableDictionary new];
NSMutableArray * newArguments = [arguments mutableCopy];

if ([WXSDKManager sharedInstance].multiContext && [instance.bundleType.lowercaseString isEqualToString:@"rax"]) {
if ([instance.bundleType.lowercaseString isEqualToString:@"rax"]) {
// we need to adjust __weex_options__ params in arguments to options compatible with rax javaScript framework.
NSDictionary * weexOptions = nil;
for(int i = 0;i < [arguments count]; i ++) {
Expand Down Expand Up @@ -420,13 +420,9 @@ - (void)createInstance:(NSString *)instanceIdString
NSArray *args = nil;
WX_MONITOR_INSTANCE_PERF_START(WXFirstScreenJSFExecuteTime, [WXSDKManager instanceForID:instanceIdString]);
WX_MONITOR_INSTANCE_PERF_START(WXPTJSCreateInstance, [WXSDKManager instanceForID:instanceIdString]);
BOOL shoudMultiContext = [WXSDKManager sharedInstance].multiContext;
NSString * bundleType = nil;

if (shoudMultiContext) {
bundleType = [self _pareJSBundleType:instanceIdString jsBundleString:jsBundleString]; // bundleType can be Vue, Rax and the new framework.
}
if (bundleType&&shoudMultiContext) {

NSString * bundleType = [self _pareJSBundleType:instanceIdString jsBundleString:jsBundleString]; // bundleType can be Vue, Rax and the new framework.
if (bundleType) {
[sdkInstance.apmInstance setProperty:KEY_PAGE_PROPERTIES_BUNDLE_TYPE withValue:bundleType];
NSMutableDictionary *newOptions = [options mutableCopy];
if (!options) {
Expand Down Expand Up @@ -1029,29 +1025,26 @@ + (void)mountContextEnvironment:(JSContext*)context
NSDictionary *userInfo = nil;
BOOL commitException = YES;
WXSDKInstance * instance = nil;
if ([WXSDKManager sharedInstance].multiContext) {
if (context.instanceId) {
// instance page javaScript runtime exception
instance = [WXSDKManager instanceForID:context.instanceId];
if (instance) {
// instance already existed
commitException = YES;
} else {
// instance already destroyed
commitException = NO;
}

if (context.instanceId) {
// instance page javaScript runtime exception
instance = [WXSDKManager instanceForID:context.instanceId];
if (instance) {
// instance already existed
commitException = YES;
} else {
// weex-main-jsfm.js runtime exception throws
message = [NSString stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] [%@:%@:%@] %@ js stack: %@", exception[@"sourceURL"], exception[@"line"], exception[@"column"], [exception toString], [exception[@"stack"] toObject]];
if (!JSValueIsUndefined(context.JSGlobalContextRef, exception[@"sourceURL"].JSValueRef)) {
bundleUrl = exception[@"sourceURL"].toString;
} else {
bundleUrl = @"weex-main-jsfm";
}
userInfo = [NSDictionary dictionary];
// instance already destroyed
commitException = NO;
}
} else {
instance = [WXSDKEngine topInstance];
// weex-main-jsfm.js runtime exception throws
message = [NSString stringWithFormat:@"[WX_KEY_EXCEPTION_WXBRIDGE] [%@:%@:%@] %@ js stack: %@", exception[@"sourceURL"], exception[@"line"], exception[@"column"], [exception toString], [exception[@"stack"] toObject]];
if (!JSValueIsUndefined(context.JSGlobalContextRef, exception[@"sourceURL"].JSValueRef)) {
bundleUrl = exception[@"sourceURL"].toString;
} else {
bundleUrl = @"weex-main-jsfm";
}
userInfo = [NSDictionary dictionary];
}

NSDictionary* wxExtFuncInfo = [context[@"wxExtFuncInfo"] toDictionary];
Expand Down
5 changes: 4 additions & 1 deletion ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
#import "WXBridgeProtocol.h"

@interface WXJSCoreBridge : NSObject <WXBridgeProtocol>
@property (nonatomic,assign)BOOL multiContext;

- (instancetype)init;
- (instancetype)initWithoutDefaultContext;

@end
99 changes: 51 additions & 48 deletions ios/sdk/WeexSDK/Sources/Bridge/WXJSCoreBridge.mm
Original file line number Diff line number Diff line change
Expand Up @@ -60,54 +60,18 @@ @implementation WXJSCoreBridge

- (instancetype)init
{
self = [super init];

if(self){
self = [self initWithoutDefaultContext];
[self createDefaultContext];
return self;
}

_jsContext = [[JSContext alloc] init];
if (WX_SYS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
_jsContext.name = @"Weex Context";
}
- (instancetype)initWithoutDefaultContext
{
if (self = [super init]) {
_timers = [NSMutableArray new];
_callbacks = [NSMutableDictionary new];
_intervalTimerId = 0;
_intervaltimers = [NSMutableDictionary new];
_multiContext = NO;

__weak typeof(self) weakSelf = self;

[WXBridgeContext mountContextEnvironment:_jsContext];

_jsContext[@"setTimeout"] = ^(JSValue *function, JSValue *timeout) {
// this setTimeout is used by internal logic in JS framework, normal setTimeout called by users will call WXTimerModule's method;
[weakSelf performSelector: @selector(triggerTimeout:) withObject:^() {
[function callWithArguments:@[]];
} afterDelay:[timeout toDouble] / 1000];
};

_jsContext[@"setTimeoutWeex"] = ^(JSValue *appId, JSValue *ret,JSValue *arg ) {
[weakSelf triggerTimeout:[appId toString] ret:[ret toString] arg:[arg toString]];
};

_jsContext[@"setIntervalWeex"] = ^(JSValue *appId, JSValue *function,JSValue *arg) {
return [weakSelf triggerInterval:[appId toString] function:^() {
[function callWithArguments:@[]];
} arg:[arg toString]];
};

_jsContext[@"clearIntervalWeex"] = ^(JSValue *appId, JSValue *ret,JSValue *arg) {

[weakSelf triggerClearInterval:[appId toString] ret:[[ret toNumber] longLongValue]];
};

_jsContext[@"clearTimeoutWeex"] = ^(JSValue *ret) {
[weakSelf triggerClearTimeout:[ret toString]];
};

_jsContext[@"extendCallNative"] = ^(JSValue *value ) {
return [weakSelf extendCallNative:[value toDictionary]];
};

}
return self;
}
Expand Down Expand Up @@ -145,11 +109,7 @@ - (void)executeJSFramework:(NSString *)frameworkScript

WXAssertParam(frameworkScript);
if (WX_SYS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
NSString * fileName = @"native-bundle-main.js";
if ([WXSDKManager sharedInstance].multiContext) {
fileName = @"weex-main-jsfm.js";
}
[_jsContext evaluateScript:frameworkScript withSourceURL:[NSURL URLWithString:fileName]];
[_jsContext evaluateScript:frameworkScript withSourceURL:[NSURL URLWithString:@"weex-main-jsfm.js"]];
}else{
[_jsContext evaluateScript:frameworkScript];
}
Expand Down Expand Up @@ -426,6 +386,49 @@ -(void)removeTimers:(NSString *)instance
}

#pragma mark - Private

- (void)createDefaultContext
{
__weak typeof(self) weakSelf = self;

_jsContext = [[JSContext alloc] init];
if (WX_SYS_VERSION_GREATER_THAN_OR_EQUAL_TO(@"8.0")) {
_jsContext.name = @"Weex Context";
}

[WXBridgeContext mountContextEnvironment:_jsContext];

_jsContext[@"setTimeout"] = ^(JSValue *function, JSValue *timeout) {
// this setTimeout is used by internal logic in JS framework, normal setTimeout called by users will call WXTimerModule's method;
[weakSelf performSelector: @selector(triggerTimeout:) withObject:^() {
[function callWithArguments:@[]];
} afterDelay:[timeout toDouble] / 1000];
};

_jsContext[@"setTimeoutWeex"] = ^(JSValue *appId, JSValue *ret,JSValue *arg ) {
[weakSelf triggerTimeout:[appId toString] ret:[ret toString] arg:[arg toString]];
};

_jsContext[@"setIntervalWeex"] = ^(JSValue *appId, JSValue *function,JSValue *arg) {
return [weakSelf triggerInterval:[appId toString] function:^() {
[function callWithArguments:@[]];
} arg:[arg toString]];
};

_jsContext[@"clearIntervalWeex"] = ^(JSValue *appId, JSValue *ret,JSValue *arg) {

[weakSelf triggerClearInterval:[appId toString] ret:[[ret toNumber] longLongValue]];
};

_jsContext[@"clearTimeoutWeex"] = ^(JSValue *ret) {
[weakSelf triggerClearTimeout:[ret toString]];
};

_jsContext[@"extendCallNative"] = ^(JSValue *value ) {
return [weakSelf extendCallNative:[value toDictionary]];
};
}

-(void)addInstance:(NSString *)instance callback:(NSString *)callback
{
if(instance.length > 0){
Expand Down
17 changes: 0 additions & 17 deletions ios/sdk/WeexSDK/Sources/Engine/WXSDKEngine.m
Original file line number Diff line number Diff line change
Expand Up @@ -224,15 +224,6 @@ + (id)handlerForProtocol:(Protocol *)protocol
+ (void)initSDKEnvironment
{
NSString *fileName = @"weex-main-jsfm";
[WXSDKManager sharedInstance].multiContext = YES;

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"createInstanceUsingMutliContext"]) {
BOOL createInstanceUsingMutliContext = [[[NSUserDefaults standardUserDefaults] objectForKey:@"createInstanceUsingMutliContext"] boolValue];
if (!createInstanceUsingMutliContext) {
fileName = @"native-bundle-main";
[WXSDKManager sharedInstance].multiContext = NO;
}
}
NSString *filePath = [[NSBundle bundleForClass:self] pathForResource:fileName ofType:@"js"];
if (filePath == nil) {
filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"js"];
Expand Down Expand Up @@ -330,14 +321,6 @@ + (void)unload
+ (void)restart
{
NSString *fileName = @"weex-main-jsfm";
[WXSDKManager sharedInstance].multiContext = YES;
if ([[NSUserDefaults standardUserDefaults] objectForKey:@"createInstanceUsingMutliContext"]) {
BOOL createInstanceUsingMutliContext = [[[NSUserDefaults standardUserDefaults] objectForKey:@"createInstanceUsingMutliContext"] boolValue];
if (!createInstanceUsingMutliContext) {
fileName = @"native-bundle-main";
[WXSDKManager sharedInstance].multiContext = NO;
}
}
NSString *filePath = [[NSBundle bundleForClass:self] pathForResource:fileName ofType:@"js"];
if (filePath == nil) {
filePath = [[NSBundle mainBundle] pathForResource:fileName ofType:@"js"];
Expand Down
2 changes: 0 additions & 2 deletions ios/sdk/WeexSDK/Sources/Manager/WXSDKManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

@interface WXSDKManager : NSObject

@property (nonatomic, assign) BOOL multiContext;

/**
*@abstract Returns sdk manager
*/
Expand Down
21 changes: 3 additions & 18 deletions ios/sdk/WeexSDK/Sources/Model/WXSDKInstance.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ - (instancetype)init
}

// WXDebugger is a singleton actually and should not call its init twice.
_instanceJavaScriptContext = _debugJS ? [NSClassFromString(@"WXDebugger") alloc] : [[WXJSCoreBridge alloc] init];
_instanceJavaScriptContext = _debugJS ? [NSClassFromString(@"WXDebugger") alloc] : [[WXJSCoreBridge alloc] initWithoutDefaultContext];
if (!_debugJS) {
id<WXBridgeProtocol> jsBridge = [[WXSDKManager bridgeMgr] valueForKeyPath:@"bridgeCtx.jsBridge"];
JSContext* globalContex = jsBridge.javaScriptContext;
Expand Down Expand Up @@ -450,30 +450,15 @@ - (BOOL)_handleConfigCenter

BOOL enableRTLLayoutDirection = [[configCenter configForKey:@"iOS_weex_ext_config.enableRTLLayoutDirection" defaultValue:@(YES) isDefault:NULL] boolValue];
[WXUtility setEnableRTLLayoutDirection:enableRTLLayoutDirection];

BOOL shoudMultiContext = NO;
shoudMultiContext = [[configCenter configForKey:@"iOS_weex_ext_config.createInstanceUsingMutliContext" defaultValue:@(YES) isDefault:NULL] boolValue];
if(shoudMultiContext && ![WXSDKManager sharedInstance].multiContext) {
[WXSDKManager sharedInstance].multiContext = YES;
[[NSUserDefaults standardUserDefaults] setObject:@"1" forKey:@"createInstanceUsingMutliContext"];
[WXSDKEngine restart];
return YES;
}
if (!shoudMultiContext && [WXSDKManager sharedInstance].multiContext) {
[WXSDKManager sharedInstance].multiContext = NO;
[[NSUserDefaults standardUserDefaults] setObject:@"0" forKey:@"createInstanceUsingMutliContext"];
[WXSDKEngine restart];
return YES;
}
}
return NO;
}

- (void)renderWithMainBundleString:(NSNotification*)notification {
- (void)renderWithMainBundleString:(NSNotification*)notification
{
[self _renderWithMainBundleString:_mainBundleString];
}


- (void)_renderWithRequest:(WXResourceRequest *)request options:(NSDictionary *)options data:(id)data;
{
NSURL *url = request.URL;
Expand Down

0 comments on commit adfa157

Please sign in to comment.