1#include <node.h>
2#include <v8.h>
3
4using v8::Context;
5using v8::FunctionCallbackInfo;
6using v8::Isolate;
7using v8::Local;
8using v8::Object;
9using v8::Value;
10
11void TriggerReport(const FunctionCallbackInfo<Value>& args) {
12  Isolate* isolate = args.GetIsolate();
13
14  node::TriggerNodeReport(
15      isolate, "FooMessage", "BarTrigger", std::string(), Local<Value>());
16}
17
18void TriggerReportNoIsolate(const FunctionCallbackInfo<Value>& args) {
19  node::TriggerNodeReport(static_cast<Isolate*>(nullptr),
20                          "FooMessage",
21                          "BarTrigger",
22                          std::string(),
23                          Local<Value>());
24}
25
26void 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
37void 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
45void 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
60void 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
69void 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
78NODE_MODULE(NODE_GYP_MODULE_NAME, init)
79