Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>wsam sample</title>
- <script>
- 'use strict';
- (()=>{
- const wasmBin = new Uint8Array([
- // module header
- 0x00, 0x61, 0x73, 0x6d, // magic number
- 0x01, 0x00 ,0x00, 0x00, // version
- // --------------------------
- // Type section
- // section header
- 0x01, // section code: Type section
- 0x06, // payload bytes
- // section payload data
- 0x01, // number of entries
- // entry 0
- 0x60, // form (func)
- 0x01, // number of params
- 0x7F, // param 1: i32
- 0x01, // number of return values
- 0x7F, // return value type: i32
- // --------------------------
- // Import section
- // section header
- 0x02, // section code: Import section
- 0x0B, // payload bytes
- // section payload data
- 0x01, // number of entries
- // entry 0
- 0x03, // import module name length
- // import module (UTF-8): 'mod'
- 0x6D, 0x6F, 0x64,
- 0x03, // import field name length
- // import field (UTF-8): 'fnc'
- 0x66, 0x6E, 0x63,
- 0x00, // import external kind: Function
- 0x00, // import index: Type entry 0
- // ---------------------------
- // Function section
- // section header
- 0x03, // section code: Function section
- 0x02, // payload bytes
- // section payload data
- 0x01, // number of entries
- 0x00, // entry 0: Type entry 0
- // ---------------------------
- // Export section
- // section header
- 0x07, // section code: Function section
- 0x07, // payload bytes
- // section payload data
- 0x01, // number of entries
- // entry 0
- 0x03, // export name length
- // export name (UTF-8): 'inc'
- 0x69, 0x6E, 0x63,
- 0x00, // external kind: Function
- 0x01, // export index: Function entry 0
- // ----------------------------
- // Code Section
- // section header
- 0x0A, // section code: Code section
- 0x10, // payload bytes
- // section payload data
- 0x01, // number of function bodies
- // function body 0: function entry 0
- 0x0E, // body size
- 0x01, // number of local entries
- // local entry 0
- 0x01, // number of local variables
- 0x7F, // local valiable 0 type: i32
- // byte codes
- 0x20, 0x00, // get_local 0
- 0x41, 0x01, // i32.const 1
- 0x6A, // i32.add
- 0x10, 0x00, // call function entry 0
- 0x41, 0x01, // i32.const 1
- 0x6A, // i32.add
- 0x0B, // end of function
- ]);
- document.addEventListener('DOMContentLoaded', ()=>{
- WebAssembly.instantiate(wasmBin, {
- mod: {
- fnc: (x)=>{
- alert(x);
- return x + 10;
- }
- }
- }).then((obj)=>{
- const resultVal = obj.instance.exports.inc(10);
- document.getElementById('resultDiv').appendChild(document.createTextNode(
- 'resultVal=' + resultVal
- ));
- }).catch((err)=>{
- console.log(err);
- document.getElementById('resultDiv').appendChild(document.createTextNode(
- 'error'));
- });
- });
- })();
- </script>
- </head>
- <body>
- <div id="resultDiv"></div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement