changeset 15:547b12bed241

Added more error correction to JS interface
author Atul Varma <avarma@mozilla.com>
date Sun, 11 Apr 2010 22:16:41 -0700
parents 7a3d7ce08927
children 1e83bbfdd190
files JavaScriptQuasimode.m
diffstat 1 files changed, 49 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/JavaScriptQuasimode.m	Sun Apr 11 21:54:07 2010 -0700
+++ b/JavaScriptQuasimode.m	Sun Apr 11 22:16:41 2010 -0700
@@ -4,12 +4,30 @@
 #import "Quasimode.h"
 #import "JavaScriptQuasimode.h"
 
+#define RAISE_ERROR(reason) \
+        return raiseError(ctx, exception, reason);
+
+#define ENSURE_ARGCOUNT(number) \
+        if (argumentCount < number) \
+          RAISE_ERROR("not enough arguments");
+
 typedef struct {
   const char *name;
   JSObjectCallAsFunctionCallback callback;
 } JSQuasimodeMethod;
 
 static JSValueRef
+raiseError(JSContextRef ctx,
+           JSValueRef* exception,
+           const char *reason)
+{
+  JSStringRef str = JSStringCreateWithUTF8CString(reason);
+  JSValueRef argument = JSValueMakeString(ctx, str);
+  *exception = JSObjectMakeError(ctx, 1, &argument, NULL);
+  return JSValueMakeUndefined(ctx);
+}
+
+static JSValueRef
 method_setSize(JSContextRef ctx,
                JSObjectRef function,
                JSObjectRef thisObject,
@@ -17,19 +35,28 @@
                const JSValueRef arguments[],
                JSValueRef* exception)
 {
-  if (argumentCount >= 2 &&
-      JSValueIsNumber(ctx, arguments[0]) &&
-      JSValueIsNumber(ctx, arguments[1])) {
-    Quasimode *quasimode = (Quasimode *) JSObjectGetPrivate(thisObject);
-    int width = (int) JSValueToNumber(ctx, arguments[0], NULL);
-    int height = (int) JSValueToNumber(ctx, arguments[1], NULL);
+  ENSURE_ARGCOUNT(2);
+
+  if (!JSValueIsNumber(ctx, arguments[0]))
+    RAISE_ERROR("first argument must be a number");
+
+  if (!JSValueIsNumber(ctx, arguments[1]))
+    RAISE_ERROR("second argument must be a number");
 
-    if (width > 0 && height > 0) {
-      NSSize size = {width, height};
+  Quasimode *quasimode = (Quasimode *) JSObjectGetPrivate(thisObject);
+  if (!quasimode)
+    return raiseError(ctx, exception,
+                      "method must be invoked on a Quasimode instance");
 
-      [[quasimode window] setContentSize: size];
-    }
-  }
+  int width = (int) JSValueToNumber(ctx, arguments[0], NULL);
+  int height = (int) JSValueToNumber(ctx, arguments[1], NULL);
+
+  if (width <= 0) RAISE_ERROR("width must be positive");
+  if (height <= 0) RAISE_ERROR("height must be positive");
+
+  NSSize size = {width, height};
+
+  [[quasimode window] setContentSize: size];
   return JSValueMakeUndefined(ctx);
 }
 
@@ -41,15 +68,17 @@
            const JSValueRef arguments[],
            JSValueRef* exception)
 {
-  if (argumentCount >= 1 &&
-      JSValueIsString(ctx, arguments[0])) {
-    Quasimode *quasimode = (Quasimode *) JSObjectGetPrivate(thisObject);
-    JSStringRef str = JSValueToStringCopy(ctx, arguments[0], NULL);
-    size_t len = JSStringGetMaximumUTF8CStringSize(str);
-    char buf[len];
-    JSStringGetUTF8CString(str, buf, len);
-    printf("%s\n", buf);
-  }
+  ENSURE_ARGCOUNT(1);
+
+  if (!JSValueIsString(ctx, arguments[0]))
+    RAISE_ERROR("first argument must be a string");
+
+  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);
 }