Created
September 29, 2019 18:47
-
-
Save jeroen/c5303beb63e478330507c17c66a4632f to your computer and use it in GitHub Desktop.
Run wasm in V8 version 7.7
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <v8.h> | |
#include <libplatform/libplatform.h> | |
using v8::HandleScope; | |
using v8::Isolate; | |
using v8::MemorySpan; | |
using v8::WasmModuleObject; | |
using v8::Context; | |
using v8::Local; | |
using v8::Value; | |
using v8::String; | |
using v8::Object; | |
using v8::Function; | |
using v8::Int32; | |
using args_type = Local<Value>[]; | |
int main(int argc, char* argv[]) { | |
v8::V8::InitializeICUDefaultLocation(argv[0]); | |
v8::V8::InitializeExternalStartupData(argv[0]); | |
std::unique_ptr<v8::Platform> platform = v8::platform::NewDefaultPlatform(); | |
v8::V8::InitializePlatform(platform.get()); | |
v8::V8::Initialize(); | |
Isolate::CreateParams create_params; | |
create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); | |
Isolate* isolate = Isolate::New(create_params); | |
Isolate::Scope isolate_scope(isolate); | |
HandleScope scope(isolate); | |
Local<Context> context = Context::New(isolate); | |
Context::Scope context_scope(context); | |
//WasmModuleObjectBuilderStreaming stream(isolate); | |
// Use the v8 API to generate a WebAssembly module. | |
// | |
// |bytes| contains the binary format for the following module: // | |
// (func (export "add") (param i32 i32) (result i32) | |
// get_local 0 | |
// get_local 1 | |
// i32.add) | |
// | |
// taken from: https://github.com/v8/v8/blob/master/samples/hello-world.cc#L66 | |
std::vector<uint8_t> wasmbin { | |
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x07, 0x01, | |
0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x03, 0x02, 0x01, 0x00, 0x07, | |
0x07, 0x01, 0x03, 0x61, 0x64, 0x64, 0x00, 0x00, 0x0a, 0x09, 0x01, | |
0x07, 0x00, 0x20, 0x00, 0x20, 0x01, 0x6a, 0x0b | |
}; | |
// same as calling: | |
// let module = new WebAssembly.Module(bytes); | |
Local<WasmModuleObject> module = WasmModuleObject::DeserializeOrCompile(isolate, | |
MemorySpan<const uint8_t>(0, 0), | |
MemorySpan<const uint8_t>(wasmbin.data(), wasmbin.size()) | |
).ToLocalChecked(); | |
// same as calling: | |
// let module_instance_exports = new WebAssembly.Instance(module).exports; | |
args_type instance_args{module}; | |
Local<Object> module_instance_exports = context->Global() | |
->Get(context, String::NewFromUtf8(isolate, "WebAssembly").ToLocalChecked()) | |
.ToLocalChecked().As<Object>() | |
->Get(context, String::NewFromUtf8(isolate, "Instance").ToLocalChecked()) | |
.ToLocalChecked().As<Object>() | |
->CallAsConstructor(context, 1, instance_args) | |
.ToLocalChecked().As<Object>() | |
->Get(context, String::NewFromUtf8(isolate, "exports").ToLocalChecked()) | |
.ToLocalChecked().As<Object>(); | |
// same as calling: | |
// module_instance_exports.add(77, 88) | |
args_type add_args{Int32::New(isolate, 77), Int32::New(isolate, 88)}; | |
Local<Int32> adder_res = module_instance_exports | |
->Get(context, String::NewFromUtf8(isolate, "add").ToLocalChecked()) | |
.ToLocalChecked().As<Function>() | |
->Call(context, context->Global(), 2, add_args) | |
.ToLocalChecked().As<Int32>(); | |
printf("77 + 88 = %d\n", adder_res->Value()); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment