Advertisement
Guest User

Untitled

a guest
Nov 13th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <Windows.h>
  5.  
  6. #include "libplatform/libplatform.h"
  7. #include "v8.h"
  8.  
  9. #include "V8Helper.h"
  10.  
  11. #define V8_STR(s) v8::String::NewFromUtf8(v8::Isolate::GetCurrent(), s, v8::NewStringType::kNormal).ToLocalChecked()
  12.  
  13. class Point
  14. {
  15. private:
  16.     static v8::Local<v8::ObjectTemplate> v8_class_template;
  17.  
  18. public:
  19.     int x = 0;
  20.     int y = 0;
  21.  
  22.     static void V8GetX(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
  23.     {
  24.         Point* point = V8Helper::UnwrapObject<Point>(info);
  25.         int value = point->x;
  26.         info.GetReturnValue().Set(value);
  27.     }
  28.  
  29.     static void V8SetX(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
  30.     {
  31.         Point* point = V8Helper::UnwrapObject<Point>(info);
  32.         point->y = value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromMaybe(0);
  33.     }
  34.  
  35.     static void V8GetY(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
  36.     {
  37.         Point* point = V8Helper::UnwrapObject<Point>(info);
  38.         int value = point->y;
  39.         info.GetReturnValue().Set(value);
  40.     }
  41.  
  42.     static void V8SetY(v8::Local<v8::Name> property, v8::Local<v8::Value> value, const v8::PropertyCallbackInfo<void>& info)
  43.     {
  44.         Point* point = V8Helper::UnwrapObject<Point>(info);
  45.         point->x = value->Int32Value(info.GetIsolate()->GetCurrentContext()).FromMaybe(0);
  46.     }
  47.  
  48.     static v8::Local<v8::Object> WrapObject(Point* object)
  49.     {
  50.         v8::Isolate* isolate = v8::Isolate::GetCurrent();
  51.         v8::Local<v8::Context> context = isolate->GetCurrentContext();
  52.  
  53.         if (v8_class_template.IsEmpty())
  54.         {
  55.             v8_class_template = v8::ObjectTemplate::New(isolate);
  56.             v8_class_template->SetInternalFieldCount(1);
  57.             v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "x"), Point::V8GetX, Point::V8SetX);
  58.             v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "y"), Point::V8GetY, Point::V8SetY);
  59.         }
  60.  
  61.         v8::Local<v8::Object> obj = v8_class_template->NewInstance(context).ToLocalChecked();
  62.         obj->SetInternalField(0, v8::External::New(isolate, object));
  63.  
  64.         return obj;
  65.     }
  66. };
  67. v8::Local<v8::ObjectTemplate> Point::v8_class_template;
  68.  
  69. class Line
  70. {
  71. private:
  72.     static v8::Local<v8::ObjectTemplate> v8_class_template;
  73.  
  74. public:
  75.     Point* startPoint;
  76.     Point* endPoint;
  77.  
  78.     static void V8GetStartPoint(v8::Local<v8::Name> property, const v8::PropertyCallbackInfo<v8::Value>& info)
  79.     {
  80.         Line* line = V8Helper::UnwrapObject<Line>(info);
  81.         Point* point = line->startPoint;
  82.         info.GetReturnValue().Set(Point::WrapObject(point));
  83.     }
  84.  
  85.     static v8::Local<v8::Object> WrapObject(Line* object)
  86.     {
  87.         v8::Isolate* isolate = v8::Isolate::GetCurrent();
  88.         v8::Local<v8::Context> context = isolate->GetCurrentContext();
  89.  
  90.         if (v8_class_template.IsEmpty())
  91.         {
  92.             v8_class_template = v8::ObjectTemplate::New(isolate);
  93.             v8_class_template->SetInternalFieldCount(1);
  94.             v8_class_template->SetAccessor(v8::String::NewFromUtf8(isolate, "startPoint"), Line::V8GetStartPoint /*, Line::V8SetX */);
  95.         }
  96.  
  97.         v8::Local<v8::Object> obj = v8_class_template->NewInstance(context).ToLocalChecked();
  98.         obj->SetInternalField(0, v8::External::New(isolate, object));
  99.  
  100.         return obj;
  101.     }
  102. };
  103. v8::Local<v8::ObjectTemplate> Line::v8_class_template;
  104.  
  105. void RunScript(v8::Isolate* isolate);
  106.  
  107. int main()
  108. {
  109.     char filename[MAX_PATH];
  110.     GetModuleFileNameA(NULL, filename, MAX_PATH);
  111.  
  112.     v8::V8::InitializeICUDefaultLocation(filename);
  113.     v8::V8::InitializeExternalStartupData(filename);
  114.     std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
  115.     v8::V8::InitializePlatform(platform.get());
  116.     v8::V8::Initialize();
  117.  
  118.     v8::Isolate::CreateParams create_params;
  119.     create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
  120.     v8::Isolate* isolate = v8::Isolate::New(create_params);
  121.  
  122.     RunScript(isolate);
  123.  
  124.     isolate->Dispose();
  125.     v8::V8::Dispose();
  126.     v8::V8::ShutdownPlatform();
  127.     delete create_params.array_buffer_allocator;
  128.     return 0;
  129. }
  130.  
  131. void RunScript(v8::Isolate* isolate)
  132. {
  133.     v8::Isolate::Scope isolate_scope(isolate);
  134.     v8::HandleScope handle_scope(isolate);
  135.     v8::Local<v8::Context> context = v8::Context::New(isolate);
  136.     v8::Context::Scope context_scope(context);
  137.     {
  138.         v8::Local<v8::Object> global = context->Global();
  139.  
  140.         Point* p = new Point();
  141.         p->x = 5;
  142.         p->y = 6;
  143.  
  144.         Line* l = new Line();
  145.         l->startPoint = p;
  146.  
  147.         v8::Local<v8::Object> obj = Line::WrapObject(l);
  148.         global->Set(V8_STR("line"), obj);
  149.  
  150.         v8::Local<v8::String> source = V8_STR("line.startPoint;"); // <----------------------------- Prints [object Object]
  151.         v8::Local<v8::Script> script = v8::Script::Compile(context, source).ToLocalChecked();
  152.         v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
  153.  
  154.         //v8::Local<v8::Value> incFunctionVal = global->Get(V8_STR("inc"));
  155.         //v8::Local<v8::Function> incFunction = v8::Local<v8::Function>::Cast(incFunctionVal);
  156.         //incFunction->Call(context, global, 0, {});
  157.  
  158.         //source = V8_STR("a;");
  159.         //script = v8::Script::Compile(context, source).ToLocalChecked();
  160.         //result = script->Run(context).ToLocalChecked();
  161.  
  162.         v8::String::Utf8Value utf8(isolate, result);
  163.         printf("%s\n", *utf8);
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement