1 #include "node_test_fixture.h"
2 #include "node_internals.h" // RunBootstrapping()
3
InitializeBinding(v8::Local<v8::Object> exports,v8::Local<v8::Value> module,v8::Local<v8::Context> context,void * priv)4 void InitializeBinding(v8::Local<v8::Object> exports,
5 v8::Local<v8::Value> module,
6 v8::Local<v8::Context> context,
7 void* priv) {
8 v8::Isolate* isolate = context->GetIsolate();
9 exports->Set(
10 context,
11 v8::String::NewFromOneByte(isolate,
12 reinterpret_cast<const uint8_t*>("key"),
13 v8::NewStringType::kNormal).ToLocalChecked(),
14 v8::String::NewFromOneByte(isolate,
15 reinterpret_cast<const uint8_t*>("value"),
16 v8::NewStringType::kNormal).ToLocalChecked())
17 .FromJust();
18 }
19
20 NODE_MODULE_LINKED(cctest_linkedbinding, InitializeBinding)
21
22 class LinkedBindingTest : public EnvironmentTestFixture {};
23
TEST_F(LinkedBindingTest,SimpleTest)24 TEST_F(LinkedBindingTest, SimpleTest) {
25 const v8::HandleScope handle_scope(isolate_);
26 const Argv argv;
27 Env test_env {handle_scope, argv};
28
29 v8::Local<v8::Context> context = isolate_->GetCurrentContext();
30
31 const char* run_script =
32 "process._linkedBinding('cctest_linkedbinding').key";
33 v8::Local<v8::Script> script = v8::Script::Compile(
34 context,
35 v8::String::NewFromOneByte(isolate_,
36 reinterpret_cast<const uint8_t*>(run_script),
37 v8::NewStringType::kNormal).ToLocalChecked())
38 .ToLocalChecked();
39 v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
40 v8::String::Utf8Value utf8val(isolate_, completion_value);
41 CHECK_NOT_NULL(*utf8val);
42 CHECK_EQ(strcmp(*utf8val, "value"), 0);
43 }
44
InitializeLocalBinding(v8::Local<v8::Object> exports,v8::Local<v8::Value> module,v8::Local<v8::Context> context,void * priv)45 void InitializeLocalBinding(v8::Local<v8::Object> exports,
46 v8::Local<v8::Value> module,
47 v8::Local<v8::Context> context,
48 void* priv) {
49 ++*static_cast<int*>(priv);
50 v8::Isolate* isolate = context->GetIsolate();
51 exports->Set(
52 context,
53 v8::String::NewFromOneByte(isolate,
54 reinterpret_cast<const uint8_t*>("key"),
55 v8::NewStringType::kNormal).ToLocalChecked(),
56 v8::String::NewFromOneByte(isolate,
57 reinterpret_cast<const uint8_t*>("value"),
58 v8::NewStringType::kNormal).ToLocalChecked())
59 .FromJust();
60 }
61
TEST_F(LinkedBindingTest,LocallyDefinedLinkedBindingTest)62 TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingTest) {
63 const v8::HandleScope handle_scope(isolate_);
64 const Argv argv;
65 Env test_env {handle_scope, argv};
66
67 int calls = 0;
68 AddLinkedBinding(*test_env, "local_linked", InitializeLocalBinding, &calls);
69
70 v8::Local<v8::Context> context = isolate_->GetCurrentContext();
71
72 const char* run_script =
73 "process._linkedBinding('local_linked').key";
74 v8::Local<v8::Script> script = v8::Script::Compile(
75 context,
76 v8::String::NewFromOneByte(isolate_,
77 reinterpret_cast<const uint8_t*>(run_script),
78 v8::NewStringType::kNormal).ToLocalChecked())
79 .ToLocalChecked();
80 v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
81 v8::String::Utf8Value utf8val(isolate_, completion_value);
82 CHECK_NOT_NULL(*utf8val);
83 CHECK_EQ(strcmp(*utf8val, "value"), 0);
84 CHECK_EQ(calls, 1);
85 }
86