| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,14 +10,12 @@ const { | |||
| 10 | 10 | } = primordials; | |
| 11 | 11 | ||
| 12 | 12 | const { | |
| 13 | - ERR_INVALID_ARG_TYPE, | ||
| 14 | 13 | ERR_WASI_ALREADY_STARTED | |
| 15 | 14 | } = require('internal/errors').codes; | |
| 16 | 15 | const { | |
| 17 | 16 | emitExperimentalWarning, | |
| 18 | 17 | kEmptyObject, | |
| 19 | 18 | } = require('internal/util'); | |
| 20 | - const { isArrayBuffer } = require('internal/util/types'); | ||
| 21 | 19 | const { | |
| 22 | 20 | validateArray, | |
| 23 | 21 | validateBoolean, | |
@@ -39,20 +37,6 @@ function setupInstance(self, instance) { | |||
| 39 | 37 | validateObject(instance, 'instance'); | |
| 40 | 38 | validateObject(instance.exports, 'instance.exports'); | |
| 41 | 39 | ||
| 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 | - | ||
| 56 | 40 | self[kInstance] = instance; | |
| 57 | 41 | self[kSetMemory](instance.exports.memory); | |
| 58 | 42 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -72,9 +72,7 @@ inline void Debug(WASI* wasi, Args&&... args) { | |||
| 72 | 72 | } \ | |
| 73 | 73 | } while (0) | |
| 74 | 74 | ||
| 75 | - | ||
| 76 | 75 | using v8::Array; | |
| 77 | - using v8::ArrayBuffer; | ||
| 78 | 76 | using v8::BackingStore; | |
| 79 | 77 | using v8::BigInt; | |
| 80 | 78 | using v8::Context; | |
@@ -89,7 +87,7 @@ using v8::Object; | |||
| 89 | 87 | using v8::String; | |
| 90 | 88 | using v8::Uint32; | |
| 91 | 89 | using v8::Value; | |
| 92 | - | ||
| 90 | + using v8::WasmMemoryObject; | ||
| 93 | 91 | ||
| 94 | 92 | static MaybeLocal<Value> WASIException(Local<Context> context, | |
| 95 | 93 | int errorno, | |
@@ -1642,26 +1640,22 @@ void WASI::SockShutdown(const FunctionCallbackInfo<Value>& args) { | |||
| 1642 | 1640 | ||
| 1643 | 1641 | void WASI::_SetMemory(const FunctionCallbackInfo<Value>& args) { | |
| 1644 | 1642 | WASI* wasi; | |
| 1645 | - CHECK_EQ(args.Length(), 1); | ||
| 1646 | - CHECK(args[0]->IsObject()); | ||
| 1647 | 1643 | ASSIGN_OR_RETURN_UNWRAP(&wasi, args.This()); | |
| 1648 | - wasi->memory_.Reset(wasi->env()->isolate(), args[0].As<Object>()); | ||
| 1644 | + CHECK_EQ(args.Length(), 1); | ||
| 1645 | + if (!args[0]->IsWasmMemoryObject()) { | ||
| 1646 | + return node::THROW_ERR_INVALID_ARG_TYPE( | ||
| 1647 | + wasi->env(), | ||
| 1648 | + "\"instance.exports.memory\" property must be a WebAssembly.Memory " | ||
| 1649 | + "object"); | ||
| 1650 | + } | ||
| 1651 | + wasi->memory_.Reset(wasi->env()->isolate(), args[0].As<WasmMemoryObject>()); | ||
| 1649 | 1652 | } | |
| 1650 | 1653 | ||
| 1651 | 1654 | ||
| 1652 | 1655 | uvwasi_errno_t WASI::backingStore(char** store, size_t* byte_length) { | |
| 1653 | - 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(); | ||
| 1656 | + Local<WasmMemoryObject> memory = PersistentToLocal::Strong(this->memory_); | ||
| 1657 | + std::shared_ptr<BackingStore> backing_store = | ||
| 1658 | + memory->Buffer()->GetBackingStore(); | ||
| 1665 | 1659 | *byte_length = backing_store->ByteLength(); | |
| 1666 | 1660 | *store = static_cast<char*>(backing_store->Data()); | |
| 1667 | 1661 | CHECK_NOT_NULL(*store); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -94,7 +94,7 @@ class WASI : public BaseObject, | |||
| 94 | 94 | inline void writeUInt64(char* memory, uint64_t value, uint32_t offset); | |
| 95 | 95 | uvwasi_errno_t backingStore(char** store, size_t* byte_length); | |
| 96 | 96 | uvwasi_t uvw_; | |
| 97 | - v8::Global<v8::Object> memory_; | ||
| 97 | + v8::Global<v8::WasmMemoryObject> memory_; | ||
| 98 | 98 | uvwasi_mem_t alloc_info_; | |
| 99 | 99 | size_t current_uvwasi_memory_ = 0; | |
| 100 | 100 | }; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,7 +47,10 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 47 | 47 | ||
| 48 | 48 | Object.defineProperty(instance, 'exports', { | |
| 49 | 49 | get() { | |
| 50 | - return { _initialize: 5, memory: new Uint8Array() }; | ||
| 50 | + return { | ||
| 51 | + _initialize: 5, | ||
| 52 | + memory: new WebAssembly.Memory({ initial: 1 }), | ||
| 53 | + }; | ||
| 51 | 54 | }, | |
| 52 | 55 | }); | |
| 53 | 56 | assert.throws( | |
@@ -70,7 +73,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 70 | 73 | return { | |
| 71 | 74 | _start() {}, | |
| 72 | 75 | _initialize() {}, | |
| 73 | - memory: new Uint8Array(), | ||
| 76 | + memory: new WebAssembly.Memory({ initial: 1 }), | ||
| 74 | 77 | }; | |
| 75 | 78 | } | |
| 76 | 79 | }); | |
@@ -97,55 +100,11 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 97 | 100 | () => { wasi.initialize(instance); }, | |
| 98 | 101 | { | |
| 99 | 102 | code: 'ERR_INVALID_ARG_TYPE', | |
| 100 | - message: /"instance\.exports\.memory" property must be of type object/ | ||
| 103 | + message: /"instance\.exports\.memory" property must be a WebAssembly\.Memory object/ | ||
| 101 | 104 | } | |
| 102 | 105 | ); | |
| 103 | 106 | } | |
| 104 | 107 | ||
| 105 | - { | ||
| 106 | - // Verify that a non-ArrayBuffer memory.buffer is rejected. | ||
| 107 | - const wasi = new WASI({}); | ||
| 108 | - const wasm = await WebAssembly.compile(bufferSource); | ||
| 109 | - const instance = await WebAssembly.instantiate(wasm); | ||
| 110 | - | ||
| 111 | - Object.defineProperty(instance, 'exports', { | ||
| 112 | - get() { | ||
| 113 | - return { | ||
| 114 | - _initialize() {}, | ||
| 115 | - memory: {}, | ||
| 116 | - }; | ||
| 117 | - } | ||
| 118 | - }); | ||
| 119 | - // The error message is a little white lie because any object | ||
| 120 | - // with a .buffer property of type ArrayBuffer is accepted, | ||
| 121 | - // but 99% of the time a WebAssembly.Memory object is used. | ||
| 122 | - assert.throws( | ||
| 123 | - () => { wasi.initialize(instance); }, | ||
| 124 | - { | ||
| 125 | - code: 'ERR_INVALID_ARG_TYPE', | ||
| 126 | - message: /"instance\.exports\.memory\.buffer" property must be an WebAssembly\.Memory/ | ||
| 127 | - } | ||
| 128 | - ); | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - { | ||
| 132 | - // Verify that an argument that duck-types as a WebAssembly.Instance | ||
| 133 | - // is accepted. | ||
| 134 | - const wasi = new WASI({}); | ||
| 135 | - const wasm = await WebAssembly.compile(bufferSource); | ||
| 136 | - const instance = await WebAssembly.instantiate(wasm); | ||
| 137 | - | ||
| 138 | - Object.defineProperty(instance, 'exports', { | ||
| 139 | - get() { | ||
| 140 | - return { | ||
| 141 | - _initialize() {}, | ||
| 142 | - memory: { buffer: new ArrayBuffer(0) }, | ||
| 143 | - }; | ||
| 144 | - } | ||
| 145 | - }); | ||
| 146 | - wasi.initialize(instance); | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | 108 | { | |
| 150 | 109 | // Verify that a WebAssembly.Instance from another VM context is accepted. | |
| 151 | 110 | const wasi = new WASI({}); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -47,7 +47,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 47 | 47 | ||
| 48 | 48 | Object.defineProperty(instance, 'exports', { | |
| 49 | 49 | get() { | |
| 50 | - return { memory: new Uint8Array() }; | ||
| 50 | + return { memory: new WebAssembly.Memory({ initial: 1 }) }; | ||
| 51 | 51 | }, | |
| 52 | 52 | }); | |
| 53 | 53 | assert.throws( | |
@@ -70,7 +70,7 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 70 | 70 | return { | |
| 71 | 71 | _start() {}, | |
| 72 | 72 | _initialize() {}, | |
| 73 | - memory: new Uint8Array(), | ||
| 73 | + memory: new WebAssembly.Memory({ initial: 1 }), | ||
| 74 | 74 | }; | |
| 75 | 75 | } | |
| 76 | 76 | }); | |
@@ -97,55 +97,11 @@ const bufferSource = fixtures.readSync('simple.wasm'); | |||
| 97 | 97 | () => { wasi.start(instance); }, | |
| 98 | 98 | { | |
| 99 | 99 | code: 'ERR_INVALID_ARG_TYPE', | |
| 100 | - message: /"instance\.exports\.memory" property must be of type object/ | ||
| 100 | + message: /"instance\.exports\.memory" property must be a WebAssembly\.Memory object/ | ||
| 101 | 101 | } | |
| 102 | 102 | ); | |
| 103 | 103 | } | |
| 104 | 104 | ||
| 105 | - { | ||
| 106 | - // Verify that a non-ArrayBuffer memory.buffer is rejected. | ||
| 107 | - const wasi = new WASI({}); | ||
| 108 | - const wasm = await WebAssembly.compile(bufferSource); | ||
| 109 | - const instance = await WebAssembly.instantiate(wasm); | ||
| 110 | - | ||
| 111 | - Object.defineProperty(instance, 'exports', { | ||
| 112 | - get() { | ||
| 113 | - return { | ||
| 114 | - _start() {}, | ||
| 115 | - memory: {}, | ||
| 116 | - }; | ||
| 117 | - } | ||
| 118 | - }); | ||
| 119 | - // The error message is a little white lie because any object | ||
| 120 | - // with a .buffer property of type ArrayBuffer is accepted, | ||
| 121 | - // but 99% of the time a WebAssembly.Memory object is used. | ||
| 122 | - assert.throws( | ||
| 123 | - () => { wasi.start(instance); }, | ||
| 124 | - { | ||
| 125 | - code: 'ERR_INVALID_ARG_TYPE', | ||
| 126 | - message: /"instance\.exports\.memory\.buffer" property must be an WebAssembly\.Memory/ | ||
| 127 | - } | ||
| 128 | - ); | ||
| 129 | - } | ||
| 130 | - | ||
| 131 | - { | ||
| 132 | - // Verify that an argument that duck-types as a WebAssembly.Instance | ||
| 133 | - // is accepted. | ||
| 134 | - const wasi = new WASI({}); | ||
| 135 | - const wasm = await WebAssembly.compile(bufferSource); | ||
| 136 | - const instance = await WebAssembly.instantiate(wasm); | ||
| 137 | - | ||
| 138 | - Object.defineProperty(instance, 'exports', { | ||
| 139 | - get() { | ||
| 140 | - return { | ||
| 141 | - _start() {}, | ||
| 142 | - memory: { buffer: new ArrayBuffer(0) }, | ||
| 143 | - }; | ||
| 144 | - } | ||
| 145 | - }); | ||
| 146 | - wasi.start(instance); | ||
| 147 | - } | ||
| 148 | - | ||
| 149 | 105 | { | |
| 150 | 106 | // Verify that a WebAssembly.Instance from another VM context is accepted. | |
| 151 | 107 | const wasi = new WASI({}); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments