Some more Fun VM and Rust interaction. (0.38.2)
This commit is contained in:
parent
543483041b
commit
c2e7f3869e
17 changed files with 251 additions and 46 deletions
26
src/vm/rust/get_sp.c
Normal file
26
src/vm/rust/get_sp.c
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-29
|
||||
*/
|
||||
|
||||
/*
|
||||
* Rust FFI demo opcode: OP_RUST_GET_SP
|
||||
* Calls into Rust to read vm.sp via raw pointer math and pushes it (int).
|
||||
*/
|
||||
case OP_RUST_GET_SP: {
|
||||
#ifdef FUN_WITH_RUST
|
||||
extern int fun_op_rget_sp(VM *vm);
|
||||
int rc = fun_op_rget_sp(vm);
|
||||
(void)rc; /* rc currently unused; 0 means OK */
|
||||
#else
|
||||
vm_raise_error(vm, "RUST_GET_SP requires FUN_WITH_RUST=ON at build time");
|
||||
push_value(vm, make_int(-1));
|
||||
#endif
|
||||
break;
|
||||
}
|
||||
|
|
@ -2,7 +2,7 @@
|
|||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
|
|
@ -25,29 +25,3 @@ 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;
|
||||
}
|
||||
|
|
|
|||
36
src/vm/rust/hello_args.c
Normal file
36
src/vm/rust/hello_args.c
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-29
|
||||
*/
|
||||
|
||||
/*
|
||||
* 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;
|
||||
}
|
||||
33
src/vm/rust/set_exit.c
Normal file
33
src/vm/rust/set_exit.c
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
/*
|
||||
* This file is part of the Fun programming language.
|
||||
* https://fun-lang.xyz/
|
||||
*
|
||||
* Copyright 2026 Johannes Findeisen <you@hanez.org>
|
||||
* Licensed under the terms of the Apache-2.0 license.
|
||||
* https://opensource.org/license/apache-2-0
|
||||
*
|
||||
* Added: 2026-01-29
|
||||
*/
|
||||
|
||||
/*
|
||||
* Rust FFI demo opcode: OP_RUST_SET_EXIT
|
||||
* Pops an int code and asks Rust to write it into vm.exit_code.
|
||||
*/
|
||||
case OP_RUST_SET_EXIT: {
|
||||
#ifdef FUN_WITH_RUST
|
||||
extern int fun_op_rset_exit(VM *vm);
|
||||
/* Expect an integer on the stack already (produced by prior ops) */
|
||||
/* fun_op_rset_exit will pop it and store into vm.exit_code */
|
||||
int rc = fun_op_rset_exit(vm);
|
||||
(void)rc;
|
||||
/* push Nil as a conventional result */
|
||||
push_value(vm, make_nil());
|
||||
#else
|
||||
/* pop and ignore to keep stack sane */
|
||||
Value v = pop_value(vm);
|
||||
free_value(v);
|
||||
vm_raise_error(vm, "RUST_SET_EXIT 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