From db2a5b05dfb3634e8fdb965ef5d4c75f73094e9e Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Tue, 5 Mar 2019 20:09:43 +0100 Subject: src: add public API for linked bindings (Re-?)add a public API for creating linked bindings (access to `NM_F_LINKED` as a constant was previously removed in d6ac8a4db0c0a588258f594dc21fbd8018bef7c2), and add a test for the functionality. PR-URL: https://github.com/nodejs/node/pull/26457 Reviewed-By: Ben Noordhuis Reviewed-By: Joyee Cheung Reviewed-By: Shelley Vohr Reviewed-By: James M Snell --- test/cctest/test_linked_binding.cc | 42 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 test/cctest/test_linked_binding.cc (limited to 'test/cctest') diff --git a/test/cctest/test_linked_binding.cc b/test/cctest/test_linked_binding.cc new file mode 100644 index 0000000000..e0b46d3822 --- /dev/null +++ b/test/cctest/test_linked_binding.cc @@ -0,0 +1,42 @@ +#include "node_test_fixture.h" +#include "node_internals.h" // RunBootstrapping() + +void InitializeBinding(v8::Local exports, + v8::Local module, + v8::Local context) { + v8::Isolate* isolate = context->GetIsolate(); + exports->Set( + context, + v8::String::NewFromOneByte(isolate, + reinterpret_cast("key"), + v8::NewStringType::kNormal).ToLocalChecked(), + v8::String::NewFromOneByte(isolate, + reinterpret_cast("value"), + v8::NewStringType::kNormal).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 context = isolate_->GetCurrentContext(); + + const char* run_script = + "process._linkedBinding('cctest_linkedbinding').key"; + v8::Local script = v8::Script::Compile( + context, + v8::String::NewFromOneByte(isolate_, + reinterpret_cast(run_script), + v8::NewStringType::kNormal).ToLocalChecked()) + .ToLocalChecked(); + v8::Local completion_value = script->Run(context).ToLocalChecked(); + v8::String::Utf8Value utf8val(isolate_, completion_value); + CHECK_NOT_NULL(*utf8val); + CHECK_EQ(strcmp(*utf8val, "value"), 0); +} -- cgit v1.2.3