![]() |
Fun 0.41.5
The programming language that makes you have fun!
|
Core virtual machine implementation and opcode dispatch for Fun. More...
#include <math.h>#include <stdarg.h>#include <stddef.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <time.h>#include "iter.c"#include "map.c"#include "string.c"#include "value.h"#include "vm.h"#include "extensions/curl.c"#include "extensions/ini.c"#include "vm/ini/handles.c"#include "extensions/json.c"#include "extensions/openssl.c"#include "extensions/pcre2.c"#include "extensions/pcsc.c"#include "extensions/sqlite.c"#include "extensions/xml2.c"#include "vm/os/thread_common.c"#include <setjmp.h>#include "vm/arithmetic/add.c"#include "vm/arithmetic/div.c"#include "vm/arithmetic/mul.c"#include "vm/arithmetic/sub.c"#include "vm/arrays/apop.c"#include "vm/arrays/clear.c"#include "vm/arrays/contains.c"#include "vm/arrays/enumerate.c"#include "vm/arrays/index_get.c"#include "vm/arrays/index_of.c"#include "vm/arrays/index_set.c"#include "vm/arrays/insert.c"#include "vm/arrays/join.c"#include "vm/arrays/make_array.c"#include "vm/arrays/push.c"#include "vm/arrays/remove.c"#include "vm/arrays/set.c"#include "vm/arrays/slice.c"#include "vm/arrays/zip.c"#include "vm/bitwise/band.c"#include "vm/bitwise/bnot.c"#include "vm/bitwise/bor.c"#include "vm/bitwise/bxor.c"#include "vm/bitwise/rol.c"#include "vm/bitwise/ror.c"#include "vm/bitwise/shl.c"#include "vm/bitwise/shr.c"#include "vm/core/call.c"#include "vm/core/dup.c"#include "vm/core/exit.c"#include "vm/core/halt.c"#include "vm/core/jump.c"#include "vm/core/jump_if_false.c"#include "vm/core/load_const.c"#include "vm/core/load_global.c"#include "vm/core/load_local.c"#include "vm/core/nop.c"#include "vm/core/pop.c"#include "vm/core/return.c"#include "vm/core/store_global.c"#include "vm/core/store_local.c"#include "vm/core/swap.c"#include "vm/core/throw.c"#include "vm/core/try_pop.c"#include "vm/core/try_push.c"#include "vm/io/input_line.c"#include "vm/io/read_file.c"#include "vm/io/write_file.c"#include "vm/logic/and.c"#include "vm/logic/eq.c"#include "vm/logic/gt.c"#include "vm/logic/gte.c"#include "vm/logic/lt.c"#include "vm/logic/lte.c"#include "vm/logic/neq.c"#include "vm/logic/not.c"#include "vm/logic/or.c"#include "vm/maps/has_key.c"#include "vm/maps/keys.c"#include "vm/maps/make_map.c"#include "vm/maps/values.c"#include "vm/math/abs.c"#include "vm/math/ceil.c"#include "vm/math/clamp.c"#include "vm/math/cos.c"#include "vm/math/exp.c"#include "vm/math/floor.c"#include "vm/math/fmax.c"#include "vm/math/fmin.c"#include "vm/math/gcd.c"#include "vm/math/isqrt.c"#include "vm/math/lcm.c"#include "vm/math/log.c"#include "vm/math/log10.c"#include "vm/math/max.c"#include "vm/math/min.c"#include "vm/math/mod.c"#include "vm/math/pow.c"#include "vm/math/random_int.c"#include "vm/math/random_seed.c"#include "vm/math/round.c"#include "vm/math/sign.c"#include "vm/math/sin.c"#include "vm/math/sqrt.c"#include "vm/math/tan.c"#include "vm/math/trunc.c"#include "vm/rust/get_sp.c"#include "vm/rust/hello.c"#include "vm/rust/hello_args.c"#include "vm/rust/hello_args_return.c"#include "vm/rust/set_exit.c"#include "vm/os/clock_mono_ms.c"#include "vm/os/date_format.c"#include "vm/os/env.c"#include "vm/os/env_all.c"#include "vm/os/fun_version.c"#include "vm/os/proc_run.c"#include "vm/os/proc_system.c"#include "vm/os/random_number.c"#include "vm/os/serial_close.c"#include "vm/os/serial_config.c"#include "vm/os/serial_open.c"#include "vm/os/serial_recv.c"#include "vm/os/serial_send.c"#include "vm/os/sleep_ms.c"#include "vm/os/thread_join.c"#include "vm/os/thread_spawn.c"#include "vm/os/time_now_ms.c"#include "vm/os/socket_close.c"#include "vm/os/socket_recv.c"#include "vm/os/socket_send.c"#include "vm/os/socket_tcp_accept.c"#include "vm/os/socket_tcp_connect.c"#include "vm/os/socket_tcp_listen.c"#include "vm/os/socket_unix_connect.c"#include "vm/os/socket_unix_listen.c"#include "vm/os/fd_set_nonblock.c"#include "vm/os/fd_poll_read.c"#include "vm/os/fd_poll_write.c"#include "vm/pcsc/connect.c"#include "vm/pcsc/disconnect.c"#include "vm/pcsc/establish.c"#include "vm/pcsc/list_readers.c"#include "vm/pcsc/release.c"#include "vm/pcsc/transmit.c"#include "vm/json/from_file.c"#include "vm/json/parse.c"#include "vm/json/stringify.c"#include "vm/json/to_file.c"#include "vm/xml/name.c"#include "vm/xml/parse.c"#include "vm/xml/root.c"#include "vm/xml/text.c"#include "vm/ini/free.c"#include "vm/ini/get_bool.c"#include "vm/ini/get_double.c"#include "vm/ini/get_int.c"#include "vm/ini/get_string.c"#include "vm/ini/load.c"#include "vm/ini/save.c"#include "vm/ini/set.c"#include "vm/ini/unset.c"#include "vm/curl/download.c"#include "vm/curl/get.c"#include "vm/curl/post.c"#include "vm/openssl/md5.c"#include "vm/openssl/ripemd160.c"#include "vm/openssl/sha256.c"#include "vm/openssl/sha512.c"#include "vm/sqlite/close.c"#include "vm/sqlite/exec.c"#include "vm/sqlite/open.c"#include "vm/sqlite/query.c"#include "vm/pcre2/findall.c"#include "vm/pcre2/match.c"#include "vm/pcre2/test.c"#include "vm/strings/find.c"#include "vm/strings/regex_match.c"#include "vm/strings/regex_replace.c"#include "vm/strings/regex_search.c"#include "vm/strings/split.c"#include "vm/strings/substr.c"#include "vm/cast.c"#include "vm/echo.c"#include "vm/len.c"#include "vm/line.c"#include "vm/os/list_dir.c"#include "vm/print.c"#include "vm/sclamp.c"#include "vm/to_number.c"#include "vm/to_string.c"#include "vm/typeof.c"#include "vm/uclamp.c"Go to the source code of this file.
Macros | |
| #define | _POSIX_C_SOURCE 200809L |
| #define | _XOPEN_SOURCE 700 |
| #define | fprintf fun_vm_fprintf |
| #define | exit(code) |
| #define | FUN_VERSION "0.0.0-dev" |
Functions | |
| char * | preprocess_includes (const char *src) |
| Public wrapper to preprocess includes without a current path. | |
| int | map_expanded_line_to_include_path (const char *path, int line, char *out_path, size_t out_path_cap, int *out_line) |
| Map a line number in expanded source back to original include path/line. | |
| static int | fun_vm_vfprintf (FILE *stream, const char *fmt, va_list ap) |
| fprintf-like wrapper that annotates stderr messages with VM source context. | |
| static int | fun_vm_fprintf (FILE *stream, const char *fmt,...) |
| fprintf wrapper forwarding to fun_vm_vfprintf. | |
| static void | fun_vm_exit (int code) |
| Replacement for exit() used within this translation unit. | |
| static void | push_value (VM *vm, Value v) |
| Push a Value onto the VM operand stack. | |
| void | vm_raise_error (VM *vm, const char *msg) |
| Raise a runtime error inside the VM, honoring try/catch/finally. | |
| static const char * | value_type_name (ValueType t) |
| Get a human-readable name for a ValueType. | |
| void | vm_clear_output (VM *vm) |
| Clear the VM's buffered output values and partial flags. | |
| void | vm_free (VM *vm) |
| Free resources owned directly by the VM structure. | |
| static void | vm_pop_frame (VM *vm) |
| Pop the current call frame and free its local variables. | |
| void | vm_reset (VM *vm) |
| Reset the VM to a clean state. | |
| void | vm_dump_globals (VM *vm) |
| Print all non-nil global variables to stdout for debugging. | |
| 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). | |
| static Value | pop_value (VM *vm) |
| Pop a Value from the VM operand stack. | |
| 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). | |
| 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) |
| Cast the VM pointer to an opaque mutable void* (unsafe FFI helper). | |
| 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. | |
| static void | frame_init (Frame *f) |
| Initialize a call frame to a clean state. | |
| void | vm_init (VM *vm) |
| Initialize a VM instance to its default state. | |
| static void | vm_push_frame (VM *vm, Bytecode *fn, int argc, Value *args) |
| Push a new call frame for a function and transfer arguments. | |
| void | vm_print_output (VM *vm) |
| Print the VM's buffered output values to stdout. | |
Variables | |
| static __thread VM * | g_active_vm = NULL |
| static __thread jmp_buf | g_vm_err_jmp |
Core virtual machine implementation and opcode dispatch for Fun.
Defines the VM state, stack helpers, debugger/stepping support, built-in opcode handlers (included as amalgamated C files), platform I/O helpers, and the main interpreter loop that executes bytecode produced by the Fun compiler. This file is the central runtime of the language.
Definition in file vm.c.
| #define exit | ( | code | ) |
| #define fprintf fun_vm_fprintf |
| #define FUN_VERSION "0.0.0-dev" |
|
static |
|
static |
Replacement for exit() used within this translation unit.
If REPL-on-error is enabled for the active VM, this performs a longjmp back to vm_run so the REPL can be entered with the current VM state intact. Otherwise, terminates the process immediately using _Exit/_exit.
| code | Exit status code (non-zero values indicate error conditions). |
|
static |
fprintf wrapper forwarding to fun_vm_vfprintf.
Convenience wrapper that collects varargs and calls fun_vm_vfprintf so that VM-aware diagnostics are consistently applied in this translation unit.
| stream | Output stream. |
| fmt | printf-style format string. |
|
static |
fprintf-like wrapper that annotates stderr messages with VM source context.
When writing to stderr and a VM is active, this function appends file name, line number, function name, opcode and instruction pointer information to the message. It attempts to map expanded preprocessed line numbers back to the original included file for clearer diagnostics.
| stream | Output stream (typically stderr or stdout). |
| fmt | printf-style format string. |
| ap | Variable argument list corresponding to fmt. |
|
extern |
Map a line number in expanded source back to original include path/line.
Scans the expanded text for the nearest preceding __include_begin__ marker and computes the corresponding inner line number.
| path | Path to the original top-level file that was expanded. |
| line | 1-based line number in the expanded text. |
| out_path | Output buffer for the resolved file path. |
| out_path_cap | Capacity of out_path. |
| out_line | Receives 1-based line number within resolved file. |
Definition at line 996 of file parser_utils.c.
|
extern |
Public wrapper to preprocess includes without a current path.
Definition at line 965 of file parser_utils.c.
|
static |
| void * vm_as_mut_ptr | ( | VM * | vm | ) |
| void vm_clear_output | ( | VM * | vm | ) |
| int vm_debug_add_breakpoint | ( | VM * | vm, |
| const char * | file, | ||
| int | line ) |
| void vm_debug_clear_breakpoints | ( | VM * | vm | ) |
| int vm_debug_delete_breakpoint | ( | VM * | vm, |
| int | id ) |
| 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 | ) |
| void vm_dump_globals | ( | VM * | vm | ) |
| void vm_free | ( | VM * | vm | ) |
Free resources owned directly by the VM structure.
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. |
| void vm_init | ( | VM * | vm | ) |
| 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 | ) |
|
static |
| int64_t vm_pop_i64 | ( | VM * | vm | ) |
| void vm_print_output | ( | VM * | vm | ) |
Push a new call frame for a function and transfer arguments.
The first argc Values from args are moved (ownership transfer) into the new frame's local slots starting at index 0. Aborts on frame stack overflow.
| void vm_push_i64 | ( | VM * | vm, |
| int64_t | v ) |
| void vm_raise_error | ( | VM * | vm, |
| const char * | msg ) |
Raise a runtime error inside the VM, honoring try/catch/finally.
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.
| void vm_reset | ( | VM * | vm | ) |
Reset the VM to a clean state.
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. |
| size_t vm_sizeof | ( | void | ) |
| size_t vm_value_sizeof | ( | void | ) |