Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Throw an exception when a wasm trap is hit #88

Merged
merged 2 commits into from
Sep 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions wasm/lib/src/runtime.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -597,9 +597,8 @@ class WasmRuntime {
calloc.free(trapMessage);
final entry = _traps.remove(message);
if (entry == null) {
throw WasmError(
'This case is not (yet) supported. Please file an issue on pkg:wasm.',
);
// TODO(#87): Report a full stack trace to the user.
throw WasmException(message);
}
// ignore: only_throw_errors
throw entry.exception;
Expand Down
11 changes: 11 additions & 0 deletions wasm/lib/src/wasm_error.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ class WasmError extends Error {
@override
String toString() => 'WasmError: $message';
}

/// Exception that wraps exceptions (traps) thrown inside wasm code.
class WasmException implements Exception {
/// Describes the nature of the exception.
final String message;

WasmException(this.message);

@override
String toString() => 'WasmException: $message';
}
5 changes: 5 additions & 0 deletions wasm/test/test_shared.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ import 'package:wasm/src/wasm_error.dart';
Matcher throwsWasmError(Object messageMatcher) => throwsA(
isA<WasmError>().having((p0) => p0.message, 'message', messageMatcher),
);

Matcher throwsWasmException(Object messageMatcher) => throwsA(
isA<WasmException>()
.having((p0) => p0.message, 'message', messageMatcher),
);
33 changes: 33 additions & 0 deletions wasm/test/trap_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

// Test that we can load a wasm module, find a function, and call it.
import 'dart:typed_data';

import 'package:test/test.dart';
import 'package:wasm/wasm.dart';

import 'test_shared.dart';

void main() {
test('basics', () {
// (func $foo (export "foo") (type $t0) (unreachable))
final data = Uint8List.fromList([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, //
0x00, 0x00, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x07,
0x07, 0x05, 0x03, 0x01, 0x00, 0x11, 0x07, 0x10, 0x02, 0x03, 0x66, 0x6f,
0x6f, 0x00, 0x00, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00,
0x0a, 0x05, 0x01, 0x03, 0x00, 0x00, 0x0b, 0x00, 0x2b, 0x04, 0x6e, 0x61,
0x6d, 0x65, 0x01, 0x06, 0x01, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x02, 0x03,
0x01, 0x00, 0x00, 0x04, 0x05, 0x01, 0x00, 0x02, 0x74, 0x30, 0x05, 0x05,
0x01, 0x00, 0x02, 0x54, 0x30, 0x06, 0x09, 0x01, 0x00, 0x06, 0x6d, 0x65,
0x6d, 0x6f, 0x72, 0x79,
]);

final inst = WasmModule(data).builder().build();
final foo = inst.lookupFunction('foo');
// ignore: unnecessary_lambdas
expect(() => foo(), throwsWasmException('unreachable'));
});
}
5 changes: 2 additions & 3 deletions wasm/tool/runtime_template.dart.t
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ class WasmRuntime {
calloc.free(trapMessage);
final entry = _traps.remove(message);
if (entry == null) {
throw WasmError(
'This case is not (yet) supported. Please file an issue on pkg:wasm.',
);
// TODO(#87): Report a full stack trace to the user.
throw WasmException(message);
}
// ignore: only_throw_errors
throw entry.exception;
Expand Down