json-as 1.0.0-alpha.3 → 1.0.0-alpha.4

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.
@@ -5,46 +5,77 @@ import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
5
5
  */
6
6
  export namespace bs {
7
7
  /** Current buffer pointer. */ // @ts-ignore
8
- export let buffer: usize = __new(32, idof<ArrayBuffer>());
8
+ export let buffer: ArrayBuffer = new ArrayBuffer(32);//__new(32, idof<ArrayBuffer>());
9
9
 
10
10
  /** Current offset within the buffer. */
11
- export let offset: usize = buffer;
11
+ export let offset: usize = changetype<usize>(buffer);
12
12
 
13
13
  /** Byte length of the buffer. */
14
- export let byteLength: usize = 32;
14
+ let bufferSize: usize = 32;
15
15
 
16
16
  /** Proposed size of output */
17
- export let realSize: usize = offset;
17
+ export let stackSize: usize = 0;
18
18
 
19
19
  /**
20
20
  * Proposes that the buffer size is should be greater than or equal to the proposed size.
21
21
  * If necessary, reallocates the buffer to the exact new size.
22
22
  * @param size - The size to propose.
23
23
  */
24
- // @ts-ignore: Decorator valid here
24
+ // @ts-ignore: decorator
25
+ @inline export function ensureSize(size: u32): void {
26
+ // console.log("Ensure " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
27
+ if (offset + size > bufferSize + changetype<usize>(buffer)) {
28
+ const deltaBytes = nextPowerOf2(size + 64);
29
+ bufferSize += deltaBytes;
30
+ // @ts-ignore: exists
31
+ const newPtr = changetype<ArrayBuffer>(__renew(
32
+ changetype<usize>(buffer),
33
+ bufferSize
34
+ ));
35
+ offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
36
+ buffer = newPtr;
37
+ }
38
+ }
39
+
40
+ /**
41
+ * Proposes that the buffer size is should be greater than or equal to the proposed size.
42
+ * If necessary, reallocates the buffer to the exact new size.
43
+ * @param size - The size to propose.
44
+ */
45
+ // @ts-ignore: decorator
25
46
  @inline export function proposeSize(size: u32): void {
26
- if ((realSize = size) > byteLength) {
27
- byteLength = nextPowerOf2(size);
28
- // @ts-ignore
29
- const newPtr = __renew(buffer, byteLength);
30
- offset = offset - buffer + newPtr;
47
+ // console.log("Propose " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
48
+ if ((stackSize += size) > bufferSize) {
49
+ const deltaBytes = nextPowerOf2(size);
50
+ bufferSize += deltaBytes;
51
+ // @ts-ignore: exists
52
+ const newPtr = changetype<ArrayBuffer>(__renew(
53
+ changetype<usize>(buffer),
54
+ bufferSize
55
+ ));
56
+ offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
31
57
  buffer = newPtr;
32
58
  }
33
59
  }
34
60
 
35
61
  /**
36
- * Increases the proposed size by nextPowerOf2(n + 8) if necessary.
62
+ * Increases the proposed size by nextPowerOf2(n + 64) if necessary.
37
63
  * If necessary, reallocates the buffer to the exact new size.
38
64
  * @param size - The size to grow by.
39
65
  */
40
- // @ts-ignore: Decorator valid here
66
+ // @ts-ignore: decorator
41
67
  @inline export function growSize(size: u32): void {
42
- realSize += size;
43
- if (realSize > byteLength) {
44
- byteLength += nextPowerOf2(size + 8);
68
+ // console.log("Grow " + (stackSize).toString() + " -> " + (stackSize + size).toString() + " (" + size.toString() + ") " + (((stackSize + size) > bufferSize) ? "+" : ""));
69
+ if ((stackSize += size) > bufferSize) {
70
+ const deltaBytes = nextPowerOf2(size + 64);
71
+ bufferSize += deltaBytes;
45
72
  // @ts-ignore
46
- const newPtr = __renew(buffer, byteLength);
47
- offset = offset - buffer + newPtr;
73
+ const newPtr = changetype<ArrayBuffer>(__renew(
74
+ changetype<usize>(buffer),
75
+ bufferSize
76
+ ));
77
+ // if (buffer != newPtr) console.log(" Old: " + changetype<usize>(buffer).toString() + "\n New: " + changetype<usize>(newPtr).toString());
78
+ offset = offset + changetype<usize>(newPtr) - changetype<usize>(buffer);
48
79
  buffer = newPtr;
49
80
  }
50
81
  }
@@ -55,12 +86,12 @@ export namespace bs {
55
86
  */
56
87
  // @ts-ignore: Decorator valid here
57
88
  @inline export function resize(newSize: u32): void {
58
- // @ts-ignore
89
+ // @ts-ignore: exists
59
90
  const newPtr = __renew(buffer, newSize);
60
- byteLength = newSize;
91
+ bufferSize = newSize;
61
92
  buffer = newPtr;
62
93
  offset = newPtr + newSize;
63
- realSize = newPtr;
94
+ stackSize = newPtr;
64
95
  }
65
96
 
66
97
  /**
@@ -69,13 +100,13 @@ export namespace bs {
69
100
  */
70
101
  // @ts-ignore: Decorator valid here
71
102
  @inline export function out<T>(): T {
72
- const len = offset - buffer;
73
- // @ts-ignore
103
+ const len = offset - changetype<usize>(buffer);
104
+ // @ts-ignore: exists
74
105
  const _out = __new(len, idof<T>());
75
- memory.copy(_out, buffer, len);
106
+ memory.copy(_out, changetype<usize>(buffer), len);
76
107
 
77
- offset = buffer;
78
- realSize = buffer;
108
+ offset = changetype<usize>(buffer);
109
+ stackSize = 0;
79
110
  return changetype<T>(_out);
80
111
  }
81
112
 
@@ -88,13 +119,13 @@ export namespace bs {
88
119
  */
89
120
  // @ts-ignore: Decorator valid here
90
121
  @inline export function outTo<T>(dst: usize): T {
91
- const len = offset - buffer;
92
- // @ts-ignore
122
+ const len = offset - changetype<usize>(buffer);
123
+ // @ts-ignore: exists
93
124
  if (len != changetype<OBJECT>(dst - TOTAL_OVERHEAD).rtSize) __renew(len, idof<T>());
94
- memory.copy(dst, buffer, len);
125
+ memory.copy(dst, changetype<usize>(buffer), len);
95
126
 
96
- offset = buffer;
97
- realSize = buffer;
127
+ offset = changetype<usize>(buffer);
128
+ stackSize = 0;
98
129
  return changetype<T>(dst);
99
130
  }
100
131
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-as",
3
- "version": "1.0.0-alpha.3",
3
+ "version": "1.0.0-alpha.4",
4
4
  "author": "Jairus Tanaka",
5
5
  "repository": {
6
6
  "type": "git",
@@ -9,12 +9,12 @@
9
9
  "main": "transform/lib/index.js",
10
10
  "devDependencies": {
11
11
  "@assemblyscript/wasi-shim": "^0.1.0",
12
- "@types/node": "latest",
12
+ "@types/node": "^22.13.1",
13
13
  "as-console": "^7.0.0",
14
- "assemblyscript": "^0.27.31",
14
+ "assemblyscript": "^0.27.34",
15
15
  "assemblyscript-prettier": "^3.0.1",
16
- "prettier": "^3.4.2",
17
- "typescript": "latest"
16
+ "prettier": "^3.5.0",
17
+ "typescript": "^5.7.3"
18
18
  },
19
19
  "bugs": {
20
20
  "url": "https://github.com/JairusSW/as-json/issues"