[gjs/wip/ptomato/mozjs45prep: 39/39] WIP - byte array stuff



commit 8aff4de3270ca4d13af8dbad070f42a71f670d26
Author: Philip Chimento <philip chimento gmail com>
Date:   Tue Apr 11 23:44:59 2017 -0700

    WIP - byte array stuff

 gjs/byteArray.cpp |   76 +++++++++++++++++-----------------------------------
 1 files changed, 25 insertions(+), 51 deletions(-)
---
diff --git a/gjs/byteArray.cpp b/gjs/byteArray.cpp
index 06390d0..3b16adf 100644
--- a/gjs/byteArray.cpp
+++ b/gjs/byteArray.cpp
@@ -52,6 +52,8 @@ GJS_NATIVE_CONSTRUCTOR_DECLARE(byte_array);
 static void   byte_array_finalize      (JSFreeOp     *fop,
                                         JSObject     *obj);
 
+static bool gjs_byte_array_ensure_proto(JSContext *, JS::MutableHandleObject);
+static JSObject *gjs_byte_array_define_constructor(JSContext *, JS::HandleObject);
 
 struct JSClass gjs_byte_array_class = {
     "ByteArray",
@@ -507,33 +509,22 @@ to_gbytes_func(JSContext *context,
     return true;
 }
 
-/* Ensure that the module and class objects exists, and that in turn
- * ensures that JS_InitClass has been called. */
-static JSObject *
-byte_array_get_prototype(JSContext *context)
-{
-    JS::RootedValue retval(context,
-        gjs_get_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE));
-
-    if (!retval.isObject()) {
-        if (!gjs_eval_with_scope(context, nullptr,
-                                 "imports.byteArray.ByteArray.prototype;", -1,
-                                 "<internal>", &retval))
-            g_error ("Could not import byte array prototype\n");
-    }
-
-    return &retval.toObject();
-}
-
 static JSObject*
 byte_array_new(JSContext *context)
 {
     ByteArrayInstance *priv;
 
-    JS::RootedObject proto(context, byte_array_get_prototype(context));
+    JS::RootedObject proto(context);
+    if (!gjs_byte_array_ensure_proto(context, &proto))
+        return nullptr;
     JS::RootedObject array(context,
         JS_NewObjectWithGivenProto(context, &gjs_byte_array_class, proto));
 
+    if (!array) {
+        gjs_throw(context, "failed to create byte array");
+        return nullptr;
+    }
+
     priv = g_slice_new0(ByteArrayInstance);
 
     g_assert(priv_from_js(context, array) == NULL);
@@ -756,23 +747,12 @@ JSObject *
 gjs_byte_array_from_byte_array (JSContext *context,
                                 GByteArray *array)
 {
-    ByteArrayInstance *priv;
-
     g_return_val_if_fail(context != NULL, NULL);
     g_return_val_if_fail(array != NULL, NULL);
 
-    JS::RootedObject proto(context, byte_array_get_prototype(context));
-    JS::RootedObject object(context,
-        JS_NewObjectWithGivenProto(context, &gjs_byte_array_class, proto));
-
-    if (!object) {
-        gjs_throw(context, "failed to create byte array");
-        return NULL;
-    }
+    JS::RootedObject object(context, byte_array_new(context));
 
-    priv = g_slice_new0(ByteArrayInstance);
-    g_assert(priv_from_js(context, object) == NULL);
-    JS_SetPrivate(object, priv);
+    ByteArrayInstance *priv = priv_from_js(context, object);
     priv->array = g_byte_array_new();
     priv->array->data = (guint8*) g_memdup(array->data, array->len);
     priv->array->len = array->len;
@@ -826,18 +806,20 @@ gjs_byte_array_peek_data (JSContext       *context,
     }
 }
 
-JSPropertySpec gjs_byte_array_proto_props[] = {
+static JSPropertySpec gjs_byte_array_proto_props[] = {
     JS_PSGS("length", byte_array_length_getter, byte_array_length_setter,
             JSPROP_PERMANENT),
     JS_PS_END
 };
 
-JSFunctionSpec gjs_byte_array_proto_funcs[] = {
+static JSFunctionSpec gjs_byte_array_proto_funcs[] = {
     JS_FS("toString", to_string_func, 0, 0),
     JS_FS("toGBytes", to_gbytes_func, 0, 0),
     JS_FS_END
 };
 
+static JSFunctionSpec *gjs_byte_array_static_funcs = nullptr;
+
 static JSFunctionSpec gjs_byte_array_module_funcs[] = {
     JS_FS("fromString", from_string_func, 1, 0),
     JS_FS("fromArray", from_array_func, 1, 0),
@@ -845,28 +827,20 @@ static JSFunctionSpec gjs_byte_array_module_funcs[] = {
     JS_FS_END
 };
 
+GJS_DEFINE_ENSURE_PROTO(byte_array)
+GJS_EXPORT_PROTO(byte_array, "ByteArray")
+
 bool
 gjs_define_byte_array_stuff(JSContext              *context,
                             JS::MutableHandleObject module)
 {
-    JSObject *prototype;
-
     module.set(JS_NewPlainObject(context));
 
-    prototype = JS_InitClass(context, module, nullptr, &gjs_byte_array_class,
-                             gjs_byte_array_constructor,
-                             0,
-                             &gjs_byte_array_proto_props[0],
-                             &gjs_byte_array_proto_funcs[0],
-                             NULL,
-                             NULL);
-
-    if (!JS_DefineFunctions(context, module, &gjs_byte_array_module_funcs[0]))
-        return false;
-
-    g_assert(gjs_get_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE).isUndefined());
-    gjs_set_global_slot(context, GJS_GLOBAL_SLOT_BYTE_ARRAY_PROTOTYPE,
-                        JS::ObjectOrNullValue(prototype));
+    g_assert(((void) "gjs_define_byte_array_stuff should only be called once",
+              gjs_get_global_slot(context, GJS_GLOBAL_SLOT_PROTOTYPE_byte_array).isUndefined()));
 
-    return true;
+    JS::RootedObject prototype(context);
+    return gjs_byte_array_ensure_proto(context, &prototype) &&
+        gjs_byte_array_define_constructor(context, module) &&
+        JS_DefineFunctions(context, module, gjs_byte_array_module_funcs);
 }


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