# HG changeset patch # User Atul Varma # Date 1271038628 25200 # Node ID cd9bef04e813a5d57989caa572b844b7df46f9b4 # Parent ea0dcbf6a0bcfdbf11aea725e7b684f2c3b40fcc Renamed MyApp to Quasimode, made a new JSClass that reflects the app to JS diff -r ea0dcbf6a0bc -r cd9bef04e813 .hgignore --- a/.hgignore Sun Apr 11 18:33:24 2010 -0700 +++ b/.hgignore Sun Apr 11 19:17:08 2010 -0700 @@ -1,2 +1,2 @@ syntax: glob -MyApp +Quasimode diff -r ea0dcbf6a0bc -r cd9bef04e813 Makefile --- a/Makefile Sun Apr 11 18:33:24 2010 -0700 +++ b/Makefile Sun Apr 11 19:17:08 2010 -0700 @@ -1,3 +1,3 @@ all: - clang MyApp.m -oMyApp -framework AppKit -framework WebKit \ + clang Quasimode.m -oQuasimode -framework AppKit -framework WebKit \ -framework JavaScriptCore diff -r ea0dcbf6a0bc -r cd9bef04e813 MyApp.m --- a/MyApp.m Sun Apr 11 18:33:24 2010 -0700 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,97 +0,0 @@ -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import -#import - -JSValueRef boop(JSContextRef ctx, - JSObjectRef function, - JSObjectRef thisObject, - size_t argumentCount, - const JSValueRef arguments[], - JSValueRef* exception) -{ - if (argumentCount >= 1 && - JSValueIsNumber(ctx, arguments[0])) { - printf("BOOP IS CALLED WITH %f\n", - JSValueToNumber(ctx, arguments[0], NULL)); - } - return JSValueMakeUndefined(ctx); -} - -@interface MyApp : NSObject { - NSWindow *window; - WebView *view; -} -- (id)init; -@end - -@implementation MyApp -- (void)defineJSGlobals { - JSGlobalContextRef ctx = [[view mainFrame] globalContext]; - - JSObjectRef global = JSContextGetGlobalObject(ctx); - JSStringRef boopStr = JSStringCreateWithUTF8CString("boop"); - JSObjectRef func = JSObjectMakeFunctionWithCallback(ctx, boopStr, boop); - JSObjectSetProperty(ctx, global, boopStr, func, NULL, - kJSPropertyAttributeNone); -} - -- (id)init { - if (self = [super init]) { - NSRect rect = {{20, 20}, - {100, 100}}; - - window = [[NSWindow alloc] initWithContentRect: rect - styleMask: NSBorderlessWindowMask - backing: NSBackingStoreBuffered - defer: YES]; - - view = [[WebView alloc] initWithFrame: rect - frameName: nil - groupName: nil]; - - NSURL *url = [NSURL URLWithString: @"http://127.0.0.1:8000"]; - NSURLRequest *req = [ - NSURLRequest requestWithURL: url - cachePolicy: NSURLRequestReloadIgnoringLocalCacheData - timeoutInterval: 5 - ]; - - [view setDrawsBackground: NO]; - [[[view mainFrame] frameView] setAllowsScrolling: NO]; - [[view mainFrame] loadRequest: req]; - - [self defineJSGlobals]; - - [window setOpaque: NO]; - [window setBackgroundColor: [NSColor colorWithCalibratedRed: 0.0 - green: 0.0 - blue: 0.0 - alpha: 0.0]]; - [window setContentView: view]; - [window setLevel: NSScreenSaverWindowLevel]; - [window makeKeyAndOrderFront: self]; - } - return self; -} -@end - -int main(int argc, const char *argv[]) -{ - NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; - - [NSApplication sharedApplication]; - - MyApp *app = [[MyApp alloc] init]; - - [NSApp run]; - - return 0; -} diff -r ea0dcbf6a0bc -r cd9bef04e813 Quasimode.m --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Quasimode.m Sun Apr 11 19:17:08 2010 -0700 @@ -0,0 +1,131 @@ +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import +#import + +void QModeClassInitialize(JSContextRef ctx, JSObjectRef object); + +JSValueRef boop(JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception); + +@interface QMode : NSObject { + NSWindow *window; + WebView *view; +} +- (id)init; +- (void)setSizeWithX:(int)X Y:(int)Y; +@end + +@implementation QMode +- (void)setSizeWithX:(int)X Y:(int)Y { + printf("setSize(%d, %d)\n", X, Y); + } + +- (void)defineJSGlobals { + JSGlobalContextRef ctx = [[view mainFrame] globalContext]; + + JSClassDefinition classDef = kJSClassDefinitionEmpty; + classDef.initialize = QModeClassInitialize; + classDef.className = "QMode"; + + JSClassRef class = JSClassCreate(&classDef); + JSObjectRef myApp = JSObjectMake(ctx, class, self); + JSObjectRef global = JSContextGetGlobalObject(ctx); + JSStringRef myAppStr = JSStringCreateWithUTF8CString("myApp"); + + JSObjectSetProperty(ctx, global, myAppStr, myApp, + kJSPropertyAttributeReadOnly | + kJSPropertyAttributeDontDelete, + NULL); + } + +- (id)init { + if (self = [super init]) { + NSRect rect = {{20, 20}, + {100, 100}}; + + window = [[NSWindow alloc] initWithContentRect: rect + styleMask: NSBorderlessWindowMask + backing: NSBackingStoreBuffered + defer: YES]; + + view = [[WebView alloc] initWithFrame: rect + frameName: nil + groupName: nil]; + + NSURL *url = [NSURL URLWithString: @"http://127.0.0.1:8000"]; + NSURLRequest *req = [ + NSURLRequest requestWithURL: url + cachePolicy: NSURLRequestReloadIgnoringLocalCacheData + timeoutInterval: 5 + ]; + + [view setDrawsBackground: NO]; + [[[view mainFrame] frameView] setAllowsScrolling: NO]; + [[view mainFrame] loadRequest: req]; + + [self defineJSGlobals]; + + [window setOpaque: NO]; + [window setBackgroundColor: [NSColor colorWithCalibratedRed: 0.0 + green: 0.0 + blue: 0.0 + alpha: 0.0]]; + [window setContentView: view]; + [window setLevel: NSScreenSaverWindowLevel]; + [window makeKeyAndOrderFront: self]; + } + return self; + } +@end + +void QModeClassInitialize(JSContextRef ctx, JSObjectRef object) +{ + JSStringRef boopStr = JSStringCreateWithUTF8CString("boop"); + JSObjectRef func = JSObjectMakeFunctionWithCallback(ctx, boopStr, boop); + + JSObjectSetProperty(ctx, object, boopStr, func, + kJSPropertyAttributeReadOnly | + kJSPropertyAttributeDontDelete, + NULL); +} + +JSValueRef boop(JSContextRef ctx, + JSObjectRef function, + JSObjectRef thisObject, + size_t argumentCount, + const JSValueRef arguments[], + JSValueRef* exception) +{ + if (argumentCount >= 1 && + JSValueIsNumber(ctx, arguments[0])) { + QMode *app = (QMode *) JSObjectGetPrivate(thisObject); + [app setSizeWithX: (int) JSValueToNumber(ctx, arguments[0], NULL) + Y: 0]; + } + return JSValueMakeUndefined(ctx); +} + +int main(int argc, const char *argv[]) +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + + [NSApplication sharedApplication]; + + QMode *app = [[QMode alloc] init]; + + [NSApp run]; + + return 0; +}