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