summaryrefslogtreecommitdiff
path: root/prelude.js
blob: 11ad08448f4033cb94d1de5ca63720ecce981d72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// The prelude defines basic functionality
// that is expected by the Taler wallet core JavaScript,
// but not provided by quickjs or the "tart" module directly.

import * as os from "os";
import * as tart from "tart";

class TextEncoder {
  encode(str) {
    return new Uint8Array(tart.encodeUtf8(str));
  }
}

class TextDecoder {
  decode(bytes) {
    if (ArrayBuffer.isView(bytes)) {
      return tart.decodeUtf8(bytes.buffer, bytes.byteOffset, bytes.byteLength);
    }
    // Assume it is an ArrayBuffer
    return tart.decodeUtf8(bytes, 0, bytes.byteLength);
  }
}

globalThis.TextEncoder = TextEncoder;
globalThis.TextDecoder = TextDecoder;
globalThis.setTimeout = (f, t) => os.setTimeout(f, t);
globalThis.clearTimeout = (h) => os.clearTimeout(h);
globalThis.setImmediate = (f) => os.setTimeout(f, 0);

// FIXME: log to the right streams!
console.info = (...args) => { console.log(...args); };
console.warn = (...args) => { console.log(...args); };
console.error = (...args) => { console.log(...args); };
console.assert = (b) => { if (!b) throw Error("assertion failed") };

globalThis._tart = tart;