diff --git a/.dart_tool/package_config.json b/.dart_tool/package_config.json index 079d7fecd93..93ba1d7389c 100644 --- a/.dart_tool/package_config.json +++ b/.dart_tool/package_config.json @@ -769,12 +769,6 @@ "packageUri": "lib/", "languageVersion": "2.12" }, - { - "name": "wasm", - "rootUri": "../pkg/wasm", - "packageUri": "lib/", - "languageVersion": "2.12" - }, { "name": "watcher", "rootUri": "../third_party/pkg/watcher", diff --git a/.packages b/.packages index 2a1f1af54eb..2bd3c0a2e49 100644 --- a/.packages +++ b/.packages @@ -116,7 +116,6 @@ vector_math:third_party/pkg/vector_math/lib vm:pkg/vm/lib vm_service:pkg/vm_service/lib vm_snapshot_analysis:pkg/vm_snapshot_analysis/lib -wasm:pkg/wasm/lib watcher:third_party/pkg/watcher/lib webdriver:third_party/pkg/webdriver/lib webkit_inspection_protocol:third_party/pkg/webkit_inspection_protocol/lib diff --git a/pkg/wasm/.gitignore b/pkg/wasm/.gitignore deleted file mode 100644 index 49ce72d76a2..00000000000 --- a/pkg/wasm/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -.dart_tool/ -.packages -pubspec.lock diff --git a/pkg/wasm/AUTHORS b/pkg/wasm/AUTHORS deleted file mode 100644 index 846e4a15687..00000000000 --- a/pkg/wasm/AUTHORS +++ /dev/null @@ -1,6 +0,0 @@ -# Below is a list of people and organizations that have contributed -# to the Dart project. Names should be added to the list like so: -# -# Name/Organization - -Google LLC diff --git a/pkg/wasm/LICENSE b/pkg/wasm/LICENSE deleted file mode 100644 index 467a9828627..00000000000 --- a/pkg/wasm/LICENSE +++ /dev/null @@ -1,27 +0,0 @@ -Copyright 2020, the Dart project authors. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are -met: - - * Redistributions of source code must retain the above copyright - notice, this list of conditions and the following disclaimer. - * Redistributions in binary form must reproduce the above - copyright notice, this list of conditions and the following - disclaimer in the documentation and/or other materials provided - with the distribution. - * Neither the name of Google LLC nor the names of its - contributors may be used to endorse or promote products derived - from this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/pkg/wasm/README.md b/pkg/wasm/README.md deleted file mode 100644 index 4558a3c179f..00000000000 --- a/pkg/wasm/README.md +++ /dev/null @@ -1,48 +0,0 @@ -Provides utilities for loading and running WASM modules - -Built on top of the [Wasmer](https://github.com/wasmerio/wasmer) runtime. - -## Setup - -Run `dart run wasm:setup` to build the Wasmer runtime. - -## Basic Usage - -As a simple example, we'll try to call the following C function from Dart using -`package:wasm`. For a more detailed example that uses WASI, check out the -example directory. - -```c++ -extern "C" int square(int n) { return n * n; } -``` - -We can compile this C++ code to WASM using a recent version of clang: - -```bash -clang --target=wasm32 -nostdlib -Wl,--export-all -Wl,--no-entry -o square.wasm square.cc -``` - -Then we can load and run it like this: - -```dart -import 'dart:io'; -import 'package:wasm/wasm.dart'; - -void main() { - final data = File('square.wasm').readAsBytesSync(); - final mod = WasmModule(data); - print(mod.describe()); - final inst = mod.instantiate().build(); - final square = inst.lookupFunction('square'); - print(square(12)); -} -``` - -This should print: - -``` -export memory: memory -export function: int32 square(int32) - -144 -``` diff --git a/pkg/wasm/analysis_options.yaml b/pkg/wasm/analysis_options.yaml deleted file mode 100644 index d54b9cc9240..00000000000 --- a/pkg/wasm/analysis_options.yaml +++ /dev/null @@ -1,70 +0,0 @@ -include: package:lints/recommended.yaml - -analyzer: - strong-mode: - implicit-casts: false - -linter: - rules: - - avoid_catching_errors - #- avoid_dynamic_calls - - avoid_function_literals_in_foreach_calls - - avoid_private_typedef_functions - - avoid_redundant_argument_values - - avoid_renaming_method_parameters - - avoid_returning_null_for_void - - avoid_returning_this - - avoid_unused_constructor_parameters - - await_only_futures - - cancel_subscriptions - - camel_case_types - - cascade_invocations - - constant_identifier_names - - directives_ordering - - empty_statements - - file_names - - implementation_imports - - iterable_contains_unrelated_type - - join_return_with_assignment - - library_private_types_in_public_api - - lines_longer_than_80_chars - - list_remove_unrelated_type - - missing_whitespace_between_adjacent_strings - - no_runtimeType_toString - - non_constant_identifier_names - - only_throw_errors - - overridden_fields - - package_names - - package_prefixed_library_names - - prefer_asserts_in_initializer_lists - - prefer_const_constructors - - prefer_const_declarations - - prefer_expression_function_bodies - - prefer_function_declarations_over_variables - - prefer_if_null_operators - - prefer_initializing_formals - - prefer_inlined_adds - - prefer_interpolation_to_compose_strings - - prefer_is_not_operator - - prefer_null_aware_operators - - prefer_single_quotes - - prefer_relative_imports - - prefer_typing_uninitialized_variables - - prefer_void_to_null - - provide_deprecation_message - - require_trailing_commas - - sort_pub_dependencies - - test_types_in_equals - - throw_in_finally - - type_annotate_public_apis - - unawaited_futures - - unnecessary_brace_in_string_interps - - unnecessary_lambdas - - unnecessary_null_aware_assignments - - unnecessary_overrides - - unnecessary_parenthesis - - unnecessary_statements - - unnecessary_string_interpolations - - use_is_even_rather_than_modulo - - use_string_buffers - - void_checks diff --git a/pkg/wasm/bin/.gitignore b/pkg/wasm/bin/.gitignore deleted file mode 100644 index 8b44e90c538..00000000000 --- a/pkg/wasm/bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -Cargo.lock -/out diff --git a/pkg/wasm/bin/Cargo.toml b/pkg/wasm/bin/Cargo.toml deleted file mode 100644 index a476549e6ff..00000000000 --- a/pkg/wasm/bin/Cargo.toml +++ /dev/null @@ -1,13 +0,0 @@ -[package] -name = "wasmer" -version = "1.0.0-alpha5" - -[lib] -name = "wasmer" -crate-type = ["staticlib"] -path = "wasmer.rs" - -[dependencies.wasmer-c-api] -version = "1.0.0-alpha5" -default-features = false -features = ["jit", "cranelift", "wasi"] diff --git a/pkg/wasm/bin/finalizers.cc b/pkg/wasm/bin/finalizers.cc deleted file mode 100644 index 192277eda97..00000000000 --- a/pkg/wasm/bin/finalizers.cc +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright (c) 2021, 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. - -#include "include/dart_api.h" -#include "include/dart_api_dl.h" - -#define FINALIZER(type) \ - extern "C" void wasm_##type##_delete(void*); \ - extern "C" void wasm_##type##_finalizer(void*, void* native_object) { \ - wasm_##type##_delete(native_object); \ - } \ - DART_EXPORT void set_finalizer_for_##type(Dart_Handle dart_object, \ - void* native_object) { \ - Dart_NewFinalizableHandle_DL(dart_object, native_object, 0, \ - wasm_##type##_finalizer); \ - } - -FINALIZER(engine); -FINALIZER(store); -FINALIZER(module); -FINALIZER(instance); -FINALIZER(trap); -FINALIZER(memorytype); -FINALIZER(memory); -FINALIZER(func); diff --git a/pkg/wasm/bin/setup.dart b/pkg/wasm/bin/setup.dart deleted file mode 100644 index 4445f375c1b..00000000000 --- a/pkg/wasm/bin/setup.dart +++ /dev/null @@ -1,201 +0,0 @@ -// Copyright (c) 2021, 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. - -// Builds the wasmer runtime library, to by used by package:wasm. Requires -// rustc, cargo, clang, and clang++. If a target triple is not specified, it -// will default to the host target. -// Usage: dart run wasm:setup [target-triple] - -import 'dart:convert'; -import 'dart:io' hide exit; - -import 'package:wasm/src/shared.dart'; - -Future main(List args) async { - if (args.length > 1) { - print('Usage: $invocationString [target-triple]'); - exitCode = 64; // bad usage - return; - } - - final target = args.isNotEmpty ? args[0] : await _getTargetTriple(); - - try { - await _main(target); - } on ProcessException catch (e) { - final invocation = [e.executable, ...e.arguments].join(' '); - print('FAILED with exit code ${e.errorCode} `$invocation`'); - exitCode = 70; // software error - return; - } -} - -Uri _getSdkDir() { - // The common case, and how cli_util.dart computes the Dart SDK directory, - // path.dirname called twice on Platform.resolvedExecutable. - final exe = Uri.file(Platform.resolvedExecutable); - final commonSdkDir = exe.resolve('../../dart-sdk/'); - if (FileSystemEntity.isDirectorySync(commonSdkDir.path)) { - return commonSdkDir; - } - - // This is the less common case where the user is in the checked out Dart - // SDK, and is executing dart via: - // ./out/ReleaseX64/dart ... - final checkedOutSdkDir = exe.resolve('../dart-sdk/'); - if (FileSystemEntity.isDirectorySync(checkedOutSdkDir.path)) { - return checkedOutSdkDir; - } - - final homebrewOutSdkDir = exe.resolve('..'); - final homebrewIncludeDir = homebrewOutSdkDir.resolve('include'); - if (FileSystemEntity.isDirectorySync(homebrewIncludeDir.path)) { - return homebrewOutSdkDir; - } - - // If neither returned above, we return the common case: - return commonSdkDir; -} - -Uri _getOutDir(Uri root) { - final pkgRoot = packageRootUri(root); - if (pkgRoot == null) { - throw ArgumentError('Could not find "$pkgConfigFile" within "$root".'); - } - return pkgRoot.resolve(wasmToolDir); -} - -String _getOutLib(String target) { - final os = RegExp(r'^.*-.*-(.*)').firstMatch(target)?.group(1) ?? ''; - if (os == 'darwin' || os == 'ios') { - return appleLib; - } else if (os == 'windows') { - return 'wasmer.dll'; - } - return linuxLib; -} - -Future _getTargetTriple() async { - final _regexp = RegExp(r'^([^=]+)="(.*)"$'); - final process = await Process.start('rustc', ['--print', 'cfg']); - final sub = process.stderr - .transform(utf8.decoder) - .transform(const LineSplitter()) - .listen((line) => stderr.writeln(line)); - final cfg = {}; - await process.stdout - .transform(utf8.decoder) - .transform(const LineSplitter()) - .forEach((line) { - final match = _regexp.firstMatch(line); - if (match != null) cfg[match.group(1)!] = match.group(2); - }); - await sub.cancel(); - var arch = cfg['target_arch'] ?? 'unknown'; - var vendor = cfg['target_vendor'] ?? 'unknown'; - var os = cfg['target_os'] ?? 'unknown'; - if (os == 'macos') os = 'darwin'; - var env = cfg['target_env']; - return [arch, vendor, os, env] - .where((element) => element != null && element.isNotEmpty) - .join('-'); -} - -Future _run(String exe, List args) async { - print('\n$exe ${args.join(' ')}\n'); - final process = - await Process.start(exe, args, mode: ProcessStartMode.inheritStdio); - final result = await process.exitCode; - if (result != 0) { - throw ProcessException(exe, args, '', result); - } -} - -Future _main(String target) async { - final sdkDir = _getSdkDir(); - final binDir = Platform.script; - final outDir = _getOutDir(Directory.current.uri); - final outLib = outDir.resolve(_getOutLib(target)).path; - - print('Dart SDK directory: ${sdkDir.path}'); - print('Script directory: ${binDir.path}'); - print('Output directory: ${outDir.path}'); - print('Target: $target'); - print('Output library: $outLib'); - - // Build wasmer crate. - await _run('cargo', [ - 'build', - '--target', - target, - '--target-dir', - outDir.path, - '--manifest-path', - binDir.resolve('Cargo.toml').path, - '--release' - ]); - - const dartApiDlImplPath = 'include/internal/dart_api_dl_impl.h'; - - final dartApiDlImplFile = File.fromUri(sdkDir.resolve(dartApiDlImplPath)); - // Hack around a bug with dart_api_dl_impl.h include path in dart_api_dl.c. - if (!dartApiDlImplFile.existsSync()) { - Directory(outDir.resolve('include/internal/').path) - .createSync(recursive: true); - await dartApiDlImplFile.copy(outDir.resolve(dartApiDlImplPath).path); - } - - // Build dart_api_dl.o. - await _run('clang', [ - '-DDART_SHARED_LIB', - '-DNDEBUG', - '-fno-exceptions', - '-fPIC', - '-O3', - '-target', - target, - '-I', - sdkDir.resolve('include/').path, - '-I', - outDir.resolve('include/').path, - '-c', - sdkDir.resolve('include/dart_api_dl.c').path, - '-o', - outDir.resolve('dart_api_dl.o').path - ]); - - // Build finalizers.o. - await _run('clang++', [ - '-DDART_SHARED_LIB', - '-DNDEBUG', - '-fno-exceptions', - '-fno-rtti', - '-fPIC', - '-O3', - '-std=c++11', - '-target', - target, - '-I', - sdkDir.path, - '-I', - outDir.resolve('include/').path, - '-c', - binDir.resolve('finalizers.cc').path, - '-o', - outDir.resolve('finalizers.o').path - ]); - - // Link wasmer, dart_api_dl, and finalizers to create the output library. - await _run('clang++', [ - '-shared', - '-fPIC', - '-target', - target, - outDir.resolve('dart_api_dl.o').path, - outDir.resolve('finalizers.o').path, - outDir.resolve('$target/release/libwasmer.a').path, - '-o', - outLib - ]); -} diff --git a/pkg/wasm/bin/wasmer.rs b/pkg/wasm/bin/wasmer.rs deleted file mode 100644 index c4bd6e5fcca..00000000000 --- a/pkg/wasm/bin/wasmer.rs +++ /dev/null @@ -1 +0,0 @@ -pub extern crate wasmer_c_api; diff --git a/pkg/wasm/example/README.md b/pkg/wasm/example/README.md deleted file mode 100644 index e6ff491c98b..00000000000 --- a/pkg/wasm/example/README.md +++ /dev/null @@ -1,49 +0,0 @@ -# Example usage of `package:wasm` - -This example demonstrates how to use package:wasm to run a wasm build of the -[Brotli compression library](https://github.com/google/brotli). - -### Running the example - -`dart brotli.dart lipsum.txt` - -This will compress lipsum.txt, report the compression ratio, then decompress it -and verify that the result matches the input. - -### Generating wasm code - -`libbrotli.wasm` was built by cloning the -[Brotli repo](https://github.com/google/brotli), and compiling it using -[wasienv](https://github.com/wasienv/wasienv). - -There are several ways of building wasm code. The most important difference -between the tool sets is how the wasm code they generate interacts with the OS. -For very simple code this difference doesn't matter. But if the library does any -sort of OS interaction, such as file IO, or even using malloc, it will need to -use either Emscripten or WASI for that interaction. package:wasm only supports -WASI at the moment. - -To target WASI, one option is to use -[wasi-libc](https://github.com/WebAssembly/wasi-libc) and a recent version of -clang. Set the target to `--target=wasm32-unknown-wasi` and the `--sysroot` to -wasi-libc. - -Another option is to build using [wasienv](https://github.com/wasienv/wasienv), -which is a set of tools that are essentially just an ergonomic wrapper around -the clang + wasi-libc approach. This is how libbrotli.wasm was built: - -1. Install [wasienv](https://github.com/wasienv/wasienv) and clone the - [Brotli repo](https://github.com/google/brotli). -2. Compile every .c file in brotli/c/common/, dec/, and enc/, using wasicc: - `wasicc -c foo.c -o out/foo.o -I c/include` -3. Link all the .o files together using wasild: - `wasild --no-entry --export=bar out/foo.o $wasienv_sysroot/lib/wasm32-wasi/libc.a` - The `--no-entry` flag tells the linker to ignore the fact that there's no - `main()` function, which is important for libraries. `--export=bar` will - export the `bar()` function from the library, so that it can be found by - package:wasm. For libbrotli.wasm, every function in c/include/brotli/encode.h - and decode.h was exported. Brotli used functions from libc, so the wasm - version of libc that comes with wasienv was also linked in. If there are - still undefined symbols after linking in the wasi libraries, the - `--allow-undefined` flag tells the linker to treat undefined symbols as - function imports. These functions can then be supplied from Dart code. diff --git a/pkg/wasm/example/brotli.dart b/pkg/wasm/example/brotli.dart deleted file mode 100644 index 2eb808a524f..00000000000 --- a/pkg/wasm/example/brotli.dart +++ /dev/null @@ -1,111 +0,0 @@ -// Copyright (c) 2021, 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. - -// Example of using package:wasm to run a wasm build of the Brotli compression -// library. Usage: -// dart brotli.dart input.txt - -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:wasm/wasm.dart'; - -// Brotli compression parameters. -const _kDefaultQuality = 11; -const _kDefaultWindow = 22; -const _kDefaultMode = 0; - -void main(List args) { - if (args.length != 1) { - print('Requires one argument: a path to the input file.'); - exitCode = 64; // bad usage - return; - } - - final inputFilePath = args.single; - - print('Loading "$inputFilePath"...'); - var inputDataFile = File(inputFilePath); - if (!inputDataFile.existsSync()) { - print('Input file "$inputFilePath" does not exist.'); - exitCode = 66; // no input file - return; - } - - var inputData = inputDataFile.readAsBytesSync(); - print('Input size: ${inputData.length} bytes'); - - print('\nLoading wasm module'); - var brotliPath = Platform.script.resolve('libbrotli.wasm'); - var moduleData = File(brotliPath.path).readAsBytesSync(); - var module = WasmModule(moduleData); - print(module.describe()); - - var builder = module.builder()..enableWasi(); - var instance = builder.build(); - var memory = instance.memory; - var compress = instance.lookupFunction('BrotliEncoderCompress'); - var decompress = instance.lookupFunction('BrotliDecoderDecompress'); - - // Grow the module's memory to get unused space to put our data. - // [initial memory][input data][output data][size][decoded data][size] - var inputPtr = memory.lengthInBytes; - memory.grow((3 * inputData.length / WasmMemory.kPageSizeInBytes).ceil()); - var memoryView = memory.view; - var outputPtr = inputPtr + inputData.length; - var outSizePtr = outputPtr + inputData.length; - var decodedPtr = outSizePtr + 4; - var decSizePtr = decodedPtr + inputData.length; - - memoryView.setRange(inputPtr, inputPtr + inputData.length, inputData); - - var sizeBytes = ByteData(4)..setUint32(0, inputData.length, Endian.host); - memoryView.setRange( - outSizePtr, - outSizePtr + 4, - sizeBytes.buffer.asUint8List(), - ); - - print('\nCompressing...'); - var status = compress( - _kDefaultQuality, - _kDefaultWindow, - _kDefaultMode, - inputData.length, - inputPtr, - outSizePtr, - outputPtr, - ); - print('Compression status: $status'); - - var compressedSize = - ByteData.sublistView(memoryView, outSizePtr, outSizePtr + 4) - .getUint32(0, Endian.host); - print('Compressed size: $compressedSize bytes'); - var spaceSaving = 100 * (1 - compressedSize / inputData.length); - print('Space saving: ${spaceSaving.toStringAsFixed(2)}%'); - - var decSizeBytes = ByteData(4)..setUint32(0, inputData.length, Endian.host); - memoryView.setRange( - decSizePtr, - decSizePtr + 4, - decSizeBytes.buffer.asUint8List(), - ); - - print('\nDecompressing...'); - status = decompress(compressedSize, outputPtr, decSizePtr, decodedPtr); - print('Decompression status: $status'); - - var decompressedSize = - ByteData.sublistView(memoryView, decSizePtr, decSizePtr + 4) - .getUint32(0, Endian.host); - print('Decompressed size: $decompressedSize bytes'); - - print('\nVerifying decompression...'); - assert(inputData.length == decompressedSize); - for (var i = 0; i < inputData.length; ++i) { - assert(inputData[i] == memoryView[decodedPtr + i]); - } - print('Decompression succeeded :)'); -} diff --git a/pkg/wasm/example/libbrotli.wasm b/pkg/wasm/example/libbrotli.wasm deleted file mode 100755 index 0f8ddb34359..00000000000 Binary files a/pkg/wasm/example/libbrotli.wasm and /dev/null differ diff --git a/pkg/wasm/example/lipsum.txt b/pkg/wasm/example/lipsum.txt deleted file mode 100644 index f3b5ee0fbf0..00000000000 --- a/pkg/wasm/example/lipsum.txt +++ /dev/null @@ -1,10 +0,0 @@ -Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam feugiat efficitur est a sodales. Ut et odio at nunc fermentum ullamcorper nec in libero. Mauris et interdum elit. Integer in diam nec felis venenatis consectetur. Curabitur elementum egestas augue ac sollicitudin. Cras vulputate efficitur nulla. In blandit sapien ultrices maximus posuere. - -Suspendisse in mi at arcu scelerisque tincidunt. Nullam venenatis ex nunc, ut efficitur nulla vehicula eget. Fusce sagittis facilisis ligula nec semper. Nullam et semper ligula. Curabitur sollicitudin ultrices elit et sodales. In iaculis erat scelerisque ipsum scelerisque rutrum. Quisque sollicitudin dolor eu venenatis venenatis. Donec non varius lacus. Phasellus fermentum id mauris nec consequat. Curabitur ultrices, mauris ut scelerisque aliquam, neque augue elementum mi, at accumsan est massa vitae metus. Donec sit amet accumsan dolor, sed vehicula augue. Nunc augue ligula, faucibus tincidunt lorem sed, efficitur ullamcorper erat. Curabitur pellentesque auctor nisi. Cras placerat, massa quis scelerisque commodo, augue leo aliquam elit, sed tempor turpis neque sed nulla. Proin vulputate malesuada augue, quis finibus felis rutrum nec. - -Phasellus molestie, tellus eget hendrerit accumsan, diam nunc scelerisque nisi, quis aliquet augue lacus non diam. Nulla facilisi. Nulla est urna, egestas vel luctus nec, sagittis in risus. Mauris aliquam viverra massa vitae efficitur. Integer fringilla sollicitudin ex, et maximus sem gravida ultrices. Vestibulum nec sodales nulla. Cras dapibus maximus venenatis. Vivamus condimentum porttitor mollis. Aliquam congue eleifend condimentum. Donec sagittis bibendum gravida. Nulla condimentum viverra sapien, quis congue libero aliquet nec. Fusce et interdum nisi. Suspendisse at commodo eros. Mauris malesuada nisi in tortor accumsan iaculis. Nam hendrerit interdum magna, eu aliquam est varius eu. Nullam auctor ornare erat, sit amet maximus orci fringilla eu. - -Vivamus ullamcorper enim eget tellus lobortis mattis. Vivamus nec tincidunt ipsum. Quisque pharetra non neque non sagittis. Morbi ultrices massa nulla, ac eleifend nulla bibendum mollis. Donec in sodales massa, id luctus dolor. Pellentesque vel auctor tortor, eu imperdiet felis. Pellentesque eleifend eros ipsum, sagittis feugiat enim placerat at. Interdum et malesuada fames ac ante ipsum primis in faucibus. Aliquam lacinia pharetra est. - -Duis elit arcu, faucibus ac libero ut, auctor volutpat elit. Duis blandit quis felis at ultricies. Duis ac eros id velit pretium sagittis. Praesent eget orci porttitor, posuere purus ac, interdum eros. Nam augue velit, euismod nec lobortis vitae, rutrum ut libero. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean porttitor sem ante. Integer elit purus, sollicitudin sit amet est in, accumsan suscipit libero. Donec finibus metus scelerisque, mattis dui quis, suscipit turpis. Aliquam rhoncus leo ipsum, vel hendrerit ante porta sit amet. Donec dui nisi, bibendum non rutrum vel, ornare nec diam. Proin tristique ipsum eu pulvinar finibus. Duis pellentesque massa a condimentum elementum. Maecenas efficitur ac est in eleifend. Mauris sit amet lacus blandit, pulvinar ex in, commodo eros. - diff --git a/pkg/wasm/lib/src/module.dart b/pkg/wasm/lib/src/module.dart deleted file mode 100644 index 29be5546cbe..00000000000 --- a/pkg/wasm/lib/src/module.dart +++ /dev/null @@ -1,449 +0,0 @@ -// Copyright (c) 2020, 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. - -import 'dart:ffi'; -import 'dart:typed_data'; - -import 'package:ffi/ffi.dart'; - -import 'runtime.dart'; -import 'wasm_error.dart'; -import 'wasmer_api.dart'; - -/// A compiled module that can be instantiated. -class WasmModule { - late final Pointer _store; - late final Pointer _module; - - /// Compile a module. - WasmModule(Uint8List data) { - var runtime = WasmRuntime(); - _store = runtime.newStore(this); - _module = runtime.compile(this, _store, data); - } - - /// Returns a [WasmInstanceBuilder] that is used to add all the imports that - /// the module needs before instantiating it. - WasmInstanceBuilder builder() => WasmInstanceBuilder._(this); - - /// Create a new memory with the given number of initial pages, and optional - /// maximum number of pages. - WasmMemory createMemory(int pages, [int? maxPages]) => - WasmMemory._create(_store, pages, maxPages); - - /// Returns a description of all of the module's imports and exports, for - /// debugging. - String describe() { - var description = StringBuffer(); - var runtime = WasmRuntime(); - var imports = runtime.importDescriptors(_module); - for (var imp in imports) { - description.write('import $imp\n'); - } - var exports = runtime.exportDescriptors(_module); - for (var exp in exports) { - description.write('export $exp\n'); - } - return description.toString(); - } -} - -Pointer _wasmFnImportTrampoline( - Pointer<_WasmFnImport> imp, - Pointer args, - Pointer results, -) { - try { - _WasmFnImport._call(imp, args, results); - } catch (exception) { - return WasmRuntime().newTrap(imp.ref.store, exception); - } - return nullptr; -} - -void _wasmFnImportFinalizer(Pointer<_WasmFnImport> imp) { - _wasmFnImportToFn.remove(imp.address); - calloc.free(imp); -} - -final _wasmFnImportTrampolineNative = Pointer.fromFunction< - Pointer Function( - Pointer<_WasmFnImport>, - Pointer, - Pointer, -)>(_wasmFnImportTrampoline); -final _wasmFnImportToFn = {}; -final _wasmFnImportFinalizerNative = - Pointer.fromFunction)>( - _wasmFnImportFinalizer, -); - -class _WasmFnImport extends Struct { - @Int32() - external int returnType; - - external Pointer store; - - static void _call( - Pointer<_WasmFnImport> imp, - Pointer rawArgs, - Pointer rawResult, - ) { - var fn = _wasmFnImportToFn[imp.address] as Function; - var args = []; - for (var i = 0; i < rawArgs.ref.length; ++i) { - args.add(rawArgs.ref.data[i].toDynamic); - } - assert( - rawResult.ref.length == 1 || imp.ref.returnType == wasmerValKindVoid, - ); - var result = Function.apply(fn, args); - if (imp.ref.returnType != wasmerValKindVoid) { - rawResult.ref.data[0].kind = imp.ref.returnType; - switch (imp.ref.returnType) { - case wasmerValKindI32: - rawResult.ref.data[0].i32 = result as int; - break; - case wasmerValKindI64: - rawResult.ref.data[0].i64 = result as int; - break; - case wasmerValKindF32: - rawResult.ref.data[0].f32 = result as int; - break; - case wasmerValKindF64: - rawResult.ref.data[0].f64 = result as int; - break; - } - } - } -} - -/// Used to collect all of the imports that a [WasmModule] requires before it is -/// built. -class WasmInstanceBuilder { - final _importOwner = _WasmImportOwner(); - final _importIndex = {}; - final _imports = calloc(); - final WasmModule _module; - late final List _importDescs; - Pointer _wasiEnv = nullptr; - - WasmInstanceBuilder._(this._module) - : _importDescs = WasmRuntime().importDescriptors(_module._module) { - _imports.ref.length = _importDescs.length; - _imports.ref.data = calloc>(_importDescs.length); - for (var i = 0; i < _importDescs.length; ++i) { - var imp = _importDescs[i]; - _importIndex['${imp.moduleName}::${imp.name}'] = i; - _imports.ref.data[i] = nullptr; - } - } - - int _getIndex(String moduleName, String name) { - var index = _importIndex['$moduleName::$name']; - if (index == null) { - throw WasmError('Import not found: $moduleName::$name'); - } else if (_imports.ref.data[index] != nullptr) { - throw WasmError('Import already filled: $moduleName::$name'); - } else { - return index; - } - } - - /// Add a WasmMemory to the imports. - void addMemory( - String moduleName, - String name, - WasmMemory memory, - ) { - var index = _getIndex(moduleName, name); - var imp = _importDescs[index]; - if (imp.kind != wasmerExternKindMemory) { - throw WasmError('Import is not a memory: $imp'); - } - _imports.ref.data[index] = WasmRuntime().memoryToExtern(memory._mem); - } - - /// Add a function to the imports. - void addFunction(String moduleName, String name, Function fn) { - var index = _getIndex(moduleName, name); - var imp = _importDescs[index]; - - if (imp.kind != wasmerExternKindFunction) { - throw WasmError('Import is not a function: $imp'); - } - - var runtime = WasmRuntime(); - var returnType = runtime.getReturnType(imp.funcType); - var wasmFnImport = calloc<_WasmFnImport>(); - wasmFnImport.ref.returnType = returnType; - wasmFnImport.ref.store = _module._store; - _wasmFnImportToFn[wasmFnImport.address] = fn; - var fnImp = runtime.newFunc( - _importOwner, - _module._store, - imp.funcType, - _wasmFnImportTrampolineNative, - wasmFnImport, - _wasmFnImportFinalizerNative, - ); - _imports.ref.data[index] = runtime.functionToExtern(fnImp); - } - - /// Enable WASI and add the default WASI imports. - void enableWasi({ - bool captureStdout = false, - bool captureStderr = false, - }) { - if (_wasiEnv != nullptr) { - throw WasmError('WASI is already enabled.'); - } - var runtime = WasmRuntime(); - var config = runtime.newWasiConfig(); - if (captureStdout) runtime.captureWasiStdout(config); - if (captureStderr) runtime.captureWasiStderr(config); - _wasiEnv = runtime.newWasiEnv(config); - runtime.getWasiImports(_module._store, _module._module, _wasiEnv, _imports); - } - - /// Build the module instance. - WasmInstance build() { - for (var i = 0; i < _importDescs.length; ++i) { - if (_imports.ref.data[i] == nullptr) { - throw WasmError('Missing import: ${_importDescs[i]}'); - } - } - return WasmInstance._(_module, _importOwner, _imports, _wasiEnv); - } -} - -// TODO: should not be required once the min supported Dart SDK includes -// github.com/dart-lang/sdk/commit/8fd81f72281d9d3aa5ef3890c947cc7305c56a50 -class _WasmImportOwner {} - -/// An instantiated [WasmModule]. -/// -/// Created by calling [WasmInstanceBuilder.build]. -class WasmInstance { - final _WasmImportOwner _importOwner; - final _functions = {}; - final WasmModule _module; - final Pointer _wasiEnv; - - late final Pointer _instance; - - Pointer? _exportedMemory; - Stream>? _stdout; - Stream>? _stderr; - - WasmInstance._( - this._module, - this._importOwner, - Pointer imports, - this._wasiEnv, - ) { - var runtime = WasmRuntime(); - _instance = runtime.instantiate( - _importOwner, - _module._store, - _module._module, - imports, - ); - var exports = runtime.exports(_instance); - var exportDescs = runtime.exportDescriptors(_module._module); - assert(exports.ref.length == exportDescs.length); - for (var i = 0; i < exports.ref.length; ++i) { - var e = exports.ref.data[i]; - var kind = runtime.externKind(exports.ref.data[i]); - var name = exportDescs[i].name; - if (kind == wasmerExternKindFunction) { - var f = runtime.externToFunction(e); - var ft = exportDescs[i].funcType; - _functions[name] = WasmFunction._( - name, - f, - runtime.getArgTypes(ft), - runtime.getReturnType(ft), - ); - } else if (kind == wasmerExternKindMemory) { - // WASM currently allows only one memory per module. - var mem = runtime.externToMemory(e); - _exportedMemory = mem; - if (_wasiEnv != nullptr) { - runtime.wasiEnvSetMemory(_wasiEnv, mem); - } - } - } - } - - /// Searches the instantiated module for the given function. - /// - /// Returns a [WasmFunction], but the return type is [dynamic] to allow - /// easy invocation as a [Function]. - /// - /// Returns `null` if no function exists with name [name]. - dynamic lookupFunction(String name) => _functions[name]; - - /// Returns the memory exported from this instance. - WasmMemory get memory { - if (_exportedMemory == null) { - throw WasmError('Wasm module did not export its memory.'); - } - return WasmMemory._fromExport(_exportedMemory as Pointer); - } - - /// Returns a stream that reads from `stdout`. - /// - /// To use this, you must enable WASI when instantiating the module, and set - /// `captureStdout` to `true`. - Stream> get stdout { - if (_wasiEnv == nullptr) { - throw WasmError("Can't capture stdout without WASI enabled."); - } - return _stdout ??= WasmRuntime().getWasiStdoutStream(_wasiEnv); - } - - /// Returns a stream that reads from `stderr`. - /// - /// To use this, you must enable WASI when instantiating the module, and set - /// `captureStderr` to `true`. - Stream> get stderr { - if (_wasiEnv == nullptr) { - throw WasmError("Can't capture stderr without WASI enabled."); - } - return _stderr ??= WasmRuntime().getWasiStderrStream(_wasiEnv); - } -} - -/// Memory of a [WasmInstance]. -/// -/// Access via [WasmInstance.memory] or create via [WasmModule.createMemory]. -class WasmMemory { - late final Pointer _mem; - late Uint8List _view; - - WasmMemory._fromExport(this._mem) { - _view = WasmRuntime().memoryView(_mem); - } - - /// Create a new memory with the given number of initial pages, and optional - /// maximum number of pages. - WasmMemory._create(Pointer store, int pages, int? maxPages) { - _mem = WasmRuntime().newMemory(this, store, pages, maxPages); - _view = WasmRuntime().memoryView(_mem); - } - - /// The WASM spec defines the page size as 64KiB. - static const int kPageSizeInBytes = 64 * 1024; - - /// The length of the memory in pages. - int get lengthInPages => WasmRuntime().memoryLength(_mem); - - /// The length of the memory in bytes. - int get lengthInBytes => _view.lengthInBytes; - - /// The byte at the given [index]. - int operator [](int index) => _view[index]; - - /// Sets the byte at the given index to value. - void operator []=(int index, int value) { - _view[index] = value; - } - - /// A view into the memory. - Uint8List get view => _view; - - /// Grow the memory by [deltaPages] and invalidates any existing views into - /// the memory. - void grow(int deltaPages) { - var runtime = WasmRuntime()..growMemory(_mem, deltaPages); - _view = runtime.memoryView(_mem); - } -} - -/// A callable function from a [WasmInstance]. -/// -/// Access by calling [WasmInstance.lookupFunction]. -class WasmFunction { - final String _name; - final Pointer _func; - final List _argTypes; - final int _returnType; - final Pointer _args = calloc(); - final Pointer _results = calloc(); - - WasmFunction._(this._name, this._func, this._argTypes, this._returnType) { - _args.ref.length = _argTypes.length; - _args.ref.data = - _argTypes.isEmpty ? nullptr : calloc(_argTypes.length); - _results.ref.length = _returnType == wasmerValKindVoid ? 0 : 1; - _results.ref.data = - _returnType == wasmerValKindVoid ? nullptr : calloc(); - for (var i = 0; i < _argTypes.length; ++i) { - _args.ref.data[i].kind = _argTypes[i]; - } - } - - @override - String toString() => - WasmRuntime.getSignatureString(_name, _argTypes, _returnType); - - bool _fillArg(dynamic arg, int i) { - switch (_argTypes[i]) { - case wasmerValKindI32: - if (arg is! int) return false; - _args.ref.data[i].i32 = arg; - return true; - case wasmerValKindI64: - if (arg is! int) return false; - _args.ref.data[i].i64 = arg; - return true; - case wasmerValKindF32: - if (arg is! num) return false; - _args.ref.data[i].f32 = arg; - return true; - case wasmerValKindF64: - if (arg is! num) return false; - _args.ref.data[i].f64 = arg; - return true; - } - return false; - } - - dynamic apply(List args) { - if (args.length != _argTypes.length) { - throw ArgumentError('Wrong number arguments for WASM function: $this'); - } - for (var i = 0; i < args.length; ++i) { - if (!_fillArg(args[i], i)) { - throw ArgumentError('Bad argument type for WASM function: $this'); - } - } - WasmRuntime().call(_func, _args, _results, toString()); - - if (_returnType == wasmerValKindVoid) { - return null; - } - var result = _results.ref.data[0]; - assert(_returnType == result.kind); - switch (_returnType) { - case wasmerValKindI32: - return result.i32; - case wasmerValKindI64: - return result.i64; - case wasmerValKindF32: - return result.f32; - case wasmerValKindF64: - return result.f64; - } - } - - @override - dynamic noSuchMethod(Invocation invocation) { - if (invocation.memberName == #call) { - return apply(invocation.positionalArguments); - } - return super.noSuchMethod(invocation); - } -} diff --git a/pkg/wasm/lib/src/runtime.dart b/pkg/wasm/lib/src/runtime.dart deleted file mode 100644 index e6e6d6a5584..00000000000 --- a/pkg/wasm/lib/src/runtime.dart +++ /dev/null @@ -1,122 +0,0 @@ -// Copyright (c) 2020, 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. - -import 'dart:async'; -import 'dart:convert'; -import 'dart:ffi'; -import 'dart:io'; -import 'dart:typed_data'; - -import 'package:ffi/ffi.dart'; - -import 'shared.dart'; -import 'wasm_error.dart'; -import 'wasmer_api.dart'; - -part 'runtime.g.dart'; - -class WasmImportDescriptor { - final int kind; - final String moduleName; - final String name; - final Pointer funcType; - - WasmImportDescriptor._(this.kind, this.moduleName, this.name, this.funcType); - - @override - String toString() { - var kindName = wasmerExternKindName(kind); - if (kind == wasmerExternKindFunction) { - var runtime = WasmRuntime(); - var sig = WasmRuntime.getSignatureString( - '$moduleName::$name', - runtime.getArgTypes(funcType), - runtime.getReturnType(funcType), - ); - return '$kindName: $sig'; - } else { - return '$kindName: $moduleName::$name'; - } - } -} - -class WasmExportDescriptor { - final int kind; - final String name; - final Pointer funcType; - - WasmExportDescriptor._(this.kind, this.name, this.funcType); - - @override - String toString() { - var kindName = wasmerExternKindName(kind); - if (kind == wasmerExternKindFunction) { - var runtime = WasmRuntime(); - var sig = WasmRuntime.getSignatureString( - name, - runtime.getArgTypes(funcType), - runtime.getReturnType(funcType), - ); - return '$kindName: $sig'; - } else { - return '$kindName: $name'; - } - } -} - -class _WasmTrapsEntry { - final Object exception; - - _WasmTrapsEntry(this.exception); -} - -class _WasiStreamIterator implements Iterator> { - static const int _bufferLength = 1024; - final Pointer _buf = calloc(_bufferLength); - final Pointer _env; - final Function _reader; - int _length = 0; - - _WasiStreamIterator(this._env, this._reader); - - @override - bool moveNext() { - _length = _reader(_env, _buf, _bufferLength) as int; - return true; - } - - @override - List get current => _buf.asTypedList(_length); -} - -class _WasiStreamIterable extends Iterable> { - final Pointer _env; - final Function _reader; - - _WasiStreamIterable(this._env, this._reader); - - @override - Iterator> get iterator => _WasiStreamIterator(_env, _reader); -} - -String _getLibName() { - if (Platform.isMacOS) return appleLib; - if (Platform.isLinux) return linuxLib; - // TODO(dartbug.com/37882): Support more platforms. - throw WasmError('Wasm not currently supported on this platform'); -} - -String? _getLibPathFrom(Uri root) { - final pkgRoot = packageRootUri(root); - - return pkgRoot?.resolve('$wasmToolDir${_getLibName()}').path; -} - -String _getLibPath() { - var path = _getLibPathFrom(Platform.script.resolve('./')); - if (path != null) return path; - path = _getLibPathFrom(Directory.current.uri); - if (path != null) return path; - throw WasmError('Wasm library not found. Did you `$invocationString`?'); -} diff --git a/pkg/wasm/lib/src/runtime.g.dart b/pkg/wasm/lib/src/runtime.g.dart deleted file mode 100644 index 4a54117cb1e..00000000000 --- a/pkg/wasm/lib/src/runtime.g.dart +++ /dev/null @@ -1,768 +0,0 @@ -// Copyright (c) 2020, 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. - -// This file has been automatically generated. Please do not edit it manually. -// To regenerate the file, use the following command -// "generate_ffi_boilerplate.py". - -// ignore_for_file: cascade_invocations -// ignore_for_file: non_constant_identifier_names -// ignore_for_file: unused_field - -part of 'runtime.dart'; - -class WasmRuntime { - static final WasmRuntime _inst = WasmRuntime._init(); - final DynamicLibrary _lib; - final _traps = {}; - late final Pointer _engine; - - late final WasmerDartInitializeApiDLFn _Dart_InitializeApiDL; - late final WasmerSetFinalizerForEngineFn _set_finalizer_for_engine; - late final WasmerSetFinalizerForFuncFn _set_finalizer_for_func; - late final WasmerSetFinalizerForInstanceFn _set_finalizer_for_instance; - late final WasmerSetFinalizerForMemoryFn _set_finalizer_for_memory; - late final WasmerSetFinalizerForMemorytypeFn _set_finalizer_for_memorytype; - late final WasmerSetFinalizerForModuleFn _set_finalizer_for_module; - late final WasmerSetFinalizerForStoreFn _set_finalizer_for_store; - late final WasmerSetFinalizerForTrapFn _set_finalizer_for_trap; - late final WasmerWasiConfigInheritStderrFn _wasi_config_inherit_stderr; - late final WasmerWasiConfigInheritStdoutFn _wasi_config_inherit_stdout; - late final WasmerWasiConfigNewFn _wasi_config_new; - late final WasmerWasiEnvDeleteFn _wasi_env_delete; - late final WasmerWasiEnvNewFn _wasi_env_new; - late final WasmerWasiEnvReadStderrFn _wasi_env_read_stderr; - late final WasmerWasiEnvReadStdoutFn _wasi_env_read_stdout; - late final WasmerWasiEnvSetMemoryFn _wasi_env_set_memory; - late final WasmerWasiGetImportsFn _wasi_get_imports; - late final WasmerByteVecDeleteFn _byte_vec_delete; - late final WasmerByteVecNewFn _byte_vec_new; - late final WasmerByteVecNewEmptyFn _byte_vec_new_empty; - late final WasmerByteVecNewUninitializedFn _byte_vec_new_uninitialized; - late final WasmerEngineDeleteFn _engine_delete; - late final WasmerEngineNewFn _engine_new; - late final WasmerExporttypeNameFn _exporttype_name; - late final WasmerExporttypeTypeFn _exporttype_type; - late final WasmerExporttypeVecDeleteFn _exporttype_vec_delete; - late final WasmerExporttypeVecNewFn _exporttype_vec_new; - late final WasmerExporttypeVecNewEmptyFn _exporttype_vec_new_empty; - late final WasmerExporttypeVecNewUninitializedFn - _exporttype_vec_new_uninitialized; - late final WasmerExternAsFuncFn _extern_as_func; - late final WasmerExternAsMemoryFn _extern_as_memory; - late final WasmerExternDeleteFn _extern_delete; - late final WasmerExternKindFn _extern_kind; - late final WasmerExternVecDeleteFn _extern_vec_delete; - late final WasmerExternVecNewFn _extern_vec_new; - late final WasmerExternVecNewEmptyFn _extern_vec_new_empty; - late final WasmerExternVecNewUninitializedFn _extern_vec_new_uninitialized; - late final WasmerExterntypeAsFunctypeFn _externtype_as_functype; - late final WasmerExterntypeDeleteFn _externtype_delete; - late final WasmerExterntypeKindFn _externtype_kind; - late final WasmerFuncAsExternFn _func_as_extern; - late final WasmerFuncCallFn _func_call; - late final WasmerFuncDeleteFn _func_delete; - late final WasmerFuncNewWithEnvFn _func_new_with_env; - late final WasmerFunctypeDeleteFn _functype_delete; - late final WasmerFunctypeParamsFn _functype_params; - late final WasmerFunctypeResultsFn _functype_results; - late final WasmerImporttypeModuleFn _importtype_module; - late final WasmerImporttypeNameFn _importtype_name; - late final WasmerImporttypeTypeFn _importtype_type; - late final WasmerImporttypeVecDeleteFn _importtype_vec_delete; - late final WasmerImporttypeVecNewFn _importtype_vec_new; - late final WasmerImporttypeVecNewEmptyFn _importtype_vec_new_empty; - late final WasmerImporttypeVecNewUninitializedFn - _importtype_vec_new_uninitialized; - late final WasmerInstanceDeleteFn _instance_delete; - late final WasmerInstanceExportsFn _instance_exports; - late final WasmerInstanceNewFn _instance_new; - late final WasmerMemoryAsExternFn _memory_as_extern; - late final WasmerMemoryDataFn _memory_data; - late final WasmerMemoryDataSizeFn _memory_data_size; - late final WasmerMemoryDeleteFn _memory_delete; - late final WasmerMemoryGrowFn _memory_grow; - late final WasmerMemoryNewFn _memory_new; - late final WasmerMemorySizeFn _memory_size; - late final WasmerMemorytypeDeleteFn _memorytype_delete; - late final WasmerMemorytypeNewFn _memorytype_new; - late final WasmerModuleDeleteFn _module_delete; - late final WasmerModuleExportsFn _module_exports; - late final WasmerModuleImportsFn _module_imports; - late final WasmerModuleNewFn _module_new; - late final WasmerStoreDeleteFn _store_delete; - late final WasmerStoreNewFn _store_new; - late final WasmerTrapDeleteFn _trap_delete; - late final WasmerTrapMessageFn _trap_message; - late final WasmerTrapNewFn _trap_new; - late final WasmerValtypeDeleteFn _valtype_delete; - late final WasmerValtypeKindFn _valtype_kind; - late final WasmerValtypeVecDeleteFn _valtype_vec_delete; - late final WasmerValtypeVecNewFn _valtype_vec_new; - late final WasmerValtypeVecNewEmptyFn _valtype_vec_new_empty; - late final WasmerValtypeVecNewUninitializedFn _valtype_vec_new_uninitialized; - late final WasmerWasmerLastErrorLengthFn _wasmer_last_error_length; - late final WasmerWasmerLastErrorMessageFn _wasmer_last_error_message; - - factory WasmRuntime() => _inst; - - WasmRuntime._init() : _lib = DynamicLibrary.open(_getLibPath()) { - _Dart_InitializeApiDL = _lib.lookupFunction< - NativeWasmerDartInitializeApiDLFn, WasmerDartInitializeApiDLFn>( - 'Dart_InitializeApiDL', - ); - _set_finalizer_for_engine = _lib.lookupFunction< - NativeWasmerSetFinalizerForEngineFn, WasmerSetFinalizerForEngineFn>( - 'set_finalizer_for_engine', - ); - _set_finalizer_for_func = _lib.lookupFunction< - NativeWasmerSetFinalizerForFuncFn, WasmerSetFinalizerForFuncFn>( - 'set_finalizer_for_func', - ); - _set_finalizer_for_instance = _lib.lookupFunction< - NativeWasmerSetFinalizerForInstanceFn, WasmerSetFinalizerForInstanceFn>( - 'set_finalizer_for_instance', - ); - _set_finalizer_for_memory = _lib.lookupFunction< - NativeWasmerSetFinalizerForMemoryFn, WasmerSetFinalizerForMemoryFn>( - 'set_finalizer_for_memory', - ); - _set_finalizer_for_memorytype = _lib.lookupFunction< - NativeWasmerSetFinalizerForMemorytypeFn, - WasmerSetFinalizerForMemorytypeFn>( - 'set_finalizer_for_memorytype', - ); - _set_finalizer_for_module = _lib.lookupFunction< - NativeWasmerSetFinalizerForModuleFn, WasmerSetFinalizerForModuleFn>( - 'set_finalizer_for_module', - ); - _set_finalizer_for_store = _lib.lookupFunction< - NativeWasmerSetFinalizerForStoreFn, WasmerSetFinalizerForStoreFn>( - 'set_finalizer_for_store', - ); - _set_finalizer_for_trap = _lib.lookupFunction< - NativeWasmerSetFinalizerForTrapFn, WasmerSetFinalizerForTrapFn>( - 'set_finalizer_for_trap', - ); - _wasi_config_inherit_stderr = _lib.lookupFunction< - NativeWasmerWasiConfigInheritStderrFn, WasmerWasiConfigInheritStderrFn>( - 'wasi_config_inherit_stderr', - ); - _wasi_config_inherit_stdout = _lib.lookupFunction< - NativeWasmerWasiConfigInheritStdoutFn, WasmerWasiConfigInheritStdoutFn>( - 'wasi_config_inherit_stdout', - ); - _wasi_config_new = - _lib.lookupFunction( - 'wasi_config_new', - ); - _wasi_env_delete = - _lib.lookupFunction( - 'wasi_env_delete', - ); - _wasi_env_new = - _lib.lookupFunction( - 'wasi_env_new', - ); - _wasi_env_read_stderr = _lib.lookupFunction( - 'wasi_env_read_stderr', - ); - _wasi_env_read_stdout = _lib.lookupFunction( - 'wasi_env_read_stdout', - ); - _wasi_env_set_memory = _lib.lookupFunction( - 'wasi_env_set_memory', - ); - _wasi_get_imports = _lib - .lookupFunction( - 'wasi_get_imports', - ); - _byte_vec_delete = - _lib.lookupFunction( - 'wasm_byte_vec_delete', - ); - _byte_vec_new = - _lib.lookupFunction( - 'wasm_byte_vec_new', - ); - _byte_vec_new_empty = _lib - .lookupFunction( - 'wasm_byte_vec_new_empty', - ); - _byte_vec_new_uninitialized = _lib.lookupFunction< - NativeWasmerByteVecNewUninitializedFn, WasmerByteVecNewUninitializedFn>( - 'wasm_byte_vec_new_uninitialized', - ); - _engine_delete = - _lib.lookupFunction( - 'wasm_engine_delete', - ); - _engine_new = - _lib.lookupFunction( - 'wasm_engine_new', - ); - _exporttype_name = _lib - .lookupFunction( - 'wasm_exporttype_name', - ); - _exporttype_type = _lib - .lookupFunction( - 'wasm_exporttype_type', - ); - _exporttype_vec_delete = _lib.lookupFunction< - NativeWasmerExporttypeVecDeleteFn, WasmerExporttypeVecDeleteFn>( - 'wasm_exporttype_vec_delete', - ); - _exporttype_vec_new = _lib.lookupFunction( - 'wasm_exporttype_vec_new', - ); - _exporttype_vec_new_empty = _lib.lookupFunction< - NativeWasmerExporttypeVecNewEmptyFn, WasmerExporttypeVecNewEmptyFn>( - 'wasm_exporttype_vec_new_empty', - ); - _exporttype_vec_new_uninitialized = _lib.lookupFunction< - NativeWasmerExporttypeVecNewUninitializedFn, - WasmerExporttypeVecNewUninitializedFn>( - 'wasm_exporttype_vec_new_uninitialized', - ); - _extern_as_func = - _lib.lookupFunction( - 'wasm_extern_as_func', - ); - _extern_as_memory = _lib - .lookupFunction( - 'wasm_extern_as_memory', - ); - _extern_delete = - _lib.lookupFunction( - 'wasm_extern_delete', - ); - _extern_kind = - _lib.lookupFunction( - 'wasm_extern_kind', - ); - _extern_vec_delete = _lib - .lookupFunction( - 'wasm_extern_vec_delete', - ); - _extern_vec_new = - _lib.lookupFunction( - 'wasm_extern_vec_new', - ); - _extern_vec_new_empty = _lib.lookupFunction( - 'wasm_extern_vec_new_empty', - ); - _extern_vec_new_uninitialized = _lib.lookupFunction< - NativeWasmerExternVecNewUninitializedFn, - WasmerExternVecNewUninitializedFn>( - 'wasm_extern_vec_new_uninitialized', - ); - _externtype_as_functype = _lib.lookupFunction< - NativeWasmerExterntypeAsFunctypeFn, WasmerExterntypeAsFunctypeFn>( - 'wasm_externtype_as_functype', - ); - _externtype_delete = _lib.lookupFunction( - 'wasm_externtype_delete', - ); - _externtype_kind = _lib - .lookupFunction( - 'wasm_externtype_kind', - ); - _func_as_extern = - _lib.lookupFunction( - 'wasm_func_as_extern', - ); - _func_call = _lib.lookupFunction( - 'wasm_func_call', - ); - _func_delete = - _lib.lookupFunction( - 'wasm_func_delete', - ); - _func_new_with_env = _lib - .lookupFunction( - 'wasm_func_new_with_env', - ); - _functype_delete = _lib - .lookupFunction( - 'wasm_functype_delete', - ); - _functype_params = _lib - .lookupFunction( - 'wasm_functype_params', - ); - _functype_results = _lib - .lookupFunction( - 'wasm_functype_results', - ); - _importtype_module = _lib.lookupFunction( - 'wasm_importtype_module', - ); - _importtype_name = _lib - .lookupFunction( - 'wasm_importtype_name', - ); - _importtype_type = _lib - .lookupFunction( - 'wasm_importtype_type', - ); - _importtype_vec_delete = _lib.lookupFunction< - NativeWasmerImporttypeVecDeleteFn, WasmerImporttypeVecDeleteFn>( - 'wasm_importtype_vec_delete', - ); - _importtype_vec_new = _lib.lookupFunction( - 'wasm_importtype_vec_new', - ); - _importtype_vec_new_empty = _lib.lookupFunction< - NativeWasmerImporttypeVecNewEmptyFn, WasmerImporttypeVecNewEmptyFn>( - 'wasm_importtype_vec_new_empty', - ); - _importtype_vec_new_uninitialized = _lib.lookupFunction< - NativeWasmerImporttypeVecNewUninitializedFn, - WasmerImporttypeVecNewUninitializedFn>( - 'wasm_importtype_vec_new_uninitialized', - ); - _instance_delete = _lib - .lookupFunction( - 'wasm_instance_delete', - ); - _instance_exports = _lib - .lookupFunction( - 'wasm_instance_exports', - ); - _instance_new = - _lib.lookupFunction( - 'wasm_instance_new', - ); - _memory_as_extern = _lib - .lookupFunction( - 'wasm_memory_as_extern', - ); - _memory_data = - _lib.lookupFunction( - 'wasm_memory_data', - ); - _memory_data_size = _lib - .lookupFunction( - 'wasm_memory_data_size', - ); - _memory_delete = - _lib.lookupFunction( - 'wasm_memory_delete', - ); - _memory_grow = - _lib.lookupFunction( - 'wasm_memory_grow', - ); - _memory_new = - _lib.lookupFunction( - 'wasm_memory_new', - ); - _memory_size = - _lib.lookupFunction( - 'wasm_memory_size', - ); - _memorytype_delete = _lib.lookupFunction( - 'wasm_memorytype_delete', - ); - _memorytype_new = - _lib.lookupFunction( - 'wasm_memorytype_new', - ); - _module_delete = - _lib.lookupFunction( - 'wasm_module_delete', - ); - _module_exports = - _lib.lookupFunction( - 'wasm_module_exports', - ); - _module_imports = - _lib.lookupFunction( - 'wasm_module_imports', - ); - _module_new = - _lib.lookupFunction( - 'wasm_module_new', - ); - _store_delete = - _lib.lookupFunction( - 'wasm_store_delete', - ); - _store_new = _lib.lookupFunction( - 'wasm_store_new', - ); - _trap_delete = - _lib.lookupFunction( - 'wasm_trap_delete', - ); - _trap_message = - _lib.lookupFunction( - 'wasm_trap_message', - ); - _trap_new = _lib.lookupFunction( - 'wasm_trap_new', - ); - _valtype_delete = - _lib.lookupFunction( - 'wasm_valtype_delete', - ); - _valtype_kind = - _lib.lookupFunction( - 'wasm_valtype_kind', - ); - _valtype_vec_delete = _lib.lookupFunction( - 'wasm_valtype_vec_delete', - ); - _valtype_vec_new = - _lib.lookupFunction( - 'wasm_valtype_vec_new', - ); - _valtype_vec_new_empty = _lib.lookupFunction< - NativeWasmerValtypeVecNewEmptyFn, WasmerValtypeVecNewEmptyFn>( - 'wasm_valtype_vec_new_empty', - ); - _valtype_vec_new_uninitialized = _lib.lookupFunction< - NativeWasmerValtypeVecNewUninitializedFn, - WasmerValtypeVecNewUninitializedFn>( - 'wasm_valtype_vec_new_uninitialized', - ); - _wasmer_last_error_length = _lib.lookupFunction< - NativeWasmerWasmerLastErrorLengthFn, WasmerWasmerLastErrorLengthFn>( - 'wasmer_last_error_length', - ); - _wasmer_last_error_message = _lib.lookupFunction< - NativeWasmerWasmerLastErrorMessageFn, WasmerWasmerLastErrorMessageFn>( - 'wasmer_last_error_message', - ); - - if (_Dart_InitializeApiDL(NativeApi.initializeApiDLData) != 0) { - throw WasmError('Failed to initialize Dart API'); - } - _engine = _engine_new(); - _checkNotEqual(_engine, nullptr, 'Failed to initialize Wasm engine.'); - _set_finalizer_for_engine(this, _engine); - } - - Pointer newStore(Object owner) { - var store = _checkNotEqual( - _store_new(_engine), - nullptr, - 'Failed to create Wasm store.', - ); - _set_finalizer_for_store(owner, store); - return store; - } - - Pointer compile( - Object owner, - Pointer store, - Uint8List data, - ) { - var dataPtr = calloc(data.length); - for (var i = 0; i < data.length; ++i) { - dataPtr[i] = data[i]; - } - var dataVec = calloc(); - dataVec.ref.data = dataPtr; - dataVec.ref.length = data.length; - - var modulePtr = _module_new(store, dataVec); - - calloc.free(dataPtr); - calloc.free(dataVec); - - _checkNotEqual(modulePtr, nullptr, 'Wasm module compile failed.'); - _set_finalizer_for_module(owner, modulePtr); - return modulePtr; - } - - List exportDescriptors(Pointer module) { - var exportsVec = calloc(); - _module_exports(module, exportsVec); - var exps = []; - for (var i = 0; i < exportsVec.ref.length; ++i) { - var exp = exportsVec.ref.data[i]; - var extern = _exporttype_type(exp); - var kind = _externtype_kind(extern); - var fnType = kind == wasmerExternKindFunction - ? _externtype_as_functype(extern) - : nullptr; - exps.add( - WasmExportDescriptor._( - kind, - _exporttype_name(exp).ref.toString(), - fnType, - ), - ); - } - calloc.free(exportsVec); - return exps; - } - - List importDescriptors(Pointer module) { - var importsVec = calloc(); - _module_imports(module, importsVec); - var imps = []; - for (var i = 0; i < importsVec.ref.length; ++i) { - var imp = importsVec.ref.data[i]; - var extern = _importtype_type(imp); - var kind = _externtype_kind(extern); - var fnType = kind == wasmerExternKindFunction - ? _externtype_as_functype(extern) - : nullptr; - imps.add( - WasmImportDescriptor._( - kind, - _importtype_module(imp).ref.toString(), - _importtype_name(imp).ref.toString(), - fnType, - ), - ); - } - calloc.free(importsVec); - return imps; - } - - void maybeThrowTrap(Pointer trap, String source) { - if (trap != nullptr) { - // There are 2 kinds of trap, and their memory is managed differently. - // Traps created in the newTrap method below are stored in the traps map - // with a corresponding exception, and their memory is managed using a - // finalizer on the _WasmTrapsEntry. Traps can also be created by WASM - // code, and in that case we delete them in this function. - var entry = _traps.remove(trap.address); - if (entry == null) { - throw WasmError( - 'This case is not (yet) supported. Please file an issue on pkg:wasm.', - ); - } - // ignore: only_throw_errors - throw entry.exception; - } - } - - Pointer instantiate( - Object owner, - Pointer store, - Pointer module, - Pointer imports, - ) { - var trap = calloc>(); - trap.value = nullptr; - var inst = _instance_new(store, module, imports, trap); - maybeThrowTrap(trap.value, 'module initialization function'); - calloc.free(trap); - _checkNotEqual(inst, nullptr, 'Wasm module instantiation failed.'); - _set_finalizer_for_instance(owner, inst); - return inst; - } - - // Clean up the exports after use, with deleteExports. - Pointer exports(Pointer instancePtr) { - var exports = calloc(); - _instance_exports(instancePtr, exports); - return exports; - } - - void deleteExports(Pointer exports) { - _extern_vec_delete(exports); - calloc.free(exports); - } - - int externKind(Pointer extern) => _extern_kind(extern); - - Pointer externToFunction(Pointer extern) => - _extern_as_func(extern); - - Pointer functionToExtern(Pointer func) => - _func_as_extern(func); - - List getArgTypes(Pointer funcType) { - var types = []; - var args = _functype_params(funcType); - for (var i = 0; i < args.ref.length; ++i) { - types.add(_valtype_kind(args.ref.data[i])); - } - return types; - } - - int getReturnType(Pointer funcType) { - var rets = _functype_results(funcType); - if (rets.ref.length == 0) { - return wasmerValKindVoid; - } else if (rets.ref.length > 1) { - throw WasmError('Multiple return values are not supported'); - } - return _valtype_kind(rets.ref.data[0]); - } - - void call( - Pointer func, - Pointer args, - Pointer results, - String source, - ) { - maybeThrowTrap(_func_call(func, args, results), source); - } - - Pointer externToMemory(Pointer extern) => - _extern_as_memory(extern); - - Pointer memoryToExtern(Pointer memory) => - _memory_as_extern(memory); - - Pointer newMemory( - Object owner, - Pointer store, - int pages, - int? maxPages, - ) { - var limPtr = calloc(); - limPtr.ref.min = pages; - limPtr.ref.max = maxPages ?? wasmLimitsMaxDefault; - var memType = _memorytype_new(limPtr); - calloc.free(limPtr); - _checkNotEqual(memType, nullptr, 'Failed to create memory type.'); - _set_finalizer_for_memorytype(owner, memType); - var memory = _checkNotEqual( - _memory_new(store, memType), - nullptr, - 'Failed to create memory.', - ); - _set_finalizer_for_memory(owner, memory); - return memory; - } - - void growMemory(Pointer memory, int deltaPages) { - _checkNotEqual( - _memory_grow(memory, deltaPages), - 0, - 'Failed to grow memory.', - ); - } - - int memoryLength(Pointer memory) => _memory_size(memory); - - Uint8List memoryView(Pointer memory) => - _memory_data(memory).asTypedList(_memory_data_size(memory)); - - Pointer newFunc( - Object owner, - Pointer store, - Pointer funcType, - Pointer func, - Pointer env, - Pointer finalizer, - ) { - var f = _func_new_with_env( - store, - funcType, - func.cast(), - env.cast(), - finalizer.cast(), - ); - _checkNotEqual(f, nullptr, 'Failed to create function.'); - _set_finalizer_for_func(owner, f); - return f; - } - - Pointer newTrap(Pointer store, Object exception) { - var msg = calloc(); - msg.ref.data = calloc(); - msg.ref.data[0] = 0; - msg.ref.length = 0; - var trap = _trap_new(store, msg); - calloc.free(msg.ref.data); - calloc.free(msg); - _checkNotEqual(trap, nullptr, 'Failed to create trap.'); - var entry = _WasmTrapsEntry(exception); - _set_finalizer_for_trap(entry, trap); - _traps[trap.address] = entry; - return trap; - } - - Pointer newWasiConfig() { - var name = calloc(); - name[0] = 0; - var config = _wasi_config_new(name); - calloc.free(name); - return _checkNotEqual(config, nullptr, 'Failed to create WASI config.'); - } - - void captureWasiStdout(Pointer config) { - _wasi_config_inherit_stdout(config); - } - - void captureWasiStderr(Pointer config) { - _wasi_config_inherit_stderr(config); - } - - Pointer newWasiEnv(Pointer config) => - _checkNotEqual( - _wasi_env_new(config), - nullptr, - 'Failed to create WASI environment.', - ); - - void wasiEnvSetMemory( - Pointer env, - Pointer memory, - ) { - _wasi_env_set_memory(env, memory); - } - - void getWasiImports( - Pointer store, - Pointer mod, - Pointer env, - Pointer imports, - ) { - _checkNotEqual( - _wasi_get_imports(store, mod, env, imports), - 0, - 'Failed to fill WASI imports.', - ); - } - - Stream> getWasiStdoutStream(Pointer env) => - Stream.fromIterable(_WasiStreamIterable(env, _wasi_env_read_stdout)); - - Stream> getWasiStderrStream(Pointer env) => - Stream.fromIterable(_WasiStreamIterable(env, _wasi_env_read_stderr)); - - String _getLastError() { - var length = _wasmer_last_error_length(); - var buf = calloc(length); - _wasmer_last_error_message(buf, length); - var message = utf8.decode(buf.asTypedList(length)); - calloc.free(buf); - return message; - } - - T _checkNotEqual(T x, T y, String errorMessage) { - if (x == y) { - throw WasmError('$errorMessage\n${_getLastError()}'.trim()); - } - return x; - } - - static String getSignatureString( - String name, - List argTypes, - int returnType, - ) => - '${wasmerValKindName(returnType)} ' - "$name(${argTypes.map(wasmerValKindName).join(", ")})"; -} diff --git a/pkg/wasm/lib/src/shared.dart b/pkg/wasm/lib/src/shared.dart deleted file mode 100644 index 0f666b7612a..00000000000 --- a/pkg/wasm/lib/src/shared.dart +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright (c) 2020, 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. - -import 'dart:io'; - -const invocationString = 'dart run wasm:setup'; - -const pkgConfigFile = '.dart_tool/package_config.json'; -const wasmToolDir = '.dart_tool/wasm/'; - -const appleLib = 'libwasmer.dylib'; -const linuxLib = 'libwasmer.so'; - -Uri? packageRootUri(Uri root) { - do { - if (FileSystemEntity.isFileSync(root.resolve(pkgConfigFile).path)) { - return root; - } - } while (root != (root = root.resolve('..'))); - return null; -} diff --git a/pkg/wasm/lib/src/wasm_error.dart b/pkg/wasm/lib/src/wasm_error.dart deleted file mode 100644 index 7081bcc2cd6..00000000000 --- a/pkg/wasm/lib/src/wasm_error.dart +++ /dev/null @@ -1,14 +0,0 @@ -// Copyright (c) 2020, 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. - -/// Error specific to unexpected behavior or incorrect usage of this package. -class WasmError extends Error { - /// Describes the nature of the error. - final String message; - - WasmError(this.message) : assert(message.trim() == message); - - @override - String toString() => 'WasmError:$message'; -} diff --git a/pkg/wasm/lib/src/wasmer_api.dart b/pkg/wasm/lib/src/wasmer_api.dart deleted file mode 100644 index b9f45b06dc8..00000000000 --- a/pkg/wasm/lib/src/wasmer_api.dart +++ /dev/null @@ -1,130 +0,0 @@ -// Copyright (c) 2020, 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. - -// This file has been automatically generated. Please do not edit it manually. -// To regenerate the file, use the following command -// "generate_ffi_boilerplate.py". - -import 'dart:convert'; -import 'dart:ffi'; -import 'dart:typed_data'; - -part 'wasmer_api.g.dart'; - -// wasm_valkind_enum -const int wasmerValKindI32 = 0; -const int wasmerValKindI64 = 1; -const int wasmerValKindF32 = 2; -const int wasmerValKindF64 = 3; -// The void tag is not part of the C API. It's used to represent the return type -// of a void function. -const int wasmerValKindVoid = -1; - -// wasm_externkind_enum -const int wasmerExternKindFunction = 0; -const int wasmerExternKindGlobal = 1; -const int wasmerExternKindTable = 2; -const int wasmerExternKindMemory = 3; - -String wasmerExternKindName(int kind) { - switch (kind) { - case wasmerExternKindFunction: - return 'function'; - case wasmerExternKindGlobal: - return 'global'; - case wasmerExternKindTable: - return 'table'; - case wasmerExternKindMemory: - return 'memory'; - default: - return 'unknown'; - } -} - -String wasmerValKindName(int kind) { - switch (kind) { - case wasmerValKindI32: - return 'int32'; - case wasmerValKindI64: - return 'int64'; - case wasmerValKindF32: - return 'float32'; - case wasmerValKindF64: - return 'float64'; - case wasmerValKindVoid: - return 'void'; - default: - return 'unknown'; - } -} - -// wasm_val_t -class WasmerVal extends Struct { - // wasm_valkind_t - @Uint8() - external int kind; - - // This is a union of int32_t, int64_t, float, and double. The kind determines - // which type it is. It's declared as an int64_t because that's large enough - // to hold all the types. We use ByteData to get the other types. - @Int64() - external int value; - - int get _off32 => Endian.host == Endian.little ? 0 : 4; - - int get i64 => value; - - ByteData get _getterBytes => ByteData(8)..setInt64(0, value, Endian.host); - - int get i32 => _getterBytes.getInt32(_off32, Endian.host); - - double get f32 => _getterBytes.getFloat32(_off32, Endian.host); - - double get f64 => _getterBytes.getFloat64(0, Endian.host); - - set i64(int val) => value = val; - - set _val(ByteData bytes) => value = bytes.getInt64(0, Endian.host); - - set i32(int val) => _val = ByteData(8)..setInt32(_off32, val, Endian.host); - - set f32(num val) => - _val = ByteData(8)..setFloat32(_off32, val as double, Endian.host); - - set f64(num val) => - _val = ByteData(8)..setFloat64(0, val as double, Endian.host); - - bool get isI32 => kind == wasmerValKindI32; - - bool get isI64 => kind == wasmerValKindI64; - - bool get isF32 => kind == wasmerValKindF32; - - bool get isF64 => kind == wasmerValKindF64; - - dynamic get toDynamic { - switch (kind) { - case wasmerValKindI32: - return i32; - case wasmerValKindI64: - return i64; - case wasmerValKindF32: - return f32; - case wasmerValKindF64: - return f64; - } - } -} - -// wasmer_limits_t -class WasmerLimits extends Struct { - @Uint32() - external int min; - - @Uint32() - external int max; -} - -// Default maximum, which indicates no upper limit. -const int wasmLimitsMaxDefault = 0xffffffff; diff --git a/pkg/wasm/lib/src/wasmer_api.g.dart b/pkg/wasm/lib/src/wasmer_api.g.dart deleted file mode 100644 index 4211e7f1daf..00000000000 --- a/pkg/wasm/lib/src/wasmer_api.g.dart +++ /dev/null @@ -1,577 +0,0 @@ -// Copyright (c) 2020, 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. - -// This file has been automatically generated. Please do not edit it manually. -// To regenerate the file, use the following command -// "generate_ffi_boilerplate.py". - -// ignore_for_file: require_trailing_commas - -part of 'wasmer_api.dart'; - -// wasm_engine_t -class WasmerEngine extends Opaque {} - -// wasm_exporttype_t -class WasmerExporttype extends Opaque {} - -// wasm_extern_t -class WasmerExtern extends Opaque {} - -// wasm_externtype_t -class WasmerExterntype extends Opaque {} - -// wasm_func_t -class WasmerFunc extends Opaque {} - -// wasm_functype_t -class WasmerFunctype extends Opaque {} - -// wasm_importtype_t -class WasmerImporttype extends Opaque {} - -// wasm_instance_t -class WasmerInstance extends Opaque {} - -// wasm_memory_t -class WasmerMemory extends Opaque {} - -// wasm_memorytype_t -class WasmerMemorytype extends Opaque {} - -// wasm_module_t -class WasmerModule extends Opaque {} - -// wasm_store_t -class WasmerStore extends Opaque {} - -// wasm_trap_t -class WasmerTrap extends Opaque {} - -// wasm_valtype_t -class WasmerValtype extends Opaque {} - -// wasi_config_t -class WasmerWasiConfig extends Opaque {} - -// wasi_env_t -class WasmerWasiEnv extends Opaque {} - -// wasm_byte_vec_t -class WasmerByteVec extends Struct { - @Uint64() - external int length; - - external Pointer data; - - Uint8List get list => data.asTypedList(length); - @override - String toString() => utf8.decode(list); -} - -// wasm_exporttype_vec_t -class WasmerExporttypeVec extends Struct { - @Uint64() - external int length; - - external Pointer> data; -} - -// wasm_extern_vec_t -class WasmerExternVec extends Struct { - @Uint64() - external int length; - - external Pointer> data; -} - -// wasm_importtype_vec_t -class WasmerImporttypeVec extends Struct { - @Uint64() - external int length; - - external Pointer> data; -} - -// wasm_val_vec_t -class WasmerValVec extends Struct { - @Uint64() - external int length; - - external Pointer data; -} - -// wasm_valtype_vec_t -class WasmerValtypeVec extends Struct { - @Uint64() - external int length; - - external Pointer> data; -} - -// Dart_InitializeApiDL -typedef NativeWasmerDartInitializeApiDLFn = Int64 Function(Pointer); -typedef WasmerDartInitializeApiDLFn = int Function(Pointer); - -// set_finalizer_for_engine -typedef NativeWasmerSetFinalizerForEngineFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForEngineFn = void Function( - Object, Pointer); - -// set_finalizer_for_func -typedef NativeWasmerSetFinalizerForFuncFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForFuncFn = void Function( - Object, Pointer); - -// set_finalizer_for_instance -typedef NativeWasmerSetFinalizerForInstanceFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForInstanceFn = void Function( - Object, Pointer); - -// set_finalizer_for_memory -typedef NativeWasmerSetFinalizerForMemoryFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForMemoryFn = void Function( - Object, Pointer); - -// set_finalizer_for_memorytype -typedef NativeWasmerSetFinalizerForMemorytypeFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForMemorytypeFn = void Function( - Object, Pointer); - -// set_finalizer_for_module -typedef NativeWasmerSetFinalizerForModuleFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForModuleFn = void Function( - Object, Pointer); - -// set_finalizer_for_store -typedef NativeWasmerSetFinalizerForStoreFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForStoreFn = void Function( - Object, Pointer); - -// set_finalizer_for_trap -typedef NativeWasmerSetFinalizerForTrapFn = Void Function( - Handle, Pointer); -typedef WasmerSetFinalizerForTrapFn = void Function( - Object, Pointer); - -// wasi_config_inherit_stderr -typedef NativeWasmerWasiConfigInheritStderrFn = Void Function( - Pointer); -typedef WasmerWasiConfigInheritStderrFn = void Function( - Pointer); - -// wasi_config_inherit_stdout -typedef NativeWasmerWasiConfigInheritStdoutFn = Void Function( - Pointer); -typedef WasmerWasiConfigInheritStdoutFn = void Function( - Pointer); - -// wasi_config_new -typedef NativeWasmerWasiConfigNewFn = Pointer Function( - Pointer); -typedef WasmerWasiConfigNewFn = Pointer Function( - Pointer); - -// wasi_env_delete -typedef NativeWasmerWasiEnvDeleteFn = Void Function(Pointer); -typedef WasmerWasiEnvDeleteFn = void Function(Pointer); - -// wasi_env_new -typedef NativeWasmerWasiEnvNewFn = Pointer Function( - Pointer); -typedef WasmerWasiEnvNewFn = Pointer Function( - Pointer); - -// wasi_env_read_stderr -typedef NativeWasmerWasiEnvReadStderrFn = Int64 Function( - Pointer, Pointer, Uint64); -typedef WasmerWasiEnvReadStderrFn = int Function( - Pointer, Pointer, int); - -// wasi_env_read_stdout -typedef NativeWasmerWasiEnvReadStdoutFn = Int64 Function( - Pointer, Pointer, Uint64); -typedef WasmerWasiEnvReadStdoutFn = int Function( - Pointer, Pointer, int); - -// wasi_env_set_memory -typedef NativeWasmerWasiEnvSetMemoryFn = Void Function( - Pointer, Pointer); -typedef WasmerWasiEnvSetMemoryFn = void Function( - Pointer, Pointer); - -// wasi_get_imports -typedef NativeWasmerWasiGetImportsFn = Uint8 Function(Pointer, - Pointer, Pointer, Pointer); -typedef WasmerWasiGetImportsFn = int Function(Pointer, - Pointer, Pointer, Pointer); - -// wasm_byte_vec_delete -typedef NativeWasmerByteVecDeleteFn = Void Function(Pointer); -typedef WasmerByteVecDeleteFn = void Function(Pointer); - -// wasm_byte_vec_new -typedef NativeWasmerByteVecNewFn = Void Function( - Pointer, Uint64, Pointer); -typedef WasmerByteVecNewFn = void Function( - Pointer, int, Pointer); - -// wasm_byte_vec_new_empty -typedef NativeWasmerByteVecNewEmptyFn = Void Function(Pointer); -typedef WasmerByteVecNewEmptyFn = void Function(Pointer); - -// wasm_byte_vec_new_uninitialized -typedef NativeWasmerByteVecNewUninitializedFn = Void Function( - Pointer, Uint64); -typedef WasmerByteVecNewUninitializedFn = void Function( - Pointer, int); - -// wasm_engine_delete -typedef NativeWasmerEngineDeleteFn = Void Function(Pointer); -typedef WasmerEngineDeleteFn = void Function(Pointer); - -// wasm_engine_new -typedef NativeWasmerEngineNewFn = Pointer Function(); -typedef WasmerEngineNewFn = Pointer Function(); - -// wasm_exporttype_name -typedef NativeWasmerExporttypeNameFn = Pointer Function( - Pointer); -typedef WasmerExporttypeNameFn = Pointer Function( - Pointer); - -// wasm_exporttype_type -typedef NativeWasmerExporttypeTypeFn = Pointer Function( - Pointer); -typedef WasmerExporttypeTypeFn = Pointer Function( - Pointer); - -// wasm_exporttype_vec_delete -typedef NativeWasmerExporttypeVecDeleteFn = Void Function( - Pointer); -typedef WasmerExporttypeVecDeleteFn = void Function( - Pointer); - -// wasm_exporttype_vec_new -typedef NativeWasmerExporttypeVecNewFn = Void Function( - Pointer, Uint64, Pointer>); -typedef WasmerExporttypeVecNewFn = void Function( - Pointer, int, Pointer>); - -// wasm_exporttype_vec_new_empty -typedef NativeWasmerExporttypeVecNewEmptyFn = Void Function( - Pointer); -typedef WasmerExporttypeVecNewEmptyFn = void Function( - Pointer); - -// wasm_exporttype_vec_new_uninitialized -typedef NativeWasmerExporttypeVecNewUninitializedFn = Void Function( - Pointer, Uint64); -typedef WasmerExporttypeVecNewUninitializedFn = void Function( - Pointer, int); - -// wasm_extern_as_func -typedef NativeWasmerExternAsFuncFn = Pointer Function( - Pointer); -typedef WasmerExternAsFuncFn = Pointer Function( - Pointer); - -// wasm_extern_as_memory -typedef NativeWasmerExternAsMemoryFn = Pointer Function( - Pointer); -typedef WasmerExternAsMemoryFn = Pointer Function( - Pointer); - -// wasm_extern_delete -typedef NativeWasmerExternDeleteFn = Void Function(Pointer); -typedef WasmerExternDeleteFn = void Function(Pointer); - -// wasm_extern_kind -typedef NativeWasmerExternKindFn = Uint8 Function(Pointer); -typedef WasmerExternKindFn = int Function(Pointer); - -// wasm_extern_vec_delete -typedef NativeWasmerExternVecDeleteFn = Void Function(Pointer); -typedef WasmerExternVecDeleteFn = void Function(Pointer); - -// wasm_extern_vec_new -typedef NativeWasmerExternVecNewFn = Void Function( - Pointer, Uint64, Pointer>); -typedef WasmerExternVecNewFn = void Function( - Pointer, int, Pointer>); - -// wasm_extern_vec_new_empty -typedef NativeWasmerExternVecNewEmptyFn = Void Function( - Pointer); -typedef WasmerExternVecNewEmptyFn = void Function(Pointer); - -// wasm_extern_vec_new_uninitialized -typedef NativeWasmerExternVecNewUninitializedFn = Void Function( - Pointer, Uint64); -typedef WasmerExternVecNewUninitializedFn = void Function( - Pointer, int); - -// wasm_externtype_as_functype -typedef NativeWasmerExterntypeAsFunctypeFn = Pointer Function( - Pointer); -typedef WasmerExterntypeAsFunctypeFn = Pointer Function( - Pointer); - -// wasm_externtype_delete -typedef NativeWasmerExterntypeDeleteFn = Void Function( - Pointer); -typedef WasmerExterntypeDeleteFn = void Function(Pointer); - -// wasm_externtype_kind -typedef NativeWasmerExterntypeKindFn = Uint8 Function( - Pointer); -typedef WasmerExterntypeKindFn = int Function(Pointer); - -// wasm_func_as_extern -typedef NativeWasmerFuncAsExternFn = Pointer Function( - Pointer); -typedef WasmerFuncAsExternFn = Pointer Function( - Pointer); - -// wasm_func_call -typedef NativeWasmerFuncCallFn = Pointer Function( - Pointer, Pointer, Pointer); -typedef WasmerFuncCallFn = Pointer Function( - Pointer, Pointer, Pointer); - -// wasm_func_delete -typedef NativeWasmerFuncDeleteFn = Void Function(Pointer); -typedef WasmerFuncDeleteFn = void Function(Pointer); - -// wasm_func_new_with_env -typedef NativeWasmerFuncNewWithEnvFn = Pointer Function( - Pointer, - Pointer, - Pointer, - Pointer, - Pointer); -typedef WasmerFuncNewWithEnvFn = Pointer Function( - Pointer, - Pointer, - Pointer, - Pointer, - Pointer); - -// wasm_functype_delete -typedef NativeWasmerFunctypeDeleteFn = Void Function(Pointer); -typedef WasmerFunctypeDeleteFn = void Function(Pointer); - -// wasm_functype_params -typedef NativeWasmerFunctypeParamsFn = Pointer Function( - Pointer); -typedef WasmerFunctypeParamsFn = Pointer Function( - Pointer); - -// wasm_functype_results -typedef NativeWasmerFunctypeResultsFn = Pointer Function( - Pointer); -typedef WasmerFunctypeResultsFn = Pointer Function( - Pointer); - -// wasm_importtype_module -typedef NativeWasmerImporttypeModuleFn = Pointer Function( - Pointer); -typedef WasmerImporttypeModuleFn = Pointer Function( - Pointer); - -// wasm_importtype_name -typedef NativeWasmerImporttypeNameFn = Pointer Function( - Pointer); -typedef WasmerImporttypeNameFn = Pointer Function( - Pointer); - -// wasm_importtype_type -typedef NativeWasmerImporttypeTypeFn = Pointer Function( - Pointer); -typedef WasmerImporttypeTypeFn = Pointer Function( - Pointer); - -// wasm_importtype_vec_delete -typedef NativeWasmerImporttypeVecDeleteFn = Void Function( - Pointer); -typedef WasmerImporttypeVecDeleteFn = void Function( - Pointer); - -// wasm_importtype_vec_new -typedef NativeWasmerImporttypeVecNewFn = Void Function( - Pointer, Uint64, Pointer>); -typedef WasmerImporttypeVecNewFn = void Function( - Pointer, int, Pointer>); - -// wasm_importtype_vec_new_empty -typedef NativeWasmerImporttypeVecNewEmptyFn = Void Function( - Pointer); -typedef WasmerImporttypeVecNewEmptyFn = void Function( - Pointer); - -// wasm_importtype_vec_new_uninitialized -typedef NativeWasmerImporttypeVecNewUninitializedFn = Void Function( - Pointer, Uint64); -typedef WasmerImporttypeVecNewUninitializedFn = void Function( - Pointer, int); - -// wasm_instance_delete -typedef NativeWasmerInstanceDeleteFn = Void Function(Pointer); -typedef WasmerInstanceDeleteFn = void Function(Pointer); - -// wasm_instance_exports -typedef NativeWasmerInstanceExportsFn = Void Function( - Pointer, Pointer); -typedef WasmerInstanceExportsFn = void Function( - Pointer, Pointer); - -// wasm_instance_new -typedef NativeWasmerInstanceNewFn = Pointer Function( - Pointer, - Pointer, - Pointer, - Pointer>); -typedef WasmerInstanceNewFn = Pointer Function( - Pointer, - Pointer, - Pointer, - Pointer>); - -// wasm_memory_as_extern -typedef NativeWasmerMemoryAsExternFn = Pointer Function( - Pointer); -typedef WasmerMemoryAsExternFn = Pointer Function( - Pointer); - -// wasm_memory_data -typedef NativeWasmerMemoryDataFn = Pointer Function( - Pointer); -typedef WasmerMemoryDataFn = Pointer Function(Pointer); - -// wasm_memory_data_size -typedef NativeWasmerMemoryDataSizeFn = Uint64 Function(Pointer); -typedef WasmerMemoryDataSizeFn = int Function(Pointer); - -// wasm_memory_delete -typedef NativeWasmerMemoryDeleteFn = Void Function(Pointer); -typedef WasmerMemoryDeleteFn = void Function(Pointer); - -// wasm_memory_grow -typedef NativeWasmerMemoryGrowFn = Uint8 Function( - Pointer, Uint32); -typedef WasmerMemoryGrowFn = int Function(Pointer, int); - -// wasm_memory_new -typedef NativeWasmerMemoryNewFn = Pointer Function( - Pointer, Pointer); -typedef WasmerMemoryNewFn = Pointer Function( - Pointer, Pointer); - -// wasm_memory_size -typedef NativeWasmerMemorySizeFn = Uint32 Function(Pointer); -typedef WasmerMemorySizeFn = int Function(Pointer); - -// wasm_memorytype_delete -typedef NativeWasmerMemorytypeDeleteFn = Void Function( - Pointer); -typedef WasmerMemorytypeDeleteFn = void Function(Pointer); - -// wasm_memorytype_new -typedef NativeWasmerMemorytypeNewFn = Pointer Function( - Pointer); -typedef WasmerMemorytypeNewFn = Pointer Function( - Pointer); - -// wasm_module_delete -typedef NativeWasmerModuleDeleteFn = Void Function(Pointer); -typedef WasmerModuleDeleteFn = void Function(Pointer); - -// wasm_module_exports -typedef NativeWasmerModuleExportsFn = Void Function( - Pointer, Pointer); -typedef WasmerModuleExportsFn = void Function( - Pointer, Pointer); - -// wasm_module_imports -typedef NativeWasmerModuleImportsFn = Void Function( - Pointer, Pointer); -typedef WasmerModuleImportsFn = void Function( - Pointer, Pointer); - -// wasm_module_new -typedef NativeWasmerModuleNewFn = Pointer Function( - Pointer, Pointer); -typedef WasmerModuleNewFn = Pointer Function( - Pointer, Pointer); - -// wasm_store_delete -typedef NativeWasmerStoreDeleteFn = Void Function(Pointer); -typedef WasmerStoreDeleteFn = void Function(Pointer); - -// wasm_store_new -typedef NativeWasmerStoreNewFn = Pointer Function( - Pointer); -typedef WasmerStoreNewFn = Pointer Function(Pointer); - -// wasm_trap_delete -typedef NativeWasmerTrapDeleteFn = Void Function(Pointer); -typedef WasmerTrapDeleteFn = void Function(Pointer); - -// wasm_trap_message -typedef NativeWasmerTrapMessageFn = Void Function( - Pointer, Pointer); -typedef WasmerTrapMessageFn = void Function( - Pointer, Pointer); - -// wasm_trap_new -typedef NativeWasmerTrapNewFn = Pointer Function( - Pointer, Pointer); -typedef WasmerTrapNewFn = Pointer Function( - Pointer, Pointer); - -// wasm_valtype_delete -typedef NativeWasmerValtypeDeleteFn = Void Function(Pointer); -typedef WasmerValtypeDeleteFn = void Function(Pointer); - -// wasm_valtype_kind -typedef NativeWasmerValtypeKindFn = Uint8 Function(Pointer); -typedef WasmerValtypeKindFn = int Function(Pointer); - -// wasm_valtype_vec_delete -typedef NativeWasmerValtypeVecDeleteFn = Void Function( - Pointer); -typedef WasmerValtypeVecDeleteFn = void Function(Pointer); - -// wasm_valtype_vec_new -typedef NativeWasmerValtypeVecNewFn = Void Function( - Pointer, Uint64, Pointer>); -typedef WasmerValtypeVecNewFn = void Function( - Pointer, int, Pointer>); - -// wasm_valtype_vec_new_empty -typedef NativeWasmerValtypeVecNewEmptyFn = Void Function( - Pointer); -typedef WasmerValtypeVecNewEmptyFn = void Function(Pointer); - -// wasm_valtype_vec_new_uninitialized -typedef NativeWasmerValtypeVecNewUninitializedFn = Void Function( - Pointer, Uint64); -typedef WasmerValtypeVecNewUninitializedFn = void Function( - Pointer, int); - -// wasmer_last_error_length -typedef NativeWasmerWasmerLastErrorLengthFn = Int64 Function(); -typedef WasmerWasmerLastErrorLengthFn = int Function(); - -// wasmer_last_error_message -typedef NativeWasmerWasmerLastErrorMessageFn = Int64 Function( - Pointer, Int64); -typedef WasmerWasmerLastErrorMessageFn = int Function(Pointer, int); diff --git a/pkg/wasm/lib/wasm.dart b/pkg/wasm/lib/wasm.dart deleted file mode 100644 index b28020519ba..00000000000 --- a/pkg/wasm/lib/wasm.dart +++ /dev/null @@ -1,6 +0,0 @@ -// Copyright (c) 2020, 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. - -export 'src/module.dart'; -export 'src/wasm_error.dart'; diff --git a/pkg/wasm/pubspec.yaml b/pkg/wasm/pubspec.yaml deleted file mode 100644 index a91c321c12e..00000000000 --- a/pkg/wasm/pubspec.yaml +++ /dev/null @@ -1,14 +0,0 @@ -name: wasm -version: 0.1.0 -description: Utilities for loading and running WASM modules from Dart code -homepage: https://github.com/dart-lang/sdk/tree/master/pkg/wasm - -environment: - sdk: '>=2.12.0 <3.0.0' - -dependencies: - ffi: ^1.0.0 - -dev_dependencies: - lints: ^1.0.0 - test: ^1.16.0 diff --git a/pkg/wasm/test/basic_test.dart b/pkg/wasm/test/basic_test.dart deleted file mode 100644 index ad0a2dd72f0..00000000000 --- a/pkg/wasm/test/basic_test.dart +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2021, 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'; - -void main() { - test('basics', () { - // int64_t square(int64_t n) { return n * n; } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, // - 0x01, 0x7e, 0x01, 0x7e, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, - 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, - 0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, - 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x73, 0x71, 0x75, 0x61, 0x72, - 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x00, - 0x7e, 0x0b, - ]); - - var inst = WasmModule(data).builder().build(); - var fn = inst.lookupFunction('square'); - var n = fn(1234) as int; - - expect(n, 1234 * 1234); - - expect(inst.lookupFunction('not_a_function'), isNull); - }); -} diff --git a/pkg/wasm/test/corrupted_error_test.dart b/pkg/wasm/test/corrupted_error_test.dart deleted file mode 100644 index 4ff5d08066d..00000000000 --- a/pkg/wasm/test/corrupted_error_test.dart +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2021, 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 error thrown when the wasm module is corrupted. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -import 'test_shared.dart'; - -void main() { - test('corrupted module', () { - var data = Uint8List.fromList([ - 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, 0x01, 0x7e, 0x01, 0x7e, // - 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, - 0x06, 0x73, 0x71, 0x75, 0x61, 0x72, 0x65, 0x00, 0x00, 0x00, 0x20, 0x00, - 0x7e, 0x0b, - ]); - - expect( - () => WasmModule(data), - throwsWasmError( - allOf( - contains('Wasm module compile failed.'), - contains('Validation error: Bad magic number (at offset 0)'), - ), - ), - ); - }); -} diff --git a/pkg/wasm/test/fn_call_error_test.dart b/pkg/wasm/test/fn_call_error_test.dart deleted file mode 100644 index b1050570bdb..00000000000 --- a/pkg/wasm/test/fn_call_error_test.dart +++ /dev/null @@ -1,32 +0,0 @@ -// Copyright (c) 2021, 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. - -import 'dart:typed_data'; - -// Test error thrown when a function is called with the wrong args. - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('function with wrong arguments', () { - // int64_t square(int64_t n) { return n * n; } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x06, 0x01, 0x60, // - 0x01, 0x7e, 0x01, 0x7e, 0x03, 0x02, 0x01, 0x00, 0x04, 0x05, 0x01, 0x70, - 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, - 0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x07, 0x13, 0x02, 0x06, 0x6d, 0x65, - 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x06, 0x73, 0x71, 0x75, 0x61, 0x72, - 0x65, 0x00, 0x00, 0x0a, 0x09, 0x01, 0x07, 0x00, 0x20, 0x00, 0x20, 0x00, - 0x7e, 0x0b, - ]); - - var inst = WasmModule(data).builder().build(); - var fn = inst.lookupFunction('square'); - - expect(() => fn(), throwsA(isArgumentError)); - expect(() => fn(1, 2, 3), throwsA(isArgumentError)); - expect(() => fn(1.23), throwsA(isArgumentError)); - }); -} diff --git a/pkg/wasm/test/fn_import_error_test.dart b/pkg/wasm/test/fn_import_error_test.dart deleted file mode 100644 index c3ebd6fa50b..00000000000 --- a/pkg/wasm/test/fn_import_error_test.dart +++ /dev/null @@ -1,95 +0,0 @@ -// Copyright (c) 2021, 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. - -import 'dart:typed_data'; - -// Test errors thrown by function imports. - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -import 'test_shared.dart'; - -void main() { - test('bad function imports', () { - // This module expects a function import like: - // int64_t someFn(int32_t a, int64_t b, float c, double d); - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0c, 0x02, 0x60, // - 0x04, 0x7f, 0x7e, 0x7d, 0x7c, 0x01, 0x7e, 0x60, 0x00, 0x00, 0x02, 0x0e, - 0x01, 0x03, 0x65, 0x6e, 0x76, 0x06, 0x73, 0x6f, 0x6d, 0x65, 0x46, 0x6e, - 0x00, 0x00, 0x03, 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, - 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41, - 0x80, 0x88, 0x04, 0x0b, 0x07, 0x11, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, - 0x72, 0x79, 0x02, 0x00, 0x04, 0x62, 0x6c, 0x61, 0x68, 0x00, 0x01, 0x0a, - 0x1d, 0x01, 0x1b, 0x00, 0x41, 0x01, 0x42, 0x02, 0x43, 0x00, 0x00, 0x40, - 0x40, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x40, 0x10, 0x80, - 0x80, 0x80, 0x80, 0x00, 0x1a, 0x0b, - ]); - - var mod = WasmModule(data); - - // Valid instantiation. - (mod.builder() - ..addFunction( - 'env', - 'someFn', - (int a, int b, num c, double d) => 123, - )) - .build(); - - // Missing imports. - expect( - () => mod.builder().build(), - throwsWasmError(startsWith('Missing import')), - ); - - // Wrong kind of import. - expect( - () => mod.builder().addMemory('env', 'someFn', mod.createMemory(10)), - throwsWasmError(startsWith('Import is not a memory:')), - ); - - // Wrong namespace. - expect( - () => (mod.builder() - ..addFunction( - 'foo', - 'someFn', - (int a, int b, num c, double d) => 123, - )) - .build(), - throwsWasmError(startsWith('Import not found:')), - ); - - // Wrong name. - expect( - () => (mod.builder() - ..addFunction( - 'env', - 'otherFn', - (int a, int b, num c, double d) => 123, - )) - .build(), - throwsWasmError(startsWith('Import not found:')), - ); - - // Already filled. - expect( - () => (mod.builder() - ..addFunction( - 'env', - 'someFn', - (int a, int b, num c, double d) => 123, - ) - ..addFunction( - 'env', - 'someFn', - (int a, int b, num c, double d) => 456, - )) - .build(), - throwsWasmError(startsWith('Import already filled: env::someFn')), - ); - }); -} diff --git a/pkg/wasm/test/fn_import_exception_test.dart b/pkg/wasm/test/fn_import_exception_test.dart deleted file mode 100644 index 4f1e502fca0..00000000000 --- a/pkg/wasm/test/fn_import_exception_test.dart +++ /dev/null @@ -1,48 +0,0 @@ -// Copyright (c) 2021, 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 throwing exceptions from an imported function. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('exception thrown from imported function', () { - // void fn() { - // a(); - // b(); - // } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x04, 0x01, 0x60, // - 0x00, 0x00, 0x02, 0x11, 0x02, 0x03, 0x65, 0x6e, 0x76, 0x01, 0x61, 0x00, - 0x00, 0x03, 0x65, 0x6e, 0x76, 0x01, 0x62, 0x00, 0x00, 0x03, 0x02, 0x01, - 0x00, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, - 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41, 0x80, 0x88, 0x04, 0x0b, 0x07, - 0x0f, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x02, - 0x66, 0x6e, 0x00, 0x02, 0x0a, 0x10, 0x01, 0x0e, 0x00, 0x10, 0x80, 0x80, - 0x80, 0x80, 0x00, 0x10, 0x81, 0x80, 0x80, 0x80, 0x00, 0x0b, - ]); - - var thrownException = Exception('Hello exception!'); - var inst = (WasmModule(data).builder() - ..addFunction('env', 'a', () { - throw thrownException; - }) - ..addFunction('env', 'b', () { - fail('should not get here!'); - })) - .build(); - - var fn = inst.lookupFunction('fn'); - expect(() => fn(), throwsA(thrownException)); - - inst = (WasmModule(data).builder() - ..addFunction('env', 'a', expectAsync0(() => null)) - ..addFunction('env', 'b', expectAsync0(() => null))) - .build(); - fn = inst.lookupFunction('fn'); - fn(); - }); -} diff --git a/pkg/wasm/test/fn_import_test.dart b/pkg/wasm/test/fn_import_test.dart deleted file mode 100644 index 0c813f9873e..00000000000 --- a/pkg/wasm/test/fn_import_test.dart +++ /dev/null @@ -1,40 +0,0 @@ -// Copyright (c) 2021, 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'; - -void main() { - test('function import', () { - // void reportStuff() { report(123, 456); } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x09, 0x02, 0x60, // - 0x02, 0x7e, 0x7e, 0x00, 0x60, 0x00, 0x00, 0x02, 0x0e, 0x01, 0x03, 0x65, - 0x6e, 0x76, 0x06, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x00, 0x00, 0x03, - 0x02, 0x01, 0x01, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, - 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41, 0x80, 0x88, 0x04, - 0x0b, 0x07, 0x18, 0x02, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, - 0x00, 0x0b, 0x72, 0x65, 0x70, 0x6f, 0x72, 0x74, 0x53, 0x74, 0x75, 0x66, - 0x66, 0x00, 0x01, 0x0a, 0x10, 0x01, 0x0e, 0x00, 0x42, 0xfb, 0x00, 0x42, - 0xc8, 0x03, 0x10, 0x80, 0x80, 0x80, 0x80, 0x00, 0x0b, - ]); - - var reportX = -1; - var reportY = -1; - - var inst = (WasmModule(data).builder() - ..addFunction('env', 'report', (int x, int y) { - reportX = x; - reportY = y; - })) - .build(); - var fn = inst.lookupFunction('reportStuff'); - fn(); - expect(123, reportX); - expect(456, reportY); - }); -} diff --git a/pkg/wasm/test/hello_wasi_test.dart b/pkg/wasm/test/hello_wasi_test.dart deleted file mode 100644 index 5693bbbc85f..00000000000 --- a/pkg/wasm/test/hello_wasi_test.dart +++ /dev/null @@ -1,183 +0,0 @@ -// Copyright (c) 2021, 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. - -// Variant of hello_world_test that uses the default WASI imports. -import 'dart:convert'; -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('hello wasi', () async { - // Hello world module generated by emscripten+WASI. Exports a function like - // `void _start()`, and prints using `int fd_write(int, int, int, int)`. - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x33, 0x09, 0x60, // - 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, - 0x01, 0x7f, 0x60, 0x00, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, - 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7e, 0x7f, 0x01, 0x7e, 0x60, - 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x03, 0x7f, 0x7f, 0x7f, - 0x00, 0x02, 0x1a, 0x01, 0x0d, 0x77, 0x61, 0x73, 0x69, 0x5f, 0x75, 0x6e, - 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x08, 0x66, 0x64, 0x5f, 0x77, 0x72, - 0x69, 0x74, 0x65, 0x00, 0x01, 0x03, 0x0f, 0x0e, 0x03, 0x04, 0x00, 0x03, - 0x02, 0x07, 0x05, 0x04, 0x03, 0x06, 0x02, 0x02, 0x08, 0x00, 0x04, 0x05, - 0x01, 0x70, 0x01, 0x04, 0x04, 0x05, 0x06, 0x01, 0x01, 0x80, 0x02, 0x80, - 0x02, 0x06, 0x09, 0x01, 0x7f, 0x01, 0x41, 0xc0, 0x95, 0xc0, 0x02, 0x0b, - 0x07, 0x2e, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, - 0x11, 0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x63, 0x61, 0x6c, 0x6c, - 0x5f, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x00, 0x05, 0x04, 0x6d, 0x61, 0x69, - 0x6e, 0x00, 0x04, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x0b, - 0x09, 0x09, 0x01, 0x00, 0x41, 0x01, 0x0b, 0x03, 0x08, 0x0e, 0x07, 0x0a, - 0xae, 0x0c, 0x0e, 0xbf, 0x01, 0x01, 0x05, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x04, 0x02, 0x40, 0x20, 0x01, 0x28, 0x02, 0x10, 0x22, 0x02, 0x04, 0x7f, - 0x20, 0x02, 0x05, 0x20, 0x01, 0x10, 0x02, 0x0d, 0x01, 0x20, 0x01, 0x28, - 0x02, 0x10, 0x0b, 0x20, 0x01, 0x28, 0x02, 0x14, 0x22, 0x05, 0x6b, 0x20, - 0x00, 0x49, 0x04, 0x40, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, 0x00, 0x20, - 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x0f, 0x0b, 0x02, 0x40, 0x20, - 0x01, 0x2c, 0x00, 0x4b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x20, 0x00, 0x21, - 0x03, 0x03, 0x40, 0x20, 0x03, 0x22, 0x02, 0x45, 0x0d, 0x01, 0x20, 0x02, - 0x41, 0x7f, 0x6a, 0x22, 0x03, 0x41, 0x80, 0x08, 0x6a, 0x2d, 0x00, 0x00, - 0x41, 0x0a, 0x47, 0x0d, 0x00, 0x0b, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, - 0x02, 0x20, 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x22, 0x03, 0x20, - 0x02, 0x49, 0x0d, 0x01, 0x20, 0x00, 0x20, 0x02, 0x6b, 0x21, 0x00, 0x20, - 0x02, 0x41, 0x80, 0x08, 0x6a, 0x21, 0x04, 0x20, 0x01, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x02, 0x21, 0x06, 0x0b, 0x20, 0x05, 0x20, 0x04, 0x20, - 0x00, 0x10, 0x03, 0x1a, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x14, 0x20, - 0x00, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x06, 0x6a, 0x21, 0x03, - 0x0b, 0x20, 0x03, 0x0b, 0x59, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x20, 0x00, - 0x2d, 0x00, 0x4a, 0x22, 0x01, 0x41, 0x7f, 0x6a, 0x20, 0x01, 0x72, 0x3a, - 0x00, 0x4a, 0x20, 0x00, 0x28, 0x02, 0x00, 0x22, 0x01, 0x41, 0x08, 0x71, - 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x7f, 0x0f, 0x0b, 0x20, 0x00, 0x42, 0x00, 0x37, 0x02, 0x04, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, 0x1c, 0x20, - 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, 0x20, 0x00, - 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x41, 0x00, 0x0b, 0x82, 0x04, - 0x01, 0x03, 0x7f, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x4f, 0x04, 0x40, - 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x10, 0x0d, 0x20, 0x00, 0x0f, 0x0b, - 0x20, 0x00, 0x20, 0x02, 0x6a, 0x21, 0x03, 0x02, 0x40, 0x20, 0x00, 0x20, - 0x01, 0x73, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x02, 0x40, 0x20, 0x02, - 0x41, 0x01, 0x48, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, - 0x20, 0x00, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, 0x03, 0x40, 0x20, 0x02, 0x20, - 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, - 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, 0x20, 0x03, 0x4f, - 0x0d, 0x01, 0x20, 0x02, 0x41, 0x03, 0x71, 0x0d, 0x00, 0x0b, 0x0b, 0x02, - 0x40, 0x20, 0x03, 0x41, 0x7c, 0x71, 0x22, 0x04, 0x41, 0xc0, 0x00, 0x49, - 0x0d, 0x00, 0x20, 0x02, 0x20, 0x04, 0x41, 0x40, 0x6a, 0x22, 0x05, 0x4b, - 0x0d, 0x00, 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, - 0x02, 0x00, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x04, 0x36, 0x02, 0x04, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x08, 0x36, 0x02, 0x08, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x0c, 0x36, 0x02, 0x0c, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x10, 0x36, 0x02, 0x10, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x14, 0x36, 0x02, 0x14, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x18, 0x36, - 0x02, 0x18, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x1c, 0x36, 0x02, 0x1c, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x20, 0x36, 0x02, 0x20, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x24, 0x36, 0x02, 0x24, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x28, 0x36, 0x02, 0x28, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x2c, 0x36, 0x02, 0x2c, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x30, 0x36, - 0x02, 0x30, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x34, 0x36, 0x02, 0x34, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x38, 0x36, 0x02, 0x38, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x3c, 0x36, 0x02, 0x3c, 0x20, 0x01, 0x41, 0x40, - 0x6b, 0x21, 0x01, 0x20, 0x02, 0x41, 0x40, 0x6b, 0x22, 0x02, 0x20, 0x05, - 0x4d, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x04, 0x4f, 0x0d, 0x01, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, - 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, - 0x22, 0x02, 0x20, 0x04, 0x49, 0x0d, 0x00, 0x0b, 0x0c, 0x01, 0x0b, 0x20, - 0x03, 0x41, 0x04, 0x49, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, - 0x0b, 0x20, 0x03, 0x41, 0x7c, 0x6a, 0x22, 0x04, 0x20, 0x00, 0x49, 0x04, - 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x3a, 0x00, 0x01, 0x20, 0x02, - 0x20, 0x01, 0x2d, 0x00, 0x02, 0x3a, 0x00, 0x02, 0x20, 0x02, 0x20, 0x01, - 0x2d, 0x00, 0x03, 0x3a, 0x00, 0x03, 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, - 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, 0x22, 0x02, 0x20, 0x04, 0x4d, 0x0d, - 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x03, 0x49, 0x04, 0x40, 0x03, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, - 0x41, 0x01, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, - 0x20, 0x03, 0x47, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x00, 0x0b, 0x06, 0x00, - 0x10, 0x0c, 0x41, 0x00, 0x0b, 0x03, 0x00, 0x01, 0x0b, 0x7e, 0x01, 0x03, - 0x7f, 0x23, 0x00, 0x41, 0x10, 0x6b, 0x22, 0x01, 0x24, 0x00, 0x20, 0x01, - 0x41, 0x0a, 0x3a, 0x00, 0x0f, 0x02, 0x40, 0x20, 0x00, 0x28, 0x02, 0x10, - 0x22, 0x02, 0x45, 0x04, 0x40, 0x20, 0x00, 0x10, 0x02, 0x0d, 0x01, 0x20, - 0x00, 0x28, 0x02, 0x10, 0x21, 0x02, 0x0b, 0x02, 0x40, 0x20, 0x00, 0x28, - 0x02, 0x14, 0x22, 0x03, 0x20, 0x02, 0x4f, 0x0d, 0x00, 0x20, 0x00, 0x2c, - 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, 0x20, 0x03, 0x41, - 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x03, 0x41, 0x0a, 0x3a, 0x00, 0x00, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x20, 0x01, 0x41, 0x0f, 0x6a, 0x41, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x41, 0x01, 0x47, 0x0d, - 0x00, 0x20, 0x01, 0x2d, 0x00, 0x0f, 0x1a, 0x0b, 0x20, 0x01, 0x41, 0x10, - 0x6a, 0x24, 0x00, 0x0b, 0x04, 0x00, 0x42, 0x00, 0x0b, 0x04, 0x00, 0x41, - 0x00, 0x0b, 0x31, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x21, 0x02, 0x20, 0x02, - 0x02, 0x7f, 0x20, 0x01, 0x28, 0x02, 0x4c, 0x41, 0x7f, 0x4c, 0x04, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x10, 0x01, 0x0c, 0x01, 0x0b, 0x20, 0x02, 0x20, - 0x01, 0x10, 0x01, 0x0b, 0x22, 0x01, 0x46, 0x04, 0x40, 0x20, 0x00, 0x0f, - 0x0b, 0x20, 0x01, 0x0b, 0x62, 0x01, 0x03, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x00, 0x03, 0x40, 0x20, 0x00, 0x22, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x00, - 0x20, 0x01, 0x28, 0x02, 0x00, 0x22, 0x02, 0x41, 0x7f, 0x73, 0x20, 0x02, - 0x41, 0xff, 0xfd, 0xfb, 0x77, 0x6a, 0x71, 0x41, 0x80, 0x81, 0x82, 0x84, - 0x78, 0x71, 0x45, 0x0d, 0x00, 0x0b, 0x02, 0x40, 0x20, 0x02, 0x41, 0xff, - 0x01, 0x71, 0x45, 0x04, 0x40, 0x20, 0x01, 0x21, 0x00, 0x0c, 0x01, 0x0b, - 0x03, 0x40, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x21, 0x02, 0x20, 0x01, 0x41, - 0x01, 0x6a, 0x22, 0x00, 0x21, 0x01, 0x20, 0x02, 0x0d, 0x00, 0x0b, 0x0b, - 0x20, 0x00, 0x41, 0x80, 0x08, 0x6b, 0x0b, 0x0c, 0x00, 0x02, 0x7f, 0x41, - 0x00, 0x41, 0x00, 0x10, 0x04, 0x0b, 0x1a, 0x0b, 0x66, 0x01, 0x02, 0x7f, - 0x41, 0x90, 0x08, 0x28, 0x02, 0x00, 0x22, 0x00, 0x28, 0x02, 0x4c, 0x41, - 0x00, 0x4e, 0x04, 0x7f, 0x41, 0x01, 0x05, 0x20, 0x01, 0x0b, 0x1a, 0x02, - 0x40, 0x41, 0x7f, 0x41, 0x00, 0x10, 0x0a, 0x22, 0x01, 0x20, 0x01, 0x20, - 0x00, 0x10, 0x09, 0x47, 0x1b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x02, 0x40, - 0x20, 0x00, 0x2d, 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, - 0x28, 0x02, 0x14, 0x22, 0x01, 0x20, 0x00, 0x28, 0x02, 0x10, 0x4f, 0x0d, - 0x00, 0x20, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, - 0x01, 0x41, 0x0a, 0x3a, 0x00, 0x00, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x10, - 0x06, 0x0b, 0x0b, 0x3d, 0x01, 0x01, 0x7f, 0x20, 0x02, 0x04, 0x40, 0x03, - 0x40, 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x20, - 0x02, 0x41, 0x80, 0xc0, 0x00, 0x49, 0x1b, 0x22, 0x03, 0x10, 0x03, 0x21, - 0x00, 0x20, 0x01, 0x41, 0x80, 0x40, 0x6b, 0x21, 0x01, 0x20, 0x00, 0x41, - 0x80, 0x40, 0x6b, 0x21, 0x00, 0x20, 0x02, 0x20, 0x03, 0x6b, 0x22, 0x02, - 0x0d, 0x00, 0x0b, 0x0b, 0x0b, 0xb1, 0x02, 0x01, 0x06, 0x7f, 0x23, 0x00, - 0x41, 0x20, 0x6b, 0x22, 0x03, 0x24, 0x00, 0x20, 0x03, 0x20, 0x00, 0x28, - 0x02, 0x1c, 0x22, 0x04, 0x36, 0x02, 0x10, 0x20, 0x00, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x03, 0x20, 0x02, 0x36, 0x02, 0x1c, 0x20, 0x03, 0x20, - 0x01, 0x36, 0x02, 0x18, 0x20, 0x03, 0x20, 0x05, 0x20, 0x04, 0x6b, 0x22, - 0x01, 0x36, 0x02, 0x14, 0x20, 0x01, 0x20, 0x02, 0x6a, 0x21, 0x06, 0x41, - 0x02, 0x21, 0x05, 0x20, 0x03, 0x41, 0x10, 0x6a, 0x21, 0x01, 0x03, 0x40, - 0x02, 0x40, 0x02, 0x7f, 0x20, 0x06, 0x02, 0x7f, 0x20, 0x00, 0x28, 0x02, - 0x3c, 0x20, 0x01, 0x20, 0x05, 0x20, 0x03, 0x41, 0x0c, 0x6a, 0x10, 0x00, - 0x04, 0x40, 0x20, 0x03, 0x41, 0x7f, 0x36, 0x02, 0x0c, 0x41, 0x7f, 0x0c, - 0x01, 0x0b, 0x20, 0x03, 0x28, 0x02, 0x0c, 0x0b, 0x22, 0x04, 0x46, 0x04, - 0x40, 0x20, 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, - 0x1c, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x20, 0x02, 0x0c, - 0x01, 0x0b, 0x20, 0x04, 0x41, 0x7f, 0x4a, 0x0d, 0x01, 0x20, 0x00, 0x41, - 0x00, 0x36, 0x02, 0x1c, 0x20, 0x00, 0x42, 0x00, 0x37, 0x03, 0x10, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x00, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x00, 0x20, 0x05, 0x41, 0x02, 0x46, 0x0d, 0x00, 0x1a, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x04, 0x6b, 0x0b, 0x21, 0x04, 0x20, 0x03, 0x41, - 0x20, 0x6a, 0x24, 0x00, 0x20, 0x04, 0x0f, 0x0b, 0x20, 0x01, 0x41, 0x08, - 0x6a, 0x20, 0x01, 0x20, 0x04, 0x20, 0x01, 0x28, 0x02, 0x04, 0x22, 0x07, - 0x4b, 0x22, 0x08, 0x1b, 0x22, 0x01, 0x20, 0x04, 0x20, 0x07, 0x41, 0x00, - 0x20, 0x08, 0x1b, 0x6b, 0x22, 0x07, 0x20, 0x01, 0x28, 0x02, 0x00, 0x6a, - 0x36, 0x02, 0x00, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x04, 0x20, 0x07, - 0x6b, 0x36, 0x02, 0x04, 0x20, 0x06, 0x20, 0x04, 0x6b, 0x21, 0x06, 0x20, - 0x05, 0x20, 0x08, 0x6b, 0x21, 0x05, 0x0c, 0x00, 0x00, 0x0b, 0x00, 0x0b, - 0x0b, 0x4d, 0x06, 0x00, 0x41, 0x80, 0x08, 0x0b, 0x12, 0x68, 0x65, 0x6c, - 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00, 0x00, - 0x00, 0x18, 0x04, 0x00, 0x41, 0x98, 0x08, 0x0b, 0x01, 0x05, 0x00, 0x41, - 0xa4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xbc, 0x08, 0x0b, 0x0e, 0x02, - 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xb8, 0x04, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x41, 0xd4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xe3, 0x08, - 0x0b, 0x05, 0x0a, 0xff, 0xff, 0xff, 0xff, - ]); - - var inst = - (WasmModule(data).builder()..enableWasi(captureStdout: true)).build(); - - var fn = inst.lookupFunction('_start'); - fn(); - // TODO: failing on mac https://github.com/dart-lang/sdk/issues/46222 - var out = utf8.decode(await inst.stdout.first); - expect(out, 'hello, world!\n'); - }); -} diff --git a/pkg/wasm/test/hello_world_test.dart b/pkg/wasm/test/hello_world_test.dart deleted file mode 100644 index ef582d67c54..00000000000 --- a/pkg/wasm/test/hello_world_test.dart +++ /dev/null @@ -1,211 +0,0 @@ -// Copyright (c) 2021, 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. - -// @dart = 2.12 -// Test for hello world built using emscripten with WASI. - -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('hello world', () { - // Hello world module generated by emscripten+WASI. Exports a function like - // `void _start()`, and prints using `int fd_write(int, int, int, int)`. - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x33, 0x09, 0x60, // - 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, - 0x01, 0x7f, 0x60, 0x00, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, - 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7e, 0x7f, 0x01, 0x7e, 0x60, - 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x03, 0x7f, 0x7f, 0x7f, - 0x00, 0x02, 0x1a, 0x01, 0x0d, 0x77, 0x61, 0x73, 0x69, 0x5f, 0x75, 0x6e, - 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x08, 0x66, 0x64, 0x5f, 0x77, 0x72, - 0x69, 0x74, 0x65, 0x00, 0x01, 0x03, 0x0f, 0x0e, 0x03, 0x04, 0x00, 0x03, - 0x02, 0x07, 0x05, 0x04, 0x03, 0x06, 0x02, 0x02, 0x08, 0x00, 0x04, 0x05, - 0x01, 0x70, 0x01, 0x04, 0x04, 0x05, 0x06, 0x01, 0x01, 0x80, 0x02, 0x80, - 0x02, 0x06, 0x09, 0x01, 0x7f, 0x01, 0x41, 0xc0, 0x95, 0xc0, 0x02, 0x0b, - 0x07, 0x2e, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, - 0x11, 0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x63, 0x61, 0x6c, 0x6c, - 0x5f, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x00, 0x05, 0x04, 0x6d, 0x61, 0x69, - 0x6e, 0x00, 0x04, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x0b, - 0x09, 0x09, 0x01, 0x00, 0x41, 0x01, 0x0b, 0x03, 0x08, 0x0e, 0x07, 0x0a, - 0xae, 0x0c, 0x0e, 0xbf, 0x01, 0x01, 0x05, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x04, 0x02, 0x40, 0x20, 0x01, 0x28, 0x02, 0x10, 0x22, 0x02, 0x04, 0x7f, - 0x20, 0x02, 0x05, 0x20, 0x01, 0x10, 0x02, 0x0d, 0x01, 0x20, 0x01, 0x28, - 0x02, 0x10, 0x0b, 0x20, 0x01, 0x28, 0x02, 0x14, 0x22, 0x05, 0x6b, 0x20, - 0x00, 0x49, 0x04, 0x40, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, 0x00, 0x20, - 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x0f, 0x0b, 0x02, 0x40, 0x20, - 0x01, 0x2c, 0x00, 0x4b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x20, 0x00, 0x21, - 0x03, 0x03, 0x40, 0x20, 0x03, 0x22, 0x02, 0x45, 0x0d, 0x01, 0x20, 0x02, - 0x41, 0x7f, 0x6a, 0x22, 0x03, 0x41, 0x80, 0x08, 0x6a, 0x2d, 0x00, 0x00, - 0x41, 0x0a, 0x47, 0x0d, 0x00, 0x0b, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, - 0x02, 0x20, 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x22, 0x03, 0x20, - 0x02, 0x49, 0x0d, 0x01, 0x20, 0x00, 0x20, 0x02, 0x6b, 0x21, 0x00, 0x20, - 0x02, 0x41, 0x80, 0x08, 0x6a, 0x21, 0x04, 0x20, 0x01, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x02, 0x21, 0x06, 0x0b, 0x20, 0x05, 0x20, 0x04, 0x20, - 0x00, 0x10, 0x03, 0x1a, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x14, 0x20, - 0x00, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x06, 0x6a, 0x21, 0x03, - 0x0b, 0x20, 0x03, 0x0b, 0x59, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x20, 0x00, - 0x2d, 0x00, 0x4a, 0x22, 0x01, 0x41, 0x7f, 0x6a, 0x20, 0x01, 0x72, 0x3a, - 0x00, 0x4a, 0x20, 0x00, 0x28, 0x02, 0x00, 0x22, 0x01, 0x41, 0x08, 0x71, - 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x7f, 0x0f, 0x0b, 0x20, 0x00, 0x42, 0x00, 0x37, 0x02, 0x04, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, 0x1c, 0x20, - 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, 0x20, 0x00, - 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x41, 0x00, 0x0b, 0x82, 0x04, - 0x01, 0x03, 0x7f, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x4f, 0x04, 0x40, - 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x10, 0x0d, 0x20, 0x00, 0x0f, 0x0b, - 0x20, 0x00, 0x20, 0x02, 0x6a, 0x21, 0x03, 0x02, 0x40, 0x20, 0x00, 0x20, - 0x01, 0x73, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x02, 0x40, 0x20, 0x02, - 0x41, 0x01, 0x48, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, - 0x20, 0x00, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, 0x03, 0x40, 0x20, 0x02, 0x20, - 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, - 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, 0x20, 0x03, 0x4f, - 0x0d, 0x01, 0x20, 0x02, 0x41, 0x03, 0x71, 0x0d, 0x00, 0x0b, 0x0b, 0x02, - 0x40, 0x20, 0x03, 0x41, 0x7c, 0x71, 0x22, 0x04, 0x41, 0xc0, 0x00, 0x49, - 0x0d, 0x00, 0x20, 0x02, 0x20, 0x04, 0x41, 0x40, 0x6a, 0x22, 0x05, 0x4b, - 0x0d, 0x00, 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, - 0x02, 0x00, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x04, 0x36, 0x02, 0x04, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x08, 0x36, 0x02, 0x08, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x0c, 0x36, 0x02, 0x0c, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x10, 0x36, 0x02, 0x10, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x14, 0x36, 0x02, 0x14, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x18, 0x36, - 0x02, 0x18, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x1c, 0x36, 0x02, 0x1c, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x20, 0x36, 0x02, 0x20, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x24, 0x36, 0x02, 0x24, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x28, 0x36, 0x02, 0x28, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x2c, 0x36, 0x02, 0x2c, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x30, 0x36, - 0x02, 0x30, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x34, 0x36, 0x02, 0x34, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x38, 0x36, 0x02, 0x38, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x3c, 0x36, 0x02, 0x3c, 0x20, 0x01, 0x41, 0x40, - 0x6b, 0x21, 0x01, 0x20, 0x02, 0x41, 0x40, 0x6b, 0x22, 0x02, 0x20, 0x05, - 0x4d, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x04, 0x4f, 0x0d, 0x01, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, - 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, - 0x22, 0x02, 0x20, 0x04, 0x49, 0x0d, 0x00, 0x0b, 0x0c, 0x01, 0x0b, 0x20, - 0x03, 0x41, 0x04, 0x49, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, - 0x0b, 0x20, 0x03, 0x41, 0x7c, 0x6a, 0x22, 0x04, 0x20, 0x00, 0x49, 0x04, - 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x3a, 0x00, 0x01, 0x20, 0x02, - 0x20, 0x01, 0x2d, 0x00, 0x02, 0x3a, 0x00, 0x02, 0x20, 0x02, 0x20, 0x01, - 0x2d, 0x00, 0x03, 0x3a, 0x00, 0x03, 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, - 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, 0x22, 0x02, 0x20, 0x04, 0x4d, 0x0d, - 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x03, 0x49, 0x04, 0x40, 0x03, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, - 0x41, 0x01, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, - 0x20, 0x03, 0x47, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x00, 0x0b, 0x06, 0x00, - 0x10, 0x0c, 0x41, 0x00, 0x0b, 0x03, 0x00, 0x01, 0x0b, 0x7e, 0x01, 0x03, - 0x7f, 0x23, 0x00, 0x41, 0x10, 0x6b, 0x22, 0x01, 0x24, 0x00, 0x20, 0x01, - 0x41, 0x0a, 0x3a, 0x00, 0x0f, 0x02, 0x40, 0x20, 0x00, 0x28, 0x02, 0x10, - 0x22, 0x02, 0x45, 0x04, 0x40, 0x20, 0x00, 0x10, 0x02, 0x0d, 0x01, 0x20, - 0x00, 0x28, 0x02, 0x10, 0x21, 0x02, 0x0b, 0x02, 0x40, 0x20, 0x00, 0x28, - 0x02, 0x14, 0x22, 0x03, 0x20, 0x02, 0x4f, 0x0d, 0x00, 0x20, 0x00, 0x2c, - 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, 0x20, 0x03, 0x41, - 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x03, 0x41, 0x0a, 0x3a, 0x00, 0x00, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x20, 0x01, 0x41, 0x0f, 0x6a, 0x41, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x41, 0x01, 0x47, 0x0d, - 0x00, 0x20, 0x01, 0x2d, 0x00, 0x0f, 0x1a, 0x0b, 0x20, 0x01, 0x41, 0x10, - 0x6a, 0x24, 0x00, 0x0b, 0x04, 0x00, 0x42, 0x00, 0x0b, 0x04, 0x00, 0x41, - 0x00, 0x0b, 0x31, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x21, 0x02, 0x20, 0x02, - 0x02, 0x7f, 0x20, 0x01, 0x28, 0x02, 0x4c, 0x41, 0x7f, 0x4c, 0x04, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x10, 0x01, 0x0c, 0x01, 0x0b, 0x20, 0x02, 0x20, - 0x01, 0x10, 0x01, 0x0b, 0x22, 0x01, 0x46, 0x04, 0x40, 0x20, 0x00, 0x0f, - 0x0b, 0x20, 0x01, 0x0b, 0x62, 0x01, 0x03, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x00, 0x03, 0x40, 0x20, 0x00, 0x22, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x00, - 0x20, 0x01, 0x28, 0x02, 0x00, 0x22, 0x02, 0x41, 0x7f, 0x73, 0x20, 0x02, - 0x41, 0xff, 0xfd, 0xfb, 0x77, 0x6a, 0x71, 0x41, 0x80, 0x81, 0x82, 0x84, - 0x78, 0x71, 0x45, 0x0d, 0x00, 0x0b, 0x02, 0x40, 0x20, 0x02, 0x41, 0xff, - 0x01, 0x71, 0x45, 0x04, 0x40, 0x20, 0x01, 0x21, 0x00, 0x0c, 0x01, 0x0b, - 0x03, 0x40, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x21, 0x02, 0x20, 0x01, 0x41, - 0x01, 0x6a, 0x22, 0x00, 0x21, 0x01, 0x20, 0x02, 0x0d, 0x00, 0x0b, 0x0b, - 0x20, 0x00, 0x41, 0x80, 0x08, 0x6b, 0x0b, 0x0c, 0x00, 0x02, 0x7f, 0x41, - 0x00, 0x41, 0x00, 0x10, 0x04, 0x0b, 0x1a, 0x0b, 0x66, 0x01, 0x02, 0x7f, - 0x41, 0x90, 0x08, 0x28, 0x02, 0x00, 0x22, 0x00, 0x28, 0x02, 0x4c, 0x41, - 0x00, 0x4e, 0x04, 0x7f, 0x41, 0x01, 0x05, 0x20, 0x01, 0x0b, 0x1a, 0x02, - 0x40, 0x41, 0x7f, 0x41, 0x00, 0x10, 0x0a, 0x22, 0x01, 0x20, 0x01, 0x20, - 0x00, 0x10, 0x09, 0x47, 0x1b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x02, 0x40, - 0x20, 0x00, 0x2d, 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, - 0x28, 0x02, 0x14, 0x22, 0x01, 0x20, 0x00, 0x28, 0x02, 0x10, 0x4f, 0x0d, - 0x00, 0x20, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, - 0x01, 0x41, 0x0a, 0x3a, 0x00, 0x00, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x10, - 0x06, 0x0b, 0x0b, 0x3d, 0x01, 0x01, 0x7f, 0x20, 0x02, 0x04, 0x40, 0x03, - 0x40, 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x20, - 0x02, 0x41, 0x80, 0xc0, 0x00, 0x49, 0x1b, 0x22, 0x03, 0x10, 0x03, 0x21, - 0x00, 0x20, 0x01, 0x41, 0x80, 0x40, 0x6b, 0x21, 0x01, 0x20, 0x00, 0x41, - 0x80, 0x40, 0x6b, 0x21, 0x00, 0x20, 0x02, 0x20, 0x03, 0x6b, 0x22, 0x02, - 0x0d, 0x00, 0x0b, 0x0b, 0x0b, 0xb1, 0x02, 0x01, 0x06, 0x7f, 0x23, 0x00, - 0x41, 0x20, 0x6b, 0x22, 0x03, 0x24, 0x00, 0x20, 0x03, 0x20, 0x00, 0x28, - 0x02, 0x1c, 0x22, 0x04, 0x36, 0x02, 0x10, 0x20, 0x00, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x03, 0x20, 0x02, 0x36, 0x02, 0x1c, 0x20, 0x03, 0x20, - 0x01, 0x36, 0x02, 0x18, 0x20, 0x03, 0x20, 0x05, 0x20, 0x04, 0x6b, 0x22, - 0x01, 0x36, 0x02, 0x14, 0x20, 0x01, 0x20, 0x02, 0x6a, 0x21, 0x06, 0x41, - 0x02, 0x21, 0x05, 0x20, 0x03, 0x41, 0x10, 0x6a, 0x21, 0x01, 0x03, 0x40, - 0x02, 0x40, 0x02, 0x7f, 0x20, 0x06, 0x02, 0x7f, 0x20, 0x00, 0x28, 0x02, - 0x3c, 0x20, 0x01, 0x20, 0x05, 0x20, 0x03, 0x41, 0x0c, 0x6a, 0x10, 0x00, - 0x04, 0x40, 0x20, 0x03, 0x41, 0x7f, 0x36, 0x02, 0x0c, 0x41, 0x7f, 0x0c, - 0x01, 0x0b, 0x20, 0x03, 0x28, 0x02, 0x0c, 0x0b, 0x22, 0x04, 0x46, 0x04, - 0x40, 0x20, 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, - 0x1c, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x20, 0x02, 0x0c, - 0x01, 0x0b, 0x20, 0x04, 0x41, 0x7f, 0x4a, 0x0d, 0x01, 0x20, 0x00, 0x41, - 0x00, 0x36, 0x02, 0x1c, 0x20, 0x00, 0x42, 0x00, 0x37, 0x03, 0x10, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x00, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x00, 0x20, 0x05, 0x41, 0x02, 0x46, 0x0d, 0x00, 0x1a, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x04, 0x6b, 0x0b, 0x21, 0x04, 0x20, 0x03, 0x41, - 0x20, 0x6a, 0x24, 0x00, 0x20, 0x04, 0x0f, 0x0b, 0x20, 0x01, 0x41, 0x08, - 0x6a, 0x20, 0x01, 0x20, 0x04, 0x20, 0x01, 0x28, 0x02, 0x04, 0x22, 0x07, - 0x4b, 0x22, 0x08, 0x1b, 0x22, 0x01, 0x20, 0x04, 0x20, 0x07, 0x41, 0x00, - 0x20, 0x08, 0x1b, 0x6b, 0x22, 0x07, 0x20, 0x01, 0x28, 0x02, 0x00, 0x6a, - 0x36, 0x02, 0x00, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x04, 0x20, 0x07, - 0x6b, 0x36, 0x02, 0x04, 0x20, 0x06, 0x20, 0x04, 0x6b, 0x21, 0x06, 0x20, - 0x05, 0x20, 0x08, 0x6b, 0x21, 0x05, 0x0c, 0x00, 0x00, 0x0b, 0x00, 0x0b, - 0x0b, 0x4d, 0x06, 0x00, 0x41, 0x80, 0x08, 0x0b, 0x12, 0x68, 0x65, 0x6c, - 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00, 0x00, - 0x00, 0x18, 0x04, 0x00, 0x41, 0x98, 0x08, 0x0b, 0x01, 0x05, 0x00, 0x41, - 0xa4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xbc, 0x08, 0x0b, 0x0e, 0x02, - 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xb8, 0x04, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x41, 0xd4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xe3, 0x08, - 0x0b, 0x05, 0x0a, 0xff, 0xff, 0xff, 0xff, - ]); - - late WasmMemory mem; - var out = StringBuffer(); - int getI32(int p) { - // Read a little-endian I32. - var n = 0; - for (var i = p + 3; i >= p; --i) { - n *= 256; - n += mem[i]; - } - return n; - } - - var builder = WasmModule(data).builder() - ..addFunction('wasi_unstable', 'fd_write', - (int fd, int iovs, int iovsLen, int unused) { - // iovs points to an array of length iovs_len. Each element is two I32s, - // a char* and a length. - var o = StringBuffer(); - for (var i = 0; i < iovsLen; ++i) { - var str = getI32(iovs + 8 * i); - var len = getI32(iovs + 4 + 8 * i); - for (var j = 0; j < len; ++j) { - o.write(String.fromCharCode(mem[str + j])); - } - } - out.write(o.toString()); - return o.length; - }); - - var inst = builder.build(); - mem = inst.memory; - - var fn = inst.lookupFunction('_start'); - fn(); - expect(out.toString(), 'hello, world!\n'); - }); -} diff --git a/pkg/wasm/test/memory_error_test.dart b/pkg/wasm/test/memory_error_test.dart deleted file mode 100644 index ec7033c59fd..00000000000 --- a/pkg/wasm/test/memory_error_test.dart +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021, 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 errors thrown by WasmMemory. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -import 'test_shared.dart'; - -void main() { - test('memory errors', () { - // Empty wasm module. - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x06, 0x81, 0x00, 0x00, // - ]); - var module = WasmModule(data); - - expect( - () => module.createMemory(1000000000), - throwsWasmError(startsWith('Failed to create memory.')), - ); - var mem = module.createMemory(100); - expect( - () => mem.grow(1000000000), - throwsWasmError('Failed to grow memory.'), - ); - mem = module.createMemory(100, 200); - expect( - () => mem.grow(300), - throwsWasmError('Failed to grow memory.'), - ); - }); -} diff --git a/pkg/wasm/test/memory_test.dart b/pkg/wasm/test/memory_test.dart deleted file mode 100644 index 483c2252f97..00000000000 --- a/pkg/wasm/test/memory_test.dart +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) 2021, 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 create a WasmMemory, edit it, and grow it. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('memory', () { - // Empty wasm module. - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x06, 0x81, 0x00, 0x00, // - ]); - var module = WasmModule(data); - - var mem = module.createMemory(100); - expect(mem.lengthInPages, 100); - expect(mem.lengthInBytes, 100 * WasmMemory.kPageSizeInBytes); - - mem[123] = 45; - expect(mem[123], 45); - - mem.grow(10); - expect(mem.lengthInPages, 110); - expect(mem.lengthInBytes, 110 * WasmMemory.kPageSizeInBytes); - expect(mem[123], 45); - }); -} diff --git a/pkg/wasm/test/numerics_test.dart b/pkg/wasm/test/numerics_test.dart deleted file mode 100644 index bec8f2ad754..00000000000 --- a/pkg/wasm/test/numerics_test.dart +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright (c) 2021, 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 numeric types. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('numerics', () { - // int64_t addI64(int64_t x, int64_t y) { return x + y; } - // int32_t addI32(int32_t x, int32_t y) { return x + y; } - // double addF64(double x, double y) { return x + y; } - // float addF32(float x, float y) { return x + y; } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x19, 0x04, 0x60, // - 0x02, 0x7e, 0x7e, 0x01, 0x7e, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, - 0x02, 0x7c, 0x7c, 0x01, 0x7c, 0x60, 0x02, 0x7d, 0x7d, 0x01, 0x7d, 0x03, - 0x05, 0x04, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, - 0x01, 0x05, 0x03, 0x01, 0x00, 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41, - 0x80, 0x88, 0x04, 0x0b, 0x07, 0x2e, 0x05, 0x06, 0x6d, 0x65, 0x6d, 0x6f, - 0x72, 0x79, 0x02, 0x00, 0x06, 0x61, 0x64, 0x64, 0x49, 0x36, 0x34, 0x00, - 0x00, 0x06, 0x61, 0x64, 0x64, 0x49, 0x33, 0x32, 0x00, 0x01, 0x06, 0x61, - 0x64, 0x64, 0x46, 0x36, 0x34, 0x00, 0x02, 0x06, 0x61, 0x64, 0x64, 0x46, - 0x33, 0x32, 0x00, 0x03, 0x0a, 0x21, 0x04, 0x07, 0x00, 0x20, 0x01, 0x20, - 0x00, 0x7c, 0x0b, 0x07, 0x00, 0x20, 0x01, 0x20, 0x00, 0x6a, 0x0b, 0x07, - 0x00, 0x20, 0x00, 0x20, 0x01, 0xa0, 0x0b, 0x07, 0x00, 0x20, 0x00, 0x20, - 0x01, 0x92, 0x0b, - ]); - - var inst = WasmModule(data).builder().build(); - var addI64 = inst.lookupFunction('addI64'); - var addI32 = inst.lookupFunction('addI32'); - var addF64 = inst.lookupFunction('addF64'); - var addF32 = inst.lookupFunction('addF32'); - - var i64 = addI64(0x123456789ABCDEF, 0xFEDCBA987654321) as int; - expect(i64, 0x1111111111111110); - - var i32 = addI32(0xABCDEF, 0xFEDCBA) as int; - expect(i32, 0x1aaaaa9); - - var f64 = addF64(1234.5678, 8765.4321) as double; - expect(f64, closeTo(9999.9999, 1e-6)); - - var f32 = addF32(1234.5678, 8765.4321) as double; - expect(f32, closeTo(9999.9999, 1e-3)); - }); -} diff --git a/pkg/wasm/test/test_shared.dart b/pkg/wasm/test/test_shared.dart deleted file mode 100644 index 33434a16c50..00000000000 --- a/pkg/wasm/test/test_shared.dart +++ /dev/null @@ -1,10 +0,0 @@ -// Copyright (c) 2021, 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. - -import 'package:test/test.dart'; -import 'package:wasm/src/wasm_error.dart'; - -Matcher throwsWasmError(Object messageMatcher) => throwsA( - isA().having((p0) => p0.message, 'message', messageMatcher), - ); diff --git a/pkg/wasm/test/void_test.dart b/pkg/wasm/test/void_test.dart deleted file mode 100644 index 670e459f57d..00000000000 --- a/pkg/wasm/test/void_test.dart +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2021, 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 functions with void return type, and functions that take no args. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -void main() { - test('void return type', () { - // int64_t x = 0; - // void set(int64_t a, int64_t b) { x = a + b; } - // int64_t get() { return x; } - var data = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x0a, 0x02, 0x60, // - 0x02, 0x7e, 0x7e, 0x00, 0x60, 0x00, 0x01, 0x7e, 0x03, 0x03, 0x02, 0x00, - 0x01, 0x04, 0x05, 0x01, 0x70, 0x01, 0x01, 0x01, 0x05, 0x03, 0x01, 0x00, - 0x02, 0x06, 0x08, 0x01, 0x7f, 0x01, 0x41, 0x90, 0x88, 0x04, 0x0b, 0x07, - 0x16, 0x03, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, 0x03, - 0x73, 0x65, 0x74, 0x00, 0x00, 0x03, 0x67, 0x65, 0x74, 0x00, 0x01, 0x0a, - 0x1e, 0x02, 0x10, 0x00, 0x41, 0x00, 0x20, 0x01, 0x20, 0x00, 0x7c, 0x37, - 0x03, 0x80, 0x88, 0x80, 0x80, 0x00, 0x0b, 0x0b, 0x00, 0x41, 0x00, 0x29, - 0x03, 0x80, 0x88, 0x80, 0x80, 0x00, 0x0b, 0x0b, 0x0f, 0x01, 0x00, 0x41, - 0x80, 0x08, 0x0b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - ]); - - var inst = WasmModule(data).builder().build(); - var setFn = inst.lookupFunction('set'); - var getFn = inst.lookupFunction('get'); - expect(setFn(123, 456), isNull); - var n = getFn() as int; - expect(n, 123 + 456); - }); -} diff --git a/pkg/wasm/test/wasi_error_test.dart b/pkg/wasm/test/wasi_error_test.dart deleted file mode 100644 index 986b5745afc..00000000000 --- a/pkg/wasm/test/wasi_error_test.dart +++ /dev/null @@ -1,216 +0,0 @@ -// Copyright (c) 2021, 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 the errors that can be thrown by WASI. -import 'dart:typed_data'; - -import 'package:test/test.dart'; -import 'package:wasm/wasm.dart'; - -import 'test_shared.dart'; - -void main() { - test('wasi error', () { - // Empty wasm module. - var emptyModuleData = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x06, 0x81, 0x00, 0x00, // - ]); - - // Failed to fill WASI imports (the empty module was not built with WASI). - expect( - () => WasmModule(emptyModuleData).builder().enableWasi(), - throwsWasmError(startsWith('Failed to fill WASI imports.')), - ); - - // Hello world module generated by emscripten+WASI. Exports a function like - // `void _start()`, and prints using `int fd_write(int, int, int, int)`. - var helloWorldData = Uint8List.fromList([ - 0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00, 0x01, 0x33, 0x09, 0x60, // - 0x03, 0x7f, 0x7f, 0x7f, 0x01, 0x7f, 0x60, 0x04, 0x7f, 0x7f, 0x7f, 0x7f, - 0x01, 0x7f, 0x60, 0x00, 0x00, 0x60, 0x02, 0x7f, 0x7f, 0x01, 0x7f, 0x60, - 0x01, 0x7f, 0x01, 0x7f, 0x60, 0x03, 0x7f, 0x7e, 0x7f, 0x01, 0x7e, 0x60, - 0x00, 0x01, 0x7f, 0x60, 0x01, 0x7f, 0x00, 0x60, 0x03, 0x7f, 0x7f, 0x7f, - 0x00, 0x02, 0x1a, 0x01, 0x0d, 0x77, 0x61, 0x73, 0x69, 0x5f, 0x75, 0x6e, - 0x73, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x08, 0x66, 0x64, 0x5f, 0x77, 0x72, - 0x69, 0x74, 0x65, 0x00, 0x01, 0x03, 0x0f, 0x0e, 0x03, 0x04, 0x00, 0x03, - 0x02, 0x07, 0x05, 0x04, 0x03, 0x06, 0x02, 0x02, 0x08, 0x00, 0x04, 0x05, - 0x01, 0x70, 0x01, 0x04, 0x04, 0x05, 0x06, 0x01, 0x01, 0x80, 0x02, 0x80, - 0x02, 0x06, 0x09, 0x01, 0x7f, 0x01, 0x41, 0xc0, 0x95, 0xc0, 0x02, 0x0b, - 0x07, 0x2e, 0x04, 0x06, 0x6d, 0x65, 0x6d, 0x6f, 0x72, 0x79, 0x02, 0x00, - 0x11, 0x5f, 0x5f, 0x77, 0x61, 0x73, 0x6d, 0x5f, 0x63, 0x61, 0x6c, 0x6c, - 0x5f, 0x63, 0x74, 0x6f, 0x72, 0x73, 0x00, 0x05, 0x04, 0x6d, 0x61, 0x69, - 0x6e, 0x00, 0x04, 0x06, 0x5f, 0x73, 0x74, 0x61, 0x72, 0x74, 0x00, 0x0b, - 0x09, 0x09, 0x01, 0x00, 0x41, 0x01, 0x0b, 0x03, 0x08, 0x0e, 0x07, 0x0a, - 0xae, 0x0c, 0x0e, 0xbf, 0x01, 0x01, 0x05, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x04, 0x02, 0x40, 0x20, 0x01, 0x28, 0x02, 0x10, 0x22, 0x02, 0x04, 0x7f, - 0x20, 0x02, 0x05, 0x20, 0x01, 0x10, 0x02, 0x0d, 0x01, 0x20, 0x01, 0x28, - 0x02, 0x10, 0x0b, 0x20, 0x01, 0x28, 0x02, 0x14, 0x22, 0x05, 0x6b, 0x20, - 0x00, 0x49, 0x04, 0x40, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, 0x00, 0x20, - 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x0f, 0x0b, 0x02, 0x40, 0x20, - 0x01, 0x2c, 0x00, 0x4b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x20, 0x00, 0x21, - 0x03, 0x03, 0x40, 0x20, 0x03, 0x22, 0x02, 0x45, 0x0d, 0x01, 0x20, 0x02, - 0x41, 0x7f, 0x6a, 0x22, 0x03, 0x41, 0x80, 0x08, 0x6a, 0x2d, 0x00, 0x00, - 0x41, 0x0a, 0x47, 0x0d, 0x00, 0x0b, 0x20, 0x01, 0x41, 0x80, 0x08, 0x20, - 0x02, 0x20, 0x01, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x22, 0x03, 0x20, - 0x02, 0x49, 0x0d, 0x01, 0x20, 0x00, 0x20, 0x02, 0x6b, 0x21, 0x00, 0x20, - 0x02, 0x41, 0x80, 0x08, 0x6a, 0x21, 0x04, 0x20, 0x01, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x02, 0x21, 0x06, 0x0b, 0x20, 0x05, 0x20, 0x04, 0x20, - 0x00, 0x10, 0x03, 0x1a, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x14, 0x20, - 0x00, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x06, 0x6a, 0x21, 0x03, - 0x0b, 0x20, 0x03, 0x0b, 0x59, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x20, 0x00, - 0x2d, 0x00, 0x4a, 0x22, 0x01, 0x41, 0x7f, 0x6a, 0x20, 0x01, 0x72, 0x3a, - 0x00, 0x4a, 0x20, 0x00, 0x28, 0x02, 0x00, 0x22, 0x01, 0x41, 0x08, 0x71, - 0x04, 0x40, 0x20, 0x00, 0x20, 0x01, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x7f, 0x0f, 0x0b, 0x20, 0x00, 0x42, 0x00, 0x37, 0x02, 0x04, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, 0x1c, 0x20, - 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, 0x20, 0x00, - 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x41, 0x00, 0x0b, 0x82, 0x04, - 0x01, 0x03, 0x7f, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x4f, 0x04, 0x40, - 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x10, 0x0d, 0x20, 0x00, 0x0f, 0x0b, - 0x20, 0x00, 0x20, 0x02, 0x6a, 0x21, 0x03, 0x02, 0x40, 0x20, 0x00, 0x20, - 0x01, 0x73, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x02, 0x40, 0x20, 0x02, - 0x41, 0x01, 0x48, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, - 0x20, 0x00, 0x41, 0x03, 0x71, 0x45, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, 0x03, 0x40, 0x20, 0x02, 0x20, - 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, - 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, 0x20, 0x03, 0x4f, - 0x0d, 0x01, 0x20, 0x02, 0x41, 0x03, 0x71, 0x0d, 0x00, 0x0b, 0x0b, 0x02, - 0x40, 0x20, 0x03, 0x41, 0x7c, 0x71, 0x22, 0x04, 0x41, 0xc0, 0x00, 0x49, - 0x0d, 0x00, 0x20, 0x02, 0x20, 0x04, 0x41, 0x40, 0x6a, 0x22, 0x05, 0x4b, - 0x0d, 0x00, 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, - 0x02, 0x00, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x04, 0x36, 0x02, 0x04, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x08, 0x36, 0x02, 0x08, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x0c, 0x36, 0x02, 0x0c, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x10, 0x36, 0x02, 0x10, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x14, 0x36, 0x02, 0x14, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x18, 0x36, - 0x02, 0x18, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x1c, 0x36, 0x02, 0x1c, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x20, 0x36, 0x02, 0x20, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x24, 0x36, 0x02, 0x24, 0x20, 0x02, 0x20, 0x01, - 0x28, 0x02, 0x28, 0x36, 0x02, 0x28, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, - 0x2c, 0x36, 0x02, 0x2c, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x30, 0x36, - 0x02, 0x30, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x34, 0x36, 0x02, 0x34, - 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x38, 0x36, 0x02, 0x38, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x3c, 0x36, 0x02, 0x3c, 0x20, 0x01, 0x41, 0x40, - 0x6b, 0x21, 0x01, 0x20, 0x02, 0x41, 0x40, 0x6b, 0x22, 0x02, 0x20, 0x05, - 0x4d, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x04, 0x4f, 0x0d, 0x01, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x28, 0x02, 0x00, 0x36, 0x02, 0x00, - 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, - 0x22, 0x02, 0x20, 0x04, 0x49, 0x0d, 0x00, 0x0b, 0x0c, 0x01, 0x0b, 0x20, - 0x03, 0x41, 0x04, 0x49, 0x04, 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, - 0x0b, 0x20, 0x03, 0x41, 0x7c, 0x6a, 0x22, 0x04, 0x20, 0x00, 0x49, 0x04, - 0x40, 0x20, 0x00, 0x21, 0x02, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x21, 0x02, - 0x03, 0x40, 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x3a, 0x00, 0x01, 0x20, 0x02, - 0x20, 0x01, 0x2d, 0x00, 0x02, 0x3a, 0x00, 0x02, 0x20, 0x02, 0x20, 0x01, - 0x2d, 0x00, 0x03, 0x3a, 0x00, 0x03, 0x20, 0x01, 0x41, 0x04, 0x6a, 0x21, - 0x01, 0x20, 0x02, 0x41, 0x04, 0x6a, 0x22, 0x02, 0x20, 0x04, 0x4d, 0x0d, - 0x00, 0x0b, 0x0b, 0x20, 0x02, 0x20, 0x03, 0x49, 0x04, 0x40, 0x03, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x2d, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x20, 0x01, - 0x41, 0x01, 0x6a, 0x21, 0x01, 0x20, 0x02, 0x41, 0x01, 0x6a, 0x22, 0x02, - 0x20, 0x03, 0x47, 0x0d, 0x00, 0x0b, 0x0b, 0x20, 0x00, 0x0b, 0x06, 0x00, - 0x10, 0x0c, 0x41, 0x00, 0x0b, 0x03, 0x00, 0x01, 0x0b, 0x7e, 0x01, 0x03, - 0x7f, 0x23, 0x00, 0x41, 0x10, 0x6b, 0x22, 0x01, 0x24, 0x00, 0x20, 0x01, - 0x41, 0x0a, 0x3a, 0x00, 0x0f, 0x02, 0x40, 0x20, 0x00, 0x28, 0x02, 0x10, - 0x22, 0x02, 0x45, 0x04, 0x40, 0x20, 0x00, 0x10, 0x02, 0x0d, 0x01, 0x20, - 0x00, 0x28, 0x02, 0x10, 0x21, 0x02, 0x0b, 0x02, 0x40, 0x20, 0x00, 0x28, - 0x02, 0x14, 0x22, 0x03, 0x20, 0x02, 0x4f, 0x0d, 0x00, 0x20, 0x00, 0x2c, - 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, 0x20, 0x03, 0x41, - 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, 0x03, 0x41, 0x0a, 0x3a, 0x00, 0x00, - 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x20, 0x01, 0x41, 0x0f, 0x6a, 0x41, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x24, 0x11, 0x00, 0x00, 0x41, 0x01, 0x47, 0x0d, - 0x00, 0x20, 0x01, 0x2d, 0x00, 0x0f, 0x1a, 0x0b, 0x20, 0x01, 0x41, 0x10, - 0x6a, 0x24, 0x00, 0x0b, 0x04, 0x00, 0x42, 0x00, 0x0b, 0x04, 0x00, 0x41, - 0x00, 0x0b, 0x31, 0x01, 0x01, 0x7f, 0x20, 0x00, 0x21, 0x02, 0x20, 0x02, - 0x02, 0x7f, 0x20, 0x01, 0x28, 0x02, 0x4c, 0x41, 0x7f, 0x4c, 0x04, 0x40, - 0x20, 0x02, 0x20, 0x01, 0x10, 0x01, 0x0c, 0x01, 0x0b, 0x20, 0x02, 0x20, - 0x01, 0x10, 0x01, 0x0b, 0x22, 0x01, 0x46, 0x04, 0x40, 0x20, 0x00, 0x0f, - 0x0b, 0x20, 0x01, 0x0b, 0x62, 0x01, 0x03, 0x7f, 0x41, 0x80, 0x08, 0x21, - 0x00, 0x03, 0x40, 0x20, 0x00, 0x22, 0x01, 0x41, 0x04, 0x6a, 0x21, 0x00, - 0x20, 0x01, 0x28, 0x02, 0x00, 0x22, 0x02, 0x41, 0x7f, 0x73, 0x20, 0x02, - 0x41, 0xff, 0xfd, 0xfb, 0x77, 0x6a, 0x71, 0x41, 0x80, 0x81, 0x82, 0x84, - 0x78, 0x71, 0x45, 0x0d, 0x00, 0x0b, 0x02, 0x40, 0x20, 0x02, 0x41, 0xff, - 0x01, 0x71, 0x45, 0x04, 0x40, 0x20, 0x01, 0x21, 0x00, 0x0c, 0x01, 0x0b, - 0x03, 0x40, 0x20, 0x01, 0x2d, 0x00, 0x01, 0x21, 0x02, 0x20, 0x01, 0x41, - 0x01, 0x6a, 0x22, 0x00, 0x21, 0x01, 0x20, 0x02, 0x0d, 0x00, 0x0b, 0x0b, - 0x20, 0x00, 0x41, 0x80, 0x08, 0x6b, 0x0b, 0x0c, 0x00, 0x02, 0x7f, 0x41, - 0x00, 0x41, 0x00, 0x10, 0x04, 0x0b, 0x1a, 0x0b, 0x66, 0x01, 0x02, 0x7f, - 0x41, 0x90, 0x08, 0x28, 0x02, 0x00, 0x22, 0x00, 0x28, 0x02, 0x4c, 0x41, - 0x00, 0x4e, 0x04, 0x7f, 0x41, 0x01, 0x05, 0x20, 0x01, 0x0b, 0x1a, 0x02, - 0x40, 0x41, 0x7f, 0x41, 0x00, 0x10, 0x0a, 0x22, 0x01, 0x20, 0x01, 0x20, - 0x00, 0x10, 0x09, 0x47, 0x1b, 0x41, 0x00, 0x48, 0x0d, 0x00, 0x02, 0x40, - 0x20, 0x00, 0x2d, 0x00, 0x4b, 0x41, 0x0a, 0x46, 0x0d, 0x00, 0x20, 0x00, - 0x28, 0x02, 0x14, 0x22, 0x01, 0x20, 0x00, 0x28, 0x02, 0x10, 0x4f, 0x0d, - 0x00, 0x20, 0x00, 0x20, 0x01, 0x41, 0x01, 0x6a, 0x36, 0x02, 0x14, 0x20, - 0x01, 0x41, 0x0a, 0x3a, 0x00, 0x00, 0x0c, 0x01, 0x0b, 0x20, 0x00, 0x10, - 0x06, 0x0b, 0x0b, 0x3d, 0x01, 0x01, 0x7f, 0x20, 0x02, 0x04, 0x40, 0x03, - 0x40, 0x20, 0x00, 0x20, 0x01, 0x20, 0x02, 0x41, 0x80, 0xc0, 0x00, 0x20, - 0x02, 0x41, 0x80, 0xc0, 0x00, 0x49, 0x1b, 0x22, 0x03, 0x10, 0x03, 0x21, - 0x00, 0x20, 0x01, 0x41, 0x80, 0x40, 0x6b, 0x21, 0x01, 0x20, 0x00, 0x41, - 0x80, 0x40, 0x6b, 0x21, 0x00, 0x20, 0x02, 0x20, 0x03, 0x6b, 0x22, 0x02, - 0x0d, 0x00, 0x0b, 0x0b, 0x0b, 0xb1, 0x02, 0x01, 0x06, 0x7f, 0x23, 0x00, - 0x41, 0x20, 0x6b, 0x22, 0x03, 0x24, 0x00, 0x20, 0x03, 0x20, 0x00, 0x28, - 0x02, 0x1c, 0x22, 0x04, 0x36, 0x02, 0x10, 0x20, 0x00, 0x28, 0x02, 0x14, - 0x21, 0x05, 0x20, 0x03, 0x20, 0x02, 0x36, 0x02, 0x1c, 0x20, 0x03, 0x20, - 0x01, 0x36, 0x02, 0x18, 0x20, 0x03, 0x20, 0x05, 0x20, 0x04, 0x6b, 0x22, - 0x01, 0x36, 0x02, 0x14, 0x20, 0x01, 0x20, 0x02, 0x6a, 0x21, 0x06, 0x41, - 0x02, 0x21, 0x05, 0x20, 0x03, 0x41, 0x10, 0x6a, 0x21, 0x01, 0x03, 0x40, - 0x02, 0x40, 0x02, 0x7f, 0x20, 0x06, 0x02, 0x7f, 0x20, 0x00, 0x28, 0x02, - 0x3c, 0x20, 0x01, 0x20, 0x05, 0x20, 0x03, 0x41, 0x0c, 0x6a, 0x10, 0x00, - 0x04, 0x40, 0x20, 0x03, 0x41, 0x7f, 0x36, 0x02, 0x0c, 0x41, 0x7f, 0x0c, - 0x01, 0x0b, 0x20, 0x03, 0x28, 0x02, 0x0c, 0x0b, 0x22, 0x04, 0x46, 0x04, - 0x40, 0x20, 0x00, 0x20, 0x00, 0x28, 0x02, 0x2c, 0x22, 0x01, 0x36, 0x02, - 0x1c, 0x20, 0x00, 0x20, 0x01, 0x36, 0x02, 0x14, 0x20, 0x00, 0x20, 0x01, - 0x20, 0x00, 0x28, 0x02, 0x30, 0x6a, 0x36, 0x02, 0x10, 0x20, 0x02, 0x0c, - 0x01, 0x0b, 0x20, 0x04, 0x41, 0x7f, 0x4a, 0x0d, 0x01, 0x20, 0x00, 0x41, - 0x00, 0x36, 0x02, 0x1c, 0x20, 0x00, 0x42, 0x00, 0x37, 0x03, 0x10, 0x20, - 0x00, 0x20, 0x00, 0x28, 0x02, 0x00, 0x41, 0x20, 0x72, 0x36, 0x02, 0x00, - 0x41, 0x00, 0x20, 0x05, 0x41, 0x02, 0x46, 0x0d, 0x00, 0x1a, 0x20, 0x02, - 0x20, 0x01, 0x28, 0x02, 0x04, 0x6b, 0x0b, 0x21, 0x04, 0x20, 0x03, 0x41, - 0x20, 0x6a, 0x24, 0x00, 0x20, 0x04, 0x0f, 0x0b, 0x20, 0x01, 0x41, 0x08, - 0x6a, 0x20, 0x01, 0x20, 0x04, 0x20, 0x01, 0x28, 0x02, 0x04, 0x22, 0x07, - 0x4b, 0x22, 0x08, 0x1b, 0x22, 0x01, 0x20, 0x04, 0x20, 0x07, 0x41, 0x00, - 0x20, 0x08, 0x1b, 0x6b, 0x22, 0x07, 0x20, 0x01, 0x28, 0x02, 0x00, 0x6a, - 0x36, 0x02, 0x00, 0x20, 0x01, 0x20, 0x01, 0x28, 0x02, 0x04, 0x20, 0x07, - 0x6b, 0x36, 0x02, 0x04, 0x20, 0x06, 0x20, 0x04, 0x6b, 0x21, 0x06, 0x20, - 0x05, 0x20, 0x08, 0x6b, 0x21, 0x05, 0x0c, 0x00, 0x00, 0x0b, 0x00, 0x0b, - 0x0b, 0x4d, 0x06, 0x00, 0x41, 0x80, 0x08, 0x0b, 0x12, 0x68, 0x65, 0x6c, - 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21, 0x00, 0x00, - 0x00, 0x18, 0x04, 0x00, 0x41, 0x98, 0x08, 0x0b, 0x01, 0x05, 0x00, 0x41, - 0xa4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xbc, 0x08, 0x0b, 0x0e, 0x02, - 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0xb8, 0x04, 0x00, 0x00, 0x00, - 0x04, 0x00, 0x41, 0xd4, 0x08, 0x0b, 0x01, 0x01, 0x00, 0x41, 0xe3, 0x08, - 0x0b, 0x05, 0x0a, 0xff, 0xff, 0xff, 0xff, - ]); - - // Trying to import WASI twice. - expect( - () => WasmModule(helloWorldData).builder()..enableWasi()..enableWasi(), - throwsWasmError(startsWith('WASI is already enabled')), - ); - - // Missing imports due to not enabling WASI. - expect( - () => WasmModule(helloWorldData).builder().build(), - throwsWasmError(startsWith('Missing import: ')), - ); - - // Trying to get stdout/stderr without WASI enabled (WASI function import has - // been manually filled). - var inst = (WasmModule(helloWorldData).builder() - ..addFunction( - 'wasi_unstable', - 'fd_write', - (int fd, int iovs, int iovsLen, int unused) => 0, - )) - .build(); - expect( - () => inst.stdout, - throwsWasmError("Can't capture stdout without WASI enabled."), - ); - expect( - () => inst.stderr, - throwsWasmError("Can't capture stderr without WASI enabled."), - ); - }); -} diff --git a/pkg/wasm/tool/generate_ffi_boilerplate.py b/pkg/wasm/tool/generate_ffi_boilerplate.py deleted file mode 100755 index ed620ccdfa4..00000000000 --- a/pkg/wasm/tool/generate_ffi_boilerplate.py +++ /dev/null @@ -1,389 +0,0 @@ -#!/usr/bin/env python3 -# -# Copyright (c) 2020, 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. - -# This is an ad-hoc script that generates FFI boilderplate for the Wasmer API. -# The relevant functions from wasm.h have been copied below, and are parsed to -# figure out the FFI boilerplate. The results are inserted into -# wasmer_api_template.dart and runtime_template.dart to generate wasmer_api.dart -# and runtime.dart. - -# Usage: -# ./generate_ffi_boilerplate.py && dart format -owrite ../lib/ - -import os -import re - -predefTypes = {} -opaqueTypes = set() -vecTypes = {} -fns = [] -unusedFns = set() - - -def camel(t): - return ''.join([s[0].upper() + s[1:] for s in t.split('_')]) - - -def ptrWrap(t, n): - for i in range(n): - t = 'Pointer<%s>' % t - return t - - -def removePrefix(t): - assert (t.startswith('wasm_') or t.startswith('wasi_') or - t.startswith('wasmer_') or t.startswith('Dart_') or - t.startswith('set_finalizer_')) - return t[(5 if t.startswith('wasm_') else 0):] - - -def addPrefix(t): - if t.startswith('wasi_') or t.startswith('wasmer_') or t.startswith( - 'Dart_') or t.startswith('set_finalizer_'): - return t - return 'wasm_' + t - - -def getDartType(t, i): - if t in predefTypes: - return predefTypes[t][i] - assert (t.endswith('_t')) - return 'Wasmer' + camel(removePrefix(t[:-2])) - - -def dartArgType(a, i): - n, t = a - j = i if n == 0 else 0 - return ptrWrap(getDartType(t, j), n) - - -def dartFnType(r, a, i): - return '%s Function(%s)' % (dartArgType(r, i), ', '.join( - [dartArgType(t, i) for t in a])) - - -def dartFnTypeName(n): - return camel(removePrefix(n)) - - -def dartFnMembName(n): - return '_' + removePrefix(n) - - -def nativeTypeToFfi(n): - return getDartType(n, 0) - - -def nativeTypeToDart(n): - return getDartType(n, 1) - - -def getFns(): - for name, retType, args in sorted(fns): - if name not in unusedFns: - yield name, retType, args - - -opaqueTypeTemplate = '''// %s_t -class Wasmer%s extends Opaque {}''' - -vecTypeTemplate = '''// %s_vec_t -class Wasmer%sVec extends Struct { - @Uint64() - external int length; - - external Pointer<%s> data; - - %s -}''' - -byteVecToStringTemplate = ''' - Uint8List get list => data.asTypedList(length); - @override - String toString() => utf8.decode(list); -''' - -fnApiTemplate = ''' -// %s -typedef NativeWasmer%sFn = %s; -typedef Wasmer%sFn = %s;''' - - -def getWasmerApi(): - return ('\n\n'.join([ - opaqueTypeTemplate % (addPrefix(t), camel(t)) - for t in sorted(opaqueTypes) - ]) + '\n\n' + '\n\n'.join([ - vecTypeTemplate % (addPrefix(t), camel(t), - ('Pointer<%s>' if ptr else '%s') % - nativeTypeToFfi('%s_t' % addPrefix(t)), - (byteVecToStringTemplate if t == 'byte' else '')) - for t, ptr in sorted(vecTypes.items()) - ]) + '\n' + '\n'.join([ - fnApiTemplate % - (name, dartFnTypeName(name), dartFnType(retType, args, 0), - dartFnTypeName(name), dartFnType(retType, args, 1)) - for name, retType, args in getFns() - ])) - - -def getRuntimeMemb(): - return '\n'.join([ - " late final Wasmer%sFn %s;" % - (dartFnTypeName(name), dartFnMembName(name)) for name, _, _ in getFns() - ]) - - -def getRuntimeLoad(): - return '\n'.join([ - " %s = _lib.lookupFunction('%s',);" % - (dartFnMembName(name), dartFnTypeName(name), dartFnTypeName(name), name) - for name, _, _ in getFns() - ]) - - -def predefinedType(nativeType, ffiType, dartType): - predefTypes[nativeType] = (ffiType, dartType) - - -def match(r, s): - return r.fullmatch(s).groups() - - -reReplace = [(re.compile('\\b%s\\b' % k), v) for k, v in [ - ('const', ''), - ('own', ''), - ('WASM_API_EXTERN', ''), - ('wasm_name_t', 'wasm_byte_vec_t'), - ('wasm_message_t', 'wasm_byte_vec_t'), - ('wasm_memory_pages_t', 'uint32_t'), - ('wasm_externkind_t', 'uint8_t'), - ('wasm_valkind_t', 'uint8_t'), -]] -reWord = re.compile(r'\b\w+\b') - - -def parseType(s): - for r, t in reReplace: - s = r.sub(t, s) - s = s.strip() - numWords = len(reWord.findall(s)) - assert (numWords == 1 or numWords == 2) - if numWords == 2: - i = 0 - - def lastWordRepl(m): - nonlocal i - i += 1 - return '' if i == numWords else m.group(0) - - s = reWord.sub(lastWordRepl, s) - numPtr = 0 - while True: - s = s.strip() - if s.endswith('*'): - s = s[:-1] - elif s.endswith('[]'): - s = s[:-2] - else: - break - numPtr += 1 - return (numPtr, s) - - -reFnSig = re.compile(r'(.*) ([^ ]*)\((.*)\);?') - - -def addFn(sig): - ret, name, argpack = match(reFnSig, sig) - retType = parseType(ret) - args = [parseType(a) for a in argpack.split(',') if len(a.strip()) > 0] - for _, t in args + [retType]: - if t not in predefTypes and removePrefix( - t[:-2]) not in opaqueTypes and removePrefix( - t[:-6]) not in vecTypes: - print('Missing type: ' + t) - fns.append((name, retType, args)) - - -def declareOwn(name): - opaqueTypes.add(name) - n = addPrefix(name) - addFn('void %s_delete(%s_t*)' % (n, n)) - - -def declareVec(name, storePtr): - vecTypes[name] = storePtr - n = addPrefix(name) - addFn('void %s_vec_new_empty(%s_vec_t* out)' % (n, n)) - addFn('void %s_vec_new_uninitialized(%s_vec_t* out, size_t)' % (n, n)) - addFn('void %s_vec_new(%s_vec_t* out, size_t, %s_t %s[])' % - (n, n, n, '*' if storePtr else '')) - addFn('void %s_vec_copy(%s_vec_t* out, const %s_vec_t*)' % (n, n, n)) - addFn('void %s_vec_delete(%s_vec_t*)' % (n, n)) - - -def declareType(name, withCopy=True): - declareOwn(name) - declareVec(name, True) - if withCopy: - n = addPrefix(name) - addFn('%s_t* %s_copy(%s_t*)' % (n, n, n)) - - -predefinedType('void', 'Void', 'void') -predefinedType('bool', 'Uint8', 'int') -predefinedType('int', 'Int64', 'int') -predefinedType('byte_t', 'Uint8', 'int') -predefinedType('wasm_byte_t', 'Uint8', 'int') -predefinedType('uint8_t', 'Uint8', 'int') -predefinedType('uint16_t', 'Uint16', 'int') -predefinedType('uint32_t', 'Uint32', 'int') -predefinedType('uint64_t', 'Uint64', 'int') -predefinedType('size_t', 'Uint64', 'int') -predefinedType('uintptr_t', 'Uint64', 'int') -predefinedType('intptr_t', 'Int64', 'int') -predefinedType('int8_t', 'Int8', 'int') -predefinedType('int16_t', 'Int16', 'int') -predefinedType('int32_t', 'Int32', 'int') -predefinedType('int64_t', 'Int64', 'int') -predefinedType('float32_t', 'Float32', 'double') -predefinedType('float64_t', 'Float64', 'double') -predefinedType('wasm_limits_t', 'WasmerLimits', 'WasmerLimits') -predefinedType('wasm_val_t', 'WasmerVal', 'WasmerVal') -predefinedType('Dart_Handle', 'Handle', 'Object') - -declareOwn('engine') -declareOwn('store') -declareOwn('wasi_config') -declareOwn('wasi_env') -declareVec('byte', False) -declareVec('val', False) -declareType('importtype') -declareType('exporttype') -declareType('valtype') -declareType('extern', False) - -# These are actually DECLARE_TYPE, but we don't need the vec or copy stuff. -declareOwn('memorytype') -declareOwn('externtype') -declareOwn('functype') - -# These are actually DECLARE_SHARABLE_REF, but we don't need the ref stuff. -declareOwn('module') - -# These are actually DECLARE_REF, but we don't need the ref stuff. -declareOwn('memory') -declareOwn('trap') -declareOwn('instance') -declareOwn('func') - -rawFns = ''' -WASM_API_EXTERN own wasm_engine_t* wasm_engine_new(); -WASM_API_EXTERN own wasm_store_t* wasm_store_new(wasm_engine_t*); -WASM_API_EXTERN own wasm_memorytype_t* wasm_memorytype_new(const wasm_limits_t*); -WASM_API_EXTERN own wasm_module_t* wasm_module_new(wasm_store_t*, const wasm_byte_vec_t* binary); -WASM_API_EXTERN void wasm_module_imports(const wasm_module_t*, own wasm_importtype_vec_t* out); -WASM_API_EXTERN const wasm_name_t* wasm_importtype_module(const wasm_importtype_t*); -WASM_API_EXTERN const wasm_name_t* wasm_importtype_name(const wasm_importtype_t*); -WASM_API_EXTERN const wasm_externtype_t* wasm_importtype_type(const wasm_importtype_t*); -WASM_API_EXTERN wasm_functype_t* wasm_externtype_as_functype(wasm_externtype_t*); -WASM_API_EXTERN void wasm_module_exports(const wasm_module_t*, own wasm_exporttype_vec_t* out); -WASM_API_EXTERN const wasm_name_t* wasm_exporttype_name(const wasm_exporttype_t*); -WASM_API_EXTERN const wasm_externtype_t* wasm_exporttype_type(const wasm_exporttype_t*); -WASM_API_EXTERN wasm_externkind_t wasm_externtype_kind(const wasm_externtype_t*); -WASM_API_EXTERN own wasm_instance_t* wasm_instance_new(wasm_store_t*, const wasm_module_t*, const wasm_extern_vec_t* imports, own wasm_trap_t**); -WASM_API_EXTERN void wasm_instance_exports(const wasm_instance_t*, own wasm_extern_vec_t* out); -WASM_API_EXTERN own wasm_memory_t* wasm_memory_new(wasm_store_t*, const wasm_memorytype_t*); -WASM_API_EXTERN byte_t* wasm_memory_data(wasm_memory_t*); -WASM_API_EXTERN size_t wasm_memory_data_size(const wasm_memory_t*); -WASM_API_EXTERN wasm_memory_pages_t wasm_memory_size(const wasm_memory_t*); -WASM_API_EXTERN bool wasm_memory_grow(wasm_memory_t*, wasm_memory_pages_t delta); -WASM_API_EXTERN wasm_externkind_t wasm_extern_kind(const wasm_extern_t*); -WASM_API_EXTERN wasm_func_t* wasm_extern_as_func(wasm_extern_t*); -WASM_API_EXTERN wasm_extern_t* wasm_func_as_extern(wasm_func_t*); -WASM_API_EXTERN wasm_memory_t* wasm_extern_as_memory(wasm_extern_t*); -WASM_API_EXTERN wasm_extern_t* wasm_memory_as_extern(wasm_memory_t*); -WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_params(const wasm_functype_t*); -WASM_API_EXTERN const wasm_valtype_vec_t* wasm_functype_results(const wasm_functype_t*); -WASM_API_EXTERN own wasm_func_t* wasm_func_new_with_env( wasm_store_t*, const wasm_functype_t* type, void* fn, void* env, void *finalizer); -WASM_API_EXTERN own wasm_trap_t* wasm_func_call(const wasm_func_t*, const wasm_val_vec_t* args, wasm_val_vec_t* results); -WASM_API_EXTERN own wasm_trap_t* wasm_trap_new(wasm_store_t* store, const wasm_message_t*); -WASM_API_EXTERN void wasm_trap_message(const wasm_trap_t*, own wasm_message_t* out); -WASM_API_EXTERN wasm_valkind_t wasm_valtype_kind(const wasm_valtype_t*); - -wasi_config_t* wasi_config_new(const uint8_t* program_name); -wasi_env_t* wasi_env_new(wasi_config_t* config); -bool wasi_get_imports(const wasm_store_t* store, const wasm_module_t* module, const wasi_env_t* wasi_env, wasm_extern_vec_t* imports); -int wasmer_last_error_message(uint8_t* buffer, int length); -int wasmer_last_error_length(); -void wasi_env_set_memory(wasi_env_t* env, const wasm_memory_t* memory); -void wasi_config_inherit_stdout(wasi_config_t* config); -void wasi_config_inherit_stderr(wasi_config_t* config); -intptr_t wasi_env_read_stderr(wasi_env_t* env, uint8_t* buffer, uintptr_t buffer_len); -intptr_t wasi_env_read_stdout(wasi_env_t* env, uint8_t* buffer, uintptr_t buffer_len); - -intptr_t Dart_InitializeApiDL(void* data); -void set_finalizer_for_engine(Dart_Handle, wasm_engine_t*); -void set_finalizer_for_store(Dart_Handle, wasm_store_t*); -void set_finalizer_for_module(Dart_Handle, wasm_module_t*); -void set_finalizer_for_instance(Dart_Handle, wasm_instance_t*); -void set_finalizer_for_trap(Dart_Handle, wasm_trap_t*); -void set_finalizer_for_memorytype(Dart_Handle, wasm_memorytype_t*); -void set_finalizer_for_memory(Dart_Handle, wasm_memory_t*); -void set_finalizer_for_func(Dart_Handle, wasm_func_t*); -''' -for f in rawFns.split('\n'): - if len(f.strip()) > 0: - addFn(f) - -unusedFns = { - 'wasm_byte_vec_copy', - 'wasm_exporttype_delete', - 'wasm_exporttype_copy', - 'wasm_exporttype_vec_copy', - 'wasm_extern_vec_copy', - 'wasm_importtype_delete', - 'wasm_importtype_copy', - 'wasm_importtype_vec_copy', - 'wasm_val_vec_copy', - 'wasm_val_vec_delete', - 'wasm_val_vec_new', - 'wasm_val_vec_new_empty', - 'wasm_val_vec_new_uninitialized', - 'wasm_valtype_copy', - 'wasm_valtype_vec_copy', - 'wasi_config_delete', -} - -genDoc = '''// This file has been automatically generated. Please do not edit it manually. -// To regenerate the file, use the following command -// "generate_ffi_boilerplate.py".''' - -thisDir = os.path.dirname(os.path.abspath(__file__)) - - -def readFile(filename): - with open(os.path.abspath(os.path.join(thisDir, filename)), 'r') as f: - return f.read() - - -def writeFile(filename, content): - with open(os.path.abspath(os.path.join(thisDir, '../lib/src', filename)), - 'w') as f: - f.write(content) - - -wasmerApiText = readFile('wasmer_api_template.dart.t') -wasmerApiText = wasmerApiText.replace('/* */', getWasmerApi()) -wasmerApiText = wasmerApiText.replace('/* */', genDoc) -writeFile('wasmer_api.g.dart', wasmerApiText) - -runtimeText = readFile('runtime_template.dart.t') -runtimeText = runtimeText.replace('/* */', getRuntimeMemb()) -runtimeText = runtimeText.replace('/* */', getRuntimeLoad()) -runtimeText = runtimeText.replace('/* */', genDoc) -writeFile('runtime.g.dart', runtimeText) diff --git a/pkg/wasm/tool/runtime_template.dart.t b/pkg/wasm/tool/runtime_template.dart.t deleted file mode 100644 index c2c42e50021..00000000000 --- a/pkg/wasm/tool/runtime_template.dart.t +++ /dev/null @@ -1,350 +0,0 @@ -// Copyright (c) 2020, 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. - -/* */ - -// ignore_for_file: cascade_invocations -// ignore_for_file: non_constant_identifier_names -// ignore_for_file: unused_field - -part of 'runtime.dart'; - -class WasmRuntime { - static final WasmRuntime _inst = WasmRuntime._init(); - final DynamicLibrary _lib; - final _traps = {}; - late final Pointer _engine; - -/* */ - - factory WasmRuntime() => _inst; - - WasmRuntime._init() : _lib = DynamicLibrary.open(_getLibPath()) { -/* */ - - if (_Dart_InitializeApiDL(NativeApi.initializeApiDLData) != 0) { - throw WasmError('Failed to initialize Dart API'); - } - _engine = _engine_new(); - _checkNotEqual(_engine, nullptr, 'Failed to initialize Wasm engine.'); - _set_finalizer_for_engine(this, _engine); - } - - Pointer newStore(Object owner) { - var store = _checkNotEqual( - _store_new(_engine), - nullptr, - 'Failed to create Wasm store.', - ); - _set_finalizer_for_store(owner, store); - return store; - } - - Pointer compile( - Object owner, - Pointer store, - Uint8List data, - ) { - var dataPtr = calloc(data.length); - for (var i = 0; i < data.length; ++i) { - dataPtr[i] = data[i]; - } - var dataVec = calloc(); - dataVec.ref.data = dataPtr; - dataVec.ref.length = data.length; - - var modulePtr = _module_new(store, dataVec); - - calloc.free(dataPtr); - calloc.free(dataVec); - - _checkNotEqual(modulePtr, nullptr, 'Wasm module compile failed.'); - _set_finalizer_for_module(owner, modulePtr); - return modulePtr; - } - - List exportDescriptors(Pointer module) { - var exportsVec = calloc(); - _module_exports(module, exportsVec); - var exps = []; - for (var i = 0; i < exportsVec.ref.length; ++i) { - var exp = exportsVec.ref.data[i]; - var extern = _exporttype_type(exp); - var kind = _externtype_kind(extern); - var fnType = kind == wasmerExternKindFunction - ? _externtype_as_functype(extern) - : nullptr; - exps.add( - WasmExportDescriptor._( - kind, - _exporttype_name(exp).ref.toString(), - fnType, - ), - ); - } - calloc.free(exportsVec); - return exps; - } - - List importDescriptors(Pointer module) { - var importsVec = calloc(); - _module_imports(module, importsVec); - var imps = []; - for (var i = 0; i < importsVec.ref.length; ++i) { - var imp = importsVec.ref.data[i]; - var extern = _importtype_type(imp); - var kind = _externtype_kind(extern); - var fnType = kind == wasmerExternKindFunction - ? _externtype_as_functype(extern) - : nullptr; - imps.add( - WasmImportDescriptor._( - kind, - _importtype_module(imp).ref.toString(), - _importtype_name(imp).ref.toString(), - fnType, - ), - ); - } - calloc.free(importsVec); - return imps; - } - - void maybeThrowTrap(Pointer trap, String source) { - if (trap != nullptr) { - // There are 2 kinds of trap, and their memory is managed differently. - // Traps created in the newTrap method below are stored in the traps map - // with a corresponding exception, and their memory is managed using a - // finalizer on the _WasmTrapsEntry. Traps can also be created by WASM - // code, and in that case we delete them in this function. - var entry = _traps.remove(trap.address); - if (entry != null) { - // ignore: only_throw_errors - throw entry.exception; - } else { - // TODO: code path not hit in tests! - var trapMessage = calloc(); - _trap_message(trap, trapMessage); - var message = 'Wasm trap when calling $source: ${trapMessage.ref}'; - _byte_vec_delete(trapMessage); - calloc.free(trapMessage); - _trap_delete(trap); - throw WasmError(message); - } - } - } - - Pointer instantiate( - Object owner, - Pointer store, - Pointer module, - Pointer imports, - ) { - var trap = calloc>(); - trap.value = nullptr; - var inst = _instance_new(store, module, imports, trap); - maybeThrowTrap(trap.value, 'module initialization function'); - calloc.free(trap); - _checkNotEqual(inst, nullptr, 'Wasm module instantiation failed.'); - _set_finalizer_for_instance(owner, inst); - return inst; - } - - // Clean up the exports after use, with deleteExports. - Pointer exports(Pointer instancePtr) { - var exports = calloc(); - _instance_exports(instancePtr, exports); - return exports; - } - - void deleteExports(Pointer exports) { - _extern_vec_delete(exports); - calloc.free(exports); - } - - int externKind(Pointer extern) => _extern_kind(extern); - - Pointer externToFunction(Pointer extern) => - _extern_as_func(extern); - - Pointer functionToExtern(Pointer func) => - _func_as_extern(func); - - List getArgTypes(Pointer funcType) { - var types = []; - var args = _functype_params(funcType); - for (var i = 0; i < args.ref.length; ++i) { - types.add(_valtype_kind(args.ref.data[i])); - } - return types; - } - - int getReturnType(Pointer funcType) { - var rets = _functype_results(funcType); - if (rets.ref.length == 0) { - return wasmerValKindVoid; - } else if (rets.ref.length > 1) { - throw WasmError('Multiple return values are not supported'); - } - return _valtype_kind(rets.ref.data[0]); - } - - void call( - Pointer func, - Pointer args, - Pointer results, - String source, - ) { - maybeThrowTrap(_func_call(func, args, results), source); - } - - Pointer externToMemory(Pointer extern) => - _extern_as_memory(extern); - - Pointer memoryToExtern(Pointer memory) => - _memory_as_extern(memory); - - Pointer newMemory( - Object owner, - Pointer store, - int pages, - int? maxPages, - ) { - var limPtr = calloc(); - limPtr.ref.min = pages; - limPtr.ref.max = maxPages ?? wasmLimitsMaxDefault; - var memType = _memorytype_new(limPtr); - calloc.free(limPtr); - _checkNotEqual(memType, nullptr, 'Failed to create memory type.'); - _set_finalizer_for_memorytype(owner, memType); - var memory = _checkNotEqual( - _memory_new(store, memType), - nullptr, - 'Failed to create memory.', - ); - _set_finalizer_for_memory(owner, memory); - return memory; - } - - void growMemory(Pointer memory, int deltaPages) { - _checkNotEqual( - _memory_grow(memory, deltaPages), - 0, - 'Failed to grow memory.', - ); - } - - int memoryLength(Pointer memory) => _memory_size(memory); - - Uint8List memoryView(Pointer memory) => - _memory_data(memory).asTypedList(_memory_data_size(memory)); - - Pointer newFunc( - Object owner, - Pointer store, - Pointer funcType, - Pointer func, - Pointer env, - Pointer finalizer, - ) { - var f = _func_new_with_env( - store, - funcType, - func.cast(), - env.cast(), - finalizer.cast(), - ); - _checkNotEqual(f, nullptr, 'Failed to create function.'); - _set_finalizer_for_func(owner, f); - return f; - } - - Pointer newTrap(Pointer store, Object exception) { - var msg = calloc(); - msg.ref.data = calloc(); - msg.ref.data[0] = 0; - msg.ref.length = 0; - var trap = _trap_new(store, msg); - calloc.free(msg.ref.data); - calloc.free(msg); - _checkNotEqual(trap, nullptr, 'Failed to create trap.'); - var entry = _WasmTrapsEntry(exception); - _set_finalizer_for_trap(entry, trap); - _traps[trap.address] = entry; - return trap; - } - - Pointer newWasiConfig() { - var name = calloc(); - name[0] = 0; - var config = _wasi_config_new(name); - calloc.free(name); - return _checkNotEqual(config, nullptr, 'Failed to create WASI config.'); - } - - void captureWasiStdout(Pointer config) { - _wasi_config_inherit_stdout(config); - } - - void captureWasiStderr(Pointer config) { - _wasi_config_inherit_stderr(config); - } - - Pointer newWasiEnv(Pointer config) => - _checkNotEqual( - _wasi_env_new(config), - nullptr, - 'Failed to create WASI environment.', - ); - - void wasiEnvSetMemory( - Pointer env, - Pointer memory, - ) { - _wasi_env_set_memory(env, memory); - } - - void getWasiImports( - Pointer store, - Pointer mod, - Pointer env, - Pointer imports, - ) { - _checkNotEqual( - _wasi_get_imports(store, mod, env, imports), - 0, - 'Failed to fill WASI imports.', - ); - } - - Stream> getWasiStdoutStream(Pointer env) => - Stream.fromIterable(_WasiStreamIterable(env, _wasi_env_read_stdout)); - - Stream> getWasiStderrStream(Pointer env) => - Stream.fromIterable(_WasiStreamIterable(env, _wasi_env_read_stderr)); - - String _getLastError() { - var length = _wasmer_last_error_length(); - var buf = calloc(length); - _wasmer_last_error_message(buf, length); - var message = utf8.decode(buf.asTypedList(length)); - calloc.free(buf); - return message; - } - - T _checkNotEqual(T x, T y, String errorMessage) { - if (x == y) { - throw WasmError('$errorMessage\n${_getLastError()}'.trim()); - } - return x; - } - - static String getSignatureString( - String name, - List argTypes, - int returnType, - ) => - '${wasmerValKindName(returnType)} ' - "$name(${argTypes.map(wasmerValKindName).join(", ")})"; -} diff --git a/pkg/wasm/tool/wasmer_api_template.dart.t b/pkg/wasm/tool/wasmer_api_template.dart.t deleted file mode 100644 index bae678ebec8..00000000000 --- a/pkg/wasm/tool/wasmer_api_template.dart.t +++ /dev/null @@ -1,11 +0,0 @@ -// Copyright (c) 2020, 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. - -/* */ - -// ignore_for_file: require_trailing_commas - -part of 'wasmer_api.dart'; - -/* */ diff --git a/tools/bots/test_matrix.json b/tools/bots/test_matrix.json index 0744088e3db..1d078c026b7 100644 --- a/tools/bots/test_matrix.json +++ b/tools/bots/test_matrix.json @@ -3356,14 +3356,6 @@ "pkg/vm_snapshot_analysis" ] }, - { - "name": "analyze pkg/wasm", - "script": "out/ReleaseX64/dart-sdk/bin/dart", - "arguments": [ - "analyze", - "pkg/wasm" - ] - }, { "name": "analyze pkg/dds", "script": "out/ReleaseX64/dart-sdk/bin/dart",