(ast: SchemaAST.Objects)
| 2487 | } |
| 2488 | |
| 2489 | function compileStruct(ast: SchemaAST.Objects): StructLayout { |
| 2490 | const fields: Array<Field> = [] |
| 2491 | const types: Array<SchemaAST.AST> = [] |
| 2492 | const idNames = new Map<number, Array<string>>() |
| 2493 | for (const ps of ast.propertySignatures) { |
| 2494 | if (typeof ps.name === "symbol") { |
| 2495 | throw new Error("Binary layout: symbol property names are illegal") |
| 2496 | } |
| 2497 | const name = String(ps.name) |
| 2498 | const annotations = ps.type.context?.annotations |
| 2499 | const explicit = annotations?.[FIELD_ID_ANNOTATION_KEY] |
| 2500 | const id = typeof explicit === "number" ? explicit : fnv32(utf8Encode.encode(name)) |
| 2501 | if (!Number.isInteger(id) || id < 0 || id > 0xFFFFFFFF) { |
| 2502 | throw new Error(`Binary layout: illegal field id for ${name}`) |
| 2503 | } |
| 2504 | const names = idNames.get(id) |
| 2505 | if (names === undefined) idNames.set(id, [name]) |
| 2506 | else names.push(name) |
| 2507 | fields.push({ |
| 2508 | name, |
| 2509 | id, |
| 2510 | tagBytes: uvarintBytes(id * FIELD_WIRE_FACTOR + FIELD_WIRE_SIZED), |
| 2511 | index: 0, |
| 2512 | optional: ps.type.context?.isOptional === true, |
| 2513 | annotations, |
| 2514 | layout: undefined as unknown as Layout, |
| 2515 | wireMask: 0, |
| 2516 | inline: false |
| 2517 | }) |
| 2518 | types.push(ps.type) |
| 2519 | } |
| 2520 | for (const [id, names] of idNames) { |
| 2521 | if (id === 0 || names.length > 1) { |
| 2522 | throw new Error(`Binary layout field id collision: ${id} (${names.join(", ")})`) |
| 2523 | } |
| 2524 | } |
| 2525 | const extra: Array<ExtraSignature> = [] |
| 2526 | for (const is of ast.indexSignatures) { |
| 2527 | if (parameterHasSymbol(is.parameter)) { |
| 2528 | throw new Error("Binary layout: symbol property names are illegal") |
| 2529 | } |
| 2530 | extra.push({ parameter: is.parameter, layout: undefined as unknown as Layout, wireMask: 0 }) |
| 2531 | } |
| 2532 | const layout: StructLayout = { |
| 2533 | _: "struct", |
| 2534 | ast, |
| 2535 | fields, |
| 2536 | byId: new Map(), |
| 2537 | extra, |
| 2538 | extraAll: extra.length === 1 && matchesEveryKey(extra[0].parameter) ? extra[0] : undefined, |
| 2539 | names: new Set(fields.map((f) => f.name)), |
| 2540 | extraShape: undefined, |
| 2541 | optionalCount: 0, |
| 2542 | requiredMask: 0, |
| 2543 | requiredWide: false |
| 2544 | } |
| 2545 | memo.set(ast, layout) |
| 2546 | for (let i = 0; i < fields.length; i++) { |
no test coverage detected