Skip to content

Commit

Permalink
remove staticSize stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed Jan 17, 2025
1 parent 6003b7d commit 0e72aba
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 52 deletions.
16 changes: 8 additions & 8 deletions assembly/serialize/simple/arbitrary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ import { serializeBool } from "./bool";
import { serializeInteger } from "./integer";
import { serializeString } from "./string";

export function serializeArbitrary(src: JSON.Value, staticSize: bool = false): void {
export function serializeArbitrary(src: JSON.Value): void {
switch (src.type) {
case JSON.Types.U8:
serializeInteger<u8>(src.get<u8>(), staticSize);
serializeInteger<u8>(src.get<u8>());
break;
case JSON.Types.U16:
serializeInteger<u16>(src.get<u16>(), staticSize);
serializeInteger<u16>(src.get<u16>());
break;
case JSON.Types.U32:
serializeInteger<u32>(src.get<u32>(), staticSize);
serializeInteger<u32>(src.get<u32>());
break;
case JSON.Types.U64:
serializeInteger<u64>(src.get<u64>(), staticSize);
serializeInteger<u64>(src.get<u64>());
break;
case JSON.Types.String:
serializeString(src.get<string>(), staticSize);
serializeString(src.get<string>());
break;
case JSON.Types.Bool:
serializeBool(src.get<bool>(), staticSize);
serializeBool(src.get<bool>());
case JSON.Types.Array: {
serializeArray(src.get<JSON.Value[]>(), staticSize);
serializeArray(src.get<JSON.Value[]>());
break;
}
default: {
Expand Down
13 changes: 6 additions & 7 deletions assembly/serialize/simple/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,36 @@ import { bs } from "as-bs";
import { COMMA, BRACKET_RIGHT, BRACKET_LEFT } from "../../custom/chars";
import { JSON } from "../..";

export function serializeArray<T extends any[]>(src: T, staticSize: bool = false): void {
export function serializeArray<T extends any[]>(src: T): void {
const srcSize = load<u32>(changetype<usize>(src), offsetof<T>("byteLength"));

if (srcSize == 0) {
if (!staticSize) bs.ensureSize(4);
bs.ensureSize(4);
store<u32>(bs.offset, 6094939);
bs.offset += 4;
return;
}

let srcPtr = src.dataStart;
const srcEnd = srcPtr + srcSize - sizeof<valueof<T>>();
// if (!bs.buffer) bs.setBuffer(__new(srcSize << 3, idof<string>()));
// else
if (!staticSize) bs.ensureSize(srcSize << 3);

bs.ensureSize(srcSize << 3);

store<u16>(bs.offset, BRACKET_LEFT);
bs.offset += 2;

while (srcPtr < srcEnd) {
const block = load<valueof<T>>(srcPtr);
JSON.__serialize<valueof<T>>(block);
if (!staticSize) bs.ensureSize(2);
bs.ensureSize(2);
store<u16>(bs.offset, COMMA);
bs.offset += 2;
srcPtr += sizeof<string>();
}

const lastBlock = load<valueof<T>>(srcPtr);
JSON.__serialize<valueof<T>>(lastBlock);
if (!staticSize) bs.ensureSize(2);
bs.ensureSize(2);
store<u16>(bs.offset, BRACKET_RIGHT);
bs.offset += 2;
}
6 changes: 3 additions & 3 deletions assembly/serialize/simple/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import { bs } from "as-bs";
* @param data data to serialize
* @returns void
*/
export function serializeBool(data: bool, staticSize: bool = false): void {
export function serializeBool(data: bool): void {
if (data == true) {
if (!staticSize) bs.ensureSize(8);
bs.ensureSize(8);
store<u64>(bs.offset, 28429475166421108);
bs.offset += 8;
} else {
if (!staticSize) bs.ensureSize(10);
bs.ensureSize(10);
store<u64>(bs.offset, 32370086184550502);
store<u64>(bs.offset, 101, 8);
bs.offset += 10;
Expand Down
4 changes: 2 additions & 2 deletions assembly/serialize/simple/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { bs } from "as-bs";
import { QUOTE } from "../../custom/chars";
import { bytes } from "../../util/bytes";

export function serializeDate(src: Date, staticSize: bool = false): void {
export function serializeDate(src: Date): void {
const data = src.toISOString();
const dataSize = bytes(data);
if (!staticSize) bs.ensureSize(dataSize + 4);
bs.ensureSize(dataSize + 4);
store<u16>(bs.offset, QUOTE);
memory.copy(bs.offset + 2, changetype<usize>(data), dataSize);
store<u16>(bs.offset + dataSize, QUOTE, 2);
Expand Down
4 changes: 2 additions & 2 deletions assembly/serialize/simple/float.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { dtoa_buffered } from "util/number";
import { bs } from "as-bs";

export function serializeFloat<T extends number>(data: T, staticSize: bool = false): void {
if (!staticSize) bs.ensureSize(64);
export function serializeFloat<T extends number>(data: T): void {
bs.ensureSize(64);
bs.offset += dtoa_buffered(bs.offset, data) << 1;
}
2 changes: 1 addition & 1 deletion assembly/serialize/simple/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import { itoa_buffered } from "util/number";
import { bs } from "as-bs";

export function serializeInteger<T extends number>(data: T): void {
// bs.ensureSize(sizeof<T>() << 3);
bs.ensureSize(sizeof<T>() << 3);
bs.offset += itoa_buffered(bs.offset, data) << 1;
}
25 changes: 13 additions & 12 deletions assembly/serialize/simple/map.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { JSON } from "../..";
import { BRACE_LEFT, BRACE_RIGHT, COLON, COMMA } from "../../custom/chars";
import { bs } from "as-bs";

export function serializeMap<T extends Map<any, any>>(src: T, staticSize: bool = false): void {
export function serializeMap<T extends Map<any, any>>(src: T): void {
const srcSize = src.size;
const srcEnd = srcSize - 1;

if (!srcSize) {
if (!staticSize) bs.ensureSize(4);
if (srcSize == 0) {
bs.ensureSize(4);
store<u32>(bs.offset, 8192123);
bs.offset += 4;
return;
Expand All @@ -15,28 +16,28 @@ export function serializeMap<T extends Map<any, any>>(src: T, staticSize: bool =
let keys = src.keys();
let values = src.values();

if (!staticSize) bs.ensureSize(srcSize << 3); // This needs to be predicted better
bs.ensureSize(srcSize << 3); // This needs to be predicted better

store<u16>(bs.offset, BRACE_LEFT);
bs.offset += 2;

for (let i = 0; i < srcEnd; i++) {
__serialize(unchecked(keys[i]));
if (!staticSize) bs.ensureSize(2);
JSON.__serialize(unchecked(keys[i]));
bs.ensureSize(2);
store<u16>(bs.offset, COLON);
bs.offset += 2;
__serialize(unchecked(values[i]));
if (!staticSize) bs.ensureSize(2);
JSON.__serialize(unchecked(values[i]));
bs.ensureSize(2);
store<u16>(bs.offset, COMMA);
bs.offset += 2;
}

__serialize(unchecked(keys[srcEnd]));
if (!staticSize) bs.ensureSize(2);
JSON.__serialize(unchecked(keys[srcEnd]));
bs.ensureSize(2);
store<u16>(bs.offset, COLON);
bs.offset += 2;
__serialize(unchecked(values[srcEnd]));
if (!staticSize) bs.ensureSize(2);
JSON.__serialize(unchecked(values[srcEnd]));
bs.ensureSize(2);
store<u16>(bs.offset, BRACE_RIGHT);
bs.offset += 2;
}
6 changes: 3 additions & 3 deletions assembly/serialize/simple/object.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
interface GeneratedInterface {
__SERIALIZE(ptr: usize, staticSize: bool): string;
__SERIALIZE(ptr: usize): string;
}

export function serializeObject<T extends GeneratedInterface>(data: T, staticSize: bool): void {
changetype<nonnull<T>>(data).__SERIALIZE(changetype<usize>(data), staticSize);
export function serializeObject<T extends GeneratedInterface>(data: T): void {
changetype<nonnull<T>>(data).__SERIALIZE(changetype<usize>(data));
}
14 changes: 0 additions & 14 deletions assembly/types.ts
Original file line number Diff line number Diff line change
@@ -1,14 +0,0 @@

@json
export class VecBase {
base: i32 = 0;

@inline __ALLOCATE(): void {
bs.ensureSize(4);
}

@inline __SERIALIZE(ptr: usize, staticSize: bool): void {
store<u32>(bs.offset, 8192123);
bs.offset += 4;
}
}

0 comments on commit 0e72aba

Please sign in to comment.