Skip to content

Commit cd6749b

Browse files
committed
js: Add gjs_debug_callable() debug function
This DRYs up a bunch of repeated code to debug the display ID of a function.
1 parent 155299d commit cd6749b

File tree

5 files changed

+18
-26
lines changed

5 files changed

+18
-26
lines changed

gi/function.cpp

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <js/ValueArray.h>
3636
#include <js/Warnings.h>
3737
#include <jsapi.h> // for HandleValueArray
38-
#include <jsfriendapi.h> // for JS_GetObjectFunction
3938
#include <jspubtd.h> // for JSProtoKey
4039

4140
#include "gi/arg-cache.h"
@@ -390,12 +389,9 @@ void GjsCallbackTrampoline::callback_closure(GIArgument** args, void* result) {
390389
gjs->exit_immediately(code);
391390

392391
// Some other uncatchable exception, e.g. out of memory
393-
JSFunction* fn = JS_GetObjectFunction(callable());
394-
std::string descr =
395-
fn ? "function " + gjs_debug_string(JS_GetFunctionDisplayId(fn))
396-
: "callable object " + gjs_debug_object(callable());
397392
g_error("Call to %s (%s.%s) terminated with uncatchable exception",
398-
descr.c_str(), m_info.ns(), m_info.name());
393+
gjs_debug_callable(callable()).c_str(), m_info.ns(),
394+
m_info.name());
399395
}
400396

401397
// If the callback has a GError** argument, then make a GError from the
@@ -553,14 +549,11 @@ bool GjsCallbackTrampoline::callback_closure_inner(
553549
return false;
554550

555551
if (!is_array) {
556-
JSFunction* fn = JS_GetObjectFunction(callable());
557-
std::string descr =
558-
fn ? "function " + gjs_debug_string(JS_GetFunctionDisplayId(fn))
559-
: "callable object " + gjs_debug_object(callable());
560552
gjs_throw(context,
561553
"Call to %s (%s.%s) returned unexpected value, expecting "
562554
"an Array",
563-
descr.c_str(), m_info.ns(), m_info.name());
555+
gjs_debug_callable(callable()).c_str(), m_info.ns(),
556+
m_info.name());
564557
return false;
565558
}
566559

gi/value.cpp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
#include <js/ValueArray.h>
2929
#include <js/experimental/TypedData.h>
3030
#include <jsapi.h> // for InformalValueTypeName, JS_Get...
31-
#include <jsfriendapi.h> // for JS_GetObjectFunction
3231

3332
#include "gi/arg-inl.h"
3433
#include "gi/arg.h"
@@ -381,12 +380,8 @@ void Gjs::Closure::marshal(GValue* return_value, unsigned n_param_values,
381380
gjs->exit_immediately(code);
382381

383382
// Some other uncatchable exception, e.g. out of memory
384-
JSFunction* fn = JS_GetObjectFunction(callable());
385-
std::string descr =
386-
fn ? "function " + gjs_debug_string(JS_GetFunctionDisplayId(fn))
387-
: "callable object " + gjs_debug_object(callable());
388383
g_error("Call to %s terminated with uncatchable exception",
389-
descr.c_str());
384+
gjs_debug_callable(callable()).c_str());
390385
}
391386
}
392387

gjs/context.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -542,10 +542,8 @@ static bool on_context_module_rejected_log_exception(JSContext* cx,
542542
JS::Value* vp) {
543543
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
544544

545-
JSString* id =
546-
JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee()));
547545
gjs_debug(GJS_DEBUG_IMPORTER, "Module evaluation promise rejected: %s",
548-
gjs_debug_string(id).c_str());
546+
gjs_debug_callable(&args.callee()).c_str());
549547

550548
JS::HandleValue error = args.get(0);
551549

@@ -564,10 +562,8 @@ static bool on_context_module_resolved(JSContext* cx, unsigned argc,
564562
JS::Value* vp) {
565563
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
566564

567-
JSString* id =
568-
JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee()));
569565
gjs_debug(GJS_DEBUG_IMPORTER, "Module evaluation promise resolved: %s",
570-
gjs_debug_string(id).c_str());
566+
gjs_debug_callable(&args.callee()).c_str());
571567

572568
args.rval().setUndefined();
573569

@@ -628,11 +624,9 @@ static void load_context_module(JSContext* cx, const char* uri,
628624
[](JSContext* cx, unsigned argc, JS::Value* vp) {
629625
JS::CallArgs args = JS::CallArgsFromVp(argc, vp);
630626

631-
JSString* id =
632-
JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee()));
633627
gjs_debug(GJS_DEBUG_IMPORTER,
634628
"Module evaluation promise rejected: %s",
635-
gjs_debug_string(id).c_str());
629+
gjs_debug_callable(&args.callee()).c_str());
636630

637631
JS::HandleValue error = args.get(0);
638632
// Abort because this module is required.

gjs/jsapi-util-string.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,15 @@ gjs_debug_object(JSObject * const obj)
622622
return out.str();
623623
}
624624

625+
std::string gjs_debug_callable(JSObject* callable) {
626+
if (JSFunction* fn = JS_GetObjectFunction(callable)) {
627+
if (JSString* display_id = JS_GetFunctionDisplayId(fn))
628+
return {"function " + gjs_debug_string(display_id)};
629+
return {"unnamed function"};
630+
}
631+
return {"callable object " + gjs_debug_object(callable)};
632+
}
633+
625634
std::string
626635
gjs_debug_value(JS::Value v)
627636
{

gjs/jsapi-util.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ bool gjs_object_require_converted_property(JSContext *context,
600600
[[nodiscard]] std::string gjs_debug_string(JSString* str);
601601
[[nodiscard]] std::string gjs_debug_symbol(JS::Symbol* const sym);
602602
[[nodiscard]] std::string gjs_debug_object(JSObject* obj);
603+
[[nodiscard]] std::string gjs_debug_callable(JSObject* callable);
603604
[[nodiscard]] std::string gjs_debug_value(JS::Value v);
604605
[[nodiscard]] std::string gjs_debug_id(jsid id);
605606

0 commit comments

Comments
 (0)