Skip to content

Commit

Permalink
feat($JPEngine): Add 'po'(print object) and 'bt'(backtrack) command f…
Browse files Browse the repository at this point in the history
…or debugging in Safari.

'po': print object, inspect the corresponding Objective-C object of JS object  when debugging in Safari;
'bt': backtrack, print call stack of the thread which invoking this JS script.

[USAGE]:

In Safari debubger/console

> po(viewController)
``` output
  "<UIViewController: 0x7fcc75074c00>" = $2
```

> bt()
``` output
0 "0 Demo 0x000000010c1dc88a JPForwardInvocation + 90"
1 "1 CoreFoundation 0x0000000118ecba2e ___forwarding___ + 526"
2 "2 CoreFoundation 0x0000000118ecb798 _CF_forwarding_prep_0 + 120"
3 "3 AlipayWallet 0x000000010c22c003 -[ALPLauncherController setSelectedIndex:] + 291"
4 "4 UIKit 0x000000011c812ada +[UIView(Animation) performWithoutAnimation:] + 90"
5 "5 UIKit 0x000000011c954148 -[UITabBarController _setViewControllers:animated:] + 3761"
6 "6 UIKit 0x000000011c95430b -[UITabBarController setViewControllers:animated:] + 119"
7 "7 Demo 0x000000010aaa78d1 -[UITabBarController(APPageSwitch) alipayPageSwitchSetViewControllers:animated:…"
8 "8 Demo 0x000000010c22536d -[ALPLauncherController initTabBarView] + 1373"
9 "9 Demo 0x000000010c223e16 __29-[ALPLauncherController init]_block_invoke + 38"
10 "10 Demo 0x000000010ad13f69 __34-[SSAuthServiceImpl loginCallback]_block_invoke + 128"
11 "11 Demo 0x000000010a68afa6 APMainCall + 150"
12 "12 Demo 0x000000010ad13eb3 -[SSAuthServiceImpl loginCallback] + 100"
13 "13 Demo 0x000000010ad1188c -[SSAuthServiceImpl tryLoginAsync] + 455"
14 "14 Demo 0x000000010c223b3f -[ALPLauncherController init] + 1215"
15 "15 Demo 0x000000010c231c64 -[LauncherAppDelegate rootControllerInApplication:] + 132"
16 "16 Demo 0x000000010a70ac6a -[DFMicroApplication rootController] + 154"
...
```
  • Loading branch information
卡迩 committed Oct 14, 2016
1 parent 068b26b commit 91c53ea
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions JSPatch/JPEngine.m
Expand Up @@ -138,6 +138,10 @@ + (void)jp_fixMethodSignature
static NSMutableDictionary *_protocolTypeEncodeDict;
static NSMutableArray *_pointersToRelease;

#ifdef DEBUG
static NSArray *_JSLastCallStack;
#endif

static void (^_exceptionBlock)(NSString *log) = ^void(NSString *log) {
NSCAssert(NO, log);
};
Expand All @@ -154,6 +158,17 @@ + (void)startEngine

JSContext *context = [[JSContext alloc] init];

#ifdef DEBUG
context[@"po"] = ^JSValue*(JSValue *obj) {
id ocObject = formatJSToOC(obj);
return [JSValue valueWithObject:[ocObject description] inContext:_context];
};

context[@"bt"] = ^JSValue*() {
return [JSValue valueWithObject:_JSLastCallStack inContext:_context];
};
#endif

context[@"_OC_defineClass"] = ^(NSString *classDeclaration, JSValue *instanceMethods, JSValue *classMethods) {
return defineClass(classDeclaration, instanceMethods, classMethods);
};
Expand Down Expand Up @@ -632,6 +647,9 @@ static void addMethodToProtocol(Protocol* protocol, NSString *selectorName, NSSt

static void JPForwardInvocation(__unsafe_unretained id assignSlf, SEL selector, NSInvocation *invocation)
{
#ifdef DEBUG
_JSLastCallStack = [NSThread callStackSymbols];
#endif
BOOL deallocFlag = NO;
id slf = assignSlf;
NSMethodSignature *methodSignature = [invocation methodSignature];
Expand Down

0 comments on commit 91c53ea

Please sign in to comment.