Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test: Exception handling in WASM component model #226605

Closed
2 tasks done
dbaeumer opened this issue Aug 26, 2024 · 1 comment
Closed
2 tasks done

Test: Exception handling in WASM component model #226605

dbaeumer opened this issue Aug 26, 2024 · 1 comment
Assignees
Milestone

Comments

@dbaeumer
Copy link
Member

dbaeumer commented Aug 26, 2024

Refs: microsoft/vscode-wasm#177

Complexity: 3

Create Issue


The latest implementation of VS Code's component model converts result types returned from function and methods into a normal return type (the Ok part) and an exception.

Prerequisites

  1. Rust: installation instructions can be found here
  2. wasm-tools: releases can be found here. You need at least version >= 1.200 to run the example.

Testplan

You can make your own component model sample using the two blog posts as a starter: https://code.visualstudio.com/blogs/2024/05/08/wasm and https://code.visualstudio.com/blogs/2024/06/07/wasm-part2

Easier is to clone the vscode-extension-samples and start off the wasm-component-model sample

  • run npm install

  • add an error-code enumeration to the calculator.wit file inside the types interface. Something like this:

	enum error-code {
		none,
		overflow,
		divide-by-zero
	}
  • use error-code inside the calculator world and change the signature of the calc function to return result<u32, error-code>. The world will look like this:
world calculator {
	use types.{ operation, error-code };
	import log: func(msg: string);

	export calc: func(o: operation) -> result<u32, error-code>;
}
  • run npm run generate:model
  • observe: a new calculator.ts file got created with as an ErrorCode enumeration and a corresponding exception.
  • adapt the Rust code in lib.rs to return Result::Err(ErrorCode::DivideByZero) in case a division by Zero happens.
  • adapt the TypeScript code in extension.ts to catch error when calling a Div operation. Ensure that the return exception if of type Types.ErrorCode.Error_ and its value is error.value === Types.ErrorCode.divideByZero. Something like
		try {
			const div = Types.Operation.Div({ left: 10, right: 0 });
			channel.appendLine(`Div ${api.calc(div)}`);
		} catch (error) {
			if (error instanceof Types.ErrorCode.Error_ && error.value === Types.ErrorCode.divideByZero) {
				channel.appendLine('Division by zero not allowed');
			}
		}
@vs-code-engineering vs-code-engineering bot added this to the August 2024 milestone Aug 26, 2024
@hediet hediet removed their assignment Aug 27, 2024
@hediet
Copy link
Member

hediet commented Aug 27, 2024

Nice work!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants
@dbaeumer @connor4312 @hediet and others