changeset 7:cd9bef04e813

Renamed MyApp to Quasimode, made a new JSClass that reflects the app to JS
author Atul Varma <avarma@mozilla.com>
date Sun, 11 Apr 2010 19:17:08 -0700
parents ea0dcbf6a0bc
children 353aee1ddf91
files .hgignore Makefile MyApp.m Quasimode.m
diffstat 4 files changed, 133 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- 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
--- 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
--- 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 <Foundation/NSAutoreleasePool.h>
-#import <Foundation/NSURL.h>
-#import <Foundation/NSURLRequest.h>
-#import <AppKit/NSApplication.h>
-#import <AppKit/NSWindow.h>
-#import <AppKit/NSColor.h>
-#import <WebKit/WebView.h>
-#import <WebKit/WebFrame.h>
-#import <WebKit/WebFrameView.h>
-#import <WebKit/DOMEvents.h>
-#import <JavaScriptCore/JavaScriptCore.h>
-
-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;
-}
--- /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 <Foundation/NSAutoreleasePool.h>
+#import <Foundation/NSURL.h>
+#import <Foundation/NSURLRequest.h>
+#import <AppKit/NSApplication.h>
+#import <AppKit/NSWindow.h>
+#import <AppKit/NSColor.h>
+#import <WebKit/WebView.h>
+#import <WebKit/WebFrame.h>
+#import <WebKit/WebFrameView.h>
+#import <WebKit/DOMEvents.h>
+#import <JavaScriptCore/JavaScriptCore.h>
+
+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;
+}