-
Notifications
You must be signed in to change notification settings - Fork 5.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor(core): unwrap sync ops in rust #15449
Conversation
Pull request denoland#14019 enabled initial support for realms, but it did not include support for async ops anywhere other than the main realm. The main issue was that the `js_recv_cb` callback, which resolves promises corresponding to async ops, was only set for the main realm, so async ops in other realms would never resolve. Furthermore, promise ID's are specific to each realm, which meant that async ops from other realms would result in a wrong promise from the main realm being resolved. This change creates a `ContextState` struct, similar to `JsRuntimeState` but stored in a slot of each `v8::Context`, which contains a `js_recv_cb` callback for each realm. Combined with a new list of known realms, which stores them as `v8::Weak<v8::Context>`, and a change in the `#[op]` macro to pass the current context to `queue_async_op`, this makes it possible to send the results of promises for different realms to their realm, and prevent the ID's from getting mixed up. Additionally, since promise ID's are no longer unique to the isolate, having a single set of unrefed ops doesn't work. This change therefore also moves `unrefed_ops` from `JsRuntimeState` to `ContextState`, and adds the lengths of the unrefed op sets for all known realms to get the total number of unrefed ops to compare in the event loop. Towards #13239. Co-authored-by: Luis Malheiro <[email protected]>
This is so the `ContextState` reference isn't tied to the lifetime of the isolate. The `JsRealm::state_from_scope` method used to take a callback and pass it a `&mut ContextState` because the mutable reference was tied to the lifetime of a variable internal to the function, but with this change it isn't anymore. So this method is also updated to return a `Rc<RefCell<ContextState>>`.
…ise-ring' into op-sync-unwrap-in-rust
This reverts commit 4327a6d.
…ise-ring' into op-sync-unwrap-in-rust
} | ||
return error; | ||
} | ||
|
||
function unwrapOpResult(res) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should now be able to remove the unwrapOpResult
function, yes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's still used for async ops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, my bad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
LGTM |
const error = errorMap[className]?.(message); | ||
// Strip buildCustomError() calls from stack trace | ||
if (typeof error == "object") { | ||
ErrorCaptureStackTrace(error, buildCustomError); | ||
if (code) { | ||
error.code = code; | ||
} | ||
} | ||
return error; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: minor, but it can be clearer, (when isn't the result an object?)
const error = errorMap[className]?.(message); | |
// Strip buildCustomError() calls from stack trace | |
if (typeof error == "object") { | |
ErrorCaptureStackTrace(error, buildCustomError); | |
if (code) { | |
error.code = code; | |
} | |
} | |
return error; | |
const errorConstructor = errorMap[className]; | |
if (errorConstructor !== undefined) { | |
const error = errorConstructor(message); | |
// Strip buildCustomError() calls from stack trace | |
ErrorCaptureStackTrace(error, buildCustomError); | |
if (code) { | |
error.code = code; | |
} | |
return error; | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Theoretically the error constructor function could return anything, eg. null, I guess.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Helps with #15337.
Supersedes #14811.
I messed up the branch at #15351 and had to open a new PR.