Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
set.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 2025 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 set.c
12 * @brief VM opcode snippet for setting an INI value (OP_INI_SET).
13 *
14 * Opcode: OP_INI_SET
15 * Stack: [value:any] [key:string] [section:string] [handle:int] -> [ok:int]
16 *
17 * Behavior
18 * - Pops value, key, section, and handle. Converts the value to a string using
19 * value_to_string_alloc() and stores it under "section:key" (and a dotted
20 * fallback) via dictionary_set().
21 * - Pushes 1 on success, 0 on failure (invalid args/handle or allocation fail).
22 *
23 * Errors
24 * - No VM exception is thrown; failures return 0.
25 */
26
27/* OP_INI_SET */
28#ifdef FUN_WITH_INI
30 Value vval = pop_value(vm);
34 dictionary *d = ini_get((vh.type == VAL_INT) ? (int)vh.i : 0);
35 const char *key = (vkey.type == VAL_STRING) ? vkey.s : NULL;
36 const char *sec = (vsec.type == VAL_STRING) ? vsec.s : NULL;
37 int ok = 0;
38 if (d && sec && key) {
39 char *valstr = value_to_string_alloc(&vval);
40 if (valstr) {
41 char full[1024];
42 char alt[1024];
43 ini_make_full_key(full, sizeof(full), sec, key);
44 memcpy(alt, full, sizeof(alt));
45 for (size_t i = 0; i < sizeof(alt) && alt[i]; ++i) {
46 if (alt[i] == ':') {
47 alt[i] = '.';
48 break;
49 }
50 }
51 /* iniparser 4.x does not expose iniparser_set; use dictionary_set */
52 if (dictionary_set(d, full, valstr) == 0) {
53 ok = 1; /* 0 means success */
54 } else if (dictionary_set(d, alt, valstr) == 0) {
55 ok = 1;
56 }
57 free(valstr);
58 }
59 }
65 break;
66}
67#endif
free_value(arr)
push_value(vm, copy_value(&v))
@ OP_INI_SET
Definition bytecode.h:209
int ok
Definition contains.c:38
const char * sec
Definition get_bool.c:41
Value vh
Definition get_bool.c:38
Value vsec
Definition get_bool.c:37
const char * key
Definition get_bool.c:40
Value vkey
Definition get_bool.c:36
dictionary * d
Definition get_bool.c:43
dictionary * ini_get(int h)
Look up a dictionary pointer by registry handle.
Definition handles.c:55
void ini_make_full_key(char *buf, size_t cap, const char *sec, const char *key)
Build a fully qualified key "section:key" into a caller-provided buffer.
Definition handles.c:68
free(vals)
char * full
Definition match.c:95
Tagged union representing a Fun value.
Definition value.h:68
char * value_to_string_alloc(const Value *v)
Allocate a printable C string for a Value.
Definition value.c:641
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ VAL_STRING
Definition value.h:53
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580