Skip to content

Commit

Permalink
update readme
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 3, 2025
1 parent 8d5f8cf commit 85f119d
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 84 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
<h5 align="center">
<pre> __ _____ _____ _____ _____ _____
__| || __|| || | | ___ | _ || __|
| | ||__ || | || | | ||___|| ||__ |
|_____||_____||_____||_|___| |__|__||_____|
v0.9.26
</pre>
<pre>
<span style="font-size: 0.9em;"> ██ ███████ ██████ ███ ██ █████ ███████
██ ██ ██ ██ ████ ██ ██ ██ ██
██ ███████ ██ ██ ██ ██ ██ █████ ███████ ███████
██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
█████ ███████ ██████ ██ ████ ██ ██ ███████</span>
AssemblyScript - v1.0.0
</pre>
</h5>

## Installation
Expand Down
129 changes: 114 additions & 15 deletions assembly/custom/bs.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,38 @@
import { bytes } from "../util/bytes";
import { nextPowerOf2 } from "../util/nextPowerOf2";
import { OBJECT, TOTAL_OVERHEAD } from "rt/common";

let maxOffset: usize = __new(0, idof<ArrayBuffer>());

/**
* 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<T>(data: T): void {
buffer = changetype<usize>(data);
offset = changetype<usize>(data);
byteLength = bytes(data);
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) {
Expand All @@ -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));
Expand All @@ -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>(): T {
shrink();
offset = buffer;
return changetype<T>(buffer);
}

// @ts-ignore
@inline export function out<T>(): 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<T>(s: bool = false): T {
const len = offset - buffer;
const _out = __new(len, idof<T>());
memory.copy(_out, buffer, len);
// shrink();
if (s) shrink();
offset = buffer;
return changetype<T>(_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<T>(dst: usize, s: bool = false): T {
const len = offset - buffer;
if (len != changetype<OBJECT>(dst - TOTAL_OVERHEAD).rtSize) __renew(len, idof<T>());
memory.copy(dst, buffer, len);
if (s) shrink();
offset = buffer;
return changetype<T>(dst);
}
}

// @ts-ignore: Decorator valid here
@inline function nextPowerOf2(n: u32): u32 {
return 1 << (32 - clz(n - 1));
}

// @ts-ignore: Decorator valid here
@inline function bytes<T>(o: T): i32 {
if (isInteger<T>() || isFloat<T>()) {
return sizeof<T>();
} else if (isManaged<T>() || isReference<T>()) {
return changetype<OBJECT>(changetype<usize>(o) - TOTAL_OVERHEAD).rtSize;
} else {
ERROR("Cannot convert type " + nameof<T>() + " to bytes!");
}
}
2 changes: 1 addition & 1 deletion assembly/serialize/simple/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { serializeString } from "./string";
} else if (isString<nonnull<T>>()) {
serializeString(src as string, staticSize);
// @ts-ignore: Supplied by transform
} else if (isDefined(src.__SERIALIZE)) {
} else if (isDefined(src.__SERIALIZE_BS)) {
// @ts-ignore
serializeObject(changetype<nonnull<T>>(src), staticSize);
} else if (src instanceof Date) {
Expand Down
74 changes: 31 additions & 43 deletions assembly/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,47 +4,31 @@ import { VecBase } from "./types";
import { bytes } from "./util/bytes";

@json
class Vec3 extends VecBase{
@omitnull()
str: string | null = null
class Vec3 {
base: VecBase = new VecBase();
x: i32 = 1;
y: i32 = 2;
z: i32 = 3;

__SERIALIZE(ptr: usize = 0): string {
if (ptr == 0) ptr = changetype<usize>(this);
let out = `{"x":${load<i32>(ptr, offsetof<this>("x")).toString()},"y":${load<i32>(ptr, offsetof<this>("y")).toString()},"z":${load<i32>(ptr, offsetof<this>("z")).toString()},`;
store<u16>(changetype<usize>(out) + ((out.length - 1) << 1), 125);
return out;
}
// @ts-ignore: Decorator valid here
@inline __ENSURE_SIZE(): void {
bs.ensureSize(128);
__SERIALIZE_BS(ptr: usize, staticSize: bool): void {
store<u64>(bs.offset, 27303493649956987, 0); // {"ba
store<u64>(bs.offset, 16325694684725363, 8); // se":
bs.offset += 16;
serialize_simple<VecBase>(load<VecBase>(ptr, offsetof<this>("base")), staticSize);
store<u64>(bs.offset, 9570664606466092, 0); // ,"x"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("x")), staticSize);
store<u64>(bs.offset, 9570668901433388, 0); // ,"y"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("y")), staticSize);
store<u64>(bs.offset, 9570673196400684, 0); // ,"z"
store<u16>(bs.offset, 58, 8); // :
bs.offset += 10;
serialize_simple<i32>(load<i32>(ptr, offsetof<this>("z")), staticSize);
store<u16>(bs.offset, 125, 0); // }
bs.offset += 2;
}
// __SERIALIZE_BS(ptr: usize, staticSize: bool): void {
// let block: usize = 0;
// store<u16>(bs.offset, 123, 0); // {
// bs.offset += 2;
// if ((block = load<usize>(ptr, offsetof<this>("str"))) !== 0) {
// store<u64>(bs.offset, 32088645568757794, 0); // "str
// store<u32>(bs.offset, 3801122, 8); // ":
// bs.offset += 12;
// serialize_simple<string | null>(changetype<string | null>(block), staticSize);
// store<u16>(bs.offset, 44, 0) // ,
// bs.offset += 2;
// }
// store<u64>(bs.offset, 9570664606466092, 0); // ,"x"
// store<u16>(bs.offset, 58, 8); // :
// bs.offset += 10;
// serialize_simple<i32>(load<i32>(ptr, offsetof<this>("x")), staticSize); store<u64>(bs.offset, 9570668901433388, 0); // ,"y"
// store<u16>(bs.offset, 58, 8); // :
// bs.offset += 10;
// serialize_simple<i32>(load<i32>(ptr, offsetof<this>("y")), staticSize); store<u64>(bs.offset, 9570673196400684, 0); // ,"z"
// store<u16>(bs.offset, 58, 8); // :
// bs.offset += 10;
// serialize_simple<i32>(load<i32>(ptr, offsetof<this>("z")), staticSize); store<u16>(bs.offset, 125, 0);
// bs.offset += 2;
// }
}

function strToNum(data: string): void {
Expand Down Expand Up @@ -72,20 +56,24 @@ function strToNum(data: string): void {
console.log(load<u32>(changetype<usize>("{}")).toString())
strToNum(',"z":')
const vec: Vec3 = {
str: null,
base: {
base: 9
},
x: 1,
y: 2,
z: 3
}
vec.__ENSURE_SIZE();

bs.ensureSize(1024);
vec.__SERIALIZE_BS(changetype<usize>(vec), true);
let out = __new(bs.offset - bs.buffer, idof<string>())
const serialized = bs.shrinkTo<string>();
console.log("Serialized: " + serialized);

// bench("Serialize Object (New)", () => {
// vec.__SERIALIZE_BS(changetype<usize>(vec), true);
// bs.out<string>();
// });
bench("Serialize Object (New)", () => {
vec.__SERIALIZE_BS(changetype<usize>(vec), true);
bs.out<string>();
});

// __reset()

Expand Down
4 changes: 4 additions & 0 deletions assembly/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { bs } from "./custom/bs";
import { serialize_simple } from "./serialize/simple";

@json
export class VecBase {
@omit()
base: i32 = 0;
}
19 changes: 10 additions & 9 deletions transform/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion transform/lib/index.js.map

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions transform/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ClassDeclaration, FieldDeclaration, IdentifierExpression, Parser, Source, NodeKind, Expression, CommonFlags, StringLiteralExpression, IntegerLiteralExpression, FloatLiteralExpression, NullExpression, TrueExpression, FalseExpression, CallExpression, ImportStatement, NamespaceDeclaration, Node, Statement, Tokenizer, SourceKind, PropertyAccessExpression, Token, CommentHandler, ExpressionStatement, BinaryExpression, NamedTypeNode, Range } from "assemblyscript/dist/assemblyscript.js";
import { ClassDeclaration, FieldDeclaration, IdentifierExpression, Parser, Source, NodeKind, Expression, CommonFlags, StringLiteralExpression, IntegerLiteralExpression, FloatLiteralExpression, NullExpression, TrueExpression, FalseExpression, CallExpression, ImportStatement, NamespaceDeclaration, Node, Statement, Tokenizer, SourceKind, PropertyAccessExpression, Token, CommentHandler, ExpressionStatement, BinaryExpression, NamedTypeNode, Range, FEATURE_SIMD } from "assemblyscript/dist/assemblyscript.js";
import { Transform } from "assemblyscript/dist/transform.js";
import { Visitor } from "./visitor.js";
import { SimpleParser, toString } from "./util.js";
Expand Down Expand Up @@ -131,10 +131,10 @@ class JSONTransform extends Visitor {

if (!this.schema.static) this.schema.members = sortMembers(this.schema.members);

let SERIALIZE_RAW = "__SERIALIZE(ptr: usize = changetype<usize>(this)): string \n let out = `{";
let SERIALIZE_BS = "__SERIALIZE_BS(ptr: usize, staticSize: bool): void {\n";
let INITIALIZE = "__INITIALIZE(): this {\n";
let DESERIALIZE = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n const len = key_end - key_start;\n";
let SERIALIZE_RAW = "@inline __SERIALIZE(ptr: usize = changetype<usize>(this)): string \n let out = `{";
let SERIALIZE_BS = "@inline __SERIALIZE_BS(ptr: usize, staticSize: bool): void {\n";
let INITIALIZE = "@inline __INITIALIZE(): this {\n";
let DESERIALIZE = "@inline __DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n const len = key_end - key_start;\n";

indent = " ";

Expand Down Expand Up @@ -206,10 +206,10 @@ class JSONTransform extends Visitor {
super.visitClassDeclaration(node);
}
generateEmptyMethods(node: ClassDeclaration): void {
let SERIALIZE_RAW_EMPTY = '__SERIALIZE(ptr: usize = changetype<usize>(this)): string {\n return "{}";\n}';
let SERIALIZE_BS_EMPTY = "__SERIALIZE_BS(ptr: usize, staticSize: bool): void {\n bs.ensureSize(4);\n store<u32>(bs.offset, 8192123);\n}";
let INITIALIZE_EMPTY = "__INITIALIZE(): this {\n return this;\n}";
let DESERIALIZE_EMPTY = "__DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";
let SERIALIZE_RAW_EMPTY = '@inline __SERIALIZE(ptr: usize = changetype<usize>(this)): string {\n return "{}";\n}';
let SERIALIZE_BS_EMPTY = "@inline __SERIALIZE_BS(ptr: usize, staticSize: bool): void {\n store<u32>(bs.offset, 8192123);\n bs.offset += 4;\n}";
let INITIALIZE_EMPTY = "@inline __INITIALIZE(): this {\n return this;\n}";
let DESERIALIZE_EMPTY = "@inline __DESERIALIZE(data: string, key_start: i32, key_end: i32, value_start: i32, value_end: i32): boolean {\n return false;\n}";

if (process.env["JSON_DEBUG"]) {
console.log(SERIALIZE_RAW_EMPTY);
Expand Down

0 comments on commit 85f119d

Please sign in to comment.