How i get them : https://v8.dev/blog/fast-properties

67 views
Skip to first unread message

oreng...@gmail.com

unread,
Jul 1, 2020, 5:06:53 AM7/1/20
to v8-dev
Hi

Do you know how I get for K the keys a & b ?  

const K = {a: "foo", b: "bar"}.

I see here - https://v8.dev/blog/fast-properties 
that it's on Elements data structure but I can't restore them.

snippet code : 
const char * text= ("const K = {a: "foo", b: "bar"};);
Local<v8::String> source = v8::String::NewFromUtf8(m_Isolate, text);


Local<Script> compiled_script;
if (!Script::Compile(m_Context, source).ToLocal(&compiled_script))
...
        showprop("K")


void  showprop(const char * name)
{
v8::Local<v8::Name> val = v8::String::NewFromUtf8(m_Isolate, name);

Local<Object> obj = val->ToObject(m_Context).ToLocalChecked();
Local<Array>  arr = obj->GetPropertyNames(m_Context).ToLocalChecked();

int len = arr->Length();
for (uint32_t i = 0; i < len; ++i)
{
Local<Value> key = arr->Get(i);
Local<Value> val = obj->Get(m_Context, key).ToLocalChecked();

// i got val as string K  !!!   and not get a or b keys 
}
}


Any help appreciated 
Oren 

Jakob Kummerow

unread,
Jul 1, 2020, 5:55:37 AM7/1/20
to v8-...@googlegroups.com
void  showprop(const char * name)
{
v8::Local<v8::Name> val = v8::String::NewFromUtf8(m_Isolate, name);
Local<Object> obj = val->ToObject(m_Context).ToLocalChecked();

This is the C++ equivalent of the following JavaScript:
let val = "K";
let obj = Object(val);
So that's not the same as reading the global variable K, which is presumably what you're trying to do.

Instead of posting the same question on all mailing lists you can find, please do some research on your own. Please read the embedders documentation on v8.dev/docs, and study the examples in the samples/ directory. That should give you a general understanding of how to work with V8's API.

oreng...@gmail.com

unread,
Jul 1, 2020, 6:13:16 AM7/1/20
to v8-dev
Thanks 

I already read the documents + check a lot of api.
At the sample code, I read the K object from a specific context.
Why do you say its global? 

In addition, I didn't see any sample or documentation about getting the keys from the HiddenClass / Elements data structure 

Oren

Jakob Kummerow

unread,
Jul 1, 2020, 6:31:54 AM7/1/20
to v8-...@googlegroups.com
On Wed, Jul 1, 2020 at 12:13 PM <oreng...@gmail.com> wrote:
Why do you say its global? 

After evaluating the string "const K = {...}", K is a global variable in the provided context.

At the sample code, I read the K object from a specific context.

No, that's not what you're doing. You're creating an object wrapper around a string "K". That object creation needs a context, but that's not the same as reading a variable from that context.

oreng...@gmail.com

unread,
Jul 1, 2020, 12:36:48 PM7/1/20
to v8-dev
Thanks, I got you.
Do you know how I retrieve objects from context?  

oreng...@gmail.com

unread,
Jul 2, 2020, 7:47:00 AM7/2/20
to v8-dev
i"m trying to get the variable from context / global without success.

Any idea how to do it correctly?

Leszek Swirski

unread,
Jul 2, 2020, 7:57:35 AM7/2/20
to v8-dev
You can access your K object via the context's global object, and then iterate it's properties from there.

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/c5187f07-6220-471b-958e-ab12f74bb614o%40googlegroups.com.

oreng...@gmail.com

unread,
Jul 2, 2020, 10:01:33 AM7/2/20
to v8-dev
What i"m doing wrong ?

v8::Local<v8::Object> obj = m_Context->Global();

1) Local<Array>  arr = obj->GetPropertyNames(m_Context).ToLocalChecked();
// I got arr->Length = 0 
2) Local<Array> arr= obj->GetPropertyNames(m_Context, KeyCollectionMode::kIncludePrototypes, PropertyFilter::ALL_PROPERTIES, IndexFilter::kSkipIndices).ToLocalChecked();
// i got 71 items , but none of them are mine 

3) Local<Value> key =obj->Get(m_Context, v8::String::NewFromUtf8(m_Isolate, "AA")).ToLocalChecked();
// i got undefined

Thanks a lot for all your help
Oren 

On Thursday, July 2, 2020 at 2:57:35 PM UTC+3, Leszek Swirski wrote:
You can access your K object via the context's global object, and then iterate it's properties from there.

On Thu, Jul 2, 2020 at 1:47 PM <oreng...@gmail.com> wrote:
i"m trying to get the variable from context / global without success.

Any idea how to do it correctly?


On Wednesday, July 1, 2020 at 7:36:48 PM UTC+3, oreng...@gmail.com wrote:
Thanks, I got you.
Do you know how I retrieve objects from context?  

On Wednesday, July 1, 2020 at 1:31:54 PM UTC+3, Jakob Kummerow wrote:
On Wed, Jul 1, 2020 at 12:13 PM <oreng...@gmail.com> wrote:
Why do you say its global? 

After evaluating the string "const K = {...}", K is a global variable in the provided context.

At the sample code, I read the K object from a specific context.

No, that's not what you're doing. You're creating an object wrapper around a string "K". That object creation needs a context, but that's not the same as reading a variable from that context.

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-...@googlegroups.com.

Leszek Swirski

unread,
Jul 2, 2020, 10:22:39 AM7/2/20
to v8-dev
I think you're misunderstanding the API.

You have created a program which creates an object "K" in the global context, which you can access via the global object. So, you have to _first_ get the global object, _then_ get K from it, and only _then_ get the properties of K.

So it'll be something like (haven't tested):

// Get global object (effectively globalThis)
v8::Local<v8::Object> global_obj = context->Global();
// Get K from global object (effectively globalThis["K"])
v8::Local<v8::Object> K_obj = global_obj->Get(context, v8::String::NewFromUtf8(isolate, "K"))
// Get the properties from K (effectively Object.keys(globalThis["K"]))
v8::Local<Array> arr = K_obj->GetPropertyNames(context);

To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/01ac6bc4-a9fe-493c-8bdc-9cb28998379co%40googlegroups.com.
Message has been deleted
Message has been deleted

oreng...@gmail.com

unread,
Jul 3, 2020, 5:02:49 AM7/3/20
to v8-dev
I compile & run this string  "const K = {a: 1, b: 2}; "
But when I try to get properties of object K  ,  I got undefined at val_k , Any idea why ?

v8::Local<v8::Object> global_obj = m_Context->Global();
Local<Value> val_k = global_obj->Get(m_Context, v8::String::NewFromUtf8(m_Isolate, "K")).ToLocalChecked();

Attached sample that reproduce it 

Thanks 
Oren  
testv8.cpp

Marja Hölttä

unread,
Jul 3, 2020, 10:56:54 AM7/3/20
to v8-...@googlegroups.com
"const" doesn't define a property in the global object. Does it work if you use "var K" instead of "const K"?


--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
Message has been deleted

oreng...@gmail.com

unread,
Jul 4, 2020, 6:35:33 AM7/4/20
to v8-dev
Yes , with var k its works :-)

Any idea how i can get properties of const / let ?
Which object hold them ? 
i try to retrieve from context without succeess 
 
Thanks a lot 
Oren 

On Friday, July 3, 2020 at 5:56:54 PM UTC+3, Marja Hölttä wrote:
"const" doesn't define a property in the global object. Does it work if you use "var K" instead of "const K"?


On Fri, Jul 3, 2020, 12:02 <oreng...@gmail.com> wrote:
I compile & run this string  "const K = {a: 1, b: 2}; "
But when I try to get properties of object K  ,  I got undefined at val_k , Any idea why ?

v8::Local<v8::Object> global_obj = m_Context->Global();
Local<Value> val_k = global_obj->Get(m_Context, v8::String::NewFromUtf8(m_Isolate, "K")).ToLocalChecked();

Attached sample that reproduce it 

Thanks 
Oren  

--
--
v8-dev mailing list
v8-...@googlegroups.com
http://groups.google.com/group/v8-dev
---
You received this message because you are subscribed to the Google Groups "v8-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to v8-...@googlegroups.com.

Leszek Swirski

unread,
Jul 6, 2020, 4:05:09 AM7/6/20
to v8-dev
Ah, I didn't pay attention to the "const" there. Yes, those are in the "script context" rather than the global context. Easiest way to get it is probably just to evaluate the string "K" and read the result.

Something like:

Local<v8::String> K_source = v8::String::NewFromUtf8(isolate, "K");
Local<v8::Script> K_script = v8::Script::Compile(context, source).ToLocalChecked();
Local<v8::Value> K_val = K_script->Run(context).ToLocalChecked();
Local<v8::Object> K = Local<v8::Object>::Cast(K_val);

To unsubscribe from this group and stop receiving emails from it, send an email to v8-dev+un...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/v8-dev/abcefcd3-41a0-4690-9c8f-577d8a7bf525o%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages