967 lines
27 KiB
C
967 lines
27 KiB
C
/**
|
|
* This file is part of the Fun programming language.
|
|
* https://fun-lang.xyz/
|
|
*
|
|
* Copyright 2025 Johannes Findeisen <you@hanez.org>
|
|
* Licensed under the terms of the Apache-2.0 license.
|
|
* https://opensource.org/license/apache-2-0
|
|
*/
|
|
|
|
/* Ensure POSIX/XSI prototypes (nanosleep, wcwidth, etc.) are available
|
|
* before any system headers are included by amalgamated .c files. */
|
|
#ifndef _WIN32
|
|
#ifndef _POSIX_C_SOURCE
|
|
#define _POSIX_C_SOURCE 200809L
|
|
#endif
|
|
#ifndef _XOPEN_SOURCE
|
|
#define _XOPEN_SOURCE 700
|
|
#endif
|
|
#endif
|
|
|
|
#include <math.h>
|
|
#include <stdarg.h>
|
|
#include <stddef.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
#ifdef __unix__
|
|
#include <netdb.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <sys/un.h>
|
|
#include <sys/wait.h>
|
|
#include <unistd.h>
|
|
/* For hidden input (password) handling in OP_INPUT_LINE */
|
|
#include <termios.h>
|
|
/* For non-blocking sockets and polling */
|
|
#include <fcntl.h>
|
|
#include <poll.h>
|
|
// #include <arpa/inet.h>
|
|
#endif
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#endif
|
|
|
|
/* Bring in split-out built-ins without changing the build system yet */
|
|
#include "iter.c"
|
|
#include "map.c"
|
|
#include "string.c"
|
|
#include "value.h"
|
|
#include "vm.h"
|
|
|
|
/* Shared Notcurses state (optional) */
|
|
#ifdef FUN_WITH_NOTCURSES
|
|
#include <notcurses/notcurses.h>
|
|
#include <wchar.h> /* ensure wcwidth/wcswidth prototypes present before notcurses.h on some systems */
|
|
#endif
|
|
#include "vm/notcurses/common.h"
|
|
|
|
// Optional by extensions commonly used code. #ifdef's are in each single file.
|
|
#include "external/curl.c"
|
|
#include "external/ini.c"
|
|
#ifdef FUN_WITH_INI
|
|
/* Central INI handle registry and helpers */
|
|
#include "vm/ini/handles.c"
|
|
#endif
|
|
/* Note: INI opcode handlers are included below; changes in vm/ini/ .c files
|
|
* require vm.c to rebuild. */
|
|
#include "external/json.c"
|
|
#include "external/libsql.c"
|
|
#include "external/openssl.c"
|
|
#include "external/pcre2.c"
|
|
#include "external/pcsc.c"
|
|
#include "external/sqlite.c"
|
|
#include "external/tcltk.c"
|
|
#include "external/xml2.c"
|
|
|
|
/* forward declarations for include mapping used in error reporting */
|
|
extern char *preprocess_includes(const char *src);
|
|
extern int map_expanded_line_to_include_path(const char *path, int line, char *out_path, size_t out_path_cap, int *out_line);
|
|
|
|
/* Threading internals (registry and platform glue) */
|
|
#include "vm/os/thread_common.c"
|
|
|
|
/* Track the currently running VM to annotate error messages */
|
|
static VM *g_active_vm = NULL;
|
|
|
|
/* fprintf wrapper that appends source line info for stderr messages */
|
|
static int fun_vm_vfprintf(FILE *stream, const char *fmt, va_list ap) {
|
|
int written = vfprintf(stream, fmt, ap);
|
|
if (stream == stderr && g_active_vm) {
|
|
/* Determine current frame and instruction pointer of the faulting op */
|
|
const char *opname = "unknown";
|
|
const char *fname = NULL;
|
|
const char *sfile = NULL;
|
|
int ip = -1;
|
|
int line = -1;
|
|
if (g_active_vm->fp >= 0) {
|
|
Frame *f = &g_active_vm->frames[g_active_vm->fp];
|
|
ip = f->ip - 1; /* last executed instruction */
|
|
if (f->fn) {
|
|
fname = f->fn->name;
|
|
sfile = f->fn->source_file;
|
|
/* derive opcode name */
|
|
if (ip >= 0 && ip < f->fn->instr_count) {
|
|
int op = f->fn->instructions[ip].op;
|
|
if (op >= 0 && op < (int)(sizeof(opcode_names) / sizeof(opcode_names[0]))) {
|
|
opname = opcode_names[op];
|
|
}
|
|
}
|
|
/* derive source line by scanning back to the most recent OP_LINE marker */
|
|
for (int i = ip; i >= 0; --i) {
|
|
Instruction prev = f->fn->instructions[i];
|
|
if (prev.op == OP_LINE) {
|
|
line = prev.operand;
|
|
break;
|
|
}
|
|
}
|
|
/* fallback to VM's last recorded line if no marker found */
|
|
if (line <= 0) line = g_active_vm->current_line > 0 ? g_active_vm->current_line : 1;
|
|
|
|
/* Map expanded line back to the real included file.
|
|
* Important: OP_LINE operands refer to the expanded, top-level source produced by
|
|
* the preprocessor. Therefore we must pass the TOP-LEVEL script path to the mapper,
|
|
* not the current function's own source_file (which may point at an included file).
|
|
*/
|
|
if (line > 0) {
|
|
/* Attempt to retrieve the entry frame's source file as the true top-level path */
|
|
const char *top_path = NULL;
|
|
if (g_active_vm->fp >= 0) {
|
|
Frame *entry = &g_active_vm->frames[0];
|
|
if (entry && entry->fn && entry->fn->source_file) top_path = entry->fn->source_file;
|
|
}
|
|
/* Fallback: use current function's source when entry is unavailable */
|
|
if (!top_path) top_path = sfile;
|
|
if (top_path) {
|
|
char mapped_path[1024];
|
|
int mapped_line = line;
|
|
if (map_expanded_line_to_include_path(top_path, line, mapped_path, sizeof(mapped_path), &mapped_line)) {
|
|
sfile = strdup(mapped_path); /* leak acceptable on error paths */
|
|
line = mapped_line;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
fprintf(stream == stderr ? stderr : stream,
|
|
" (at %s:%d in %s, op %s @ip %d)\n",
|
|
sfile ? sfile : "<unknown>",
|
|
line > 0 ? line : 1,
|
|
fname ? fname : "<entry>",
|
|
opname,
|
|
ip);
|
|
}
|
|
return written;
|
|
}
|
|
|
|
static int fun_vm_fprintf(FILE *stream, const char *fmt, ...) {
|
|
va_list ap;
|
|
va_start(ap, fmt);
|
|
int r = fun_vm_vfprintf(stream, fmt, ap);
|
|
va_end(ap);
|
|
return r;
|
|
}
|
|
|
|
|
|
/* Redirect fprintf within this translation unit so opcode handlers use our wrapper */
|
|
#define fprintf fun_vm_fprintf
|
|
|
|
/* Intercept exit() in this translation unit so VM errors don't terminate the process outright */
|
|
#include <setjmp.h>
|
|
|
|
static jmp_buf g_vm_err_jmp;
|
|
|
|
static void fun_vm_exit(int code) {
|
|
if (g_active_vm && g_active_vm->repl_on_error) {
|
|
/* Jump back to vm_run to allow dropping into the REPL with intact VM state */
|
|
longjmp(g_vm_err_jmp, code ? code : 1);
|
|
}
|
|
/* Fallback: terminate immediately if not in REPL-on-error mode */
|
|
#ifdef _WIN32
|
|
_exit(code);
|
|
#else
|
|
_Exit(code);
|
|
#endif
|
|
}
|
|
|
|
/* Redirect exit inside this TU (affects included opcode handlers) */
|
|
#define exit(code) fun_vm_exit(code)
|
|
|
|
/* Forward decl for stack push used by vm_raise_error */
|
|
static void push_value(VM *vm, Value v);
|
|
|
|
/* Raise a runtime error that respects try/catch/finally semantics.
|
|
* If a handler is installed for the current frame, jump to it and push
|
|
* an error string for the catch clause. Otherwise, print and stop VM. */
|
|
void vm_raise_error(VM *vm, const char *msg) {
|
|
if (!vm || vm->fp < 0) {
|
|
fprintf(stderr, "Runtime error: %s\n", msg ? msg : "<error>");
|
|
return;
|
|
}
|
|
Frame *f = &vm->frames[vm->fp];
|
|
if (f->try_sp >= 0) {
|
|
char buf[256];
|
|
if (msg) {
|
|
snprintf(buf, sizeof(buf), "Runtime error: %s", msg);
|
|
} else {
|
|
snprintf(buf, sizeof(buf), "Runtime error");
|
|
}
|
|
/* push error value and transfer control to handler target */
|
|
Value err = make_string(buf);
|
|
push_value(vm, err);
|
|
int try_idx = f->try_stack[f->try_sp--];
|
|
int target = f->fn->instructions[try_idx].operand;
|
|
f->ip = target;
|
|
return;
|
|
}
|
|
/* No handler: print annotated message and terminate VM */
|
|
fprintf(stderr, "Runtime error: %s\n", msg ? msg : "<error>");
|
|
vm->fp = -1; /* stop execution */
|
|
}
|
|
|
|
/*
|
|
Opcode case include index (vm_case_*.inc):
|
|
- Core/stack/frame:
|
|
vm_case_nop.inc, vm_case_halt.inc,
|
|
vm_case_load_const.inc, vm_case_load_local.inc, vm_case_store_local.inc,
|
|
vm_case_load_global.inc, vm_case_store_global.inc,
|
|
vm_case_pop.inc, vm_case_dup.inc, vm_case_swap.inc,
|
|
vm_case_call.inc, vm_case_return.inc, vm_case_print.inc, vm_case_jump.inc, vm_case_jump_if_false.inc
|
|
- Arithmetic and logic:
|
|
vm_case_add.inc, vm_case_sub.inc, vm_case_mul.inc, vm_case_div.inc,
|
|
vm_case_mod.inc, vm_case_lt.inc, vm_case_lte.inc, vm_case_gt.inc, vm_case_gte.inc,
|
|
vm_case_eq.inc, vm_case_neq.inc, vm_case_and.inc, vm_case_or.inc, vm_case_not.inc
|
|
- Conversions:
|
|
vm_case_to_number.inc, vm_case_to_string.inc
|
|
- Arrays and slices:
|
|
vm_case_make_array.inc, vm_case_len.inc,
|
|
vm_case_index_get.inc, vm_case_index_set.inc,
|
|
vm_case_arr_push.inc, vm_case_arr_pop.inc, vm_case_arr_set.inc, vm_case_arr_insert.inc, vm_case_arr_remove.inc,
|
|
vm_case_slice.inc
|
|
- Strings and iteration helpers:
|
|
vm_case_split.inc, vm_case_join.inc, vm_case_substr.inc, vm_case_find.inc,
|
|
vm_case_enumerate.inc, vm_case_zip.inc
|
|
- Maps and I/O:
|
|
vm_case_make_map.inc, vm_case_keys.inc, vm_case_values.inc, vm_case_has_key.inc,
|
|
vm_case_read_file.inc, vm_case_write_file.inc
|
|
- Math utils / RNG:
|
|
vm_case_min.inc, vm_case_max.inc, vm_case_clamp.inc, vm_case_abs.inc, vm_case_pow.inc,
|
|
vm_case_random_seed.inc, vm_case_random_int.inc
|
|
|
|
Dev tips:
|
|
- When adding a new opcode:
|
|
1) Define OP_<NAME> in bytecode.h and opcode_names[] in vm.h.
|
|
2) Implement its VM handler in src/vm_case_<lowercase>.inc.
|
|
3) Include it in the switch below.
|
|
4) Run scripts/check_op_includes.py to verify coverage.
|
|
- You can run scripts/run_examples.sh to sanity-check examples quickly.
|
|
*/
|
|
|
|
static const char *value_type_name(ValueType t) {
|
|
switch (t) {
|
|
case VAL_FUNCTION:
|
|
return "function";
|
|
case VAL_INT:
|
|
return "int";
|
|
case VAL_FLOAT:
|
|
return "float";
|
|
case VAL_BOOL:
|
|
return "boolean";
|
|
case VAL_ARRAY:
|
|
return "array";
|
|
case VAL_MAP:
|
|
return "map";
|
|
case VAL_NIL:
|
|
return "nil";
|
|
case VAL_STRING:
|
|
return "string";
|
|
default:
|
|
return "unknown";
|
|
}
|
|
}
|
|
|
|
void vm_clear_output(VM *vm) {
|
|
for (int i = 0; i < vm->output_count; ++i) {
|
|
free_value(vm->output[i]);
|
|
}
|
|
vm->output_count = 0;
|
|
// reset partial flags
|
|
for (int i = 0; i < OUTPUT_SIZE; ++i)
|
|
vm->output_is_partial[i] = 0;
|
|
}
|
|
|
|
void vm_free(VM *vm) {
|
|
// currently nothing persistent allocated inside VM itself
|
|
}
|
|
|
|
/* forward declaration for helper used in vm_reset */
|
|
static void vm_pop_frame(VM *vm);
|
|
|
|
void vm_reset(VM *vm) {
|
|
// Pop all frames (free locals)
|
|
while (vm->fp >= 0) {
|
|
vm_pop_frame(vm);
|
|
}
|
|
// Clear stack
|
|
vm->sp = -1;
|
|
// Free globals
|
|
for (int i = 0; i < MAX_GLOBALS; ++i) {
|
|
free_value(vm->globals[i]);
|
|
vm->globals[i] = make_nil();
|
|
}
|
|
// Clear output buffer
|
|
vm_clear_output(vm);
|
|
// Reset exit code
|
|
vm->exit_code = 0;
|
|
|
|
// Reset debugger state (breakpoints, stepping)
|
|
vm_debug_reset(vm);
|
|
}
|
|
|
|
void vm_dump_globals(VM *vm) {
|
|
printf("=== globals ===\n");
|
|
for (int i = 0; i < MAX_GLOBALS; ++i) {
|
|
if (vm->globals[i].type != VAL_NIL) {
|
|
printf("[%d] ", i);
|
|
print_value(&vm->globals[i]);
|
|
printf("\n");
|
|
}
|
|
}
|
|
printf("===============\n");
|
|
}
|
|
|
|
/* --- Debugger API impl --- */
|
|
|
|
void vm_debug_reset(VM *vm) {
|
|
for (int i = 0; i < vm->break_count; ++i) {
|
|
if (vm->breakpoints[i].file) {
|
|
free(vm->breakpoints[i].file);
|
|
vm->breakpoints[i].file = NULL;
|
|
}
|
|
vm->breakpoints[i].active = 0;
|
|
vm->breakpoints[i].line = 0;
|
|
}
|
|
vm->break_count = 0;
|
|
vm->debug_step_mode = 0;
|
|
vm->debug_step_target_fp = -1;
|
|
vm->debug_step_start_ic = vm->instr_count;
|
|
vm->debug_stop_requested = 0;
|
|
}
|
|
|
|
int vm_debug_add_breakpoint(VM *vm, const char *file, int line) {
|
|
if (!file || line <= 0) return -1;
|
|
if (vm->break_count >= (int)(sizeof(vm->breakpoints) / sizeof(vm->breakpoints[0]))) return -1;
|
|
int id = vm->break_count++;
|
|
vm->breakpoints[id].file = strdup(file);
|
|
vm->breakpoints[id].line = line;
|
|
vm->breakpoints[id].active = 1;
|
|
return id;
|
|
}
|
|
|
|
int vm_debug_delete_breakpoint(VM *vm, int id) {
|
|
if (id < 0 || id >= vm->break_count) return 0;
|
|
if (vm->breakpoints[id].file) free(vm->breakpoints[id].file);
|
|
for (int i = id + 1; i < vm->break_count; ++i) {
|
|
vm->breakpoints[i - 1] = vm->breakpoints[i];
|
|
}
|
|
vm->break_count--;
|
|
if (vm->break_count >= 0) {
|
|
vm->breakpoints[vm->break_count].file = NULL;
|
|
vm->breakpoints[vm->break_count].line = 0;
|
|
vm->breakpoints[vm->break_count].active = 0;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void vm_debug_clear_breakpoints(VM *vm) {
|
|
vm_debug_reset(vm);
|
|
}
|
|
|
|
void vm_debug_list_breakpoints(VM *vm) {
|
|
if (vm->break_count <= 0) {
|
|
printf("(no breakpoints)\n");
|
|
return;
|
|
}
|
|
for (int i = 0; i < vm->break_count; ++i) {
|
|
if (!vm->breakpoints[i].active) continue;
|
|
printf(" [%d] %s:%d\n", i, vm->breakpoints[i].file ? vm->breakpoints[i].file : "<unknown>", vm->breakpoints[i].line);
|
|
}
|
|
}
|
|
|
|
void vm_debug_request_step(VM *vm) {
|
|
vm->debug_step_mode = 1; // step
|
|
vm->debug_step_start_ic = vm->instr_count;
|
|
vm->debug_stop_requested = 0;
|
|
}
|
|
|
|
void vm_debug_request_next(VM *vm) {
|
|
vm->debug_step_mode = 2; // next (step over)
|
|
vm->debug_step_target_fp = vm->fp;
|
|
vm->debug_step_start_ic = vm->instr_count;
|
|
vm->debug_stop_requested = 0;
|
|
}
|
|
|
|
void vm_debug_request_finish(VM *vm) {
|
|
vm->debug_step_mode = 3; // finish (until return)
|
|
vm->debug_step_target_fp = vm->fp;
|
|
vm->debug_stop_requested = 0;
|
|
}
|
|
|
|
void vm_debug_request_continue(VM *vm) {
|
|
vm->debug_step_mode = 0;
|
|
vm->debug_stop_requested = 0;
|
|
}
|
|
|
|
static void push_value(VM *vm, Value v) {
|
|
if (vm->sp >= STACK_SIZE - 1) {
|
|
fprintf(stderr, "Runtime error: stack overflow\n");
|
|
exit(1);
|
|
}
|
|
vm->stack[++vm->sp] = v; /* take ownership of v */
|
|
}
|
|
|
|
static Value pop_value(VM *vm) {
|
|
if (vm->sp < 0) {
|
|
fprintf(stderr, "Runtime error: stack underflow\n");
|
|
exit(1);
|
|
}
|
|
return vm->stack[vm->sp--]; /* caller owns returned Value */
|
|
}
|
|
|
|
/* --- C ABI helpers for Rust FFI --- */
|
|
int64_t vm_pop_i64(VM *vm) {
|
|
Value v = pop_value(vm);
|
|
int64_t out = 0;
|
|
if (v.type == VAL_INT) {
|
|
out = v.i;
|
|
} else if (v.type == VAL_FLOAT) {
|
|
out = (int64_t)v.d;
|
|
} else {
|
|
fprintf(stderr, "Runtime type error: expected int/float on stack, got %s\n", value_type_name(v.type));
|
|
free_value(v);
|
|
exit(1);
|
|
}
|
|
/* free any dynamic payload (no-op for int/float) */
|
|
free_value(v);
|
|
return out;
|
|
}
|
|
|
|
void vm_push_i64(VM *vm, int64_t v) {
|
|
push_value(vm, make_int(v));
|
|
}
|
|
|
|
/* --- Extended C ABI for Rust to access VM internals (unsafe) --- */
|
|
size_t vm_sizeof(void) {
|
|
return sizeof(VM);
|
|
}
|
|
|
|
size_t vm_value_sizeof(void) {
|
|
return sizeof(Value);
|
|
}
|
|
|
|
void *vm_as_mut_ptr(VM *vm) {
|
|
return (void *)vm;
|
|
}
|
|
|
|
size_t vm_offset_of_exit_code(void) {
|
|
return offsetof(VM, exit_code);
|
|
}
|
|
|
|
size_t vm_offset_of_sp(void) {
|
|
return offsetof(VM, sp);
|
|
}
|
|
|
|
size_t vm_offset_of_stack(void) {
|
|
return offsetof(VM, stack);
|
|
}
|
|
|
|
size_t vm_offset_of_globals(void) {
|
|
return offsetof(VM, globals);
|
|
}
|
|
|
|
static void frame_init(Frame *f) {
|
|
f->fn = NULL;
|
|
f->ip = 0;
|
|
for (int i = 0; i < MAX_FRAME_LOCALS; ++i)
|
|
f->locals[i] = make_nil();
|
|
f->try_sp = -1;
|
|
}
|
|
|
|
void vm_init(VM *vm) {
|
|
vm->sp = -1;
|
|
vm->fp = -1;
|
|
vm->output_count = 0;
|
|
for (int i = 0; i < OUTPUT_SIZE; ++i)
|
|
vm->output_is_partial[i] = 0;
|
|
vm->instr_count = 0;
|
|
vm->exit_code = 0;
|
|
vm->trace_enabled = 0;
|
|
vm->repl_on_error = 0;
|
|
vm->on_error_repl = NULL;
|
|
|
|
/* Debugger state */
|
|
vm->debug_step_mode = 0;
|
|
vm->debug_step_target_fp = -1;
|
|
vm->debug_step_start_ic = 0;
|
|
vm->debug_stop_requested = 0;
|
|
vm->break_count = 0;
|
|
for (int i = 0; i < (int)(sizeof(vm->breakpoints) / sizeof(vm->breakpoints[0])); ++i) {
|
|
vm->breakpoints[i].file = NULL;
|
|
vm->breakpoints[i].line = 0;
|
|
vm->breakpoints[i].active = 0;
|
|
}
|
|
|
|
for (int i = 0; i < MAX_GLOBALS; ++i)
|
|
vm->globals[i] = make_nil();
|
|
}
|
|
|
|
/* push a new frame, transferring ownership of args[] into frame->locals[0..argc-1] */
|
|
static void vm_push_frame(VM *vm, Bytecode *fn, int argc, Value *args) {
|
|
if (vm->fp >= MAX_FRAMES - 1) {
|
|
fprintf(stderr, "Runtime error: too many frames\n");
|
|
exit(1);
|
|
}
|
|
Frame *f = &vm->frames[++vm->fp];
|
|
frame_init(f);
|
|
f->fn = fn;
|
|
f->ip = 0;
|
|
/* move args into locals 0..argc-1 */
|
|
for (int i = 0; i < argc && i < MAX_FRAME_LOCALS; ++i) {
|
|
f->locals[i] = args[i]; /* transfer ownership */
|
|
}
|
|
}
|
|
|
|
/* pop current frame and free its locals */
|
|
static void vm_pop_frame(VM *vm) {
|
|
if (vm->fp < 0) {
|
|
fprintf(stderr, "Runtime error: pop frame with empty frame stack\n");
|
|
exit(1);
|
|
}
|
|
Frame *f = &vm->frames[vm->fp];
|
|
for (int i = 0; i < MAX_FRAME_LOCALS; ++i) {
|
|
free_value(f->locals[i]);
|
|
f->locals[i] = make_nil();
|
|
}
|
|
vm->fp--;
|
|
}
|
|
|
|
void vm_print_output(VM *vm) {
|
|
for (int i = 0; i < vm->output_count; ++i) {
|
|
print_value(&vm->output[i]);
|
|
if (!vm->output_is_partial[i]) {
|
|
printf("\n");
|
|
}
|
|
}
|
|
}
|
|
|
|
void vm_run(VM *vm, Bytecode *entry) {
|
|
/* reset instruction count for this run */
|
|
vm->instr_count = 0;
|
|
vm->current_line = 1;
|
|
g_active_vm = vm;
|
|
|
|
/* set error trap if REPL-on-error is enabled */
|
|
if (vm->repl_on_error) {
|
|
int jcode = setjmp(g_vm_err_jmp);
|
|
if (jcode != 0) {
|
|
/* We got here from a trapped exit() in an error path */
|
|
fprintf(stderr, "Entering REPL due to runtime error (code %d)\n", jcode);
|
|
if (vm->on_error_repl) {
|
|
vm->on_error_repl(vm);
|
|
}
|
|
g_active_vm = NULL;
|
|
return;
|
|
}
|
|
}
|
|
|
|
/* start with entry frame (no args) */
|
|
vm_push_frame(vm, entry, 0, NULL);
|
|
|
|
while (vm->fp >= 0) {
|
|
Frame *f = &vm->frames[vm->fp];
|
|
|
|
/* Stop conditions at top of loop (stepping/finish) */
|
|
if (vm->on_error_repl) {
|
|
int should_stop = 0;
|
|
if (vm->debug_stop_requested) {
|
|
should_stop = 1;
|
|
} else if (vm->debug_step_mode == 1 && vm->instr_count > vm->debug_step_start_ic) { /* step */
|
|
should_stop = 1;
|
|
vm->debug_step_mode = 0;
|
|
} else if (vm->debug_step_mode == 2 && vm->instr_count > vm->debug_step_start_ic && vm->fp <= vm->debug_step_target_fp) { /* next */
|
|
should_stop = 1;
|
|
vm->debug_step_mode = 0;
|
|
} else if (vm->debug_step_mode == 3 && vm->fp < vm->debug_step_target_fp) { /* finish */
|
|
should_stop = 1;
|
|
vm->debug_step_mode = 0;
|
|
}
|
|
if (should_stop) {
|
|
vm->debug_stop_requested = 0;
|
|
fprintf(stderr, "Paused (debug)\n");
|
|
vm->on_error_repl(vm);
|
|
/* Frame pointer might have changed (reset/cont); refresh f */
|
|
if (vm->fp < 0) break;
|
|
f = &vm->frames[vm->fp];
|
|
}
|
|
}
|
|
|
|
if (f->ip < 0 || f->ip >= f->fn->instr_count) {
|
|
/* no more instructions in this frame -> implicit return nil */
|
|
Value nilv = make_nil();
|
|
vm_pop_frame(vm);
|
|
push_value(vm, nilv);
|
|
continue;
|
|
}
|
|
|
|
Instruction inst = f->fn->instructions[f->ip++];
|
|
vm->instr_count++; /* count each executed instruction */
|
|
|
|
if (vm->trace_enabled) {
|
|
const char *opname = (inst.op >= 0 && inst.op < (int)(sizeof(opcode_names) / sizeof(opcode_names[0])))
|
|
? opcode_names[inst.op]
|
|
: "???";
|
|
const char *fname = f->fn && f->fn->name ? f->fn->name : "<entry>";
|
|
const char *sfile = f->fn && f->fn->source_file ? f->fn->source_file : "<unknown>";
|
|
/* Dump up to top 4 stack values */
|
|
int count = vm->sp + 1;
|
|
int start = count - 4;
|
|
if (start < 0) start = 0;
|
|
fprintf(stdout, "TRACE %s:%d %s ip=%d %-14s %d | stack[%d]=[", sfile, vm->current_line, fname, f->ip - 1, opname, inst.operand, count);
|
|
for (int i = start; i < count; ++i) {
|
|
char *sv = value_to_string_alloc(&vm->stack[i]);
|
|
if (!sv) sv = strdup("<oom>");
|
|
fprintf(stdout, "%s%s", sv, (i == count - 1 ? "" : ", "));
|
|
free(sv);
|
|
}
|
|
fprintf(stdout, "]\n");
|
|
}
|
|
|
|
/* Breakpoint hit detection: breakpoints are set on source_file:line via LINE markers */
|
|
if (vm->on_error_repl && inst.op == OP_LINE && vm->break_count > 0) {
|
|
const char *sfile = (f->fn && f->fn->source_file) ? f->fn->source_file : NULL;
|
|
int line = inst.operand;
|
|
for (int bi = 0; bi < vm->break_count; ++bi) {
|
|
if (!vm->breakpoints[bi].active) continue;
|
|
if (vm->breakpoints[bi].line != line) continue;
|
|
if (!sfile || !vm->breakpoints[bi].file) continue;
|
|
if (strcmp(vm->breakpoints[bi].file, sfile) != 0) continue;
|
|
fprintf(stderr, "Breakpoint %d hit at %s:%d\n", bi, sfile, line);
|
|
vm->on_error_repl(vm);
|
|
/* After returning, refresh frame pointer and frame */
|
|
if (vm->fp < 0) break;
|
|
f = &vm->frames[vm->fp];
|
|
break;
|
|
}
|
|
}
|
|
|
|
switch (inst.op) {
|
|
/* All opcode handlers as .c includes */
|
|
#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"
|
|
|
|
/* Bitwise and shifts/rotates */
|
|
#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"
|
|
|
|
/* Rust FFI demo opcode(s) */
|
|
#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"
|
|
|
|
/* Socket ops */
|
|
#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"
|
|
|
|
/* Async-friendly FD helpers (UNIX) */
|
|
#include "vm/os/fd_set_nonblock.c"
|
|
#include "vm/os/fd_poll_read.c"
|
|
#include "vm/os/fd_poll_write.c"
|
|
|
|
#ifdef FUN_WITH_PCSC
|
|
#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"
|
|
#endif
|
|
|
|
/* JSON ops (implemented in jsonc.c, included above) */
|
|
#ifdef FUN_WITH_JSON
|
|
#include "vm/json/from_file.c"
|
|
#include "vm/json/parse.c"
|
|
#include "vm/json/stringify.c"
|
|
#include "vm/json/to_file.c"
|
|
#endif
|
|
|
|
/* XML ops (libxml2) */
|
|
#ifdef FUN_WITH_XML2
|
|
#include "vm/xml/name.c"
|
|
#include "vm/xml/parse.c"
|
|
#include "vm/xml/root.c"
|
|
#include "vm/xml/text.c"
|
|
#endif
|
|
|
|
/* INI ops (iniparser 4.2.6) */
|
|
#ifdef FUN_WITH_INI
|
|
#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"
|
|
#else
|
|
#include "vm/ini/stubs.c"
|
|
#endif
|
|
|
|
/* CURL ops */
|
|
#ifdef FUN_WITH_CURL
|
|
#include "vm/curl/download.c"
|
|
#include "vm/curl/get.c"
|
|
#include "vm/curl/post.c"
|
|
#endif
|
|
|
|
/* OpenSSL ops (md5/sha256/sha512/ripemd160) */
|
|
#ifdef FUN_WITH_OPENSSL
|
|
#include "vm/openssl/md5.c"
|
|
#include "vm/openssl/ripemd160.c"
|
|
#include "vm/openssl/sha256.c"
|
|
#include "vm/openssl/sha512.c"
|
|
#endif
|
|
|
|
|
|
/* Tk (Tcl/Tk) ops */
|
|
#ifdef FUN_WITH_TCLTK
|
|
#include "vm/tk/bind.c"
|
|
#include "vm/tk/button.c"
|
|
#include "vm/tk/eval.c"
|
|
#include "vm/tk/label.c"
|
|
#include "vm/tk/loop.c"
|
|
#include "vm/tk/pack.c"
|
|
#include "vm/tk/result.c"
|
|
#include "vm/tk/wm_title.c"
|
|
#endif
|
|
|
|
/* Notcurses TUI ops (optional) */
|
|
#ifdef FUN_WITH_NOTCURSES
|
|
#include "vm/notcurses/clear.c"
|
|
#include "vm/notcurses/draw_text.c"
|
|
#include "vm/notcurses/getch.c"
|
|
#include "vm/notcurses/init.c"
|
|
#include "vm/notcurses/shutdown.c"
|
|
#include "vm/notcurses/get_size.c"
|
|
#include "vm/notcurses/set_style.c"
|
|
#include "vm/notcurses/draw_char.c"
|
|
#include "vm/notcurses/hline.c"
|
|
#include "vm/notcurses/vline.c"
|
|
#include "vm/notcurses/box.c"
|
|
#include "vm/notcurses/fill.c"
|
|
#include "vm/notcurses/render.c"
|
|
#endif
|
|
|
|
/* SQLite ops */
|
|
#ifdef FUN_WITH_SQLITE
|
|
#include "vm/sqlite/close.c"
|
|
#include "vm/sqlite/exec.c"
|
|
#include "vm/sqlite/open.c"
|
|
#include "vm/sqlite/query.c"
|
|
#endif
|
|
|
|
/* C++ demo opcodes (guarded) */
|
|
#if defined(FUN_WITH_CPP)
|
|
case OP_CPP_ADD: {
|
|
int rc = fun_op_cpp_add(vm);
|
|
if (rc != 0) {
|
|
vm_raise_error(vm, "cpp_add failed");
|
|
}
|
|
break;
|
|
}
|
|
#else
|
|
case OP_CPP_ADD: {
|
|
vm_raise_error(vm, "CPP support is not enabled (build with -DFUN_WITH_CPP=ON)");
|
|
break;
|
|
}
|
|
#endif
|
|
|
|
/* libsql ops (independent) */
|
|
#ifdef FUN_WITH_LIBSQL
|
|
#include "vm/libsql/close.c"
|
|
#include "vm/libsql/exec.c"
|
|
#include "vm/libsql/open.c"
|
|
#include "vm/libsql/query.c"
|
|
#endif
|
|
|
|
/* PCRE2 ops */
|
|
#ifdef FUN_WITH_PCRE2
|
|
#include "vm/pcre2/findall.c"
|
|
#include "vm/pcre2/match.c"
|
|
#include "vm/pcre2/test.c"
|
|
#endif
|
|
|
|
#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"
|
|
|
|
default:
|
|
if (!opcode_is_valid(inst.op)) {
|
|
fprintf(stderr, "Runtime error: unknown opcode %d (%s) at instruction %d\n",
|
|
inst.op,
|
|
(inst.op >= 0 && inst.op < sizeof(opcode_names) / sizeof(opcode_names[0]))
|
|
? opcode_names[inst.op]
|
|
: "???",
|
|
f->ip - 1);
|
|
exit(1);
|
|
}
|
|
break;
|
|
}
|
|
|
|
/* Stream console output in realtime for scripts:
|
|
* When PRINT/ECHO pushed items into the VM's output buffer, flush them
|
|
* immediately to stdout and clear the buffer to avoid end-of-run bursts.
|
|
* This keeps REPL compatibility (REPL still prints after each submit),
|
|
* while regular script execution shows progress bars live.
|
|
*/
|
|
if (inst.op == OP_PRINT || inst.op == OP_ECHO) {
|
|
vm_print_output(vm);
|
|
vm_clear_output(vm);
|
|
fflush(stdout);
|
|
}
|
|
}
|
|
g_active_vm = NULL;
|
|
}
|