Skip to content

Commit 86f9838

Browse files
druntime: Use WASIp2 directly instead of POSIX emulation
1 parent 1c412ea commit 86f9838

59 files changed

Lines changed: 6919 additions & 153 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
@@ -54,63 +54,21 @@ void abort(scope string msg, scope string filename = __FILE__, size_t line = __L
5454
cast(void)fdWrite(2, iovecs[], bytesWritten);
5555
}
5656
}
57-
} else version (WASIp2) {
58-
// currently depends on wasi-libc being linked in to provide these "syscalls"
59-
// TODO: detach this from wasi-libc
60-
extern(C) struct wasip2_list_u8_t {
61-
ubyte* ptr;
62-
size_t len;
63-
}
64-
extern(C) struct streams_own_output_stream_t {
65-
int __handle;
66-
}
67-
extern(C) struct streams_borrow_output_stream_t {
68-
int __handle;
69-
}
70-
alias streams_own_output_stream_t stderr_own_output_stream_t;
71-
extern(C) struct io_error_own_error_t {
72-
int __handle;
73-
}
74-
alias io_error_own_error_t streams_own_error_t;
75-
76-
extern(C) struct streams_stream_error_t {
77-
ubyte tag;
78-
79-
union Val {
80-
streams_own_error_t last_operation_failed;
81-
}
82-
Val val;
83-
}
84-
85-
pragma(mangle, "streams_output_stream_drop_own")
86-
extern(C) static void streams_output_stream_drop_own(streams_own_output_stream_t handle) @nogc nothrow;
87-
pragma(mangle, "streams_borrow_output_stream")
88-
extern(C) static streams_borrow_output_stream_t streams_borrow_output_stream(streams_own_output_stream_t handle) @nogc nothrow;
89-
pragma(mangle, "stderr_get_stderr")
90-
extern(C) static stderr_own_output_stream_t stderr_get_stderr() @nogc nothrow;
91-
pragma(mangle, "io_error_error_drop_own")
92-
extern(C) static void io_error_error_drop_own(io_error_own_error_t handle) @nogc nothrow;
93-
pragma(mangle, "streams_stream_error_free")
94-
extern(C) static void streams_stream_error_free(streams_stream_error_t *ptr) @nogc nothrow;
95-
pragma(mangle, "streams_method_output_stream_blocking_write_and_flush")
96-
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;
97-
57+
}
58+
else version (WASIp2)
59+
{
60+
import core.sys.wasi.p2.cli.stderr.imports : getStderr;
61+
import core.sys.wasi.wit_common : witFree, witList;
9862
static void writeStr(scope const(char)[][] m...) @nogc nothrow @trusted
9963
{
100-
auto stderr_own = stderr_get_stderr();
101-
scope(exit) streams_output_stream_drop_own(stderr_own);
102-
103-
auto stderr = streams_borrow_output_stream(stderr_own);
64+
auto stderr = getStderr();
65+
scope(exit) stderr.drop;
10466

10567
foreach (s; m) {
106-
wasip2_list_u8_t contents;
107-
contents.ptr = cast(ubyte*)s.ptr;
108-
contents.len = s.length;
109-
streams_stream_error_t err;
110-
bool success = streams_method_output_stream_blocking_write_and_flush(stderr, &contents, &err);
111-
if (!success) {
112-
streams_stream_error_free(&err);
113-
}
68+
auto result = stderr.blockingWriteAndFlush((cast(const(ubyte)[])s).witList);
69+
scope(exit) result.witFree;
70+
71+
// ignore errors
11472
}
11573
}
11674
}
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)