Advertisement
Guest User

Untitled

a guest
Oct 12th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.80 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>wsam sample</title>
  6. <script>
  7. 'use strict';
  8.  
  9. (()=>{
  10. const wasmBin = new Uint8Array([
  11. // module header
  12. 0x00, 0x61, 0x73, 0x6d, // magic number
  13. 0x01, 0x00 ,0x00, 0x00, // version
  14.  
  15. // --------------------------
  16. // Type section
  17. // section header
  18. 0x01, // section code: Type section
  19. 0x06, // payload bytes
  20.  
  21. // section payload data
  22. 0x01, // number of entries
  23.  
  24. // entry 0
  25. 0x60, // form (func)
  26. 0x01, // number of params
  27. 0x7F, // param 1: i32
  28. 0x01, // number of return values
  29. 0x7F, // return value type: i32
  30.  
  31. // --------------------------
  32. // Import section
  33. // section header
  34. 0x02, // section code: Import section
  35. 0x0B, // payload bytes
  36.  
  37. // section payload data
  38. 0x01, // number of entries
  39.  
  40. // entry 0
  41. 0x03, // import module name length
  42. // import module (UTF-8): 'mod'
  43. 0x6D, 0x6F, 0x64,
  44. 0x03, // import field name length
  45. // import field (UTF-8): 'fnc'
  46. 0x66, 0x6E, 0x63,
  47. 0x00, // import external kind: Function
  48. 0x00, // import index: Type entry 0
  49.  
  50.  
  51. // ---------------------------
  52. // Function section
  53. // section header
  54. 0x03, // section code: Function section
  55. 0x02, // payload bytes
  56.  
  57. // section payload data
  58. 0x01, // number of entries
  59. 0x00, // entry 0: Type entry 0
  60.  
  61. // ---------------------------
  62. // Export section
  63. // section header
  64. 0x07, // section code: Function section
  65. 0x07, // payload bytes
  66.  
  67. // section payload data
  68. 0x01, // number of entries
  69.  
  70. // entry 0
  71. 0x03, // export name length
  72. // export name (UTF-8): 'inc'
  73. 0x69, 0x6E, 0x63,
  74. 0x00, // external kind: Function
  75. 0x01, // export index: Function entry 0
  76.  
  77. // ----------------------------
  78. // Code Section
  79. // section header
  80. 0x0A, // section code: Code section
  81. 0x10, // payload bytes
  82.  
  83. // section payload data
  84. 0x01, // number of function bodies
  85.  
  86. // function body 0: function entry 0
  87. 0x0E, // body size
  88. 0x01, // number of local entries
  89. // local entry 0
  90. 0x01, // number of local variables
  91. 0x7F, // local valiable 0 type: i32
  92. // byte codes
  93. 0x20, 0x00, // get_local 0
  94. 0x41, 0x01, // i32.const 1
  95. 0x6A, // i32.add
  96. 0x10, 0x00, // call function entry 0
  97. 0x41, 0x01, // i32.const 1
  98. 0x6A, // i32.add
  99. 0x0B, // end of function
  100.  
  101. ]);
  102.  
  103. document.addEventListener('DOMContentLoaded', ()=>{
  104. WebAssembly.instantiate(wasmBin, {
  105. mod: {
  106. fnc: (x)=>{
  107. alert(x);
  108. return x + 10;
  109. }
  110. }
  111. }).then((obj)=>{
  112. const resultVal = obj.instance.exports.inc(10);
  113. document.getElementById('resultDiv').appendChild(document.createTextNode(
  114. 'resultVal=' + resultVal
  115. ));
  116. }).catch((err)=>{
  117. console.log(err);
  118. document.getElementById('resultDiv').appendChild(document.createTextNode(
  119. 'error'));
  120. });
  121. });
  122. })();
  123. </script>
  124. </head>
  125. <body>
  126. <div id="resultDiv"></div>
  127. </body>
  128. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement