forked from rogchap/v8go
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
isolate.cc
96 lines (77 loc) · 2.44 KB
/
isolate.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
#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;
}
IsolatePtr NewIsolate() {
Isolate::CreateParams params;
params.array_buffer_allocator = default_allocator;
Isolate* iso = Isolate::New(params);
Locker locker(iso);
Isolate::Scope isolate_scope(iso);
HandleScope handle_scope(iso);
iso->SetCaptureStackTraceForUncaughtExceptions(true);
// 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()};
}
}