diff --git a/packages/compiler/src/compiler/codegen.ts b/packages/compiler/src/compiler/codegen.ts index 847368c..0e278b4 100644 --- a/packages/compiler/src/compiler/codegen.ts +++ b/packages/compiler/src/compiler/codegen.ts @@ -83,7 +83,7 @@ function stringifyId(i: Identifier): Items { return [ { ... i, type: TokenType.STRING, text: JSON.stringify(i.text) } ]; } -function binderTypeGuard(ctx: ExpansionContext, t: TemplateFunction): (binder: Binder, index: number) => Items { +function binderTypeGuard(t: TemplateFunction): (binder: Binder, index: number) => Items { return (binder, index) => { if (binder.id.text[0] === '_') { return t`${`/* Ignoring underscore-prefixed binder ${binder.id.text} */`}`; @@ -103,10 +103,12 @@ function binderTypeGuard(ctx: ExpansionContext, t: TemplateFunction): (binder: B return t`if (typeof (${raw}) !== ${JSON.stringify(typeText)}) return;\n${bind}`; case 'any': return bind; - default: - ctx.emitError(`Cannot emit guard for binding of type: ${JSON.stringify(typeText)}`, - getRange(binder.type)); - return bind; /* act as if "any", for now */ + default: { + const intermediate = t`__v_${''+index}`; + return t`const ${intermediate} = ${binder.type}.__from_preserve__(${raw}); + if (${intermediate} === void 0) return; + const ${[binder.id]} = ${intermediate};`; + } } } }; @@ -158,7 +160,7 @@ export function expand(tree: Items, ctx: ExpansionContext): Items { observer: __SYNDICATE__.Turn.ref(__SYNDICATE__.assertionFacetObserver( (${ctx.argDecl(t, '__vs', '__SYNDICATE__.AnyValue')}) => { if (Array.isArray(__vs)) { -${joinItems(sa.captureBinders.map(binderTypeGuard(ctx, t)), '\n')} +${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')} ${body} } } @@ -231,7 +233,7 @@ ${joinItems(sa.captureBinders.map(binderTypeGuard(ctx, t)), '\n')} const sa = compilePattern(s.pattern); const guardBody = (body: Statement) => t`if (Array.isArray(__vs)) { -${joinItems(sa.captureBinders.map(binderTypeGuard(ctx, t)), '\n')} +${joinItems(sa.captureBinders.map(binderTypeGuard(t)), '\n')} ${body} }`; diff --git a/packages/core/src/runtime/quasivalue.ts b/packages/core/src/runtime/quasivalue.ts index c598101..488dbe8 100644 --- a/packages/core/src/runtime/quasivalue.ts +++ b/packages/core/src/runtime/quasivalue.ts @@ -4,18 +4,12 @@ import { AnyValue, Ref } from './actor.js'; import { Pattern, toPattern } from '../gen/dataspacePatterns.js'; import * as P from './pattern.js'; -import { Value, RecordConstructorInfo, is, Record } from '@preserves/core'; -import { Meta, Type, GenType } from '@preserves/schema'; - -export type DefinitionOrVariantInfo = { - schema: Value, - imports: unknown, - definitionName: symbol, -}; +import { RecordConstructorInfo, is, Record } from '@preserves/core'; +import { Meta, Type, GenType, SchemaDefinition } from '@preserves/schema'; export type QuasiValueConstructorInfo = | { constructorInfo: RecordConstructorInfo } - | { schema(): DefinitionOrVariantInfo } + | { schema(): SchemaDefinition } | { quasiValue(... args: QuasiValue[]): QuasiValue } ;