Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 11 additions & 53 deletions druntime/src/core/internal/abort.d
Original file line number Diff line number Diff line change
Expand Up @@ -54,63 +54,21 @@ void abort(scope string msg, scope string filename = __FILE__, size_t line = __L
cast(void)fdWrite(2, iovecs[], bytesWritten);
}
}
} else version (WASIp2) {
// currently depends on wasi-libc being linked in to provide these "syscalls"
// TODO: detach this from wasi-libc
extern(C) struct wasip2_list_u8_t {
ubyte* ptr;
size_t len;
}
extern(C) struct streams_own_output_stream_t {
int __handle;
}
extern(C) struct streams_borrow_output_stream_t {
int __handle;
}
alias streams_own_output_stream_t stderr_own_output_stream_t;
extern(C) struct io_error_own_error_t {
int __handle;
}
alias io_error_own_error_t streams_own_error_t;

extern(C) struct streams_stream_error_t {
ubyte tag;

union Val {
streams_own_error_t last_operation_failed;
}
Val val;
}

pragma(mangle, "streams_output_stream_drop_own")
extern(C) static void streams_output_stream_drop_own(streams_own_output_stream_t handle) @nogc nothrow;
pragma(mangle, "streams_borrow_output_stream")
extern(C) static streams_borrow_output_stream_t streams_borrow_output_stream(streams_own_output_stream_t handle) @nogc nothrow;
pragma(mangle, "stderr_get_stderr")
extern(C) static stderr_own_output_stream_t stderr_get_stderr() @nogc nothrow;
pragma(mangle, "io_error_error_drop_own")
extern(C) static void io_error_error_drop_own(io_error_own_error_t handle) @nogc nothrow;
pragma(mangle, "streams_stream_error_free")
extern(C) static void streams_stream_error_free(streams_stream_error_t *ptr) @nogc nothrow;
pragma(mangle, "streams_method_output_stream_blocking_write_and_flush")
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;

}
else version (WASIp2)
{
import core.sys.wasi.p2.cli.stderr.imports : getStderr;
import core.sys.wasi.wit_common : witFree, witList;
static void writeStr(scope const(char)[][] m...) @nogc nothrow @trusted
{
auto stderr_own = stderr_get_stderr();
scope(exit) streams_output_stream_drop_own(stderr_own);

auto stderr = streams_borrow_output_stream(stderr_own);
auto stderr = getStderr();
scope(exit) stderr.drop;

foreach (s; m) {
wasip2_list_u8_t contents;
contents.ptr = cast(ubyte*)s.ptr;
contents.len = s.length;
streams_stream_error_t err;
bool success = streams_method_output_stream_blocking_write_and_flush(stderr, &contents, &err);
if (!success) {
streams_stream_error_free(&err);
}
auto result = stderr.blockingWriteAndFlush((cast(const(ubyte)[])s).witList);
scope(exit) result.witFree;

// ignore errors
}
}
}
Expand Down
11 changes: 11 additions & 0 deletions druntime/src/core/sys/wasi/p2/cli/environment/common.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/++

+/
module core.sys.wasi.p2.cli.environment.common;

version(WASIp2):

import core.sys.wasi.wit_common;


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();
99 changes: 99 additions & 0 deletions druntime/src/core/sys/wasi/p2/cli/environment/imports.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/++

+/
module core.sys.wasi.p2.cli.environment.imports;

version(WASIp2):

import core.sys.wasi.wit_common;

public import core.sys.wasi.p2.cli.environment.common;


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();

/++
Get the POSIX-style environment variables.

Each environment variable is provided as a pair of string variable names
and string value.

Morally, these are a value import, but until value imports are available
in the component model, this import function should return the same
values each time it is called.
+/
WitList!(Tuple!(WitString, WitString)) getEnvironment() @nogc nothrow {
align(size_t.sizeof) void[(2*size_t.sizeof)] _retArea = void;
__import_getEnvironment(_retArea.ptr);
auto _listSrcPtr4 = *(cast(void**)(_retArea.ptr + 0));
auto _listLen4 = *(cast(size_t*)(_retArea.ptr + size_t.sizeof));
auto _list4 = core.sys.wasi.wit_common.mallocSlice!(Tuple!(WitString, WitString))(_listLen4);
foreach (_elem0_idx, ref _elem0; _list4) {
const auto _base0 = _listSrcPtr4 + _elem0_idx * (4*size_t.sizeof);
auto _ptr1 = cast(char*)(*(cast(void**)(_base0 + 0)));
auto _len1 = *(cast(size_t*)(_base0 + size_t.sizeof));
auto _ptr2 = cast(char*)(*(cast(void**)(_base0 + (2*size_t.sizeof))));
auto _len2 = *(cast(size_t*)(_base0 + (3*size_t.sizeof)));
auto _tuple3 = Tuple!(WitString, WitString)(
WitString(_ptr1[0.._len1]),
WitString(_ptr2[0.._len2]),
);
_elem0 = _tuple3;
}
core.sys.wasi.wit_common.free(_listSrcPtr4);
auto _flush5 = WitList!(Tuple!(WitString, WitString))(_list4);
return _flush5;
}
/// ditto
@wasmImport!("wasi:cli/environment@0.2.12", "get-environment")
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__get_environment")
private extern(C) void __import_getEnvironment(void*) @nogc nothrow;

/++
Get the POSIX-style arguments to the program.
+/
WitList!(WitString) getArguments() @nogc nothrow {
align(size_t.sizeof) void[(2*size_t.sizeof)] _retArea = void;
__import_getArguments(_retArea.ptr);
auto _listSrcPtr2 = *(cast(void**)(_retArea.ptr + 0));
auto _listLen2 = *(cast(size_t*)(_retArea.ptr + size_t.sizeof));
auto _list2 = core.sys.wasi.wit_common.mallocSlice!(WitString)(_listLen2);
foreach (_elem0_idx, ref _elem0; _list2) {
const auto _base0 = _listSrcPtr2 + _elem0_idx * (2*size_t.sizeof);
auto _ptr1 = cast(char*)(*(cast(void**)(_base0 + 0)));
auto _len1 = *(cast(size_t*)(_base0 + size_t.sizeof));
_elem0 = WitString(_ptr1[0.._len1]);
}
core.sys.wasi.wit_common.free(_listSrcPtr2);
auto _flush3 = WitList!(WitString)(_list2);
return _flush3;
}
/// ditto
@wasmImport!("wasi:cli/environment@0.2.12", "get-arguments")
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__get_arguments")
private extern(C) void __import_getArguments(void*) @nogc nothrow;

/++
Return a path that programs should use as their initial current working
directory, interpreting `.` as shorthand for this.
+/
Option!(WitString) initialCwd() @nogc nothrow {
align(size_t.sizeof) void[(3*size_t.sizeof)] _retArea = void;
__import_initialCwd(_retArea.ptr);
Option!(WitString) _option3 = void;
bool _isSome3 = (cast(uint)(*(cast(ubyte*)(_retArea.ptr + 0)))) != 0;
if (_isSome3) {
auto _ptr2 = cast(char*)(*(cast(void**)(_retArea.ptr + size_t.sizeof)));
auto _len2 = *(cast(size_t*)(_retArea.ptr + (2*size_t.sizeof)));

_option3 = Option!(WitString).some(WitString(_ptr2[0.._len2]));
} else {
_option3 = Option!(WitString).none;
}
auto _flush4 = _option3;
return _flush4;
}
/// ditto
@wasmImport!("wasi:cli/environment@0.2.12", "initial-cwd")
pragma(mangle, "__wit_import_wasi:cli__environment@0.2.12__initial_cwd")
private extern(C) void __import_initialCwd(void*) @nogc nothrow;
11 changes: 11 additions & 0 deletions druntime/src/core/sys/wasi/p2/cli/exit/common.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/++

+/
module core.sys.wasi.p2.cli.exit.common;

version(WASIp2):

import core.sys.wasi.wit_common;


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();
50 changes: 50 additions & 0 deletions druntime/src/core/sys/wasi/p2/cli/exit/imports.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/++

+/
module core.sys.wasi.p2.cli.exit.imports;

version(WASIp2):

import core.sys.wasi.wit_common;

public import core.sys.wasi.p2.cli.exit.common;


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();

/++
Exit the current instance and any linked instances.
+/
void exit(in Result!(void, void) status) @nogc nothrow {
uint _resultPart4;
if (status.isErr) {

_resultPart4 = 1;
} else {

_resultPart4 = 0;
}
__import_exit(_resultPart4);
}
/// ditto
@wasmImport!("wasi:cli/exit@0.2.12", "exit")
pragma(mangle, "__wit_import_wasi:cli__exit@0.2.12__exit")
private extern(C) void __import_exit(uint) @nogc nothrow;

/++
Exit the current instance and any linked instances, reporting the
specified status code to the host.

The meaning of the code depends on the context, with 0 usually meaning
"success", and other values indicating various types of failure.

This function does not return; the effect is analogous to a trap, but
without the connotation that something bad has happened.
+/
void exitWithCode(ubyte statusCode) @nogc nothrow {
__import_exitWithCode(cast(uint)(statusCode));
}
/// ditto
@wasmImport!("wasi:cli/exit@0.2.12", "exit-with-code")
pragma(mangle, "__wit_import_wasi:cli__exit@0.2.12__exit_with_code")
private extern(C) void __import_exitWithCode(uint) @nogc nothrow;
Loading
Loading