/** * Turning a parameter record's 32-bit value field into a typed value, and back. * * A parameter record is 8 bytes: u32 (name index in the low 24 bits, type id in the high * 8 bits) and u32 `v`. For values of at most INLINE_SIZE bytes, `v` holds the value. For a * string, `v` is either a name-table index with the top bit set, or an offset of a * NUL-terminated UTF-8 string in the complex-data area. For any other larger value, `v` is * an offset in the complex-data area. */ import type { BlkValue } from '../model.ts'; import { BlkFormatError } from '../errors.ts'; import { isInline, typeById, typeByName } from '../param-types.ts'; import type { ByteWriter } from './byte-writer.ts'; import { decodeText, encodeText } from './text-bytes.ts'; export const NAME_TABLE_STRING_FLAG = 0x8000_0000; const bits = new DataView(new ArrayBuffer(4)); export interface DecodeContext { readonly names: readonly string[]; readonly complex: Uint8Array; } export function decodeValue(typeId: number, v: number, context: DecodeContext): BlkValue { const info = typeById(typeId); if (info.type === 'string') return { type: 'string', value: decodeString(v, context) }; if (isInline(info)) return decodeInline(info.type, v); const { complex } = context; if (v + info.size > complex.length) { throw new BlkFormatError(`${info.type} value at complex offset ${v} runs past the end (${complex.length} bytes)`); } const view = new DataView(complex.buffer, complex.byteOffset + v, info.size); const floats = (count: number) => Array.from({ length: count }, (_, i) => view.getFloat32(i * 4, true)); const ints = (count: number) => Array.from({ length: count }, (_, i) => view.getInt32(i * 4, true)); switch (info.type) { case 'point2': return { type: 'point2', value: floats(2) as [number, number] }; case 'point3': return { type: 'point3', value: floats(3) as [number, number, number] }; case 'point4': return { type: 'point4', value: floats(4) as [number, number, number, number] }; case 'ipoint2': return { type: 'ipoint2', value: ints(2) as [number, number] }; case 'ipoint3': return { type: 'ipoint3', value: ints(3) as [number, number, number] }; case 'ipoint4': return { type: 'ipoint4', value: ints(4) as [number, number, number, number] }; case 'matrix': return { type: 'matrix', value: floats(12) }; case 'int64': return { type: 'int64', value: view.getBigInt64(0, true) }; default: throw new BlkFormatError(`no complex-data decoding for ${info.type}`); } } /** The value field for an inline value. */ export function encodeInline(value: BlkValue): number { switch (value.type) { case 'int': return value.value | 0; case 'bool': return value.value ? 1 : 0; case 'color': return value.value >>> 0; case 'real': bits.setFloat32(0, value.value, true); return bits.getUint32(0, true); default: throw new BlkFormatError(`${value.type} is not stored inline`); } } export function storesInline(value: BlkValue): boolean { return isInline(typeByName(value.type)); } /** Append a string to the complex-data area; returns its offset. */ export function appendString(complex: ByteWriter, text: string): number { const encoded = encodeText(text); if (encoded.includes(0)) throw new BlkFormatError('a string value cannot contain a NUL character'); const offset = complex.length; complex.bytes(encoded); complex.u8(0); return offset; } /** Append a non-string, non-inline value to the complex-data area; returns its offset. */ export function appendComplex(complex: ByteWriter, value: BlkValue): number { const offset = complex.length; const size = typeByName(value.type).size; const view = new DataView(new ArrayBuffer(size)); switch (value.type) { case 'point2': case 'point3': case 'point4': case 'matrix': value.value.forEach((item, i) => view.setFloat32(i * 4, item, true)); break; case 'ipoint2': case 'ipoint3': case 'ipoint4': value.value.forEach((item, i) => view.setInt32(i * 4, item, true)); break; case 'int64': view.setBigInt64(0, value.value, true); break; default: throw new BlkFormatError(`${value.type} is not stored in the complex-data area`); } if (value.type === 'matrix' && value.value.length !== 12) { throw new BlkFormatError(`a matrix has 12 numbers, got ${value.value.length}`); } complex.bytes(new Uint8Array(view.buffer)); return offset; } function decodeString(v: number, { names, complex }: DecodeContext): string { if (v & NAME_TABLE_STRING_FLAG) { const index = v & ~NAME_TABLE_STRING_FLAG; const name = names[index]; if (name === undefined) throw new BlkFormatError(`string refers to name ${index}, table has ${names.length}`); return name; } const end = complex.indexOf(0, v); if (v >= complex.length || end < 0) throw new BlkFormatError(`string at complex offset ${v} is not terminated`); return decodeText(complex.subarray(v, end)); } function decodeInline(type: BlkValue['type'], v: number): BlkValue { switch (type) { case 'int': return { type: 'int', value: v | 0 }; case 'bool': return { type: 'bool', value: (v & 0xff) !== 0 }; case 'color': return { type: 'color', value: v >>> 0 }; case 'real': bits.setUint32(0, v, true); return { type: 'real', value: bits.getFloat32(0, true) }; default: throw new BlkFormatError(`${type} is not stored inline`); } }