Skip to content

Commit 56be9a7

Browse files
liamappelbecommit-bot@chromium.org
authored andcommitted
[wasm] Port about half of the old dart:wasm tests to package:wasm
The rest of the tests rely of features I haven't implemented yet. I'll port them as I implement them. Bug: #37882 Change-Id: I3465f10055db8d82148004ea975c0ed3b2928178 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/162780 Reviewed-by: Ryan Macnak <[email protected]> Commit-Queue: Liam Appelbe <[email protected]>
1 parent 8f60ceb commit 56be9a7

15 files changed

+61
-132
lines changed

pkg/wasm/lib/src/function.dart

+4-3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,14 @@ import 'package:ffi/ffi.dart';
99

1010
/// WasmFunction is a callable function from a WasmInstance.
1111
class WasmFunction {
12+
String _name;
1213
Pointer<WasmerExportFunc> _func;
1314
List<int> _argTypes;
1415
int _returnType;
1516
Pointer<WasmerValue> _args;
1617
Pointer<WasmerValue> _result;
1718

18-
WasmFunction(this._func, this._argTypes, this._returnType) {
19+
WasmFunction(this._name, this._func, this._argTypes, this._returnType) {
1920
_args = allocate<WasmerValue>(count: _argTypes.length);
2021
_result = allocate<WasmerValue>();
2122
for (var i = 0; i < _argTypes.length; ++i) {
@@ -46,11 +47,11 @@ class WasmFunction {
4647

4748
dynamic apply(List<dynamic> args) {
4849
if (args.length != _argTypes.length) {
49-
throw Exception("Wrong number arguments for WASM function");
50+
throw ArgumentError("Wrong number arguments for WASM function: $_name");
5051
}
5152
for (var i = 0; i < args.length; ++i) {
5253
if (!_fillArg(args[i], i)) {
53-
throw Exception("Bad argument type for WASM function");
54+
throw ArgumentError("Bad argument type for WASM function: $_name");
5455
}
5556
}
5657
WasmRuntime().call(_func, _args, _argTypes.length, _result,

pkg/wasm/lib/src/module.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ class WasmInstance {
7373
String name = runtime.exportName(e);
7474
if (kind == WasmerImpExpKindFunction) {
7575
var f = runtime.exportToFunction(e);
76-
_functions[name] =
77-
WasmFunction(f, runtime.getArgTypes(f), runtime.getReturnType(f));
76+
_functions[name] = WasmFunction(
77+
name, f, runtime.getArgTypes(f), runtime.getReturnType(f));
7878
}
7979
}
8080
}
8181

82+
/// Searches the instantiated module for the given function. Returns null if
83+
/// it is not found.
8284
dynamic lookupFunction(String name) {
8385
return _functions[name];
8486
}

pkg/wasm/lib/src/runtime.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ class WasmRuntime {
314314
throw Exception("Multiple return values are not supported");
315315
}
316316
var returnsPtr = allocate<Uint32>();
317-
result = _export_func_params(func, returnsPtr, 1);
317+
result = _export_func_returns(func, returnsPtr, 1);
318318
if (result != WasmerResultOk) {
319319
free(returnsPtr);
320320
throw Exception("Failed to get WASM function args");

tests/lib/wasm/basic_test.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test that we can load a wasm module, find a function, and call it.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -21,8 +21,10 @@ void main() {
2121
]);
2222

2323
var inst = WasmModule(data).instantiate(WasmImports());
24-
var fn = inst.lookupFunction<Int64 Function(Int64)>("square");
25-
int n = fn.call([1234]);
24+
var fn = inst.lookupFunction("square");
25+
int n = fn(1234);
2626

2727
Expect.equals(1234 * 1234, n);
28+
29+
Expect.isNull(inst.lookupFunction("not_a_function"));
2830
}

tests/lib/wasm/corrupted_error_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test error thrown when the wasm module is corrupted.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -16,5 +16,5 @@ void main() {
1616
0x7e, 0x0b,
1717
]);
1818

19-
Expect.throwsArgumentError(() => WasmModule(data));
19+
Expect.throws(() => WasmModule(data));
2020
}

tests/lib/wasm/fn_call_error_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test error thrown when a function is called with the wrong args.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -21,9 +21,9 @@ void main() {
2121
]);
2222

2323
var inst = WasmModule(data).instantiate(WasmImports());
24-
var fn = inst.lookupFunction<Int64 Function(Int64)>("square");
24+
var fn = inst.lookupFunction("square");
2525

26-
Expect.throwsArgumentError(() => fn.call([]));
27-
Expect.throwsArgumentError(() => fn.call([1, 2, 3]));
28-
Expect.throwsArgumentError(() => fn.call([1.23]));
26+
Expect.throwsArgumentError(() => fn());
27+
Expect.throwsArgumentError(() => fn(1, 2, 3));
28+
Expect.throwsArgumentError(() => fn(1.23));
2929
}

tests/lib/wasm/fn_mismatch_error_test.dart

-39
This file was deleted.

tests/lib/wasm/numerics_test.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test numeric types.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -30,20 +30,20 @@ void main() {
3030
]);
3131

3232
var inst = WasmModule(data).instantiate(WasmImports());
33-
var addI64 = inst.lookupFunction<Int64 Function(Int64, Int64)>("addI64");
34-
var addI32 = inst.lookupFunction<Int32 Function(Int32, Int32)>("addI32");
35-
var addF64 = inst.lookupFunction<Double Function(Double, Double)>("addF64");
36-
var addF32 = inst.lookupFunction<Float Function(Float, Float)>("addF32");
33+
var addI64 = inst.lookupFunction("addI64");
34+
var addI32 = inst.lookupFunction("addI32");
35+
var addF64 = inst.lookupFunction("addF64");
36+
var addF32 = inst.lookupFunction("addF32");
3737

38-
int i64 = addI64.call([0x123456789ABCDEF, 0xFEDCBA987654321]);
38+
int i64 = addI64(0x123456789ABCDEF, 0xFEDCBA987654321);
3939
Expect.equals(0x1111111111111110, i64);
4040

41-
int i32 = addI32.call([0xABCDEF, 0xFEDCBA]);
41+
int i32 = addI32(0xABCDEF, 0xFEDCBA);
4242
Expect.equals(0x1aaaaa9, i32);
4343

44-
double f64 = addF64.call([1234.5678, 8765.4321]);
44+
double f64 = addF64(1234.5678, 8765.4321);
4545
Expect.approxEquals(9999.9999, f64, 1e-6);
4646

47-
double f32 = addF32.call([1234.5678, 8765.4321]);
47+
double f32 = addF32(1234.5678, 8765.4321);
4848
Expect.approxEquals(9999.9999, f32, 1e-3);
4949
}

tests/lib/wasm/void_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test functions with void return type, and functions that take no args.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -26,9 +26,9 @@ void main() {
2626
]);
2727

2828
var inst = WasmModule(data).instantiate(WasmImports());
29-
var setFn = inst.lookupFunction<Void Function(Int64, Int64)>("set");
30-
var getFn = inst.lookupFunction<Int64 Function()>("get");
31-
Expect.isNull(setFn.call([123, 456]));
32-
int n = getFn.call([]);
29+
var setFn = inst.lookupFunction("set");
30+
var getFn = inst.lookupFunction("get");
31+
Expect.isNull(setFn(123, 456));
32+
int n = getFn();
3333
Expect.equals(123 + 456, n);
3434
}

tests/lib_2/wasm/basic_test.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test that we can load a wasm module, find a function, and call it.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -21,8 +21,10 @@ void main() {
2121
]);
2222

2323
var inst = WasmModule(data).instantiate(WasmImports());
24-
var fn = inst.lookupFunction<Int64 Function(Int64)>("square");
25-
int n = fn.call([1234]);
24+
var fn = inst.lookupFunction("square");
25+
int n = fn.call(1234);
2626

2727
Expect.equals(1234 * 1234, n);
28+
29+
Expect.isNull(inst.lookupFunction("not_a_function"));
2830
}

tests/lib_2/wasm/corrupted_error_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test error thrown when the wasm module is corrupted.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -16,5 +16,5 @@ void main() {
1616
0x7e, 0x0b,
1717
]);
1818

19-
Expect.throwsArgumentError(() => WasmModule(data));
19+
Expect.throws(() => WasmModule(data));
2020
}

tests/lib_2/wasm/fn_call_error_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test error thrown when a function is called with the wrong args.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -21,9 +21,9 @@ void main() {
2121
]);
2222

2323
var inst = WasmModule(data).instantiate(WasmImports());
24-
var fn = inst.lookupFunction<Int64 Function(Int64)>("square");
24+
var fn = inst.lookupFunction("square");
2525

26-
Expect.throwsArgumentError(() => fn.call([]));
27-
Expect.throwsArgumentError(() => fn.call([1, 2, 3]));
28-
Expect.throwsArgumentError(() => fn.call([1.23]));
26+
Expect.throwsArgumentError(() => fn());
27+
Expect.throwsArgumentError(() => fn(1, 2, 3));
28+
Expect.throwsArgumentError(() => fn(1.23));
2929
}

tests/lib_2/wasm/fn_mismatch_error_test.dart

-39
This file was deleted.

tests/lib_2/wasm/numerics_test.dart

+9-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test numeric types.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -30,20 +30,20 @@ void main() {
3030
]);
3131

3232
var inst = WasmModule(data).instantiate(WasmImports());
33-
var addI64 = inst.lookupFunction<Int64 Function(Int64, Int64)>("addI64");
34-
var addI32 = inst.lookupFunction<Int32 Function(Int32, Int32)>("addI32");
35-
var addF64 = inst.lookupFunction<Double Function(Double, Double)>("addF64");
36-
var addF32 = inst.lookupFunction<Float Function(Float, Float)>("addF32");
33+
var addI64 = inst.lookupFunction("addI64");
34+
var addI32 = inst.lookupFunction("addI32");
35+
var addF64 = inst.lookupFunction("addF64");
36+
var addF32 = inst.lookupFunction("addF32");
3737

38-
int i64 = addI64.call([0x123456789ABCDEF, 0xFEDCBA987654321]);
38+
int i64 = addI64(0x123456789ABCDEF, 0xFEDCBA987654321);
3939
Expect.equals(0x1111111111111110, i64);
4040

41-
int i32 = addI32.call([0xABCDEF, 0xFEDCBA]);
41+
int i32 = addI32(0xABCDEF, 0xFEDCBA);
4242
Expect.equals(0x1aaaaa9, i32);
4343

44-
double f64 = addF64.call([1234.5678, 8765.4321]);
44+
double f64 = addF64(1234.5678, 8765.4321);
4545
Expect.approxEquals(9999.9999, f64, 1e-6);
4646

47-
double f32 = addF32.call([1234.5678, 8765.4321]);
47+
double f32 = addF32(1234.5678, 8765.4321);
4848
Expect.approxEquals(9999.9999, f32, 1e-3);
4949
}

tests/lib_2/wasm/void_test.dart

+5-5
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Test functions with void return type, and functions that take no args.
66

77
import "package:expect/expect.dart";
8-
import "dart:wasm";
8+
import "package:wasm/wasm.dart";
99
import "dart:typed_data";
1010

1111
void main() {
@@ -26,9 +26,9 @@ void main() {
2626
]);
2727

2828
var inst = WasmModule(data).instantiate(WasmImports());
29-
var setFn = inst.lookupFunction<Void Function(Int64, Int64)>("set");
30-
var getFn = inst.lookupFunction<Int64 Function()>("get");
31-
Expect.isNull(setFn.call([123, 456]));
32-
int n = getFn.call([]);
29+
var setFn = inst.lookupFunction("set");
30+
var getFn = inst.lookupFunction("get");
31+
Expect.isNull(setFn(123, 456));
32+
int n = getFn();
3333
Expect.equals(123 + 456, n);
3434
}

0 commit comments

Comments
 (0)