forked from tokuhirom/node-function-inspector
-
Notifications
You must be signed in to change notification settings - Fork 1
/
addon.cc
50 lines (38 loc) · 1.59 KB
/
addon.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <node.h>
using namespace v8;
void Method(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
args.GetReturnValue().Set(String::NewFromUtf8(isolate, "world"));
}
void fi_inspect(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);
if(args.Length() < 1) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Wrong number of arguments")));
return;
}
if(!args[0]->IsFunction()) {
isolate->ThrowException(Exception::TypeError(
String::NewFromUtf8(isolate, "Argument must be a function")));
return;
}
Local<Function> func = Local<Function>::Cast(args[0]);
Local<Object> retval = Object::New(isolate);
Handle<Value> resourceName = func->GetScriptOrigin().ResourceName();
if (*resourceName) {
retval->Set(String::NewFromUtf8(isolate, "File"), func->GetScriptOrigin().ResourceName());
} else {
retval->Set(String::NewFromUtf8(isolate, "File"), Undefined(isolate));
}
retval->Set(String::NewFromUtf8(isolate, "Name"), func->GetName());
retval->Set(String::NewFromUtf8(isolate, "InferredName"), func->GetInferredName());
retval->Set(String::NewFromUtf8(isolate, "LineNumber"), Integer::New(isolate, func->GetScriptLineNumber()));
retval->Set(String::NewFromUtf8(isolate, "ColumnNumber"), Integer::New(isolate, func->GetScriptColumnNumber()));
args.GetReturnValue().Set(retval);
}
void init(Handle<Object> exports) {
NODE_SET_METHOD(exports, "inspect", fi_inspect);
}
NODE_MODULE(addon, init)