sbpf_assembler.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. let wasm;
  2. function debugString(val) {
  3. // primitive types
  4. const type = typeof val;
  5. if (type == 'number' || type == 'boolean' || val == null) {
  6. return `${val}`;
  7. }
  8. if (type == 'string') {
  9. return `"${val}"`;
  10. }
  11. if (type == 'symbol') {
  12. const description = val.description;
  13. if (description == null) {
  14. return 'Symbol';
  15. } else {
  16. return `Symbol(${description})`;
  17. }
  18. }
  19. if (type == 'function') {
  20. const name = val.name;
  21. if (typeof name == 'string' && name.length > 0) {
  22. return `Function(${name})`;
  23. } else {
  24. return 'Function';
  25. }
  26. }
  27. // objects
  28. if (Array.isArray(val)) {
  29. const length = val.length;
  30. let debug = '[';
  31. if (length > 0) {
  32. debug += debugString(val[0]);
  33. }
  34. for(let i = 1; i < length; i++) {
  35. debug += ', ' + debugString(val[i]);
  36. }
  37. debug += ']';
  38. return debug;
  39. }
  40. // Test for built-in
  41. const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
  42. let className;
  43. if (builtInMatches && builtInMatches.length > 1) {
  44. className = builtInMatches[1];
  45. } else {
  46. // Failed to match the standard '[object ClassName]'
  47. return toString.call(val);
  48. }
  49. if (className == 'Object') {
  50. // we're a user defined class or Object
  51. // JSON.stringify avoids problems with cycles, and is generally much
  52. // easier than looping through ownProperties of `val`.
  53. try {
  54. return 'Object(' + JSON.stringify(val) + ')';
  55. } catch (_) {
  56. return 'Object';
  57. }
  58. }
  59. // errors
  60. if (val instanceof Error) {
  61. return `${val.name}: ${val.message}\n${val.stack}`;
  62. }
  63. // TODO we could test for more things here, like `Set`s and `Map`s.
  64. return className;
  65. }
  66. let WASM_VECTOR_LEN = 0;
  67. let cachedUint8ArrayMemory0 = null;
  68. function getUint8ArrayMemory0() {
  69. if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
  70. cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
  71. }
  72. return cachedUint8ArrayMemory0;
  73. }
  74. const cachedTextEncoder = (typeof TextEncoder !== 'undefined' ? new TextEncoder('utf-8') : { encode: () => { throw Error('TextEncoder not available') } } );
  75. const encodeString = (typeof cachedTextEncoder.encodeInto === 'function'
  76. ? function (arg, view) {
  77. return cachedTextEncoder.encodeInto(arg, view);
  78. }
  79. : function (arg, view) {
  80. const buf = cachedTextEncoder.encode(arg);
  81. view.set(buf);
  82. return {
  83. read: arg.length,
  84. written: buf.length
  85. };
  86. });
  87. function passStringToWasm0(arg, malloc, realloc) {
  88. if (realloc === undefined) {
  89. const buf = cachedTextEncoder.encode(arg);
  90. const ptr = malloc(buf.length, 1) >>> 0;
  91. getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
  92. WASM_VECTOR_LEN = buf.length;
  93. return ptr;
  94. }
  95. let len = arg.length;
  96. let ptr = malloc(len, 1) >>> 0;
  97. const mem = getUint8ArrayMemory0();
  98. let offset = 0;
  99. for (; offset < len; offset++) {
  100. const code = arg.charCodeAt(offset);
  101. if (code > 0x7F) break;
  102. mem[ptr + offset] = code;
  103. }
  104. if (offset !== len) {
  105. if (offset !== 0) {
  106. arg = arg.slice(offset);
  107. }
  108. ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
  109. const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
  110. const ret = encodeString(arg, view);
  111. offset += ret.written;
  112. ptr = realloc(ptr, len, offset, 1) >>> 0;
  113. }
  114. WASM_VECTOR_LEN = offset;
  115. return ptr;
  116. }
  117. let cachedDataViewMemory0 = null;
  118. function getDataViewMemory0() {
  119. if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
  120. cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
  121. }
  122. return cachedDataViewMemory0;
  123. }
  124. let cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
  125. if (typeof TextDecoder !== 'undefined') { cachedTextDecoder.decode(); };
  126. const MAX_SAFARI_DECODE_BYTES = 2146435072;
  127. let numBytesDecoded = 0;
  128. function decodeText(ptr, len) {
  129. numBytesDecoded += len;
  130. if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
  131. cachedTextDecoder = (typeof TextDecoder !== 'undefined' ? new TextDecoder('utf-8', { ignoreBOM: true, fatal: true }) : { decode: () => { throw Error('TextDecoder not available') } } );
  132. cachedTextDecoder.decode();
  133. numBytesDecoded = len;
  134. }
  135. return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
  136. }
  137. function getStringFromWasm0(ptr, len) {
  138. ptr = ptr >>> 0;
  139. return decodeText(ptr, len);
  140. }
  141. function takeFromExternrefTable0(idx) {
  142. const value = wasm.__wbindgen_export_2.get(idx);
  143. wasm.__externref_table_dealloc(idx);
  144. return value;
  145. }
  146. function getArrayU8FromWasm0(ptr, len) {
  147. ptr = ptr >>> 0;
  148. return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
  149. }
  150. /**
  151. * @param {string} source
  152. * @returns {Uint8Array}
  153. */
  154. export function assemble(source) {
  155. const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  156. const len0 = WASM_VECTOR_LEN;
  157. const ret = wasm.assemble(ptr0, len0);
  158. if (ret[3]) {
  159. throw takeFromExternrefTable0(ret[2]);
  160. }
  161. var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
  162. wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
  163. return v2;
  164. }
  165. const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
  166. async function __wbg_load(module, imports) {
  167. if (typeof Response === 'function' && module instanceof Response) {
  168. if (typeof WebAssembly.instantiateStreaming === 'function') {
  169. try {
  170. return await WebAssembly.instantiateStreaming(module, imports);
  171. } catch (e) {
  172. const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
  173. if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
  174. console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
  175. } else {
  176. throw e;
  177. }
  178. }
  179. }
  180. const bytes = await module.arrayBuffer();
  181. return await WebAssembly.instantiate(bytes, imports);
  182. } else {
  183. const instance = await WebAssembly.instantiate(module, imports);
  184. if (instance instanceof WebAssembly.Instance) {
  185. return { instance, module };
  186. } else {
  187. return instance;
  188. }
  189. }
  190. }
  191. function __wbg_get_imports() {
  192. const imports = {};
  193. imports.wbg = {};
  194. imports.wbg.__wbg_new_1930cbb8d9ffc31b = function() {
  195. const ret = new Object();
  196. return ret;
  197. };
  198. imports.wbg.__wbg_new_e969dc3f68d25093 = function() {
  199. const ret = new Array();
  200. return ret;
  201. };
  202. imports.wbg.__wbg_set_3f1d0b984ed272ed = function(arg0, arg1, arg2) {
  203. arg0[arg1] = arg2;
  204. };
  205. imports.wbg.__wbg_set_d636a0463acf1dbc = function(arg0, arg1, arg2) {
  206. arg0[arg1 >>> 0] = arg2;
  207. };
  208. imports.wbg.__wbg_wbindgendebugstring_bb652b1bc2061b6d = function(arg0, arg1) {
  209. const ret = debugString(arg1);
  210. const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  211. const len1 = WASM_VECTOR_LEN;
  212. getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
  213. getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
  214. };
  215. imports.wbg.__wbg_wbindgenthrow_4c11a24fca429ccf = function(arg0, arg1) {
  216. throw new Error(getStringFromWasm0(arg0, arg1));
  217. };
  218. imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
  219. // Cast intrinsic for `Ref(String) -> Externref`.
  220. const ret = getStringFromWasm0(arg0, arg1);
  221. return ret;
  222. };
  223. imports.wbg.__wbindgen_init_externref_table = function() {
  224. const table = wasm.__wbindgen_export_2;
  225. const offset = table.grow(4);
  226. table.set(0, undefined);
  227. table.set(offset + 0, undefined);
  228. table.set(offset + 1, null);
  229. table.set(offset + 2, true);
  230. table.set(offset + 3, false);
  231. ;
  232. };
  233. return imports;
  234. }
  235. function __wbg_init_memory(imports, memory) {
  236. }
  237. function __wbg_finalize_init(instance, module) {
  238. wasm = instance.exports;
  239. __wbg_init.__wbindgen_wasm_module = module;
  240. cachedDataViewMemory0 = null;
  241. cachedUint8ArrayMemory0 = null;
  242. wasm.__wbindgen_start();
  243. return wasm;
  244. }
  245. function initSync(module) {
  246. if (wasm !== undefined) return wasm;
  247. if (typeof module !== 'undefined') {
  248. if (Object.getPrototypeOf(module) === Object.prototype) {
  249. ({module} = module)
  250. } else {
  251. console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
  252. }
  253. }
  254. const imports = __wbg_get_imports();
  255. __wbg_init_memory(imports);
  256. if (!(module instanceof WebAssembly.Module)) {
  257. module = new WebAssembly.Module(module);
  258. }
  259. const instance = new WebAssembly.Instance(module, imports);
  260. return __wbg_finalize_init(instance, module);
  261. }
  262. async function __wbg_init(module_or_path) {
  263. if (wasm !== undefined) return wasm;
  264. if (typeof module_or_path !== 'undefined') {
  265. if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
  266. ({module_or_path} = module_or_path)
  267. } else {
  268. console.warn('using deprecated parameters for the initialization function; pass a single object instead')
  269. }
  270. }
  271. if (typeof module_or_path === 'undefined') {
  272. module_or_path = new URL('sbpf_assembler_bg.wasm', import.meta.url);
  273. }
  274. const imports = __wbg_get_imports();
  275. if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
  276. module_or_path = fetch(module_or_path);
  277. }
  278. __wbg_init_memory(imports);
  279. const { instance, module } = await __wbg_load(await module_or_path, imports);
  280. return __wbg_finalize_init(instance, module);
  281. }
  282. export { initSync };
  283. export default __wbg_init;