Re: [v8-users] How do I use the Accessor?

2016-09-22 Thread kito


Thank you for your answer. 


My plan is to run the javascript in c ++.

I try to output and property of the javascript object, the return value of the 
javascript function.

I was difficult to find an example of google's v8. I have to study alone, it 
was supposed to visit this site. 

I have output to the cmd console read the javascript file. I FuntionTemplate of 
google's v8 is can be used. 

It has been Callback call every time the javascript function is performed us to 
output a value to the console.

However, Property approach using the Accessor is not callback. Also, I do not 
know where to process the callback.

I have heard that the processing in the Getter of SetAccessor.  However, in the 
href, the value is not output.

I have also that the method is wrong set.

I have a correction to the now rather than "Accessing Dynamic variable," 
"Accessing Static Global Variable".

For example, I want to output the value of the location.href.

However, the output only to the value of the Run result of the compiled script.

I want to output the value at the time of callback as FuntionTemplate.


I am not so good at English.

So please be good to understand my awkward English. thank you.


On Thu, Sep 22, 2016 at 3:28 PM, Ben Noordhuis wrote:

> On Thu, Sep 22, 2016 at 2:19 AM, kito  
> wrote: 
> > I will need an example of how to use the Accessor. 
> > 
> > The code was posted above, it does not run successfully. 
>
> Take a look at test/cctest/test-api.cc and 
> test/cctest/test-accessors.cc.  If you can't distill a working example 
> from that, can you be more specific about what doesn't work and what 
> you have tried so far? 
>

-- 
-- 
v8-users mailing list
v8-users@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [v8-users] How do I use the Accessor?

2016-09-22 Thread Ben Noordhuis
On Thu, Sep 22, 2016 at 2:19 AM, kito  wrote:
> I will need an example of how to use the Accessor.
>
> The code was posted above, it does not run successfully.

Take a look at test/cctest/test-api.cc and
test/cctest/test-accessors.cc.  If you can't distill a working example
from that, can you be more specific about what doesn't work and what
you have tried so far?

-- 
-- 
v8-users mailing list
v8-users@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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: [v8-users] How do I use the Accessor?

2016-09-21 Thread Ben Noordhuis
On Wed, Sep 21, 2016 at 12:37 PM, kito  wrote:
> Hi~
>
> I try to use the google's v8 in win32 application console Project of visual
> studio 2015. (For reference, I'm using Windows 10.)
>
> I will use the Accessor, trying to output the url of location.href.
>
>
> Sample code to output a value using the Accessor is required.
>
> The following is the code that I've tried.
>
> std::string href;
>
> class lhref {
> public:
> lhref() {}
> ~lhref() {}
>
> std::string url;
> std::string getUrl(void) { return url; }
> };
>
> // lhref wrapper class
> class lhrefWrapper {
> public:
> lhrefWrapper() {};
> ~lhrefWrapper();
>
> void Initialize(v8::Isolate*, v8::Local);
>
> private:
> static lhref* Unwraplhref(v8::Local);
> static void Getter(v8::Local, const
> v8::PropertyCallbackInfo&);
> static void Setter(v8::Local, v8::Local,
> const v8::PropertyCallbackInfo&);
> };
>
>
> lhrefWrapper::~lhrefWrapper() {
> }
>
>
> void lhrefWrapper::Initialize(v8::Isolate * isolate, v8::Local
> Context) {
> v8::HandleScope handle_scope(isolate);
>
> v8::Context::Scope context_scope(Context);
>
> v8::Local ft_print =
> v8::FunctionTemplate::New(isolate, Print);
> v8::Local f_print = ft_print->GetFunction();
>
> v8::Local ft_alert =
> v8::FunctionTemplate::New(isolate, Alert);
> v8::Local f_alert = ft_alert->GetFunction();
>
> v8::Local ft_prompt =
> v8::FunctionTemplate::New(isolate, Prompt);
> v8::Local f_prompt = ft_prompt->GetFunction();
>
> v8::Local ft_window =
> v8::FunctionTemplate::New(isolate, Window);
> v8::Local f_window = ft_window->GetFunction();
>
> Context->Global()->Set(v8::String::NewFromUtf8(isolate, "print",
> v8::NewStringType::kNormal).ToLocalChecked(), f_print);
> Context->Global()->Set(v8::String::NewFromUtf8(isolate, "alert",
> v8::NewStringType::kNormal).ToLocalChecked(), f_alert);
> Context->Global()->Set(v8::String::NewFromUtf8(isolate, "prompt",
> v8::NewStringType::kNormal).ToLocalChecked(), f_prompt);
> Context->Global()->Set(v8::String::NewFromUtf8(isolate, "window",
> v8::NewStringType::kNormal).ToLocalChecked(), f_window);
>
> v8::Local lhref_templ =
> v8::ObjectTemplate::New(isolate);
> lhref_templ->SetInternalFieldCount(1);
> //lhref_templ->SetAccessor(v8::String::NewFromUtf8(isolate, "href"),
> Getter);
>
> lhref* lohref = new lhref();
>
> v8::Local lhref_obj = lhref_templ->NewInstance();
>
> lhref_obj->SetInternalField(0, v8::External::New(isolate, lohref));
>
> Context->Global()->Set(v8::String::NewFromUtf8(isolate, "location",
> v8::NewStringType::kNormal).ToLocalChecked(), lhref_obj);
> }
>
> lhref* lhrefWrapper::Unwraplhref(v8::Local obj) {
> v8::Local field =
> v8::Local::Cast(obj->GetInternalField(0));
> void* ptr = field->Value();
> return static_cast(ptr);
> }
>
>
> void lhrefWrapper::Getter(v8::Local property, const
> v8::PropertyCallbackInfo& info)
> {
> v8::Local self = info.Holder();
> v8::Local wrap =
> v8::Local::Cast(self->GetInternalField(0));
> void* ptr = wrap->Value();
> href = static_cast(ptr)->url;
> info.GetReturnValue().Set(StringToV8String(href));
> }
>
> void lhrefWrapper::Setter(v8::Local property,
> v8::Local value, const v8::PropertyCallbackInfo& info)
> {
> v8::Local self = info.Holder();
> v8::Local wrap =
> v8::Local::Cast(self->GetInternalField(0));
> void* ptr = wrap->Value();
> static_cast(ptr)->url = V8StringToString(value);
> }
>
> .
> .
> .
>
> /[[ Main
> ]]/
>
> int main(int argc, char* argv[]) {
> v8::V8::InitializeICUDefaultLocation(argv[0]);
> v8::V8::InitializeExternalStartupData(argv[0]);
> v8::Platform* platform = v8::platform::CreateDefaultPlatform();
> v8::V8::InitializePlatform(platform);
> v8::V8::Initialize();
>
> v8::V8::SetFlagsFromCommandLine(, argv, true);
> v8::Isolate::CreateParams create_params;
> create_params.array_buffer_allocator =
> v8::ArrayBuffer::Allocator::NewDefaultAllocator();
> v8::Isolate* isolate = v8::Isolate::New(create_params);
>
> {
> v8::Isolate::Scope isolate_scope(isolate);
> v8::HandleScope handle_scope(isolate);
>
> v8::Local context = v8::Context::New(isolate);
>
> v8::Context::Scope context_scope(context);
>
> v8::Local source;
>
> for (int i = 1; i < argc; i++) {
> const char* str = argv[i];
>
> ReadFile(isolate, str).ToLocal();
> }
>
> lhrefWrapper lw;
>
>