File size: 2,482 Bytes
befd7da |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
#include "dtrace_provider.h"
#include <nan.h>
namespace node {
using namespace v8;
DTraceProbe::DTraceProbe() : Nan::ObjectWrap() {
argc = 0;
probedef = NULL;
}
DTraceProbe::~DTraceProbe() {
for (size_t i = 0; i < argc; i++)
delete(this->arguments[i]);
usdt_probe_release(probedef);
}
Nan::Persistent<FunctionTemplate> DTraceProbe::constructor_template;
void DTraceProbe::Initialize(v8::Local<Object> target) {
Nan::HandleScope scope;
Local<FunctionTemplate> t = Nan::New<FunctionTemplate>(DTraceProbe::New);
t->InstanceTemplate()->SetInternalFieldCount(1);
t->SetClassName(Nan::New<String>("DTraceProbe").ToLocalChecked());
constructor_template.Reset(t);
Nan::SetPrototypeMethod(t, "fire", DTraceProbe::Fire);
target->Set(Nan::New<String>("DTraceProbe").ToLocalChecked(), t->GetFunction());
}
NAN_METHOD(DTraceProbe::New) {
Nan::HandleScope scope;
DTraceProbe *probe = new DTraceProbe();
probe->Wrap(info.This());
info.GetReturnValue().Set(info.This());
}
NAN_METHOD(DTraceProbe::Fire) {
Nan::HandleScope scope;
DTraceProbe *pd = Nan::ObjectWrap::Unwrap<DTraceProbe>(info.Holder());
info.GetReturnValue().Set(pd->_fire(info[0]));
}
v8::Local<Value> DTraceProbe::_fire(v8::Local<v8::Value> argsfn) {
Nan::HandleScope scope;
if (usdt_is_enabled(this->probedef->probe) == 0) {
return Nan::Undefined();
}
// invoke fire callback
Nan::TryCatch try_catch;
if (!argsfn->IsFunction()) {
Nan::ThrowError("Must give probe value callback as argument");
return Nan::Undefined();
}
Local<Function> cb = Local<Function>::Cast(argsfn);
Local<Value> probe_args = cb->Call(this->handle(), 0, NULL);
// exception in args callback?
if (try_catch.HasCaught()) {
Nan::FatalException(try_catch);
return Nan::Undefined();
}
// check return
if (!probe_args->IsArray()) {
return Nan::Undefined();
}
Local<Array> a = Local<Array>::Cast(probe_args);
void *argv[USDT_ARG_MAX];
// convert each argument value
for (size_t i = 0; i < argc; i++) {
argv[i] = this->arguments[i]->ArgumentValue(a->Get(i));
}
// finally fire the probe
usdt_fire_probe(this->probedef->probe, argc, argv);
// free argument values
for (size_t i = 0; i < argc; i++) {
this->arguments[i]->FreeArgument(argv[i]);
}
return Nan::True();
}
} // namespace node
|