-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathNWDebug.m
174 lines (133 loc) · 4.65 KB
/
NWDebug.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
based on "Nibless" project from http://lapcatsoftware.com/
http://lapcatsoftware.com/downloads/Nibless.zip
*/
#import "NWDebug.h"
#import <objc/runtime.h>
//#include <narwhal.h>
static int global_argc;
static char **global_argv;
static char **global_envp;
static WebView *global_webView;
extern int narwhal(JSGlobalContextRef _context, JSValueRef *_exception, int argc, char *argv[], char *envp[], int runShell);
@implementation NWApplication
- (id)init
{
if ((self = [super init]))
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[self setDelegate:[[NWAppDelegate alloc] init]];
[pool release];
}
return self;
}
- (void)dealloc
{
id delegate = [self delegate];
if (delegate)
{
[self setDelegate:nil];
[delegate release];
}
[super dealloc];
}
@end
@implementation NWAppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
webView = global_webView;// [[WebView alloc] init]
id inspector = [webView inspector];
Class inspectorClass = [[inspector class] class];
//class_addMethod(inspectorClass, @selector(isSelectorExcludedFromWebScript:), (IMP)NW_isSelectorExcludedFromWebScript, "B@::");
method_exchangeImplementations(
class_getClassMethod(inspectorClass, @selector(isSelectorExcludedFromWebScript:)),
class_getClassMethod([NWInspector class], @selector(isSelectorExcludedFromWebScript:))
);
id win = [webView windowScriptObject];
[win setValue:inspector forKey:@"_inspector"];
JSGlobalContextRef context = [[webView mainFrame] globalContext];
JSValueRef exception = NULL;
narwhal(context, &exception, global_argc, global_argv, global_envp, 0);
if (exception) {
exit(1);
}
[self replStart];
//[[webView inspector] show:self];
//[inspector showConsole:self];
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}
- (void)replPrompt
{
fprintf(stdout, "js> ");
fflush(stdout);
[stdinFileHandle readInBackgroundAndNotify];
}
- (void)replStart
{
stdinFileHandle = [[NSFileHandle fileHandleWithStandardInput] retain];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReadStdin:)
name:NSFileHandleReadCompletionNotification
object:stdinFileHandle];
[self replPrompt];
}
- (void)didReadStdin:(NSNotification *)aNotification
{
NSData *data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem];
NSString *string = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
JSStringRef source = JSStringCreateWithUTF8CString([string UTF8String]);
EvaluateREPL([[webView mainFrame] globalContext], source);
JSStringRelease(source);
[self replPrompt];
}
@end
@implementation NWInspector
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
//if (aSelector == @selector(nameAtIndex:))
return NO;
//return YES;
}
@end
@implementation NSBundle (NWDebug)
+ (BOOL)NW_loadNibNamed:(NSString *)aNibName owner:(id)owner
{
BOOL didLoadNib = YES;
if (aNibName != nil || owner != NSApp)
{
didLoadNib = [self NW_loadNibNamed:aNibName owner:owner];
}
return didLoadNib;
}
@end
void NW_install_NSBundle_hack()
{
Class bundleClass = [NSBundle class];
Method originalMethod = class_getClassMethod(bundleClass, @selector(loadNibNamed:owner:));
Method categoryMethod = class_getClassMethod(bundleClass, @selector(NW_loadNibNamed:owner:));
method_exchangeImplementations(originalMethod, categoryMethod);
}
void NW_register_defaults()
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
[[NSUserDefaults standardUserDefaults] registerDefaults:
[NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES], @"WebKitDeveloperExtras",
[NSNumber numberWithBool:NO], @"WebKitInspectorAttached",
[NSNumber numberWithBool:YES], @"WebKit Web Inspector Setting - debuggerEnabled",
[NSNumber numberWithBool:YES], @"WebKit Web Inspector Setting - profilerEnabled",
@"scripts", @"WebKit Web Inspector Setting - lastActivePanel",
//[NSNumber numberWithBool:YES], @"WebKit Web Inspector Setting - resourceTrackingEnabled",
nil]];
[pool release];
}
WebView * NW_init(int argc, char *argv[], char *envp[])
{
NW_install_NSBundle_hack();
NW_register_defaults();
global_argc = argc;
global_argv = argv;
global_envp = envp;
global_webView = [[WebView alloc] init];
return global_webView;
}