forked from danbev/learning-v8
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run-script.cc
133 lines (109 loc) · 3.94 KB
/
run-script.cc
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <string>
#include "libplatform/libplatform.h"
#include "v8.h"
using namespace v8;
v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name);
void Print(const v8::FunctionCallbackInfo<v8::Value>& args);
class Person {
private:
std::string name_;
public:
Person(std::string name) : name_(name) {
};
std::string name() const {
return name_;
}
};
void NewPerson(const FunctionCallbackInfo<Value>& args) {
String::Utf8Value str(args[0]);
Person *p = new Person(*str);
Local<Object> self = args.Holder();
self->SetAlignedPointerInInternalField(0, p);
}
void GetName(Local<String> property, const PropertyCallbackInfo<Value>& info) {
Local<Object> self = info.Holder();
Local<External> wrap = Local<External>::Cast(self->GetInternalField(0));
void* pointer = self->GetAlignedPointerFromInternalField(0);
const std::string value = static_cast<Person*>(pointer)->name();
info.GetReturnValue().Set(String::NewFromUtf8(info.GetIsolate(), value.c_str(), NewStringType::kNormal).ToLocalChecked());
}
int main(int argc, char* argv[]) {
V8::InitializeICU();
V8::InitializeExternalStartupData(argv[0]);
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator();
Isolate* isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<ObjectTemplate> global = ObjectTemplate::New(isolate);
Local<FunctionTemplate> function_template = FunctionTemplate::New(isolate, NewPerson);
function_template->SetClassName(String::NewFromUtf8(isolate, "Person"));
function_template->InstanceTemplate()->SetInternalFieldCount(1);
function_template->InstanceTemplate()->SetAccessor(String::NewFromUtf8(isolate, "name"), GetName, nullptr);
Local<ObjectTemplate> person_template = ObjectTemplate::New(isolate, function_template);
person_template->SetInternalFieldCount(1);
global->Set(String::NewFromUtf8(isolate, "Person", NewStringType::kNormal).ToLocalChecked(), function_template);
global->Set(String::NewFromUtf8(isolate, "print", NewStringType::kNormal).ToLocalChecked(), FunctionTemplate::New(isolate, Print));
Local<Context> context = Context::New(isolate, NULL, global);
Context::Scope context_scope(context);
Local<String> source = ReadFile(isolate, "script.js").ToLocalChecked();
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
script->Run(context).ToLocalChecked();
}
// Dispose the isolate and tear down V8.
isolate->Dispose();
V8::Dispose();
V8::ShutdownPlatform();
delete platform;
return 0;
}
// Reads a file into a v8 string.
v8::MaybeLocal<v8::String> ReadFile(v8::Isolate* isolate, const char* name) {
FILE* file = fopen(name, "rb");
if (file == NULL) {
return v8::MaybeLocal<v8::String>();
}
fseek(file, 0, SEEK_END);
size_t size = ftell(file);
rewind(file);
char* chars = new char[size + 1];
chars[size] = '\0';
for (size_t i = 0; i < size;) {
i += fread(&chars[i], 1, size - i, file);
if (ferror(file)) {
fclose(file);
return v8::MaybeLocal<v8::String>();
}
}
fclose(file);
v8::MaybeLocal<v8::String> result = v8::String::NewFromUtf8(isolate,
chars,
v8::NewStringType::kNormal,
static_cast<int>(size));
delete[] chars;
return result;
}
void Print(const v8::FunctionCallbackInfo<v8::Value>& args) {
bool first = true;
for (int i = 0; i < args.Length(); i++) {
v8::HandleScope handle_scope(args.GetIsolate());
if (first) {
first = false;
} else {
printf(" ");
}
v8::String::Utf8Value str(args[i]);
const char* cstr = *str;
printf("%s", cstr);
}
printf("\n");
fflush(stdout);
}