Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim

vdeturckheim/cb.cc

Last active Aug 20, 2020
Embed
What would you like to do?
#include <stdlib.h>
#include <nan.h>
#include <v8.h>
#include <iostream>
using namespace v8;
using namespace std;
static string *get_string_from_item(Local<Context> context, Local<Value> item) {
if (!item->IsString()) {
return nullptr;
}
MaybeLocal<String> maybe_str = item->ToString(context);
if (maybe_str.IsEmpty()) {
return nullptr;
}
v8::String::Utf8Value utf8str(context->GetIsolate(), item);
return new string(*utf8str);
}
static v8::ModifyCodeGenerationFromStringsResult
CodeGenerationCheckCallback(Local<Context> context, Local<Value> source) {
std::cout << "calling callback" << "\n";
string *str = get_string_from_item(context, source);
MaybeLocal<String> maybe_str = source->ToString(context);
std::cout << str << "\n";
return {true, maybe_str};
}
static void setCodeGenerationCheckCallback(const Nan::FunctionCallbackInfo<v8::Value>& info) {
Isolate *isolate = info.GetIsolate();
std::cout << "setting callback" << "\n";
isolate->SetModifyCodeGenerationFromStringsCallback(&CodeGenerationCheckCallback);
std::cout << "" << "callback set" << "\n";
}
static NAN_MODULE_INIT(Init) {
Nan::Set(target,
Nan::New<String>("enableCodeGenerationCheckCallback").ToLocalChecked(),
Nan::GetFunction(Nan::New<FunctionTemplate>(setCodeGenerationCheckCallback)).ToLocalChecked()
);
}
NODE_MODULE(addon, Init)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment