From 9db60d94ba02c57b1a2b4a9be7f082cc1a6efa95 Mon Sep 17 00:00:00 2001 From: pengfeizhou Date: Mon, 22 Feb 2021 19:03:34 +0800 Subject: [PATCH] iOS:Support debugging --- .../devkit/src/main/AndroidManifest.xml | 9 +- .../main/java/pub/doric/devkit/IDevKit.java | 2 +- .../main/java/pub/doric/devkit/WSClient.java | 2 +- .../pub/doric/devkit/ui/DoricDevActivity.java | 9 +- doric-cli/src/server.ts | 23 ++ doric-iOS/Devkit/Classes/DoricDebugDriver.h | 3 +- doric-iOS/Devkit/Classes/DoricDebugDriver.m | 14 +- doric-iOS/Devkit/Classes/DoricDebugJSEngine.h | 4 +- doric-iOS/Devkit/Classes/DoricDebugJSEngine.m | 56 +---- doric-iOS/Devkit/Classes/DoricDev.h | 7 +- doric-iOS/Devkit/Classes/DoricDev.m | 129 +++++++---- doric-iOS/Devkit/Classes/DoricDevMonitor.h | 15 ++ doric-iOS/Devkit/Classes/DoricDevMonitor.m | 50 ++-- .../Devkit/Classes/DoricDevViewController.m | 5 - .../Devkit/Classes/DoricJSRemoteExecutor.m | 213 ------------------ ...moteExecutor.h => DoricRemoteJSExecutor.h} | 16 +- .../Devkit/Classes/DoricRemoteJSExecutor.m | 194 ++++++++++++++++ doric-iOS/Devkit/Classes/DoricWSClient.h | 14 +- doric-iOS/Devkit/Classes/DoricWSClient.m | 59 ++++- .../Devkit/Classes/QRScanViewController.m | 2 - doric-iOS/Pod/Classes/Engine/DoricJSEngine.h | 2 + doric-iOS/Pod/Classes/Engine/DoricJSEngine.m | 2 +- .../Util/Category/NSString+JsonString.h | 2 +- .../Util/Category/NSString+JsonString.m | 6 +- .../Pod/Classes/Util/DoricJSRemoteArgType.h | 24 -- .../Pod/Classes/Util/DoricJSRemoteArgType.m | 21 -- 26 files changed, 461 insertions(+), 422 deletions(-) delete mode 100644 doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m rename doric-iOS/Devkit/Classes/{DoricJSRemoteExecutor.h => DoricRemoteJSExecutor.h} (74%) create mode 100644 doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.m delete mode 100644 doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.h delete mode 100644 doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.m diff --git a/doric-android/devkit/src/main/AndroidManifest.xml b/doric-android/devkit/src/main/AndroidManifest.xml index a6d467f8..e02359eb 100644 --- a/doric-android/devkit/src/main/AndroidManifest.xml +++ b/doric-android/devkit/src/main/AndroidManifest.xml @@ -1,12 +1,15 @@ - + + - - + + diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java b/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java index 18a6b0e0..dd720085 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/IDevKit.java @@ -6,7 +6,7 @@ import org.json.JSONObject; public interface IDevKit { enum Command { - HOT_RELOAD, EXCEPTION, LOG + HOT_RELOAD, EXCEPTION, LOG, DEBUG } void connectDevKit(String url); diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java b/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java index 3b5d6a62..d3766c69 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/WSClient.java @@ -49,7 +49,7 @@ public class WSClient extends WebSocketListener { boolean intercept(String type, String command, JSONObject payload) throws JSONException; } - private Set interceptors = new HashSet<>(); + private final Set interceptors = new HashSet<>(); public WSClient(String url) { OkHttpClient okHttpClient = new OkHttpClient diff --git a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java index e513ae22..9541a950 100644 --- a/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java +++ b/doric-android/devkit/src/main/java/pub/doric/devkit/ui/DoricDevActivity.java @@ -17,6 +17,8 @@ import androidx.appcompat.app.AppCompatActivity; import androidx.core.app.ActivityCompat; import androidx.core.content.ContextCompat; +import com.github.pengfeizhou.jscore.JSONBuilder; + import org.greenrobot.eventbus.EventBus; import org.greenrobot.eventbus.Subscribe; import org.greenrobot.eventbus.ThreadMode; @@ -25,6 +27,7 @@ import pub.doric.DoricContext; import pub.doric.DoricContextManager; import pub.doric.devkit.DevKit; import pub.doric.devkit.DoricDev; +import pub.doric.devkit.IDevKit; import pub.doric.devkit.R; import pub.doric.devkit.event.ConnectExceptionEvent; import pub.doric.devkit.event.EOFExceptionEvent; @@ -150,7 +153,11 @@ public class DoricDevActivity extends AppCompatActivity { cell.findViewById(R.id.debug_text_view).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - + DevKit.getInstance().sendDevCommand( + IDevKit.Command.DEBUG, + new JSONBuilder() + .put("source", doricContext.getSource()) + .toJSONObject()); } }); diff --git a/doric-cli/src/server.ts b/doric-cli/src/server.ts index 099dbe3f..b49bd7f1 100644 --- a/doric-cli/src/server.ts +++ b/doric-cli/src/server.ts @@ -1,5 +1,8 @@ import WebSocket from "ws" import "colors"; +import path from "path"; +import { glob } from "./util"; +import { Shell } from "./shell"; export type MSG = { type: "D2C" | "C2D" | "C2S" | "D2S" | "S2C" | "S2D", @@ -45,6 +48,26 @@ export async function createServer() { debug?.send(result); } else if (resultObject.type === "C2S") { switch (resultObject.cmd) { + case 'DEBUG': + let source = resultObject.payload.source as string; + if (source.endsWith(".js")) { + source = source.replace(".js", ".ts"); + } else if (!source.endsWith(".ts")) { + source = source + ".ts" + } + const tsFiles = await glob(`**/${source}`, { + cwd: path.resolve(process.cwd(), "src") + }) + if (!!!tsFiles || tsFiles.length === 0) { + console.error(`Cannot find ${source} in ${path.resolve(process.cwd(), "src")}`); + } + const sourceFile = tsFiles[0]; + Shell.exec("node", [ + "--inspect-brk", + path.resolve(process.cwd(), "src", sourceFile) + ]) + console.log(`Debugger on ${sourceFile}`); + break; case 'EXCEPTION': console.log(resultObject.payload.source.red); console.log(resultObject.payload.exception.red); diff --git a/doric-iOS/Devkit/Classes/DoricDebugDriver.h b/doric-iOS/Devkit/Classes/DoricDebugDriver.h index 8995e521..adbd0251 100644 --- a/doric-iOS/Devkit/Classes/DoricDebugDriver.h +++ b/doric-iOS/Devkit/Classes/DoricDebugDriver.h @@ -22,10 +22,11 @@ #import #import - +#import "DoricWSClient.h" NS_ASSUME_NONNULL_BEGIN @interface DoricDebugDriver : NSObject +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDebugDriver.m b/doric-iOS/Devkit/Classes/DoricDebugDriver.m index 11a8e661..57ced03e 100644 --- a/doric-iOS/Devkit/Classes/DoricDebugDriver.m +++ b/doric-iOS/Devkit/Classes/DoricDebugDriver.m @@ -23,19 +23,21 @@ #import "DoricDebugDriver.h" #import "DoricDebugJSEngine.h" #import "DoricConstant.h" -#import "DoricContextManager.h" +#import "DoricDev.h" @interface DoricDebugDriver () @property(nonatomic, strong) DoricJSEngine *jsExecutor; +@property(nonatomic, copy) NSString *theContextId; @end @implementation DoricDebugDriver @dynamic registry; -- (instancetype)init { +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient { if (self = [super init]) { - _jsExecutor = [[DoricDebugJSEngine alloc] init]; + _jsExecutor = [[DoricDebugJSEngine alloc] initWithWSClient:wsClient]; + _theContextId = nil; } return self; } @@ -150,7 +152,7 @@ - (DoricAsyncResult *)createContext:(NSString *)contextId script:(NSString *)scr __strong typeof(_self) self = _self; if (!self) return; @try { - [self.jsExecutor prepareContext:contextId script:script source:source]; + self.theContextId = contextId; [ret setupResult:@YES]; } @catch (NSException *exception) { [ret setupError:exception]; @@ -166,7 +168,9 @@ - (DoricAsyncResult *)destroyContext:(NSString *)contextId { __strong typeof(_self) self = _self; if (!self) return; @try { - [self.jsExecutor destroyContext:contextId]; + if ([contextId isEqualToString:self.theContextId]) { + [DoricDev.instance stopDebugging:NO]; + } [ret setupResult:@YES]; } @catch (NSException *exception) { [ret setupError:exception]; diff --git a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h index 8ee9c8ff..8c3b9dfb 100644 --- a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h +++ b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.h @@ -24,11 +24,11 @@ #import #import - +#import "DoricWSClient.h" NS_ASSUME_NONNULL_BEGIN @interface DoricDebugJSEngine : DoricJSEngine - +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m index 37bc5e40..a22a9e0b 100644 --- a/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m +++ b/doric-iOS/Devkit/Classes/DoricDebugJSEngine.m @@ -22,68 +22,22 @@ #import "DoricContext.h" #import "DoricDebugJSEngine.h" -#import "DoricJSRemoteExecutor.h" -#import "DoricUtil.h" -#import "NSString+JsonString.h" -#import "DoricDev.h" - -@interface DoricDebugMonitor : NSObject -@end - -@implementation DoricDebugMonitor -- (void)onException:(NSException *)exception inContext:(DoricContext *)context { - DoricLog(@"DefaultMonitor - source: %@- onException - %@", context.source, exception.reason); - NSDictionary *jsonDic = @{ - @"cmd": @"EXCEPTION", - @"data": @{ - @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"], - @"exception": exception.reason - } - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - [[DoricDev instance] sendDevCommand:jsonStr]; -} - -- (void)onLog:(DoricLogType)type message:(NSString *)message { - DoricLog(message); - - NSString *typeString = @"DEFAULT"; - if (type == DoricLogTypeDebug) { - typeString = @"DEFAULT"; - } else if (type == DoricLogTypeWarning) { - typeString = @"WARN"; - } else if (type == DoricLogTypeError) { - typeString = @"ERROR"; - } - - NSDictionary *jsonDic = @{ - @"cmd": @"LOG", - @"data": @{ - @"type": typeString, - @"message": message - } - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - [[DoricDev instance] sendDevCommand:jsonStr]; -} -@end +#import "DoricRemoteJSExecutor.h" @interface DoricDebugJSEngine () +@property(nonatomic, weak) DoricWSClient *wsClient; @end @implementation DoricDebugJSEngine -- (instancetype)init { +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient { if (self = [super init]) { + _wsClient = wsClient; } return self; } - (void)initJSEngine { - [self.registry registerMonitor:[DoricDebugMonitor new]]; - self.jsExecutor = [[DoricJSRemoteExecutor alloc] init]; + self.jsExecutor = [[DoricRemoteJSExecutor alloc] initWithWSClient:self.wsClient]; } - @end diff --git a/doric-iOS/Devkit/Classes/DoricDev.h b/doric-iOS/Devkit/Classes/DoricDev.h index 1437fefd..81d7b402 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.h +++ b/doric-iOS/Devkit/Classes/DoricDev.h @@ -19,10 +19,12 @@ // // Created by jingpeng.wang on 2020/2/25. // +#import "DoricWSClient.h" NS_ASSUME_NONNULL_BEGIN @interface DoricDev : NSObject +@property(nonatomic, strong) DoricWSClient *wsClient; + (instancetype)instance; - (void)openDevMode; @@ -33,8 +35,11 @@ NS_ASSUME_NONNULL_BEGIN - (void)connectDevKit:(NSString *)url; -- (void)sendDevCommand:(NSString *)command; +- (void)startDebugging:(NSString *)source; +- (void)stopDebugging:(BOOL)resume; + +- (void)reload:(NSString *)source script:(NSString *)script; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricDev.m b/doric-iOS/Devkit/Classes/DoricDev.m index f39d98ec..c06db301 100644 --- a/doric-iOS/Devkit/Classes/DoricDev.m +++ b/doric-iOS/Devkit/Classes/DoricDev.m @@ -20,20 +20,50 @@ // Created by jingpeng.wang on 2020/2/25. // #import -#import #import -#import +#import #import "DoricDev.h" -#import "DoricWSClient.h" #import "DoricDebugDriver.h" #import "DoricDevViewController.h" #import "DoricDevMonitor.h" +@interface DoricContextDebuggable : NSObject +@property(nonatomic, weak) DoricContext *doricContext; +@property(nonatomic, weak) id nativeDriver; +@property(nonatomic, weak) DoricWSClient *wsClient; +@end + +@implementation DoricContextDebuggable +- (instancetype)initWithWSClient:(DoricWSClient *)client context:(DoricContext *)context { + if (self = [super init]) { + _wsClient = client; + _doricContext = context; + _nativeDriver = context.driver; + } + return self; +} + +- (void)startDebug { + [self.doricContext setDriver:[[DoricDebugDriver alloc] initWithWSClient:self.wsClient]]; + [self.doricContext reload:self.doricContext.script]; +} + +- (void)stopDebug:(BOOL)resume { + id driver = self.doricContext.driver; + if ([driver isKindOfClass:DoricDebugDriver.class]) { + + } + if (resume) { + self.doricContext.driver = self.nativeDriver; + [self.doricContext reload:self.doricContext.script]; + } +} +@end + + @interface DoricDev () -@property(nonatomic, strong) DoricWSClient *wsclient; -@property(nonatomic, strong) DoricContext *context; -@property(nonatomic, strong) DoricDebugDriver *driver; +@property(nonatomic, strong) DoricContextDebuggable *debuggable; @end @implementation DoricDev @@ -43,10 +73,6 @@ - (instancetype)init { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onOpenEvent) name:@"OpenEvent" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEOFEvent) name:@"EOFEvent" object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onConnectExceptionEvent) name:@"ConnectExceptionEvent" object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStartDebugEvent:) name:@"StartDebugEvent" object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onEnterDebugEvent) name:@"EnterDebugEvent" object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onStopDebugEvent) name:@"StopDebugEvent" object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onDebuggerReadyEvent) name:@"DebuggerReadyEvent" object:nil]; [DoricNativeDriver.instance.registry registerMonitor:[DoricDevMonitor new]]; } return self; @@ -75,25 +101,21 @@ - (void)openDevMode { } - (void)closeDevMode { - if (self.wsclient) { - [self.wsclient close]; - self.wsclient = nil; + if (self.wsClient) { + [self.wsClient close]; + self.wsClient = nil; } } - (BOOL)isInDevMode { - return self.wsclient != nil; + return self.wsClient != nil; } - (void)connectDevKit:(NSString *)url { - if (self.wsclient) { - [self.wsclient close]; + if (self.wsClient) { + [self.wsClient close]; } - self.wsclient = [[DoricWSClient alloc] initWithUrl:url]; -} - -- (void)sendDevCommand:(NSString *)command { - [self.wsclient send:command]; + self.wsClient = [[DoricWSClient alloc] initWithUrl:url]; } - (void)onOpenEvent { @@ -108,34 +130,51 @@ - (void)onConnectExceptionEvent { ShowToast(@"dev kit connection exception", DoricGravityBottom); } -- (void)onStartDebugEvent:(NSNotification *)notification { - NSString *contextId = notification.object; - ShowToast(contextId, DoricGravityBottom); - for (DoricContext *context in [[DoricContextManager instance] aliveContexts]) { - BOOL result = [context.contextId compare:contextId] == NSOrderedSame; - if (result) { - _context = context; +- (DoricContext *)matchContext:(NSString *)source { + for (DoricContext *context in [DoricContextManager.instance aliveContexts]) { + if ([source containsString:context.source] || [context.source isEqualToString:@"__dev__"]) { + return context; } } + return nil; +} + +- (void)reload:(NSString *)source script:(NSString *)script { + DoricContext *context = [self matchContext:source]; + if (context) { + if ([context.driver isKindOfClass:DoricDebugDriver.class]) { + DoricLog(@"Context source %@ in debugging,skip reload", source); + } else { + DoricLog(@"Context reload :id %@,source %@", context.contextId, source); + [context reload:script]; + } + } else { + DoricLog(@"Cannot find context source %@ for reload", source); + } } -- (void)onEnterDebugEvent { - _driver = [DoricDebugDriver new]; -} +- (void)startDebugging:(NSString *)source { + [self.debuggable stopDebug:YES]; + DoricContext *context = [self matchContext:source]; + if (context) { + [self.wsClient sendToDebugger:@"DEBUG_RES" payload:@{ + @"contextId": context.contextId + }]; + self.debuggable = [[DoricContextDebuggable alloc] initWithWSClient:self.wsClient context:context]; + [self.debuggable startDebug]; + } else { + DoricLog(@"Cannot find context source %@ for debugging", source); + [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ + @"msg": @"Cannot find suitable alive context for debugging" + }]; + } +}; -- (void)onStopDebugEvent { - _context.driver = [DoricNativeDriver instance]; - _context.rootNode.viewId = nil; - [_context callEntity:DORIC_ENTITY_INIT, _context.extra, nil]; - [_context callEntity:DORIC_ENTITY_CREATE, nil]; - [_context callEntity:DORIC_ENTITY_BUILD withArgumentsArray:@[_context.initialParams]]; -} - -- (void)onDebuggerReadyEvent { - _context.driver = _driver; - _context.rootNode.viewId = nil; - [_context callEntity:DORIC_ENTITY_INIT, _context.extra, nil]; - [_context callEntity:DORIC_ENTITY_CREATE, nil]; - [_context callEntity:DORIC_ENTITY_BUILD withArgumentsArray:@[_context.initialParams]]; +- (void)stopDebugging:(BOOL)resume { + [self.wsClient sendToDebugger:@"DEBUG_STOP" payload:@{ + @"msg": @"Stop debugging" + }]; + [self.debuggable stopDebug:resume]; + self.debuggable = nil; } @end diff --git a/doric-iOS/Devkit/Classes/DoricDevMonitor.h b/doric-iOS/Devkit/Classes/DoricDevMonitor.h index 55c29233..7bde1b5a 100644 --- a/doric-iOS/Devkit/Classes/DoricDevMonitor.h +++ b/doric-iOS/Devkit/Classes/DoricDevMonitor.h @@ -1,3 +1,18 @@ +/* +* Copyright [2019] [Doric.Pub] +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ // // DoricDevMonitor.h // DoricDevkit diff --git a/doric-iOS/Devkit/Classes/DoricDevMonitor.m b/doric-iOS/Devkit/Classes/DoricDevMonitor.m index a90ac42d..2b6b40eb 100644 --- a/doric-iOS/Devkit/Classes/DoricDevMonitor.m +++ b/doric-iOS/Devkit/Classes/DoricDevMonitor.m @@ -1,3 +1,18 @@ +/* +* Copyright [2019] [Doric.Pub] +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ // // DoricDevMonitor.m // DoricDevkit @@ -8,7 +23,6 @@ #import "DoricDevMonitor.h" #import "DoricDev.h" -#import "NSString+JsonString.h" #import "Doric.h" @implementation DoricDevMonitor @@ -16,16 +30,11 @@ - (void)onException:(NSException *)exception inContext:(DoricContext *)context { if (!DoricDev.instance.isInDevMode) { return; } - NSDictionary *jsonDic = @{ - @"cmd": @"EXCEPTION", - @"data": @{ - @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"], - @"exception": exception.reason - } - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - [[DoricDev instance] sendDevCommand:jsonStr]; + [DoricDev.instance.wsClient sendToServer:@"EXCEPTION" + payload:@{ + @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"], + @"exception": exception.reason + }]; } - (void)onLog:(DoricLogType)type message:(NSString *)message { @@ -33,23 +42,16 @@ - (void)onLog:(DoricLogType)type message:(NSString *)message { return; } NSString *typeString = @"DEFAULT"; - if (type == DoricLogTypeDebug) { - typeString = @"DEFAULT"; - } else if (type == DoricLogTypeWarning) { + if (type == DoricLogTypeWarning) { typeString = @"WARN"; } else if (type == DoricLogTypeError) { typeString = @"ERROR"; } - NSDictionary *jsonDic = @{ - @"cmd": @"LOG", - @"data": @{ - @"type": typeString, - @"message": message - } - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - [[DoricDev instance] sendDevCommand:jsonStr]; + [DoricDev.instance.wsClient sendToServer:@"EXCEPTION" + payload:@{ + @"type": typeString, + @"message": message + }]; } @end diff --git a/doric-iOS/Devkit/Classes/DoricDevViewController.m b/doric-iOS/Devkit/Classes/DoricDevViewController.m index fe877fd1..6365cb83 100644 --- a/doric-iOS/Devkit/Classes/DoricDevViewController.m +++ b/doric-iOS/Devkit/Classes/DoricDevViewController.m @@ -25,7 +25,6 @@ #import "DoricDev.h" #import "DoricDevViewController.h" -#import "DoricJSRemoteExecutor.h" #import "QRScanViewController.h" @interface DoricDevViewController () @@ -51,7 +50,6 @@ - (void)viewDidLoad { } if (self.isSimulator) { NSString *result = @"127.0.0.1"; - [DoricJSRemoteExecutor configIp:result]; [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricGravityBottom); } else { @@ -94,9 +92,6 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath @"source": [context.source stringByReplacingOccurrencesOfString:@".js" withString:@".ts"] } }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - [[DoricDev instance] sendDevCommand:jsonStr]; } @end diff --git a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m b/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m deleted file mode 100644 index 6a40a872..00000000 --- a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.m +++ /dev/null @@ -1,213 +0,0 @@ -/* -* Copyright [2019] [Doric.Pub] -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*/ -// -// DoricJSRemoteExecutor.m -// Doric -// -// Created by 王劲鹏 on 2019/10/31. -// -#import "DoricJSRemoteExecutor.h" -#import "SRWebSocket.h" -#import "DoricUtil.h" -#import "DoricJSRemoteArgType.h" -#import "NSString+JsonString.h" - -static NSString *debuggerAddress = @""; - -typedef id (^Block0)(void); -typedef id (^Block1)(id arg0); -typedef id (^Block2)(id arg0, id arg1); -typedef id (^Block3)(id arg0, id arg1, id arg2); -typedef id (^Block4)(id arg0, id arg1, id arg2, id arg3); -typedef id (^Block5)(id arg0, id arg1, id arg2, id arg3, id arg4); - -@interface DoricJSRemoteExecutor () -@property(nonatomic, strong) SRWebSocket *srWebSocket; -@property(nonatomic, strong) NSMutableDictionary *blockMDic; -@property(nonatomic, strong) JSValue *temp; -@end - -@implementation DoricJSRemoteExecutor -- (instancetype)init { - if (self = [super init]) { - [self srWebSocket]; - _semaphore = dispatch_semaphore_create(0); - DC_LOCK(self.semaphore); - } - return self; -} - -#pragma mark - SRWebSocketDelegate -- (void)webSocketDidOpen:(SRWebSocket *)webSocket { - DoricLog(@"debugger webSocketDidOpen"); - DC_UNLOCK(self.semaphore); - [[NSNotificationCenter defaultCenter] postNotificationName:@"DebuggerReadyEvent" object:nil]; -} - -- (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload { - DoricLog(@"debugger webSocketdidReceivePong"); -} - -- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { - NSData *jsonData = [message dataUsingEncoding:NSUTF8StringEncoding]; - NSError *err; - NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData - options:NSJSONReadingMutableContainers - error:&err]; - if (err) { - DoricLog(@"debugger webSocketdidReceiveMessage parse error:%@", err); - return; - } - NSString *cmd = [[dic valueForKey:@"cmd"] copy]; - - if ([cmd isEqualToString:@"injectGlobalJSFunction"]) { - NSString *name = [dic valueForKey:@"name"]; - NSArray *argsArr = [dic valueForKey:@"arguments"]; - NSMutableArray *argsMarr = [NSMutableArray new]; - for (NSUInteger i = 0; i < argsArr.count; i++) { - [argsMarr addObject:argsArr[i]]; - } - - id result; - id tmpBlk = self.blockMDic[name]; - if (argsArr.count == 0) { - result = ((Block0) tmpBlk)(); - } else if (argsArr.count == 1) { - result = ((Block1) tmpBlk)(argsArr[0]); - } else if (argsArr.count == 2) { - result = ((Block2)tmpBlk)(argsArr[0], argsArr[1]); - } else if (argsArr.count == 3) { - result = ((Block3)tmpBlk)(argsArr[0], argsArr[1], argsArr[2]); - } else if (argsArr.count == 4) { - result = ((Block4)tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3]); - } else if (argsArr.count == 5) { - result = ((Block5)tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3], argsArr[4]); - }else { - DoricLog(@"error:args to more than 5. args:%@",argsArr); - } - - } else if ([cmd isEqualToString:@"invokeMethod"]) { - @try { - self.temp = [JSValue valueWithObject:[dic valueForKey:@"result"] inContext:nil]; - } @catch (NSException *exception) { - DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason); - } @finally { - DC_UNLOCK(self.semaphore); - } - } -} - -- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error { - DoricLog(@"debugger webSocketdidFailWithError"); - DC_UNLOCK(self.semaphore); -} - -- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean { - DoricLog(@"debugger webSocketdidCloseWithCode"); - [[NSNotificationCenter defaultCenter] postNotificationName:@"StopDebugEvent" object:nil]; -} - -#pragma mark - DoricJSExecutorProtocol -- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source { - - return nil; -} - -- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { - if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) { - self.blockMDic[name] = obj; - } - NSDictionary *jsonDic = @{ - @"cmd": @"injectGlobalJSFunction", - @"name": name - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - if (!jsonStr) { - return; - } - - [self.srWebSocket send:jsonStr]; -} - -- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { - - NSMutableArray *argsMArr = [NSMutableArray new]; - for (id arg in args) { - NSDictionary *dic = [self dicForArg:arg]; - [argsMArr addObject:dic]; - } - - NSArray *argsArr = [argsMArr copy]; - - NSDictionary *jsonDic = @{ - @"cmd": @"invokeMethod", - @"objectName": objName, - @"functionName": funcName, - @"values": argsArr - }; - - NSString *jsonStr = [NSString dc_convertToJsonWithDic:jsonDic]; - if (!jsonStr) { - return nil; - } - - [self.srWebSocket send:jsonStr]; - DC_LOCK(self.semaphore); - - return self.temp; -} - -- (NSDictionary *)dicForArg:(id)arg { - DoricJSRemoteArgType type = DoricargTypeWithArg(arg); - if (type == DoricJSRemoteArgTypeObject || type == DoricJSRemoteArgTypeArray) { - NSString *jsonStr = [NSString dc_convertToJsonWithDic:(NSDictionary *)arg]; - arg = jsonStr; - } - NSDictionary *dic = @{ - @"type": @(type), - @"value": arg - }; - return dic; -} - -+ (void)configIp:(NSString *)ip { - debuggerAddress = [[@"ws://" stringByAppendingString:ip] stringByAppendingString:@":2080"]; -} - -- (void)close { - [self.srWebSocket close]; -} - -#pragma mark - Properties -- (SRWebSocket *)srWebSocket { - if (!_srWebSocket) { - NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:debuggerAddress] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]; - _srWebSocket = [[SRWebSocket alloc] initWithURLRequest:request]; - _srWebSocket.delegate = self; - [_srWebSocket open]; - } - return _srWebSocket; -} - -- (NSMutableDictionary *)blockMDic { - if (!_blockMDic) { - _blockMDic = [NSMutableDictionary new]; - } - return _blockMDic; -} - -@end diff --git a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h b/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.h similarity index 74% rename from doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h rename to doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.h index f186e7cb..c9a49eab 100644 --- a/doric-iOS/Devkit/Classes/DoricJSRemoteExecutor.h +++ b/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.h @@ -14,24 +14,20 @@ * limitations under the License. */ // -// DoricJSRemoteExecutor.h -// Pods +// DoricRemoteJSExecutor.h +// DoricDevkit // -// Created by 王劲鹏 on 2019/10/31. +// Created by pengfei.zhou on 2021/2/22. // #import #import +#import "DoricWSClient.h" NS_ASSUME_NONNULL_BEGIN -@interface DoricJSRemoteExecutor : NSObject - -@property(nonatomic, strong) dispatch_semaphore_t semaphore; - -+ (void)configIp:(NSString *)ip; - -- (void)close; +@interface DoricRemoteJSExecutor : NSObject +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.m b/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.m new file mode 100644 index 00000000..611a1212 --- /dev/null +++ b/doric-iOS/Devkit/Classes/DoricRemoteJSExecutor.m @@ -0,0 +1,194 @@ +/* +* Copyright [2019] [Doric.Pub] +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +// +// DoricRemoteJSExecutor.m +// DoricDevkit +// +// Created by pengfei.zhou on 2021/2/22. +// + +#import "DoricRemoteJSExecutor.h" +#import "NSString+JsonString.h" +#import + +typedef NS_ENUM(NSUInteger, DoricJSRemoteArgType) { + DoricJSRemoteArgTypeNil = 0, + DoricJSRemoteArgTypeNumber, + DoricJSRemoteArgTypeBool, + DoricJSRemoteArgTypeString, + DoricJSRemoteArgTypeObject, + DoricJSRemoteArgTypeArray, +}; + + +typedef id (^Block0)(void); + +typedef id (^Block1)(id arg0); + +typedef id (^Block2)(id arg0, id arg1); + +typedef id (^Block3)(id arg0, id arg1, id arg2); + +typedef id (^Block4)(id arg0, id arg1, id arg2, id arg3); + +typedef id (^Block5)(id arg0, id arg1, id arg2, id arg3, id arg4); + +@interface DoricRemoteJSExecutor () +@property(nonatomic, weak) DoricWSClient *wsClient; +@property(nonatomic, strong) NSMutableDictionary *blockMDic; +@property(nonatomic, strong) JSValue *temp; +@property(nonatomic, strong) dispatch_semaphore_t semaphore; +@end + +@implementation DoricRemoteJSExecutor +- (instancetype)initWithWSClient:(DoricWSClient *)wsClient { + if (self = [super init]) { + _wsClient = wsClient; + [_wsClient addInterceptor:self]; + _blockMDic = [NSMutableDictionary new]; + _semaphore = dispatch_semaphore_create(0); + } + return self; +} + +- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source { + return nil; +} + +- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj { + if ([obj isKindOfClass:NSClassFromString(@"NSBlock")]) { + self.blockMDic[name] = obj; + [self.wsClient sendToDebugger:@"injectGlobalJSFunction" payload:@{ + @"name": name, + }]; + } else if ([obj isKindOfClass:NSNumber.class]) { + [self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{ + @"name": name, + @"type": @(DoricJSRemoteArgTypeNumber), + @"value": obj, + }]; + } else if ([obj isKindOfClass:NSString.class]) { + [self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{ + @"name": name, + @"type": @(DoricJSRemoteArgTypeString), + @"value": obj, + }]; + } else if ([obj isKindOfClass:NSObject.class]) { + [self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{ + @"name": name, + @"type": @(DoricJSRemoteArgTypeObject), + @"value": obj, + }]; + } else if ([obj isKindOfClass:NSArray.class]) { + [self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{ + @"name": name, + @"type": @(DoricJSRemoteArgTypeArray), + @"value": obj, + }]; + } else if (obj == nil) { + [self.wsClient sendToDebugger:@"injectGlobalJSObject" payload:@{ + @"name": name, + @"type": @(DoricJSRemoteArgTypeNil), + }]; + } +} + +- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args { + + NSMutableArray *argsMArr = [NSMutableArray new]; + + for (id arg in args) { + NSDictionary *dic = [self dicForArg:arg]; + [argsMArr addObject:dic]; + } + + [self.wsClient sendToDebugger:@"invokeMethod" payload:@{ + @"cmd": @"invokeMethod", + @"objectName": objName, + @"functionName": funcName, + @"values": [argsMArr copy] + }]; + + DC_LOCK(self.semaphore); + + return self.temp; +} + +- (NSDictionary *)dicForArg:(id)arg { + DoricJSRemoteArgType type = [self argType:arg]; + if (type == DoricJSRemoteArgTypeObject || type == DoricJSRemoteArgTypeArray) { + NSString *jsonStr = [NSString dc_convertToJsonWithDic:arg]; + arg = jsonStr; + } + NSDictionary *dic = @{ + @"type": @(type), + @"value": arg + }; + return dic; +} + +- (DoricJSRemoteArgType)argType:(id)arg { + DoricJSRemoteArgType type = DoricJSRemoteArgTypeNil; + if ([arg isKindOfClass:[NSNumber class]]) { + type = DoricJSRemoteArgTypeNumber; + } else if ([arg isKindOfClass:[NSString class]]) { + type = DoricJSRemoteArgTypeString; + } else if ([arg isKindOfClass:[NSDictionary class]]) { + type = DoricJSRemoteArgTypeObject; + } else if ([arg isKindOfClass:[NSMutableArray class]]) { + type = DoricJSRemoteArgTypeArray; + } + return type; +} + +- (BOOL)interceptType:(NSString *)type command:(NSString *)cmd payload:(NSDictionary *)payload { + if ([type isEqualToString:@"D2C"]) { + if ([cmd isEqualToString:@"injectGlobalJSFunction"]) { + NSString *name = payload[@"name"]; + NSArray *argsArr = payload[@"arguments"]; + id tmpBlk = self.blockMDic[name]; + id result; + if (argsArr.count == 0) { + result = ((Block0) tmpBlk)(); + } else if (argsArr.count == 1) { + result = ((Block1) tmpBlk)(argsArr[0]); + } else if (argsArr.count == 2) { + result = ((Block2) tmpBlk)(argsArr[0], argsArr[1]); + } else if (argsArr.count == 3) { + result = ((Block3) tmpBlk)(argsArr[0], argsArr[1], argsArr[2]); + } else if (argsArr.count == 4) { + result = ((Block4) tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3]); + } else if (argsArr.count == 5) { + result = ((Block5) tmpBlk)(argsArr[0], argsArr[1], argsArr[2], argsArr[3], argsArr[4]); + } else { + DoricLog(@"error:args to more than 5. args:%@", argsArr); + result = nil; + } + } else if ([cmd isEqualToString:@"invokeMethod"]) { + @try { + self.temp = [JSValue valueWithObject:payload[@"result"] inContext:nil]; + } @catch (NSException *exception) { + DoricLog(@"debugger ", NSStringFromSelector(_cmd), exception.reason); + } @finally { + DC_UNLOCK(self.semaphore); + } + } + } + return NO; +} + + +@end diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.h b/doric-iOS/Devkit/Classes/DoricWSClient.h index 63626027..2f0d61dd 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.h +++ b/doric-iOS/Devkit/Classes/DoricWSClient.h @@ -24,12 +24,22 @@ NS_ASSUME_NONNULL_BEGIN +@protocol DoricWSClientInterceptor +- (BOOL)interceptType:(NSString *)type command:(NSString *)cmd payload:(NSDictionary *)payload; +@end + @interface DoricWSClient : NSObject - (instancetype)initWithUrl:(NSString *)url; -- (void)send:(NSString *)command; - - (void)close; + +- (void)addInterceptor:(id )interceptor; + +- (void)removeInterceptor:(id )interceptor; + +- (void)sendToDebugger:(NSString *)cmd payload:(NSDictionary *)payload; + +- (void)sendToServer:(NSString *)cmd payload:(NSDictionary *)payload; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Devkit/Classes/DoricWSClient.m b/doric-iOS/Devkit/Classes/DoricWSClient.m index 47702d1b..42d9a8c0 100644 --- a/doric-iOS/Devkit/Classes/DoricWSClient.m +++ b/doric-iOS/Devkit/Classes/DoricWSClient.m @@ -22,16 +22,20 @@ #import "DoricWSClient.h" #import "SRWebSocket.h" -#import "DoricUtil.h" -#import "DoricContextManager.h" +#import +#import +#import +#import "DoricDev.h" @interface DoricWSClient () @property(nonatomic, strong) SRWebSocket *websocket; +@property(nonatomic, strong) NSHashTable > *interceptors; @end @implementation DoricWSClient - (instancetype)initWithUrl:(NSString *)url { if (self = [super init]) { + _interceptors = [NSHashTable hashTableWithOptions:NSPointerFunctionsWeakMemory]; _websocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:url]]; _websocket.delegate = self; [_websocket open]; @@ -58,7 +62,27 @@ - (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message { DoricLog(@"webSocketdidReceiveMessage parse error:%@", err); return; } - NSString *cmd = [[dic valueForKey:@"cmd"] mutableCopy]; + + NSString *type = dic[@"type"]; + NSString *cmd = dic[@"cmd"]; + NSDictionary *payload = dic[@"payload"]; + for (id interceptor in self.interceptors) { + if ([interceptor interceptType:type command:cmd payload:payload]) { + return; + } + } + + if ([cmd isEqualToString:@"DEBUG_REQ"]) { + NSString *source = payload[@"source"]; + [DoricDev.instance startDebugging:source]; + } else if ([cmd isEqualToString:@"DEBUG_STOP"]) { + [DoricDev.instance stopDebugging:YES]; + } else if ([cmd isEqualToString:@"RELOAD"]) { + NSString *source = payload[@"source"]; + NSString *script = payload[@"script"]; + [DoricDev.instance reload:source script:script]; + } + if ([cmd compare:@"SWITCH_TO_DEBUG"] == NSOrderedSame) { [[NSNotificationCenter defaultCenter] postNotificationName:@"EnterDebugEvent" object:nil]; } else if ([cmd compare:@"RELOAD"] == NSOrderedSame) { @@ -82,8 +106,33 @@ - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reas [[NSNotificationCenter defaultCenter] postNotificationName:@"EOFEvent" object:nil]; } -- (void)send:(NSString *)command { - [_websocket send:command]; +- (void)send:(NSDictionary *)command { + NSString *jsonStr = [NSString dc_convertToJsonWithDic:command]; + [_websocket send:jsonStr]; +} + +- (void)addInterceptor:(id )interceptor { + [self.interceptors addObject:interceptor]; +} + +- (void)removeInterceptor:(id )interceptor { + [self.interceptors removeObject:interceptor]; +} + +- (void)sendToDebugger:(NSString *)cmd payload:(NSDictionary *)payload { + [self send:@{ + @"type": @"C2D", + @"cmd": cmd, + @"payload": payload, + }]; +} + +- (void)sendToServer:(NSString *)cmd payload:(NSDictionary *)payload { + [self send:@{ + @"type": @"C2S", + @"cmd": cmd, + @"payload": payload, + }]; } - (void)close { diff --git a/doric-iOS/Devkit/Classes/QRScanViewController.m b/doric-iOS/Devkit/Classes/QRScanViewController.m index 0e7cb496..b897f5ac 100644 --- a/doric-iOS/Devkit/Classes/QRScanViewController.m +++ b/doric-iOS/Devkit/Classes/QRScanViewController.m @@ -24,7 +24,6 @@ #import #import #import -#import "DoricJSRemoteExecutor.h" @interface QRScanViewController () @property(strong, nonatomic) AVCaptureDevice *device; @@ -105,7 +104,6 @@ - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects: NSString *result = qrObject.stringValue; NSLog(@"Scan result is %@", result); [[DoricDev instance] connectDevKit:[NSString stringWithFormat:@"ws://%@:7777", result]]; - [DoricJSRemoteExecutor configIp:result]; ShowToast([NSString stringWithFormat:@"Connected to %@", result], DoricGravityBottom); [self.navigationController popViewControllerAnimated:NO]; } diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h index 32bd1de3..c73d2956 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.h @@ -46,6 +46,8 @@ NS_ASSUME_NONNULL_BEGIN - (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args; - (void)ensureRunOnJSThread:(dispatch_block_t)block; + +- (void)initJSEngine; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m index 18a513ca..774d6f3a 100644 --- a/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m +++ b/doric-iOS/Pod/Classes/Engine/DoricJSEngine.m @@ -96,6 +96,7 @@ - (instancetype)init { [self initJSExecutor]; [self initDoricEnvironment]; }]; + [self.registry registerMonitor:[DoricDefaultMonitor new]]; } return self; } @@ -126,7 +127,6 @@ - (void)threadRun { } - (void)initJSEngine { - [self.registry registerMonitor:[DoricDefaultMonitor new]]; self.jsExecutor = [DoricJSCoreExecutor new]; } diff --git a/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.h b/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.h index 9581e9a3..c7034458 100644 --- a/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.h +++ b/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.h @@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN @interface NSString (JsonString) -+ (NSString *)dc_convertToJsonWithDic:(NSDictionary *)dic; ++ (NSString *)dc_convertToJsonWithDic:(id)obj; @end NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.m b/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.m index bcdd6437..9304bc17 100644 --- a/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.m +++ b/doric-iOS/Pod/Classes/Util/Category/NSString+JsonString.m @@ -9,11 +9,11 @@ #import "DoricUtil.h" @implementation NSString (JsonString) -+ (NSString *)dc_convertToJsonWithDic:(NSDictionary *)dic { ++ (NSString *)dc_convertToJsonWithDic:(id)obj { NSError *err; - NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWritingPrettyPrinted error:&err]; + NSData *jsonData = [NSJSONSerialization dataWithJSONObject:obj options:NSJSONWritingPrettyPrinted error:&err]; NSString *jsonStr = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; - + if (err) { DoricLog(NSStringFromSelector(_cmd), @"Convert dictionary to json string failed."); return nil; diff --git a/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.h b/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.h deleted file mode 100644 index c156a740..00000000 --- a/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// DoricJSRemoteArgType.h -// Doric -// -// Created by Insomnia on 2019/11/7. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -typedef NS_ENUM(NSUInteger, DoricJSRemoteArgType) { - DoricJSRemoteArgTypeNil = 0, - DoricJSRemoteArgTypeNumber, - DoricJSRemoteArgTypeBool, - DoricJSRemoteArgTypeString, - DoricJSRemoteArgTypeObject, - DoricJSRemoteArgTypeArray, -}; - -DoricJSRemoteArgType DoricargTypeWithArg(id arg); - - -NS_ASSUME_NONNULL_END diff --git a/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.m b/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.m deleted file mode 100644 index a056199a..00000000 --- a/doric-iOS/Pod/Classes/Util/DoricJSRemoteArgType.m +++ /dev/null @@ -1,21 +0,0 @@ -// -// DoricJSRemoteArgType.m -// Doric -// -// Created by Insomnia on 2019/11/7. -// - -#import "DoricJSRemoteArgType.h" -DoricJSRemoteArgType DoricargTypeWithArg(id arg) { - DoricJSRemoteArgType type = DoricJSRemoteArgTypeNil; - if ([arg isKindOfClass:[NSNumber class]]) { - type = DoricJSRemoteArgTypeNumber; - }else if ([arg isKindOfClass:[NSString class]]) { - type = DoricJSRemoteArgTypeString; - }else if ([arg isKindOfClass:[NSDictionary class]]) { - type = DoricJSRemoteArgTypeObject; - }else if ([arg isKindOfClass:[NSMutableArray class]]) { - type = DoricJSRemoteArgTypeArray; - } - return type; -}