-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enable running generated wasm module in chrome (#148)
* enable running generated wasm module in chrome --------- Signed-off-by: Su Yihan <[email protected]>
- Loading branch information
Showing
7 changed files
with
224 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<!-- | ||
Copyright (C) 2023 Intel Corporation. All rights reserved. | ||
SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
--> | ||
|
||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<meta charset="utf-8" /> | ||
<title>run wasm module</title> | ||
</head> | ||
<body> | ||
<form id="processForm"> | ||
<table> | ||
<tr> | ||
<td><label for="pathInput">path:</label></td> | ||
<td><input type="text" id="pathInput" required style="width: 500px;"></td> | ||
</tr> | ||
<tr> | ||
<td><label for="funcNameInput">function name:</label></td> | ||
<td><input type="text" id="funcNameInput" style="width: 500px;"></td> | ||
</tr> | ||
<tr> | ||
<td><label for="argsInput">arguments:</label></td> | ||
<td><input type="text" id="argsInput" style="width: 500px;"></td> | ||
</tr> | ||
<tr> | ||
<td><label for="warmupTimes">warmup times:</label></td> | ||
<td><input type="text" id="warmupTimes" style="width: 500px;"></td> | ||
</tr> | ||
<tr> | ||
<td><label for="targetSelect">run target:</label></td> | ||
<td> | ||
<select id="targetSelect" style="width: 500px;"> | ||
<option value="wasm">WASM</option> | ||
<option value="js">JS</option> | ||
</select> | ||
</td> | ||
</tr> | ||
</table> | ||
<p></p> | ||
<button type="submit">submit</button> | ||
</form> | ||
|
||
<script type="module"> | ||
import { run_wasm_module } from "./run_module_on_chrome.js"; | ||
document.getElementById("processForm").addEventListener("submit", function(event) { | ||
event.preventDefault(); | ||
|
||
var path = document.getElementById("pathInput").value; | ||
var funcName = document.getElementById("funcNameInput").value; | ||
var args = document.getElementById("argsInput").value.split(","); | ||
var warmupTimes= document.getElementById("warmupTimes").value; | ||
var runTarget = document.getElementById("targetSelect").value; | ||
|
||
run_wasm_module(path, funcName, warmupTimes, runTarget, ...args); | ||
}); | ||
</script> | ||
<p id="result"></p> | ||
<p id="time"></p> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
* Copyright (C) 2023 Intel Corporation. All rights reserved. | ||
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
*/ | ||
|
||
import { importObject, setWasmMemory } from './import_object.js'; | ||
|
||
export function run_wasm_module(filePath, funcName, warmupTimes, runTarget, ...funcArgs) { | ||
const parts = filePath.split("."); | ||
const extension = parts[parts.length - 1]; | ||
if (runTarget === 'js') { | ||
if (extension !== 'js') { | ||
const resultElement = document.getElementById('result'); | ||
resultElement.innerHTML = `Error: filePath must end with ".js`; | ||
} | ||
fetch(filePath) | ||
.then(response => response.text()) | ||
.then(script => { | ||
if (warmupTimes) { | ||
for (let i = 0; i < parseInt(warmupTimes); i++) { | ||
eval(script); | ||
} | ||
} | ||
const start_time = performance.now(); | ||
let res = eval(script); | ||
if (funcName) { | ||
res = window[funcName](...funcArgs); | ||
} | ||
const end_time = performance.now(); | ||
if (typeof res !== 'object' || res === null) { | ||
const resultElement = document.getElementById('result'); | ||
resultElement.innerHTML = `The result is: ${res}`; | ||
} | ||
const timeElement = document.getElementById('time'); | ||
timeElement.innerHTML = `Execution time is: ${end_time - start_time}`; | ||
}); | ||
} else if (runTarget === 'wasm') { | ||
if (extension !== 'wasm') { | ||
const resultElement = document.getElementById('result'); | ||
resultElement.innerHTML = `Error: filePath must end with ".wasm`; | ||
} | ||
fetch(filePath) | ||
.then((response) => response.arrayBuffer()) | ||
.then((bytes) => WebAssembly.instantiate(bytes, importObject)) | ||
.then((results) => { | ||
const exports = results.instance.exports; | ||
setWasmMemory(exports.default); | ||
const startFunc = exports._entry; | ||
const exportedFunc = exports[funcName]; | ||
if (warmupTimes) { | ||
for (let i = 0; i < parseInt(warmupTimes); i++) { | ||
startFunc(); | ||
exportedFunc(...funcArgs); | ||
} | ||
} | ||
const start_time = performance.now(); | ||
startFunc(); | ||
const res = exportedFunc(...funcArgs); | ||
const end_time = performance.now(); | ||
if (typeof res !== 'object' || res === null) { | ||
const resultElement = document.getElementById('result'); | ||
resultElement.innerHTML = `The result is: ${res}`; | ||
} | ||
const timeElement = document.getElementById('time'); | ||
timeElement.innerHTML = `Execution time is: ${end_time - start_time}`; | ||
}); | ||
} | ||
} |
File renamed without changes.