diff --git a/lib/wasi.js b/lib/wasi.js index 7093953a00739a..2a0770bf6dbbb0 100644 --- a/lib/wasi.js +++ b/lib/wasi.js @@ -10,14 +10,12 @@ const { } = primordials; const { - ERR_INVALID_ARG_TYPE, ERR_WASI_ALREADY_STARTED } = require('internal/errors').codes; const { emitExperimentalWarning, kEmptyObject, } = require('internal/util'); -const { isArrayBuffer } = require('internal/util/types'); const { validateArray, validateBoolean, @@ -39,20 +37,6 @@ function setupInstance(self, instance) { validateObject(instance, 'instance'); validateObject(instance.exports, 'instance.exports'); - // WASI::_SetMemory() in src/node_wasi.cc only expects that |memory| is - // an object. It will try to look up the .buffer property when needed - // and fail with UVWASI_EINVAL when the property is missing or is not - // an ArrayBuffer. Long story short, we don't need much validation here - // but we type-check anyway because it helps catch bugs in the user's - // code early. - validateObject(instance.exports.memory, 'instance.exports.memory'); - if (!isArrayBuffer(instance.exports.memory.buffer)) { - throw new ERR_INVALID_ARG_TYPE( - 'instance.exports.memory.buffer', - ['WebAssembly.Memory'], - instance.exports.memory.buffer); - } - self[kInstance] = instance; self[kSetMemory](instance.exports.memory); } diff --git a/src/node_wasi.cc b/src/node_wasi.cc index 803edf0c91dda1..94cb9ddfc20a4d 100644 --- a/src/node_wasi.cc +++ b/src/node_wasi.cc @@ -89,7 +89,7 @@ using v8::Object; using v8::String; using v8::Uint32; using v8::Value; - +using v8::WasmMemoryObject; static MaybeLocal WASIException(Local context, int errorno, @@ -1643,25 +1643,20 @@ void WASI::SockShutdown(const FunctionCallbackInfo& args) { void WASI::_SetMemory(const FunctionCallbackInfo& args) { WASI* wasi; CHECK_EQ(args.Length(), 1); - CHECK(args[0]->IsObject()); + if (!args[0]->IsWasmMemoryObject()) { + return node::THROW_ERR_INVALID_ARG_TYPE( + env, "instance.exports.memory must be a WebAssembly.Memory object"); + } ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This()); - wasi->memory_.Reset(wasi->env()->isolate(), args[0].As()); + wasi->memory_.Reset(wasi->env()->isolate(), args[0].As()); } uvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) { Environment* env = this->env(); - Local memory = PersistentToLocal::Strong(this->memory_); - Local prop; - - if (!memory->Get(env->context(), env->buffer_string()).ToLocal(&prop)) - return UVWASI_EINVAL; - - if (!prop->IsArrayBuffer()) - return UVWASI_EINVAL; - - Local ab = prop.As(); - std::shared_ptr backing_store = ab->GetBackingStore(); + Local memory = PersistentToLocal::Strong(this->memory_); + std::shared_ptr backing_store = + memory->Buffer()->GetBackingStore(); *byte_length = backing_store->ByteLength(); *store = static_cast(backing_store->Data()); CHECK_NOT_NULL(*store); diff --git a/src/node_wasi.h b/src/node_wasi.h index 7a0be60aa645a7..b3814ddc31033a 100644 --- a/src/node_wasi.h +++ b/src/node_wasi.h @@ -94,7 +94,7 @@ class WASI : public BaseObject, inline void writeUInt64(char* memory, uint64_t value, uint32_t offset); uvwasi_errno_t backingStore(char** store, size_t* byte_length); uvwasi_t uvw_; - v8::Global memory_; + v8::Global memory_; uvwasi_mem_t alloc_info_; size_t current_uvwasi_memory_ = 0; };