reflect.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. // -*- js2-basic-offset: 4 -*-
  2. class Char {
  3. constructor(codepoint) {
  4. this.codepoint = codepoint;
  5. }
  6. toString() {
  7. let ch = String.fromCodePoint(this.codepoint);
  8. if (ch == '\n') return '#\\newline';
  9. if (ch == '\r') return '#\\return';
  10. if (ch.match(/[a-zA-Z0-9$[\]().]/)) return `#\\${ch}`;
  11. return `#\\x${this.codepoint.toString(16)}`;
  12. }
  13. }
  14. class Eof { toString() { return "#<eof>"; } }
  15. class Nil { toString() { return "#nil"; } }
  16. class Null { toString() { return "()"; } }
  17. class Unspecified { toString() { return "#<unspecified>"; } }
  18. class Complex {
  19. constructor(real, imag) {
  20. this.real = real;
  21. this.imag = imag;
  22. }
  23. toString() {
  24. const sign = this.imag >= 0 && Number.isFinite(this.imag) ? "+": "";
  25. return `${flonum_to_string(this.real)}${sign}${flonum_to_string(this.imag)}i`;
  26. }
  27. }
  28. class Fraction {
  29. constructor(num, denom) {
  30. this.num = num;
  31. this.denom = denom;
  32. }
  33. toString() {
  34. return `${this.num}/${this.denom}`;
  35. }
  36. }
  37. class HeapObject {
  38. constructor(reflector, obj) {
  39. this.reflector = reflector;
  40. this.obj = obj;
  41. }
  42. repr() { return this.toString(); } // Default implementation.
  43. }
  44. class Pair extends HeapObject {
  45. toString() { return "#<pair>"; }
  46. repr() {
  47. let car_repr = repr(this.reflector.car(this));
  48. let cdr_repr = repr(this.reflector.cdr(this));
  49. if (cdr_repr == '()')
  50. return `(${car_repr})`;
  51. if (cdr_repr.charAt(0) == '(')
  52. return `(${car_repr} ${cdr_repr.substring(1)}`;
  53. return `(${car_repr} . ${cdr_repr})`;
  54. }
  55. }
  56. class MutablePair extends Pair { toString() { return "#<mutable-pair>"; } }
  57. class Vector extends HeapObject {
  58. toString() { return "#<vector>"; }
  59. repr() {
  60. let len = this.reflector.vector_length(this);
  61. let out = '#(';
  62. for (let i = 0; i < len; i++) {
  63. if (i) out += ' ';
  64. out += repr(this.reflector.vector_ref(this, i));
  65. }
  66. out += ')';
  67. return out;
  68. }
  69. }
  70. class MutableVector extends Vector {
  71. toString() { return "#<mutable-vector>"; }
  72. }
  73. class Bytevector extends HeapObject {
  74. toString() { return "#<bytevector>"; }
  75. repr() {
  76. let len = this.reflector.bytevector_length(this);
  77. let out = '#vu8(';
  78. for (let i = 0; i < len; i++) {
  79. if (i) out += ' ';
  80. out += this.reflector.bytevector_ref(this, i);
  81. }
  82. out += ')';
  83. return out;
  84. }
  85. }
  86. class MutableBytevector extends Bytevector {
  87. toString() { return "#<mutable-bytevector>"; }
  88. }
  89. class Bitvector extends HeapObject {
  90. toString() { return "#<bitvector>"; }
  91. repr() {
  92. let len = this.reflector.bitvector_length(this);
  93. let out = '#*';
  94. for (let i = 0; i < len; i++) {
  95. out += this.reflector.bitvector_ref(this, i) ? '1' : '0';
  96. }
  97. return out;
  98. }
  99. }
  100. class MutableBitvector extends Bitvector {
  101. toString() { return "#<mutable-bitvector>"; }
  102. }
  103. class MutableString extends HeapObject {
  104. toString() { return "#<mutable-string>"; }
  105. repr() { return string_repr(this.reflector.string_value(this)); }
  106. }
  107. class Procedure extends HeapObject {
  108. toString() { return "#<procedure>"; }
  109. call(...arg) {
  110. return this.reflector.call(this, ...arg);
  111. }
  112. async call_async(...arg) {
  113. return await this.reflector.call_async(this, ...arg);
  114. }
  115. }
  116. class Sym extends HeapObject {
  117. toString() { return "#<symbol>"; }
  118. repr() { return this.reflector.symbol_name(this); }
  119. }
  120. class Keyword extends HeapObject {
  121. toString() { return "#<keyword>"; }
  122. repr() { return `#:${this.reflector.keyword_name(this)}`; }
  123. }
  124. class Variable extends HeapObject { toString() { return "#<variable>"; } }
  125. class AtomicBox extends HeapObject { toString() { return "#<atomic-box>"; } }
  126. class HashTable extends HeapObject { toString() { return "#<hash-table>"; } }
  127. class WeakTable extends HeapObject { toString() { return "#<weak-table>"; } }
  128. class Fluid extends HeapObject { toString() { return "#<fluid>"; } }
  129. class DynamicState extends HeapObject { toString() { return "#<dynamic-state>"; } }
  130. class Syntax extends HeapObject { toString() { return "#<syntax>"; } }
  131. class SyntaxTransformer extends HeapObject { toString() { return "#<syntax-transformer>"; } }
  132. class Port extends HeapObject { toString() { return "#<port>"; } }
  133. class Struct extends HeapObject { toString() { return "#<struct>"; } }
  134. function instantiate_streaming(path, imports) {
  135. if (typeof fetch !== 'undefined' && typeof window !== 'undefined')
  136. return WebAssembly.instantiateStreaming(fetch(path), imports);
  137. let bytes;
  138. if (typeof read !== 'undefined') {
  139. bytes = read(path, 'binary');
  140. } else if (typeof readFile !== 'undefined') {
  141. bytes = readFile(path);
  142. } else {
  143. let fs = require('fs');
  144. bytes = fs.readFileSync(path);
  145. }
  146. return WebAssembly.instantiate(bytes, imports);
  147. }
  148. class IterableWeakSet {
  149. #array;
  150. #set;
  151. constructor() { this.#array = []; this.#set = new WeakSet; }
  152. #kill(i) {
  153. let tail = this.#array.pop();
  154. if (i < this.#array.length)
  155. this.#array[i] = tail;
  156. }
  157. #get(i) {
  158. if (i >= this.#array.length)
  159. return null;
  160. let obj = this.#array[i].deref();
  161. if (obj)
  162. return obj;
  163. this.#kill(i);
  164. return null;
  165. }
  166. #cleanup() {
  167. let i = 0;
  168. while (this.#get(i)) i++;
  169. }
  170. has(x) { return this.#set.has(x); }
  171. add(x) {
  172. if (this.has(x))
  173. return;
  174. if (this.#array.length % 32 == 0)
  175. this.#cleanup();
  176. this.#set.add(x);
  177. this.#array.push(new WeakRef(x));
  178. }
  179. delete(x) {
  180. if (!this.has(x))
  181. return;
  182. this.#set.delete(x);
  183. let i = 0;
  184. while (this.#get(i) != x) i++;
  185. this.#kill(i);
  186. }
  187. *[Symbol.iterator]() {
  188. for (let i = 0, x; x = this.#get(i); i++)
  189. yield x;
  190. }
  191. }
  192. class Scheme {
  193. #instance;
  194. #abi;
  195. constructor(instance, abi) {
  196. this.#instance = instance;
  197. this.#abi = abi;
  198. }
  199. static async reflect(abi, {reflect_wasm_dir = '.'}) {
  200. let debug = {
  201. debug_str(x) { console.log(`reflect debug: ${x}`); },
  202. debug_str_i32(x, y) { console.log(`reflect debug: ${x}: ${y}`); },
  203. debug_str_scm: (x, y) => {
  204. console.log(`reflect debug: ${x}: #<scm>`);
  205. },
  206. code_source(x) { return ['???', 0, 0]; }
  207. };
  208. let reflect_wasm = reflect_wasm_dir + '/reflect.wasm';
  209. let rt = {
  210. quit(status) { throw new SchemeQuitError(status); },
  211. die(tag, data) { throw new SchemeTrapError(tag, data); },
  212. wtf8_to_string(wtf8) { return wtf8_to_string(wtf8); },
  213. string_to_wtf8(str) { return string_to_wtf8(str); },
  214. };
  215. let { module, instance } =
  216. await instantiate_streaming(reflect_wasm, { abi, debug, rt });
  217. return new Scheme(instance, abi);
  218. }
  219. #init_module(mod) {
  220. mod.set_debug_handler({
  221. debug_str(x) { console.log(`debug: ${x}`); },
  222. debug_str_i32(x, y) { console.log(`debug: ${x}: ${y}`); },
  223. debug_str_scm: (x, y) => {
  224. console.log(`debug: ${x}: ${repr(this.#to_js(y))}`);
  225. },
  226. });
  227. mod.set_ffi_handler({
  228. procedure_to_extern: (obj) => {
  229. const proc = this.#to_js(obj);
  230. return (...args) => {
  231. return proc.call(...args);
  232. };
  233. }
  234. });
  235. mod.set_finalization_handler({
  236. make_finalization_registry: (f) => new FinalizationRegistry(f),
  237. finalization_registry_register: (registry, target, heldValue) => {
  238. // heldValue is a Wasm struct and needs to be wrapped
  239. // so that when it goes back to Scheme via the
  240. // finalizer callback it is seen as a Scheme value and
  241. // not an external one.
  242. registry.register(target, this.#to_js(heldValue));
  243. },
  244. finalization_registry_register_with_token: (registry, target, heldValue, unregisterToken) => {
  245. registry.register(target, this.#to_js(heldValue), unregisterToken);
  246. },
  247. finalization_registry_unregister: (registry, unregisterToken) => {
  248. return registry.unregister(unregisterToken);
  249. }
  250. });
  251. let proc = new Procedure(this, mod.get_export('$load').value);
  252. return proc.call();
  253. }
  254. static async load_main(path, opts = {}) {
  255. let mod = await SchemeModule.fetch_and_instantiate(path, opts);
  256. let reflect = await mod.reflect(opts);
  257. return reflect.#init_module(mod);
  258. }
  259. async load_extension(path, opts = {}) {
  260. opts = Object.assign({ abi: this.#abi }, opts);
  261. let mod = await SchemeModule.fetch_and_instantiate(path, opts);
  262. return this.#init_module(mod);
  263. }
  264. #to_scm(js) {
  265. let api = this.#instance.exports;
  266. if (typeof(js) == 'number') {
  267. return api.scm_from_f64(js);
  268. } else if (typeof(js) == 'bigint') {
  269. if (BigInt(api.scm_most_negative_fixnum()) <= js
  270. && js <= BigInt(api.scm_most_positive_fixnum()))
  271. return api.scm_from_fixnum(Number(js));
  272. return api.scm_from_bignum(js);
  273. } else if (typeof(js) == 'boolean') {
  274. return js ? api.scm_true() : api.scm_false();
  275. } else if (typeof(js) == 'string') {
  276. return api.scm_from_string(js);
  277. } else if (typeof(js) == 'object') {
  278. if (js instanceof Eof) return api.scm_eof();
  279. if (js instanceof Nil) return api.scm_nil();
  280. if (js instanceof Null) return api.scm_null();
  281. if (js instanceof Unspecified) return api.scm_unspecified();
  282. if (js instanceof Char) return api.scm_from_char(js.codepoint);
  283. if (js instanceof HeapObject) return js.obj;
  284. if (js instanceof Fraction)
  285. return api.scm_from_fraction(this.#to_scm(js.num),
  286. this.#to_scm(js.denom));
  287. if (js instanceof Complex)
  288. return api.scm_from_complex(js.real, js.imag);
  289. return api.scm_from_extern(js);
  290. } else if (typeof(js) == 'function') {
  291. return api.scm_from_extern(js);
  292. } else {
  293. throw new Error(`unexpected; ${typeof(js)}`);
  294. }
  295. }
  296. #to_js(scm) {
  297. let api = this.#instance.exports;
  298. let descr = api.describe(scm);
  299. let handlers = {
  300. fixnum: () => BigInt(api.fixnum_value(scm)),
  301. char: () => new Char(api.char_value(scm)),
  302. true: () => true,
  303. false: () => false,
  304. eof: () => new Eof,
  305. nil: () => new Nil,
  306. null: () => new Null,
  307. unspecified: () => new Unspecified,
  308. flonum: () => api.flonum_value(scm),
  309. bignum: () => api.bignum_value(scm),
  310. complex: () => new Complex(api.complex_real(scm),
  311. api.complex_imag(scm)),
  312. fraction: () => new Fraction(this.#to_js(api.fraction_num(scm)),
  313. this.#to_js(api.fraction_denom(scm))),
  314. pair: () => new Pair(this, scm),
  315. 'mutable-pair': () => new MutablePair(this, scm),
  316. vector: () => new Vector(this, scm),
  317. 'mutable-vector': () => new MutableVector(this, scm),
  318. bytevector: () => new Bytevector(this, scm),
  319. 'mutable-bytevector': () => new MutableBytevector(this, scm),
  320. bitvector: () => new Bitvector(this, scm),
  321. 'mutable-bitvector': () => new MutableBitvector(this, scm),
  322. string: () => api.string_value(scm),
  323. 'mutable-string': () => new MutableString(this, scm),
  324. procedure: () => new Procedure(this, scm),
  325. symbol: () => new Sym(this, scm),
  326. keyword: () => new Keyword(this, scm),
  327. variable: () => new Variable(this, scm),
  328. 'atomic-box': () => new AtomicBox(this, scm),
  329. 'hash-table': () => new HashTable(this, scm),
  330. 'weak-table': () => new WeakTable(this, scm),
  331. fluid: () => new Fluid(this, scm),
  332. 'dynamic-state': () => new DynamicState(this, scm),
  333. syntax: () => new Syntax(this, scm),
  334. 'syntax-transformer': () => new SyntaxTransformer(this, scm),
  335. port: () => new Port(this, scm),
  336. struct: () => new Struct(this, scm),
  337. 'extern-ref': () => api.extern_value(scm)
  338. };
  339. let handler = handlers[descr];
  340. return handler ? handler() : scm;
  341. }
  342. call(func, ...args) {
  343. let api = this.#instance.exports;
  344. let argv = api.make_vector(args.length + 1, api.scm_false());
  345. func = this.#to_scm(func);
  346. api.vector_set(argv, 0, func);
  347. for (let [idx, arg] of args.entries())
  348. api.vector_set(argv, idx + 1, this.#to_scm(arg));
  349. argv = api.call(func, argv);
  350. let results = [];
  351. for (let idx = 0; idx < api.vector_length(argv); idx++)
  352. results.push(this.#to_js(api.vector_ref(argv, idx)))
  353. return results;
  354. }
  355. call_async(func, ...args) {
  356. return new Promise((resolve, reject) => {
  357. this.call(func,
  358. val => resolve(this.#to_js(val)),
  359. err => reject(this.#to_js(err)),
  360. ...args);
  361. })
  362. }
  363. car(x) { return this.#to_js(this.#instance.exports.car(x.obj)); }
  364. cdr(x) { return this.#to_js(this.#instance.exports.cdr(x.obj)); }
  365. vector_length(x) { return this.#instance.exports.vector_length(x.obj); }
  366. vector_ref(x, i) {
  367. return this.#to_js(this.#instance.exports.vector_ref(x.obj, i));
  368. }
  369. bytevector_length(x) {
  370. return this.#instance.exports.bytevector_length(x.obj);
  371. }
  372. bytevector_ref(x, i) {
  373. return this.#instance.exports.bytevector_ref(x.obj, i);
  374. }
  375. bitvector_length(x) {
  376. return this.#instance.exports.bitvector_length(x.obj);
  377. }
  378. bitvector_ref(x, i) {
  379. return this.#instance.exports.bitvector_ref(x.obj, i) == 1;
  380. }
  381. string_value(x) { return this.#instance.exports.string_value(x.obj); }
  382. symbol_name(x) { return this.#instance.exports.symbol_name(x.obj); }
  383. keyword_name(x) { return this.#instance.exports.keyword_name(x.obj); }
  384. }
  385. class SchemeTrapError extends Error {
  386. constructor(tag, data) { super(); this.tag = tag; this.data = data; }
  387. // FIXME: data is raw Scheme object; would need to be reflected to
  388. // have a toString.
  389. toString() { return `SchemeTrap(${this.tag}, <data>)`; }
  390. }
  391. class SchemeQuitError extends Error {
  392. constructor(status) { super(); this.status = status; }
  393. toString() { return `SchemeQuit(status=${this.status})`; }
  394. }
  395. function string_repr(str) {
  396. // FIXME: Improve to match Scheme.
  397. return '"' + str.replace(/(["\\])/g, '\\$1').replace(/\n/g, '\\n') + '"';
  398. }
  399. function flonum_to_string(f64) {
  400. if (Object.is(f64, -0)) {
  401. return '-0.0';
  402. } else if (Number.isFinite(f64)) {
  403. let repr = f64 + '';
  404. return /^-?[0-9]+$/.test(repr) ? repr + '.0' : repr;
  405. } else if (Number.isNaN(f64)) {
  406. return '+nan.0';
  407. } else {
  408. return f64 < 0 ? '-inf.0' : '+inf.0';
  409. }
  410. }
  411. let async_invoke = typeof queueMicrotask !== 'undefined'
  412. ? queueMicrotask
  413. : thunk => setTimeout(thunk, 0);
  414. function async_invoke_later(thunk, jiffies) {
  415. setTimeout(thunk, jiffies / 1000);
  416. }
  417. let wtf8_helper;
  418. function wtf8_to_string(wtf8) {
  419. let { as_iter, iter_next } = wtf8_helper.exports;
  420. let codepoints = [];
  421. let iter = as_iter(wtf8);
  422. for (let cp = iter_next(iter); cp != -1; cp = iter_next(iter))
  423. codepoints.push(cp);
  424. // Passing too many codepoints can overflow the stack.
  425. let maxcp = 100000;
  426. if (codepoints.length <= maxcp) {
  427. return String.fromCodePoint(...codepoints);
  428. }
  429. // For converting large strings, concatenate several smaller
  430. // strings.
  431. let substrings = [];
  432. let end = 0;
  433. for (let start = 0; start != codepoints.length; start = end) {
  434. end = Math.min(start + maxcp, codepoints.length);
  435. substrings.push(String.fromCodePoint(...codepoints.slice(start, end)));
  436. }
  437. return substrings.join('');
  438. }
  439. function string_to_wtf8(str) {
  440. let { string_builder, builder_push_codepoint, finish_builder } =
  441. wtf8_helper.exports;
  442. let builder = string_builder()
  443. for (let cp of str)
  444. builder_push_codepoint(builder, cp.codePointAt(0));
  445. return finish_builder(builder);
  446. }
  447. async function load_wtf8_helper_module(reflect_wasm_dir = '') {
  448. if (wtf8_helper) return;
  449. let wtf8_wasm = reflect_wasm_dir + "/wtf8.wasm";
  450. let { module, instance } = await instantiate_streaming(wtf8_wasm);
  451. wtf8_helper = instance;
  452. }
  453. function make_textual_writable_stream(write_chars) {
  454. const decoder = new TextDecoder("utf-8");
  455. return new WritableStream({
  456. write(chunk) {
  457. return new Promise((resolve, reject) => {
  458. write_chars(decoder.decode(chunk, { stream: true }));
  459. resolve();
  460. });
  461. }
  462. });
  463. }
  464. class SchemeModule {
  465. #instance;
  466. #io_handler;
  467. #debug_handler;
  468. #ffi_handler;
  469. #finalization_handler;
  470. static #rt = {
  471. bignum_from_string(str) { return BigInt(str); },
  472. bignum_from_i32(n) { return BigInt(n); },
  473. bignum_from_i64(n) { return n; },
  474. bignum_from_u64(n) { return n < 0n ? 0xffff_ffff_ffff_ffffn + (n + 1n) : n; },
  475. bignum_is_i64(n) {
  476. return -0x8000_0000_0000_0000n <= n && n <= 0x7FFF_FFFF_FFFF_FFFFn;
  477. },
  478. bignum_is_u64(n) {
  479. return 0n <= n && n <= 0xFFFF_FFFF_FFFF_FFFFn;
  480. },
  481. // This truncates; see https://tc39.es/ecma262/#sec-tobigint64.
  482. bignum_get_i64(n) { return n; },
  483. bignum_add(a, b) { return BigInt(a) + BigInt(b) },
  484. bignum_sub(a, b) { return BigInt(a) - BigInt(b) },
  485. bignum_mul(a, b) { return BigInt(a) * BigInt(b) },
  486. bignum_lsh(a, b) { return BigInt(a) << BigInt(b) },
  487. bignum_rsh(a, b) { return BigInt(a) >> BigInt(b) },
  488. bignum_quo(a, b) { return BigInt(a) / BigInt(b) },
  489. bignum_rem(a, b) { return BigInt(a) % BigInt(b) },
  490. bignum_mod(a, b) {
  491. let r = BigInt(a) % BigInt(b);
  492. if ((b > 0n && r < 0n) || (b < 0n && r > 0n)) {
  493. return b + r;
  494. } else {
  495. return r;
  496. }
  497. },
  498. bignum_gcd(a, b) {
  499. a = BigInt(a);
  500. b = BigInt(b);
  501. if (a < 0n) { a = -a; }
  502. if (b < 0n) { b = -b; }
  503. if (a == 0n) { return b; }
  504. if (b == 0n) { return a; }
  505. let r;
  506. while (b != 0n) {
  507. r = a % b;
  508. a = b;
  509. b = r;
  510. }
  511. return a;
  512. },
  513. bignum_logand(a, b) { return BigInt(a) & BigInt(b); },
  514. bignum_logior(a, b) { return BigInt(a) | BigInt(b); },
  515. bignum_logxor(a, b) { return BigInt(a) ^ BigInt(b); },
  516. bignum_lt(a, b) { return a < b; },
  517. bignum_le(a, b) { return a <= b; },
  518. bignum_eq(a, b) { return a == b; },
  519. bignum_to_f64(n) { return Number(n); },
  520. flonum_to_string,
  521. string_upcase: Function.call.bind(String.prototype.toUpperCase),
  522. string_downcase: Function.call.bind(String.prototype.toLowerCase),
  523. make_weak_ref(x) { return new WeakRef(x); },
  524. weak_ref_deref(ref, fail) {
  525. const val = ref.deref();
  526. return val === undefined ? fail: val;
  527. },
  528. make_weak_map() { return new WeakMap; },
  529. weak_map_get(map, k, fail) {
  530. const val = map.get(k);
  531. return val === undefined ? fail: val;
  532. },
  533. weak_map_set(map, k, v) { return map.set(k, v); },
  534. weak_map_delete(map, k) { return map.delete(k); },
  535. fsqrt: Math.sqrt,
  536. fsin: Math.sin,
  537. fcos: Math.cos,
  538. ftan: Math.tan,
  539. fasin: Math.asin,
  540. facos: Math.acos,
  541. fatan: Math.atan,
  542. fatan2: Math.atan2,
  543. flog: Math.log,
  544. fexp: Math.exp,
  545. jiffies_per_second() { return 1000000; },
  546. current_jiffy() { return performance.now() * 1000; },
  547. current_second() { return Date.now() / 1000; },
  548. async_invoke,
  549. async_invoke_later,
  550. promise_on_completed(p, kt, kf) {
  551. p.then((val) => {
  552. if (val === undefined) {
  553. kt(false);
  554. } else {
  555. kt(val);
  556. }
  557. }, kf);
  558. },
  559. promise_complete(callback, val) { callback(val); },
  560. // Wrap in functions to allow for lazy loading of the wtf8
  561. // module.
  562. wtf8_to_string(wtf8) { return wtf8_to_string(wtf8); },
  563. string_to_wtf8(str) { return string_to_wtf8(str); },
  564. make_regexp(pattern, flags) { return new RegExp(pattern, flags); },
  565. regexp_exec(re, str) { return re.exec(str); },
  566. regexp_match_string(m) { return m.input; },
  567. regexp_match_start(m) { return m.index; },
  568. regexp_match_end(m) { return m.index + m[0].length; },
  569. regexp_match_count(m) { return m.length; },
  570. regexp_match_substring(m, i) {
  571. const str = m[i];
  572. if (str === undefined) {
  573. return null;
  574. }
  575. return str;
  576. },
  577. die(tag, data) { throw new SchemeTrapError(tag, data); },
  578. quit(status) { throw new SchemeQuitError(status); },
  579. stream_make_chunk(len) { return new Uint8Array(len); },
  580. stream_chunk_length(chunk) { return chunk.length; },
  581. stream_chunk_ref(chunk, idx) { return chunk[idx]; },
  582. stream_chunk_set(chunk, idx, val) { chunk[idx] = val; },
  583. stream_get_reader(stream) { return stream.getReader(); },
  584. stream_read(reader) { return reader.read(); },
  585. stream_result_chunk(result) { return result.value; },
  586. stream_result_done(result) { return result.done ? 1 : 0; },
  587. stream_get_writer(stream) { return stream.getWriter(); },
  588. stream_write(writer, chunk) { return writer.write(chunk); },
  589. stream_close_writer(writer) { return writer.close(); },
  590. };
  591. static #code_origins = new WeakMap;
  592. static #all_modules = new IterableWeakSet;
  593. static #code_origin(code) {
  594. if (SchemeModule.#code_origins.has(code))
  595. return SchemeModule.#code_origins.get(code);
  596. for (let mod of SchemeModule.#all_modules) {
  597. for (let i = 0, x = null; x = mod.instance_code(i); i++) {
  598. let origin = [mod, i];
  599. if (!SchemeModule.#code_origins.has(x))
  600. SchemeModule.#code_origins.set(x, origin);
  601. if (x === code)
  602. return origin;
  603. }
  604. }
  605. return [null, 0];
  606. }
  607. static #code_name(code) {
  608. let [mod, idx] = SchemeModule.#code_origin(code);
  609. if (mod)
  610. return mod.instance_code_name(idx);
  611. return null;
  612. }
  613. static #code_source(code) {
  614. let [mod, idx] = SchemeModule.#code_origin(code);
  615. if (mod)
  616. return mod.instance_code_source(idx);
  617. return [null, 0, 0];
  618. }
  619. constructor(instance) {
  620. SchemeModule.#all_modules.add(this);
  621. this.#instance = instance;
  622. let open_file_error = (filename) => {
  623. throw new Error('No file system access');
  624. };
  625. if (typeof printErr === 'function') { // v8/sm dev console
  626. // On the console, try to use 'write' (v8) or 'putstr' (sm),
  627. // as these don't add an extraneous newline. Unfortunately
  628. // JSC doesn't have a printer that doesn't add a newline.
  629. let write_no_newline =
  630. typeof write === 'function' ? write
  631. : typeof putstr === 'function' ? putstr : print;
  632. // Use readline when available. v8 strips newlines so
  633. // we need to add them back.
  634. let read_stdin =
  635. typeof readline == 'function' ? () => {
  636. let line = readline();
  637. if (line) {
  638. return `${line}\n`;
  639. } else {
  640. return '\n';
  641. }
  642. }: () => '';
  643. this.#io_handler = {
  644. write_stdout: write_no_newline,
  645. write_stderr: printErr,
  646. read_stdin,
  647. file_exists: (filename) => false,
  648. open_input_file: open_file_error,
  649. open_output_file: open_file_error,
  650. close_file: () => undefined,
  651. read_file: (handle, length) => 0,
  652. write_file: (handle, length) => 0,
  653. seek_file: (handle, offset, whence) => -1,
  654. file_random_access: (handle) => false,
  655. file_buffer_size: (handle) => 0,
  656. file_buffer_ref: (handle, i) => 0,
  657. file_buffer_set: (handle, i, x) => undefined,
  658. delete_file: (filename) => undefined,
  659. // FIXME: We should polyfill these out.
  660. stream_stdin() { throw new Error('stream_stdin not implemented'); },
  661. stream_stdout() { throw new Error('stream_stderr not implemented'); },
  662. stream_stderr() { throw new Error('stream_stderr not implemented'); },
  663. };
  664. } else if (typeof window !== 'undefined') { // web browser
  665. this.#io_handler = {
  666. write_stdout: console.log,
  667. write_stderr: console.error,
  668. read_stdin: () => '',
  669. file_exists: (filename) => false,
  670. open_input_file: open_file_error,
  671. open_output_file: open_file_error,
  672. close_file: () => undefined,
  673. read_file: (handle, length) => 0,
  674. write_file: (handle, length) => 0,
  675. seek_file: (handle, offset, whence) => -1,
  676. file_random_access: (handle) => false,
  677. file_buffer_size: (handle) => 0,
  678. file_buffer_ref: (handle, i) => 0,
  679. file_buffer_set: (handle, i, x) => undefined,
  680. delete_file: (filename) => undefined,
  681. stream_stdin() { return new ReadableStream; },
  682. stream_stdout() {
  683. return make_textual_writable_stream(s => console.log(s));
  684. },
  685. stream_stderr() {
  686. return make_textual_writable_stream(s => console.error(s));
  687. },
  688. };
  689. } else { // nodejs
  690. const fs = require('fs');
  691. const process = require('process');
  692. const { ReadableStream, WritableStream } = require('node:stream/web');
  693. const bufLength = 1024;
  694. const stdinBuf = Buffer.alloc(bufLength);
  695. const SEEK_SET = 0, SEEK_CUR = 1, SEEK_END = 2;
  696. this.#io_handler = {
  697. write_stdout: process.stdout.write.bind(process.stdout),
  698. write_stderr: process.stderr.write.bind(process.stderr),
  699. read_stdin: () => {
  700. let n = fs.readSync(process.stdin.fd, stdinBuf, 0, stdinBuf.length);
  701. return stdinBuf.toString('utf8', 0, n);
  702. },
  703. file_exists: fs.existsSync.bind(fs),
  704. open_input_file: (filename) => {
  705. let fd = fs.openSync(filename, 'r');
  706. return {
  707. fd,
  708. buf: Buffer.alloc(bufLength),
  709. pos: 0
  710. };
  711. },
  712. open_output_file: (filename) => {
  713. let fd = fs.openSync(filename, 'w');
  714. return {
  715. fd,
  716. buf: Buffer.alloc(bufLength),
  717. pos: 0
  718. };
  719. },
  720. close_file: (handle) => {
  721. fs.closeSync(handle.fd);
  722. },
  723. read_file: (handle, count) => {
  724. const n = fs.readSync(handle.fd, handle.buf, 0, count, handle.pos);
  725. handle.pos += n;
  726. return n;
  727. },
  728. write_file: (handle, count) => {
  729. const n = fs.writeSync(handle.fd, handle.buf, 0, count, handle.pos);
  730. handle.pos += n;
  731. return n;
  732. },
  733. seek_file: (handle, offset, whence) => {
  734. // There doesn't seem to be a way to ask NodeJS if
  735. // a position is valid or not.
  736. if (whence == SEEK_SET) {
  737. handle.pos = offset;
  738. return handle.pos;
  739. } else if (whence == SEEK_CUR) {
  740. handle.pos += offset;
  741. return handle.pos;
  742. }
  743. // SEEK_END not supported.
  744. return -1;
  745. },
  746. file_random_access: (handle) => {
  747. return true;
  748. },
  749. file_buffer_size: (handle) => {
  750. return handle.buf.length;
  751. },
  752. file_buffer_ref: (handle, i) => {
  753. return handle.buf[i];
  754. },
  755. file_buffer_set: (handle, i, x) => {
  756. handle.buf[i] = x;
  757. },
  758. delete_file: fs.rmSync.bind(fs),
  759. stream_stdin() {
  760. return new ReadableStream({
  761. async start(controller) {
  762. for await (const chunk of process.stdin) {
  763. controller.enqueue(chunk);
  764. }
  765. controller.close();
  766. }
  767. });
  768. },
  769. stream_stdout() {
  770. return make_textual_writable_stream(s => process.stdout.write(s));
  771. },
  772. stream_stderr() {
  773. return make_textual_writable_stream(s => process.stderr.write(s));
  774. },
  775. };
  776. }
  777. this.#debug_handler = {
  778. debug_str(x) { console.log(`debug: ${x}`); },
  779. debug_str_i32(x, y) { console.log(`debug: ${x}: ${y}`); },
  780. debug_str_scm(x, y) { console.log(`debug: ${x}: #<scm>`); },
  781. };
  782. }
  783. static async fetch_and_instantiate(path, { abi, reflect_wasm_dir = '.',
  784. user_imports = {} }) {
  785. await load_wtf8_helper_module(reflect_wasm_dir);
  786. let io = {
  787. write_stdout(str) { mod.#io_handler.write_stdout(str); },
  788. write_stderr(str) { mod.#io_handler.write_stderr(str); },
  789. read_stdin() { return mod.#io_handler.read_stdin(); },
  790. file_exists(filename) { return mod.#io_handler.file_exists(filename); },
  791. open_input_file(filename) { return mod.#io_handler.open_input_file(filename); },
  792. open_output_file(filename) { return mod.#io_handler.open_output_file(filename); },
  793. close_file(handle) { mod.#io_handler.close_file(handle); },
  794. read_file(handle, length) { return mod.#io_handler.read_file(handle, length); },
  795. write_file(handle, length) { return mod.#io_handler.write_file(handle, length); },
  796. seek_file(handle, offset, whence) { return mod.#io_handler.seek_file(handle, offset, whence); },
  797. file_random_access(handle) { return mod.#io_handler.file_random_access(handle); },
  798. file_buffer_size(handle) { return mod.#io_handler.file_buffer_size(handle); },
  799. file_buffer_ref(handle, i) { return mod.#io_handler.file_buffer_ref(handle, i); },
  800. file_buffer_set(handle, i, x) { return mod.#io_handler.file_buffer_set(handle, i, x); },
  801. delete_file(filename) { mod.#io_handler.delete_file(filename); },
  802. stream_stdin() { return mod.#io_handler.stream_stdin(); },
  803. stream_stdout() { return mod.#io_handler.stream_stdout(); },
  804. stream_stderr() { return mod.#io_handler.stream_stderr(); },
  805. };
  806. let debug = {
  807. debug_str(x) { mod.#debug_handler.debug_str(x); },
  808. debug_str_i32(x, y) { mod.#debug_handler.debug_str_i32(x, y); },
  809. debug_str_scm(x, y) { mod.#debug_handler.debug_str_scm(x, y); },
  810. code_name(code) { return SchemeModule.#code_name(code); },
  811. code_source(code) { return SchemeModule.#code_source(code); },
  812. }
  813. let ffi = {
  814. procedure_to_extern(proc) {
  815. return mod.#ffi_handler.procedure_to_extern(proc);
  816. }
  817. };
  818. let finalization = {
  819. make_finalization_registry(f) {
  820. return mod.#finalization_handler.make_finalization_registry(f);
  821. },
  822. finalization_registry_register(registry, target, heldValue) {
  823. mod.#finalization_handler.finalization_registry_register(registry, target, heldValue);
  824. },
  825. finalization_registry_register_with_token(registry, target, heldValue, unregisterToken) {
  826. mod.#finalization_handler.finalization_registry_register_with_token(registry, target, heldValue, unregisterToken);
  827. },
  828. finalization_registry_unregister(registry, unregisterToken) {
  829. return mod.#finalization_handler.finalization_registry_unregister(registry, unregisterToken);
  830. }
  831. };
  832. let imports = {
  833. rt: SchemeModule.#rt,
  834. abi, debug, io, ffi, finalization, ...user_imports
  835. };
  836. let { module, instance } = await instantiate_streaming(path, imports);
  837. let mod = new SchemeModule(instance);
  838. return mod;
  839. }
  840. set_io_handler(h) { this.#io_handler = h; }
  841. set_debug_handler(h) { this.#debug_handler = h; }
  842. set_ffi_handler(h) { this.#ffi_handler = h; }
  843. set_finalization_handler(h) { this.#finalization_handler = h; }
  844. all_exports() { return this.#instance.exports; }
  845. exported_abi() {
  846. let abi = {}
  847. for (let [k, v] of Object.entries(this.all_exports())) {
  848. if (k.startsWith("$"))
  849. abi[k] = v;
  850. }
  851. return abi;
  852. }
  853. exports() {
  854. let ret = {}
  855. for (let [k, v] of Object.entries(this.all_exports())) {
  856. if (!k.startsWith("$"))
  857. ret[k] = v;
  858. }
  859. return ret;
  860. }
  861. get_export(name) {
  862. if (name in this.all_exports())
  863. return this.all_exports()[name];
  864. throw new Error(`unknown export: ${name}`)
  865. }
  866. instance_code(idx) {
  867. if ('%instance-code' in this.all_exports()) {
  868. return this.all_exports()['%instance-code'](idx);
  869. }
  870. return null;
  871. }
  872. instance_code_name(idx) {
  873. if ('%instance-code-name' in this.all_exports()) {
  874. return this.all_exports()['%instance-code-name'](idx);
  875. }
  876. return null;
  877. }
  878. instance_code_source(idx) {
  879. if ('%instance-code-source' in this.all_exports()) {
  880. return this.all_exports()['%instance-code-source'](idx);
  881. }
  882. return [null, 0, 0];
  883. }
  884. async reflect(opts = {}) {
  885. return await Scheme.reflect(this.exported_abi(), opts);
  886. }
  887. }
  888. function repr(obj) {
  889. if (obj instanceof HeapObject)
  890. return obj.repr();
  891. if (typeof obj === 'boolean')
  892. return obj ? '#t' : '#f';
  893. if (typeof obj === 'number')
  894. return flonum_to_string(obj);
  895. if (typeof obj === 'string')
  896. return string_repr(obj);
  897. return obj + '';
  898. }
  899. // Modulize when possible.
  900. if (typeof exports !== 'undefined') {
  901. exports.Scheme = Scheme;
  902. exports.SchemeQuitError = SchemeQuitError;
  903. exports.repr = repr;
  904. }