diff options
Diffstat (limited to 'gjs')
-rw-r--r-- | gjs/console.cpp | 2 | ||||
-rw-r--r-- | gjs/context-private.h | 5 | ||||
-rw-r--r-- | gjs/context.cpp | 52 | ||||
-rw-r--r-- | gjs/mainloop.cpp | 3 | ||||
-rw-r--r-- | gjs/mainloop.h | 8 | ||||
-rw-r--r-- | gjs/profiler.cpp | 2 | ||||
-rw-r--r-- | gjs/promise.cpp | 20 | ||||
-rw-r--r-- | gjs/stack.cpp | 2 |
8 files changed, 63 insertions, 31 deletions
diff --git a/gjs/console.cpp b/gjs/console.cpp index 3d53ade0..c2e15789 100644 --- a/gjs/console.cpp +++ b/gjs/console.cpp @@ -355,7 +355,7 @@ int main(int argc, char** argv) { if (coverage_prefixes) gjs_coverage_enable(); - GjsAutoUnref<GjsContext> js_context(static_cast<GjsContext*>(g_object_new( + GjsAutoUnref<GjsContext> js_context(GJS_CONTEXT(g_object_new( GJS_TYPE_CONTEXT, "search-path", include_path.get(), "program-name", program_name, "program-path", program_path.get(), "profiler-enabled", enable_profiler, "exec-as-module", exec_as_module, nullptr))); diff --git a/gjs/context-private.h b/gjs/context-private.h index d8b7b85d..147feae4 100644 --- a/gjs/context-private.h +++ b/gjs/context-private.h @@ -17,7 +17,6 @@ #include <utility> // for pair #include <vector> -#include <gio/gio.h> #include <glib-object.h> #include <glib.h> @@ -248,13 +247,11 @@ class GjsContextPrivate : public JS::JobQueue { JS::HandleObject allocation_site, JS::HandleObject incumbent_global) override; void runJobs(JSContext* cx) override; - void runJobs(JSContext* cx, GCancellable* cancellable); [[nodiscard]] bool empty() const override { return m_job_queue.empty(); } js::UniquePtr<JS::JobQueue::SavedJobQueue> saveJobQueue( JSContext* cx) override; - GJS_JSAPI_RETURN_CONVENTION bool run_jobs_fallible( - GCancellable* cancellable = nullptr); + GJS_JSAPI_RETURN_CONVENTION bool run_jobs_fallible(); void register_unhandled_promise_rejection(uint64_t id, GjsAutoChar&& stack); void unregister_unhandled_promise_rejection(uint64_t id); void warn_about_unhandled_promise_rejections(); diff --git a/gjs/context.cpp b/gjs/context.cpp index 94a45c8b..59ac8afd 100644 --- a/gjs/context.cpp +++ b/gjs/context.cpp @@ -531,6 +531,12 @@ static bool on_context_module_rejected_log_exception(JSContext* cx, unsigned argc, JS::Value* vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + + JSString* id = + JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee())); + gjs_debug(GJS_DEBUG_IMPORTER, "Module evaluation promise rejected: %s", + gjs_debug_string(id).c_str()); + JS::HandleValue error = args.get(0); GjsContextPrivate* gjs_cx = GjsContextPrivate::from_cx(cx); @@ -547,6 +553,12 @@ static bool on_context_module_rejected_log_exception(JSContext* cx, static bool on_context_module_resolved(JSContext* cx, unsigned argc, JS::Value* vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + + JSString* id = + JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee())); + gjs_debug(GJS_DEBUG_IMPORTER, "Module evaluation promise resolved: %s", + gjs_debug_string(id).c_str()); + args.rval().setUndefined(); GjsContextPrivate::from_cx(cx)->main_loop_release(); @@ -565,12 +577,12 @@ static bool add_promise_reactions(JSContext* cx, JS::HandleValue promise, JS::RootedFunction on_rejected( cx, - js::NewFunctionWithReserved(cx, reject, 1, 0, resolved_tag.c_str())); + js::NewFunctionWithReserved(cx, reject, 1, 0, rejected_tag.c_str())); if (!on_rejected) return false; JS::RootedFunction on_resolved( cx, - js::NewFunctionWithReserved(cx, resolve, 1, 0, rejected_tag.c_str())); + js::NewFunctionWithReserved(cx, resolve, 1, 0, resolved_tag.c_str())); if (!on_resolved) return false; @@ -606,6 +618,12 @@ static void load_context_module(JSContext* cx, const char* uri, [](JSContext* cx, unsigned argc, JS::Value* vp) { JS::CallArgs args = JS::CallArgsFromVp(argc, vp); + JSString* id = + JS_GetFunctionDisplayId(JS_GetObjectFunction(&args.callee())); + gjs_debug(GJS_DEBUG_IMPORTER, + "Module evaluation promise rejected: %s", + gjs_debug_string(id).c_str()); + JS::HandleValue error = args.get(0); // Abort because this module is required. gjs_log_exception_full(cx, error, nullptr, G_LOG_LEVEL_ERROR); @@ -613,7 +631,7 @@ static void load_context_module(JSContext* cx, const char* uri, GjsContextPrivate::from_cx(cx)->main_loop_release(); return false; }, - "context"); + debug_identifier); if (!ok) { gjs_log_exception(cx); @@ -988,15 +1006,10 @@ bool GjsContextPrivate::should_exit(uint8_t* exit_code_p) const { return m_should_exit; } -void GjsContextPrivate::start_draining_job_queue(void) { - gjs_debug(GJS_DEBUG_CONTEXT, "Starting promise job dispatcher"); - m_dispatcher.start(); -} +void GjsContextPrivate::start_draining_job_queue(void) { m_dispatcher.start(); } void GjsContextPrivate::stop_draining_job_queue(void) { m_draining_job_queue = false; - - gjs_debug(GJS_DEBUG_CONTEXT, "Stopping promise job dispatcher"); m_dispatcher.stop(); } @@ -1015,7 +1028,7 @@ bool GjsContextPrivate::enqueuePromiseJob(JSContext* cx [[maybe_unused]], g_assert(cx == m_cx); g_assert(from_cx(cx) == this); - gjs_debug(GJS_DEBUG_CONTEXT, + gjs_debug(GJS_DEBUG_MAINLOOP, "Enqueue job %s, promise=%s, allocation site=%s", gjs_debug_object(job).c_str(), gjs_debug_object(promise).c_str(), gjs_debug_object(allocation_site).c_str()); @@ -1032,12 +1045,10 @@ bool GjsContextPrivate::enqueuePromiseJob(JSContext* cx [[maybe_unused]], // Override of JobQueue::runJobs(). Called by js::RunJobs(), and when execution // of the job queue was interrupted by the debugger and is resuming. -void GjsContextPrivate::runJobs(JSContext* cx) { runJobs(cx, nullptr); } - -void GjsContextPrivate::runJobs(JSContext* cx, GCancellable* cancellable) { +void GjsContextPrivate::runJobs(JSContext* cx) { g_assert(cx == m_cx); g_assert(from_cx(cx) == this); - if (!run_jobs_fallible(cancellable)) + if (!run_jobs_fallible()) gjs_log_exception(cx); } @@ -1053,7 +1064,7 @@ void GjsContextPrivate::runJobs(JSContext* cx, GCancellable* cancellable) { * Returns: false if one of the jobs threw an uncatchable exception; * otherwise true. */ -bool GjsContextPrivate::run_jobs_fallible(GCancellable* cancellable) { +bool GjsContextPrivate::run_jobs_fallible() { bool retval = true; if (m_draining_job_queue || m_should_exit) @@ -1070,8 +1081,11 @@ bool GjsContextPrivate::run_jobs_fallible(GCancellable* cancellable) { * it's crucial to recheck the queue length during each iteration. */ for (size_t ix = 0; ix < m_job_queue.length(); ix++) { /* A previous job might have set this flag. e.g., System.exit(). */ - if (m_should_exit || g_cancellable_is_cancelled(cancellable)) + if (m_should_exit || !m_dispatcher.is_running()) { + gjs_debug(GJS_DEBUG_MAINLOOP, "Stopping jobs because of %s", + m_should_exit ? "exit" : "main loop cancel"); break; + } job = m_job_queue[ix]; @@ -1085,7 +1099,7 @@ bool GjsContextPrivate::run_jobs_fallible(GCancellable* cancellable) { m_job_queue[ix] = nullptr; { JSAutoRealm ar(m_cx, job); - gjs_debug(GJS_DEBUG_CONTEXT, "handling job %s", + gjs_debug(GJS_DEBUG_MAINLOOP, "handling job %zu, %s", ix, gjs_debug_object(job).c_str()); if (!JS::Call(m_cx, JS::UndefinedHandleValue, job, args, &rval)) { /* Uncatchable exception - return false so that @@ -1104,6 +1118,7 @@ bool GjsContextPrivate::run_jobs_fallible(GCancellable* cancellable) { gjs_log_exception_uncaught(m_cx); } } + gjs_debug(GJS_DEBUG_MAINLOOP, "Completed job %zu", ix); } m_draining_job_queue = false; @@ -1380,6 +1395,7 @@ bool GjsContextPrivate::set_main_loop_hook(JSObject* callable) { bool GjsContextPrivate::run_main_loop_hook() { JS::RootedObject hook(m_cx, m_main_loop_hook.get()); m_main_loop_hook = nullptr; + gjs_debug(GJS_DEBUG_MAINLOOP, "Running and clearing main loop hook"); JS::RootedValue ignored_rval(m_cx); return JS::Call(m_cx, JS::NullHandleValue, hook, JS::HandleValueArray::empty(), &ignored_rval); @@ -1490,7 +1506,7 @@ bool GjsContextPrivate::eval_module(const char* identifier, ok = add_promise_reactions( m_cx, evaluation_promise, on_context_module_resolved, - on_context_module_rejected_log_exception, "context"); + on_context_module_rejected_log_exception, identifier); } bool exiting = false; diff --git a/gjs/mainloop.cpp b/gjs/mainloop.cpp index b69d45a8..3ad3f5c7 100644 --- a/gjs/mainloop.cpp +++ b/gjs/mainloop.cpp @@ -20,6 +20,7 @@ bool MainLoop::spin(GjsContextPrivate* gjs) { if (gjs->should_exit(nullptr)) { // Return false to indicate the loop is exiting due to an exit call, // the queue is likely not empty + debug("Not spinning loop because System.exit called"); exit(); return false; } @@ -27,6 +28,7 @@ bool MainLoop::spin(GjsContextPrivate* gjs) { GjsAutoPointer<GMainContext, GMainContext, g_main_context_unref> main_context(g_main_context_ref_thread_default()); + debug("Spinning loop until released or hook cleared"); do { bool blocking = can_block(); @@ -36,6 +38,7 @@ bool MainLoop::spin(GjsContextPrivate* gjs) { // If System.exit() has not been called if (gjs->should_exit(nullptr)) { + debug("Stopped spinning loop because System.exit called"); exit(); return false; } diff --git a/gjs/mainloop.h b/gjs/mainloop.h index f374060c..a228d761 100644 --- a/gjs/mainloop.h +++ b/gjs/mainloop.h @@ -8,6 +8,8 @@ #include <glib.h> +#include "util/log.h" + class GjsContextPrivate; namespace Gjs { @@ -20,6 +22,10 @@ class MainLoop { grefcount m_hold_count; bool m_exiting; + void debug(const char* msg) { + gjs_debug(GJS_DEBUG_MAINLOOP, "Main loop instance %p: %s", this, msg); + } + [[nodiscard]] bool can_block() { // Don't block if exiting if (m_exiting) @@ -51,6 +57,7 @@ class MainLoop { if (m_exiting) return; + debug("hold"); g_ref_count_inc(&m_hold_count); } @@ -59,6 +66,7 @@ class MainLoop { if (m_exiting) return; + debug("release"); bool zero [[maybe_unused]] = g_ref_count_dec(&m_hold_count); g_assert(!zero && "main loop released too many times"); } diff --git a/gjs/profiler.cpp b/gjs/profiler.cpp index 39c19621..1a5560f7 100644 --- a/gjs/profiler.cpp +++ b/gjs/profiler.cpp @@ -653,7 +653,7 @@ gjs_profiler_stop(GjsProfiler *self) static gboolean gjs_profiler_sigusr2(void *data) { - auto context = static_cast<GjsContext *>(data); + GjsContext* context = GJS_CONTEXT(data); GjsProfiler *current_profiler = gjs_context_get_profiler(context); if (current_profiler) { diff --git a/gjs/promise.cpp b/gjs/promise.cpp index 39939aa9..f248b019 100644 --- a/gjs/promise.cpp +++ b/gjs/promise.cpp @@ -6,6 +6,8 @@ #include <stddef.h> // for size_t +#include <string> + #include <gio/gio.h> #include <glib-object.h> @@ -22,6 +24,7 @@ #include "gjs/jsapi-util.h" #include "gjs/macros.h" #include "gjs/promise.h" +#include "util/log.h" /** * promise.cpp - This file implements a custom GSource, PromiseJobQueueSource, @@ -79,12 +82,8 @@ class PromiseJobDispatcher::Source : public GSource { // next one to execute. (it will starve the other sources) g_source_set_ready_time(this, -1); - // A reference to the current cancellable is needed in case any - // jobs reset PromiseJobDispatcher and thus replace the cancellable. - GjsAutoUnref<GCancellable> cancellable(m_cancellable, - GjsAutoTakeOwnership{}); // Drain the job queue. - m_gjs->runJobs(m_gjs->context(), cancellable); + m_gjs->runJobs(m_gjs->context()); return G_SOURCE_CONTINUE; } @@ -135,6 +134,8 @@ class PromiseJobDispatcher::Source : public GSource { if (!g_cancellable_is_cancelled(m_cancellable)) return; + gjs_debug(GJS_DEBUG_MAINLOOP, "Uncancelling promise job dispatcher"); + if (is_running()) g_source_remove_child_source(this, m_cancellable_source); else @@ -179,10 +180,14 @@ void PromiseJobDispatcher::start() { if (is_running()) return; + gjs_debug(GJS_DEBUG_MAINLOOP, "Starting promise job dispatcher"); g_source_attach(m_source.get(), m_main_context); } -void PromiseJobDispatcher::stop() { m_source->cancel(); } +void PromiseJobDispatcher::stop() { + gjs_debug(GJS_DEBUG_MAINLOOP, "Stopping promise job dispatcher"); + m_source->cancel(); +} }; // namespace Gjs @@ -212,6 +217,9 @@ bool set_main_loop_hook(JSContext* cx, unsigned argc, JS::Value* vp) { return false; } + gjs_debug(GJS_DEBUG_MAINLOOP, "Set main loop hook to %s", + gjs_debug_object(callback).c_str()); + GjsContextPrivate* priv = GjsContextPrivate::from_cx(cx); if (!priv->set_main_loop_hook(callback)) { gjs_throw( diff --git a/gjs/stack.cpp b/gjs/stack.cpp index b3d0d9c3..3292eae4 100644 --- a/gjs/stack.cpp +++ b/gjs/stack.cpp @@ -31,7 +31,7 @@ gjs_dumpstack(void) GList *iter; for (iter = contexts; iter; iter = iter->next) { - GjsAutoUnref<GjsContext> context(static_cast<GjsContext*>(iter->data)); + GjsAutoUnref<GjsContext> context(GJS_CONTEXT(iter->data)); gjs_context_print_stack_stderr(context); } } |