Fun 0.41.5
The programming language that makes you have fun!
Loading...
Searching...
No Matches
parse.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 parse.c
12 * @brief VM opcode snippet for parsing XML text into a document handle.
13 *
14 * This snippet is included by vm.c and implements OP_XML_PARSE.
15 *
16 * Opcode: OP_XML_PARSE
17 * - Stack effect: pop (string xml_text) → push (int doc_handle | 0)
18 * - Behavior: Parses the given XML string into an in-memory xmlDoc.
19 * On success, a positive document handle is allocated via the XML handle
20 * registry and pushed. On failure (malformed XML, OOM), 0 is pushed.
21 * - Initialization: Ensures libxml2 is initialized once (xmlInitParser()).
22 * - Gating: If FUN_WITH_XML2 is disabled, the input is discarded and 0 is
23 * pushed.
24 *
25 * Notes
26 * - The handle must later be released via appropriate XML VM opcodes that
27 * free documents; leaking handles will leak memory.
28 * - The parser is invoked with XML_PARSE_NONET to disallow network access.
29 *
30 * Example
31 * - stack: [ "<root/>" ]
32 * - OP_XML_PARSE → [ 1 ] (example handle)
33 */
34
35/* OP_XML_PARSE: pops text string; pushes doc handle (>0) or 0 */
37#ifdef FUN_WITH_XML2
38 static int xml_inited = 0;
39 if (!xml_inited) {
40 xmlInitParser();
41 xml_inited = 1;
42 }
46 if (!text) {
47 push_value(vm, make_int(0));
48 break;
49 }
50 xmlDocPtr doc = xmlReadMemory(text, (int)strlen(text), NULL, NULL, XML_PARSE_NONET);
52 int h = 0;
53 if (doc) {
55 if (!h) {
56 xmlFreeDoc(doc);
57 }
58 }
60#else
61 Value drop = pop_value(vm);
62 free_value(drop);
63 push_value(vm, make_int(0));
64#endif
65 break;
66}
@ OP_XML_PARSE
Definition bytecode.h:214
CURL * h
Definition download.c:59
Value vtext
Definition findall.c:48
free(s)
free_value(text)
push_value(vm, v)
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
static Value pop_value(VM *vm)
Pop a Value from the VM operand stack.
Definition vm.c:580
static int xml_doc_alloc(xmlDocPtr d)
Allocate a document handle for the given xmlDoc pointer.
Definition xml2.c:62
char * text
Definition parse.c:44
xmlDocPtr doc
Definition parse.c:50