-
-
Notifications
You must be signed in to change notification settings - Fork 671
Add WASI abort, trace and seed implementations #1159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3a8f936
Add a WASI abort implementation
dcodeIO 9193f5f
split up UTF8.encode
dcodeIO 481e4e2
auto-detect WASI
dcodeIO cba6a4c
implement trace and seed
dcodeIO 6cd8856
make encodeUnsafe more general
dcodeIO 7d00cdf
fix
dcodeIO 4f43eda
account for trailing \n
dcodeIO c8be2b7
actually don't, not part of a string
dcodeIO a0f221d
simplify
dcodeIO 0be8f23
cleanup
dcodeIO 9628e39
Merge branch 'master' into wasi-abort
dcodeIO 0623899
Merge branch 'master' into wasi-abort
dcodeIO 88e506d
use dedicated indicator, implicit explicitStart if wasi
dcodeIO 9894c56
split bindings and implementation, unsafe
dcodeIO 0d729cd
Merge branch 'master' into wasi-abort
dcodeIO 7226d6c
trace to fd 2 to keep stdout intact
dcodeIO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,120 @@ | ||
import { | ||
proc_exit, | ||
fd_write, | ||
iovec, | ||
random_get | ||
} from "./wasi_snapshot_preview1"; | ||
|
||
import { | ||
MAX_DOUBLE_LENGTH, | ||
decimalCount32, | ||
dtoa_stream | ||
} from "util/number"; | ||
|
||
export * from "./wasi_snapshot_preview1"; | ||
|
||
/** A WASI-aware abort implementation. */ | ||
function abort( | ||
message: string | null = null, | ||
fileName: string | null = null, | ||
lineNumber: u32 = 0, | ||
columnNumber: u32 = 0 | ||
): void { | ||
// 0: iov.buf | ||
// 4: iov.buf_len | ||
// 8: len | ||
// 12: buf... | ||
const iovPtr: usize = 0; | ||
const bufPtr: usize = iovPtr + offsetof<iovec>() + sizeof<usize>(); | ||
changetype<iovec>(iovPtr).buf = bufPtr; | ||
var ptr = bufPtr; | ||
store<u64>(ptr, 0x203A74726F6261); ptr += 7; // 'abort: ' | ||
if (message !== null) { | ||
ptr += String.UTF8.encodeUnsafe(changetype<usize>(message), message.length, ptr); | ||
} | ||
store<u32>(ptr, 0x206E6920); ptr += 4; // ' in ' | ||
if (fileName !== null) { | ||
ptr += String.UTF8.encodeUnsafe(changetype<usize>(fileName), fileName.length, ptr); | ||
} | ||
store<u8>(ptr++, 0x28); // ( | ||
var len = decimalCount32(lineNumber); ptr += len; | ||
do { | ||
let t = lineNumber / 10; | ||
store<u8>(--ptr, 0x30 + lineNumber % 10); | ||
lineNumber = t; | ||
} while (lineNumber); ptr += len; | ||
store<u8>(ptr++, 0x3A); // : | ||
len = decimalCount32(columnNumber); ptr += len; | ||
do { | ||
let t = columnNumber / 10; | ||
store<u8>(--ptr, 0x30 + columnNumber % 10); | ||
columnNumber = t; | ||
} while (columnNumber); ptr += len; | ||
store<u16>(ptr, 0x0A29); ptr += 2; // )\n | ||
changetype<iovec>(iovPtr).buf_len = ptr - bufPtr; | ||
fd_write(2, iovPtr, 1, offsetof<iovec>()); | ||
proc_exit(255); | ||
} | ||
|
||
/** A WASI-aware trace implementation. */ | ||
function trace( | ||
message: string, | ||
n: i32 = 0, | ||
a0: f64 = 0, | ||
a1: f64 = 0, | ||
a2: f64 = 0, | ||
a3: f64 = 0, | ||
a4: f64 = 0 | ||
): void { | ||
// 0: iov.buf | ||
// 4: iov.buf_len | ||
// 8: len | ||
// 12: buf... | ||
var iovPtr = __alloc(offsetof<iovec>() + sizeof<usize>() + 1 + <usize>(max(String.UTF8.byteLength(message), MAX_DOUBLE_LENGTH << 1)), 0); | ||
var lenPtr = iovPtr + offsetof<iovec>(); | ||
var bufPtr = lenPtr + sizeof<usize>(); | ||
changetype<iovec>(iovPtr).buf = bufPtr; | ||
store<u64>(bufPtr, 0x203A6563617274); // 'trace: ' | ||
changetype<iovec>(iovPtr).buf_len = 7; | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
changetype<iovec>(iovPtr).buf_len = String.UTF8.encodeUnsafe(changetype<usize>(message), message.length, bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
if (n) { | ||
store<u8>(bufPtr++, 0x20); // space | ||
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a0), bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
if (n > 1) { | ||
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a1), bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
if (n > 2) { | ||
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a2), bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
if (n > 3) { | ||
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a3), bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
if (n > 4) { | ||
changetype<iovec>(iovPtr).buf_len = 1 + String.UTF8.encodeUnsafe(bufPtr, dtoa_stream(bufPtr, 0, a4), bufPtr); | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
} | ||
} | ||
} | ||
} | ||
--bufPtr; | ||
} | ||
store<u8>(bufPtr, 0x0A); // \n | ||
changetype<iovec>(iovPtr).buf_len = 1; | ||
fd_write(1, iovPtr, 1, lenPtr); | ||
__free(iovPtr); | ||
} | ||
|
||
/** A WASI-aware seed implementation. */ | ||
function seed(): f64 { | ||
var temp = load<u64>(0); | ||
var rand: u64; | ||
do { | ||
random_get(0, 8); // to be sure | ||
rand = load<u64>(0); | ||
} while (!rand); | ||
store<u64>(0, temp); | ||
return reinterpret<f64>(rand); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.