forked from M-Labs/nac3
1
0
Fork 0

standalone: Update demos

- Add `output_str` for printing a string
- Add demo_test.py to test interop
This commit is contained in:
David Mak 2023-09-21 15:30:03 +08:00
parent cb39f61e79
commit b39831b388
3 changed files with 82 additions and 3 deletions

View File

@ -1,3 +1,7 @@
use std::io;
use std::io::Write;
use std::process::exit;
mod cslice {
// copied from https://github.com/dherman/cslice
use std::marker::PhantomData;
@ -55,6 +59,14 @@ pub extern "C" fn output_asciiart(x: i32) {
}
}
#[no_mangle]
pub extern "C" fn output_str(x: &cslice::CSlice<u8>) {
for e in x.as_ref().iter() {
print!("{}", char::from(*e));
}
println!();
}
#[no_mangle]
pub extern "C" fn output_int32_list(x: &cslice::CSlice<i32>) {
print!("[");
@ -75,8 +87,14 @@ pub extern "C" fn __nac3_personality(_state: u32, _exception_object: u32, _conte
}
#[no_mangle]
pub extern "C" fn __nac3_raise(_state: u32, _exception_object: u32, _context: u32) -> u32 {
unimplemented!();
pub extern "C" fn __nac3_raise(state: u32, exception_object: u32, context: u32) -> u32 {
writeln!(io::stderr(),
"__nac3_raise(state: {:#010x}, exception_object: {:#010x}, context: {:#010x})",
state,
exception_object,
context
).unwrap();
exit(101);
}
extern "C" {

View File

@ -58,7 +58,8 @@ def patch(module):
"output_int32_list",
"output_uint32",
"output_uint64",
"output_float64"
"output_float64",
"output_str",
}:
return print
else:

View File

@ -0,0 +1,60 @@
@extern
def output_int32(x: int32):
...
@extern
def output_int64(x: int64):
...
@extern
def output_uint32(x: uint32):
...
@extern
def output_uint64(x: uint64):
...
@extern
def output_int32_list(x: list[int32]):
...
@extern
def output_asciiart(x: int32):
...
@extern
def output_str(x: str):
...
def test_output_int32():
output_int32(-128)
def test_output_int64():
output_int64(int64(-256))
def test_output_uint32():
output_uint32(uint32(128))
def test_output_uint64():
output_uint64(uint64(256))
def test_output_asciiart():
for i in range(17):
output_asciiart(i)
output_asciiart(0)
def test_output_int32_list():
output_int32_list([0, 1, 3, 5, 10])
def test_output_str_family():
output_str("hello world")
def run() -> int32:
test_output_int32()
test_output_int64()
test_output_uint32()
test_output_uint64()
test_output_asciiart()
test_output_int32_list()
test_output_str_family()
return 0