1
0
Fork 0
forked from fun/fun

Added very basic XML support using libxml2. (0.35.0)

This commit is contained in:
Johannes Findeisen 2025-12-09 03:59:49 +01:00
commit e65756226d
17 changed files with 436 additions and 3 deletions

60
src/vm/xml/handles.h Normal file
View file

@ -0,0 +1,60 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
/** Minimal handle registries for libxml2 documents and nodes */
#pragma once
#ifdef FUN_WITH_XML2
#include <libxml/parser.h>
#include <libxml/tree.h>
typedef struct { xmlDocPtr doc; int in_use; } XmlDocSlot;
typedef struct { xmlNodePtr node; int in_use; } XmlNodeSlot;
static XmlDocSlot g_xml_docs[64];
static XmlNodeSlot g_xml_nodes[256];
static int xml_doc_alloc(xmlDocPtr d) {
for (int i = 1; i < (int)(sizeof(g_xml_docs)/sizeof(g_xml_docs[0])); ++i) {
if (!g_xml_docs[i].in_use) { g_xml_docs[i].in_use = 1; g_xml_docs[i].doc = d; return i; }
}
return 0;
}
static xmlDocPtr xml_doc_get(int h) {
if (h > 0 && h < (int)(sizeof(g_xml_docs)/sizeof(g_xml_docs[0])) && g_xml_docs[h].in_use) return g_xml_docs[h].doc;
return NULL;
}
static int xml_doc_free_handle(int h) {
if (h <= 0 || h >= (int)(sizeof(g_xml_docs)/sizeof(g_xml_docs[0])) || !g_xml_docs[h].in_use) return 0;
if (g_xml_docs[h].doc) xmlFreeDoc(g_xml_docs[h].doc);
g_xml_docs[h].doc = NULL;
g_xml_docs[h].in_use = 0;
return 1;
}
static int xml_node_alloc(xmlNodePtr n) {
for (int i = 1; i < (int)(sizeof(g_xml_nodes)/sizeof(g_xml_nodes[0])); ++i) {
if (!g_xml_nodes[i].in_use) { g_xml_nodes[i].in_use = 1; g_xml_nodes[i].node = n; return i; }
}
return 0;
}
static xmlNodePtr xml_node_get(int h) {
if (h > 0 && h < (int)(sizeof(g_xml_nodes)/sizeof(g_xml_nodes[0])) && g_xml_nodes[h].in_use) return g_xml_nodes[h].node;
return NULL;
}
static int xml_node_free_handle(int h) {
if (h <= 0 || h >= (int)(sizeof(g_xml_nodes)/sizeof(g_xml_nodes[0])) || !g_xml_nodes[h].in_use) return 0;
/* nodes are owned by their document; do not free here */
g_xml_nodes[h].node = NULL;
g_xml_nodes[h].in_use = 0;
return 1;
}
#endif /* FUN_WITH_XML2 */

26
src/vm/xml/name.c Normal file
View file

@ -0,0 +1,26 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
/* OP_XML_NAME: pops node handle; pushes string */
case OP_XML_NAME: {
#ifdef FUN_WITH_XML2
Value vh = pop_value(vm);
int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
xmlNodePtr n = xml_node_get(h);
free_value(vh);
const char *name = (n && n->name) ? (const char*)n->name : "";
push_value(vm, make_string(name));
#else
Value drop = pop_value(vm); free_value(drop);
push_value(vm, make_string(""));
#endif
break;
}

34
src/vm/xml/parse.c Normal file
View file

@ -0,0 +1,34 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
/* OP_XML_PARSE: pops text string; pushes doc handle (>0) or 0 */
case OP_XML_PARSE: {
#ifdef FUN_WITH_XML2
static int xml_inited = 0;
if (!xml_inited) { xmlInitParser(); xml_inited = 1; }
Value vtext = pop_value(vm);
char *text = value_to_string_alloc(&vtext);
free_value(vtext);
if (!text) { push_value(vm, make_int(0)); break; }
xmlDocPtr doc = xmlReadMemory(text, (int)strlen(text), NULL, NULL, XML_PARSE_NONET);
free(text);
int h = 0;
if (doc) {
h = xml_doc_alloc(doc);
if (!h) { xmlFreeDoc(doc); }
}
push_value(vm, make_int(h));
#else
Value drop = pop_value(vm); free_value(drop);
push_value(vm, make_int(0));
#endif
break;
}

30
src/vm/xml/root.c Normal file
View file

@ -0,0 +1,30 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
/* OP_XML_ROOT: pops doc handle; pushes node handle (>0) or 0 */
case OP_XML_ROOT: {
#ifdef FUN_WITH_XML2
Value vh = pop_value(vm);
int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
xmlDocPtr doc = xml_doc_get(h);
free_value(vh);
int nh = 0;
if (doc) {
xmlNodePtr root = xmlDocGetRootElement(doc);
if (root) nh = xml_node_alloc(root);
}
push_value(vm, make_int(nh));
#else
Value drop = pop_value(vm); free_value(drop);
push_value(vm, make_int(0));
#endif
break;
}

29
src/vm/xml/text.c Normal file
View file

@ -0,0 +1,29 @@
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
/* OP_XML_TEXT: pops node handle; pushes string (concatenate text node children) */
case OP_XML_TEXT: {
#ifdef FUN_WITH_XML2
Value vh = pop_value(vm);
int h = (vh.type == VAL_INT) ? (int)vh.i : 0;
xmlNodePtr n = xml_node_get(h);
free_value(vh);
if (!n) { push_value(vm, make_string("")); break; }
xmlChar *content = xmlNodeGetContent(n);
if (!content) { push_value(vm, make_string("")); break; }
push_value(vm, make_string((const char*)content));
xmlFree(content);
#else
Value drop = pop_value(vm); free_value(drop);
push_value(vm, make_string(""));
#endif
break;
}