On Wed, Sep 21, 2016 at 12:37 PM, kito <kenwo...@gmail.com> 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<v8::Context>);
>
> private:
>         static lhref* Unwraplhref(v8::Local<v8::Object>);
>         static void Getter(v8::Local<v8::String>, const
> v8::PropertyCallbackInfo<v8::Value>&);
>         static void Setter(v8::Local<v8::String>, v8::Local<v8::Value>,
> const v8::PropertyCallbackInfo<v8::Value>&);
> };
>
>
> lhrefWrapper::~lhrefWrapper() {
> }
>
>
> void lhrefWrapper::Initialize(v8::Isolate * isolate, v8::Local<v8::Context>
> Context) {
>         v8::HandleScope handle_scope(isolate);
>
>         v8::Context::Scope context_scope(Context);
>
>         v8::Local<v8::FunctionTemplate> ft_print =
> v8::FunctionTemplate::New(isolate, Print);
>         v8::Local<v8::Function> f_print = ft_print->GetFunction();
>
>         v8::Local<v8::FunctionTemplate> ft_alert =
> v8::FunctionTemplate::New(isolate, Alert);
>         v8::Local<v8::Function> f_alert = ft_alert->GetFunction();
>
>         v8::Local<v8::FunctionTemplate> ft_prompt =
> v8::FunctionTemplate::New(isolate, Prompt);
>         v8::Local<v8::Function> f_prompt = ft_prompt->GetFunction();
>
>         v8::Local<v8::FunctionTemplate> ft_window =
> v8::FunctionTemplate::New(isolate, Window);
>         v8::Local<v8::Function> 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<v8::ObjectTemplate> 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<v8::Object> 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<v8::Object> obj) {
>         v8::Local<v8::External> field =
> v8::Local<v8::External>::Cast(obj->GetInternalField(0));
>         void* ptr = field->Value();
>         return static_cast<lhref*>(ptr);
> }
>
>
> void lhrefWrapper::Getter(v8::Local<v8::String> property, const
> v8::PropertyCallbackInfo<v8::Value>& info)
> {
>         v8::Local<v8::Object> self = info.Holder();
>         v8::Local<v8::External> wrap =
> v8::Local<v8::External>::Cast(self->GetInternalField(0));
>         void* ptr = wrap->Value();
>         href = static_cast<lhref*>(ptr)->url;
>         info.GetReturnValue().Set(StringToV8String(href));
> }
>
> void lhrefWrapper::Setter(v8::Local<v8::String> property,
> v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<v8::Value>& info)
> {
>         v8::Local<v8::Object> self = info.Holder();
>         v8::Local<v8::External> wrap =
> v8::Local<v8::External>::Cast(self->GetInternalField(0));
>         void* ptr = wrap->Value();
>         static_cast<lhref*>(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(&argc, 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<v8::Context> context = v8::Context::New(isolate);
>
>                 v8::Context::Scope context_scope(context);
>
>                 v8::Local<v8::String> source;
>
>                 for (int i = 1; i < argc; i++) {
>                         const char* str = argv[i];
>
>                         ReadFile(isolate, str).ToLocal(&source);
>                 }
>
>                 lhrefWrapper lw;
>
>                 lw.Initialize(isolate, context);
>
>                 // Compile the source code.
>                 v8::Local<v8::Script> script = v8::Script::Compile(context,
> source).ToLocalChecked();
>
>                 // Run the script to get the result.
>                 v8::Local<v8::Value> result =
> script->Run(context).ToLocalChecked();
>
>                 const char* cstru = href.c_str();
>                 printf("location.href(href) : %s\n", cstru);
>
>                 v8::String::Utf8Value utf8(result);
>                 printf("location.href(result) = %s\n", *utf8);
>         }
>
>         isolate->Dispose();
>         v8::V8::Dispose();
>         v8::V8::ShutdownPlatform();
>         delete platform;
>         delete create_params.array_buffer_allocator;
>
>         return 0;
> }
>
>
> Thanks for any help in advance!

What do you need help with?  There are no questions in your post, just code.

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

Reply via email to