[gjs/wip/ptomato/mozjs31prep: 3/3] WIP - Switch to JSNative property accessors



commit 4b203a89d8625be55c2bd5cf54e5c4eac6bc125a
Author: Philip Chimento <philip chimento gmail com>
Date:   Thu Oct 6 23:36:17 2016 -0700

    WIP - Switch to JSNative property accessors

 gi/function.cpp   |   20 +++++---------------
 gi/gerror.cpp     |   49 ++++++++++++++-----------------------------------
 gjs/byteArray.cpp |   36 ++++++++++++++----------------------
 3 files changed, 33 insertions(+), 72 deletions(-)
---
diff --git a/gi/function.cpp b/gi/function.cpp
index d8af8a5..53f08e7 100644
--- a/gi/function.cpp
+++ b/gi/function.cpp
@@ -1357,19 +1357,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;
@@ -1482,11 +1476,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 e331721..594d527 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,26 @@ 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)
+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);
 
     if (priv == NULL)
         return false;
@@ -182,7 +173,7 @@ error_get_message(JSContext *context, JS::HandleObject obj,
         return false;
     }
 
-    return gjs_string_from_utf8(context, priv->gerror->message, -1, vp.address());
+    return gjs_string_from_utf8(context, priv->gerror->message, -1, args.rval());
 }
 
 static bool
@@ -305,21 +296,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/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 6a9b847..b9955a7 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -219,17 +219,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. */
 
@@ -237,28 +234,26 @@ 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;
@@ -845,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]