[gjs] js: Switch to JSNative property accessors



commit 20b8c6d4405ba62f3749921fed0ab15cd751730f
Author: Philip Chimento <philip endlessm com>
Date:   Wed Oct 12 18:21:04 2016 -0700

    js: Switch to JSNative property accessors
    
    Per https://bugzilla.mozilla.org/show_bug.cgi?id=992977,
    JSPropertyOp-style property accessors are going to be removed in a future
    version of SpiderMonkey. Instead, we use JSNative property accessors.
    This allows us to use the JS_PSG and JS_PSGS macros in our JSPropertySpec
    arrays, making things safer by eliminating the callback casts.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=742249

 gi/function.cpp   |   20 ++++-----------
 gi/gerror.cpp     |   70 ++++++++++++++++++++--------------------------------
 gi/gtype.cpp      |   16 ++++-------
 gi/ns.cpp         |   15 ++++-------
 gjs/byteArray.cpp |   37 +++++++++++----------------
 5 files changed, 59 insertions(+), 99 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index 82a5692..85cf723 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1354,19 +1354,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)
 {
+    GJS_GET_PRIV(context, argc, vp, rec, to, Function, 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;
@@ -1479,11 +1473,7 @@ 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)
-    },
+    JS_PSG("length", get_num_arguments, JSPROP_READONLY | JSPROP_PERMANENT),
     JS_PS_END
 };
 
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index 46dfb95..661c090 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -42,13 +42,6 @@ typedef struct {
     GError *gerror; /* NULL if we are the prototype and not an instance */
 } Error;
 
-enum {
-    PROP_0,
-    PROP_DOMAIN,
-    PROP_CODE,
-    PROP_MESSAGE
-};
-
 extern struct JSClass gjs_error_class;
 
 static void define_error_properties(JSContext *, JSObject *);
@@ -150,28 +143,28 @@ error_finalize(JSFreeOp *fop,
     g_slice_free(Error, priv);
 }
 
-static bool
-error_get_domain(JSContext *context, JS::HandleObject obj,
-                 JS::HandleId id, JS::MutableHandleValue vp)
+static JSBool
+error_get_domain(JSContext *context,
+                 unsigned   argc,
+                 JS::Value *vp)
 {
-    Error *priv;
-
-    priv = priv_from_js(context, obj);
+    GJS_GET_PRIV(context, argc, vp, args, obj, Error, priv);
 
     if (priv == NULL)
         return false;
 
-    vp.setInt32(priv->domain);
+    args.rval().setInt32(priv->domain);
     return true;
 }
 
-static bool
-error_get_message(JSContext *context, JS::HandleObject obj,
-                  JS::HandleId id, JS::MutableHandleValue vp)
+static JSBool
+error_get_message(JSContext *context,
+                  unsigned   argc,
+                  JS::Value *vp)
 {
-    Error *priv;
-
-    priv = priv_from_js(context, obj);
+    GJS_GET_PRIV(context, argc, vp, args, obj, Error, priv);
+    JS::Value retval;
+    bool ret = false;
 
     if (priv == NULL)
         return false;
@@ -182,16 +175,19 @@ error_get_message(JSContext *context, JS::HandleObject obj,
         return false;
     }
 
-    return gjs_string_from_utf8(context, priv->gerror->message, -1, vp.address());
+    // FIXME: root gjs_string_from_utf8()
+    ret = gjs_string_from_utf8(context, priv->gerror->message, -1, &retval);
+    if (ret)
+        args.rval().set(retval);
+    return ret;
 }
 
-static bool
-error_get_code(JSContext *context, JS::HandleObject obj,
-               JS::HandleId id, JS::MutableHandleValue vp)
+static JSBool
+error_get_code(JSContext *context,
+               unsigned   argc,
+               JS::Value *vp)
 {
-    Error *priv;
-
-    priv = priv_from_js(context, obj);
+    GJS_GET_PRIV(context, argc, vp, args, obj, Error, priv);
 
     if (priv == NULL)
         return false;
@@ -202,7 +198,7 @@ error_get_code(JSContext *context, JS::HandleObject obj,
         return false;
     }
 
-    vp.setInt32(priv->gerror->code);
+    args.rval().setInt32(priv->gerror->code);
     return true;
 }
 
@@ -305,21 +301,9 @@ struct JSClass gjs_error_class = {
 /* We need to shadow all fields of GError, to prevent calling the getter from GBoxed
    (which would trash memory accessing the instance private data) */
 JSPropertySpec gjs_error_proto_props[] = {
-    { "domain", PROP_DOMAIN,
-      GJS_MODULE_PROP_FLAGS | JSPROP_READONLY,
-      JSOP_WRAPPER((JSPropertyOp)error_get_domain),
-      JSOP_WRAPPER(JS_StrictPropertyStub)
-    },
-    { "code", PROP_CODE,
-      GJS_MODULE_PROP_FLAGS | JSPROP_READONLY,
-      JSOP_WRAPPER((JSPropertyOp)error_get_code),
-      JSOP_WRAPPER(JS_StrictPropertyStub)
-    },
-    { "message", PROP_MESSAGE,
-      GJS_MODULE_PROP_FLAGS | JSPROP_READONLY,
-      JSOP_WRAPPER((JSPropertyOp)error_get_message),
-      JSOP_WRAPPER(JS_StrictPropertyStub)
-    },
+    JS_PSG("domain", error_get_domain, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY),
+    JS_PSG("code", error_get_code, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY),
+    JS_PSG("message", error_get_message, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY),
     JS_PS_END
 };
 
diff --git a/gi/gtype.cpp b/gi/gtype.cpp
index 6e6018f..6c3518c 100644
--- a/gi/gtype.cpp
+++ b/gi/gtype.cpp
@@ -85,18 +85,17 @@ to_string_func(JSContext *context,
     return ret;
 }
 
-static bool
+static JSBool
 get_name_func (JSContext *context,
-               JS::HandleObject obj,
-               JS::HandleId id,
-               JS::MutableHandleValue vp)
+               unsigned   argc,
+               JS::Value *vp)
 {
+    GJS_GET_PRIV(context, argc, vp, rec, obj, void, priv);
     GType gtype;
     bool ret;
     JS::Value retval;
-    JS::CallReceiver rec = JS::CallReceiverFromVp(vp.address());
 
-    gtype = GPOINTER_TO_SIZE(priv_from_js(context, obj));
+    gtype = GPOINTER_TO_SIZE(priv);
 
     if (gtype == 0) {
         rec.rval().setNull();
@@ -111,10 +110,7 @@ get_name_func (JSContext *context,
 
 /* Properties */
 JSPropertySpec gjs_gtype_proto_props[] = {
-    { "name", 0,
-      JSPROP_READONLY | JSPROP_PERMANENT | JSPROP_SHARED,
-      JSOP_WRAPPER((JSPropertyOp)get_name_func),
-      JSOP_WRAPPER(JS_StrictPropertyStub) },
+    JS_PSG("name", get_name_func, JSPROP_READONLY | JSPROP_PERMANENT),
     JS_PS_END,
 };
 
diff --git a/gi/ns.cpp b/gi/ns.cpp
index 38d6490..b89f948 100644
--- a/gi/ns.cpp
+++ b/gi/ns.cpp
@@ -126,23 +126,20 @@ ns_new_resolve(JSContext *context,
     return ret;
 }
 
-static bool
+static JSBool
 get_name (JSContext *context,
-          JS::HandleObject obj,
-          JS::HandleId id,
-          JS::Value       *vp)
+          unsigned   argc,
+          JS::Value *vp)
 {
-    Ns *priv;
+    GJS_GET_PRIV(context, argc, vp, args, obj, Ns, priv);
     JS::Value retval;
     bool ret = false;
 
-    priv = priv_from_js(context, obj);
-
     if (priv == NULL)
         goto out;
 
     if (gjs_string_from_utf8(context, priv->gi_namespace, -1, &retval)) {
-        *vp = retval;
+        args.rval().set(retval);
         ret = true;
     }
 
@@ -191,7 +188,7 @@ struct JSClass gjs_ns_class = {
 };
 
 JSPropertySpec gjs_ns_proto_props[] = {
-    { "__name__", 0, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY, { (JSPropertyOp)get_name, NULL } },
+    JS_PSG("__name__", get_name, GJS_MODULE_PROP_FLAGS | JSPROP_READONLY),
     JS_PS_END
 };
 
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index a2e689d..2e051e2 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -218,17 +218,14 @@ byte_array_get_prop(JSContext *context,
     return true;
 }
 
-static bool
+static JSBool
 byte_array_length_getter(JSContext *context,
-                         JS::HandleObject obj,
-                         JS::HandleId id,
-                         JS::MutableHandleValue value_p)
+                         unsigned   argc,
+                         JS::Value *vp)
 {
-    ByteArrayInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, args, to, ByteArrayInstance, priv);
     gsize len = 0;
 
-    priv = priv_from_js(context, obj);
-
     if (priv == NULL)
         return true; /* prototype, not an instance. */
 
@@ -236,33 +233,32 @@ byte_array_length_getter(JSContext *context,
         len = priv->array->len;
     else if (priv->bytes != NULL)
         len = g_bytes_get_size (priv->bytes);
-    value_p.set(gjs_value_from_gsize(len));
+    args.rval().set(gjs_value_from_gsize(len));
     return true;
 }
 
-static bool
+static JSBool
 byte_array_length_setter(JSContext *context,
-                         JS::HandleObject obj,
-                         JS::HandleId id,
-                         bool strict,
-                         JS::MutableHandleValue value_p)
+                         unsigned   argc,
+                         JS::Value *vp)
 {
-    ByteArrayInstance *priv;
+    GJS_GET_PRIV(context, argc, vp, args, to, ByteArrayInstance, priv);
     gsize len = 0;
 
-    priv = priv_from_js(context, obj);
-
     if (priv == NULL)
         return true; /* prototype, not instance */
 
     byte_array_ensure_array(priv);
 
-    if (!gjs_value_to_gsize(context, value_p, &len)) {
+    // COMPAT: Indexing JS::CallArgs should provide a handle in mozjs31
+    JS::RootedValue arg(context, args[0]);
+    if (!gjs_value_to_gsize(context, arg, &len)) {
         gjs_throw(context,
                   "Can't set ByteArray length to non-integer");
         return false;
     }
     g_byte_array_set_size(priv->array, len);
+    args.rval().setUndefined();
     return true;
 }
 
@@ -844,11 +840,8 @@ gjs_byte_array_peek_data (JSContext       *context,
 }
 
 JSPropertySpec gjs_byte_array_proto_props[] = {
-    { "length", 0,
-      JSPROP_PERMANENT,
-      JSOP_WRAPPER ((JSPropertyOp) byte_array_length_getter),
-      JSOP_WRAPPER ((JSStrictPropertyOp) byte_array_length_setter),
-    },
+    JS_PSGS("length", byte_array_length_getter, byte_array_length_setter,
+            JSPROP_PERMANENT),
     JS_PS_END
 };
 


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