gjs r76 - in trunk: gi gjs test/js



Author: otaylor
Date: Sun Nov  9 01:21:37 2008
New Revision: 76
URL: http://svn.gnome.org/viewvc/gjs?rev=76&view=rev

Log:
Improve error message when 'new' is ommitted

Bug 558882 â Bad error if you omit 'new'

gjs/jsapi-util.[ch]: Add gjs_check_constructing() that checks if
  we are called as a constructor, and if not throws.

gi/{param.c,boxed.c,object.c,union.c}: Use gjs_check_constructing()
  to catch 'obj = SomeObject()' and give a good error message.


Modified:
   trunk/gi/boxed.c
   trunk/gi/object.c
   trunk/gi/param.c
   trunk/gi/union.c
   trunk/gjs/jsapi-util.c
   trunk/gjs/jsapi-util.h
   trunk/test/js/testEverythingBasic.js

Modified: trunk/gi/boxed.c
==============================================================================
--- trunk/gi/boxed.c	(original)
+++ trunk/gi/boxed.c	Sun Nov  9 01:21:37 2008
@@ -211,6 +211,9 @@
     JSObject *proto;
     gboolean is_proto;
 
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
     priv = g_slice_new0(Boxed);
 
     GJS_INC_COUNTER(boxed);

Modified: trunk/gi/object.c
==============================================================================
--- trunk/gi/object.c	(original)
+++ trunk/gi/object.c	Sun Nov  9 01:21:37 2008
@@ -617,6 +617,9 @@
     JSClass *obj_class;
     JSClass *proto_class;
 
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
     priv = g_slice_new0(ObjectInstance);
 
     GJS_INC_COUNTER(object);

Modified: trunk/gi/param.c
==============================================================================
--- trunk/gi/param.c	(original)
+++ trunk/gi/param.c	Sun Nov  9 01:21:37 2008
@@ -155,6 +155,9 @@
     JSObject *proto;
     gboolean is_proto;
 
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
     priv = g_slice_new0(Param);
 
     GJS_INC_COUNTER(param);

Modified: trunk/gi/union.c
==============================================================================
--- trunk/gi/union.c	(original)
+++ trunk/gi/union.c	Sun Nov  9 01:21:37 2008
@@ -211,6 +211,9 @@
     JSObject *proto;
     gboolean is_proto;
 
+    if (!gjs_check_constructing(context))
+        return JS_FALSE;
+
     priv = g_slice_new0(Union);
 
     GJS_INC_COUNTER(boxed);

Modified: trunk/gjs/jsapi-util.c
==============================================================================
--- trunk/gjs/jsapi-util.c	(original)
+++ trunk/gjs/jsapi-util.c	Sun Nov  9 01:21:37 2008
@@ -384,6 +384,18 @@
     return prototype;
 }
 
+gboolean
+gjs_check_constructing(JSContext *context)
+{
+    if (!JS_IsConstructing(context)) {
+        gjs_throw(context,
+                  "Constructor called as normal method. Use 'new SomeObject()' not 'SomeObject()'");
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
 void*
 gjs_get_instance_private_dynamic(JSContext      *context,
                                  JSObject       *obj,

Modified: trunk/gjs/jsapi-util.h
==============================================================================
--- trunk/gjs/jsapi-util.h	(original)
+++ trunk/gjs/jsapi-util.h	Sun Nov  9 01:21:37 2008
@@ -125,6 +125,7 @@
                                               JSFunctionSpec  *fs,
                                               JSPropertySpec  *static_ps,
                                               JSFunctionSpec  *static_fs);
+gboolean    gjs_check_constructing           (JSContext       *context);
 void*       gjs_get_instance_private_dynamic (JSContext       *context,
                                               JSObject        *obj,
                                               JSClass         *static_clasp,

Modified: trunk/test/js/testEverythingBasic.js
==============================================================================
--- trunk/test/js/testEverythingBasic.js	(original)
+++ trunk/test/js/testEverythingBasic.js	Sun Nov  9 01:21:37 2008
@@ -1,5 +1,8 @@
 const Everything = imports.gi.Everything;
 
+// We use Gio to have some objects that we know exist
+const Gio = imports.gi.Gio;
+
 const INT8_MIN = (-128);
 const INT16_MIN = (-32767-1);
 const INT32_MIN = (-2147483647-1);
@@ -68,4 +71,12 @@
     assertRaises(function() { return Everything.test_size(-42); });
 }
 
+function testBadConstructor() {
+    try {
+	Gio.AppLaunchContext();
+    } catch (e) {
+	assert(e.message.indexOf("Constructor called as normal method") >= 0);
+    }
+}
+
 gjstestRun();



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]