This repository was archived by the owner on Mar 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathhello_world.cpp
98 lines (67 loc) · 2.77 KB
/
hello_world.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
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
#include <v8.h>
#include <libplatform/libplatform.h>
#include <stdlib.h>
#include <string.h>
using namespace v8;
class ArrayBufferAllocator : public v8::ArrayBuffer::Allocator {
public:
virtual void* Allocate(size_t length) {
void* data = AllocateUninitialized(length);
return data == NULL ? data : memset(data, 0, length);
}
virtual void* AllocateUninitialized(size_t length) { return malloc(length); }
virtual void Free(void* data, size_t) { free(data); }
};
void weak_callback(const v8::WeakCallbackInfo<v8::Persistent<v8::String>>& data) {
printf("Weak callback called\n");
data.GetParameter()->Reset();
// data.GetIsolate()->AdjustAmountOfExternalAllocatedMemory(-(1024*1024*1024));
}
int main(int argc, char* argv[]) {
// Initialize V8.
v8::V8::InitializeICU();
v8::Platform *platform = v8::platform::CreateDefaultPlatform();
v8::V8::InitializePlatform(platform);
V8::Initialize();
ArrayBufferAllocator *array_buffer_allocator;
v8::Isolate::CreateParams *create_params;
array_buffer_allocator = new ArrayBufferAllocator();
create_params = new v8::Isolate::CreateParams();
create_params->array_buffer_allocator = array_buffer_allocator;
// Create a new Isolate and make it the current one.
Isolate* isolate = v8::Isolate::New(*create_params);
v8::Persistent<v8::String> test;
{
Isolate::Scope isolate_scope(isolate);
// Create a stack-allocated handle scope.
HandleScope handle_scope(isolate);
// Create a new context.
Local<Context> context = Context::New(isolate);
// Enter the context for compiling and running the hello world script.
Context::Scope context_scope(context);
test.Reset(isolate, String::NewFromUtf8(isolate, "Hello' + ', World!'"));
test.SetWeak(&test, weak_callback, v8::WeakCallbackType::kParameter);
// isolate->AdjustAmountOfExternalAllocatedMemory((1024*1024*1024));
// Create a string containing the JavaScript source code.
// Local<String> source = String::NewFromUtf8(isolate, "(2+2*2) + ' ' + hw");
Local<String> source = String::NewFromUtf8(isolate, "(2+2*2) + ' ' + 'Hello' + ', World!'");
// v8::Local<v8::String> hw = v8::Local<v8::String>::New(isolate, test);
// context->Global()->Set(String::NewFromUtf8(isolate, "hw"), hw);
// Compile the source code.
Local<Script> script = Script::Compile(source);
// Run the script to get the result.
Local<Value> result = script->Run();
// Convert the result to an UTF8 string and print it.
String::Utf8Value utf8(isolate, result);
printf("%s\n", *utf8);
}
isolate->LowMemoryNotification();
// Dispose the isolate and tear down V8.
isolate->Dispose();
V8::Dispose();
V8::ShutdownPlatform();
delete array_buffer_allocator;
delete create_params;
delete platform;
return 0;
}