• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <node.h>
2 #include <v8.h>
3 
4 using v8::Context;
5 using v8::FunctionCallbackInfo;
6 using v8::Isolate;
7 using v8::Local;
8 using v8::Object;
9 using v8::Value;
10 
TriggerReport(const FunctionCallbackInfo<Value> & args)11 void TriggerReport(const FunctionCallbackInfo<Value>& args) {
12   Isolate* isolate = args.GetIsolate();
13 
14   node::TriggerNodeReport(
15       isolate, "FooMessage", "BarTrigger", std::string(), Local<Value>());
16 }
17 
TriggerReportNoIsolate(const FunctionCallbackInfo<Value> & args)18 void TriggerReportNoIsolate(const FunctionCallbackInfo<Value>& args) {
19   node::TriggerNodeReport(static_cast<Isolate*>(nullptr),
20                           "FooMessage",
21                           "BarTrigger",
22                           std::string(),
23                           Local<Value>());
24 }
25 
TriggerReportEnv(const FunctionCallbackInfo<Value> & args)26 void TriggerReportEnv(const FunctionCallbackInfo<Value>& args) {
27   Isolate* isolate = args.GetIsolate();
28 
29   node::TriggerNodeReport(
30       node::GetCurrentEnvironment(isolate->GetCurrentContext()),
31       "FooMessage",
32       "BarTrigger",
33       std::string(),
34       Local<Value>());
35 }
36 
TriggerReportNoEnv(const FunctionCallbackInfo<Value> & args)37 void TriggerReportNoEnv(const FunctionCallbackInfo<Value>& args) {
38   node::TriggerNodeReport(static_cast<node::Environment*>(nullptr),
39                           "FooMessage",
40                           "BarTrigger",
41                           std::string(),
42                           Local<Value>());
43 }
44 
TriggerReportNoContext(const FunctionCallbackInfo<Value> & args)45 void TriggerReportNoContext(const FunctionCallbackInfo<Value>& args) {
46   Isolate* isolate = args.GetIsolate();
47   Local<Context> context = isolate->GetCurrentContext();
48   context->Exit();
49 
50   if (isolate->GetCurrentContext().IsEmpty()) {
51     node::TriggerNodeReport(
52         isolate, "FooMessage", "BarTrigger", std::string(), Local<Value>());
53   }
54 
55   // Restore current context to avoid crashing in Context::Scope in
56   // SpinEventLoop.
57   context->Enter();
58 }
59 
TriggerReportNewContext(const FunctionCallbackInfo<Value> & args)60 void TriggerReportNewContext(const FunctionCallbackInfo<Value>& args) {
61   Isolate* isolate = args.GetIsolate();
62   Local<Context> context = Context::New(isolate);
63   Context::Scope context_scope(context);
64 
65   node::TriggerNodeReport(
66       isolate, "FooMessage", "BarTrigger", std::string(), Local<Value>());
67 }
68 
init(Local<Object> exports)69 void init(Local<Object> exports) {
70   NODE_SET_METHOD(exports, "triggerReport", TriggerReport);
71   NODE_SET_METHOD(exports, "triggerReportNoIsolate", TriggerReportNoIsolate);
72   NODE_SET_METHOD(exports, "triggerReportEnv", TriggerReportEnv);
73   NODE_SET_METHOD(exports, "triggerReportNoEnv", TriggerReportNoEnv);
74   NODE_SET_METHOD(exports, "triggerReportNoContext", TriggerReportNoContext);
75   NODE_SET_METHOD(exports, "triggerReportNewContext", TriggerReportNewContext);
76 }
77 
78 NODE_MODULE(NODE_GYP_MODULE_NAME, init)
79