From b39831b388965ef59b3d4c0597f6f7b4bdbb61e3 Mon Sep 17 00:00:00 2001 From: David Mak Date: Thu, 21 Sep 2023 15:30:03 +0800 Subject: [PATCH] standalone: Update demos - Add `output_str` for printing a string - Add demo_test.py to test interop --- nac3standalone/demo/demo.rs | 22 +++++++++- nac3standalone/demo/interpret_demo.py | 3 +- nac3standalone/demo/src/demo_test.py | 60 +++++++++++++++++++++++++++ 3 files changed, 82 insertions(+), 3 deletions(-) create mode 100644 nac3standalone/demo/src/demo_test.py diff --git a/nac3standalone/demo/demo.rs b/nac3standalone/demo/demo.rs index c7e445271..06b1b51ae 100644 --- a/nac3standalone/demo/demo.rs +++ b/nac3standalone/demo/demo.rs @@ -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) { + for e in x.as_ref().iter() { + print!("{}", char::from(*e)); + } + println!(); +} + #[no_mangle] pub extern "C" fn output_int32_list(x: &cslice::CSlice) { 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" { diff --git a/nac3standalone/demo/interpret_demo.py b/nac3standalone/demo/interpret_demo.py index 71bb4264b..dcee20022 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -58,7 +58,8 @@ def patch(module): "output_int32_list", "output_uint32", "output_uint64", - "output_float64" + "output_float64", + "output_str", }: return print else: diff --git a/nac3standalone/demo/src/demo_test.py b/nac3standalone/demo/src/demo_test.py new file mode 100644 index 000000000..3a99887a8 --- /dev/null +++ b/nac3standalone/demo/src/demo_test.py @@ -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 \ No newline at end of file