Move wasm samples to latest wasm-component-model (#1092)

This commit is contained in:
Dirk Bäumer
2024-08-28 13:10:25 +02:00
committed by GitHub
parent a11599ca99
commit d4c646fa3c
10 changed files with 61 additions and 27 deletions

View File

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/node": "^18.14.6",
@ -794,9 +794,9 @@
"dev": true
},
"node_modules/@vscode/wasm-component-model": {
"version": "0.1.0-pre.16",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.16.tgz",
"integrity": "sha512-fe4J7Nz7PKlLuZ3z8zQNp7n/6w+RwDW2kCyHI0dRGQ0Htjz0al3guEynn+o/FywCamywwRUoQO9wIhRJBwvj/w==",
"version": "0.1.0-pre.17",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.17.tgz",
"integrity": "sha512-/oKAi/MNOJskxtsvhKVK/6k+xgqrM/76nYs9zdmnWEuRzoCCmnEpShSRgLfRJrBsMUiVl6vwfJTsj07iqrYNaA==",
"dependencies": {
"semver": "^7.6.0",
"uuid": "^9.0.1",

View File

@ -29,7 +29,7 @@
]
},
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/vscode": "1.89.0",

View File

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/node": "^18.14.6",
@ -794,9 +794,9 @@
"dev": true
},
"node_modules/@vscode/wasm-component-model": {
"version": "0.1.0-pre.16",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.16.tgz",
"integrity": "sha512-fe4J7Nz7PKlLuZ3z8zQNp7n/6w+RwDW2kCyHI0dRGQ0Htjz0al3guEynn+o/FywCamywwRUoQO9wIhRJBwvj/w==",
"version": "0.1.0-pre.17",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.17.tgz",
"integrity": "sha512-/oKAi/MNOJskxtsvhKVK/6k+xgqrM/76nYs9zdmnWEuRzoCCmnEpShSRgLfRJrBsMUiVl6vwfJTsj07iqrYNaA==",
"dependencies": {
"semver": "^7.6.0",
"uuid": "^9.0.1",

View File

@ -29,7 +29,7 @@
]
},
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/vscode": "1.88.0",

View File

@ -9,7 +9,7 @@
"version": "1.0.0",
"license": "MIT",
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/node": "^18.14.6",
@ -794,9 +794,9 @@
"dev": true
},
"node_modules/@vscode/wasm-component-model": {
"version": "0.1.0-pre.16",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.16.tgz",
"integrity": "sha512-fe4J7Nz7PKlLuZ3z8zQNp7n/6w+RwDW2kCyHI0dRGQ0Htjz0al3guEynn+o/FywCamywwRUoQO9wIhRJBwvj/w==",
"version": "0.1.0-pre.17",
"resolved": "https://registry.npmjs.org/@vscode/wasm-component-model/-/wasm-component-model-0.1.0-pre.17.tgz",
"integrity": "sha512-/oKAi/MNOJskxtsvhKVK/6k+xgqrM/76nYs9zdmnWEuRzoCCmnEpShSRgLfRJrBsMUiVl6vwfJTsj07iqrYNaA==",
"dependencies": {
"semver": "^7.6.0",
"uuid": "^9.0.1",

View File

@ -29,7 +29,7 @@
]
},
"dependencies": {
"@vscode/wasm-component-model": "0.1.0-pre.16"
"@vscode/wasm-component-model": "0.1.0-pre.17"
},
"devDependencies": {
"@types/vscode": "1.88.0",

View File

@ -4,7 +4,7 @@
*--------------------------------------------------------------------------------------------*/
/* eslint-disable @typescript-eslint/ban-types */
import * as $wcm from '@vscode/wasm-component-model';
import type { u32, i32 } from '@vscode/wasm-component-model';
import type { u32, i32, ptr, result } from '@vscode/wasm-component-model';
export namespace Types {
export type Operands = {
@ -71,12 +71,27 @@ export namespace Types {
}
}
export type Operation = Operation.Add | Operation.Sub | Operation.Mul | Operation.Div;
export enum ErrorCode {
none = 'none',
overflow = 'overflow',
divideByZero = 'divideByZero'
}
export namespace ErrorCode {
export class Error_ extends $wcm.ResultError<ErrorCode> {
constructor(cause: ErrorCode) {
super(`ErrorCode: ${cause}`, cause);
}
}
}
}
export type Types = {
};
export namespace calculator {
export type Operation = Types.Operation;
export const Operation = Types.Operation;
export type ErrorCode = Types.ErrorCode;
export const ErrorCode = Types.ErrorCode;
export type Imports = {
log: (msg: string) => void;
};
@ -87,6 +102,9 @@ export namespace calculator {
export type Promisify<T> = $wcm.$imports.Promisify<T>;
}
export type Exports = {
/**
* @throws ErrorCode.Error_
*/
calc: (o: Operation) => u32;
};
export namespace Exports {
@ -103,19 +121,22 @@ export namespace Types.$ {
['right', $wcm.u32],
]);
export const Operation = new $wcm.VariantType<Types.Operation, Types.Operation._tt, Types.Operation._vt>([['add', Operands], ['sub', Operands], ['mul', Operands], ['div', Operands]], Types.Operation._ctor);
export const ErrorCode = new $wcm.EnumType<Types.ErrorCode>(['none', 'overflow', 'divideByZero']);
}
export namespace Types._ {
export const id = 'vscode:example/types' as const;
export const witName = 'types' as const;
export const types: Map<string, $wcm.AnyComponentModelType> = new Map<string, $wcm.AnyComponentModelType>([
['Operands', $.Operands],
['Operation', $.Operation]
['Operation', $.Operation],
['ErrorCode', $.ErrorCode]
]);
export type WasmInterface = {
};
}
export namespace calculator.$ {
export const Operation = Types.$.Operation;
export const ErrorCode = Types.$.ErrorCode;
export namespace imports {
export const log = new $wcm.FunctionType<calculator.Imports['log']>('log',[
['msg', $wcm.wstring],
@ -124,7 +145,7 @@ export namespace calculator.$ {
export namespace exports {
export const calc = new $wcm.FunctionType<calculator.Exports['calc']>('calc',[
['o', Operation],
], $wcm.u32);
], new $wcm.ResultType<u32, calculator.ErrorCode>($wcm.u32, ErrorCode, Types.ErrorCode.Error_));
}
}
export namespace calculator._ {
@ -159,7 +180,7 @@ export namespace calculator._ {
}
}
export type Exports = {
'calc': (o_Operation_case: i32, o_Operation_0: i32, o_Operation_1: i32) => i32;
'calc': (o_Operation_case: i32, o_Operation_0: i32, o_Operation_1: i32, result: ptr<result<u32, ErrorCode>>) => void;
};
export function bind(service: calculator.Imports, code: $wcm.Code, context?: $wcm.ComponentModelContext): Promise<calculator.Exports>;
export function bind(service: calculator.Imports.Promisified, code: $wcm.Code, port: $wcm.RAL.ConnectionPort, context?: $wcm.ComponentModelContext): Promise<calculator.Exports.Promisified>;

View File

@ -54,5 +54,12 @@ export async function activate(context: vscode.ExtensionContext): Promise<void>
channel.appendLine(`Mul ${api.calc(mul)}`);
const div = Types.Operation.Div({ left: 10, right: 2 });
channel.appendLine(`Div ${api.calc(div)}`);
try {
channel.appendLine(`Divide by Zero ${api.calc(Types.Operation.Div({ left: 10, right: 0 }))}`);
} catch (error) {
if (error instanceof Types.ErrorCode.Error_ && error.cause === Types.ErrorCode.divideByZero) {
channel.appendLine('Division by zero not allowed');
}
}
}));
}

View File

@ -9,13 +9,13 @@ struct Calculator;
impl Guest for Calculator {
fn calc(op: Operation) -> u32 {
fn calc(op: Operation) -> Result<u32, ErrorCode> {
log(&format!("Starting calculation: {:?}", op));
let result = match op {
Operation::Add(operands) => operands.left + operands.right,
Operation::Sub(operands) => operands.left - operands.right,
Operation::Mul(operands) => operands.left * operands.right,
Operation::Div(operands) => operands.left / operands.right,
let result: Result<u32, ErrorCode> = match op {
Operation::Add(operands) => Result::Ok(operands.left + operands.right),
Operation::Sub(operands) => Result::Ok(operands.left - operands.right),
Operation::Mul(operands) => Result::Ok(operands.left * operands.right),
Operation::Div(operands) => if operands.right == 0 { Result::Err(ErrorCode::DivideByZero) } else { Result::Ok(operands.left / operands.right) }
};
log(&format!("Finished calculation: {:?}", op));
result

View File

@ -13,10 +13,16 @@ interface types {
mul(operands),
div(operands)
}
enum error-code {
none,
overflow,
divide-by-zero
}
}
world calculator {
use types.{ operation };
use types.{ operation, error-code };
import log: func(msg: string);
export calc: func(o: operation) -> u32;
export calc: func(o: operation) -> result<u32, error-code>;
}