Commit 0adcb82a authored by Andreas Haas's avatar Andreas Haas Committed by Commit Bot

[wasm][anyref] Allow export of mutable anyref globals

R=titzer@chromium.org

Bug: v8:7581
Change-Id: I4725eada889cc8cf6a3ca537f05b1da2f5e83f2b
Reviewed-on: https://chromium-review.googlesource.com/c/1400413
Commit-Queue: Andreas Haas <ahaas@chromium.org>
Reviewed-by: 's avatarBen Titzer <titzer@chromium.org>
Cr-Commit-Position: refs/heads/master@{#58632}
parent 7164251a
......@@ -2077,6 +2077,7 @@ void InstanceBuilder::ProcessExports(Handle<WasmInstanceObject> instance) {
const WasmGlobal& global = module_->globals[exp.index];
if (enabled_.mut_global) {
Handle<JSArrayBuffer> untagged_buffer;
Handle<FixedArray> tagged_buffer;
uint32_t offset;
if (global.mutability && global.imported) {
......@@ -2093,18 +2094,22 @@ void InstanceBuilder::ProcessExports(Handle<WasmInstanceObject> instance) {
CHECK(global_addr >= backing_store &&
global_addr < backing_store + buffer_size);
offset = static_cast<uint32_t>(global_addr - backing_store);
} else {
if (global.type == kWasmAnyRef) {
tagged_buffer =
handle(instance->tagged_globals_buffer(), isolate_);
} else {
untagged_buffer =
handle(instance->untagged_globals_buffer(), isolate_);
}
offset = global.offset;
}
// Since the global's array untagged_buffer is always provided,
// allocation should never fail.
Handle<WasmGlobalObject> global_obj =
WasmGlobalObject::New(isolate_, untagged_buffer,
MaybeHandle<FixedArray>(), global.type,
offset, global.mutability)
WasmGlobalObject::New(isolate_, untagged_buffer, tagged_buffer,
global.type, offset, global.mutability)
.ToHandleChecked();
desc.set_value(global_obj);
} else {
......
......@@ -1106,6 +1106,7 @@ MaybeHandle<WasmGlobalObject> WasmGlobalObject::New(
isolate->factory()->NewJSObject(global_ctor));
if (type == wasm::kWasmAnyRef) {
DCHECK(maybe_untagged_buffer.is_null());
Handle<FixedArray> tagged_buffer;
if (!maybe_tagged_buffer.ToHandle(&tagged_buffer)) {
// If no buffer was provided, create one.
......@@ -1114,6 +1115,7 @@ MaybeHandle<WasmGlobalObject> WasmGlobalObject::New(
}
global_obj->set_tagged_buffer(*tagged_buffer);
} else {
DCHECK(maybe_tagged_buffer.is_null());
Handle<JSArrayBuffer> untagged_buffer;
uint32_t type_size = wasm::ValueTypes::ElementSizeInBytes(type);
if (!maybe_untagged_buffer.ToHandle(&untagged_buffer)) {
......
......@@ -170,3 +170,27 @@ load("test/mjsunit/wasm/wasm-module-builder.js");
TestGlobal({a: 11});
TestGlobal(print);
})();
(function TestExportImmutableAnyRefGlobal() {
print(arguments.callee.name);
let builder = new WasmModuleBuilder();
const g1 = builder.addGlobal(kWasmAnyRef, true).exportAs("global1");
builder.addGlobal(kWasmI32, true); // Dummy.
builder.addGlobal(kWasmAnyRef, true); // Dummy.
const g2 = builder.addGlobal(kWasmAnyRef, true).exportAs("global2");
builder.addFunction("main", kSig_v_rr)
.addBody([
kExprGetLocal, 0,
kExprSetGlobal, g1.index,
kExprGetLocal, 1,
kExprSetGlobal, g2.index
])
.exportAs("main");
const instance = builder.instantiate();
const obj1 = {x: 221};
const obj2 = print;
instance.exports.main(obj1, obj2);
assertSame(obj1, instance.exports.global1.value);
assertSame(obj2, instance.exports.global2.value);
})();
......@@ -146,6 +146,7 @@ let kSig_d_d = makeSig([kWasmF64], [kWasmF64]);
let kSig_r_r = makeSig([kWasmAnyRef], [kWasmAnyRef]);
let kSig_i_r = makeSig([kWasmAnyRef], [kWasmI32]);
let kSig_v_r = makeSig([kWasmAnyRef], []);
let kSig_v_rr = makeSig([kWasmAnyRef, kWasmAnyRef], []);
let kSig_r_v = makeSig([], [kWasmAnyRef]);
function makeSig(params, results) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment