import { Bytes } from "./bytes"; import { Record, Tuple } from "./record"; import { AsPreserve } from "./symbols"; import { DefaultPointer, Value } from "./values"; export function fromJS(x: any): Value { switch (typeof x) { case 'number': if (!Number.isInteger(x)) { // We require that clients be explicit about integer vs. non-integer types. throw new TypeError("Refusing to autoconvert non-integer number to Single or Double"); } // FALL THROUGH case 'string': case 'symbol': case 'boolean': return x; case 'undefined': case 'function': case 'bigint': break; case 'object': if (x === null) { break; } if (typeof x[AsPreserve] === 'function') { return x[AsPreserve](); } if (Record.isRecord, Tuple>, T>(x)) { return x; } if (Array.isArray(x)) { return x.map>(fromJS); } if (ArrayBuffer.isView(x) || x instanceof ArrayBuffer) { return Bytes.from(x); } // Just... assume it's a T. return (x as T); default: break; } throw new TypeError("Cannot represent JavaScript value as Preserves: " + x); }