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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
|
// Include node.h first to define NAPI_VERSION built with the
// binary.
// The node.h should also be included first in embedder's use case.
#include "node.h"
#include "node_api.h"
#include "node_test_fixture.h"
void InitializeBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
exports
->Set(context,
v8::String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>("key"))
.ToLocalChecked(),
v8::String::NewFromOneByte(
isolate, reinterpret_cast<const uint8_t*>("value"))
.ToLocalChecked())
.FromJust();
}
NODE_MODULE_LINKED(cctest_linkedbinding, InitializeBinding)
class LinkedBindingTest : public EnvironmentTestFixture {};
TEST_F(LinkedBindingTest, SimpleTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script = "process._linkedBinding('cctest_linkedbinding').key";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "value"), 0);
}
void InitializeLocalBinding(v8::Local<v8::Object> exports,
v8::Local<v8::Value> module,
v8::Local<v8::Context> context,
void* priv) {
++*static_cast<int*>(priv);
v8::Isolate* isolate = context->GetIsolate();
exports
->Set(context,
v8::String::NewFromOneByte(isolate,
reinterpret_cast<const uint8_t*>("key"))
.ToLocalChecked(),
v8::String::NewFromOneByte(
isolate, reinterpret_cast<const uint8_t*>("value"))
.ToLocalChecked())
.FromJust();
}
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
int calls = 0;
AddLinkedBinding(*test_env, "local_linked", InitializeLocalBinding, &calls);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script = "process._linkedBinding('local_linked').key";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "value"), 0);
CHECK_EQ(calls, 1);
}
napi_value InitializeLocalNapiBinding(napi_env env, napi_value exports) {
napi_value key, value;
CHECK_EQ(napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &key),
napi_ok);
CHECK_EQ(napi_create_string_utf8(env, "world", NAPI_AUTO_LENGTH, &value),
napi_ok);
CHECK_EQ(napi_set_property(env, exports, key, value), napi_ok);
return nullptr;
}
static napi_module local_linked_napi = {
NAPI_MODULE_VERSION,
node::ModuleFlags::kLinked,
nullptr,
InitializeLocalNapiBinding,
"local_linked_napi",
nullptr,
{0},
};
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env, local_linked_napi);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script = "process._linkedBinding('local_linked_napi').hello";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "world"), 0);
}
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiCallbackTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env,
"local_linked_napi_cb",
InitializeLocalNapiBinding,
NAPI_VERSION);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script =
"process._linkedBinding('local_linked_napi_cb').hello";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "world"), 0);
}
static int32_t node_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
napi_value InitializeLocalNapiRefBinding(napi_env env, napi_value exports) {
napi_value key, value;
CHECK_EQ(
napi_create_string_utf8(env, "napi_ref_created", NAPI_AUTO_LENGTH, &key),
napi_ok);
// In experimental Node-API version we can create napi_ref to any value type.
// Here we are trying to create a reference to the key string.
napi_ref ref{};
if (node_api_version == NAPI_VERSION_EXPERIMENTAL) {
CHECK_EQ(napi_create_reference(env, key, 1, &ref), napi_ok);
CHECK_EQ(napi_delete_reference(env, ref), napi_ok);
} else {
CHECK_EQ(napi_create_reference(env, key, 1, &ref), napi_invalid_arg);
}
CHECK_EQ(napi_get_boolean(env, ref != nullptr, &value), napi_ok);
CHECK_EQ(napi_set_property(env, exports, key, value), napi_ok);
return nullptr;
}
// napi_ref in Node-API version 8 cannot accept strings.
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiRefVersion8Test) {
node_api_version = NODE_API_DEFAULT_MODULE_API_VERSION;
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env,
"local_linked_napi_ref_v8",
InitializeLocalNapiRefBinding,
node_api_version);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script =
"process._linkedBinding('local_linked_napi_ref_v8').napi_ref_created";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
CHECK(completion_value->IsBoolean());
CHECK(!completion_value.As<v8::Boolean>()->Value());
}
// Experimental version of napi_ref in Node-API can accept strings.
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiRefExperimentalTest) {
node_api_version = NAPI_VERSION_EXPERIMENTAL;
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env,
"local_linked_napi_ref_experimental",
InitializeLocalNapiRefBinding,
node_api_version);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script = "process._linkedBinding('local_linked_napi_ref_"
"experimental').napi_ref_created";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
CHECK(completion_value->IsBoolean());
CHECK(completion_value.As<v8::Boolean>()->Value());
}
napi_value NapiLinkedWithInstanceData(napi_env env, napi_value exports) {
int* instance_data = new int(0);
CHECK_EQ(napi_set_instance_data(
env,
instance_data,
[](napi_env env, void* data, void* hint) {
++*static_cast<int*>(data);
},
nullptr),
napi_ok);
napi_value key, value;
CHECK_EQ(napi_create_string_utf8(env, "hello", NAPI_AUTO_LENGTH, &key),
napi_ok);
CHECK_EQ(napi_create_external_arraybuffer(
env, instance_data, 1, nullptr, nullptr, &value),
napi_ok);
CHECK_EQ(napi_set_property(env, exports, key, value), napi_ok);
return nullptr;
}
static napi_module local_linked_napi_id = {
NAPI_MODULE_VERSION,
node::ModuleFlags::kLinked,
nullptr,
NapiLinkedWithInstanceData,
"local_linked_napi_id",
nullptr,
{0},
};
TEST_F(LinkedBindingTest, LocallyDefinedLinkedBindingNapiInstanceDataTest) {
const v8::HandleScope handle_scope(isolate_);
int* instance_data = nullptr;
{
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env, local_linked_napi_id);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script =
"process._linkedBinding('local_linked_napi_id').hello";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value =
script->Run(context).ToLocalChecked();
CHECK(completion_value->IsArrayBuffer());
instance_data =
static_cast<int*>(completion_value.As<v8::ArrayBuffer>()->Data());
CHECK_NE(instance_data, nullptr);
CHECK_EQ(*instance_data, 0);
}
CHECK_EQ(*instance_data, 1);
delete instance_data;
}
TEST_F(LinkedBindingTest,
LocallyDefinedLinkedBindingNapiCallbackInstanceDataTest) {
const v8::HandleScope handle_scope(isolate_);
int* instance_data = nullptr;
{
const Argv argv;
Env test_env{handle_scope, argv};
AddLinkedBinding(*test_env,
"local_linked_napi_id_cb",
NapiLinkedWithInstanceData,
NAPI_VERSION);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script =
"process._linkedBinding('local_linked_napi_id_cb').hello";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value =
script->Run(context).ToLocalChecked();
CHECK(completion_value->IsArrayBuffer());
instance_data =
static_cast<int*>(completion_value.As<v8::ArrayBuffer>()->Data());
CHECK_NE(instance_data, nullptr);
CHECK_EQ(*instance_data, 0);
}
CHECK_EQ(*instance_data, 1);
delete instance_data;
}
TEST_F(LinkedBindingTest, ManyBindingsTest) {
const v8::HandleScope handle_scope(isolate_);
const Argv argv;
Env test_env{handle_scope, argv};
int calls = 0;
AddLinkedBinding(*test_env, "local_linked1", InitializeLocalBinding, &calls);
AddLinkedBinding(*test_env, "local_linked2", InitializeLocalBinding, &calls);
AddLinkedBinding(*test_env, "local_linked3", InitializeLocalBinding, &calls);
AddLinkedBinding(*test_env, local_linked_napi); // Add a N-API addon as well.
AddLinkedBinding(*test_env, "local_linked4", InitializeLocalBinding, &calls);
AddLinkedBinding(*test_env, "local_linked5", InitializeLocalBinding, &calls);
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
const char* run_script =
"for (let i = 1; i <= 5; i++)process._linkedBinding(`local_linked${i}`);"
"process._linkedBinding('local_linked_napi').hello";
v8::Local<v8::Script> script =
v8::Script::Compile(
context,
v8::String::NewFromOneByte(
isolate_, reinterpret_cast<const uint8_t*>(run_script))
.ToLocalChecked())
.ToLocalChecked();
v8::Local<v8::Value> completion_value = script->Run(context).ToLocalChecked();
v8::String::Utf8Value utf8val(isolate_, completion_value);
CHECK_NOT_NULL(*utf8val);
CHECK_EQ(strcmp(*utf8val, "world"), 0);
CHECK_EQ(calls, 5);
}
|