forked from pmed/v8pp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_context.cpp
124 lines (98 loc) · 4.19 KB
/
test_context.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
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
#include "v8pp/context.hpp"
#include "v8pp/object.hpp"
#include "v8pp/module.hpp"
#include "test.hpp"
#include <type_traits>
static_assert(std::is_move_constructible_v<v8pp::context>);
static_assert(std::is_move_assignable_v<v8pp::context>);
static_assert(!std::is_copy_assignable_v<v8pp::context>);
static_assert(!std::is_copy_constructible_v<v8pp::context>);
void test_context()
{
{
v8pp::context context;
v8::HandleScope scope(context.isolate());
v8::Local<v8::Value> result = context.run_script("42");
int const r = result->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
check_eq("run_script", r, 42);
v8::TryCatch try_catch(context.isolate());
result = context.run_script("syntax error");
check("script with syntax error", result.IsEmpty());
check(" has caught", try_catch.HasCaught());
}
{
v8::Isolate* isolate = nullptr;
v8::ArrayBuffer::Allocator* allocator = nullptr;
bool add_default_global_methods = false;
v8pp::context context(isolate, allocator, add_default_global_methods);
v8::HandleScope scope(context.isolate());
v8::Local<v8::Object> global = context.isolate()->GetCurrentContext()->Global();
v8::Local<v8::Value> value;
check("no global require", !v8pp::get_option(context.isolate(), global, "require", value));
check("no global run", !v8pp::get_option(context.isolate(), global, "run", value));
int const r = context.run_script("'4' + 2")->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
check_eq("run_script", r, 42);
}
{
v8pp::context::options options;
options.add_default_global_methods = false;
options.enter_context = false;
v8pp::context context(options);
v8::HandleScope scope(context.isolate());
v8::Context::Scope context_scope(context.impl());
v8::Local<v8::Object> global = context.isolate()->GetCurrentContext()->Global();
v8::Local<v8::Value> value;
check("no global require", !v8pp::get_option(context.isolate(), global, "require", value));
check("no global run", !v8pp::get_option(context.isolate(), global, "run", value));
int const r = context.run_script("'4' + 2")->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
check_eq("run_script with explicit context", r, 42);
}
{
// Move constuctor allows to set up context inside function
// also it allows to move class with v8pp::context as a member value
auto setup_context = []()
{
v8pp::context::options options;
options.add_default_global_methods = false;
options.enter_context = false;
v8pp::context context(options);
return context;
};
v8pp::context context0 = setup_context();
check("returned context", context0.isolate() != nullptr && !context0.impl().IsEmpty());
v8pp::context context = std::move(context0);
check("moved from context", context0.isolate() == nullptr && context0.impl().IsEmpty());
check("moved context", context.isolate() != nullptr && !context.impl().IsEmpty());
v8::HandleScope scope(context.isolate());
v8::Context::Scope context_scope(context.impl());
v8::Local<v8::Object> global = context.isolate()->GetCurrentContext()->Global();
v8::Local<v8::Value> value;
check("no global require", !v8pp::get_option(context.isolate(), global, "require", value));
check("no global run", !v8pp::get_option(context.isolate(), global, "run", value));
int const r = context.run_script("'4' + 2")->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
check_eq("run_script with externally set up context", r, 42);
}
{
const auto init_global = [](v8::Isolate* isolate)
{
v8pp::module m(isolate);
m.const_("value", 40);
m.function("func", []() { return 2; });
return m.impl();
};
// Isolate and HandleScope shall exist before init_global
v8::Isolate* isolate = v8pp::context::create_isolate();
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope scope(isolate);
v8pp::context::options opt;
opt.isolate = isolate; // use existing one
opt.add_default_global_methods = false;
opt.global = init_global(isolate);
v8pp::context context(opt);
int const r = context.run_script("value + func()")->Int32Value(context.isolate()->GetCurrentContext()).FromJust();
check_eq("run_script with customized global", r, 42);
}
isolate->Dispose();
}
}