Returning v8::MaybeLocal<T> from C++ functions

160 views
Skip to first unread message

Gautham B A

unread,
Nov 23, 2018, 1:08:57 AM11/23/18
to v8-users
Hi all,

I was wondering if it's alright to just return a v8::MaybeLocal<T> from a C++ function. We know that v8::Local<T> objects must be escaped using a v8::EscapableHandleScope if we are to return v8::Local<T> from a function. But I couldn't find the corresponding Escape() overload for v8::MaybeLocal<T> objects.

void Callback(const v8::FunctionCallbackInfo<v8::Value> &args) {
}

v8::MaybeLocal<v8::Object> GetValue(v8::Isolate *isolate, const v8::Local<v8::Context> &context) {
v8::HandleScope handle_scope(isolate);
auto func = v8::Function::New(isolate, Callback);
v8::MaybeLocal<v8::Object> instance = func->NewInstance(context); // Do I need to Escape() this "instance" ? If so, how?
return instance; // Is this alright?
}

Thanks,
--Gautham

Ben Noordhuis

unread,
Nov 23, 2018, 3:29:09 AM11/23/18
to v8-u...@googlegroups.com
There's an EscapableHandleScope::EscapeMaybe() method that you can use
in newer versions of V8 (starting with V8 6.9.something.)

Gautham B A

unread,
Nov 23, 2018, 3:33:54 AM11/23/18
to v8-users
Thank you. I'm using v8 5.9.211. It doesn't have the EscapableHandleScope::EscapeMaybe() function. Is there any other way to achieve this?

Ben Noordhuis

unread,
Nov 23, 2018, 8:22:24 AM11/23/18
to v8-u...@googlegroups.com
On Fri, Nov 23, 2018 at 9:33 AM Gautham B A <gautham....@gmail.com> wrote:
> Thank you. I'm using v8 5.9.211. It doesn't have the EscapableHandleScope::EscapeMaybe() function. Is there any other way to achieve this?

Sure, just check if the handle is non-empty:

Local<Value> value;
if (maybe_value.ToLocal(&value)) handle_scope.Escape(value);

Callers of your function should check the return value for .IsEmpty().
Reply all
Reply to author
Forward
0 new messages