Is it necessary to check if v8::Local is empty before using it?

44 views
Skip to first unread message

Gautham B A

unread,
May 15, 2018, 6:10:30 AM5/15/18
to v8-users
Hi all,

Please consider the following code -
void ParseJSON(v8::Isolate *isolate, const v8::Local<v8::String> &v8_str) {
  v8::HandleScope handle_scope(isolate);

  auto v8val_json = v8::JSON::Parse(isolate, v8_str);

  v8::Local<v8::Object> v8obj_json;
  if (!v8val_json.ToLocal(&v8obj_json)) {
    // Empty
    return;
  }

  // v8obj_json is a v8::Local. Is this check necessary?
  if (!v8obj_json.IsEmpty()) {
    return;
  }

  auto field_zero = v8obj_json->Get(0);
}

When I obtain a v8::Local object from a v8::MaybeLocal, is it necessary to check if the v8::Local object is empty before accessing it?

Thanks,
--Gautham

Ben Noordhuis

unread,
May 15, 2018, 6:16:43 AM5/15/18
to v8-users
No, the .ToLocal() call took care of that.

Gautham B A

unread,
May 15, 2018, 6:26:03 AM5/15/18
to v8-users
Thanks. So, once I have a non-empty v8::Local object (v8obj_json in this case), am I guaranteed that its value won't turn empty? - I'm asking this in the context of calling v8::V8::TerminateExecution(isolate). Please consider the following code -

v8::Local<v8::Object> v8obj_json;
if (!v8val_json.ToLocal(&v8obj_json)) { // v8val_json is a v8::MaybeLocal 
  return;
}

// Let's say we terminate the execution after getting a v8::Local object
v8::V8::TerminateExecution(isolate);

// Will v8obj_json become empty?

Ben Noordhuis

unread,
May 15, 2018, 10:39:10 AM5/15/18
to v8-users
On Tue, May 15, 2018 at 12:26 PM, Gautham B A
<gautham....@gmail.com> wrote:
> Thanks. So, once I have a non-empty v8::Local object (v8obj_json in this
> case), am I guaranteed that its value won't turn empty? - I'm asking this in
> the context of calling v8::V8::TerminateExecution(isolate). Please consider
> the following code -
>
> v8::Local<v8::Object> v8obj_json;
> if (!v8val_json.ToLocal(&v8obj_json)) { // v8val_json is a v8::MaybeLocal
> return;
> }
>
> // Let's say we terminate the execution after getting a v8::Local object
> v8::V8::TerminateExecution(isolate);
>
> // Will v8obj_json become empty?
> auto field_zero = v8obj_json->Get(0);

That's a different question, with a different answer. The handle
won't go empty when a termination exception is in effect but you
should nevertheless stop calling into V8 because that won't work
(reliably) when it's shutting down.
Reply all
Reply to author
Forward
0 new messages