Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
name.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 name.c
12 * @brief VM opcode snippet for XML node name retrieval.
13 *
14 * This file is included into the main VM dispatch switch in vm.c and
15 * implements the OP_XML_NAME instruction.
16 *
17 * Opcode: OP_XML_NAME
18 * - Stack effect: pop (int node_handle) → push (string name)
19 * - Success: pushes the element/node name as a string. If the node has no
20 * name, an empty string is pushed.
21 * - Errors/edge cases: if the handle is invalid or the node cannot be
22 * retrieved, an empty string is pushed. The instruction does not throw.
23 * - Gating: When FUN_WITH_XML2 is not enabled at build time, the operand is
24 * still popped and an empty string is pushed.
25 *
26 * Notes
27 * - Node handles are small positive integers managed by the XML handle
28 * registry defined in the xml2 extension (see extensions/xml2.c).
29 * - The pushed string is a new VM value; ownership is transferred to the VM
30 * stack as usual.
31 *
32 * Example
33 * - stack: [ node_handle ]
34 * - OP_XML_NAME → [ "book" ]
35 */
36
37/* OP_XML_NAME: pops node handle; pushes string */
39#ifdef FUN_WITH_XML2
40 Value vh = pop_value(vm);
41 int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
42 xmlNodePtr n = xml_node_get(h);
44 const char *name = (n && n->name) ? (const char *)n->name : "";
46#else
47 Value drop = pop_value(vm);
48 free_value(drop);
49 push_value(vm, make_string(""));
50#endif
51 break;
52}
@ OP_XML_NAME
Definition bytecode.h:216
CURL * h
Definition download.c:59
const char * name
Definition env.c:29
Value vh
Definition get_bool.c:38
int n
Definition insert.c:41
free_value(vh)
push_value(vm, make_string(name))
Tagged union representing a Fun value.
Definition value.h:68
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition value.c:95
@ VAL_INT
Definition value.h:51
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
static xmlNodePtr xml_node_get(int h)
Retrieve a registered xmlNode by handle.
Definition xml2.c:120