| 413 | } |
| 414 | |
| 415 | void GjsContextPrivate::dispose() { |
| 416 | if (m_cx) { |
| 417 | stop_draining_job_queue(); |
| 418 | |
| 419 | gjs_debug(GJS_DEBUG_CONTEXT, |
| 420 | "Notifying reference holders of GjsContext dispose"); |
| 421 | |
| 422 | for (auto const& destroy_notify : m_destroy_notifications) |
| 423 | destroy_notify.first(m_cx, destroy_notify.second); |
| 424 | |
| 425 | gjs_debug(GJS_DEBUG_CONTEXT, |
| 426 | "Checking unhandled promise rejections"); |
| 427 | warn_about_unhandled_promise_rejections(); |
| 428 | |
| 429 | gjs_debug(GJS_DEBUG_CONTEXT, "Releasing cached JS wrappers"); |
| 430 | m_fundamental_table->clear(); |
| 431 | m_gtype_table->clear(); |
| 432 | |
| 433 | /* Do a full GC here before tearing down, since once we do that we may |
| 434 | * not have the JS::GetReservedSlot(, 0) to access the context |
| 435 | */ |
| 436 | gjs_debug(GJS_DEBUG_CONTEXT, "Final triggered GC"); |
| 437 | JS_GC(m_cx, Gjs::GCReason::GJS_CONTEXT_DISPOSE); |
| 438 | |
| 439 | gjs_debug(GJS_DEBUG_CONTEXT, "Destroying JS context"); |
| 440 | m_destroying.store(true); |
| 441 | |
| 442 | /* Now, release all native objects, to avoid recursion between the JS |
| 443 | * teardown and the C teardown. The JSObject proxies still exist, but |
| 444 | * point to null. |
| 445 | */ |
| 446 | gjs_debug(GJS_DEBUG_CONTEXT, "Releasing all native objects"); |
| 447 | ObjectInstance::prepare_shutdown(); |
| 448 | GjsCallbackTrampoline::prepare_shutdown(); |
| 449 | |
| 450 | gjs_debug(GJS_DEBUG_CONTEXT, "Disabling auto GC"); |
| 451 | if (m_auto_gc_id > 0) { |
| 452 | g_source_remove(m_auto_gc_id); |
| 453 | m_auto_gc_id = 0; |
| 454 | } |
| 455 | |
| 456 | gjs_debug(GJS_DEBUG_CONTEXT, "Ending trace on global object"); |
| 457 | JS_RemoveExtraGCRootsTracer(m_cx, &GjsContextPrivate::trace, this); |
| 458 | m_global = nullptr; |
| 459 | m_internal_global = nullptr; |
| 460 | m_main_loop_hook = nullptr; |
| 461 | |
| 462 | gjs_debug(GJS_DEBUG_CONTEXT, "Freeing allocated resources"); |
| 463 | delete m_fundamental_table; |
| 464 | delete m_gtype_table; |
| 465 | delete m_atoms; |
| 466 | |
| 467 | m_job_queue.clear(); |
| 468 | m_object_init_list.clear(); |
| 469 | |
| 470 | // Tear down JS |
| 471 | JS_DestroyContext(m_cx); |
| 472 | m_cx = nullptr; |
no test coverage detected