| <!doctype html> |
| <meta charset="utf8"> |
| <meta name="timeout" content="long"> |
| <title>IndexedDB: WebAssembly module values</title> |
| <link rel="help" href="https://w3c.github.io/IndexedDB/"> |
| <link rel="help" href="https://webassembly.github.io/spec/"> |
| <link rel="author" href="pwnall@chromium.org" title="Victor Costan"> |
| <script src="/resources/testharness.js"></script> |
| <script src="/resources/testharnessreport.js"></script> |
| <script src="support-promises.js"></script> |
| <script> |
| 'use strict'; |
| |
| // Binary representation for a WASM module that exports an "inc" function. |
| // |
| // This test covers storing WASM modules in IndexedDB. Its failure should only |
| // be debugged if WASM specification tests pass. To this end, the test does not |
| // use the WASM module builder helpers, trading off WASM debuggability in return |
| // for having the WASM wire bytes listed explicitly in the test body. Having the |
| // wire bytes spelled out can be helpful when debugging IndexedDB failures. |
| let wasm_module_bytes = new Uint8Array([ |
| 0x00, 0x61, 0x73, 0x6d, // Magic. |
| 0x01, 0x00, 0x00, 0x00, // Version. |
| 0x01, 0x06, 0x01, // Type section - 6 bytes, 1 entry |
| 0x60, 0x01, 0x7f, 0x01, 0x7f, // Type 0. Function: (i32) -> (i32) |
| 0x03, 0x02, 0x01, // Function section - 2 bytes, 1 entry |
| 0x00, // Function 0: Type 0 |
| 0x07, 0x07, 0x01, // Export section - 7 bytes, 1 entry |
| 0x03, 0x69, 0x6e, 0x63, // Export 1. { name: "inc" |
| 0x00, 0x00, // desc: function 0 } |
| 0x0a, 0x09, 0x01, // Code section: 9 bytes, 1 entry |
| 0x07, 0x00, // Function 1: 7 code bytes, 0 locals |
| 0x20, 0x00, // getlocal 0 |
| 0x41, 0x01, // i32.const 1 |
| 0x6a, // i32.add |
| 0x0b, // end |
| ]); |
| |
| promise_test(async testCase => { |
| const wasm_module = await WebAssembly.compile(wasm_module_bytes.buffer); |
| |
| const database = await createDatabase(testCase, (database, transaction) => { |
| const store = database.createObjectStore('store'); |
| store.put(wasm_module, 'key1'); |
| }); |
| |
| const result = await new Promise((resolve, reject) => { |
| const transaction = database.transaction(['store'], 'readonly'); |
| const store = transaction.objectStore('store'); |
| const request = store.get('key1'); |
| request.onsuccess = (event) => resolve(event.target.result); |
| request.onerror = (event) => reject(event.target.error); |
| }); |
| |
| database.close(); |
| |
| const instance = await WebAssembly.instantiate(result); |
| assert_equals( |
| instance.exports['inc'](42), 43, 'inc should increment its argument'); |
| }, 'WebAssembly module as an IndexedDB value'); |
| |
| promise_test(async testCase => { |
| const wasm_module = await WebAssembly.compile(wasm_module_bytes.buffer); |
| |
| const database = await createDatabase(testCase, (database, transaction) => { |
| const store = database.createObjectStore('store'); |
| store.put({ module: wasm_module }, 'key1'); |
| }); |
| |
| const result = await new Promise((resolve, reject) => { |
| const transaction = database.transaction(['store'], 'readonly'); |
| const store = transaction.objectStore('store'); |
| const request = store.get('key1'); |
| request.onsuccess = (event) => resolve(event.target.result); |
| request.onerror = (event) => reject(event.target.error); |
| }); |
| |
| database.close(); |
| |
| const instance = await WebAssembly.instantiate(result.module); |
| assert_equals( |
| instance.exports['inc'](42), 43, 'inc should increment its argument'); |
| }, 'WebAssembly module in a JavaScript object IndexedDB value'); |
| |
| promise_test(async testCase => { |
| const wasm_module = await WebAssembly.compile(wasm_module_bytes.buffer); |
| |
| const database = await createDatabase(testCase, (database, transaction) => { |
| const store = database.createObjectStore('store', { keyPath: 'key' }); |
| store.put({ key: 'key1', module: wasm_module }); |
| }); |
| |
| const result = await new Promise((resolve, reject) => { |
| const transaction = database.transaction(['store'], 'readonly'); |
| const store = transaction.objectStore('store'); |
| const request = store.get('key1'); |
| request.onsuccess = (event) => resolve(event.target.result); |
| request.onerror = (event) => reject(event.target.error); |
| }); |
| |
| database.close(); |
| |
| assert_equals('key1', result.key); |
| const instance = await WebAssembly.instantiate(result.module); |
| assert_equals( |
| instance.exports['inc'](42), 43, 'inc should increment its argument'); |
| }, 'WebAssembly module in an IndexedDB value with an inline key'); |
| |
| </script> |