|
| 1 | +// Flags: --experimental-wasi-unstable-preview1 |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +constcommon=require('../common'); |
| 5 | +constassert=require('assert'); |
| 6 | +constvm=require('vm'); |
| 7 | +const{WASI}=require('wasi'); |
| 8 | + |
| 9 | +constfixtures=require('../common/fixtures'); |
| 10 | +constbufferSource=fixtures.readSync('simple.wasm'); |
| 11 | + |
| 12 | +(async()=>{ |
| 13 | +{ |
| 14 | +// Verify that a WebAssembly.Instance is passed in. |
| 15 | +constwasi=newWASI(); |
| 16 | + |
| 17 | +assert.throws( |
| 18 | +()=>{wasi.initialize();}, |
| 19 | +{ |
| 20 | +code: 'ERR_INVALID_ARG_TYPE', |
| 21 | +message: /"instance"argumentmustbeoftypeobject/ |
| 22 | +} |
| 23 | +); |
| 24 | +} |
| 25 | + |
| 26 | +{ |
| 27 | +// Verify that the passed instance has an exports objects. |
| 28 | +constwasi=newWASI({}); |
| 29 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 30 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 31 | + |
| 32 | +Object.defineProperty(instance,'exports',{get(){returnnull;}}); |
| 33 | +assert.throws( |
| 34 | +()=>{wasi.initialize(instance);}, |
| 35 | +{ |
| 36 | +code: 'ERR_INVALID_ARG_TYPE', |
| 37 | +message: /"instance\.exports"propertymustbeoftypeobject/ |
| 38 | +} |
| 39 | +); |
| 40 | +} |
| 41 | + |
| 42 | +{ |
| 43 | +// Verify that a _initialize() export was passed. |
| 44 | +constwasi=newWASI({}); |
| 45 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 46 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 47 | + |
| 48 | +Object.defineProperty(instance,'exports',{ |
| 49 | +get(){ |
| 50 | +return{_initialize: 5,memory: newUint8Array()}; |
| 51 | +}, |
| 52 | +}); |
| 53 | +assert.throws( |
| 54 | +()=>{wasi.initialize(instance);}, |
| 55 | +{ |
| 56 | +code: 'ERR_INVALID_ARG_TYPE', |
| 57 | +message: /"instance\.exports\._initialize"propertymustbeoftypefunction/ |
| 58 | +} |
| 59 | +); |
| 60 | +} |
| 61 | + |
| 62 | +{ |
| 63 | +// Verify that a _start export was not passed. |
| 64 | +constwasi=newWASI({}); |
| 65 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 66 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 67 | + |
| 68 | +Object.defineProperty(instance,'exports',{ |
| 69 | +get(){ |
| 70 | +return{ |
| 71 | +_start(){}, |
| 72 | +_initialize(){}, |
| 73 | +memory: newUint8Array(), |
| 74 | +}; |
| 75 | +} |
| 76 | +}); |
| 77 | +assert.throws( |
| 78 | +()=>{wasi.initialize(instance);}, |
| 79 | +{ |
| 80 | +code: 'ERR_INVALID_ARG_TYPE', |
| 81 | +message: /"instance\.exports\._start"propertymustbeundefined/ |
| 82 | +} |
| 83 | +); |
| 84 | +} |
| 85 | + |
| 86 | +{ |
| 87 | +// Verify that a memory export was passed. |
| 88 | +constwasi=newWASI({}); |
| 89 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 90 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 91 | + |
| 92 | +Object.defineProperty(instance,'exports',{ |
| 93 | +get(){return{_initialize(){}};} |
| 94 | +}); |
| 95 | +assert.throws( |
| 96 | +()=>{wasi.initialize(instance);}, |
| 97 | +{ |
| 98 | +code: 'ERR_INVALID_ARG_TYPE', |
| 99 | +message: /"instance\.exports\.memory"propertymustbeoftypeobject/ |
| 100 | +} |
| 101 | +); |
| 102 | +} |
| 103 | + |
| 104 | +{ |
| 105 | +// Verify that a non-ArrayBuffer memory.buffer is rejected. |
| 106 | +constwasi=newWASI({}); |
| 107 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 108 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 109 | + |
| 110 | +Object.defineProperty(instance,'exports',{ |
| 111 | +get(){ |
| 112 | +return{ |
| 113 | +_initialize(){}, |
| 114 | +memory: {}, |
| 115 | +}; |
| 116 | +} |
| 117 | +}); |
| 118 | +// The error message is a little white lie because any object |
| 119 | +// with a .buffer property of type ArrayBuffer is accepted, |
| 120 | +// but 99% of the time a WebAssembly.Memory object is used. |
| 121 | +assert.throws( |
| 122 | +()=>{wasi.initialize(instance);}, |
| 123 | +{ |
| 124 | +code: 'ERR_INVALID_ARG_TYPE', |
| 125 | +message: /"instance\.exports\.memory\.buffer"propertymustbeanWebAssembly\.Memory/ |
| 126 | +} |
| 127 | +); |
| 128 | +} |
| 129 | + |
| 130 | +{ |
| 131 | +// Verify that an argument that duck-types as a WebAssembly.Instance |
| 132 | +// is accepted. |
| 133 | +constwasi=newWASI({}); |
| 134 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 135 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 136 | + |
| 137 | +Object.defineProperty(instance,'exports',{ |
| 138 | +get(){ |
| 139 | +return{ |
| 140 | +_initialize(){}, |
| 141 | +memory: {buffer: newArrayBuffer(0)}, |
| 142 | +}; |
| 143 | +} |
| 144 | +}); |
| 145 | +wasi.initialize(instance); |
| 146 | +} |
| 147 | + |
| 148 | +{ |
| 149 | +// Verify that a WebAssembly.Instance from another VM context is accepted. |
| 150 | +constwasi=newWASI({}); |
| 151 | +constinstance=awaitvm.runInNewContext(` |
| 152 | + (async () => { |
| 153 | + const wasm = await WebAssembly.compile(bufferSource); |
| 154 | + const instance = await WebAssembly.instantiate(wasm); |
| 155 | +
|
| 156 | + Object.defineProperty(instance, 'exports', { |
| 157 | + get() { |
| 158 | + return { |
| 159 | + _initialize() {}, |
| 160 | + memory: new WebAssembly.Memory({ initial: 1 }) |
| 161 | + }; |
| 162 | + } |
| 163 | + }); |
| 164 | +
|
| 165 | + return instance; |
| 166 | + })() |
| 167 | + `,{ bufferSource }); |
| 168 | + |
| 169 | +wasi.initialize(instance); |
| 170 | +} |
| 171 | + |
| 172 | +{ |
| 173 | +// Verify that initialize() can only be called once. |
| 174 | +constwasi=newWASI({}); |
| 175 | +constwasm=awaitWebAssembly.compile(bufferSource); |
| 176 | +constinstance=awaitWebAssembly.instantiate(wasm); |
| 177 | + |
| 178 | +Object.defineProperty(instance,'exports',{ |
| 179 | +get(){ |
| 180 | +return{ |
| 181 | +_initialize(){}, |
| 182 | +memory: newWebAssembly.Memory({initial: 1}) |
| 183 | +}; |
| 184 | +} |
| 185 | +}); |
| 186 | +wasi.initialize(instance); |
| 187 | +assert.throws( |
| 188 | +()=>{wasi.initialize(instance);}, |
| 189 | +{ |
| 190 | +code: 'ERR_WASI_ALREADY_STARTED', |
| 191 | +message: /^WASIinstancehasalreadystarted$/ |
| 192 | +} |
| 193 | +); |
| 194 | +} |
| 195 | +})().then(common.mustCall()); |
0 commit comments