Skip to content
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

src,lib: re-implement DOMException as a cloneable native object #41044

Closed
wants to merge 3 commits into from

Conversation

jasnell
Copy link
Member

@jasnell jasnell commented Dec 1, 2021

The JavaScript-implemented DOMException is not structured cloneable without ugly hacks.

This re-implements it in C++ as a cloneable BaseObject.

Also makes AbortError structured cloneable.

Refs: #41038

@nodejs-github-bot nodejs-github-bot added lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Dec 1, 2021
@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot

This comment has been minimized.

@nodejs-github-bot
Copy link
Collaborator

@@ -270,6 +276,97 @@ void DecorateErrorStack(Environment* env,
const errors::TryCatchScope& try_catch);
} // namespace errors

class DOMException : public BaseObject {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why not inherit from JSTransferable?

node/src/node_messaging.h

Lines 318 to 321 in 3f51f05

// Provide a base class from which JS classes that should be transferable or
// cloneable by postMesssage() can inherit.
// See e.g. FileHandle in internal/fs/promises.js for an example.
class JSTransferable : public BaseObject {

Copy link
Member Author

Choose a reason for hiding this comment

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

Because we can't. See the referenced issue for detail: #41038

}
Local<Object> err = Exception::Error(message_str).As<Object>();

if (!err->Set(env->context(), env->name_string(), name_str).FromJust()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (!err->Set(env->context(), env->name_string(), name_str).FromJust()) {
if (err->Set(env->context(), env->name_string(), name_str).IsNothing()) {

Why crash the process in case of an ongoing exception?

env,
obj,
message,
String::NewFromUtf8(env->isolate(), name.c_str()).ToLocalChecked());
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
String::NewFromUtf8(env->isolate(), name.c_str()).ToLocalChecked());
String::NewFromUtf8(env->isolate(), name.c_str())
.FromMaybe(Local<String>()));

Why crash the process in case of an ongoing exception?

Comment on lines +1311 to +1312
args.GetReturnValue().Set(ret);
#undef V
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
args.GetReturnValue().Set(ret);
#undef V
#undef V
args.GetReturnValue().Set(ret);

Setting the return value does not need to be done in the scope of V, does it?

const std::string& name)
: BaseObject(env, object) {
Local<Value> message_value =
String::NewFromUtf8(env->isolate(), message.c_str()).ToLocalChecked();
Copy link
Contributor

Choose a reason for hiding this comment

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

We can get rid of the calls to the *Check*() functions in the constructors and still signal to the caller if a failure occurs by using a factory function to construct these objects and return a nullish value when there is an exception. Such a thing is already implemented by the MessagePort class constructors:

class MessagePort : public HandleWrap {

Copy link
Member

@addaleax addaleax left a comment

Choose a reason for hiding this comment

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

This seems like a huge amount of unnecessary complexity, compared to making JSTransferable support transferables in the clone information … are you sure you want to do this?

return Global<Value>();
}

return Global<Value>(env->isolate(), stack);
Copy link
Member

Choose a reason for hiding this comment

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

Why does this return a Global rather than a Local?

private:
v8::Global<v8::Value> message;
v8::Global<v8::Value> name;
v8::Global<v8::Value> stack;
Copy link
Member

Choose a reason for hiding this comment

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

Are these always strings?

@jasnell
Copy link
Member Author

jasnell commented Dec 3, 2021

@addaleax:

This seems like a huge amount of unnecessary complexity, compared to making JSTransferable support transferables in the clone information … are you sure you want to do this?

That does not actually solve the problem here. The issue is that custom error objects like DOMException and AbortError are not cloneable without losing key bits of information. Making JSTransferable support transferables in clone is something we definitely want to do but that wouldn't solve the issue with custom error objects.

// This is annoying, but v8 does not give a nice way of getting a good
// stack property so we have to create an error object and capture a
// stack that way.
Global<Value> MakeErrorStack(
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't seem nice that we have to re-implement all these error properties in C++. If the only problem with implementing them in JS (which seems nicer in comparison since we can do multiple inheritance there) is getting hold of C++ stuff before internalBinding and require become available, I think we can simply put JSTransferable and the symbols to some object (primordials would do if we are being lazy and not want to invent a new argument) in InitializePrimordials and pass them into the per-context script for DOMException to use?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants