Skip to content

Commit

Permalink
use abort for compat with as-try
Browse files Browse the repository at this point in the history
  • Loading branch information
JairusSW committed May 18, 2024
1 parent 0baf662 commit 9b7dce8
Show file tree
Hide file tree
Showing 12 changed files with 43 additions and 43 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Jairus Tanaka <[email protected]>
Copyright (c) 2024 Jairus Tanaka <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
12 changes: 6 additions & 6 deletions assembly/bench/bench.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { JSON } from "..";
import { _deserializeString, deserializeString } from "../deserialize/string";
import { ProductValue } from "../product";
import { serializeUnknown } from "../serialize/unknown";
import { Sink } from "../src/sink";

import { bench, blackbox } from "as-bench/assembly/bench";
import { __atoi_fast, __atoi_fast_safe } from "../src/util";
import { serializeUnknown } from "../serialize/unknown";

const set = JSON.Value.from([
JSON.Value.from([]),
Expand Down Expand Up @@ -53,7 +52,7 @@ map.set("z", JSON.Value.from<f64>(-5.6));
bench("Serialize Set Theoretical Representation", () => {
blackbox<Sink>(serializeUnknown(set));
});
*//*
bench("Serialize Vector 3 Struct", () => {
blackbox<string>(vec.__JSON_Serialize_Unsafe());
});
Expand All @@ -73,10 +72,11 @@ bench("Parse String (Unwrap)", () => {
blackbox<string>(JSON.parse<string>("\"hello world\"").unwrap<string>());
});*/

bench("ATOI (Unsafe)", () => {
bench("ATOI (Unsafe): " + __atoi_fast<u32>("1234567890").toString(), () => {
blackbox<u32>(__atoi_fast<u32>("1234567890"));
});

bench("ATOI (Safe)", () => {
bench("ATOI (Safe): " + __atoi_fast_safe<u32>("1234567890").toString(), () => {
blackbox<u32>(__atoi_fast_safe<u32>("1234567890"));
});
});
bench("empty", () => {})
4 changes: 2 additions & 2 deletions assembly/deserialize/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ import { Product, ProductValue } from "../product";
const firstChar = unsafeCharCodeAt(data, start);
const lastChar = unsafeCharCodeAt(data, end);
if (firstChar !== quoteCode || lastChar !== quoteCode) {
throw new Error(`Expected string to start and end with ", but got ${data.slice(0, 100)} instead!`);
abort(`Expected string to start and end with ", but got ${data.slice(0, 100)} instead!`);
}
let last = start + 1;
for (let i = last; i < end; i++) {
Expand Down Expand Up @@ -144,7 +144,7 @@ import { Product, ProductValue } from "../product";
break;
}
default: {
throw new Error(`Cannot parse "${data.slice(0, 100)}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
abort(`Cannot parse "${data.slice(0, 100)}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export namespace JSON {
store<T>(changetype<usize>(this), value, STORAGE);
} else if (value instanceof Map) {
if (idof<T>() !== idof<Map<string, JSON.Value>>()) {
throw new Error("Maps must be of type Map<string, JSON.Value>!");
abort("Maps must be of type Map<string, JSON.Value>!");
}
this.type = JSON.Types.Obj;
store<T>(changetype<usize>(this), value, STORAGE);
Expand Down Expand Up @@ -177,7 +177,7 @@ export namespace JSON {
} else if (isFloat<valueof<T>>()) {
return serializeFloatArray(data, out);
} else if (isArray<valueof<T>>()) {
throw new Error("Not implemented yet")
abort("Not implemented yet")
} else if (idof<valueof<T>>() === idof<JSON.Value>()) {
return serializeUnknownArray(data as JSON.Value[], out);
}
Expand Down Expand Up @@ -210,7 +210,7 @@ export namespace JSON {
// @ts-ignore: Returns T
return deserializeUnknown(data);
}
throw new Error(`Could not deserialize data of type ${nameof<T>()}`);*/
abort(`Could not deserialize data of type ${nameof<T>()}`);*/
return Product.Err(`Could not deserialize data of type ${nameof<T>()}`);
}
}
12 changes: 6 additions & 6 deletions assembly/product.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import { Variant } from "as-variant/assembly";

@global let __PRODUCT_INSTANCE = changetype<Product>(__new(offsetof<Product>(), idof<Product>()));
@global let __PRODUCT_INSTANCE = changetype<ProductValue>(__new(offsetof<ProductValue>(), idof<ProductValue>()));
@global let __PRODUCT_SAFE: boolean = false;
@global let __PRODUCT_ERR: string = "";
@global let __PRODUCT_OK: u64 = 0;

@unmanaged export class Product {
constructor() { unreachable(); }
// @ts-ignore: Decorator
@inline static Ok<O>(ok: O): Product {
@inline static Ok<O>(ok: O): ProductValue {
__PRODUCT_SAFE = true;
return __PRODUCT_INSTANCE;
}
// @ts-ignore: Decorator
@inline static Err(err: string): Product {
@inline static Err(err: string): ProductValue {
__PRODUCT_SAFE = false;
__PRODUCT_ERR = err;
return __PRODUCT_INSTANCE;
}
// @ts-ignore: Decorator
@inline static Init(): Product {
@inline static Init(): ProductValue {
return __PRODUCT_INSTANCE;
}
// @ts-ignore: Decorator
@inline static Wrap<T>(x: T) {
@inline static Wrap<T>(x: T): void {

}
// @ts-ignore: Decorator
@inline static catch(catchFn: ((err: string) => {})) {
@inline static catch(catchFn: ((err: string) => void)): void {
// nop
}

Expand Down
6 changes: 3 additions & 3 deletions assembly/serialize/array/bool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { serializeBool } from "../bool";
@inline export function serializeBoolArray<T extends bool[]>(data: T, out: Sink | null = null): Sink {
if (!out) {
if (!data.length) {
return Sink.fromStringLiteral("[]");
return Sink.fromString("[]");
} else {
out = Sink.fromString("[");
}
Expand All @@ -16,14 +16,14 @@ import { serializeBool } from "../bool";

for (let i = 0; i < end; i++) {
serializeBool(
unchecked(data[i]),
data[i],
out
);
out.writeCodePoint(commaCode);
}

serializeBool(
unchecked(data[end]),
data[end],
out
);
out.writeCodePoint(rightBracketCode);
Expand Down
6 changes: 3 additions & 3 deletions assembly/serialize/array/float.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { serializeFloat } from "../float";
@inline export function serializeFloatArray<T extends number[]>(data: T, out: Sink | null = null): Sink {
if (!out) {
if (!data.length) {
return Sink.fromStringLiteral("[]");
return Sink.fromString("[]");
} else {
out = Sink.fromString("[");
}
Expand All @@ -16,14 +16,14 @@ import { serializeFloat } from "../float";

for (let i = 0; i < end; i++) {
serializeFloat(
unchecked(data[i]),
data[i],
out
);
out.writeCodePoint(commaCode);
}

serializeFloat(
unchecked(data[end]),
data[end],
out
);
out.writeCodePoint(rightBracketCode);
Expand Down
6 changes: 3 additions & 3 deletions assembly/serialize/array/integer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { serializeInteger } from "../integer";
@inline export function serializeIntegerArray<T extends number[]>(data: T, out: Sink | null = null): Sink {
if (!out) {
if (!data.length) {
return Sink.fromStringLiteral("[]");
return Sink.fromString("[]");
} else {
out = Sink.fromString("[");
}
Expand All @@ -16,14 +16,14 @@ import { serializeInteger } from "../integer";

for (let i = 0; i < end; i++) {
serializeInteger<valueof<T>>(
unchecked(data[i]),
data[i],
out
);
out.writeCodePoint(commaCode);
}

serializeInteger<valueof<T>>(
unchecked(data[end]),
data[end],
out
);
out.writeCodePoint(rightBracketCode);
Expand Down
6 changes: 3 additions & 3 deletions assembly/serialize/array/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { serializeString } from "../string";
@inline export function serializeStringArray<T extends string[]>(data: T, out: Sink | null = null): Sink {
if (!out) {
if (!data.length) {
return Sink.fromStringLiteral("[]");
return Sink.fromString("[]");
} else {
out = Sink.fromString("[");
}
Expand All @@ -16,14 +16,14 @@ import { serializeString } from "../string";

for (let i = 0; i < end; i++) {
serializeString(
unchecked(data[i]),
data[i],
out
);
out.writeCodePoint(commaCode);
}

serializeString(
unchecked(data[end]),
data[end],
out
);
out.writeCodePoint(rightBracketCode);
Expand Down
2 changes: 1 addition & 1 deletion assembly/serialize/array/unknown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { serializeUnknown } from "../unknown";
@inline export function serializeUnknownArray<T extends JSON.Value[]>(data: T, out: Sink | null = null): Sink {
if (!out) {
if (!data.length) {
return Sink.fromStringLiteral("[]");
return Sink.fromString("[]");
} else {
out = Sink.fromString("[");
}
Expand Down
14 changes: 7 additions & 7 deletions assembly/src/json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ export namespace JSON {
result.writeCodePoint(rightBraceCode);
return result.toString();
} else {
throw new Error(
abort(
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
);
}
Expand Down Expand Up @@ -208,7 +208,7 @@ export namespace JSON {
return;
}
} else {
throw new Error(
abort(
`Could not serialize data of type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
);
}
Expand Down Expand Up @@ -248,7 +248,7 @@ export namespace JSON {
// @ts-ignore
return parseDate(data);
} else {
throw new Error(
abort(
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
);
}
Expand Down Expand Up @@ -281,7 +281,7 @@ export namespace JSON {
// @ts-ignore
return parseDate(data);
} else {
throw new Error(
abort(
`Could not deserialize data ${data} to type ${nameof<T>()}. Make sure to add the correct decorators to classes.`
);
}
Expand Down Expand Up @@ -408,7 +408,7 @@ export namespace JSON {
break;
}
default: {
throw new Error(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
abort(`JSON: Cannot parse "${data}" as string. Invalid escape sequence: \\${data.charAt(i)}`);
}
}
}
Expand All @@ -422,7 +422,7 @@ export namespace JSON {
@inline function parseBoolean<T extends boolean>(data: string): T {
if (data.length > 3 && data.startsWith(trueWord)) return <T>true;
else if (data.length > 4 && data.startsWith(falseWord)) return <T>false;
else throw new Error(`JSON: Cannot parse "${data}" as boolean`);
else abort(`JSON: Cannot parse "${data}" as boolean`);
}

// @ts-ignore: Decorator
Expand Down Expand Up @@ -726,7 +726,7 @@ export namespace JSON {
return parseNumber<T>(k);
}

throw new Error(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
abort(`JSON: Cannot parse JSON object to a Map with a key of type ${nameof<T>()}`);
}

// @ts-ignore: Decorator
Expand Down
10 changes: 5 additions & 5 deletions assembly/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,10 @@ import { Product } from "../product";
if (!end) end = start + u32(str.length << 1);
if (isSigned<T>()) {
// Negative path
if (load<u16>(changetype<usize>(str) + <usize>start) === 45) {
if (load<u8>(changetype<usize>(str) + <usize>start) === 45) {
start += 2;
for (; start < end; start += 2) {
const v = load<u16>(changetype<usize>(str) + <usize>start) - 48;
for (; end > start; start += 2) {
const v = load<u8>(changetype<usize>(str) + <usize>start) - 48;
// v > 9 is 1/3 slower than 9 < v
if (<u16>9 < v) {
Product.Err("Can only parse character 0-9, but found \\" + (v).toString() + " instead!");
Expand All @@ -303,8 +303,8 @@ import { Product } from "../product";
}
return -val as T;
} else {
for (; start < end; start += 2) {
const v = load<u16>(changetype<usize>(str) + <usize>start) - 48;
for (; end > start; start += 2) {
const v = load<u8>(changetype<usize>(str) + <usize>start) - 48;
if (<u16>9 < v) {
Product.Err("Can only parse character 0-9, but found \\" + (v).toString() + " instead!");
break;
Expand Down

0 comments on commit 9b7dce8

Please sign in to comment.