1 #include <node.h>
2 #include <v8.h>
3 
Method(const v8::FunctionCallbackInfo<v8::Value> & args)4 void Method(const v8::FunctionCallbackInfo<v8::Value>& args) {
5   v8::Isolate* isolate = args.GetIsolate();
6   args.GetReturnValue().Set(v8::String::NewFromUtf8(
7         isolate, "world", v8::NewStringType::kNormal).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!", v8::NewStringType::kNormal)
25           .ToLocalChecked());
26   isolate->ThrowException(exception);
27 }
28 
29 // Define a Node.js module, but with the wrong version. Node.js should still be
30 // able to load this module, multiple times even, because it exposes the
31 // specially named initializer above.
32 #undef NODE_MODULE_VERSION
33 #define NODE_MODULE_VERSION 3
34 NODE_MODULE(NODE_GYP_MODULE_NAME, FakeInit)
35