![]() | Fun 0.41.5 The programming language that makes You have fun |
Core virtual machine data structures and public VM API. More...


Go to the source code of this file.
Data Structures | |
| struct | Frame |
| Call frame representing one active function invocation. More... | |
| struct | VM |
| The Fun virtual machine state. More... | |
Macros | |
| #define | MAX_FRAMES 128 |
| #define | MAX_FRAME_LOCALS 64 |
| #define | MAX_GLOBALS 128 |
| #define | OUTPUT_SIZE 1024 |
| #define | STACK_SIZE 1024 |
Typedefs | |
| typedef struct VM | VM |
| Opaque VM alias for external users. | |
Functions | |
| void | vm_init (VM *vm) |
| Initialize a VM instance to zero/initial state. | |
| void | vm_clear_output (VM *vm) |
| Clear the buffered output captured by the VM. | |
| void | vm_print_output (VM *vm) |
| Print buffered output entries to stdout (debug aid). | |
| void | vm_free (VM *vm) |
| Free all resources owned by the VM (globals, frames, output buffers). The VM object itself is not freed when allocated on the stack. | |
| void | vm_reset (VM *vm) |
| Reset VM to initial state, freeing globals/locals/output. The VM object remains valid for reuse after this call. | |
| void | vm_dump_globals (VM *vm) |
| Print non-nil globals (index and value) to stdout. | |
| void | vm_run (VM *vm, Bytecode *entry) |
| Execute the provided entry bytecode in the VM. Pushes an initial frame and runs until HALT or an unrecoverable error. | |
| void | vm_raise_error (VM *vm, const char *msg) |
| Raise a runtime error honoring active try/catch/finally handlers. If a try handler is active in the current frame, control jumps to it with an error string pushed on the stack. Otherwise, prints the error (with location) and terminates execution. | |
| void | vm_debug_reset (VM *vm) |
| Reset debugger state: breakpoints and stepping controls. | |
| int | vm_debug_add_breakpoint (VM *vm, const char *file, int line) |
| Add a source breakpoint. | |
| int | vm_debug_delete_breakpoint (VM *vm, int id) |
| Delete a breakpoint by id. | |
| void | vm_debug_clear_breakpoints (VM *vm) |
| Remove all breakpoints from the VM. | |
| void | vm_debug_list_breakpoints (VM *vm) |
| Print active breakpoints to stdout. | |
| void | vm_debug_request_step (VM *vm) |
| Request single-step execution (stop after next instruction). | |
| void | vm_debug_request_next (VM *vm) |
| Request step-over (stop after next instruction in current frame). | |
| void | vm_debug_request_finish (VM *vm) |
| Request finish (run until the current frame returns). | |
| void | vm_debug_request_continue (VM *vm) |
| Resume normal execution (clear stepping state and stop flag). | |
| int64_t | vm_pop_i64 (VM *vm) |
| Pop a numeric Value and convert it to a 64-bit integer (C ABI helper). | |
| void | vm_push_i64 (VM *vm, int64_t v) |
| Push a 64-bit integer as a VM int Value (C ABI helper). | |
| int | fun_op_radd (VM *vm) |
| const char * | fun_rust_get_string (void) |
| int | fun_rust_print_string (const char *msg) |
| char * | fun_rust_echo_string (const char *input) |
| void | fun_rust_string_free (char *ptr) |
| int | fun_op_cpp_add (struct VM *vm) |
| Add two 64-bit integers from the VM stack and push the sum. | |
| size_t | vm_sizeof (void) |
| Return sizeof(VM) for external FFI consumers. | |
| size_t | vm_value_sizeof (void) |
| Return sizeof(Value) for external FFI consumers. | |
| void * | vm_as_mut_ptr (VM *vm) |
| Get a mutable byte pointer to the VM object. Extremely unsafe; for low-level FFI use only. | |
| size_t | vm_offset_of_exit_code (void) |
| Obtain offsetof(VM, exit_code) for FFI struct field access. | |
| size_t | vm_offset_of_sp (void) |
| Obtain offsetof(VM, sp) for FFI struct field access. | |
| size_t | vm_offset_of_stack (void) |
| Obtain offsetof(VM, stack) for FFI struct field access. | |
| size_t | vm_offset_of_globals (void) |
| Obtain offsetof(VM, globals) for FFI struct field access. | |
| int | fun_op_rget_sp (VM *vm) |
| int | fun_op_rset_exit (VM *vm) |
Core virtual machine data structures and public VM API.
Declares the execution stack/frame layout, global state of the Fun VM, human-readable opcode names for diagnostics, and the public functions for initializing, running, resetting, and debugging the VM. FFI helper declarations for Rust/C++ experiments are also exposed here.
Definition in file vm.h.
| int fun_op_cpp_add | ( | VM * | vm | ) |
Add two 64-bit integers from the VM stack and push the sum.
C++ demo opcode entry point (C ABI).
Pops two values from the VM stack using vm_pop_i64, adds them as 64-bit signed integers, and pushes the result via vm_push_i64.
Stack effect:
| vm | Pointer to the VM instance. Must not be NULL. |
Definition at line 47 of file add.cpp.

| int fun_op_radd | ( | VM * | vm | ) |
Example Rust-implemented opcode (adds top two ints on stack).

| int fun_op_rget_sp | ( | VM * | vm | ) |
| int fun_op_rset_exit | ( | VM * | vm | ) |
| char * fun_rust_echo_string | ( | const char * | input | ) |
Return a newly allocated duplicate of the input C string (caller frees).
| const char * fun_rust_get_string | ( | void | ) |
Return a demo null-terminated C string owned by Rust.

| int fun_rust_print_string | ( | const char * | msg | ) |
Print a C string via Rust; returns 0 on success.
| void fun_rust_string_free | ( | char * | ptr | ) |
Free a C string previously returned by fun_rust_echo_string().
| void * vm_as_mut_ptr | ( | VM * | vm | ) |
| void vm_clear_output | ( | VM * | vm | ) |
Clear the buffered output captured by the VM.
| vm | VM instance. |
Clear the buffered output captured by the VM.
Frees any dynamic storage held by buffered output Values and resets the output counters and partial line indicators.
| vm | VM instance whose output buffer should be cleared. |
Definition at line 349 of file vm.c.


| int vm_debug_add_breakpoint | ( | VM * | vm, |
| const char * | file, | ||
| int | line ) |
Add a source breakpoint.
Add a breakpoint at file:line; returns non-negative id on success or -1.
| vm | VM instance. |
| file | Source file path (must not be NULL). |
| line | One-based source line number (> 0). |
| void vm_debug_clear_breakpoints | ( | VM * | vm | ) |
| int vm_debug_delete_breakpoint | ( | VM * | vm, |
| int | id ) |
Delete a breakpoint by id.
Delete a breakpoint by id; returns 1 on success, 0 on failure.
Compacts the internal breakpoint list to keep ids dense.
| vm | VM instance. |
| id | Breakpoint identifier previously returned by add. |
Definition at line 472 of file vm.c.

| void vm_debug_list_breakpoints | ( | VM * | vm | ) |
| void vm_debug_request_continue | ( | VM * | vm | ) |
| void vm_debug_request_finish | ( | VM * | vm | ) |
| void vm_debug_request_next | ( | VM * | vm | ) |
| void vm_debug_request_step | ( | VM * | vm | ) |
| void vm_debug_reset | ( | VM * | vm | ) |
Reset debugger state: breakpoints and stepping controls.
Reset debugger state (clear step mode and breakpoints).
Clears all breakpoints, disables stepping modes, and resets counters.
| vm | VM instance with debugger state to reset. |
Definition at line 429 of file vm.c.


| void vm_dump_globals | ( | VM * | vm | ) |
| void vm_free | ( | VM * | vm | ) |
Free all resources owned by the VM (globals, frames, output buffers). The VM object itself is not freed when allocated on the stack.
| vm | VM instance to dispose. |
Free all resources owned by the VM (globals, frames, output buffers). The VM object itself is not freed when allocated on the stack.
Currently a no-op because the VM does not allocate persistent internal resources outside frames, globals and outputs, which are managed elsewhere.
| vm | VM instance to free resources for. |
Definition at line 367 of file vm.c.

| void vm_init | ( | VM * | vm | ) |
Initialize a VM instance to zero/initial state.
| vm | Non-NULL pointer to VM storage to initialize. |
Initialize a VM instance to zero/initial state.
Resets stack/frame pointers, output buffers, instruction counters, debugger state and globals. Does not allocate memory.
| vm | VM instance to initialize. |
Definition at line 714 of file vm.c.


| size_t vm_offset_of_exit_code | ( | void | ) |
| size_t vm_offset_of_globals | ( | void | ) |
| size_t vm_offset_of_sp | ( | void | ) |
| size_t vm_offset_of_stack | ( | void | ) |
| int64_t vm_pop_i64 | ( | VM * | vm | ) |
Pop a numeric Value and convert it to a 64-bit integer (C ABI helper).
Pop an int64 from the VM stack; accepts int/float; returns truncated value.
Accepts int or float Values on the stack. Other types raise a runtime type error. The popped Value is freed.
| vm | VM instance. |
Definition at line 598 of file vm.c.


| void vm_print_output | ( | VM * | vm | ) |
Print buffered output entries to stdout (debug aid).
| vm | VM instance. |
Print buffered output entries to stdout (debug aid).
Emits a newline after each value unless the corresponding partial flag is set.
| vm | VM instance whose output should be printed. |
Definition at line 797 of file vm.c.


| void vm_push_i64 | ( | VM * | vm, |
| int64_t | v ) |
| void vm_raise_error | ( | VM * | vm, |
| const char * | msg ) |
Raise a runtime error honoring active try/catch/finally handlers. If a try handler is active in the current frame, control jumps to it with an error string pushed on the stack. Otherwise, prints the error (with location) and terminates execution.
| vm | VM instance. |
| msg | Null-terminated error message. |
Raise a runtime error honoring active try/catch/finally handlers. If a try handler is active in the current frame, control jumps to it with an error string pushed on the stack. Otherwise, prints the error (with location) and terminates execution.
If the current frame has a pending try handler, control is transferred to that handler and the error message is pushed onto the stack for the catch clause. If no handler exists, the error is printed and the VM is stopped.
| vm | Pointer to the VM instance. |
| msg | Human-readable error message (may be NULL). |
Definition at line 248 of file vm.c.


| void vm_reset | ( | VM * | vm | ) |
Reset VM to initial state, freeing globals/locals/output. The VM object remains valid for reuse after this call.
| vm | VM instance to reset. |
Reset VM to initial state, freeing globals/locals/output. The VM object remains valid for reuse after this call.
Pops all frames (releasing local variables), clears the operand stack and globals, resets the output buffer and debugger state, and zeros the exit code.
| vm | VM instance to reset. |
Definition at line 382 of file vm.c.

| size_t vm_sizeof | ( | void | ) |