diff --git a/testbeds/component-model/src/calculator.ts b/testbeds/component-model/src/calculator.ts index 9da1ce00..4cc2d051 100644 --- a/testbeds/component-model/src/calculator.ts +++ b/testbeds/component-model/src/calculator.ts @@ -76,6 +76,7 @@ export type Types = { }; export namespace calculator { export type Operation = Types.Operation; + export const Operation = Types.Operation; export type Imports = { generate: () => string; }; diff --git a/wasm-component-model/src/tools/main.ts b/wasm-component-model/src/tools/main.ts index b5a4eb31..d44dfbd5 100644 --- a/wasm-component-model/src/tools/main.ts +++ b/wasm-component-model/src/tools/main.ts @@ -55,8 +55,8 @@ async function run(options: Options): Promise { if (semVersion === null) { process.stderr.write(`wasm-tools --version didn't provide a parsable version number. Output was ${output}.\n`); return 1; - } else if (!semverGte(semVersion, '1.200.0')) { - process.stderr.write(`wit2ts required wasm-tools >= 1.200.0, but found version ${version}.\n`); + } else if (!semverGte(semVersion, '1.226.0')) { + process.stderr.write(`wit2ts required wasm-tools >= 1.226.0, but found version ${version}.\n`); return 1; } let data: string; diff --git a/wasm-component-model/src/tools/wit-json.ts b/wasm-component-model/src/tools/wit-json.ts index e9e59b1c..85e41d09 100644 --- a/wasm-component-model/src/tools/wit-json.ts +++ b/wasm-component-model/src/tools/wit-json.ts @@ -109,7 +109,7 @@ interface AbstractCallable { name: string; docs?: Documentation | undefined; params: Param[]; - results: TypeObject[]; + result?: TypeReference | undefined; } export interface Func extends AbstractCallable { diff --git a/wasm-component-model/src/tools/wit2ts.ts b/wasm-component-model/src/tools/wit2ts.ts index 783293d8..291d3355 100644 --- a/wasm-component-model/src/tools/wit2ts.ts +++ b/wasm-component-model/src/tools/wit2ts.ts @@ -649,20 +649,19 @@ class SymbolTable { if (seenCallables.has(callable)) { return; } - for (const result of Object.values(callable.results)) { - if (TypeReference.isNumber(result.type)) { - const type = this.getType(result.type); - if (TypeKind.isResult(type.kind)) { - if (type.kind.result.err !== null) { - if (TypeReference.isString(type.kind.result.err)) { - this.resultErrorTypes.add(type.kind.result.err); - } else { - let errorType = this.getType(type.kind.result.err); - while (TypeKind.isReference(errorType.kind)) { - errorType = this.getType(errorType.kind.type); - } - this.resultErrorTypes.add(errorType); + const result = callable.result; + if (result !== undefined && TypeReference.isNumber(result)) { + const type = this.getType(result); + if (TypeKind.isResult(type.kind)) { + if (type.kind.result.err !== null) { + if (TypeReference.isString(type.kind.result.err)) { + this.resultErrorTypes.add(type.kind.result.err); + } else { + let errorType = this.getType(type.kind.result.err); + while (TypeKind.isReference(errorType.kind)) { + errorType = this.getType(errorType.kind.type); } + this.resultErrorTypes.add(errorType); } } } @@ -1517,12 +1516,10 @@ class TypeFlattener { public flattenResult(callable: Callable): string[] { const result: string[] = []; - if (callable.results.length === 0) { + if (callable.result === undefined) { result.push('void'); } else { - for (const r of callable.results) { - this.flattenResultType(result, r.type); - } + this.flattenResultType(result, callable.result); } return result; } @@ -3858,12 +3855,8 @@ namespace ResourceEmitter { } let returnType: string = 'void'; const context = TypeScript.TypePrinterContext.create(TypeUsage.witFunction); - if (this.method.results !== null && omitReturn === false) { - if (this.method.results.length === 1) { - returnType = printers.typeScript.printTypeReference(this.method.results[0].type, context); - } else if (this.method.results.length > 1) { - returnType = `[${this.method.results.map(r => printers.typeScript.printTypeReference(r.type, context)).join(', ')}]`; - } + if (this.method.result !== undefined && omitReturn === false) { + returnType = printers.typeScript.printTypeReference(this.method.result, context); } return [params, paramNames, returnType, context.errorClasses.length > 0 ? context.errorClasses : undefined]; } @@ -4075,10 +4068,8 @@ function CallableEmitter 1) { - metaReturnType = `[${this.callable.results.map(r => this.context.printers.metaModel.printTypeReference(r.type, TypeUsage.witFunction)).join(', ')}]`; + if (this.callable.result !== undefined) { + metaReturnType = this.context.printers.metaModel.printTypeReference(this.callable.result, TypeUsage.witFunction); } } return [metaDataParams, metaReturnType]; @@ -4140,10 +4131,8 @@ function CallableEmitter 1) { - returnType = `[${this.callable.results.map(r => this.context.printers.typeScript.printTypeReference(r.type, context)).join(', ')}]`; + if (this.callable.result !== undefined) { + returnType = this.context.printers.typeScript.printTypeReference(this.callable.result, context); } result.return = returnType; result.exceptions = context.errorClasses.length > 0 ? context.errorClasses : undefined;