@@ -11,42 +11,70 @@ export namespace bs {
1111 export let offset : usize = buffer ;
1212
1313 /** Byte length of the buffer. */
14- export let byteLength : usize = 32 ;
14+ let bufferEnd : usize = buffer + 32 ;
1515
1616 /** Proposed size of output */
17- export let realSize : usize = offset ;
17+ export let realSize : usize = buffer ;
1818
19+ /**
20+ * Byte length of the buffer
21+ * @returns usize
22+ */
23+ // @ts -ignore: decorator
24+ @inline export function byteLength ( ) : usize {
25+ return bufferEnd - buffer ;
26+ }
1927 /**
2028 * Proposes that the buffer size is should be greater than or equal to the proposed size.
2129 * If necessary, reallocates the buffer to the exact new size.
2230 * @param size - The size to propose.
2331 */
24- // @ts -ignore: Decorator valid here
32+ // @ts -ignore: decorator
33+ @inline export function ensureSize ( size : u32 ) : void {
34+ if ( offset + size > bufferEnd ) {
35+ bufferEnd += nextPowerOf2 ( size + 32 ) ;
36+ // @ts -ignore: exists
37+ const newPtr = __renew ( buffer , bufferEnd - buffer ) ;
38+ // I don't know if this is even needed. I'll need to take a look at the runtime
39+ // offset = offset - buffer + newPtr;
40+ // buffer = newPtr;
41+ }
42+ console . log ( "Ensure " + ( realSize - buffer ) . toString ( ) + " " + size . toString ( ) ) ;
43+ }
44+ /**
45+ * Proposes that the buffer size is should be greater than or equal to the proposed size.
46+ * If necessary, reallocates the buffer to the exact new size.
47+ * @param size - The size to propose.
48+ */
49+ // @ts -ignore: decorator
2550 @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 ;
31- buffer = newPtr ;
51+ realSize = offset + size ;
52+ if ( realSize > bufferEnd ) {
53+ bufferEnd += nextPowerOf2 ( size ) ;
54+ // @ts -ignore: exists
55+ const newPtr = __renew ( buffer , bufferEnd - buffer ) ;
56+ // I don't know if this is even needed. I'll need to take a look at the runtime
57+ // offset = offset - buffer + newPtr;
58+ // buffer = newPtr;
3259 }
60+ console . log ( "Propose " + ( realSize - buffer ) . toString ( ) + " " + size . toString ( ) ) ;
3361 }
3462
3563 /**
3664 * Increases the proposed size by nextPowerOf2(n + 8) if necessary.
3765 * If necessary, reallocates the buffer to the exact new size.
3866 * @param size - The size to grow by.
3967 */
40- // @ts -ignore: Decorator valid here
68+ // @ts -ignore: decorator
4169 @inline export function growSize ( size : u32 ) : void {
42- realSize += size ;
43- if ( realSize > byteLength ) {
44- byteLength += nextPowerOf2 ( size + 8 ) ;
70+ if ( ( realSize += size ) > bufferEnd ) {
71+ bufferEnd += nextPowerOf2 ( size + 32 ) ;
4572 // @ts -ignore
46- const newPtr = __renew ( buffer , byteLength ) ;
47- offset = offset - buffer + newPtr ;
48- buffer = newPtr ;
73+ const newPtr = __renew ( buffer , bufferEnd ) ;
74+ // offset = offset - buffer + newPtr;
75+ // buffer = newPtr;
4976 }
77+ console . log ( "Grow " + ( realSize - buffer ) . toString ( ) + " " + size . toString ( ) ) ;
5078 }
5179
5280 /**
@@ -55,9 +83,9 @@ export namespace bs {
5583 */
5684 // @ts -ignore: Decorator valid here
5785 @inline export function resize ( newSize : u32 ) : void {
58- // @ts -ignore
86+ // @ts -ignore: exists
5987 const newPtr = __renew ( buffer , newSize ) ;
60- byteLength = newSize ;
88+ bufferEnd = newSize ;
6189 buffer = newPtr ;
6290 offset = newPtr + newSize ;
6391 realSize = newPtr ;
@@ -70,7 +98,7 @@ export namespace bs {
7098 // @ts -ignore: Decorator valid here
7199 @inline export function out < T > ( ) : T {
72100 const len = offset - buffer ;
73- // @ts -ignore
101+ // @ts -ignore: exists
74102 const _out = __new ( len , idof < T > ( ) ) ;
75103 memory . copy ( _out , buffer , len ) ;
76104
@@ -89,7 +117,7 @@ export namespace bs {
89117 // @ts -ignore: Decorator valid here
90118 @inline export function outTo < T > ( dst : usize ) : T {
91119 const len = offset - buffer ;
92- // @ts -ignore
120+ // @ts -ignore: exists
93121 if ( len != changetype < OBJECT > ( dst - TOTAL_OVERHEAD ) . rtSize ) __renew ( len , idof < T > ( ) ) ;
94122 memory . copy ( dst , buffer , len ) ;
95123
0 commit comments