1
0
Fork 0
forked from fun/fun

Added fun_version and env_all as opcodes and some fixes in httpserver. (0.37.30)

This commit is contained in:
Johannes Findeisen 2025-12-28 23:36:37 +01:00
commit a27ff12b08
12 changed files with 126 additions and 6 deletions

View file

@ -131,6 +131,8 @@ static const char *opcode_name(OpCode op) {
case OP_TIME_NOW_MS: return "TIME_NOW_MS";
case OP_CLOCK_MONO_MS: return "CLOCK_MONO_MS";
case OP_DATE_FORMAT: return "DATE_FORMAT";
case OP_ENV_ALL: return "ENV_ALL";
case OP_FUN_VERSION: return "FUN_VERSION";
case OP_THREAD_SPAWN: return "THREAD_SPAWN";
case OP_THREAD_JOIN: return "THREAD_JOIN";
case OP_SLEEP_MS: return "SLEEP_MS";

View file

@ -126,6 +126,8 @@ typedef enum {
OP_TIME_NOW_MS, // pushes current wall-clock time in milliseconds since Unix epoch
OP_CLOCK_MONO_MS, // pushes monotonic clock in milliseconds (not wall time)
OP_DATE_FORMAT, // pops fmt string, ms epoch (int); pushes formatted date string using strftime
OP_ENV_ALL, // pushes map of all environment variables
OP_FUN_VERSION, // pushes version string
// Threads
OP_THREAD_SPAWN, // operand: 0=no args, 1=has args; pops [args?], fn; pushes thread id (int>0)

View file

@ -9,6 +9,7 @@
#include "parser.h"
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifdef FUN_WITH_REPL
@ -42,6 +43,9 @@ static void print_usage(const char *prog) {
}
int main(int argc, char **argv) {
/* Set FUN_EXECUTABLE environment variable to the path of this binary */
setenv("FUN_EXECUTABLE", argv[0], 1);
VM vm;
vm_init(&vm);

View file

@ -757,6 +757,20 @@ static int emit_primary(Bytecode *bc, const char *src, size_t len, size_t *pos)
free(name);
return 1;
}
if (strcmp(name, "env_all") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "env_all expects ()"); free(name); return 0; }
bytecode_add_instruction(bc, OP_ENV_ALL, 0);
free(name);
return 1;
}
if (strcmp(name, "fun_version") == 0) {
(*pos)++; /* '(' */
if (!consume_char(src, len, pos, ')')) { parser_fail(*pos, "fun_version expects ()"); free(name); return 0; }
bytecode_add_instruction(bc, OP_FUN_VERSION, 0);
free(name);
return 1;
}
if (strcmp(name, "os_list_dir") == 0) {
(*pos)++; /* '(' */
if (!emit_expression(bc, src, len, pos)) { parser_fail(*pos, "os_list_dir expects (path)"); free(name); return 0; }

View file

@ -667,6 +667,8 @@ void vm_run(VM *vm, Bytecode *entry) {
#include "vm/math/random_seed.c"
#include "vm/os/env.c"
#include "vm/os/env_all.c"
#include "vm/os/fun_version.c"
#include "vm/os/sleep_ms.c"
#include "vm/os/thread_join.c"
#include "vm/os/thread_spawn.c"

35
src/vm/os/env_all.c Normal file
View file

@ -0,0 +1,35 @@
/**
* 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
*
* Added: 2025-12-28
*/
// Get all environment variables of the operation system and push them as a map.
case OP_ENV_ALL: {
extern char **environ;
Value m = make_map_empty();
if (environ) {
for (char **env = environ; *env; ++env) {
char *entry = *env;
char *equals = strchr(entry, '=');
if (equals) {
size_t key_len = equals - entry;
char *key = malloc(key_len + 1);
if (key) {
memcpy(key, entry, key_len);
key[key_len] = '\0';
map_set(&m, key, make_string(equals + 1));
free(key);
}
}
}
}
push_value(vm, m);
break;
}

20
src/vm/os/fun_version.c Normal file
View file

@ -0,0 +1,20 @@
/**
* 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
*
* Added: 2025-12-28
*/
// Pushes the current Fun version string onto the stack.
case OP_FUN_VERSION: {
#ifndef FUN_VERSION
#define FUN_VERSION "0.0.0-dev"
#endif
push_value(vm, make_string(FUN_VERSION));
break;
}