Skip to content

Commit 4adcae4

Browse files
druntime: Use WASIp2 directly instead of POSIX emulation
1 parent c050aa5 commit 4adcae4

59 files changed

Lines changed: 6967 additions & 74 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

druntime/src/core/internal/abort.d

Lines changed: 11 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -69,63 +69,21 @@ void abort(scope string msg, scope string filename = __FILE__, size_t line = __L
6969
cast(void)__wasi_fd_write(2, &iovec, 1, &ret);
7070
}
7171
}
72-
} else version (WASIp2) {
73-
// currently depends on wasi-libc being linked in to provide these "syscalls"
74-
// TODO: detach this from wasi-libc
75-
extern(C) struct wasip2_list_u8_t {
76-
ubyte* ptr;
77-
size_t len;
78-
}
79-
extern(C) struct streams_own_output_stream_t {
80-
int __handle;
81-
}
82-
extern(C) struct streams_borrow_output_stream_t {
83-
int __handle;
84-
}
85-
alias streams_own_output_stream_t stderr_own_output_stream_t;
86-
extern(C) struct io_error_own_error_t {
87-
int __handle;
88-
}
89-
alias io_error_own_error_t streams_own_error_t;
90-
91-
extern(C) struct streams_stream_error_t {
92-
ubyte tag;
93-
94-
union Val {
95-
streams_own_error_t last_operation_failed;
96-
}
97-
Val val;
98-
}
99-
100-
pragma(mangle, "streams_output_stream_drop_own")
101-
extern(C) static void streams_output_stream_drop_own(streams_own_output_stream_t handle) @nogc nothrow;
102-
pragma(mangle, "streams_borrow_output_stream")
103-
extern(C) static streams_borrow_output_stream_t streams_borrow_output_stream(streams_own_output_stream_t handle) @nogc nothrow;
104-
pragma(mangle, "stderr_get_stderr")
105-
extern(C) static stderr_own_output_stream_t stderr_get_stderr() @nogc nothrow;
106-
pragma(mangle, "io_error_error_drop_own")
107-
extern(C) static void io_error_error_drop_own(io_error_own_error_t handle) @nogc nothrow;
108-
pragma(mangle, "streams_stream_error_free")
109-
extern(C) static void streams_stream_error_free(streams_stream_error_t *ptr) @nogc nothrow;
110-
pragma(mangle, "streams_method_output_stream_blocking_write_and_flush")
111-
extern(C) static bool streams_method_output_stream_blocking_write_and_flush(streams_borrow_output_stream_t self, wasip2_list_u8_t *contents, streams_stream_error_t *err) @nogc nothrow;
112-
72+
}
73+
else version (WASIp2)
74+
{
75+
import core.sys.wasi.p2.cli.stderr.imports : getStderr;
76+
import core.sys.wasi.wit_common : witFree, witList;
11377
static void writeStr(scope const(char)[][] m...) @nogc nothrow @trusted
11478
{
115-
auto stderr_own = stderr_get_stderr();
116-
scope(exit) streams_output_stream_drop_own(stderr_own);
117-
118-
auto stderr = streams_borrow_output_stream(stderr_own);
79+
auto stderr = getStderr();
80+
scope(exit) stderr.drop;
11981

12082
foreach (s; m) {
121-
wasip2_list_u8_t contents;
122-
contents.ptr = cast(ubyte*)s.ptr;
123-
contents.len = s.length;
124-
streams_stream_error_t err;
125-
bool success = streams_method_output_stream_blocking_write_and_flush(stderr, &contents, &err);
126-
if (!success) {
127-
streams_stream_error_free(&err);
128-
}
83+
auto result = stderr.blockingWriteAndFlush((cast(const(ubyte)[])s).witList);
84+
scope(exit) result.witFree;
85+
86+
// ignore errors
12987
}
13088
}
13189
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/++
2+
3+
+/
4+
module core.sys.wasi.p2.cli.environment.common;
5+
6+
import core.sys.wasi.wit_common;
7+
8+
9+
package (core.sys.wasi.p2) void __wit_bindgen_component_type_force_link() pure @nogc nothrow => imported!"core.sys.wasi.p2.cli.imports".__wit_bindgen_component_type_force_link();
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/++
2+
3+
+/
4+
module core.sys.wasi.p2.cli.environment.imports;
5+
6+
import core.sys.wasi.wit_common;
7+
8+
public import core.sys.wasi.p2.cli.environment.common;
9+
10+
11+
package (core.sys.wasi.p2) void __wit_bindgen_component_type_force_link() pure @nogc nothrow => imported!"core.sys.wasi.p2.cli.imports".__wit_bindgen_component_type_force_link();
12+
13+
/++
14+
Get the POSIX-style environment variables.
15+
16+
Each environment variable is provided as a pair of string variable names
17+
and string value.
18+
19+
Morally, these are a value import, but until value imports are available
20+
in the component model, this import function should return the same
21+
values each time it is called.
22+
+/
23+
WitList!(Tuple!(WitString, WitString)) getEnvironment() @nogc nothrow {
24+
align(size_t.sizeof) void[(2*size_t.sizeof)] _retArea = void;
25+
__import_getEnvironment(_retArea.ptr);
26+
auto _listSrcPtr4 = *(cast(void**)(_retArea.ptr + 0));
27+
auto _listLen4 = *(cast(size_t*)(_retArea.ptr + size_t.sizeof));
28+
auto _list4 = core.sys.wasi.wit_common.mallocSlice!(Tuple!(WitString, WitString))(_listLen4);
29+
foreach (_elem0_idx, ref _elem0; _list4) {
30+
const auto _base0 = _listSrcPtr4 + _elem0_idx * (4*size_t.sizeof);
31+
auto _ptr1 = cast(char*)(*(cast(void**)(_base0 + 0)));
32+
auto _len1 = *(cast(size_t*)(_base0 + size_t.sizeof));
33+
auto _ptr2 = cast(char*)(*(cast(void**)(_base0 + (2*size_t.sizeof))));
34+
auto _len2 = *(cast(size_t*)(_base0 + (3*size_t.sizeof)));
35+
auto _tuple3 = Tuple!(WitString, WitString)(
36+
WitString(_ptr1[0.._len1]),
37+
WitString(_ptr2[0.._len2]),
38+
);
39+
_elem0 = _tuple3;
40+
}
41+
auto _flush5 = WitList!(Tuple!(WitString, WitString))(_list4);
42+
return _flush5;
43+
}
44+
/// ditto
45+
@wasmImport!("wasi:cli/environment@0.2.12", "get-environment")
46+
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__get_environment")
47+
private extern(C) void __import_getEnvironment(void*) @nogc nothrow;
48+
49+
/++
50+
Get the POSIX-style arguments to the program.
51+
+/
52+
WitList!(WitString) getArguments() @nogc nothrow {
53+
align(size_t.sizeof) void[(2*size_t.sizeof)] _retArea = void;
54+
__import_getArguments(_retArea.ptr);
55+
auto _listSrcPtr2 = *(cast(void**)(_retArea.ptr + 0));
56+
auto _listLen2 = *(cast(size_t*)(_retArea.ptr + size_t.sizeof));
57+
auto _list2 = core.sys.wasi.wit_common.mallocSlice!(WitString)(_listLen2);
58+
foreach (_elem0_idx, ref _elem0; _list2) {
59+
const auto _base0 = _listSrcPtr2 + _elem0_idx * (2*size_t.sizeof);
60+
auto _ptr1 = cast(char*)(*(cast(void**)(_base0 + 0)));
61+
auto _len1 = *(cast(size_t*)(_base0 + size_t.sizeof));
62+
_elem0 = WitString(_ptr1[0.._len1]);
63+
}
64+
auto _flush3 = WitList!(WitString)(_list2);
65+
return _flush3;
66+
}
67+
/// ditto
68+
@wasmImport!("wasi:cli/environment@0.2.12", "get-arguments")
69+
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__get_arguments")
70+
private extern(C) void __import_getArguments(void*) @nogc nothrow;
71+
72+
/++
73+
Return a path that programs should use as their initial current working
74+
directory, interpreting `.` as shorthand for this.
75+
+/
76+
Option!(WitString) initialCwd() @nogc nothrow {
77+
align(size_t.sizeof) void[(3*size_t.sizeof)] _retArea = void;
78+
__import_initialCwd(_retArea.ptr);
79+
Option!(WitString) _option3 = void;
80+
bool _isSome3 = (cast(uint)(*(cast(ubyte*)(_retArea.ptr + 0)))) != 0;
81+
if (_isSome3) {
82+
auto _ptr2 = cast(char*)(*(cast(void**)(_retArea.ptr + size_t.sizeof)));
83+
auto _len2 = *(cast(size_t*)(_retArea.ptr + (2*size_t.sizeof)));
84+
85+
_option3 = Option!(WitString).some(WitString(_ptr2[0.._len2]));
86+
} else {
87+
_option3 = Option!(WitString).none;
88+
}
89+
auto _flush4 = _option3;
90+
return _flush4;
91+
}
92+
/// ditto
93+
@wasmImport!("wasi:cli/environment@0.2.12", "initial-cwd")
94+
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__initial_cwd")
95+
private extern(C) void __import_initialCwd(void*) @nogc nothrow;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
/++
2+
3+
+/
4+
module core.sys.wasi.p2.cli.exit.common;
5+
6+
import core.sys.wasi.wit_common;
7+
8+
9+
package (core.sys.wasi.p2) void __wit_bindgen_component_type_force_link() pure @nogc nothrow => imported!"core.sys.wasi.p2.cli.imports".__wit_bindgen_component_type_force_link();
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/++
2+
3+
+/
4+
module core.sys.wasi.p2.cli.exit.imports;
5+
6+
import core.sys.wasi.wit_common;
7+
8+
public import core.sys.wasi.p2.cli.exit.common;
9+
10+
11+
package (core.sys.wasi.p2) void __wit_bindgen_component_type_force_link() pure @nogc nothrow => imported!"core.sys.wasi.p2.cli.imports".__wit_bindgen_component_type_force_link();
12+
13+
/++
14+
Exit the current instance and any linked instances.
15+
+/
16+
void exit(in Result!(void, void) status) @nogc nothrow {
17+
uint _resultPart4;
18+
if (status.isErr) {
19+
20+
_resultPart4 = 1;
21+
} else {
22+
23+
_resultPart4 = 0;
24+
}
25+
__import_exit(_resultPart4);
26+
}
27+
/// ditto
28+
@wasmImport!("wasi:cli/exit@0.2.12", "exit")
29+
pragma(mangle, "__wit_import_wasi:cli__exit@0.2.12__exit")
30+
private extern(C) void __import_exit(uint) @nogc nothrow;
31+
32+
/++
33+
Exit the current instance and any linked instances, reporting the
34+
specified status code to the host.
35+
36+
The meaning of the code depends on the context, with 0 usually meaning
37+
"success", and other values indicating various types of failure.
38+
39+
This function does not return; the effect is analogous to a trap, but
40+
without the connotation that something bad has happened.
41+
+/
42+
void exitWithCode(ubyte statusCode) @nogc nothrow {
43+
__import_exitWithCode(cast(uint)(statusCode));
44+
}
45+
/// ditto
46+
@wasmImport!("wasi:cli/exit@0.2.12", "exit-with-code")
47+
pragma(mangle, "__wit_import_wasi:cli__exit@0.2.12__exit_with_code")
48+
private extern(C) void __import_exitWithCode(uint) @nogc nothrow;

0 commit comments

Comments
 (0)