Skip to content

Commit fba9c25

Browse files
committed
fix(JobQueue): allocate callbacks on the heap and delete global before destroying the context
1 parent c54d5e8 commit fba9c25

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

include/JobQueue.hh

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class JobQueue : public JS::JobQueue {
2727
// JS::JobQueue methods.
2828
//
2929
public:
30-
explicit JobQueue(JSContext *cx) : finalizationRegistryCallbacks(cx) {}
31-
~JobQueue() = default;
30+
explicit JobQueue(JSContext *cx);
31+
~JobQueue();
3232

3333
/**
3434
* @brief Ask the embedding for the incumbent global.
@@ -92,7 +92,7 @@ bool runFinalizationRegistryCallbacks(JSContext *cx);
9292

9393
private:
9494
using FunctionVector = JS::GCVector<JSFunction *, 0, js::SystemAllocPolicy>;
95-
JS::PersistentRooted<FunctionVector> finalizationRegistryCallbacks;
95+
JS::PersistentRooted<FunctionVector> *finalizationRegistryCallbacks;
9696

9797
/**
9898
* @brief Capture this JobQueue's current job queue as a SavedJobQueue and return it,

src/JobQueue.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,14 @@
1111

1212
#include <stdexcept>
1313

14+
JobQueue::JobQueue(JSContext *cx) {
15+
finalizationRegistryCallbacks = new JS::PersistentRooted<FunctionVector>(cx);
16+
}
17+
18+
JobQueue::~JobQueue() {
19+
delete finalizationRegistryCallbacks;
20+
}
21+
1422
JSObject *JobQueue::getIncumbentGlobal(JSContext *cx) {
1523
return JS::CurrentGlobalOrNull(cx);
1624
}
@@ -108,13 +116,13 @@ bool JobQueue::dispatchToEventLoop(void *closure, JS::Dispatchable *dispatchable
108116
}
109117

110118
void JobQueue::queueFinalizationRegistryCallback(JSFunction *callback) {
111-
mozilla::Unused << finalizationRegistryCallbacks.append(callback);
119+
mozilla::Unused << finalizationRegistryCallbacks->append(callback);
112120
}
113121

114122
bool JobQueue::runFinalizationRegistryCallbacks(JSContext *cx) {
115123
bool ranCallbacks = false;
116124
JS::Rooted<FunctionVector> callbacks(cx);
117-
std::swap(callbacks.get(), finalizationRegistryCallbacks.get());
125+
std::swap(callbacks.get(), finalizationRegistryCallbacks->get());
118126
for (JSFunction *f: callbacks) {
119127
JS::ExposeObjectToActiveJS(JS_GetFunctionObject(f));
120128

src/modules/pythonmonkey/pythonmonkey.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,8 @@ PyTypeObject JSObjectItemsProxyType = {
263263

264264
static void cleanup() {
265265
delete autoRealm;
266-
if (GLOBAL_CX) JS_DestroyContext(GLOBAL_CX);
267266
delete global;
267+
if (GLOBAL_CX) JS_DestroyContext(GLOBAL_CX);
268268
delete JOB_QUEUE;
269269
JS_ShutDown();
270270
}

0 commit comments

Comments
 (0)