[gjs: 9/12] jsapi-util-string: Improve gjs_debug_object() to print more info




commit dccaa64da6b22d119e1eb436849ab8f82752d21c
Author: Philip Chimento <philip chimento gmail com>
Date:   Thu Feb 18 22:23:42 2021 -0800

    jsapi-util-string: Improve gjs_debug_object() to print more info
    
    Previously we'd print the name of the function if dumping a function
    object that was in a JS::Value but not if dumping the object directly.
    Move that code into gjs_debug_object(), and also add more code to dump
    information about a Promise object.
    
    Like Symbols, Promise objects don't require a JSContext to get information
    about them and can't trigger a GC, so they are safe to debug-dump here.

 gjs/jsapi-util-string.cpp | 41 +++++++++++++++++++++++++++++++----------
 1 file changed, 31 insertions(+), 10 deletions(-)
---
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 28182cec..41fb7ef3 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -21,6 +21,7 @@
 #include <js/GCAPI.h>  // for AutoCheckCannotGC
 #include <js/GCPolicyAPI.h>
 #include <js/Id.h>     // for JSID_IS_STRING...
+#include <js/Promise.h>
 #include <js/RootingAPI.h>
 #include <js/Symbol.h>
 #include <js/TypeDecls.h>
@@ -453,6 +454,36 @@ gjs_debug_object(JSObject * const obj)
         return "<null object>";
 
     std::ostringstream out;
+
+    if (js::IsFunctionObject(obj)) {
+        JSFunction* fun = JS_GetObjectFunction(obj);
+        JSString* display_name = JS_GetFunctionDisplayId(fun);
+        if (display_name && JS_GetStringLength(display_name))
+            out << "<function " << gjs_debug_string(display_name);
+        else
+            out << "<anonymous function";
+        out << " at " << fun << '>';
+        return out.str();
+    }
+
+    // This is OK because the promise methods can't cause a garbage collection
+    JS::HandleObject handle = JS::HandleObject::fromMarkedLocation(&obj);
+    if (JS::IsPromiseObject(handle)) {
+        out << '<';
+        JS::PromiseState state = JS::GetPromiseState(handle);
+        if (state == JS::PromiseState::Pending)
+            out << "pending ";
+        out << "promise " << JS::GetPromiseID(handle) << " at " << obj;
+        if (state != JS::PromiseState::Pending) {
+            out << ' ';
+            out << (state == JS::PromiseState::Rejected ? "rejected"
+                                                        : "resolved");
+            out << " with " << gjs_debug_value(JS::GetPromiseResult(handle));
+        }
+        out << '>';
+        return out.str();
+    }
+
     const JSClass* clasp = JS_GetClass(obj);
     out << "<object " << clasp->name << " at " << obj <<  '>';
     return out.str();
@@ -482,16 +513,6 @@ gjs_debug_value(JS::Value v)
         out << gjs_debug_symbol(v.toSymbol());
         return out.str();
     }
-    if (v.isObject() && js::IsFunctionObject(&v.toObject())) {
-        JSFunction* fun = JS_GetObjectFunction(&v.toObject());
-        JSString *display_name = JS_GetFunctionDisplayId(fun);
-        if (display_name)
-            out << "<function " << gjs_debug_string(display_name);
-        else
-            out << "<unnamed function";
-        out << " at " << fun << '>';
-        return out.str();
-    }
     if (v.isObject()) {
         out << gjs_debug_object(&v.toObject());
         return out.str();


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