>
> 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
[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/5610ff0d-acc1-4d95-9b91-272301386307o%40googlegroups.com.
// testv8.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include "v8.h"
#include "../include/libplatform/libplatform.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