Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <Windows.h>
- #include "libplatform/libplatform.h"
- #include "v8.h"
- #include "V8Helper.h"
- #define V8_STR(s) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), s, v8::NewStringType::kNormal).ToLocalChecked()
- class Point
- {
- private:
- static v8::Local<v8::ObjectTemplate> v8_class_template;
- public:
- int x = 0;
- int y = 0;
- static void V8GetX(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
- {
- Point* point = V8Helper::UnwrapObject<Point>(info);
- int value = point->x;
- info.GetReturnValue().Set(value);
- }
- static void V8SetX(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
- {
- Point* point = V8Helper::UnwrapObject<Point>(info);
- point->y = value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromMaybe(0);
- }
- static void V8GetY(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
- {
- Point* point = V8Helper::UnwrapObject<Point>(info);
- int value = point->y;
- info.GetReturnValue().Set(value);
- }
- static void V8SetY(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
- {
- Point* point = V8Helper::UnwrapObject<Point>(info);
- point->x = value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromMaybe(0);
- }
- static v8::Local<v8::Object> WrapObject(Point* object)
- {
- v8::Isolate* isolate = v8::Isolate::GetCurrent();
- v8::Local<v8::Context> context = isolate->GetCurrentContext();
- if (v8_class_template.IsEmpty())
- {
- v8_class_template = v8::ObjectTemplate::New(isolate);
- v8_class_template->SetInternalFieldCount(1);
- v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "x"), Point::V8GetX, Point::V8SetX);
- v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "y"), Point::V8GetY, Point::V8SetY);
- }
- v8::Local<v8::Object> obj = v8_class_template->NewInstance(context).ToLocalChecked();
- obj->SetInternalField(0, v8::External::New(isolate, object));
- return obj;
- }
- };
- v8::Local<v8::ObjectTemplate> Point::v8_class_template;
- class Line
- {
- private:
- static v8::Local<v8::ObjectTemplate> v8_class_template;
- public:
- Point* startPoint;
- Point* endPoint;
- static void V8GetStartPoint(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
- {
- Line* line = V8Helper::UnwrapObject<Line>(info);
- Point* point = line->startPoint;
- info.GetReturnValue().Set(Point::WrapObject(point));
- }
- static v8::Local<v8::Object> WrapObject(Line* object)
- {
- v8::Isolate* isolate = v8::Isolate::GetCurrent();
- v8::Local<v8::Context> context = isolate->GetCurrentContext();
- if (v8_class_template.IsEmpty())
- {
- v8_class_template = v8::ObjectTemplate::New(isolate);
- v8_class_template->SetInternalFieldCount(1);
- v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "startPoint"), Line::V8GetStartPoint /*, Line::V8SetX */);
- }
- v8::Local<v8::Object> obj = v8_class_template->NewInstance(context).ToLocalChecked();
- obj->SetInternalField(0, v8::External::New(isolate, object));
- return obj;
- }
- };
- v8::Local<v8::ObjectTemplate> Line::v8_class_template;
- void RunScript(v8::Isolate* isolate);
- int main()
- {
- char filename[MAX_PATH];
- GetModuleFileNameA(NULL, filename, MAX_PATH);
- v8::V8::InitializeICUDefaultLocation(filename);
- v8::V8::InitializeExternalStartupData(filename);
- std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
- v8::V8::InitializePlatform(platform.get());
- v8::V8::Initialize();
- v8::Isolate::CreateParams create_params;
- create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
- v8::Isolate* isolate = v8::Isolate::New(create_params);
- RunScript(isolate);
- isolate->Dispose();
- v8::V8::Dispose();
- v8::V8::ShutdownPlatform();
- delete create_params.array_buffer_allocator;
- return 0;
- }
- void RunScript(v8::Isolate* isolate)
- {
- 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::Object> global = context->Global();
- Point* p = new Point();
- p->x = 5;
- p->y = 6;
- Line* l = new Line();
- l->startPoint = p;
- v8::Local<v8::Object> obj = Line::WrapObject(l);
- global->Set(V8_STR("line"), obj);
- v8::Local<v8::String> source = V8_STR("line.startPoint;"); // <----------------------------- Prints [object Object]
- v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
- v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
- //v8::Local<v8::Value> incFunctionVal = global->Get(V8_STR("inc"));
- //v8::Local<v8::Function> incFunction = v8::Local<v8::Function>::Cast(incFunctionVal);
- //incFunction->Call(context, global, 0, {});
- //source = V8_STR("a;");
- //script = v8::Script::Compile(context, source).ToLocalChecked();
- //result = script->Run(context).ToLocalChecked();
- v8::String::Utf8Value utf8(isolate, result);
- printf("%s\n", *utf8);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement