forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathisolate.cc
More file actions
118 lines (94 loc) · 3.15 KB
/
isolate.cc
File metadata and controls
118 lines (94 loc) · 3.15 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
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
#include "deps/include/v8-context.h"
#include "deps/include/v8-initialization.h"
#include "deps/include/v8-locker.h"
#include "deps/include/v8-platform.h"
#include "context.h"
#include "isolate.h"
#include "libplatform/libplatform.h"
using namespace v8;
auto default_platform = platform::NewDefaultPlatform();
ArrayBuffer::Allocator* default_allocator;
extern "C" {
/********** Isolate **********/
#define ISOLATE_SCOPE(iso) \
Locker locker(iso); \
Isolate::Scope isolate_scope(iso); \
HandleScope handle_scope(iso);
void Init() {
#ifdef _WIN32
V8::InitializeExternalStartupData(".");
#endif
V8::InitializePlatform(default_platform.get());
V8::Initialize();
default_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
return;
}
size_t NearMemoryLimitCallback(void* data, size_t current_heap_limit, size_t initial_heap_limit)
{
auto iso = static_cast<Isolate*>(data);
iso->TerminateExecution();
// if we return the initial heap limit, the VM will crash, so here we give it room to exit gracefully
return current_heap_limit * 2;
}
IsolatePtr NewIsolate(IsolateConstraintsPtr constraints) {
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
if (constraints != nullptr) {
ResourceConstraints rc;
rc.ConfigureDefaultsFromHeapSize(
constraints->initial_heap_size_in_bytes,
constraints->maximum_heap_size_in_bytes
);
params.constraints = rc;
}
Isolate* iso = Isolate::New(params);
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
iso->SetCaptureStackTraceForUncaughtExceptions(true);
// Try to catch the OOM condition and stop execution before killing the process
iso->AddNearHeapLimitCallback(NearMemoryLimitCallback, iso);
// Create a Context for internal use
m_ctx* ctx = new m_ctx;
ctx->ptr.Reset(iso, Context::New(iso));
ctx->iso = iso;
iso->SetData(0, ctx);
return iso;
}
void IsolatePerformMicrotaskCheckpoint(IsolatePtr iso) {
ISOLATE_SCOPE(iso)
iso->PerformMicrotaskCheckpoint();
}
void IsolateDispose(IsolatePtr iso) {
if (iso == nullptr) {
return;
}
auto ctx = static_cast<m_ctx*>(iso->GetData(0));
ContextFree(ctx);
iso->Dispose();
}
void IsolateTerminateExecution(IsolatePtr iso) {
iso->TerminateExecution();
}
int IsolateIsExecutionTerminating(IsolatePtr iso) {
return iso->IsExecutionTerminating();
}
IsolateHStatistics IsolationGetHeapStatistics(IsolatePtr iso) {
if (iso == nullptr) {
return IsolateHStatistics{0};
}
v8::HeapStatistics hs;
iso->GetHeapStatistics(&hs);
return IsolateHStatistics{hs.total_heap_size(),
hs.total_heap_size_executable(),
hs.total_physical_size(),
hs.total_available_size(),
hs.used_heap_size(),
hs.heap_size_limit(),
hs.malloced_memory(),
hs.external_memory(),
hs.peak_malloced_memory(),
hs.number_of_native_contexts(),
hs.number_of_detached_contexts()};
}
}