I try it and 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();

I attached a file that reproduce it 
Oren 



On Thursday, July 2, 2020 at 5:22:39 PM UTC+3, Leszek Swirski wrote:
>
> 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);
>
>
> On Thu, Jul 2, 2020 at 4:01 PM <[email protected] <javascript:>> wrote:
>
>> 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 
>>> <https://v8.github.io/api/head/classv8_1_1Context.html#af5cd9f97ef6a3307c1c21f80f4b743eb>,
>>>  
>>> and then iterate it's properties from there.
>>>
>>> On Thu, Jul 2, 2020 at 1:47 PM <[email protected]> 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, [email protected] 
>>>> 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 <[email protected]> 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
>>>> [email protected]
>>>> 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 [email protected].
>>>> To view this discussion on the web visit 
>>>> https://groups.google.com/d/msgid/v8-dev/c5187f07-6220-471b-958e-ab12f74bb614o%40googlegroups.com
>>>>  
>>>> <https://groups.google.com/d/msgid/v8-dev/c5187f07-6220-471b-958e-ab12f74bb614o%40googlegroups.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> -- 
>> -- 
>> v8-dev mailing list
>> [email protected] <javascript:>
>> 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 [email protected] <javascript:>.
>> To view this discussion on the web visit 
>> https://groups.google.com/d/msgid/v8-dev/01ac6bc4-a9fe-493c-8bdc-9cb28998379co%40googlegroups.com
>>  
>> <https://groups.google.com/d/msgid/v8-dev/01ac6bc4-a9fe-493c-8bdc-9cb28998379co%40googlegroups.com?utm_medium=email&utm_source=footer>
>> .
>>
>

-- 
-- 
v8-dev mailing list
[email protected]
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 [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/v8-dev/9ecbc651-3967-40cf-803c-826c5e2b17ceo%40googlegroups.com.
// testv8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "v8.h"
#include "../include/libplatform/libplatform.h"
//#include "../src/objects/elements.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

using namespace v8;
using v8::TryCatch;

const char * exe_path = "C:\\v8\\bin\\";  // read from there icudtl.dat , natives_blob.bin , snapshot_blob.bin

//#define V8_COMPRESS_POINTERS 

std::unique_ptr<v8::Platform> g_platform;
v8::Local<v8::Context> m_Context;
v8::Isolate         *  m_Isolate;


void InitializeV8(const char* exec_path)
{
	V8::InitializeICUDefaultLocation(exec_path);
	V8::InitializeExternalStartupData(exec_path);
	g_platform = v8::platform::NewDefaultPlatform();
	V8::InitializePlatform(g_platform.get());
	V8::Initialize();
}

void UnInitializeV8()
{
	v8::V8::Dispose();
	v8::V8::ShutdownPlatform();
}


void Log(const char * error) {
	OutputDebugStringA(error);
	printf("Logged: %s\n", error);
}


void PrintValueAsString(const Handle<Value> key)
{
	String::Utf8Value Str(m_Isolate, key);
	OutputDebugStringA(std::string(*Str).c_str());
}


void checkProperty(Local<Value> val)
{
	if (val->IsName()) {
		v8::String::Utf8Value str(m_Isolate, val);
		std::string cppStr(*str);
		OutputDebugStringA(cppStr.c_str());
	}
	else if (val->IsStringObject()) {
		v8::String::Utf8Value str(m_Isolate, val);
		std::string cppStr(*str);
		OutputDebugStringA(cppStr.c_str());
	}
	else if (val->IsNumberObject())
	{
		Maybe<double> db = val->NumberValue(m_Context);
	}
	else if (val->IsFunction())
	{
		Handle<v8::Function> func = v8::Handle<v8::Function>::Cast(val);
		if (func.IsEmpty())
			return;
	}
	else if (val->IsArray())
	{
		Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(val);
		int len = array->Length();
		for (int i = 0; i < len; ++i)
		{
			const Handle<Value> key = array->Get(i);
			checkProperty(key);
		}
	}
	else if (val->IsObject())
	{
		bool a = val->IsObject();
		bool b = val->IsArgumentsObject();


		Local<Object> obj = val->ToObject(m_Context).ToLocalChecked();
		if (obj.IsEmpty())
			return;


		String::Utf8Value objascii(m_Isolate, obj);

		Handle<Array> properties = obj->GetOwnPropertyNames(m_Context).ToLocalChecked();
		int len = properties->Length();
		if (len == 0)
			return;

		for (int i = 0; i < len; ++i)
		{
			const Handle<Value> key = properties->Get(i);
			checkProperty(key);
		}
	}
	else if (val->IsBoolean()) {
		;
	}
	else if (val->IsUndefined()) {
		;
	}
	else if (val->IsSymbol()) {
		;
	}
	else if (val->IsNull()) {
		;
	}
	else {
		;
	}


}



int main(int argc, char* argv[])
{
	InitializeV8(exe_path);

	Isolate::CreateParams create_params;
	create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();

	m_Isolate = v8::Isolate::New(create_params);  // need to define 

	Isolate::Scope isolate_scope(m_Isolate);

	HandleScope handle_scope(m_Isolate);

	TryCatch try_catch(m_Isolate);

	m_Context = v8::Context::New(m_Isolate);

	Context::Scope context_scope(m_Context);

	
	const char * src = ("const K = {a: 1, b: 2}; ");
	Local<v8::String> source = v8::String::NewFromUtf8(m_Isolate, src);

	Local<Script> compiled_script;
	if (!Script::Compile(m_Context, source).ToLocal(&compiled_script))
	{
		String::Utf8Value error(m_Isolate, try_catch.Exception());
		Log(*error);  // The script failed to compile; 
		return false;
	}

	Local<v8::Value> result = compiled_script->Run(m_Context).ToLocalChecked(); // Run the script to get the result.


	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();

	PrintValueAsString(val_k);
	checkProperty(val_k);



	UnInitializeV8();

	delete create_params.array_buffer_allocator;

	return 0;

}


Reply via email to