sbpf_assembler.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 = new TextEncoder();
  75. if (!('encodeInto' in cachedTextEncoder)) {
  76. cachedTextEncoder.encodeInto = function (arg, view) {
  77. const buf = cachedTextEncoder.encode(arg);
  78. view.set(buf);
  79. return {
  80. read: arg.length,
  81. written: buf.length
  82. };
  83. }
  84. }
  85. function passStringToWasm0(arg, malloc, realloc) {
  86. if (realloc === undefined) {
  87. const buf = cachedTextEncoder.encode(arg);
  88. const ptr = malloc(buf.length, 1) >>> 0;
  89. getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
  90. WASM_VECTOR_LEN = buf.length;
  91. return ptr;
  92. }
  93. let len = arg.length;
  94. let ptr = malloc(len, 1) >>> 0;
  95. const mem = getUint8ArrayMemory0();
  96. let offset = 0;
  97. for (; offset < len; offset++) {
  98. const code = arg.charCodeAt(offset);
  99. if (code > 0x7F) break;
  100. mem[ptr + offset] = code;
  101. }
  102. if (offset !== len) {
  103. if (offset !== 0) {
  104. arg = arg.slice(offset);
  105. }
  106. ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
  107. const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
  108. const ret = cachedTextEncoder.encodeInto(arg, view);
  109. offset += ret.written;
  110. ptr = realloc(ptr, len, offset, 1) >>> 0;
  111. }
  112. WASM_VECTOR_LEN = offset;
  113. return ptr;
  114. }
  115. let cachedDataViewMemory0 = null;
  116. function getDataViewMemory0() {
  117. if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
  118. cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
  119. }
  120. return cachedDataViewMemory0;
  121. }
  122. let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  123. cachedTextDecoder.decode();
  124. const MAX_SAFARI_DECODE_BYTES = 2146435072;
  125. let numBytesDecoded = 0;
  126. function decodeText(ptr, len) {
  127. numBytesDecoded += len;
  128. if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
  129. cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
  130. cachedTextDecoder.decode();
  131. numBytesDecoded = len;
  132. }
  133. return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
  134. }
  135. function getStringFromWasm0(ptr, len) {
  136. ptr = ptr >>> 0;
  137. return decodeText(ptr, len);
  138. }
  139. function takeFromExternrefTable0(idx) {
  140. const value = wasm.__wbindgen_export_2.get(idx);
  141. wasm.__externref_table_dealloc(idx);
  142. return value;
  143. }
  144. function getArrayU8FromWasm0(ptr, len) {
  145. ptr = ptr >>> 0;
  146. return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
  147. }
  148. /**
  149. * @param {string} source
  150. * @returns {Uint8Array}
  151. */
  152. export function assemble(source) {
  153. const ptr0 = passStringToWasm0(source, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  154. const len0 = WASM_VECTOR_LEN;
  155. const ret = wasm.assemble(ptr0, len0);
  156. if (ret[3]) {
  157. throw takeFromExternrefTable0(ret[2]);
  158. }
  159. var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
  160. wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
  161. return v2;
  162. }
  163. const EXPECTED_RESPONSE_TYPES = new Set(['basic', 'cors', 'default']);
  164. async function __wbg_load(module, imports) {
  165. if (typeof Response === 'function' && module instanceof Response) {
  166. if (typeof WebAssembly.instantiateStreaming === 'function') {
  167. try {
  168. return await WebAssembly.instantiateStreaming(module, imports);
  169. } catch (e) {
  170. const validResponse = module.ok && EXPECTED_RESPONSE_TYPES.has(module.type);
  171. if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
  172. 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);
  173. } else {
  174. throw e;
  175. }
  176. }
  177. }
  178. const bytes = await module.arrayBuffer();
  179. return await WebAssembly.instantiate(bytes, imports);
  180. } else {
  181. const instance = await WebAssembly.instantiate(module, imports);
  182. if (instance instanceof WebAssembly.Instance) {
  183. return { instance, module };
  184. } else {
  185. return instance;
  186. }
  187. }
  188. }
  189. function __wbg_get_imports() {
  190. const imports = {};
  191. imports.wbg = {};
  192. imports.wbg.__wbg_new_19c25a3f2fa63a02 = function() {
  193. const ret = new Object();
  194. return ret;
  195. };
  196. imports.wbg.__wbg_new_1f3a344cf3123716 = function() {
  197. const ret = new Array();
  198. return ret;
  199. };
  200. imports.wbg.__wbg_set_3f1d0b984ed272ed = function(arg0, arg1, arg2) {
  201. arg0[arg1] = arg2;
  202. };
  203. imports.wbg.__wbg_set_90f6c0f7bd8c0415 = function(arg0, arg1, arg2) {
  204. arg0[arg1 >>> 0] = arg2;
  205. };
  206. imports.wbg.__wbg_wbindgendebugstring_99ef257a3ddda34d = function(arg0, arg1) {
  207. const ret = debugString(arg1);
  208. const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
  209. const len1 = WASM_VECTOR_LEN;
  210. getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
  211. getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
  212. };
  213. imports.wbg.__wbg_wbindgenthrow_451ec1a8469d7eb6 = function(arg0, arg1) {
  214. throw new Error(getStringFromWasm0(arg0, arg1));
  215. };
  216. imports.wbg.__wbindgen_cast_2241b6af4c4b2941 = function(arg0, arg1) {
  217. // Cast intrinsic for `Ref(String) -> Externref`.
  218. const ret = getStringFromWasm0(arg0, arg1);
  219. return ret;
  220. };
  221. imports.wbg.__wbindgen_init_externref_table = function() {
  222. const table = wasm.__wbindgen_export_2;
  223. const offset = table.grow(4);
  224. table.set(0, undefined);
  225. table.set(offset + 0, undefined);
  226. table.set(offset + 1, null);
  227. table.set(offset + 2, true);
  228. table.set(offset + 3, false);
  229. ;
  230. };
  231. return imports;
  232. }
  233. function __wbg_init_memory(imports, memory) {
  234. }
  235. function __wbg_finalize_init(instance, module) {
  236. wasm = instance.exports;
  237. __wbg_init.__wbindgen_wasm_module = module;
  238. cachedDataViewMemory0 = null;
  239. cachedUint8ArrayMemory0 = null;
  240. wasm.__wbindgen_start();
  241. return wasm;
  242. }
  243. function initSync(module) {
  244. if (wasm !== undefined) return wasm;
  245. if (typeof module !== 'undefined') {
  246. if (Object.getPrototypeOf(module) === Object.prototype) {
  247. ({module} = module)
  248. } else {
  249. console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
  250. }
  251. }
  252. const imports = __wbg_get_imports();
  253. __wbg_init_memory(imports);
  254. if (!(module instanceof WebAssembly.Module)) {
  255. module = new WebAssembly.Module(module);
  256. }
  257. const instance = new WebAssembly.Instance(module, imports);
  258. return __wbg_finalize_init(instance, module);
  259. }
  260. async function __wbg_init(module_or_path) {
  261. if (wasm !== undefined) return wasm;
  262. if (typeof module_or_path !== 'undefined') {
  263. if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
  264. ({module_or_path} = module_or_path)
  265. } else {
  266. console.warn('using deprecated parameters for the initialization function; pass a single object instead')
  267. }
  268. }
  269. if (typeof module_or_path === 'undefined') {
  270. module_or_path = new URL('sbpf_assembler_bg.wasm', import.meta.url);
  271. }
  272. const imports = __wbg_get_imports();
  273. if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
  274. module_or_path = fetch(module_or_path);
  275. }
  276. __wbg_init_memory(imports);
  277. const { instance, module } = await __wbg_load(await module_or_path, imports);
  278. return __wbg_finalize_init(instance, module);
  279. }
  280. export { initSync };
  281. export default __wbg_init;