Skip to content

Commit 0424be9

Browse files
committed
wasi: use WasmMemoryObject handle for perf
1 parent 6f924ac commit 0424be9

File tree

3 files changed

+10
-31
lines changed

3 files changed

+10
-31
lines changed

lib/wasi.js

-16
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@ const {
1010
} = primordials;
1111

1212
const {
13-
ERR_INVALID_ARG_TYPE,
1413
ERR_WASI_ALREADY_STARTED
1514
} = require('internal/errors').codes;
1615
const {
1716
emitExperimentalWarning,
1817
kEmptyObject,
1918
} = require('internal/util');
20-
const { isArrayBuffer } = require('internal/util/types');
2119
const {
2220
validateArray,
2321
validateBoolean,
@@ -39,20 +37,6 @@ function setupInstance(self, instance) {
3937
validateObject(instance, 'instance');
4038
validateObject(instance.exports, 'instance.exports');
4139

42-
// WASI::_SetMemory() in src/node_wasi.cc only expects that |memory| is
43-
// an object. It will try to look up the .buffer property when needed
44-
// and fail with UVWASI_EINVAL when the property is missing or is not
45-
// an ArrayBuffer. Long story short, we don't need much validation here
46-
// but we type-check anyway because it helps catch bugs in the user's
47-
// code early.
48-
validateObject(instance.exports.memory, 'instance.exports.memory');
49-
if (!isArrayBuffer(instance.exports.memory.buffer)) {
50-
throw new ERR_INVALID_ARG_TYPE(
51-
'instance.exports.memory.buffer',
52-
['WebAssembly.Memory'],
53-
instance.exports.memory.buffer);
54-
}
55-
5640
self[kInstance] = instance;
5741
self[kSetMemory](instance.exports.memory);
5842
}

src/node_wasi.cc

+9-14
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ using v8::Object;
8989
using v8::String;
9090
using v8::Uint32;
9191
using v8::Value;
92-
92+
using v8::WasmMemoryObject;
9393

9494
static MaybeLocal<Value> WASIException(Local<Context> context,
9595
int errorno,
@@ -1643,25 +1643,20 @@ void WASI::SockShutdown(const FunctionCallbackInfo<Value>& args) {
16431643
void WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) {
16441644
WASI* wasi;
16451645
CHECK_EQ(args.Length(), 1);
1646-
CHECK(args[0]->IsObject());
1646+
if (!args[0]->IsWasmMemoryObject()) {
1647+
return node::THROW_ERR_INVALID_ARG_TYPE(
1648+
env, "instance.exports.memory must be a WebAssembly.Memory object");
1649+
}
16471650
ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This());
1648-
wasi->memory_.Reset(wasi->env()->isolate(), args[0].As<Object>());
1651+
wasi->memory_.Reset(wasi->env()->isolate(), args[0].As<WasmMemoryObject>());
16491652
}
16501653

16511654

16521655
uvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) {
16531656
Environment* env = this->env();
1654-
Local<Object> memory = PersistentToLocal::Strong(this->memory_);
1655-
Local<Value> prop;
1656-
1657-
if (!memory->Get(env->context(), env->buffer_string()).ToLocal(&prop))
1658-
return UVWASI_EINVAL;
1659-
1660-
if (!prop->IsArrayBuffer())
1661-
return UVWASI_EINVAL;
1662-
1663-
Local<ArrayBuffer> ab = prop.As<ArrayBuffer>();
1664-
std::shared_ptr<BackingStore> backing_store = ab->GetBackingStore();
1657+
Local<WasmMemoryObject> memory = PersistentToLocal::Strong(this->memory_);
1658+
std::shared_ptr<BackingStore> backing_store =
1659+
memory->Buffer()->GetBackingStore();
16651660
*byte_length = backing_store->ByteLength();
16661661
*store = static_cast<char*>(backing_store->Data());
16671662
CHECK_NOT_NULL(*store);

src/node_wasi.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class WASI : public BaseObject,
9494
inline void writeUInt64(char* memory, uint64_t value, uint32_t offset);
9595
uvwasi_errno_t backingStore(char** store, size_t* byte_length);
9696
uvwasi_t uvw_;
97-
v8::Global<v8::Object> memory_;
97+
v8::Global<v8::WasmMemoryObject> memory_;
9898
uvwasi_mem_t alloc_info_;
9999
size_t current_uvwasi_memory_ = 0;
100100
};

0 commit comments

Comments
 (0)