[gjs/wip/ptomato/mozjs31: 9/9] WIP - jsapi-util



commit da91c8d9dcd62e06f20b77f097d5a48be9c5b556
Author: Philip Chimento <philip endlessm com>
Date:   Tue Oct 25 18:55:29 2016 -0700

    WIP - jsapi-util

 gjs/jsapi-util.cpp |  145 +++++++++++++++++++++++++---------------------------
 gjs/jsapi-util.h   |   23 ++++----
 2 files changed, 81 insertions(+), 87 deletions(-)
---
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 23b2af0..0060c18 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -47,8 +47,7 @@ gjs_util_error_quark (void)
 static JSClass global_class = {
     "GjsGlobal", JSCLASS_GLOBAL_FLAGS_WITH_SLOTS(GJS_GLOBAL_SLOT_LAST),
     JS_PropertyStub, JS_DeletePropertyStub, JS_PropertyStub, JS_StrictPropertyStub,
-    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL,
-    JSCLASS_NO_OPTIONAL_MEMBERS
+    JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, NULL
 };
 
 /**
@@ -171,7 +170,7 @@ gjs_object_require_property(JSContext             *context,
 {
     value.setUndefined();
 
-    if (G_UNLIKELY(!JS_GetPropertyById(context, obj, property_name, value.address())))
+    if (G_UNLIKELY(!JS_GetPropertyById(context, obj, property_name, value)))
         return false;
 
     if (G_LIKELY(!value.isUndefined()))
@@ -190,7 +189,7 @@ gjs_object_require_property_value(JSContext       *cx,
                                   bool            *value)
 {
     JS::RootedValue prop_value(cx);
-    if (JS_GetPropertyById(cx, obj, property_name, prop_value.address()) &&
+    if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
         prop_value.isBoolean()) {
         *value = prop_value.toBoolean();
         return true;
@@ -209,7 +208,7 @@ gjs_object_require_property_value(JSContext       *cx,
                                   int32_t         *value)
 {
     JS::RootedValue prop_value(cx);
-    if (JS_GetPropertyById(cx, obj, property_name, prop_value.address()) &&
+    if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
         prop_value.isInt32()) {
         *value = prop_value.toInt32();
         return true;
@@ -229,7 +228,7 @@ gjs_object_require_property_value(JSContext       *cx,
                                   char           **value)
 {
     JS::RootedValue prop_value(cx);
-    if (JS_GetPropertyById(cx, obj, property_name, prop_value.address()) &&
+    if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
         gjs_string_to_utf8(cx, prop_value, value)) {
         return true;
     }
@@ -247,7 +246,7 @@ gjs_object_require_property_value(JSContext              *cx,
                                   JS::MutableHandleObject value)
 {
     JS::RootedValue prop_value(cx);
-    if (JS_GetPropertyById(cx, obj, property_name, prop_value.address()) &&
+    if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
         prop_value.isObject()) {
         value.set(&prop_value.toObject());
         return true;
@@ -266,7 +265,7 @@ gjs_object_require_converted_property_value(JSContext       *cx,
                                             uint32_t        *value)
 {
     JS::RootedValue prop_value(cx);
-    if (JS_GetPropertyById(cx, obj, property_name, prop_value.address()) &&
+    if (JS_GetPropertyById(cx, obj, property_name, &prop_value) &&
         JS::ToUint32(cx, prop_value, value)) {
         return true;
     }
@@ -320,27 +319,25 @@ gjs_build_string_array(JSContext   *context,
         elems.append(element);
     }
 
-    return JS_NewArrayObject(context, elems.length(), &elems[0]);
+    return JS_NewArrayObject(context, elems);
 }
 
 JSObject*
-gjs_define_string_array(JSContext   *context,
-                        JSObject    *in_object,
-                        const char  *array_name,
-                        gssize       array_length,
-                        const char **array_values,
-                        unsigned     attrs)
+gjs_define_string_array(JSContext       *context,
+                        JS::HandleObject in_object,
+                        const char      *array_name,
+                        gssize           array_length,
+                        const char     **array_values,
+                        unsigned         attrs)
 {
-    JSObject *array;
-
     JSAutoRequest ar(context);
 
-    array = gjs_build_string_array(context, array_length, (char **) array_values);
+    JS::RootedObject array(context,
+        gjs_build_string_array(context, array_length, (char **) array_values));
 
     if (array != NULL) {
-        if (!JS_DefineProperty(context, in_object,
-                               array_name, JS::ObjectValue(*array),
-                               NULL, NULL, attrs))
+        JS::RootedValue v_array(context, JS::ObjectValue(*array));
+        if (!JS_DefineProperty(context, in_object, array_name, v_array, attrs))
             array = NULL;
     }
 
@@ -440,7 +437,7 @@ _gjs_g_utf8_make_valid (const char *name)
  */
 char*
 gjs_value_debug_string(JSContext      *context,
-                       JS::Value       value)
+                       JS::HandleValue value)
 {
     char *bytes;
     char *debugstr;
@@ -459,7 +456,7 @@ gjs_value_debug_string(JSContext      *context,
             /* Specifically the Call object (see jsfun.c in spidermonkey)
              * does not have a toString; there may be others also.
              */
-            JSClass *klass;
+            const JSClass *klass;
 
             klass = JS_GetClass(&value.toObject());
             if (klass != NULL) {
@@ -499,8 +496,8 @@ gjs_value_debug_string(JSContext      *context,
 }
 
 static char *
-utf8_exception_from_non_gerror_value(JSContext *cx,
-                                     JS::Value  exc)
+utf8_exception_from_non_gerror_value(JSContext      *cx,
+                                     JS::HandleValue exc)
 {
     char *utf8_exception = NULL;
     JS::RootedString exc_str(cx, JS::ToString(cx, exc));
@@ -510,9 +507,9 @@ utf8_exception_from_non_gerror_value(JSContext *cx,
 }
 
 bool
-gjs_log_exception_full(JSContext *context,
-                       JS::Value  exc,
-                       JSString  *message)
+gjs_log_exception_full(JSContext      *context,
+                       JS::HandleValue exc,
+                       JSString       *message)
 {
     char *utf8_exception, *utf8_message;
     bool is_syntax;
@@ -623,8 +620,8 @@ gjs_log_exception(JSContext  *context)
 
     JS_BeginRequest(context);
 
-    JS::RootedValue exc(context, JS::UndefinedValue());
-    if (!JS_GetPendingException(context, exc.address()))
+    JS::RootedValue exc(context);
+    if (!JS_GetPendingException(context, &exc))
         goto out;
 
     JS_ClearPendingException(context);
@@ -640,49 +637,50 @@ gjs_log_exception(JSContext  *context)
 }
 
 static void
-try_to_chain_stack_trace(JSContext *src_context,
-                         JSContext *dst_context,
-                         JS::Value  src_exc)
+try_to_chain_stack_trace(JSContext      *src_context,
+                         JSContext      *dst_context,
+                         JS::HandleValue src_exc)
 {
     /* append current stack of dst_context to stack trace for src_exc.
      * we bail if anything goes wrong, just using the src_exc unmodified
      * in that case. */
-    JS::Value chained, src_stack, dst_stack, new_stack;
-    JSString *new_stack_str;
-
-    JS_BeginRequest(src_context);
-    JS_BeginRequest(dst_context);
+    JSAutoRequest src_ar(src_context);
+    JSAutoRequest dst_ar(dst_context);
 
     if (!src_exc.isObject())
-        goto out; // src_exc doesn't have a stack trace
+        return;  // src_exc doesn't have a stack trace
 
     /* create a new exception in dst_context to get a stack trace */
+    JS::RootedValue chained(dst_context);
     gjs_throw_literal(dst_context, "Chained exception");
     if (!(JS_GetPendingException(dst_context, &chained) && chained.isObject()))
-        goto out; // gjs_throw_literal didn't work?!
+        return;  // gjs_throw_literal didn't work?!
     JS_ClearPendingException(dst_context);
 
+    JS::RootedObject chained_obj(dst_context, &chained.toObject());
+    JS::RootedValue dst_stack(dst_context);
     /* get stack trace for src_exc and chained */
-    if (!(JS_GetProperty(dst_context, &chained.toObject(),
-                         "stack", &dst_stack) &&
+    if (!(JS_GetProperty(dst_context, chained_obj, "stack", &dst_stack) &&
           dst_stack.isString()))
-        goto out; // couldn't get chained stack
-    if (!(JS_GetProperty(src_context, &src_exc.toObject(),
-                         "stack", &src_stack) &&
+        return;  // couldn't get chained stack
+
+    JS::RootedObject src_exc_obj(src_context, &src_exc.toObject());
+    JS::RootedValue src_stack(src_context);
+    if (!(JS_GetProperty(src_context, src_exc_obj, "stack", &src_stack) &&
           src_stack.isString()))
-        goto out; // couldn't get source stack
+        return;  // couldn't get source stack
 
     /* add chained exception's stack trace to src_exc */
-    new_stack_str = JS_ConcatStrings(dst_context, src_stack.toString(),
-        dst_stack.toString());
+    JS::RootedString src_string(dst_context, src_stack.toString());
+    JS::RootedString dst_string(dst_context, dst_stack.toString());
+    JS::RootedString new_stack_str(dst_context,
+        JS_ConcatStrings(dst_context, src_string, dst_string));
+
     if (new_stack_str==NULL)
-        goto out; // couldn't concatenate src and dst stacks?!
-    new_stack = JS::StringValue(new_stack_str);
-    JS_SetProperty(dst_context, &src_exc.toObject(), "stack", &new_stack);
+        return;  // couldn't concatenate src and dst stacks?!
 
- out:
-    JS_EndRequest(dst_context);
-    JS_EndRequest(src_context);
+    JS::RootedValue new_stack(src_context, JS::StringValue(new_stack_str));
+    JS_SetProperty(src_context, src_exc_obj, "stack", new_stack);
 }
 
 bool
@@ -695,7 +693,7 @@ gjs_move_exception(JSContext      *src_context,
     JS_BeginRequest(dest_context);
 
     /* NOTE: src and dest could be the same. */
-    JS::Value exc;
+    JS::RootedValue exc(src_context);
     if (JS_GetPendingException(src_context, &exc)) {
         if (src_context != dest_context) {
             /* try to add the current stack of dest_context to the
@@ -717,19 +715,18 @@ gjs_move_exception(JSContext      *src_context,
 }
 
 bool
-gjs_call_function_value(JSContext             *context,
-                        JSObject              *obj,
-                        JS::Value              fval,
-                        unsigned               argc,
-                        JS::Value             *argv,
-                        JS::MutableHandleValue rval)
+gjs_call_function_value(JSContext                  *context,
+                        JS::HandleObject            obj,
+                        JS::HandleValue             fval,
+                        const JS::HandleValueArray& args,
+                        JS::MutableHandleValue      rval)
 {
     bool result;
 
     JS_BeginRequest(context);
 
     result = JS_CallFunctionValue(context, obj, fval,
-                                  argc, argv, rval.address());
+                                  args, rval);
 
     if (result)
         gjs_schedule_gc_if_needed(context);
@@ -919,15 +916,14 @@ gjs_strip_unix_shebang(const char  *script,
 }
 
 bool
-gjs_eval_with_scope(JSContext    *context,
-                    JSObject     *object,
-                    const char   *script,
-                    gssize        script_len,
-                    const char   *filename,
-                    JS::Value    *retval_p)
+gjs_eval_with_scope(JSContext             *context,
+                    JS::HandleObject       object,
+                    const char            *script,
+                    ssize_t                script_len,
+                    const char            *filename,
+                    JS::MutableHandleValue retval)
 {
     int start_line_number = 1;
-    JS::Value retval = JS::UndefinedValue();
     JSAutoRequest ar(context);
 
     if (script_len < 0)
@@ -944,16 +940,16 @@ gjs_eval_with_scope(JSContext    *context,
     }
 
     if (!object)
-        object = JS_NewObject(context, NULL, NULL, NULL);
+        object = JS_NewObject(context, NULL, JS::NullPtr(), JS::NullPtr());
 
     JS::CompileOptions options(context);
     options.setUTF8(true)
            .setFileAndLine(filename, start_line_number)
-           .setSourcePolicy(JS::CompileOptions::LAZY_SOURCE);
+           .setSourceIsLazy(true);
 
-    js::RootedObject rootedObj(context, object);
+    JS::RootedObject rootedObj(context, object);
 
-    if (!JS::Evaluate(context, rootedObj, options, script, script_len, &retval))
+    if (!JS::Evaluate(context, rootedObj, options, script, script_len, retval))
         return false;
 
     gjs_schedule_gc_if_needed(context);
@@ -967,8 +963,5 @@ gjs_eval_with_scope(JSContext    *context,
     gjs_debug(GJS_DEBUG_CONTEXT,
               "Script evaluation succeeded");
 
-    if (retval_p)
-        *retval_p = retval;
-
     return true;
 }
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index fa9e365..b25201c 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -372,12 +372,13 @@ void        gjs_throw_g_error                (JSContext       *context,
 bool        gjs_log_exception                (JSContext       *context);
 bool        gjs_move_exception               (JSContext       *src_context,
                                               JSContext       *dest_context);
-bool        gjs_log_exception_full           (JSContext       *context,
-                                              JS::Value        exc,
-                                              JSString        *message);
 
-char*       gjs_value_debug_string           (JSContext       *context,
-                                              JS::Value        value);
+bool gjs_log_exception_full(JSContext      *context,
+                            JS::HandleValue exc,
+                            JSString       *message);
+
+char *gjs_value_debug_string(JSContext      *context,
+                             JS::HandleValue value);
 
 bool gjs_call_function_value(JSContext             *context,
                              JSObject              *obj,
@@ -441,12 +442,12 @@ bool gjs_context_get_frame_info(JSContext                              *context,
                                 mozilla::Maybe<JS::MutableHandleValue>& fileName,
                                 mozilla::Maybe<JS::MutableHandleValue>& lineNumber);
 
-bool              gjs_eval_with_scope        (JSContext    *context,
-                                              JSObject     *object,
-                                              const char   *script,
-                                              gssize        script_len,
-                                              const char   *filename,
-                                              JS::Value    *retval_p);
+bool gjs_eval_with_scope(JSContext             *context,
+                         JS::HandleObject       object,
+                         const char            *script,
+                         ssize_t                script_len,
+                         const char            *filename,
+                         JS::MutableHandleValue retval);
 
 typedef enum {
   GJS_STRING_CONSTRUCTOR,


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