#include "dtrace_provider.h" #include namespace node { using namespace v8; // Integer Argument #ifdef __x86_64__ # define INTMETHOD int64_t #else # define INTMETHOD int32_t #endif void * DTraceIntegerArgument::ArgumentValue(v8::Local value) { if (value->IsUndefined()) return 0; else return (void *)(long) Nan::To(value).FromJust(); } void DTraceIntegerArgument::FreeArgument(void *arg) { } const char * DTraceIntegerArgument::Type() { return "int"; } // String Argument void * DTraceStringArgument::ArgumentValue(v8::Local value) { if (value->IsUndefined()) return (void *) strdup("undefined"); Nan::Utf8String str(value); return (void *) strdup(*str); } void DTraceStringArgument::FreeArgument(void *arg) { free(arg); } const char * DTraceStringArgument::Type() { return "char *"; } // JSON Argument DTraceJsonArgument::DTraceJsonArgument() { Nan::HandleScope scope; v8::Local context = Nan::GetCurrentContext(); v8::Local global = context->Global(); v8::Local json = Nan::New("JSON").ToLocalChecked(); v8::Local l_JSON = Nan::To(global->Get(json)).ToLocalChecked(); v8::Local l_JSON_stringify = v8::Local::Cast(l_JSON->Get(Nan::New("stringify").ToLocalChecked())); JSON.Reset(l_JSON); JSON_stringify.Reset(l_JSON_stringify); } DTraceJsonArgument::~DTraceJsonArgument() { JSON.Reset(); JSON_stringify.Reset(); } void * DTraceJsonArgument::ArgumentValue(v8::Local value) { Nan::HandleScope scope; if (value->IsUndefined()) return (void *) strdup("undefined"); v8::Local info[1]; info[0] = value; v8::Local cb = Nan::New(JSON_stringify); v8::Local obj = Nan::New(JSON); Local j = Nan::Call(cb, obj, 1, info).ToLocalChecked(); if (*j == NULL) return (void *) strdup("{ \"error\": \"stringify failed\" }"); Nan::Utf8String json(j); return (void *) strdup(*json); } void DTraceJsonArgument::FreeArgument(void *arg) { free(arg); } const char * DTraceJsonArgument::Type() { return "char *"; } } // namespace node