[gjs/wip/ptomato/mozjs31prep] WIP - use native accessors and macros



commit f15dcf05b6493ee3dc0145a76595cd3fd80043fc
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Oct 5 22:24:18 2016 -0700

    WIP - use native accessors and macros

 gi/boxed.cpp    |    8 ++++----
 gi/function.cpp |   52 +++++++++++++++++++++-------------------------------
 gi/union.cpp    |    8 ++++----
 3 files changed, 29 insertions(+), 39 deletions(-)
---
diff --git a/gi/boxed.cpp b/gi/boxed.cpp
index 2182c9b..5c38c3d 100644
--- a/gi/boxed.cpp
+++ b/gi/boxed.cpp
@@ -892,7 +892,7 @@ define_boxed_class_fields (JSContext *context,
     return true;
 }
 
-static bool
+static JSBool
 to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
@@ -943,12 +943,12 @@ struct JSClass gjs_boxed_class = {
 };
 
 JSPropertySpec gjs_boxed_proto_props[] = {
-    { NULL }
+    JS_PS_END
 };
 
 JSFunctionSpec gjs_boxed_proto_funcs[] = {
-    { "toString", JSOP_WRAPPER((JSNative)to_string_func), 0, 0 },
-    { NULL }
+    JS_FS("toString", to_string_func, 0, 0),
+    JS_FS_END
 };
 
 static bool
diff --git a/gi/function.cpp b/gi/function.cpp
index 70c4808..ce62ba6 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -60,6 +60,18 @@ typedef struct {
     GIFunctionInvoker invoker;
 } Function;
 
+#define THIS_FUNCTION(cx, argc, vp, args, priv)        \
+    JS::CallArgs args = JS::CallArgsFromVp(argc, vp);  \
+    JS::RootedValue thisv(cx, args.thisv());           \
+    if (!thisv.isObject()) {                           \
+        gjs_throw(cx, "Wrong type for this");          \
+        return false;                                  \
+    }                                                  \
+    JS::RootedObject thisobj(cx, &thisv.toObject());   \
+    Function *priv = priv_from_js(cx, thisobj);        \
+    if (priv == NULL)                                  \
+        return false
+
 extern struct JSClass gjs_function_class;
 
 /* Because we can't free the mmap'd data for a callback
@@ -1355,22 +1367,13 @@ function_finalize(JSFreeOp *fop,
     g_slice_free(Function, priv);
 }
 
-static bool
+static JSBool
 get_num_arguments (JSContext *context,
-                   JS::HandleObject obj,
-                   JS::HandleId id,
-                   JS::MutableHandleValue vp)
+                   unsigned   argc,
+                   JS::Value *vp)
 {
+    THIS_FUNCTION(context, argc, vp, args, priv);
     int n_args, n_jsargs, i;
-    JS::Value retval;
-    Function *priv;
-
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp.address());
-
-    priv = priv_from_js(context, obj);
-
-    if (priv == NULL)
-        return false;
 
     n_args = g_callable_info_get_n_args(priv->info);
     n_jsargs = 0;
@@ -1386,7 +1389,7 @@ get_num_arguments (JSContext *context,
             continue;
     }
 
-    rec.rval().setInt32(n_jsargs);
+    args.rval().setInt32(n_jsargs);
     return true;
 }
 
@@ -1395,7 +1398,7 @@ function_to_string (JSContext *context,
                     guint      argc,
                     JS::Value *vp)
 {
-    Function *priv;
+    THIS_FUNCTION(context, argc, vp, args, priv);
     gchar *string;
     bool free;
     JS::Value retval;
@@ -1404,15 +1407,6 @@ function_to_string (JSContext *context,
     GString *arg_names_str;
     gchar *arg_names;
 
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp);
-
-    if (rec.thisv().isNull()) {
-        gjs_throw(context, "this cannot be null");
-        return false;
-    }
-    JS::RootedObject self(context, &rec.thisv().toObject());
-
-    priv = priv_from_js (context, self);
     if (priv == NULL) {
         string = (gchar *) "function () {\n}";
         free = false;
@@ -1458,7 +1452,7 @@ function_to_string (JSContext *context,
 
  out:
     if (gjs_string_from_utf8(context, string, -1, &retval)) {
-        rec.rval().set(retval);
+        args.rval().set(retval);
         ret = true;
     }
 
@@ -1489,12 +1483,8 @@ struct JSClass gjs_function_class = {
 };
 
 JSPropertySpec gjs_function_proto_props[] = {
-    { "length", 0,
-      (JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_SHARED),
-      JSOP_WRAPPER((JSPropertyOp)get_num_arguments),
-      JSOP_WRAPPER(JS_StrictPropertyStub)
-    },
-    { NULL }
+    JS_PSG("length", get_num_arguments, JSPROP_READONLY | JSPROP_PERMANENT),
+    JS_PS_END
 };
 
 /* The original Function.prototype.toString complains when
diff --git a/gi/union.cpp b/gi/union.cpp
index 86cc755..6781efb 100644
--- a/gi/union.cpp
+++ b/gi/union.cpp
@@ -282,7 +282,7 @@ union_finalize(JSFreeOp *fop,
     g_slice_free(Union, priv);
 }
 
-static bool
+static JSBool
 to_string_func(JSContext *context,
                unsigned   argc,
                JS::Value *vp)
@@ -327,12 +327,12 @@ struct JSClass gjs_union_class = {
 };
 
 JSPropertySpec gjs_union_proto_props[] = {
-    { NULL }
+    JS_PS_END
 };
 
 JSFunctionSpec gjs_union_proto_funcs[] = {
-    { "toString", JSOP_WRAPPER((JSNative)to_string_func), 0, 0 },
-    { NULL }
+    JS_FS("toString", to_string_func, 0, 0),
+    JS_FS_END
 };
 
 bool


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