Some arg Fun in Rust. (0.38.1)
This commit is contained in:
parent
87833b0c1e
commit
543483041b
16 changed files with 167 additions and 66 deletions
|
|
@ -207,6 +207,7 @@ static const char *opcode_name(OpCode op) {
|
|||
case OP_FMIN: return "FMIN";
|
||||
case OP_FMAX: return "FMAX";
|
||||
case OP_RUST_HELLO: return "RUST_HELLO";
|
||||
case OP_RUST_HELLO_ARGS: return "RUST_HELLO_ARGS";
|
||||
default: return "???";
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -265,6 +265,7 @@ typedef enum {
|
|||
|
||||
// Rust FFI demo opcode(s)
|
||||
OP_RUST_HELLO, // pushes string returned from Rust (hello world)
|
||||
OP_RUST_HELLO_ARGS, // pops message string; prints it via Rust; pushes Nil
|
||||
|
||||
/* Notcurses TUI (optional) */
|
||||
OP_NC_INIT, // initializes Notcurses; returns 1 on success, 0 on failure
|
||||
|
|
|
|||
|
|
@ -793,6 +793,14 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
|
|||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "rust_hello_args") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "rust_hello_args expects (message:string)"); free(name); return 0; }
|
||||
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "Expected ')' after rust_hello_args arg"); free(name); return 0; }
|
||||
bytecode_add_instruction(bc, OP_RUST_HELLO_ARGS, 0);
|
||||
free(name);
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(name, "os_list_dir") == 0) {
|
||||
(*pos)++; /* '(' */
|
||||
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "os_list_dir expects (path)"); free(name); return 0; }
|
||||
|
|
|
|||
|
|
@ -6,6 +6,9 @@ edition = "2021"
|
|||
[lib]
|
||||
crate-type = ["staticlib"]
|
||||
|
||||
[profile.dev]
|
||||
panic = "abort"
|
||||
|
||||
[profile.release]
|
||||
panic = "abort"
|
||||
# Optimize for minimal size
|
||||
|
|
|
|||
2
src/rust/eh_personality.c
Normal file
2
src/rust/eh_personality.c
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
/* Workaround for linking Rust staticlib in no_std/abort mode on some toolchains */
|
||||
void rust_eh_personality(void) {}
|
||||
|
|
@ -11,8 +11,8 @@
|
|||
|
||||
#![no_std]
|
||||
|
||||
#[repr(C)]
|
||||
pub struct Vm;
|
||||
// Use an FFI-safe opaque handle for the VM pointer
|
||||
pub type Vm = core::ffi::c_void;
|
||||
|
||||
extern "C" {
|
||||
fn vm_pop_i64(vm: *mut Vm) -> i64;
|
||||
|
|
@ -37,6 +37,23 @@ pub extern "C" fn fun_rust_get_string() -> *const core::ffi::c_char {
|
|||
b"Hello from Rust ops!\0".as_ptr() as *const _
|
||||
}
|
||||
|
||||
// Print a C string via libc printf to stdout (acceptable under no_std via extern)
|
||||
#[no_mangle]
|
||||
pub extern "C" fn fun_rust_print_string(msg: *const core::ffi::c_char) -> i32 {
|
||||
unsafe {
|
||||
extern "C" {
|
||||
fn printf(fmt: *const core::ffi::c_char, ...) -> i32;
|
||||
}
|
||||
let fmt = b"%s\n\0".as_ptr() as *const core::ffi::c_char;
|
||||
printf(fmt, msg);
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
// Provide personality symbol to satisfy some linkers in no_std builds
|
||||
#[no_mangle]
|
||||
pub extern "C" fn rust_eh_personality() {}
|
||||
|
||||
// Minimal panic handler for no_std; abort behavior requested via Cargo profile
|
||||
#[panic_handler]
|
||||
fn panic(_info: &core::panic::PanicInfo) -> ! {
|
||||
|
|
|
|||
4
src/vm.h
4
src/vm.h
|
|
@ -56,7 +56,7 @@ static const char *opcode_names[] = {
|
|||
"TRY_PUSH","TRY_POP","THROW",
|
||||
"FMIN","FMAX",
|
||||
/* Rust FFI demo */
|
||||
"RUST_HELLO",
|
||||
"RUST_HELLO","RUST_HELLO_ARGS",
|
||||
/* Notcurses TUI (optional) */
|
||||
"NC_INIT","NC_SHUTDOWN","NC_CLEAR","NC_DRAW_TEXT","NC_GETCH"
|
||||
};
|
||||
|
|
@ -157,5 +157,7 @@ int fun_op_radd(VM *vm);
|
|||
|
||||
/* Example Rust function returning a demo C string (null-terminated). */
|
||||
const char *fun_rust_get_string(void);
|
||||
/* Rust function that prints a passed C string, returns 0 on success. */
|
||||
int fun_rust_print_string(const char *msg);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -25,3 +25,29 @@ case OP_RUST_HELLO: {
|
|||
#endif
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Rust FFI demo opcode with argument: OP_RUST_HELLO_ARGS
|
||||
* Pops a string from the stack and asks Rust to print it. Pushes Nil.
|
||||
*/
|
||||
case OP_RUST_HELLO_ARGS: {
|
||||
#ifdef FUN_WITH_RUST
|
||||
Value vmsg = pop_value(vm);
|
||||
char *msg = value_to_string_alloc(&vmsg);
|
||||
free_value(vmsg);
|
||||
if (msg) {
|
||||
fun_rust_print_string(msg);
|
||||
free(msg);
|
||||
} else {
|
||||
fun_rust_print_string("");
|
||||
}
|
||||
push_value(vm, make_nil());
|
||||
#else
|
||||
/* Still pop and free the arg to keep stack sane */
|
||||
Value vmsg = pop_value(vm);
|
||||
free_value(vmsg);
|
||||
vm_raise_error(vm, "RUST_HELLO_ARGS requires FUN_WITH_RUST=ON at build time");
|
||||
push_value(vm, make_nil());
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue