|
1 | | -import { deserializeStaticArrayInteger } from "./staticarray/integer"; |
2 | | -import { deserializeStaticArrayFloat } from "./staticarray/float"; |
| 1 | +import { OBJECT, TOTAL_OVERHEAD } from "rt/common"; |
| 2 | +import { JSON } from "../.."; |
| 3 | +import { deserializeArbitraryArray } from "./array/arbitrary"; |
| 4 | +import { deserializeArrayArray } from "./array/array"; |
| 5 | +import { deserializeBooleanArray } from "./array/bool"; |
| 6 | +import { deserializeBoxArray } from "./array/box"; |
| 7 | +import { deserializeFloatArray } from "./array/float"; |
| 8 | +import { deserializeIntegerArray } from "./array/integer"; |
| 9 | +import { deserializeMapArray } from "./array/map"; |
| 10 | +import { deserializeObjectArray } from "./array/object"; |
| 11 | +import { deserializeRawArray } from "./array/raw"; |
| 12 | +import { deserializeStructArray } from "./array/struct"; |
| 13 | +import { deserializeStringArray } from "./array/string"; |
3 | 14 | import { deserializeStaticArrayBoolean } from "./staticarray/bool"; |
| 15 | +import { deserializeStaticArrayFloat } from "./staticarray/float"; |
| 16 | +import { deserializeStaticArrayInteger } from "./staticarray/integer"; |
4 | 17 | import { deserializeStaticArrayString } from "./staticarray/string"; |
5 | | -import { deserializeStaticArrayArray } from "./staticarray/array"; |
6 | | -import { deserializeStaticArrayStruct } from "./staticarray/struct"; |
| 18 | + |
| 19 | +@inline function materializeStaticArray<T extends StaticArray<any>>(src: valueof<T>[], dst: usize): T { |
| 20 | + const byteLength = (<usize>src.length) << alignof<valueof<T>>(); |
| 21 | + let out = dst; |
| 22 | + |
| 23 | + if (!out) { |
| 24 | + out = __new(byteLength, idof<T>()); |
| 25 | + } else if (changetype<OBJECT>(out - TOTAL_OVERHEAD).rtSize != byteLength) { |
| 26 | + out = __renew(out, byteLength); |
| 27 | + } |
| 28 | + |
| 29 | + const typed = changetype<T>(out); |
| 30 | + for (let i = 0; i < src.length; i++) { |
| 31 | + unchecked((typed[i] = unchecked(src[i]))); |
| 32 | + } |
| 33 | + return typed; |
| 34 | +} |
7 | 35 |
|
8 | 36 | export function deserializeStaticArray<T extends StaticArray<any>>(srcStart: usize, srcEnd: usize, dst: usize): T { |
9 | 37 | if (isString<valueof<T>>()) { |
10 | | - return <T>deserializeStaticArrayString(srcStart, srcEnd, dst); |
| 38 | + return changetype<T>(deserializeStaticArrayString(srcStart, srcEnd, dst)); |
11 | 39 | } else if (isBoolean<valueof<T>>()) { |
12 | 40 | return deserializeStaticArrayBoolean<T>(srcStart, srcEnd, dst); |
13 | 41 | } else if (isInteger<valueof<T>>()) { |
14 | 42 | return deserializeStaticArrayInteger<T>(srcStart, srcEnd, dst); |
15 | 43 | } else if (isFloat<valueof<T>>()) { |
16 | 44 | return deserializeStaticArrayFloat<T>(srcStart, srcEnd, dst); |
17 | | - } else if (isArrayLike<valueof<T>>()) { |
18 | | - return deserializeStaticArrayArray<T>(srcStart, srcEnd, dst); |
| 45 | + } else if (isArray<valueof<T>>()) { |
| 46 | + return materializeStaticArray<T>(deserializeArrayArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
19 | 47 | } else if (isManaged<valueof<T>>() || isReference<valueof<T>>()) { |
20 | 48 | const type = changetype<nonnull<valueof<T>>>(0); |
21 | | - if (isDefined(type.__DESERIALIZE)) { |
22 | | - return deserializeStaticArrayStruct<T>(srcStart, srcEnd, dst); |
| 49 | + if (type instanceof StaticArray) { |
| 50 | + return materializeStaticArray<T>(deserializeArrayArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
| 51 | + } else if (type instanceof JSON.Value) { |
| 52 | + return materializeStaticArray<T>(changetype<valueof<T>[]>(deserializeArbitraryArray(srcStart, srcEnd, 0)), dst); |
| 53 | + } else if (type instanceof JSON.Box) { |
| 54 | + return materializeStaticArray<T>(changetype<valueof<T>[]>(deserializeBoxArray<valueof<T>[]>(srcStart, srcEnd, 0)), dst); |
| 55 | + } else if (type instanceof JSON.Obj) { |
| 56 | + return materializeStaticArray<T>(deserializeObjectArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
| 57 | + } else if (type instanceof JSON.Raw) { |
| 58 | + return materializeStaticArray<T>(changetype<valueof<T>[]>(deserializeRawArray(srcStart, srcEnd, 0)), dst); |
| 59 | + } else if (type instanceof Map) { |
| 60 | + return materializeStaticArray<T>(deserializeMapArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
| 61 | + // @ts-ignore: supplied by transform |
| 62 | + } else if (isDefined(type.__DESERIALIZE_CUSTOM)) { |
| 63 | + return materializeStaticArray<T>(deserializeStructArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
| 64 | + // @ts-ignore: supplied by transform |
| 65 | + } else if (isDefined(type.__DESERIALIZE)) { |
| 66 | + return materializeStaticArray<T>(deserializeStructArray<valueof<T>[]>(srcStart, srcEnd, 0), dst); |
23 | 67 | } |
24 | | - throw new Error("Could not parse static array of type " + nameof<T>() + "!"); |
25 | | - } else { |
26 | | - throw new Error("Could not parse static array of type " + nameof<T>() + "!"); |
27 | 68 | } |
| 69 | + |
| 70 | + throw new Error("Could not parse static array of type " + nameof<T>() + "!"); |
28 | 71 | } |
0 commit comments