rename dir
This commit is contained in:
32
doric-iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h
Normal file
32
doric-iOS/Pod/Classes/Engine/DoricJSCoreExecutor.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
//
|
||||
// DoricJSCoreExecutor.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricJSExecutorProtocol.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricJSCoreExecutor : NSObject <DoricJSExecutorProtocol>
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
64
doric-iOS/Pod/Classes/Engine/DoricJSCoreExecutor.m
Normal file
64
doric-iOS/Pod/Classes/Engine/DoricJSCoreExecutor.m
Normal file
@@ -0,0 +1,64 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
//
|
||||
// DoricJSCoreExecutor.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
|
||||
@interface DoricJSCoreExecutor ()
|
||||
|
||||
@property(nonatomic, strong) JSContext *jsContext;
|
||||
|
||||
@end
|
||||
|
||||
@implementation DoricJSCoreExecutor
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_jsContext = [[JSContext alloc] init];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)checkJSException {
|
||||
if (self.jsContext.exception) {
|
||||
NSString *errMsg = [NSString stringWithFormat:@"%@ (line %@ in the generated bundle)\n/***StackTrace***/\n%@/***StackTrace***/", self.jsContext.exception, self.jsContext.exception[@"line"], self.jsContext.exception[@"stack"]];
|
||||
@throw [[NSException alloc] initWithName:@"DoricJS" reason:errMsg userInfo:nil];
|
||||
}
|
||||
}
|
||||
|
||||
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source {
|
||||
NSString *ret = [[self.jsContext evaluateScript:script withSourceURL:[NSURL URLWithString:source]] toString];
|
||||
[self checkJSException];
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj {
|
||||
self.jsContext[name] = obj;
|
||||
[self checkJSException];
|
||||
}
|
||||
|
||||
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args {
|
||||
JSValue *obj = [self.jsContext objectForKeyedSubscript:objName];
|
||||
JSValue *ret = [obj invokeMethod:funcName withArguments:args];
|
||||
[self checkJSException];
|
||||
return ret;
|
||||
}
|
||||
|
||||
@end
|
46
doric-iOS/Pod/Classes/Engine/DoricJSEngine.h
Normal file
46
doric-iOS/Pod/Classes/Engine/DoricJSEngine.h
Normal file
@@ -0,0 +1,46 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
//
|
||||
// DoricJSEngine.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/26.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
#import "DoricRegistry.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricJSEngine : NSObject
|
||||
|
||||
@property(nonatomic, strong) dispatch_queue_t jsQueue;
|
||||
|
||||
@property(nonatomic, strong) DoricRegistry *registry;
|
||||
|
||||
- (void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source;
|
||||
|
||||
- (void)destroyContext:(NSString *)contextId;
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method, ...;
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args;
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
189
doric-iOS/Pod/Classes/Engine/DoricJSEngine.m
Normal file
189
doric-iOS/Pod/Classes/Engine/DoricJSEngine.m
Normal file
@@ -0,0 +1,189 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
//
|
||||
// DoricJSEngine.m
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/26.
|
||||
//
|
||||
|
||||
#import "DoricJSEngine.h"
|
||||
#import "DoricJSExecutorProtocol.h"
|
||||
#import "DoricJSCoreExecutor.h"
|
||||
#import "DoricJSRemoteExecutor.h"
|
||||
#import "DoricConstant.h"
|
||||
#import "DoricUtil.h"
|
||||
#import "DoricBridgeExtension.h"
|
||||
|
||||
@interface DoricJSEngine ()
|
||||
@property(nonatomic, strong) id <DoricJSExecutorProtocol> jsExecutor;
|
||||
@property(nonatomic, strong) NSMutableDictionary *timers;
|
||||
@property(nonatomic, strong) DoricBridgeExtension *bridgeExtension;
|
||||
@end
|
||||
|
||||
@implementation DoricJSEngine
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
_jsQueue = dispatch_queue_create("doric.jsengine", DISPATCH_QUEUE_SERIAL);
|
||||
_bridgeExtension = [[DoricBridgeExtension alloc] init];
|
||||
dispatch_async(_jsQueue, ^() {
|
||||
self.timers = [[NSMutableDictionary alloc] init];
|
||||
// Debug: 切换
|
||||
// self.jsExecutor = [[DoricJSRemoteExecutor alloc] init];
|
||||
self.jsExecutor = [DoricJSCoreExecutor new];
|
||||
self.registry = [[DoricRegistry alloc] init];
|
||||
[self initJSExecutor];
|
||||
[self initDoricEnvironment];
|
||||
});
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)initJSExecutor {
|
||||
__weak typeof(self) _self = self;
|
||||
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_ENVIRONMENT obj:@{
|
||||
@"platform": @"iOS",
|
||||
@"platformVersion": [[UIDevice currentDevice] systemVersion],
|
||||
@"appName": infoDictionary[@"CFBundleName"],
|
||||
@"appVersion": infoDictionary[@"CFBundleShortVersionString"],
|
||||
@"screenWidth": @([[UIScreen mainScreen] bounds].size.width),
|
||||
@"screenHeight": @([[UIScreen mainScreen] bounds].size.height),
|
||||
}];
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_LOG obj:^(NSString *type, NSString *message) {
|
||||
DoricLog(@"JS:%@", message);
|
||||
}];
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_EMPTY obj:^() {
|
||||
|
||||
}];
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_REQUIRE obj:^(NSString *name) {
|
||||
__strong typeof(_self) self = _self;
|
||||
if (!self) return NO;
|
||||
NSString *content = [self.registry acquireJSBundle:name];
|
||||
if (!content) {
|
||||
DoricLog(@"require js bundle:%@ is empty", name);
|
||||
return NO;
|
||||
}
|
||||
@try {
|
||||
[self.jsExecutor loadJSScript:[self packageModuleScript:name content:content]
|
||||
source:[@"Module://" stringByAppendingString:name]];
|
||||
} @catch (NSException *e) {
|
||||
DoricLog(@"require js bundle:%@ error,for %@", name, e.reason);
|
||||
}
|
||||
return YES;
|
||||
}];
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_SET
|
||||
obj:^(NSNumber *timerId, NSNumber *interval, NSNumber *isInterval) {
|
||||
__strong typeof(_self) self = _self;
|
||||
NSString *timerId_str = [timerId stringValue];
|
||||
BOOL repeat = [isInterval boolValue];
|
||||
NSTimer *timer = [NSTimer timerWithTimeInterval:[interval doubleValue] / 1000 target:self selector:@selector(callbackTimer:) userInfo:@{@"timerId": timerId, @"repeat": isInterval} repeats:repeat];
|
||||
[self.timers setValue:timer forKey:timerId_str];
|
||||
dispatch_async(dispatch_get_main_queue(), ^() {
|
||||
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
|
||||
});
|
||||
}];
|
||||
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_TIMER_CLEAR
|
||||
obj:^(NSString *timerId) {
|
||||
__strong typeof(_self) self = _self;
|
||||
NSTimer *timer = [self.timers valueForKey:timerId];
|
||||
if (timer) {
|
||||
[timer invalidate];
|
||||
[self.timers removeObjectForKey:timerId];
|
||||
}
|
||||
}];
|
||||
|
||||
[self.jsExecutor injectGlobalJSObject:INJECT_BRIDGE obj:^(NSString *contextId, NSString *module, NSString *method, NSString *callbackId, id argument) {
|
||||
return [self.bridgeExtension callNativeWithContextId:contextId module:module method:method callbackId:callbackId argument:argument];
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)initDoricEnvironment {
|
||||
[self loadBuiltinJS:DORIC_BUNDLE_SANDBOX];
|
||||
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_LIB ofType:@"js" inDirectory:@"bundle"];
|
||||
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
||||
[self.jsExecutor loadJSScript:[self packageModuleScript:DORIC_MODULE_LIB content:jsContent]
|
||||
source:[@"Module://" stringByAppendingString:DORIC_MODULE_LIB]];
|
||||
}
|
||||
|
||||
- (void)loadBuiltinJS:(NSString *)fileName {
|
||||
NSString *path = [DoricBundle() pathForResource:DORIC_BUNDLE_SANDBOX ofType:@"js" inDirectory:@"bundle"];
|
||||
NSString *jsContent = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
|
||||
[self.jsExecutor loadJSScript:jsContent source:[@"Assets://" stringByAppendingString:fileName]];
|
||||
}
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method, ... {
|
||||
va_list args;
|
||||
va_start(args, method);
|
||||
JSValue *ret = [self invokeDoricMethod:method arguments:args];
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method arguments:(va_list)args {
|
||||
NSMutableArray *array = [[NSMutableArray alloc] init];
|
||||
id arg = va_arg(args, id);
|
||||
while (arg != nil) {
|
||||
[array addObject:arg];
|
||||
arg = va_arg(args, JSValue *);
|
||||
}
|
||||
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:array];
|
||||
}
|
||||
|
||||
- (JSValue *)invokeDoricMethod:(NSString *)method argumentsArray:(NSArray *)args {
|
||||
return [self.jsExecutor invokeObject:GLOBAL_DORIC method:method args:args];
|
||||
}
|
||||
|
||||
- (NSString *)packageContextScript:(NSString *)contextId content:(NSString *)content {
|
||||
NSString *ret = [NSString stringWithFormat:TEMPLATE_CONTEXT_CREATE, content, contextId, contextId, contextId];
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (NSString *)packageModuleScript:(NSString *)moduleName content:(NSString *)content {
|
||||
NSString *ret = [NSString stringWithFormat:TEMPLATE_MODULE, moduleName, content];
|
||||
return ret;
|
||||
}
|
||||
|
||||
- (void)prepareContext:(NSString *)contextId script:(NSString *)script source:(NSString *)source {
|
||||
[self.jsExecutor loadJSScript:[self packageContextScript:contextId content:script]
|
||||
source:[@"Context://" stringByAppendingString:source]];
|
||||
}
|
||||
|
||||
- (void)destroyContext:(NSString *)contextId {
|
||||
[self.jsExecutor loadJSScript:[NSString stringWithFormat:TEMPLATE_CONTEXT_DESTROY, contextId]
|
||||
source:[@"_Context://" stringByAppendingString:contextId]];
|
||||
}
|
||||
|
||||
- (void)callbackTimer:(NSTimer *)timer {
|
||||
NSDictionary *userInfo = timer.userInfo;
|
||||
NSNumber *timerId = [userInfo valueForKey:@"timerId"];
|
||||
NSNumber *repeat = [userInfo valueForKey:@"repeat"];
|
||||
__weak typeof(self) _self = self;
|
||||
dispatch_async(self.jsQueue, ^() {
|
||||
__strong typeof(_self) self = _self;
|
||||
@try {
|
||||
[self invokeDoricMethod:DORIC_TIMER_CALLBACK, timerId, nil];
|
||||
} @catch (NSException *exception) {
|
||||
DoricLog(@"Timer Callback error:%@", exception.reason);
|
||||
}
|
||||
if (![repeat boolValue]) {
|
||||
[self.timers removeObjectForKey:[timerId stringValue]];
|
||||
}
|
||||
});
|
||||
}
|
||||
@end
|
38
doric-iOS/Pod/Classes/Engine/DoricJSExecutorProtocol.h
Normal file
38
doric-iOS/Pod/Classes/Engine/DoricJSExecutorProtocol.h
Normal file
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
//
|
||||
// DoricJSEngineProtocal.h
|
||||
// Doric
|
||||
//
|
||||
// Created by pengfei.zhou on 2019/7/25.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import <JavaScriptCore/JavaScriptCore.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@protocol DoricJSExecutorProtocol <NSObject>
|
||||
|
||||
- (NSString *)loadJSScript:(NSString *)script source:(NSString *)source;
|
||||
|
||||
- (void)injectGlobalJSObject:(NSString *)name obj:(id)obj;
|
||||
|
||||
- (JSValue *)invokeObject:(NSString *)objName method:(NSString *)funcName args:(NSArray *)args;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
35
doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h
Normal file
35
doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.h
Normal file
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* 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.h
|
||||
// Pods
|
||||
//
|
||||
// Created by 王劲鹏 on 2019/10/31.
|
||||
//
|
||||
|
||||
#import <Foundation/Foundation.h>
|
||||
#import "DoricJSExecutorProtocol.h"
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
@interface DoricJSRemoteExecutor : NSObject <DoricJSExecutorProtocol>
|
||||
|
||||
@property(nonatomic, strong) dispatch_semaphore_t semaphore;
|
||||
|
||||
- (void)close;
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
207
doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m
Normal file
207
doric-iOS/Pod/Classes/Engine/DoricJSRemoteExecutor.m
Normal file
@@ -0,0 +1,207 @@
|
||||
/*
|
||||
* 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 <SocketRocket/SRWebSocket.h>
|
||||
#import "DoricUtil.h"
|
||||
#import "DoricJSRemoteArgType.h"
|
||||
#import "NSString+JsonString.h"
|
||||
|
||||
static NSString * const kUrlStr = @"ws://192.168.24.240:2080";
|
||||
|
||||
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 () <SRWebSocketDelegate>
|
||||
@property(nonatomic, strong) SRWebSocket *srWebSocket;
|
||||
@property(nonatomic, strong) NSMutableDictionary <NSString *, id> *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);
|
||||
}
|
||||
|
||||
- (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");
|
||||
}
|
||||
|
||||
#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,
|
||||
@"javaValues": 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)close {
|
||||
[self.srWebSocket close];
|
||||
}
|
||||
|
||||
#pragma mark - Properties
|
||||
- (SRWebSocket *)srWebSocket {
|
||||
if (!_srWebSocket) {
|
||||
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:kUrlStr] 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
|
Reference in New Issue
Block a user