1
0
Fork 0
mirror of https://github.com/rubyjs/mini_racer synced 2023-03-27 23:21:28 -04:00

FEATURE: add wasm support

Previously we had no way of pumping the message loop, this meant that when
we instansiated wasm the promise would never resolve.

This demonstrates how to call wasm from Ruby using the new
context.isolate.pump_message_loop
This commit is contained in:
Sam Saffron 2020-06-29 13:25:50 +10:00
parent 23e0369720
commit 470602a762
No known key found for this signature in database
GPG key ID: B9606168D2FFD9F5
4 changed files with 60 additions and 1 deletions

View file

@ -779,6 +779,19 @@ static VALUE rb_isolate_low_memory_notification(VALUE self) {
return Qnil;
}
static VALUE rb_isolate_pump_message_loop(VALUE self) {
IsolateInfo* isolate_info;
Data_Get_Struct(self, IsolateInfo, isolate_info);
if (current_platform == NULL) return Qfalse;
if (platform::PumpMessageLoop(current_platform.get(), isolate_info->isolate)){
return Qtrue;
} else {
return Qfalse;
}
}
static VALUE rb_context_init_unsafe(VALUE self, VALUE isolate, VALUE snap) {
ContextInfo* context_info;
Data_Get_Struct(self, ContextInfo, context_info);
@ -1668,7 +1681,7 @@ extern "C" {
rb_define_method(rb_cIsolate, "idle_notification", (VALUE(*)(...))&rb_isolate_idle_notification, 1);
rb_define_method(rb_cIsolate, "low_memory_notification", (VALUE(*)(...))&rb_isolate_low_memory_notification, 0);
rb_define_method(rb_cIsolate, "pump_message_loop", (VALUE(*)(...))&rb_isolate_pump_message_loop, 0);
rb_define_private_method(rb_cIsolate, "init_with_snapshot",(VALUE(*)(...))&rb_isolate_init_with_snapshot, 1);
rb_define_singleton_method(rb_cPlatform, "set_flag_as_str!", (VALUE(*)(...))&rb_platform_set_flag_as_str, 1);

View file

@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rake", ">= 12.3.3"
spec.add_development_dependency "minitest", "~> 5.0"
spec.add_development_dependency "rake-compiler"
spec.add_development_dependency "m"
spec.add_dependency 'libv8', '> 7.3'
spec.require_paths = ["lib", "ext"]

View file

@ -873,4 +873,49 @@ raise FooError, "I like foos"
context.attach('myFunctionLogger', ->(property) { })
context.eval(js)
end
def test_promise
context = MiniRacer::Context.new()
context.eval <<~JS
var x = 0;
async function test() {
return 99;
}
test().then(v => x = v);
JS
context.isolate.run_microtasks
v = context.eval("x");
assert_equal(v, 99)
end
def test_webassembly
context = MiniRacer::Context.new()
context.eval("let instance = null;")
filename = File.expand_path("../support/add.wasm", __FILE__)
context.attach("loadwasm", proc {|f| File.read(filename).each_byte.to_a})
context.attach("print", proc {|f| puts f})
context.eval("function module() { print(arguments) }")
context.eval <<~JS
WebAssembly
.instantiate(new Uint8Array(loadwasm()), {
wasi_snapshot_preview1: {
proc_exit: function() { print("exit"); },
args_get: function() { return 0 },
args_sizes_get: function() { return 0 }
}
})
.then(i => { instance = i["instance"];})
.catch(e => print(e.toString()));
JS
while !context.eval("instance") do
context.isolate.pump_message_loop
end
assert_equal(3, context.eval("instance.exports.add(1,2)"))
end
end

BIN
test/support/add.wasm Normal file

Binary file not shown.