[gjs] gerror: Use JS::CaptureCurrentStack for stack info



commit 12ff7d15c5d43f0d6010fd7b9369834cacba1cd7
Author: Philip Chimento <philip endlessm com>
Date:   Fri Aug 11 13:11:20 2017 +0100

    gerror: Use JS::CaptureCurrentStack for stack info
    
    This uses the new facilities available in JSAPI to define the standard JS
    Error properties on GError-marshalled exceptions, rather than the
    workaround of creating a new Error object and reading its properties that
    was previously used in gjs_context_get_frame_info().
    
    https://bugzilla.gnome.org/show_bug.cgi?id=786183

 gi/gerror.cpp      |   36 +++++++++++++++++++++++-------------
 gjs/jsapi-util.cpp |   28 ++++++++++++++++++++++++++++
 gjs/jsapi-util.h   |   16 ++++++++++++++++
 3 files changed, 67 insertions(+), 13 deletions(-)
---
diff --git a/gi/gerror.cpp b/gi/gerror.cpp
index f3c0f97..509c3e6 100644
--- a/gi/gerror.cpp
+++ b/gi/gerror.cpp
@@ -392,25 +392,35 @@ find_error_domain_info(GQuark domain)
    fileName, lineNumber and stack
 */
 static void
-define_error_properties(JSContext       *context,
+define_error_properties(JSContext       *cx,
                         JS::HandleObject obj)
 {
-    JS::RootedValue stack(context), fileName(context), lineNumber(context);
-
-    if (!gjs_context_get_frame_info(context,
-                                    mozilla::Some<JS::MutableHandleValue>(&stack),
-                                    mozilla::Some<JS::MutableHandleValue>(&fileName),
-                                    mozilla::Some<JS::MutableHandleValue>(&lineNumber)))
+    JS::RootedObject frame(cx);
+    JS::RootedString stack(cx);
+    JS::RootedString source(cx);
+    uint32_t line;
+    JS::AutoSaveExceptionState exc(cx);
+
+    if (!JS::CaptureCurrentStack(cx, &frame) ||
+        !JS::BuildStackString(cx, frame, &stack)) {
+        exc.restore();
         return;
+    }
 
-    gjs_object_define_property(context, obj, GJS_STRING_STACK, stack,
-                               JSPROP_ENUMERATE);
+    JS::SavedFrameResult result;
+    result = JS::GetSavedFrameSource(cx, frame, &source);
+    g_assert(result == JS::SavedFrameResult::Ok);
 
-    gjs_object_define_property(context, obj, GJS_STRING_FILENAME,
-                               fileName, JSPROP_ENUMERATE);
+    result = JS::GetSavedFrameLine(cx, frame, &line);
+    g_assert(result == JS::SavedFrameResult::Ok);
 
-    gjs_object_define_property(context, obj, GJS_STRING_LINE_NUMBER,
-                               lineNumber, JSPROP_ENUMERATE);
+    if (!gjs_object_define_property(cx, obj, GJS_STRING_STACK, stack,
+                                    JSPROP_ENUMERATE) ||
+        !gjs_object_define_property(cx, obj, GJS_STRING_FILENAME, source,
+                                    JSPROP_ENUMERATE) ||
+        !gjs_object_define_property(cx, obj, GJS_STRING_LINE_NUMBER, line,
+                                    JSPROP_ENUMERATE))
+        exc.restore();
 }
 
 JSObject*
diff --git a/gjs/jsapi-util.cpp b/gjs/jsapi-util.cpp
index 57e4cb4..bbec8d5 100644
--- a/gjs/jsapi-util.cpp
+++ b/gjs/jsapi-util.cpp
@@ -106,6 +106,34 @@ gjs_object_define_property(JSContext       *cx,
                                  value, flags, getter, setter);
 }
 
+bool
+gjs_object_define_property(JSContext       *cx,
+                           JS::HandleObject obj,
+                           GjsConstString   property_name,
+                           JS::HandleString value,
+                           unsigned         flags,
+                           JSNative         getter,
+                           JSNative         setter)
+{
+    return JS_DefinePropertyById(cx, obj,
+                                 gjs_context_get_const_string(cx, property_name),
+                                 value, flags, getter, setter);
+}
+
+bool
+gjs_object_define_property(JSContext       *cx,
+                           JS::HandleObject obj,
+                           GjsConstString   property_name,
+                           uint32_t         value,
+                           unsigned         flags,
+                           JSNative         getter,
+                           JSNative         setter)
+{
+    return JS_DefinePropertyById(cx, obj,
+                                 gjs_context_get_const_string(cx, property_name),
+                                 value, flags, getter, setter);
+}
+
 static void
 throw_property_lookup_error(JSContext       *cx,
                             JS::HandleObject obj,
diff --git a/gjs/jsapi-util.h b/gjs/jsapi-util.h
index fe5b524..2bc2468 100644
--- a/gjs/jsapi-util.h
+++ b/gjs/jsapi-util.h
@@ -340,6 +340,22 @@ bool gjs_object_define_property(JSContext       *cx,
                                 JSNative         getter = nullptr,
                                 JSNative         setter = nullptr);
 
+bool gjs_object_define_property(JSContext       *cx,
+                                JS::HandleObject obj,
+                                GjsConstString   property_name,
+                                JS::HandleString value,
+                                unsigned         flags,
+                                JSNative         getter = nullptr,
+                                JSNative         setter = nullptr);
+
+bool gjs_object_define_property(JSContext       *cx,
+                                JS::HandleObject obj,
+                                GjsConstString   property_name,
+                                uint32_t         value,
+                                unsigned         flags,
+                                JSNative         getter = nullptr,
+                                JSNative         setter = nullptr);
+
 JS::HandleId gjs_context_get_const_string(JSContext     *cx,
                                           GjsConstString string);
 


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