affc3392a0
This CL introduces a new kernel-to-kernel pass in Wasm backend to
compile `FfiNative`-annotated external functions to external functions
with `wasm:import` pragmas.
The new pass `WasmFfiNativeTransformer` extends `FfiNativeTransformer`.
Some `FfiNativeTransformer` methods made public to allow reuse in the
pass.
The conversion works like this: when we see a member like:
@FfiNative<Int8 Function(Int8, Int8)>("ffi.addInt8")
external int addInt8(int a, int b);
We generate a Wasm import using Wasm ABI types according to emscripten
calling conventions:
@pragma('wasm:import', 'ffi.addInt8')
external static WasmI32 addInt8_$import(WasmI32 a, WasmI32 b);
and convert the original member (`addInt8()`) to a wrapper function that
calls the Wasm import, converting the arguments and return values to
Dart types:
static int addInt8(int a, int b) {
return WasmI32.toIntSigned(
addInt8_$import(
WasmI32::int8FromInt(a),
WasmI32::int8FromInt(b),
));
}
Build, test, and CI changes:
- Test runner now uses `// SharedObjects=...` lines in dart2wasm tests
to pass extra Wasm modules to d8 command used to run the tests.
Support for `// SharedObjects=...` lines were already used in
native/AOT FFI tests, not added in this CL. We just start to use those
lines in dart2wasm tests.
- dart2wasm gn file updated with a target "test_wasm_modules" that
builds FFI test Wasm modules to Wasm using emcc (emscripten C
compiler).
- CI dart2wasm_hostasserts configuration updated to copy the Wasm files
generated by the "test_wasm_modules" target to the shards.
TEST=tests/web/wasm/ffi_native_test
Change-Id: I6527fe4e2ca2b582e16d84fee244e9cbe6dee307
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/252822
Reviewed-by: Alexander Thomas <athom@google.com>
Commit-Queue: Ömer Ağacan <omersa@google.com>
Reviewed-by: Ryan Macnak <rmacnak@google.com>
Reviewed-by: Aske Simon Christensen <askesc@google.com>
91 lines
1.5 KiB
C
91 lines
1.5 KiB
C
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
|
|
// for details. All rights reserved. Use of this source code is governed by a
|
|
// BSD-style license that can be found in the LICENSE file.
|
|
|
|
#include <emscripten.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
|
|
struct MyStruct {
|
|
double x;
|
|
int16_t y;
|
|
} s = { 1.0, 2 }, a[10];
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
struct MyStruct* getStruct() {
|
|
return &s;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
void clearStruct(struct MyStruct* s) {
|
|
s->x = 0.0;
|
|
s->y = 0;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
void empty() {}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
int8_t addInt8(int8_t a, int8_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
uint8_t addUint8(uint8_t a, uint8_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
int16_t addInt16(int16_t a, int16_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
uint16_t addUint16(uint16_t a, uint16_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
int32_t addInt32(int32_t a, int32_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
uint32_t addUint32(uint32_t a, uint32_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
int64_t addInt64(int64_t a, int64_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
uint64_t addUint64(uint64_t a, uint64_t b) {
|
|
return a + b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
bool negateBool(bool a) {
|
|
return !a;
|
|
}
|
|
|
|
static bool b = true;
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
void toggleBool() {
|
|
b = !b;
|
|
}
|
|
|
|
EMSCRIPTEN_KEEPALIVE
|
|
bool boolReturn(int a) {
|
|
if (a == 123) {
|
|
return true;
|
|
} else if (a == 456) {
|
|
return false;
|
|
} else {
|
|
return b;
|
|
}
|
|
}
|