1 #include <node.h>
2 #include <v8.h>
3
Method(const v8::FunctionCallbackInfo<v8::Value> & args)4 static void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
5 v8::Isolate* isolate = args.GetIsolate();
6 args.GetReturnValue().Set(v8::String::NewFromUtf8(
7 isolate, "world").ToLocalChecked());
8 }
9
10 // Not using the full NODE_MODULE_INIT() macro here because we want to test the
11 // addon loader's reaction to the FakeInit() entry point below.
12 extern "C" NODE_MODULE_EXPORT void
NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,v8::Local<v8::Value> module,v8::Local<v8::Context> context)13 NODE_MODULE_INITIALIZER(v8::Local<v8::Object> exports,
14 v8::Local<v8::Value> module,
15 v8::Local<v8::Context> context) {
16 NODE_SET_METHOD(exports, "hello", Method);
17 }
18
FakeInit(v8::Local<v8::Object> exports,v8::Local<v8::Value> module,v8::Local<v8::Context> context)19 static void FakeInit(v8::Local<v8::Object> exports,
20 v8::Local<v8::Value> module,
21 v8::Local<v8::Context> context) {
22 auto isolate = context->GetIsolate();
23 auto exception = v8::Exception::Error(v8::String::NewFromUtf8(isolate,
24 "FakeInit should never run!").ToLocalChecked());
25 isolate->ThrowException(exception);
26 }
27
28 // Define a Node.js module, but with the wrong version. Node.js should still be
29 // able to load this module, multiple times even, because it exposes the
30 // specially named initializer above.
31 #undef NODE_MODULE_VERSION
32 #define NODE_MODULE_VERSION 3
33 NODE_MODULE(NODE_GYP_MODULE_NAME, FakeInit)
34