src: add public API for linked bindings · nodejs/node@a13f0a6 (original) (raw)
``
1
`+
#include "node_test_fixture.h"
`
``
2
`+
#include "node_internals.h" // RunBootstrapping()
`
``
3
+
``
4
`+
void InitializeBinding(v8::Localv8::Object exports,
`
``
5
`+
v8::Localv8::Value module,
`
``
6
`+
v8::Localv8::Context context) {
`
``
7
`+
v8::Isolate* isolate = context->GetIsolate();
`
``
8
`+
exports->Set(
`
``
9
`+
context,
`
``
10
`+
v8::String::NewFromOneByte(isolate,
`
``
11
`+
reinterpret_cast<const uint8_t*>("key"),
`
``
12
`+
v8::NewStringType::kNormal).ToLocalChecked(),
`
``
13
`+
v8::String::NewFromOneByte(isolate,
`
``
14
`+
reinterpret_cast<const uint8_t*>("value"),
`
``
15
`+
v8::NewStringType::kNormal).ToLocalChecked())
`
``
16
`+
.FromJust();
`
``
17
`+
}
`
``
18
+
``
19
`+
NODE_MODULE_LINKED(cctest_linkedbinding, InitializeBinding);
`
``
20
+
``
21
`+
class LinkedBindingTest : public EnvironmentTestFixture {};
`
``
22
+
``
23
`+
TEST_F(LinkedBindingTest, SimpleTest) {
`
``
24
`+
const v8::HandleScope handle_scope(isolate_);
`
``
25
`+
const Argv argv;
`
``
26
`+
Env test_env {handle_scope, argv};
`
``
27
+
``
28
`+
v8::Localv8::Context context = isolate_->GetCurrentContext();
`
``
29
+
``
30
`+
const char* run_script =
`
``
31
`+
"process._linkedBinding('cctest_linkedbinding').key";
`
``
32
`+
v8::Localv8::Script script = v8::Script::Compile(
`
``
33
`+
context,
`
``
34
`+
v8::String::NewFromOneByte(isolate_,
`
``
35
`+
reinterpret_cast<const uint8_t*>(run_script),
`
``
36
`+
v8::NewStringType::kNormal).ToLocalChecked())
`
``
37
`+
.ToLocalChecked();
`
``
38
`+
v8::Localv8::Value completion_value = script->Run(context).ToLocalChecked();
`
``
39
`+
v8::String::Utf8Value utf8val(isolate_, completion_value);
`
``
40
`+
CHECK_NOT_NULL(*utf8val);
`
``
41
`+
CHECK_EQ(strcmp(*utf8val, "value"), 0);
`
``
42
`+
}
`