Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
hello_args.c
Go to the documentation of this file.
1/*
2 * This file is part of the Fun programming language.
3 * https://fun-lang.xyz/
4 *
5 * Copyright 2026 Johannes Findeisen <you@hanez.org>
6 * Licensed under the terms of the Apache-2.0 license.
7 * https://opensource.org/license/apache-2-0
8 */
9
10/**
11 * @file hello_args.c
12 * @brief Implements the OP_RUST_HELLO_ARGS opcode (conditional build).
13 *
14 * Demonstrates passing a VM value to Rust. Converts the top stack value to a
15 * string, calls into Rust to print/log it, and pushes Nil. When Rust support is
16 * disabled, the argument is still popped and freed to keep the stack sane,
17 * then Nil is pushed after raising an error message.
18 * OP_RUST_HELLO_ARGS: (msg:any) -> Nil
19 *
20 * Behavior (FUN_WITH_RUST=ON):
21 * - Pops one value from the stack and converts it to a newly allocated C
22 * string via value_to_string_alloc().
23 * - Calls fun_rust_print_string(msg) and then frees the allocated buffer.
24 * - Pushes Nil.
25 *
26 * Behavior (FUN_WITH_RUST=OFF):
27 * - Pops and frees one value to preserve stack discipline.
28 * - Raises a runtime error indicating missing Rust support.
29 * - Pushes Nil.
30 */
31
33#ifdef FUN_WITH_RUST
34 Value vmsg = pop_value(vm);
35 char *msg = value_to_string_alloc(&vmsg);
37 if (msg) {
39 free(msg);
40 } else {
42 }
44#else
45 /* Still pop and free the arg to keep stack sane */
46 Value vmsg = pop_value(vm);
47 free_value(vmsg);
48 vm_raise_error(vm, "RUST_HELLO_ARGS requires FUN_WITH_RUST=ON at build time");
49 push_value(vm, make_nil());
50#endif
51 break;
52}
@ OP_RUST_HELLO_ARGS
Definition bytecode.h:279
char * msg
Definition hello_args.c:35
push_value(vm, make_nil())
free_value(vmsg)
free(vals)
Tagged union representing a Fun value.
Definition value.h:68
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
void vm_raise_error(VM *vm, const char *msg)
Raise a runtime error inside the VM, honoring try/catch/finally.
Definition vm.c:248
int fun_rust_print_string(const char *msg)