Looking for streamlined way to access object properties

33 views
Skip to first unread message

kent williams

unread,
Dec 6, 2016, 5:04:08 PM12/6/16
to v8-users
There's a convention that we use for our C++ native functions called from C++, where required arguments are followed (optionally) by an object whose properties are additional flags. EG

var num = convertNumber(x);
var num2 = convertNumber(x, { base: 8 });

Is there a streamlined way to extract properties from this optional object?

I end up doing this:

v8::Local<v8::Object> argsObj;
argsObj.Clear();
if(args.Length() >=2)
  argsObj = args[1];

const char *cvval = 0;
if(!argsObj.IsEmpty()) {
  v8::Local<v8::String> cvt = v8::String::NewFromUtf8(isolate,
                                           "convert", v8::NewStringType::kNormal).ToLocalChecked();
  v8::MaybeLocal<v8::Value> convertval = argsObj->Get(context,cvt);

  if(!convertval.IsEmpty()) {
    v8::Local<v8::Value> cv = convertval.ToLocalChecked();
    v8::String::Utf8Value val2 = cv->ToString();
    cvval = *val2 ? *val2 : 0;
  }
}

This seems like a whole lot of work -- way more than Spidermonkey, for example.

Are there some convenience functions that make this less painful, or do I have to write my own?

Thanks!

Jochen Eisinger

unread,
Dec 7, 2016, 5:03:02 AM12/7/16
to v8-users
You could use v8::Object::GetOwnPropertyNames instead of trying each key individually

--
--
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.

kent williams

unread,
Dec 7, 2016, 11:44:20 AM12/7/16
to v8-users
I should maybe try v8::Object::Has -- it looks like it will tell you if an object has a property, but it isn't really documented anywhere what it does.

kent williams

unread,
Dec 7, 2016, 5:46:19 PM12/7/16
to v8-users
I answered my own question again, sort of.

Both v8::Object::Has() and v8::Object::HasOwnProperty() will return true if a property exists on an object.
Reply all
Reply to author
Forward
0 new messages