-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
78 lines (69 loc) · 1.97 KB
/
Copy pathmain.cpp
File metadata and controls
78 lines (69 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include "include/CppArg.h"
#include "include/CppBindClass.h"
#include "include/CppBindModule.h"
#include "include/CppFunction.h"
#include "include/CppInvoke.h"
#include "include/CppObject.h"
#include "include/V8Type.h"
#include <v8.h>
#include <libplatform/libplatform.h>
#include <string>
#include <iostream>
class Test
{
public:
Test(const char *name) : name(name) {}
void test() { std::cout << name << std::endl; }
std::string name;
};
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator
{
public:
virtual void *Allocate(size_t length)
{
auto data = AllocateUninitialized(length);
return data == nullptr ? data : memset(data, 0, length);
}
virtual void *AllocateUninitialized(size_t length)
{
return malloc(length);
}
virtual void Free(void *data, size_t)
{
free(data);
}
};
int main(int argc, char *argv[])
{
v8::V8::InitializeICU();
auto mPlatform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(mPlatform);
v8::V8::Initialize();
auto mAllocator = new ArrayBufferAllocator;
v8::Isolate::CreateParams params;
params.array_buffer_allocator = mAllocator;
auto mIsolate = v8::Isolate::New(params);
mIsolate->Enter();
{
v8::HandleScope scope(mIsolate);
v8::Handle<v8::ObjectTemplate> global = v8::ObjectTemplate::New(mIsolate);
v8::Handle<v8::Context> context = v8::Context::New(mIsolate, nullptr, global);
context->Enter();
}
{
V8Binding(v8::Isolate::GetCurrent()->GetCurrentContext()->Global())
.beginModule("Module")
.beginClass<Test>("Class")
.addConstructor(V8_ARGS(const char *))
.addFunction("test", &Test::test)
.endClass()
.endModule();
}
mIsolate->Exit();
mIsolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete mPlatform;
delete mAllocator;
return 1;
}