When will v8::Object::Set return false?

236 views
Skip to first unread message

Jane Chen

unread,
Feb 19, 2016, 12:33:31 PM2/19/16
to v8-users
I'm getting

warning: ignoring return value of function declared with warn_unused_result attribute [-Wunused-result]
      obj->Set(isolate->GetCurrentContext(),
      ^~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Presumably I should get the return result of Object::Set.  But what shall I do if it returns false?  Throw an exception and give up?  How bad is it when this happens?  I don't seem to find a code example handling this in the sample code, such as shell.cc or d8.cc.

Thanks in advance for the advice!

Dean McNamee

unread,
Feb 20, 2016, 9:06:29 AM2/20/16
to v8-u...@googlegroups.com
There is something additional to mention here, because the bool Set()
function has been deprecated in recent versions of V8, and replaced
with Maybe<bool>. The header says:

* If an API method returns a MaybeLocal<>, the API method can potentially fail
* either because an exception is thrown, or because an exception is pending,
* e.g. because a previous API call threw an exception that hasn't been caught
* yet, or because a TerminateExecution exception was thrown. In that case, an
* empty MaybeLocal is returned.

Specially how Object::Set is handled, you can just read the source in api.cc:

Maybe<bool> v8::Object::Set(v8::Local<v8::Context> context,
v8::Local<Value> key, v8::Local<Value> value) {
PREPARE_FOR_EXECUTION_PRIMITIVE(context, "v8::Object::Set()", bool);
auto self = Utils::OpenHandle(this);
auto key_obj = Utils::OpenHandle(*key);
auto value_obj = Utils::OpenHandle(*value);
has_pending_exception =
i::Runtime::SetObjectProperty(isolate, self, key_obj, value_obj,
i::SLOPPY).is_null();
RETURN_ON_FAILED_EXECUTION_PRIMITIVE(bool);
return Just(true);
}

bool v8::Object::Set(v8::Local<Value> key, v8::Local<Value> value) {
auto context = ContextFromHeapObject(Utils::OpenHandle(this));
return Set(context, key, value).FromMaybe(false);
}


So it seems from a quick read, the Maybe API will either return a
non-empty True (Just<true>), or an empty Maybe. The old deprecated
bool Set() will convert that to a true/false, the false in the
FromMaybe(false) is the value for when the Maybe is empty.

So to answer your original question, false means that an exception was
throw or is pending, and it's up to you what you want to do with that
exception.
> --
> --
> v8-users mailing list
> v8-u...@googlegroups.com
> http://groups.google.com/group/v8-users
> ---
> You received this message because you are subscribed to the Google Groups
> "v8-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to v8-users+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages