Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions mozjs-sys/src/jsglue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,21 @@ class RustJobQueue : public JS::JobQueue {
}
};

struct RustEnvironmentPreparer : public js::ScriptEnvironmentPreparer {
JSContext* cx;
explicit RustEnvironmentPreparer(JSContext* cx) : cx(cx) {}
void invoke(JS::HandleObject global, Closure& closure) override {
MOZ_ASSERT(JS_IsGlobalObject(global));
MOZ_ASSERT(!JS_IsExceptionPending(cx));

JSAutoRealm ar(cx, global);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like you're modelling it off of the jsshell version, but I intend to use this to replicate https://searchfox.org/mozilla-central/rev/fcc0cfc7f5b1df957ae29f6b2ccfeb6df47b6be8/xpcom/base/CycleCollectedJSRuntime.cpp#2177 . Since AutoEntryScript is a Servo Rust type, can we make this class implement a hook for Rust code to invoke the closure instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I intend to use this to replicate https://searchfox.org/mozilla-central/rev/fcc0cfc7f5b1df957ae29f6b2ccfeb6df47b6be8/xpcom/base/CycleCollectedJSRuntime.cpp#2177

can you elaborate on this? i’m not sure i follow, did you have existing plans to set up a ScriptEnvironmentPreparer for Servo?

Since AutoEntryScript is a Servo Rust type, can we make this class implement a hook for Rust code to invoke the closure instead?

yeah, that could work. would the idea be that RustEnvironmentPreparer::invoke() just passes the closure to Servo, which would enter the AutoEntryScript and call the closure?

also, what do we do with uncaught exceptions? the jsshell uses a thing called AutoReportException, but i’m not sure what the Servo counterpart is for that.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you elaborate on this? i’m not sure i follow, did you have existing plans to set up a ScriptEnvironmentPreparer for Servo?

I didn't know it existed until this PR, but now I want to replicate the way Gecko uses it.

yeah, that could work. would the idea be that RustEnvironmentPreparer::invoke() just passes the closure to Servo, which would enter the AutoEntryScript and call the closure

Yes.

also, what do we do with uncaught exceptions? the jsshell uses a thing called AutoReportException, but i’m not sure what the Servo counterpart is for that.

We have code that calls report_pending_exception in various places right now. Gecko uses AutoJsApi, which is another thing I want to replicate.

// TODO: AutoReportException are(cx);
if (!closure(cx)) {
return;
}
}
};

struct JSExternalStringCallbacksTraps {
void (*latin1Finalize)(const void* privateData, JS::Latin1Char* chars);
void (*utf16Finalize)(const void* privateData, char16_t* chars);
Expand Down Expand Up @@ -1106,6 +1121,10 @@ JS::JobQueue* CreateJobQueue(const JobQueueTraps* aTraps, const void* aQueue,

void DeleteJobQueue(JS::JobQueue* queue) { delete queue; }

void InitScriptEnvironmentPreparer(JSContext* cx) {
js::SetScriptEnvironmentPreparer(cx, new RustEnvironmentPreparer(cx));
}

JSExternalStringCallbacks* CreateJSExternalStringCallbacks(
const JSExternalStringCallbacksTraps* aTraps, void* privateData) {
return new RustJSExternalStringCallbacks(*aTraps, privateData);
Expand Down
Loading