#include <iostream>
#include <libplatform/libplatform.h>
#include <ostream>
#include <v8.h>
void Getter(v8::Local<v8::Name> key,
const v8::PropertyCallbackInfo<v8::Value> &info) {
std::cout << "Getter:" << std::endl;
std::cout << "IsFunction: " << key->IsFunction() << std::endl;
}
void Setter(v8::Local<v8::Name> key, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<v8::Value> &info) {
std::cout << "Setter:" << std::endl;
// key->IsFunction() returns false, expected to be true
std::cout << "Key IsFunction: " << key->IsFunction() << std::endl;
// value->IsFunction() returns true, as expected
std::cout << "\nValue IsFunction:" << value->IsFunction() << std::endl;
}
void Deleter(v8::Local<v8::Name> key,
const v8::PropertyCallbackInfo<v8::Boolean> &info) {}
bool InstallMap(v8::Isolate *isolate, const v8::Local<v8::Context> &context) {
v8::HandleScope handle_scope(isolate);
auto obj_template = v8::ObjectTemplate::New(isolate);
obj_template->SetHandler(
v8::NamedPropertyHandlerConfiguration(Getter, Setter, nullptr, Deleter));
v8::Local<v8::Object> obj;
if (!obj_template->NewInstance(context).ToLocal(&obj)) {
std::cerr << "Unable to instantiate object template" << std::endl;
return false;
}
v8::Local<v8::String> obj_name;
if (!v8::String::NewFromUtf8(isolate, "dict", v8::String::kNormalString)
->ToString(context)
.ToLocal(&obj_name)) {
std::cerr << "Unable to create map key name" << std::endl;
return false;
}
auto global = context->Global();
if (!global->Set(context, obj_name, obj).FromJust()) {
std::cerr << "Unable to install object into global scope" << std::endl;
return false;
}
return true;
}
int main(int argc, char *argv[]) {
const auto script_str = R"(
function x() {
}
dict[x] = x;
)";
v8::V8::InitializeICUDefaultLocation(argv[0]);
v8::V8::InitializeExternalStartupData(argv[0]);
auto platform = v8::platform::NewDefaultPlatform();
v8::V8::InitializePlatform(platform.get());
v8::V8::Initialize();
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator =
v8::ArrayBuffer::Allocator::NewDefaultAllocator();
v8::Isolate *isolate = v8::Isolate::New(create_params);
{
v8::Isolate::Scope isolate_scope(isolate);
v8::HandleScope handle_scope(isolate);
auto context = v8::Context::New(isolate);
v8::Context::Scope context_scope(context);
InstallMap(isolate, context);
auto source =
v8::String::NewFromUtf8(isolate, script_str, v8::NewStringType::kNormal)
.ToLocalChecked();
auto script = v8::Script::Compile(context, source).ToLocalChecked();
auto result = script->Run(context).ToLocalChecked();
v8::String::Utf8Value result_utf8(isolate, result);
std::cout << *result_utf8 << std::endl;
}
isolate->Dispose();
v8::V8::Dispose();
v8::V8::ShutdownPlatform();
delete create_params.array_buffer_allocator;
return 0;
}