json-as 0.9.29 → 1.0.0-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (107) hide show
  1. package/.github/workflows/nodejs.yml +0 -3
  2. package/.prettierrc.json +3 -2
  3. package/CHANGELOG +13 -0
  4. package/LICENSE +1 -1
  5. package/README.md +22 -7
  6. package/as-test.config.json +1 -1
  7. package/asconfig.json +2 -2
  8. package/assembly/__benches__/misc.bench.ts +17 -32
  9. package/assembly/__tests__/bool.spec.ts +1 -1
  10. package/assembly/__tests__/simd/string.spec.ts +32 -0
  11. package/assembly/custom/memory.ts +25 -0
  12. package/assembly/custom/util.ts +14 -92
  13. package/assembly/deserialize/simd/string.ts +103 -0
  14. package/assembly/deserialize/simple/arbitrary.ts +17 -0
  15. package/assembly/deserialize/simple/array/arbitrary.ts +113 -0
  16. package/assembly/deserialize/simple/array/array.ts +18 -0
  17. package/assembly/deserialize/simple/array/bool.ts +17 -0
  18. package/assembly/deserialize/simple/array/float.ts +28 -0
  19. package/assembly/deserialize/simple/array/integer.ts +27 -0
  20. package/assembly/deserialize/simple/array/map.ts +18 -0
  21. package/assembly/deserialize/simple/array/object.ts +18 -0
  22. package/assembly/deserialize/simple/array/string.ts +22 -0
  23. package/assembly/deserialize/simple/array.ts +48 -0
  24. package/assembly/deserialize/simple/bool.ts +9 -0
  25. package/assembly/deserialize/simple/date.ts +11 -0
  26. package/assembly/deserialize/simple/float.ts +10 -0
  27. package/assembly/deserialize/simple/integer.ts +5 -0
  28. package/assembly/deserialize/simple/map.ts +154 -0
  29. package/assembly/deserialize/simple/object.ts +159 -0
  30. package/assembly/deserialize/simple/string.ts +48 -0
  31. package/assembly/globals/tables.ts +417 -0
  32. package/assembly/index.d.ts +9 -13
  33. package/assembly/index.ts +266 -146
  34. package/assembly/serialize/simd/string.ts +176 -0
  35. package/assembly/serialize/simple/arbitrary.ts +36 -0
  36. package/assembly/serialize/simple/array.ts +32 -0
  37. package/assembly/serialize/simple/bool.ts +19 -0
  38. package/assembly/serialize/simple/date.ts +13 -0
  39. package/assembly/serialize/simple/float.ts +7 -0
  40. package/assembly/serialize/simple/integer.ts +7 -0
  41. package/assembly/serialize/simple/map.ts +43 -0
  42. package/assembly/serialize/simple/object.ts +7 -0
  43. package/assembly/serialize/simple/string.ts +48 -0
  44. package/assembly/test.ts +41 -27
  45. package/assembly/tsconfig.json +2 -91
  46. package/assembly/types.ts +0 -0
  47. package/assembly/util/atoi.ts +35 -0
  48. package/assembly/util/bytes.ts +12 -0
  49. package/assembly/util/concat.ts +9 -0
  50. package/assembly/util/getArrayDepth.ts +17 -0
  51. package/assembly/util/index.ts +5 -0
  52. package/assembly/util/isSpace.ts +4 -0
  53. package/assembly/util/nextPowerOf2.ts +4 -0
  54. package/assembly/util/ptrToStr.ts +7 -0
  55. package/assembly/util/snp.ts +69 -0
  56. package/bench.js +5 -5
  57. package/modules/bs/index.ts +167 -0
  58. package/modules/tsconfig.json +8 -0
  59. package/package.json +42 -48
  60. package/transform/lib/builder.js +1353 -0
  61. package/transform/lib/builder.js.map +1 -0
  62. package/transform/lib/index.js +497 -332
  63. package/transform/lib/index.js.map +1 -1
  64. package/transform/lib/index.old.js +404 -0
  65. package/transform/lib/index.old.js.map +1 -0
  66. package/transform/lib/linker.js +18 -0
  67. package/transform/lib/linker.js.map +1 -0
  68. package/transform/lib/types.js +25 -0
  69. package/transform/lib/types.js.map +1 -0
  70. package/transform/lib/util.js +47 -0
  71. package/transform/lib/util.js.map +1 -0
  72. package/transform/lib/visitor.js +529 -446
  73. package/transform/lib/visitor.js.map +1 -0
  74. package/transform/package.json +1 -34
  75. package/transform/src/builder.ts +1371 -0
  76. package/transform/src/index.ts +564 -341
  77. package/transform/src/linker.ts +21 -0
  78. package/transform/src/types.ts +27 -0
  79. package/transform/src/util.ts +56 -0
  80. package/transform/src/visitor.ts +531 -0
  81. package/transform/tsconfig.json +3 -1
  82. package/assembly/__benches__/as-tral.d.ts +0 -1
  83. package/assembly/__tests__/date.spec.ts +0 -12
  84. package/assembly/custom/bs.ts +0 -202
  85. package/assembly/deserialize/array/array.ts +0 -31
  86. package/assembly/deserialize/array/bool.ts +0 -19
  87. package/assembly/deserialize/array/float.ts +0 -24
  88. package/assembly/deserialize/array/integer.ts +0 -24
  89. package/assembly/deserialize/array/map.ts +0 -27
  90. package/assembly/deserialize/array/object.ts +0 -27
  91. package/assembly/deserialize/array/string.ts +0 -29
  92. package/assembly/deserialize/array.ts +0 -46
  93. package/assembly/deserialize/bool.ts +0 -34
  94. package/assembly/deserialize/date.ts +0 -19
  95. package/assembly/deserialize/float.ts +0 -21
  96. package/assembly/deserialize/integer.ts +0 -16
  97. package/assembly/deserialize/map.ts +0 -139
  98. package/assembly/deserialize/object.ts +0 -211
  99. package/assembly/deserialize/string.ts +0 -149
  100. package/assembly/serialize/array.ts +0 -44
  101. package/assembly/serialize/bool.ts +0 -10
  102. package/assembly/serialize/date.ts +0 -4
  103. package/assembly/serialize/float.ts +0 -4
  104. package/assembly/serialize/integer.ts +0 -5
  105. package/assembly/serialize/map.ts +0 -24
  106. package/assembly/serialize/object.ts +0 -13
  107. package/assembly/serialize/string.ts +0 -284
@@ -0,0 +1,36 @@
1
+ import { JSON } from "../..";
2
+ import { serializeArray } from "./array";
3
+ import { serializeBool } from "./bool";
4
+ import { serializeInteger } from "./integer";
5
+ import { serializeString } from "./string";
6
+
7
+ export function serializeArbitrary(src: JSON.Value): void {
8
+ switch (src.type) {
9
+ case JSON.Types.U8:
10
+ serializeInteger<u8>(src.get<u8>());
11
+ break;
12
+ case JSON.Types.U16:
13
+ serializeInteger<u16>(src.get<u16>());
14
+ break;
15
+ case JSON.Types.U32:
16
+ serializeInteger<u32>(src.get<u32>());
17
+ break;
18
+ case JSON.Types.U64:
19
+ serializeInteger<u64>(src.get<u64>());
20
+ break;
21
+ case JSON.Types.String:
22
+ serializeString(src.get<string>());
23
+ break;
24
+ case JSON.Types.Bool:
25
+ serializeBool(src.get<bool>());
26
+ case JSON.Types.Array: {
27
+ serializeArray(src.get<JSON.Value[]>());
28
+ break;
29
+ }
30
+ default: {
31
+ const fn = JSON.Value.METHODS.get(src.type - JSON.Types.Struct);
32
+ const value = src.get<usize>();
33
+ call_indirect<string>(fn, 0, value);
34
+ }
35
+ }
36
+ }
@@ -0,0 +1,32 @@
1
+ import { bs } from "../../../modules/bs";
2
+ import { COMMA, BRACKET_RIGHT, BRACKET_LEFT } from "../../custom/chars";
3
+ import { JSON } from "../..";
4
+
5
+ export function serializeArray<T extends any[]>(src: T): void {
6
+ const end = src.length - 1;
7
+ let i = 0;
8
+ if (end == -1) {
9
+ bs.ensureSize(4);
10
+ store<u32>(bs.offset, 6094939);
11
+ bs.offset += 4;
12
+ return;
13
+ }
14
+ bs.ensureSize(end << 3);
15
+
16
+ store<u16>(bs.offset, BRACKET_LEFT);
17
+ bs.offset += 2;
18
+
19
+ while (i < end) {
20
+ const block = unchecked(src[i++]);
21
+ JSON.__serialize<valueof<T>>(block);
22
+ bs.ensureSize(2);
23
+ store<u16>(bs.offset, COMMA);
24
+ bs.offset += 2;
25
+ }
26
+
27
+ const lastBlock = unchecked(src[end]);
28
+ JSON.__serialize<valueof<T>>(lastBlock);
29
+ bs.ensureSize(2);
30
+ store<u16>(bs.offset, BRACKET_RIGHT);
31
+ bs.offset += 2;
32
+ }
@@ -0,0 +1,19 @@
1
+ import { bs } from "../../../modules/bs";
2
+
3
+ /**
4
+ * Serialize a bool to type string
5
+ * @param data data to serialize
6
+ * @returns void
7
+ */
8
+ export function serializeBool(data: bool): void {
9
+ if (data == true) {
10
+ bs.ensureSize(8);
11
+ store<u64>(bs.offset, 28429475166421108);
12
+ bs.offset += 8;
13
+ } else {
14
+ bs.ensureSize(10);
15
+ store<u64>(bs.offset, 32370086184550502);
16
+ store<u64>(bs.offset, 101, 8);
17
+ bs.offset += 10;
18
+ }
19
+ }
@@ -0,0 +1,13 @@
1
+ import { bs } from "../../../modules/bs";
2
+ import { QUOTE } from "../../custom/chars";
3
+ import { bytes } from "../../util/bytes";
4
+
5
+ export function serializeDate(src: Date): void {
6
+ const data = src.toISOString();
7
+ const dataSize = bytes(data);
8
+ bs.ensureSize(dataSize + 4);
9
+ store<u16>(bs.offset, QUOTE);
10
+ memory.copy(bs.offset + 2, changetype<usize>(data), dataSize);
11
+ store<u16>(bs.offset + dataSize, QUOTE, 2);
12
+ bs.offset += dataSize + 4;
13
+ }
@@ -0,0 +1,7 @@
1
+ import { dtoa_buffered } from "util/number";
2
+ import { bs } from "../../../modules/bs";
3
+
4
+ export function serializeFloat<T extends number>(data: T): void {
5
+ bs.ensureSize(64);
6
+ bs.offset += dtoa_buffered(bs.offset, data) << 1;
7
+ }
@@ -0,0 +1,7 @@
1
+ import { itoa_buffered } from "util/number";
2
+ import { bs } from "../../../modules/bs";
3
+
4
+ export function serializeInteger<T extends number>(data: T): void {
5
+ bs.ensureSize(sizeof<T>() << 3);
6
+ bs.offset += itoa_buffered(bs.offset, data) << 1;
7
+ }
@@ -0,0 +1,43 @@
1
+ import { JSON } from "../..";
2
+ import { BRACE_LEFT, BRACE_RIGHT, COLON, COMMA } from "../../custom/chars";
3
+ import { bs } from "../../../modules/bs";
4
+
5
+ export function serializeMap<T extends Map<any, any>>(src: T): void {
6
+ const srcSize = src.size;
7
+ const srcEnd = srcSize - 1;
8
+
9
+ if (srcSize == 0) {
10
+ bs.ensureSize(4);
11
+ store<u32>(bs.offset, 8192123);
12
+ bs.offset += 4;
13
+ return;
14
+ }
15
+
16
+ let keys = src.keys();
17
+ let values = src.values();
18
+
19
+ bs.ensureSize(srcSize << 3); // This needs to be predicted better
20
+
21
+ store<u16>(bs.offset, BRACE_LEFT);
22
+ bs.offset += 2;
23
+
24
+ for (let i = 0; i < srcEnd; i++) {
25
+ JSON.__serialize(unchecked(keys[i]));
26
+ bs.ensureSize(2);
27
+ store<u16>(bs.offset, COLON);
28
+ bs.offset += 2;
29
+ JSON.__serialize(unchecked(values[i]));
30
+ bs.ensureSize(2);
31
+ store<u16>(bs.offset, COMMA);
32
+ bs.offset += 2;
33
+ }
34
+
35
+ JSON.__serialize(unchecked(keys[srcEnd]));
36
+ bs.ensureSize(2);
37
+ store<u16>(bs.offset, COLON);
38
+ bs.offset += 2;
39
+ JSON.__serialize(unchecked(values[srcEnd]));
40
+ bs.ensureSize(2);
41
+ store<u16>(bs.offset, BRACE_RIGHT);
42
+ bs.offset += 2;
43
+ }
@@ -0,0 +1,7 @@
1
+ interface GeneratedInterface {
2
+ __SERIALIZE(ptr: usize): string;
3
+ }
4
+
5
+ export function serializeObject<T extends GeneratedInterface>(data: T): void {
6
+ changetype<nonnull<T>>(data).__SERIALIZE(changetype<usize>(data));
7
+ }
@@ -0,0 +1,48 @@
1
+ import { _intTo16 } from "../../custom/util";
2
+ import { bytes } from "../../util/bytes";
3
+ import { bs } from "../../../modules/bs";
4
+ import { BACK_SLASH, QUOTE } from "../../custom/chars";
5
+ import { SERIALIZE_ESCAPE_TABLE } from "../../globals/tables";
6
+
7
+ /**
8
+ * Serializes valid strings into their JSON counterpart
9
+ * @param src string
10
+ * @returns void
11
+ */
12
+ export function serializeString(src: string): void {
13
+ const srcSize = bytes(src);
14
+ bs.ensureSize(srcSize + 4);
15
+ let srcPtr = changetype<usize>(src);
16
+ const srcEnd = srcPtr + srcSize;
17
+
18
+ store<u16>(bs.offset, QUOTE);
19
+ bs.offset += 2;
20
+
21
+ let lastPtr: i32 = srcPtr;
22
+ while (srcPtr < srcEnd) {
23
+ const code = load<u16>(srcPtr);
24
+ if (code == 34 || code == 92 || code < 32) {
25
+ const remBytes = srcPtr - lastPtr;
26
+ memory.copy(bs.offset, lastPtr, remBytes);
27
+ bs.offset += remBytes;
28
+ const escaped = load<u32>(SERIALIZE_ESCAPE_TABLE + (code << 2));
29
+ if ((escaped & 0xffff) != BACK_SLASH) {
30
+ bs.ensureSize(10);
31
+ store<u64>(bs.offset, 13511005048209500, 0);
32
+ store<u32>(bs.offset, escaped, 8);
33
+ bs.offset += 12;
34
+ } else {
35
+ bs.ensureSize(2);
36
+ store<u32>(bs.offset, escaped, 0);
37
+ bs.offset += 4;
38
+ }
39
+ lastPtr = srcPtr + 2;
40
+ }
41
+ srcPtr += 2;
42
+ }
43
+ const remBytes = srcEnd - lastPtr;
44
+ memory.copy(bs.offset, lastPtr, remBytes);
45
+ bs.offset += remBytes;
46
+ store<u16>(bs.offset, QUOTE);
47
+ bs.offset += 2;
48
+ }
package/assembly/test.ts CHANGED
@@ -1,35 +1,49 @@
1
1
  import { JSON } from ".";
2
+ // @json or @serializable work here
3
+ @json
4
+ class Vec3 {
5
+ x: f64 = 0.0;
6
+ y: f64 = 0.0;
7
+ z: f64 = 0.0;
8
+ }
2
9
 
3
10
 
4
11
  @json
5
- class Vec3 {
6
- public x: i32 = 0;
7
- public y: i32 = 0;
8
- public z: i32 = 0;
12
+ class Player {
13
+ // @alias("first name")
14
+ firstName!: string;
15
+ lastName!: string;
16
+ lastActive!: i32[];
17
+ // Drop in a code block, function, or expression that evaluates to a boolean
18
+ @omitif((self: Player): boolean => self.age < 18)
19
+ age!: i32;
20
+ // @omitnull()
21
+ pos!: Vec3 | null;
22
+ isVerified!: boolean;
23
+ @inline __INITIALIZE(): this {
24
+ this.firstName = "";
25
+ this.lastName = "";
26
+ this.pos = changetype<nonnull<Vec3 | null>>(__new(offsetof<nonnull<Vec3 | null>>(), idof<nonnull<Vec3 | null>>())).__INITIALIZE();
27
+ return this;
28
+ }
9
29
  }
10
30
 
11
- // @json
12
- // class Base {
13
- // public bam: string = "harekogkeorgke"
14
- // }
31
+ const player: Player = {
32
+ firstName: "Jairus",
33
+ lastName: "Tanaka",
34
+ lastActive: [1, 20, 2025],
35
+ age: 18,
36
+ pos: {
37
+ x: 3.4,
38
+ y: 1.2,
39
+ z: 8.3,
40
+ },
41
+ isVerified: true,
42
+ };
15
43
 
16
- // @json
17
- // class Foo extends Base {
18
- // public bar: JSON.Raw = "\"this is ok\'"
19
- // public baz: i32 = 0;
20
- // public pos: Vec3<Vec3<i32>> = {
21
- // x: 1,
22
- // y: 2,
23
- // z: {
24
- // x: 1,
25
- // y: 2,
26
- // z: 3
27
- // }
28
- // }
29
- // // ^ this is not okay
30
- // }
44
+ const stringified = JSON.stringify<Player>(player);
45
+ console.log("Serialized: " + stringified);
46
+ const parsed = JSON.parse<Player>(stringified);
47
+ console.log("Deserialized: " + JSON.stringify<Player>(parsed));
31
48
 
32
- const serialized = JSON.stringify(new Vec3());
33
- console.log("Serialized: " + serialized);
34
- const deserialized = JSON.parseSafe<Vec3>(`{"x":1,"y":true,"z":3}`);
35
- console.log("Deserialized: " + JSON.stringify(deserialized));
49
+ console.log("Deserialized: " + JSON.stringify(JSON.parse<i32[]>("[1,2,3]")))
@@ -2,96 +2,7 @@
2
2
  "extends": "assemblyscript/std/assembly.json",
3
3
  "include": ["./**/*.ts"],
4
4
  "compilerOptions": {
5
- /* Visit https://aka.ms/tsconfig to read more about this file */
6
- /* Projects */
7
- // "incremental": true, /* Save .tsbuildinfo files to allow for incremental compilation of projects. */
8
- // "composite": true, /* Enable constraints that allow a TypeScript project to be used with project references. */
9
- // "tsBuildInfoFile": "./.tsbuildinfo", /* Specify the path to .tsbuildinfo incremental compilation file. */
10
- // "disableSourceOfProjectReferenceRedirect": true, /* Disable preferring source files instead of declaration files when referencing composite projects. */
11
- // "disableSolutionSearching": true, /* Opt a project out of multi-project reference checking when editing. */
12
- // "disableReferencedProjectLoad": true, /* Reduce the number of projects loaded automatically by TypeScript. */
13
- /* Language and Environment */
14
- "target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
15
- // "lib": [], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
16
- // "jsx": "preserve", /* Specify what JSX code is generated. */
17
- "experimentalDecorators": true /* Enable experimental support for TC39 stage 2 draft decorators. */,
18
- "emitDecoratorMetadata": true /* Emit design-type metadata for decorated declarations in source files. */,
19
- // "jsxFactory": "", /* Specify the JSX factory function used when targeting React JSX emit, e.g. 'React.createElement' or 'h'. */
20
- // "jsxFragmentFactory": "", /* Specify the JSX Fragment reference used for fragments when targeting React JSX emit e.g. 'React.Fragment' or 'Fragment'. */
21
- // "jsxImportSource": "", /* Specify module specifier used to import the JSX factory functions when using 'jsx: react-jsx*'. */
22
- // "reactNamespace": "", /* Specify the object invoked for 'createElement'. This only applies when targeting 'react' JSX emit. */
23
- // "noLib": true, /* Disable including any library files, including the default lib.d.ts. */
24
- // "useDefineForClassFields": true, /* Emit ECMAScript-standard-compliant class fields. */
25
- // "moduleDetection": "auto", /* Control what method is used to detect module-format JS files. */
26
- /* Modules */
27
- "module": "commonjs" /* Specify what module code is generated. */,
28
- // "rootDir": "./", /* Specify the root folder within your source files. */
29
- // "moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
30
- // "baseUrl": "./", /* Specify the base directory to resolve non-relative module names. */
31
- // "paths": {}, /* Specify a set of entries that re-map imports to additional lookup locations. */
32
- // "rootDirs": [], /* Allow multiple folders to be treated as one when resolving modules. */
33
- // "typeRoots": [], /* Specify multiple folders that act like './node_modules/@types'. */
34
- // "types": [], /* Specify type package names to be included without being referenced in a source file. */
35
- // "allowUmdGlobalAccess": true, /* Allow accessing UMD globals from modules. */
36
- // "moduleSuffixes": [], /* List of file name suffixes to search when resolving a module. */
37
- // "resolveJsonModule": true, /* Enable importing .json files. */
38
- // "noResolve": true, /* Disallow 'import's, 'require's or '<reference>'s from expanding the number of files TypeScript should add to a project. */
39
- /* JavaScript Support */
40
- // "allowJs": true, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
41
- // "checkJs": true, /* Enable error reporting in type-checked JavaScript files. */
42
- // "maxNodeModuleJsDepth": 1, /* Specify the maximum folder depth used for checking JavaScript files from 'node_modules'. Only applicable with 'allowJs'. */
43
- /* Emit */
44
- // "declaration": true, /* Generate .d.ts files from TypeScript and JavaScript files in your project. */
45
- // "declarationMap": true, /* Create sourcemaps for d.ts files. */
46
- // "emitDeclarationOnly": true, /* Only output d.ts files and not JavaScript files. */
47
- // "sourceMap": true, /* Create source map files for emitted JavaScript files. */
48
- // "outFile": "./", /* Specify a file that bundles all outputs into one JavaScript file. If 'declaration' is true, also designates a file that bundles all .d.ts output. */
49
- // "outDir": "./", /* Specify an output folder for all emitted files. */
50
- // "removeComments": true, /* Disable emitting comments. */
51
- // "noEmit": true, /* Disable emitting files from a compilation. */
52
- // "importHelpers": true, /* Allow importing helper functions from tslib once per project, instead of including them per-file. */
53
- // "importsNotUsedAsValues": "remove", /* Specify emit/checking behavior for imports that are only used for types. */
54
- // "downlevelIteration": true, /* Emit more compliant, but verbose and less performant JavaScript for iteration. */
55
- // "sourceRoot": "", /* Specify the root path for debuggers to find the reference source code. */
56
- // "mapRoot": "", /* Specify the location where debugger should locate map files instead of generated locations. */
57
- // "inlineSourceMap": true, /* Include sourcemap files inside the emitted JavaScript. */
58
- // "inlineSources": true, /* Include source code in the sourcemaps inside the emitted JavaScript. */
59
- // "emitBOM": true, /* Emit a UTF-8 Byte Order Mark (BOM) in the beginning of output files. */
60
- // "newLine": "crlf", /* Set the newline character for emitting files. */
61
- // "stripInternal": true, /* Disable emitting declarations that have '@internal' in their JSDoc comments. */
62
- // "noEmitHelpers": true, /* Disable generating custom helper functions like '__extends' in compiled output. */
63
- // "noEmitOnError": true, /* Disable emitting files if any type checking errors are reported. */
64
- // "preserveConstEnums": true, /* Disable erasing 'const enum' declarations in generated code. */
65
- // "declarationDir": "./", /* Specify the output directory for generated declaration files. */
66
- // "preserveValueImports": true, /* Preserve unused imported values in the JavaScript output that would otherwise be removed. */
67
- /* Interop Constraints */
68
- // "isolatedModules": true, /* Ensure that each file can be safely transpiled without relying on other imports. */
69
- // "allowSyntheticDefaultImports": true, /* Allow 'import x from y' when a module doesn't have a default export. */
70
- "esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
71
- // "preserveSymlinks": true, /* Disable resolving symlinks to their realpath. This correlates to the same flag in node. */
72
- "forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
73
- /* Type Checking */
74
- "strict": false /* Enable all strict type-checking options. */,
75
- // "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
76
- "strictNullChecks": true /* When type checking, take into account 'null' and 'undefined'. */,
77
- // "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
78
- // "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
79
- // "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
80
- // "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
81
- // "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
82
- // "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
83
- "noUnusedLocals": true /* Enable error reporting when local variables aren't read. */,
84
- "noUnusedParameters": true /* Raise an error when a function parameter isn't read. */,
85
- // "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
86
- // "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
87
- // "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
88
- // "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
89
- // "noImplicitOverride": true, /* Ensure overriding members in derived classes are marked with an override modifier. */
90
- // "noPropertyAccessFromIndexSignature": true, /* Enforces using indexed accessors for keys declared using an indexed type. */
91
- // "allowUnusedLabels": true, /* Disable error reporting for unused labels. */
92
- // "allowUnreachableCode": true, /* Disable error reporting for unreachable code. */
93
- /* Completeness */
94
- // "skipDefaultLibCheck": true, /* Skip type checking .d.ts files that are included with TypeScript. */
95
- "skipLibCheck": true /* Skip type checking all .d.ts files. */
5
+ "experimentalDecorators": true,
6
+ "emitDecoratorMetadata": true
96
7
  }
97
8
  }
File without changes
@@ -0,0 +1,35 @@
1
+ /**
2
+ * ATOI optimized for non-exponential integers
3
+ * @param str
4
+ * @returns
5
+ */
6
+ // @ts-ignore: Decorator valid here
7
+ @inline export function atoi<T>(srcStart: usize, srcEnd: usize): T {
8
+ // @ts-ignore: type
9
+ let val: T = 0;
10
+ if (isSigned<T>()) {
11
+ if (load<u16>(srcStart) == 45) {
12
+ srcStart += 2;
13
+ while (srcStart < srcEnd) {
14
+ // @ts-ignore: type
15
+ val = (val * 10 + (load<u16>(srcStart) - 48)) as T;
16
+ srcStart += 2;
17
+ }
18
+ return -val as T;
19
+ } else {
20
+ while (srcStart < srcEnd) {
21
+ // @ts-ignore: type
22
+ val = (val * 10 + (load<u16>(srcStart) - 48)) as T;
23
+ srcStart += 2;
24
+ }
25
+ return val as T;
26
+ }
27
+ } else {
28
+ while (srcStart < srcEnd) {
29
+ // @ts-ignore: type
30
+ val = (val * 10 + (load<u16>(srcStart) - 48)) as T;
31
+ srcStart += 2;
32
+ }
33
+ return val as T;
34
+ }
35
+ }
@@ -0,0 +1,12 @@
1
+ import { OBJECT, TOTAL_OVERHEAD } from "rt/common";
2
+
3
+ // @ts-ignore: Decorator valid here
4
+ @inline export function bytes<T>(o: T): i32 {
5
+ if (isInteger<T>() || isFloat<T>()) {
6
+ return sizeof<T>();
7
+ } else if (isManaged<T>() || isReference<T>()) {
8
+ return changetype<OBJECT>(changetype<usize>(o) - TOTAL_OVERHEAD).rtSize;
9
+ } else {
10
+ ERROR("Cannot convert type " + nameof<T>() + " to bytes!");
11
+ }
12
+ }
@@ -0,0 +1,9 @@
1
+ import { bytes } from "./bytes";
2
+ export function concat(left: string, right: string): string {
3
+ const leftSize: usize = bytes(left);
4
+ const rightSize: usize = bytes(right);
5
+ const jointSize: usize = leftSize + rightSize;
6
+ const jointPtr = __renew(changetype<usize>(left), jointSize);
7
+ memory.copy(changetype<usize>(left) + leftSize, changetype<usize>(right), rightSize);
8
+ return changetype<string>(jointPtr);
9
+ }
@@ -0,0 +1,17 @@
1
+ /**
2
+ * A terrible function which ascertains the depth of a certain array
3
+ * Depending on the optimization level, this can be essentially nop
4
+ * @returns depth of array
5
+ */
6
+
7
+ // @ts-ignore: Decorator
8
+ export function getArrayDepth<T extends ArrayLike>(depth: i32 = 1): i32 {
9
+ if (!isArray<T>()) {
10
+ return 0;
11
+ } else if (isArray<valueof<T>>()) {
12
+ depth++;
13
+ return getArrayDepth<valueof<T>>(depth);
14
+ } else {
15
+ return depth;
16
+ }
17
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./isSpace";
2
+ export * from "./atoi";
3
+ export * from "./concat";
4
+ export * from "./bytes";
5
+ export * from "./nextPowerOf2";
@@ -0,0 +1,4 @@
1
+ // @ts-ignore
2
+ @inline export function isSpace(code: u16): boolean {
3
+ return code == 0x20 || code - 9 <= 4;
4
+ }
@@ -0,0 +1,4 @@
1
+ // @ts-ignore: Decorator valid here
2
+ @inline export function nextPowerOf2(n: u32): u32 {
3
+ return 1 << (32 - clz(n - 1));
4
+ }
@@ -0,0 +1,7 @@
1
+ // @ts-ignore: decorator
2
+ @inline export function ptrToStr(start: usize, end: usize): string {
3
+ const size = end - start;
4
+ const out = __new(size, idof<string>());
5
+ memory.copy(out, start, size);
6
+ return changetype<string>(out);
7
+ }
@@ -0,0 +1,69 @@
1
+ // e -> 101
2
+ // E -> 69
3
+ // - -> 45
4
+ // . -> 46
5
+
6
+ import { POW_TEN_TABLE_32, POW_TEN_TABLE_64 } from "../globals/tables";
7
+ import { atoi } from "./atoi";
8
+
9
+ // @ts-ignore: Decorator valid here
10
+ @inline function snp<T extends number>(srcStart: usize, srcEnd: usize): T {
11
+ // @ts-ignore: type
12
+ let val: T = 0;
13
+ let char = load<u16>(srcStart) - 48;
14
+ if (isFloat<T>()) {
15
+ while (srcStart < srcEnd) {}
16
+ } else if (isInteger<T>()) {
17
+ if (isSigned<T>() && char == 65533) {
18
+ srcStart += 2;
19
+ while (srcStart < srcEnd) {
20
+ char = load<u16>(srcStart) - 48;
21
+ if (char < 10) {
22
+ // @ts-ignore: type
23
+ val = (val * 10 + char) as T;
24
+ } else if (char == 101 || char == 69) {
25
+ srcStart += 2;
26
+ char = load<u16>(srcStart);
27
+ if (char == 45) {
28
+ // @ts-ignore: type
29
+ return -(val / pow10(atoi(srcStart + 2, srcEnd)));
30
+ } else {
31
+ // @ts-ignore: type
32
+ return -(val * pow10(atoi(srcStart, srcEnd)));
33
+ }
34
+ }
35
+ srcStart += 2;
36
+ }
37
+ return -val as T;
38
+ } else {
39
+ while (srcStart < srcEnd) {
40
+ char = load<u16>(srcStart) - 48; // this operation is repeated twice
41
+ if (char < 10) {
42
+ // @ts-ignore: type
43
+ val = (val * 10 + char) as T;
44
+ } else if (char == 101 || char == 69) {
45
+ srcStart += 2;
46
+ char = load<u16>(srcStart);
47
+ if (char == 45) {
48
+ // @ts-ignore: type
49
+ return val / pow10(atoi(srcStart + 2, srcEnd));
50
+ } else {
51
+ // @ts-ignore: type
52
+ return val * pow10(atoi(srcStart, srcEnd));
53
+ }
54
+ }
55
+ srcStart += 2;
56
+ }
57
+ return val as T;
58
+ }
59
+ }
60
+ }
61
+
62
+ // @ts-ignore: Decorator valid here
63
+ @inline function pow10<T extends number>(x: u16): T {
64
+ if (sizeof<T>() == 8) {
65
+ return <T>load<u64>(POW_TEN_TABLE_64 + x);
66
+ } else {
67
+ return <T>load<u32>(POW_TEN_TABLE_32 + x);
68
+ }
69
+ }
package/bench.js CHANGED
@@ -34,9 +34,9 @@ const vec = {
34
34
  let data;
35
35
 
36
36
  const bench = new Bench({ time: 1000 })
37
- /*.add("stringify float", () => {
38
- data = JSON.stringify(1.2345)
39
- })
37
+ .add("stringify float", () => {
38
+ data = JSON.stringify('h\\ello wor"ld');
39
+ }) /*
40
40
  .add("parse float", () => {
41
41
  data = JSON.parse("1.2345")
42
42
  })
@@ -65,10 +65,10 @@ const bench = new Bench({ time: 1000 })
65
65
  .add("Stringify String", () => {
66
66
  data = JSON.stringify('Hello "World!');
67
67
  })
68
- */
68
+
69
69
  .add("Parse String", () => {
70
70
  data = JSON.stringify('hello "world abc');
71
- })
71
+ })*/
72
72
  .todo("unimplemented .add");
73
73
 
74
74
  await bench.run();