Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
hello.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.c
12 * @brief Implements the OP_RUST_HELLO opcode (conditional build).
13 *
14 * This opcode demonstrates calling into a Rust function from the VM. When
15 * FUN_WITH_RUST is enabled at build time, it retrieves a greeting string from
16 * Rust and pushes it as a VAL_STRING onto the VM stack. When Rust support is
17 * disabled, a runtime error is raised and Nil is pushed to keep stack
18 * consistency.
19 *
20 * OP_RUST_HELLO: () -> string | Nil
21 *
22 * Behavior (FUN_WITH_RUST=ON):
23 * - Does not pop any values.
24 * - Calls fun_rust_get_string() and pushes the returned C string as a
25 * VAL_STRING. If Rust returns NULL, an empty string is used instead.
26 *
27 * Behavior (FUN_WITH_RUST=OFF):
28 * - Raises a runtime error indicating missing Rust support.
29 * - Pushes Nil to maintain stack discipline.
30 */
31
33#ifdef FUN_WITH_RUST
34 const char *s = fun_rust_get_string();
35 if (!s) s = "";
37#else
38 vm_raise_error(vm, "RUST_HELLO requires FUN_WITH_RUST=ON at build time");
39 push_value(vm, make_nil());
40#endif
41 break;
42}
@ OP_RUST_HELLO
Definition bytecode.h:278
push_value(vm, make_string(s))
uint32_t s
Definition rol.c:31
Value make_nil(void)
Construct a nil Value.
Definition value.c:126
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
void vm_raise_error(VM *vm, const char *msg)
Raise a runtime error inside the VM, honoring try/catch/finally.
Definition vm.c:248
const char * fun_rust_get_string(void)