sbpf_assembler.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. let imports = {};
  2. imports['__wbindgen_placeholder__'] = module.exports;
  3. let wasm;
  4. const { TextEncoder, TextDecoder } = require(`util`);
  5. function debugString(val) {
  6. // primitive types
  7. const type = typeof val;
  8. if (type == 'number' || type == 'boolean' || val == null) {
  9. return `${val}`;
  10. }
  11. if (type == 'string') {
  12. return `"${val}"`;
  13. }
  14. if (type == 'symbol') {
  15. const description = val.description;
  16. if (description == null) {
  17. return 'Symbol';
  18. } else {
  19. return `Symbol(${description})`;
  20. }
  21. }
  22. if (type == 'function') {
  23. const name = val.name;
  24. if (typeof name == 'string' && name.length > 0) {
  25. return `Function(${name})`;
  26. } else {
  27. return 'Function';
  28. }
  29. }
  30. // objects
  31. if (Array.isArray(val)) {
  32. const length = val.length;
  33. let debug = '[';
  34. if (length > 0) {
  35. debug += debugString(val[0]);
  36. }
  37. for(let i = 1; i < length; i++) {
  38. debug += ', ' + debugString(val[i]);
  39. }
  40. debug += ']';
  41. return debug;
  42. }
  43. // Test for built-in
  44. const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
  45. let className;
  46. if (builtInMatches && builtInMatches.length > 1) {
  47. className = builtInMatches[1];
  48. } else {
  49. // Failed to match the standard '[object ClassName]'
  50. return toString.call(val);
  51. }
  52. if (className == 'Object') {
  53. // we're a user defined class or Object
  54. // JSON.stringify avoids problems with cycles, and is generally much
  55. // easier than looping through ownProperties of `val`.
  56. try {
  57. return 'Object(' + JSON.stringify(val) + ')';
  58. } catch (_) {
  59. return 'Object';
  60. }
  61. }
  62. // errors
  63. if (val instanceof Error) {
  64. return `${val.name}: ${val.message}\n${val.stack}`;
  65. }
  66. // TODO we could test for more things here, like `Set`s and `Map`s.
  67. return className;
  68. }
  69. let WASM_VECTOR_LEN = 0;
  70. let cachedUint8ArrayMemory0 = null;
  71. function getUint8ArrayMemory0() {
  72. if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
  73. cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
  74. }
  75. return cachedUint8ArrayMemory0;
  76. }
  77. const cachedTextEncoder = new TextEncoder('utf-8');
  78. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  79. ? function (arg, view) {
  80. return cachedTextEncoder.encodeInto(arg, view);
  81. }
  82. : function (arg, view) {
  83. const buf = cachedTextEncoder.encode(arg);
  84. view.set(buf);
  85. return {
  86. read: arg.length,
  87. written: buf.length
  88. };
  89. });
  90. function passStringToWasm0(arg, malloc, realloc) {
  91. if (realloc === undefined) {
  92. const buf = cachedTextEncoder.encode(arg);
  93. const ptr = malloc(buf.length, 1) >>> 0;
  94. getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
  95. WASM_VECTOR_LEN = buf.length;
  96. return ptr;
  97. }
  98. let len = arg.length;
  99. let ptr = malloc(len, 1) >>> 0;
  100. const mem = getUint8ArrayMemory0();
  101. let offset = 0;
  102. for (; offset < len; offset++) {
  103. const code = arg.charCodeAt(offset);
  104. if (code > 0x7F) break;
  105. mem[ptr + offset] = code;
  106. }
  107. if (offset !== len) {
  108. if (offset !== 0) {
  109. arg = arg.slice(offset);
  110. }
  111. ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
  112. const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
  113. const ret = encodeString(arg, view);
  114. offset += ret.written;
  115. ptr = realloc(ptr, len, offset, 1) >>> 0;
  116. }
  117. WASM_VECTOR_LEN = offset;
  118. return ptr;
  119. }
  120. let cachedDataViewMemory0 = null;
  121. function getDataViewMemory0() {
  122. if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
  123. cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
  124. }
  125. return cachedDataViewMemory0;
  126. }
  127. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  128. cachedTextDecoder.decode();
  129. function decodeText(ptr, len) {
  130. return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
  131. }
  132. function getStringFromWasm0(ptr, len) {
  133. ptr = ptr >>> 0;
  134. return decodeText(ptr, len);
  135. }
  136. function takeFromExternrefTable0(idx) {
  137. const value = wasm.__wbindgen_export_2.get(idx);
  138. wasm.__externref_table_dealloc(idx);
  139. return value;
  140. }
  141. function getArrayU8FromWasm0(ptr, len) {
  142. ptr = ptr >>> 0;
  143. return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
  144. }
  145. /**
  146. * @param {string} source
  147. * @returns {Uint8Array}
  148. */
  149. module.exports.assemble = function(source) {
  150. const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  151. const len0 = WASM_VECTOR_LEN;
  152. const ret = wasm.assemble(ptr0, len0);
  153. if (ret[3]) {
  154. throw takeFromExternrefTable0(ret[2]);
  155. }
  156. var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
  157. wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
  158. return v2;
  159. };
  160. module.exports.__wbg_new_1930cbb8d9ffc31b = function() {
  161. const ret = new Object();
  162. return ret;
  163. };
  164. module.exports.__wbg_new_e969dc3f68d25093 = function() {
  165. const ret = new Array();
  166. return ret;
  167. };
  168. module.exports.__wbg_set_3f1d0b984ed272ed = function(arg0, arg1, arg2) {
  169. arg0[arg1] = arg2;
  170. };
  171. module.exports.__wbg_set_d636a0463acf1dbc = function(arg0, arg1, arg2) {
  172. arg0[arg1 >>> 0] = arg2;
  173. };
  174. module.exports.__wbg_wbindgendebugstring_bb652b1bc2061b6d = function(arg0, arg1) {
  175. const ret = debugString(arg1);
  176. const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  177. const len1 = WASM_VECTOR_LEN;
  178. getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
  179. getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
  180. };
  181. module.exports.__wbg_wbindgenthrow_4c11a24fca429ccf = function(arg0, arg1) {
  182. throw new Error(getStringFromWasm0(arg0, arg1));
  183. };
  184. module.exports.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
  185. // Cast intrinsic for `Ref(String) -> Externref`.
  186. const ret = getStringFromWasm0(arg0, arg1);
  187. return ret;
  188. };
  189. module.exports.__wbindgen_init_externref_table = function() {
  190. const table = wasm.__wbindgen_export_2;
  191. const offset = table.grow(4);
  192. table.set(0, undefined);
  193. table.set(offset + 0, undefined);
  194. table.set(offset + 1, null);
  195. table.set(offset + 2, true);
  196. table.set(offset + 3, false);
  197. ;
  198. };
  199. const path = require('path').join(__dirname, 'sbpf_assembler_bg.wasm');
  200. const bytes = require('fs').readFileSync(path);
  201. const wasmModule = new WebAssembly.Module(bytes);
  202. const wasmInstance = new WebAssembly.Instance(wasmModule, imports);
  203. wasm = wasmInstance.exports;
  204. module.exports.__wasm = wasm;
  205. wasm.__wbindgen_start();