changeset 17:8c5678d7b5e9

we now coerce args to their desired types in quasimode JS methods
author Atul Varma <avarma@mozilla.com>
date Sun, 11 Apr 2010 22:40:53 -0700
parents 1e83bbfdd190
children 78807eea31b7
files JavaScriptQuasimode.m
diffstat 1 files changed, 25 insertions(+), 23 deletions(-) [+]
line wrap: on
line diff
--- a/JavaScriptQuasimode.m	Sun Apr 11 22:28:00 2010 -0700
+++ b/JavaScriptQuasimode.m	Sun Apr 11 22:40:53 2010 -0700
@@ -11,6 +11,12 @@
         if (argumentCount < number) \
           RAISE_ERROR("not enough arguments");
 
+#define RETURN_UNDEFINED \
+        return JSValueMakeUndefined(ctx);
+
+#define RETURN_IF_ERROR \
+        if (*exception) RETURN_UNDEFINED;
+
 typedef struct {
   const char *name;
   JSObjectCallAsFunctionCallback callback;
@@ -24,7 +30,8 @@
   JSStringRef str = JSStringCreateWithUTF8CString(reason);
   JSValueRef argument = JSValueMakeString(ctx, str);
   *exception = JSObjectMakeError(ctx, 1, &argument, NULL);
-  return JSValueMakeUndefined(ctx);
+
+  RETURN_UNDEFINED;
 }
 
 static JSValueRef
@@ -37,19 +44,16 @@
 {
   ENSURE_ARGCOUNT(2);
 
-  if (!JSValueIsNumber(ctx, arguments[0]))
-    RAISE_ERROR("first argument (width) must be a number");
-
-  if (!JSValueIsNumber(ctx, arguments[1]))
-    RAISE_ERROR("second argument (height) must be a number");
-
   Quasimode *quasimode = (Quasimode *) JSObjectGetPrivate(thisObject);
   if (!quasimode)
     return raiseError(ctx, exception,
                       "method must be invoked on a Quasimode instance");
 
-  int width = (int) JSValueToNumber(ctx, arguments[0], NULL);
-  int height = (int) JSValueToNumber(ctx, arguments[1], NULL);
+  int width = (int) JSValueToNumber(ctx, arguments[0], exception);
+  RETURN_IF_ERROR;
+
+  int height = (int) JSValueToNumber(ctx, arguments[1], exception);
+  RETURN_IF_ERROR;
 
   if (width <= 0) RAISE_ERROR("width must be positive");
   if (height <= 0) RAISE_ERROR("height must be positive");
@@ -57,7 +61,8 @@
   NSSize size = {width, height};
 
   [[quasimode window] setContentSize: size];
-  return JSValueMakeUndefined(ctx);
+
+  RETURN_UNDEFINED;
 }
 
 static JSValueRef
@@ -70,19 +75,16 @@
 {
   ENSURE_ARGCOUNT(2);
 
-  if (!JSValueIsNumber(ctx, arguments[0]))
-    RAISE_ERROR("first argument (x) must be a number");
-
-  if (!JSValueIsNumber(ctx, arguments[1]))
-    RAISE_ERROR("second argument (y) must be a number");
-
   Quasimode *quasimode = (Quasimode *) JSObjectGetPrivate(thisObject);
   if (!quasimode)
     return raiseError(ctx, exception,
                       "method must be invoked on a Quasimode instance");
 
-  int x = (int) JSValueToNumber(ctx, arguments[0], NULL);
-  int y = (int) JSValueToNumber(ctx, arguments[1], NULL);
+  int x = (int) JSValueToNumber(ctx, arguments[0], exception);
+  RETURN_IF_ERROR;
+
+  int y = (int) JSValueToNumber(ctx, arguments[1], exception);
+  RETURN_IF_ERROR;
 
   if (x < 0) RAISE_ERROR("x must be non-negative");
   if (y < 0) RAISE_ERROR("y must be non-negative");
@@ -90,7 +92,8 @@
   NSPoint point = {x, y};
 
   [[quasimode window] setFrameOrigin: point];
-  return JSValueMakeUndefined(ctx);
+
+  RETURN_UNDEFINED;
 }
 
 static JSValueRef
@@ -103,16 +106,15 @@
 {
   ENSURE_ARGCOUNT(1);
 
-  if (!JSValueIsString(ctx, arguments[0]))
-    RAISE_ERROR("first argument (message) must be a string");
+  JSStringRef str = JSValueToStringCopy(ctx, arguments[0], exception);
+  RETURN_IF_ERROR;
 
-  JSStringRef str = JSValueToStringCopy(ctx, arguments[0], NULL);
   size_t len = JSStringGetMaximumUTF8CStringSize(str);
   char buf[len];
   JSStringGetUTF8CString(str, buf, len);
   printf("%s\n", buf);
 
-  return JSValueMakeUndefined(ctx);
+  RETURN_UNDEFINED;
 }
 
 static JSQuasimodeMethod jsQuasimodeMethods[] = {