diff --git a/README.md b/README.md
index 096eb77..818710e 100644
--- a/README.md
+++ b/README.md
@@ -1,10 +1,12 @@
- __ _____ _____ _____ _____ _____
- __| || __|| || | | ___ | _ || __|
-| | ||__ || | || | | ||___|| ||__ |
-|_____||_____||_____||_|___| |__|__||_____|
-v0.9.26
-
+
+ ██ ███████ ██████ ███ ██ █████ ███████
+ ██ ██ ██ ██ ████ ██ ██ ██ ██
+ ██ ███████ ██ ██ ██ ██ ██ █████ ███████ ███████
+██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
+ █████ ███████ ██████ ██ ████ ██ ██ ███████
+ AssemblyScript - v1.0.0
+
## Installation
diff --git a/assembly/custom/bs.ts b/assembly/custom/bs.ts
index 101dfb0..b2f81e8 100644
--- a/assembly/custom/bs.ts
+++ b/assembly/custom/bs.ts
@@ -1,17 +1,25 @@
-import { bytes } from "../util/bytes";
-import { nextPowerOf2 } from "../util/nextPowerOf2";
+import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
let maxOffset: usize = __new(0, idof());
/**
- * This serves as the central buffer
+ * Central buffer namespace for managing memory operations.
*/
export namespace bs {
+ /** Current buffer pointer. */
export let buffer: usize = maxOffset;
+
+ /** Current offset within the buffer. */
export let offset: usize = maxOffset;
+
+ /** Byte length of the buffer. */
export let byteLength: usize = 0;
- // @ts-ignore
+ /**
+ * Sets the buffer to a given data object and initializes related properties.
+ * @param data - The data object to set as the buffer.
+ */
+ // @ts-ignore: Decorator valid here
@inline export function setBuffer(data: T): void {
buffer = changetype(data);
offset = changetype(data);
@@ -19,7 +27,12 @@ export namespace bs {
maxOffset = byteLength + buffer;
}
- // @ts-ignore
+ /**
+ * Ensures the buffer has sufficient capacity for a given size.
+ * If necessary, reallocates the buffer to accommodate the new size.
+ * @param size - The size to ensure capacity for.
+ */
+ // @ts-ignore: Decorator valid here
@inline export function ensureCapacity(size: u32): void {
const newSize = offset + size;
if (newSize > maxOffset) {
@@ -30,9 +43,13 @@ export namespace bs {
}
}
- // @ts-ignore
+ /**
+ * Ensures the buffer size is sufficient for a given size.
+ * If necessary, reallocates the buffer to the exact new size.
+ * @param size - The size to ensure.
+ */
+ // @ts-ignore: Decorator valid here
@inline export function ensureSize(size: u32): void {
- console.log("Alloc: " + size.toString())
const newSize = offset + size;
if (newSize > maxOffset) {
const newPtr = __renew(buffer, (byteLength = newSize - buffer));
@@ -42,27 +59,109 @@ export namespace bs {
}
}
- // @ts-ignore
+ /**
+ * Resizes the buffer to the specified size.
+ * @param newSize - The new buffer size.
+ */
+ // @ts-ignore: Decorator valid here
+ @inline export function resize(newSize: u32): void {
+ const newPtr = __renew(buffer, newSize);
+ byteLength = newSize;
+ buffer = newPtr;
+ offset = buffer + newSize;
+ maxOffset = buffer + byteLength;
+ }
+
+ /**
+ * Gets the remaining space available in the buffer.
+ * @returns The number of bytes remaining.
+ */
+ // @ts-ignore: Decorator valid here
+ @inline export function getRemainingSize(): usize {
+ return maxOffset - offset;
+ }
+
+ /**
+ * Clears data from a specified offset onward.
+ * @param fromOffset - The starting offset to clear from.
+ */
+ // @ts-ignore: Decorator valid here
+ @inline export function clearFromOffset(fromOffset: usize): void {
+ if (fromOffset < offset) {
+ memory.fill(fromOffset, 0, offset - fromOffset);
+ offset = fromOffset;
+ }
+ }
+
+ /**
+ * Shrinks the buffer to fit the current offset.
+ */
+ // @ts-ignore: Decorator valid here
@inline export function shrink(): void {
- byteLength = offset - buffer;
- buffer = __renew(buffer, byteLength);
- maxOffset = byteLength + buffer;
+ if (offset > maxOffset) {
+ byteLength = offset - buffer;
+ buffer = __renew(buffer, byteLength);
+ maxOffset = byteLength + buffer;
+ }
}
- // @ts-ignore
+ /**
+ * Shrinks the buffer and resets the offset, returning the buffer as a specified type.
+ * @returns The buffer cast to the specified type.
+ */
+ // @ts-ignore: Decorator valid here
@inline export function shrinkTo(): T {
shrink();
offset = buffer;
return changetype(buffer);
}
- // @ts-ignore
- @inline export function out(): T {
+ /**
+ * Copies the buffer's content to a new object of a specified type.
+ * Optionally shrinks the buffer after copying.
+ * @param s - Whether to shrink the buffer after copying.
+ * @returns The new object containing the buffer's content.
+ */
+ // @ts-ignore: Decorator valid here
+ @inline export function out(s: bool = false): T {
const len = offset - buffer;
const _out = __new(len, idof());
memory.copy(_out, buffer, len);
- // shrink();
+ if (s) shrink();
offset = buffer;
return changetype(_out);
}
+
+ /**
+ * Copies the buffer's content to a given destination pointer.
+ * Optionally shrinks the buffer after copying.
+ * @param dst - The destination pointer.
+ * @param s - Whether to shrink the buffer after copying.
+ * @returns The destination pointer cast to the specified type.
+ */
+ // @ts-ignore: Decorator valid here
+ @inline export function outTo(dst: usize, s: bool = false): T {
+ const len = offset - buffer;
+ if (len != changetype