Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
cannot get class defined as EC6 class as value with v8
/*
In my case, compile with:
/usr/bin/c++ --std=c++14 -I${PWD}/fmt -I${PWD}/include testcase3.cpp -o testcase -L$PWD/lib $PWD/lib/libv8_base.a -lv8_libbase $PWD/lib/libv8_nosnapshot.a -lv8_libplatform $PWD/lib/libv8_libsampler.a -licui18n -licuuc -Wl,-rpath,$PWD/lib -pthread
Run with ./testcase
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/libplatform/libplatform.h"
#include "include/v8.h"
#include <iostream>
#include <algorithm>
#include "v8helpers.h"
#include "v8helpers.cpp"
using namespace v8;
int main(int argc, char* argv[]) {
// Initialize V8.
V8::InitializeICUDefaultLocation(argv[0]);
V8::InitializeExternalStartupData(argv[0]);
Platform* platform = platform::CreateDefaultPlatform();
V8::InitializePlatform(platform);
V8::Initialize();
// Create a new Isolate and make it the current one.
Isolate::CreateParams create_params;
create_params.array_buffer_allocator = ArrayBuffer::Allocator::NewDefaultAllocator();
Isolate* isolate = Isolate::New(create_params);
{
Isolate::Scope isolate_scope(isolate);
HandleScope handle_scope(isolate);
Local<Context> context = Context::New(isolate);
Context::Scope context_scope(context);
std::string some_js(R"(
function foo() {
//impl
}
class some_class {
constructor() {
//impl
}
};
function bar() {
//impl
}
)");
// Compile and run above script
Local<String> source = String::NewFromUtf8(isolate, some_js.c_str(), NewStringType::kNormal).ToLocalChecked();
Local<Script> script = Script::Compile(context, source).ToLocalChecked();
Local<Value> result = script->Run(context).ToLocalChecked();
Local<String> foo = String::NewFromUtf8(isolate, "foo", NewStringType::kNormal).ToLocalChecked();
Local<String> some_class = String::NewFromUtf8(isolate, "some_class", NewStringType::kNormal).ToLocalChecked();
Local<String> bar = String::NewFromUtf8(isolate, "bar", NewStringType::kNormal).ToLocalChecked();
Handle<Value> value_foo = context->Global()->Get(foo);
Handle<Value> value_some_class = context->Global()->Get(some_class);
Handle<Value> value_bar = context->Global()->Get(bar);
std::cout << v8toolkit::stringify_value(isolate, value_foo, true, true) << std::endl;
std::cout << v8toolkit::stringify_value(isolate, value_some_class, true, true) << std::endl;
std::cout << v8toolkit::stringify_value(isolate, value_bar, true, true) << std::endl;
/* Program output=
* --------------------
* function foo() {
* //impl
* }
* undefined
* function bar() {
* //impl
* }
* --------------------
*/
}
isolate->Dispose();
V8::Dispose();
V8::ShutdownPlatform();
delete platform;
delete create_params.array_buffer_allocator;
return 0;
}
@rayburgemeestre

This comment has been minimized.

Copy link
Owner Author

commented Jan 12, 2017

This example is compile-able in pure V8, and runnable, by commenting out the two includes for "v8toolkit.{h|cpp}", and the three lines outputting v8toolkit::stringify_value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.