Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
root.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 root.c
12 * @brief VM opcode snippet for retrieving the root node of an XML document.
13 *
14 * Included by vm.c; implements OP_XML_ROOT.
15 *
16 * Opcode: OP_XML_ROOT
17 * - Stack effect: pop (int doc_handle) → push (int node_handle | 0)
18 * - Behavior: For a valid document handle, obtains the document's root
19 * element and returns a positive node handle. If the document is invalid
20 * or has no root element, 0 is pushed.
21 * - Gating: If FUN_WITH_XML2 is disabled, the input is discarded and 0 is
22 * pushed.
23 *
24 * Notes
25 * - Returned node handles are managed by the XML node registry. They must be
26 * released by corresponding XML VM opcodes to avoid leaks.
27 *
28 * Example
29 * - stack: [ doc_handle ]
30 * - OP_XML_ROOT → [ node_handle ] or [ 0 ]
31 */
32
33/* OP_XML_ROOT: pops doc handle; pushes node handle (>0) or 0 */
35#ifdef FUN_WITH_XML2
36 Value vh = pop_value(vm);
37 int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
38 xmlDocPtr doc = xml_doc_get(h);
40 int nh = 0;
41 if (doc) {
42 xmlNodePtr root = xmlDocGetRootElement(doc);
43 if (root) nh = xml_node_alloc(root);
44 }
46#else
47 Value drop = pop_value(vm);
48 free_value(drop);
49 push_value(vm, make_int(0));
50#endif
51 break;
52}
@ OP_XML_ROOT
Definition bytecode.h:215
CURL * h
Definition download.c:59
json_object * root
Definition from_file.c:44
Value vh
Definition get_bool.c:38
free_value(vh)
push_value(vm, make_int(nh))
int nh
Definition root.c:40
Tagged union representing a Fun value.
Definition value.h:68
Value make_int(int64_t v)
Construct a Value representing a 64-bit integer.
Definition value.c:51
@ 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 int xml_node_alloc(xmlNodePtr n)
Allocate a node handle for the given xmlNode pointer.
Definition xml2.c:104
static xmlDocPtr xml_doc_get(int h)
Retrieve a registered xmlDoc by handle.
Definition xml2.c:78
xmlDocPtr doc
Definition parse.c:50