|
| 1 | +// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file |
| 2 | +// for details. All rights reserved. Use of this source code is governed by a |
| 3 | +// BSD-style license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:ffi'; |
| 6 | +import 'dart:io'; |
| 7 | +import 'dart:typed_data'; |
| 8 | +import 'package:ffi/ffi.dart'; |
| 9 | +import 'package:path/path.dart' as path; |
| 10 | + |
| 11 | +const int WasmerResultOk = 1; |
| 12 | +const int WasmerResultError = 2; |
| 13 | + |
| 14 | +const int WasmerValueTagI32 = 0; |
| 15 | +const int WasmerValueTagI64 = 1; |
| 16 | +const int WasmerValueTagF32 = 2; |
| 17 | +const int WasmerValueTagF64 = 3; |
| 18 | + |
| 19 | +class WasmerModule extends Struct {} |
| 20 | + |
| 21 | +typedef NativeWasmerCompileFn = Uint32 Function( |
| 22 | + Pointer<Pointer<WasmerModule>>, Pointer<Uint8>, Uint32); |
| 23 | +typedef WasmerCompileFn = int Function( |
| 24 | + Pointer<Pointer<WasmerModule>>, Pointer<Uint8>, int); |
| 25 | + |
| 26 | +class WasmRuntime { |
| 27 | + static WasmRuntime _inst; |
| 28 | + |
| 29 | + DynamicLibrary _lib; |
| 30 | + WasmerCompileFn _compile; |
| 31 | + |
| 32 | + factory WasmRuntime() { |
| 33 | + if (_inst == null) { |
| 34 | + _inst = WasmRuntime._init(); |
| 35 | + } |
| 36 | + return _inst; |
| 37 | + } |
| 38 | + |
| 39 | + static String _getLibName() { |
| 40 | + if (Platform.isMacOS) return "libwasmer.dylib"; |
| 41 | + if (Platform.isLinux) return "libwasmer.so"; |
| 42 | + throw Exception("Wasm not currently supported on this platform"); |
| 43 | + } |
| 44 | + |
| 45 | + static String _getLibDir() { |
| 46 | + // The common case, and how cli_util.dart computes the Dart SDK directory, |
| 47 | + // path.dirname called twice on Platform.resolvedExecutable. |
| 48 | + var commonLibDir = path.join( |
| 49 | + path.absolute(path.dirname(path.dirname(Platform.resolvedExecutable))), |
| 50 | + 'bin', |
| 51 | + 'third_party', |
| 52 | + 'wasmer'); |
| 53 | + if (Directory(commonLibDir).existsSync()) { |
| 54 | + return commonLibDir; |
| 55 | + } |
| 56 | + |
| 57 | + // This is the less common case where the user is in the checked out Dart |
| 58 | + // SDK, and is executing dart via: |
| 59 | + // ./out/ReleaseX64/dart ... |
| 60 | + var checkedOutLibDir = path.join( |
| 61 | + path.absolute(path.dirname(Platform.resolvedExecutable)), |
| 62 | + 'dart-sdk', |
| 63 | + 'bin', |
| 64 | + 'third_party', |
| 65 | + 'wasmer'); |
| 66 | + if (Directory(checkedOutLibDir).existsSync()) { |
| 67 | + return checkedOutLibDir; |
| 68 | + } |
| 69 | + |
| 70 | + // If neither returned above, we return the common case: |
| 71 | + return commonLibDir; |
| 72 | + } |
| 73 | + |
| 74 | + WasmRuntime._init() { |
| 75 | + var libPath = path.join(_getLibDir(), _getLibName()); |
| 76 | + _lib = DynamicLibrary.open(libPath); |
| 77 | + _compile = _lib |
| 78 | + .lookup<NativeFunction<NativeWasmerCompileFn>>('wasmer_compile') |
| 79 | + .asFunction(); |
| 80 | + } |
| 81 | + |
| 82 | + Pointer<WasmerModule> compile(Uint8List data) { |
| 83 | + var dataPtr = allocate<Uint8>(count: data.length); |
| 84 | + for (int i = 0; i < data.length; ++i) { |
| 85 | + dataPtr[i] = data[i]; |
| 86 | + } |
| 87 | + |
| 88 | + var modulePtrPtr = allocate<Pointer<WasmerModule>>(); |
| 89 | + int result = _compile(modulePtrPtr, dataPtr, data.length); |
| 90 | + Pointer<WasmerModule> modulePtr = modulePtrPtr.value; |
| 91 | + |
| 92 | + free(modulePtrPtr); |
| 93 | + free(dataPtr); |
| 94 | + |
| 95 | + if (result != WasmerResultOk) { |
| 96 | + throw Exception("Wasm module compile failed"); |
| 97 | + } |
| 98 | + |
| 99 | + return modulePtr; |
| 100 | + } |
| 101 | +} |
0 commit comments