blob: ccca569ff449d27446a8a7015290e10e6116bffc [file] [log] [blame]
<!DOCTYPE html>
<!--
Copyright 2023 The Cobalt Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<head>
<title>Cobalt WASM Basic Test</title>
<script src='black_box_js_test_utils.js'></script>
</head>
<body>
<script>
const fail = msg => {
if (msg) {
console.error(msg);
}
notReached();
};
const timeoutId = setTimeout(fail, 3000);
const success = () => {
clearTimeout(timeoutId);
onEndTest();
};
const Module = {};
const noop = () => {};
const wasmImports = {
emscripten_notify_memory_growth: () => {
if (!Module.asm || !Module.asm.memory) {
return;
}
Module.HEAPU8 = new Uint8Array(Module.asm.memory.buffer);
},
fd_close: noop,
fd_seek: noop,
fd_write: noop,
proc_exit: noop,
};
const importObject = {
env: wasmImports,
wasi_snapshot_preview1: wasmImports,
};
const stringFromHeap = stringPointer => {
const heap = Module.HEAPU8;
let endPointer = stringPointer;
// Find null terminating character.
while (heap[endPointer]) ++endPointer;
return (new TextDecoder('utf8')).decode(heap.subarray(stringPointer, endPointer));
}
fetch('wasm_basic_test.wasm')
.then(response => response.arrayBuffer())
.then(bufferSource => WebAssembly.instantiate(bufferSource, importObject))
.then(({instance}) => {
Module.asm = instance.exports;
wasmImports.emscripten_notify_memory_growth();
assertEqual(3, Module.asm.add(1, 2));
// Include space for null terminating byte.
const helloStringPointer = Module.asm.malloc(7);
Module.asm.getHello(helloStringPointer);
assertEqual("Hello.", stringFromHeap(helloStringPointer));
Module.asm.free(helloStringPointer);
success();
})
.catch(fail);
setupFinished();
</script>
</body>