From 4abcb221832fd6da8e04246faac015189ecfab40 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 | 11 ++++- nac3standalone/demo/src/demo_test.py | 60 +++++++++++++++++++++++++++ 3 files changed, 89 insertions(+), 4 deletions(-) create mode 100644 nac3standalone/demo/src/demo_test.py diff --git a/nac3standalone/demo/demo.rs b/nac3standalone/demo/demo.rs index c7e4452..06b1b51 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 71bb426..3661888 100755 --- a/nac3standalone/demo/interpret_demo.py +++ b/nac3standalone/demo/interpret_demo.py @@ -48,6 +48,12 @@ def patch(module): else: sys.stdout.write(" .,-:;i+hHM$*#@ "[x]) + def output(x, newline: bool=True): + if newline: + print(x) + else: + print(x, end="") + def extern(fun): name = fun.__name__ if name == "output_asciiart": @@ -58,9 +64,10 @@ def patch(module): "output_int32_list", "output_uint32", "output_uint64", - "output_float64" + "output_float64", + "output_str", }: - return print + return output else: raise NotImplementedError diff --git a/nac3standalone/demo/src/demo_test.py b/nac3standalone/demo/src/demo_test.py new file mode 100644 index 0000000..3a99887 --- /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