Skip to content

Macro brainstorming for Maybe<>/MaybeLocal<> #1872

@domenic

Description

@domenic

Dealing with Maybe<>s and MaybeLocal<>s in V8 4.3 are a pain, as I found out in #1773 and @trevnorris found out in #1825. This could maybe be made better with macros.

Here is one proposal:

// Original code
MaybeLocal<Object> maybe_foo = get_foo_from_v8();
Local<Object> foo;
if (!maybe_foo.ToLocal(&foo)) {
  return;
}
// Desired code
UN_MAYBE(foo, Object, get_foo_from_v8());

This would introduce a local variable foo of type Local<Object> (and another one maybe_foo of type MaybeLocal<Object>) in the exact same way as the above code. Either the function will have returned, or foo will contain a non-empty Local<Object>. I am 95% sure this is easy to write a macro for.

Ideas/issues:

  • It would be nice if it could figure out the Object part for us. Can it, perhaps using auto or decltype or something?
  • It'd be nice to write this as Local<Object> foo = UN_MAYBE(get_foo_from_v8()). Does anyone have good enough macro skills for that?
  • I guess if we want to use this inside functions that don't return void, we'd need a return type too.
  • V8 does stuff that's a bit more complicated (focus on PREPARE_FOR_EXECUTION_GENERIC and RETURN_ON_FAILED_EXECUTION + EXCEPTION_BAILOUT_CHECK_SCOPED, as well as various instances that set has_pending_exception throughout the code). Is there a good reason for that? Should we do something more like that?

Metadata

Metadata

Assignees

No one assigned

    Labels

    c++Issues and PRs that require attention from people who are familiar with C++.v8 engineIssues and PRs related to the V8 dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions