teptris 0.3.22 → 0.3.23

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +23 -8
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "teptris",
3
- "version": "0.3.22",
3
+ "version": "0.3.23",
4
4
  "description": "TOML for JavaScript/TypeScript at libteptris speed (WebAssembly, no fallback) - version tracks the engine ({engine}.{iteration}; npm semver is 3-part)",
5
5
  "type": "module",
6
6
  "main": "src/index.js",
package/src/index.js CHANGED
@@ -10,6 +10,21 @@ function cstr(mod, s) {
10
10
  return ptr;
11
11
  }
12
12
 
13
+ // The len-bounded fast path: TextEncoder.encodeInto writes UTF-8
14
+ // STRAIGHT into the wasm heap with the native encoder, replacing
15
+ // emscripten's two JS-loop passes (lengthBytesUTF8 + stringToUTF8) -
16
+ // the measured biggest JS-side cost of the loads crossing. Only for
17
+ // entry points whose C side takes an explicit length (no NUL needed);
18
+ // NUL-terminated destinations keep cstr.
19
+ const utf8Encoder = new TextEncoder();
20
+
21
+ function cstrLen(mod, s) {
22
+ const cap = s.length * 3 + 4; /* UTF-8 worst case: 3 bytes/unit */
23
+ const ptr = mod._teptris_wasm_alloc(cap);
24
+ const { written } = utf8Encoder.encodeInto(s, mod.HEAPU8.subarray(ptr, ptr + cap));
25
+ return { ptr, len: written };
26
+ }
27
+
13
28
  function freeCstr(mod, ptr) { mod._teptris_wasm_free(ptr); }
14
29
 
15
30
  // RFC 3339-ish (the four TOML shapes) back into builder fields
@@ -148,8 +163,8 @@ export function initApi(mod) {
148
163
  if (typeof toml !== "string") {
149
164
  throw new TypeError("loads expects a string");
150
165
  }
151
- const ptr = cstr(mod, toml);
152
- const st = mod._teptris_wasm_load(ptr, lengthBytesUTF8(toml));
166
+ const { ptr, len } = cstrLen(mod, toml);
167
+ const st = mod._teptris_wasm_load(ptr, len);
153
168
  let value = null, error = null;
154
169
  if (st === 0) {
155
170
  const jptr = mod._teptris_wasm_json();
@@ -428,8 +443,8 @@ export function initApi(mod) {
428
443
  if (typeof toml !== "string") {
429
444
  throw new TypeError("loadsDoc expects a string");
430
445
  }
431
- const ptr = cstr(mod, toml);
432
- const st = mod._teptris_wasm_load_doc(ptr, lengthBytesUTF8(toml));
446
+ const { ptr, len } = cstrLen(mod, toml);
447
+ const st = mod._teptris_wasm_load_doc(ptr, len);
433
448
  freeCstr(mod, ptr);
434
449
  if (st !== 0) {
435
450
  docHeld = false;
@@ -567,8 +582,8 @@ export function initApi(mod) {
567
582
  if (typeof toml !== "string") {
568
583
  throw new TypeError("planWalk expects a TOML string");
569
584
  }
570
- const p = cstr(mod, toml);
571
- const st = mod._teptris_wasm_plan_walk(plan, p, lengthBytesUTF8(toml));
585
+ const { ptr: p, len: plen } = cstrLen(mod, toml);
586
+ const st = mod._teptris_wasm_plan_walk(plan, p, plen);
572
587
  freeCstr(mod, p);
573
588
  if (st !== 0) {
574
589
  throw planError();
@@ -594,11 +609,11 @@ export function initApi(mod) {
594
609
  throw new TypeError("edit expects three strings");
595
610
  }
596
611
  }
597
- const sp = cstr(mod, source);
612
+ const { ptr: sp, len: slen } = cstrLen(mod, source);
598
613
  const kp = cstr(mod, keyPath);
599
614
  const rp = cstr(mod, replacement);
600
615
  let out = null, error = null;
601
- const st = mod._teptris_wasm_edit(sp, lengthBytesUTF8(source), kp, rp);
616
+ const st = mod._teptris_wasm_edit(sp, slen, kp, rp);
602
617
  if (st === 0) {
603
618
  const optr = mod._teptris_wasm_json();
604
619
  const olen = mod._teptris_wasm_json_len();