-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
1,432 additions
and
746 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,139 +1,144 @@ | ||
import { Virtual } from "as-virtual/assembly"; | ||
import { containsCodePoint, unsafeCharCodeAt } from "../src/util"; | ||
import { nullWord,trueWord,falseWord,aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars"; | ||
import { deserializeString } from "./string"; | ||
import { aCode, backSlashCode, commaCode, eCode, fCode, lCode, leftBraceCode, leftBracketCode, nCode, quoteCode, rCode, rightBraceCode, rightBracketCode, sCode, tCode, uCode } from "../src/chars"; | ||
import { isSpace } from "util/string"; | ||
|
||
// @ts-ignore: Decorator | ||
@inline export function deserializeObject<T>(data: string, initializeDefaultValues: boolean): T { | ||
const schema: nonnull<T> = changetype<nonnull<T>>( | ||
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()) | ||
); | ||
// @ts-ignore | ||
if (initializeDefaultValues) schema.__JSON_Initialize(); | ||
const key = Virtual.createEmpty<string>(); | ||
let isKey = false; | ||
let depth = 0; | ||
let outerLoopIndex = 1; | ||
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) { | ||
const char = unsafeCharCodeAt(data, outerLoopIndex); | ||
if (char === leftBracketCode) { | ||
for ( | ||
let arrayValueIndex = outerLoopIndex; | ||
arrayValueIndex < data.length - 1; | ||
arrayValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, arrayValueIndex); | ||
if (char === leftBracketCode) { | ||
depth++; | ||
} else if (char === rightBracketCode) { | ||
depth--; | ||
if (depth === 0) { | ||
++arrayValueIndex; | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data, outerLoopIndex, arrayValueIndex, initializeDefaultValues); | ||
outerLoopIndex = arrayValueIndex; | ||
isKey = false; | ||
break; | ||
} | ||
const schema: nonnull<T> = changetype<nonnull<T>>( | ||
__new(offsetof<nonnull<T>>(), idof<nonnull<T>>()) | ||
); | ||
|
||
// @ts-ignore | ||
if (initializeDefaultValues) schema.__INITIALIZE(); | ||
|
||
let key_start: i32 = 0; | ||
let key_end: i32 = 0; | ||
let isKey = false; | ||
let depth = 0; | ||
let outerLoopIndex = 1; | ||
for (; outerLoopIndex < data.length - 1; outerLoopIndex++) { | ||
const char = unsafeCharCodeAt(data, outerLoopIndex); | ||
if (char === leftBracketCode) { | ||
for ( | ||
let arrayValueIndex = outerLoopIndex; | ||
arrayValueIndex < data.length - 1; | ||
arrayValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, arrayValueIndex); | ||
if (char === leftBracketCode) { | ||
depth++; | ||
} else if (char === rightBracketCode) { | ||
depth--; | ||
if (depth === 0) { | ||
++arrayValueIndex; | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, arrayValueIndex); | ||
outerLoopIndex = arrayValueIndex; | ||
isKey = false; | ||
break; | ||
} | ||
} | ||
} else if (char === leftBraceCode) { | ||
for ( | ||
let objectValueIndex = outerLoopIndex; | ||
objectValueIndex < data.length - 1; | ||
objectValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, objectValueIndex); | ||
if (char === leftBraceCode) { | ||
depth++; | ||
} else if (char === rightBraceCode) { | ||
depth--; | ||
if (depth === 0) { | ||
++objectValueIndex; | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data, outerLoopIndex, objectValueIndex, initializeDefaultValues); | ||
outerLoopIndex = objectValueIndex; | ||
isKey = false; | ||
break; | ||
} | ||
} | ||
} else if (char === leftBraceCode) { | ||
for ( | ||
let objectValueIndex = outerLoopIndex; | ||
objectValueIndex < data.length - 1; | ||
objectValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, objectValueIndex); | ||
if (char === leftBraceCode) { | ||
depth++; | ||
} else if (char === rightBraceCode) { | ||
depth--; | ||
if (depth === 0) { | ||
++objectValueIndex; | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, objectValueIndex); | ||
outerLoopIndex = objectValueIndex; | ||
isKey = false; | ||
break; | ||
} | ||
} | ||
} else if (char === quoteCode) { | ||
let escaping = false; | ||
for ( | ||
let stringValueIndex = ++outerLoopIndex; | ||
stringValueIndex < data.length - 1; | ||
stringValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, stringValueIndex); | ||
if (char === backSlashCode && !escaping) { | ||
escaping = true; | ||
} else { | ||
if (char === quoteCode && !escaping) { | ||
if (isKey === false) { | ||
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences | ||
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) { | ||
key.reinst(deserializeString(data, outerLoopIndex - 1, stringValueIndex)); | ||
} else { | ||
key.reinst(data, outerLoopIndex, stringValueIndex); | ||
} | ||
isKey = true; | ||
} | ||
} else if (char === quoteCode) { | ||
let escaping = false; | ||
for ( | ||
let stringValueIndex = ++outerLoopIndex; | ||
stringValueIndex < data.length - 1; | ||
stringValueIndex++ | ||
) { | ||
const char = unsafeCharCodeAt(data, stringValueIndex); | ||
if (char === backSlashCode && !escaping) { | ||
escaping = true; | ||
} else { | ||
if (char === quoteCode && !escaping) { | ||
if (isKey === false) { | ||
// perf: we can avoid creating a new string here if the key doesn't contain any escape sequences | ||
if (containsCodePoint(data, backSlashCode, outerLoopIndex, stringValueIndex)) { | ||
key_start = outerLoopIndex - 1; | ||
key_end = stringValueIndex; | ||
console.log(`[KEY-00]: ${data.slice(outerLoopIndex - 1, stringValueIndex)}`) | ||
} else { | ||
const value = deserializeString(data, outerLoopIndex - 1, stringValueIndex); | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, value, 0, value.length, initializeDefaultValues); | ||
isKey = false; | ||
key_start = outerLoopIndex; | ||
key_end = stringValueIndex; | ||
console.log(`[KEY-01]: ${data.slice(outerLoopIndex, stringValueIndex)}`) | ||
} | ||
outerLoopIndex = ++stringValueIndex; | ||
break; | ||
isKey = true; | ||
} else { | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, stringValueIndex); | ||
isKey = false; | ||
} | ||
escaping = false; | ||
} | ||
} | ||
} else if ( | ||
char == nCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode | ||
) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, nullWord, 0, 4, initializeDefaultValues); | ||
isKey = false; | ||
} else if ( | ||
char === tCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === rCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === uCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode | ||
) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, trueWord, 0, 4, initializeDefaultValues); | ||
isKey = false; | ||
} else if ( | ||
char === fCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === aCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === lCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === sCode && | ||
unsafeCharCodeAt(data, ++outerLoopIndex) === eCode | ||
) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, falseWord, 0, 5, initializeDefaultValues); | ||
isKey = false; | ||
} else if ((char >= 48 && char <= 57) || char === 45) { | ||
let numberValueIndex = ++outerLoopIndex; | ||
for (; numberValueIndex < data.length; numberValueIndex++) { | ||
const char = unsafeCharCodeAt(data, numberValueIndex); | ||
if (char === commaCode || char === rightBraceCode || isSpace(char)) { | ||
// @ts-ignore | ||
schema.__JSON_Set_Key(key, data, outerLoopIndex - 1, numberValueIndex, initializeDefaultValues); | ||
outerLoopIndex = numberValueIndex; | ||
isKey = false; | ||
outerLoopIndex = ++stringValueIndex; | ||
break; | ||
} | ||
escaping = false; | ||
} | ||
} | ||
} else if ( | ||
char == nCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 1) === uCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 2) === lCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 3) === lCode | ||
) { | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4); | ||
outerLoopIndex += 3; | ||
isKey = false; | ||
} else if ( | ||
char === tCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 1) === rCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 2) === uCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 3) === eCode | ||
) { | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 4); | ||
outerLoopIndex += 3; | ||
isKey = false; | ||
} else if ( | ||
char === fCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 1) === aCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 2) === lCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 3) === sCode && | ||
unsafeCharCodeAt(data, outerLoopIndex + 4) === eCode | ||
) { | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex, outerLoopIndex + 5); | ||
outerLoopIndex += 4; | ||
isKey = false; | ||
} else if ((char >= 48 && char <= 57) || char === 45) { | ||
let numberValueIndex = ++outerLoopIndex; | ||
for (; numberValueIndex < data.length; numberValueIndex++) { | ||
const char = unsafeCharCodeAt(data, numberValueIndex); | ||
if (char === commaCode || char === rightBraceCode || isSpace(char)) { | ||
// @ts-ignore | ||
schema.__DESERIALIZE(data, key_start, key_end, outerLoopIndex - 1, numberValueIndex); | ||
outerLoopIndex = numberValueIndex; | ||
isKey = false; | ||
break; | ||
} | ||
} | ||
} | ||
return schema; | ||
} | ||
} | ||
return schema; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.