-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
v8.cpp
39 lines (33 loc) · 1.44 KB
/
v8.cpp
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
#include "./dependencies/v8/include/v8.h"
#include "./dependencies/v8/include/libplatform/libplatform.h"
#include <iostream>
void InitializeV8() {
v8::V8::InitializeICUDefaultLocation("dependencies/v8");
v8::V8::InitializeExternalStartupData("dependencies/v8");
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
}
const char* RunJS(const char* source) {
v8::Isolate::CreateParams create_params;
v8::Isolate* isolate = v8::Isolate::New(create_params);
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::String> source_str = v8::String::NewFromUtf8(isolate, source,
v8::NewStringType::kNormal)
.ToLocalChecked();
v8::Local<v8::Script> script = v8::Script::Compile(context, source_str).ToLocalChecked();
v8::Local<v8::Value> result = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8(isolate, result);
return *utf8 ? strdup(*utf8) : "<conversion failed>";
}
extern "C" {
void Initialize() {
InitializeV8();
}
const char* Execute(const char* code) {
return RunJS(code);
}
}