#4901·baml

[错误] Node bridge 0.19.0: 一个返回整数的可调用主机对象对 `float` 类型进行了异常调用,导致 HostContractViolation

作者: BenSpex创建于 2026年9月16日更新于 2026年9月16日

baml_src/main.baml:

baml
class Size {
    width: float,
}

// A host callable whose declared return holds a float field.
function MeasureClass(measure: () -> Size throws never) -> float throws never {
    measure().width
}

// A host callable whose declared return is a bare float.
function MeasureFloat(measure: () -> float throws never) -> float throws never {
    measure()
}

// A plain argument, not a host callable.
function EchoSize(size: Size) -> float {
    size.width
}

repro.mjs (run with BRIDGE=<path to @boundaryml/baml-bridge/dist/index.js> node repro.mjs):

javascript
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const bridge = await import(process.env.BRIDGE);
const dir = new URL('./baml_src', import.meta.url).pathname;
const handle = bridge.BamlRuntime.initializeRuntime(dir, { 'main.baml': readFileSync(join(dir, 'main.baml'), 'utf8') });
class Size { constructor(init) { Object.assign(this, init); } }
bridge.setTypeMap(bridge.BamlTypeMap.fromLazyEntries({ classes: { 'user.Size': () => Size }, enums: {}, typeAliases: {} }));
const run = async (label, fn, kwargs) => {
  try { console.log(label, '->', JSON.stringify((await bridge.callFunction(handle, fn, kwargs)).result())); }
  catch (e) { console.log(label, '-> ERROR', String(e?.message ?? e).split('\n')[0]); }
};
await run('host returns class, width 612.5', 'MeasureClass', { measure: () => new Size({ width: 612.5 }) });
await run('host returns class, width 612  ', 'MeasureClass', { measure: () => new Size({ width: 612 }) });
await run('host returns float 0.5         ', 'MeasureFloat', { measure: () => 0.5 });
await run('host returns float 1           ', 'MeasureFloat', { measure: () => 1 });
await run('argument class, width 612      ', 'EchoSize', { size: new Size({ width: 612 }) });
process.exit(0);

Output

内容来源: BoundaryML/baml