Fun
0.41.5
The programming language that makes you have fun!
Main Page
Data Structures
Files
File List
Globals
Loading...
Searching...
No Matches
src
vm
xml
text.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 text.c
12
* @brief VM opcode snippet for retrieving the concatenated text of an XML node.
13
*
14
* Included by vm.c; implements OP_XML_TEXT.
15
*
16
* Opcode: OP_XML_TEXT
17
* - Stack effect: pop (int node_handle) → push (string text)
18
* - Behavior: Retrieves the node's textual content as produced by
19
* xmlNodeGetContent(), which concatenates all descendant text nodes.
20
* If the node is invalid or has no textual content, an empty string is
21
* pushed.
22
* - Gating: If FUN_WITH_XML2 is disabled, the input is discarded and an empty
23
* string is pushed.
24
*
25
* Notes
26
* - The returned string is copied into a VM string value; libxml2 buffers are
27
* freed immediately after use.
28
*
29
* Example
30
* - stack: [ node_handle ]
31
* - OP_XML_TEXT → [ "Hello world" ]
32
*/
33
34
/* OP_XML_TEXT: pops node handle; pushes string (concatenate text node children) */
35
case
OP_XML_TEXT
: {
36
#ifdef FUN_WITH_XML2
37
Value
vh
=
pop_value
(vm);
38
int
h
= (
vh
.type ==
VAL_INT
) ? (
int
)
vh
.i : 0;
39
xmlNodePtr
n
=
xml_node_get
(
h
);
40
free_value
(
vh
);
41
if
(!
n
) {
42
push_value
(vm,
make_string
(
""
));
43
break
;
44
}
45
xmlChar *
content
= xmlNodeGetContent(
n
);
46
if
(!
content
) {
47
push_value
(vm,
make_string
(
""
));
48
break
;
49
}
50
push_value
(vm,
make_string
((
const
char
*)
content
));
51
xmlFree
(
content
);
52
#else
53
Value
drop =
pop_value
(vm);
54
free_value
(drop);
55
push_value
(vm,
make_string
(
""
));
56
#endif
57
break
;
58
}
OP_XML_TEXT
@ OP_XML_TEXT
Definition
bytecode.h:217
h
CURL * h
Definition
download.c:59
vh
Value vh
Definition
get_bool.c:38
n
int n
Definition
insert.c:41
Value
Tagged union representing a Fun value.
Definition
value.h:68
content
xmlChar * content
Definition
text.c:45
free_value
free_value(vh)
xmlFree
xmlFree(content)
push_value
push_value(vm, make_string((const char *) content))
make_string
Value make_string(const char *s)
Construct a string Value by duplicating the given C string.
Definition
value.c:95
VAL_INT
@ VAL_INT
Definition
value.h:51
pop_value
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition
vm.c:580
xml_node_get
static xmlNodePtr xml_node_get(int h)
Retrieve a registered xmlNode by handle.
Definition
xml2.c:120
Generated on
for Fun by
1.16.1