Skip to content

Instantly share code, notes, and snippets.

@vdeturckheim
Last active August 20, 2020 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vdeturckheim/2880eb0a393e28e6070d5c286ea894b7 to your computer and use it in GitHub Desktop.
Save vdeturckheim/2880eb0a393e28e6070d5c286ea894b7 to your computer and use it in GitHub Desktop.
#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