1
0
Fork 0
forked from fun/fun

Added tons of AI generated docs and a lot of cleanups. No code changes. (0.41.5)

This commit is contained in:
Johannes Findeisen 2026-05-01 02:20:25 +02:00
commit 4e69a3ce63
151 changed files with 3897 additions and 427 deletions

View file

@ -1,21 +1,47 @@
/*
/**
* 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
*/
/**
* @file curl.c
* @brief libcurl helpers and buffers used by HTTP-related VM opcodes.
*
* Added: 2025-12-11 (2025-12-11 migrated from src/vm/libsql/common.c)
* Declares small buffer helpers and libcurl write callbacks that support
* network-related opcodes when FUN_WITH_CURL is enabled.
*/
/* Ensure libcurl headers and helpers are defined at file scope (not inside vm_run) */
#ifdef FUN_WITH_CURL
#include <curl/curl.h>
/**
* @brief Simple growable buffer for libcurl write callbacks.
*
* The buffer grows via realloc as more data arrives. The content is kept
* NUL-terminated for convenience.
*/
typedef struct {
char *d;
size_t n;
char *d; /**< Data pointer (NUL-terminated). */
size_t n; /**< Number of bytes stored (excluding NUL). */
} FunCurlBuf;
/**
* @brief libcurl write callback that appends data to a FunCurlBuf.
*
* Reallocates the destination buffer as needed and keeps it NUL-terminated.
*
* @param ptr Pointer to incoming data block provided by libcurl.
* @param sz Size of each data element.
* @param nm Number of elements in this block.
* @param ud User data; must be a FunCurlBuf*.
* @return Number of bytes actually handled (sz*nm) on success, 0 on failure
* to signal an error to libcurl.
*/
static size_t fun_curl_write_cb(void *ptr, size_t sz, size_t nm, void *ud) {
size_t add = sz * nm;
FunCurlBuf *b = (FunCurlBuf *)ud;
@ -27,6 +53,16 @@ static size_t fun_curl_write_cb(void *ptr, size_t sz, size_t nm, void *ud) {
b->d[b->n] = '\0';
return add;
}
/**
* @brief libcurl write callback that writes directly to a FILE*.
*
* @param ptr Pointer to incoming data.
* @param sz Size of each element.
* @param nm Number of elements.
* @param ud User data; must be a FILE* opened for writing in binary mode.
* @return Number of elements written (as returned by fwrite).
*/
static size_t fun_curl_file_write_cb(void *ptr, size_t sz, size_t nm, void *ud) {
FILE *f = (FILE *)ud;
return fwrite(ptr, sz, nm, f);

View file

@ -1,12 +1,18 @@
/*
/**
* 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
*/
/**
* @file ini.c
* @brief INI parsing helpers and VM opcode support via iniparser.
*
* Added: 2025-12-11 (2025-12-11 migrated from src/vm.c)
* Provides includes and declarations required for INI-related opcodes when
* FUN_WITH_INI is enabled at build time.
*/
#ifdef FUN_WITH_INI

View file

@ -1,12 +1,19 @@
/*
/**
* 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
*/
/**
* @file json.c
* @brief JSON extension helpers and VM opcode cases (conditional build).
*
* Added: 2025-12-11 (2025-12-11 migrated from src/vm.c)
* Provides conversions between json-c objects and the Fun Value type and
* supplies VM opcode case implementations when FUN_WITH_JSON is enabled.
* This unit may be included or compiled conditionally based on build flags.
*/
/* json-c helpers and VM opcode cases (included from vm.c) */
@ -19,6 +26,23 @@
#include <string.h>
/* --- Conversion helpers between json-c and Fun Value --- */
/**
* @brief Convert a json-c object into a Fun Value.
*
* Maps json-c primitive and compound types to the closest Fun Value
* representation.
* - null -> Nil
* - boolean -> Bool
* - number (int/double) -> Int/Float
* - string -> String
* - array -> Array (recursively converted)
* - object -> Map<string,any> (values recursively converted)
*
* @param j Pointer to a json_object; may be NULL.
* @return A Value representing the converted JSON data. Ownership of the
* returned Value belongs to the caller and must be freed with
* free_value() when no longer needed.
*/
static Value json_to_fun(json_object *j) {
if (!j) return make_nil();
enum json_type t = json_object_get_type(j);
@ -62,6 +86,16 @@ static Value json_to_fun(json_object *j) {
}
}
/**
* @brief Convert a Fun Value into a json-c object.
*
* Produces a newly-allocated json_object tree representing the supplied
* Value. Unsupported Fun types are stringified using a placeholder.
*
* @param v Pointer to the source Value. Must not be NULL.
* @return Newly created json_object* on success. The caller owns the
* returned object and must release it with json_object_put().
*/
static json_object *fun_to_json(const Value *v) {
switch (v->type) {
case VAL_NIL:

View file

@ -1,12 +1,20 @@
/*
/**
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2026 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*/
/**
* @file openssl.c
* @brief OpenSSL-based hashing helpers used by crypto-related opcodes.
*
* Added: 2026-02-19
* Provides thin wrappers around OpenSSL EVP routines to compute common
* digests (MD5, RIPEMD-160, SHA-256, SHA-512). When FUN_WITH_OPENSSL is
* disabled, these helpers fall back to returning empty strings to keep the
* VM behavior consistent.
*/
/*
@ -23,10 +31,16 @@ int EVP_MD_get_size(const EVP_MD *md);
#endif
#include <stdlib.h>
/* Compute MD5 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Caller must free(). Returns NULL on failure. When OpenSSL is disabled,
* returns an allocated empty string ("") to keep behavior consistent with
* other optional extensions. */
/**
* @brief Compute MD5 digest and return it as a lowercase hex string.
*
* @param data Pointer to input bytes (may be NULL if len==0).
* @param len Number of input bytes.
* @return Newly-allocated NUL-terminated hex string on success; NULL on
* failure. When FUN_WITH_OPENSSL is disabled, returns an allocated
* empty string to keep behavior consistent.
* @note The caller owns the returned buffer and must free() it.
*/
static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
@ -68,8 +82,15 @@ static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
#endif
}
/* Compute SHA-256 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Fallback when OpenSSL disabled: empty string. */
/**
* @brief Compute SHA-256 digest and return it as a lowercase hex string.
*
* @param data Pointer to input bytes (may be NULL if len==0).
* @param len Number of input bytes.
* @return Newly-allocated hex string on success; NULL on failure. When
* FUN_WITH_OPENSSL is disabled, returns an allocated empty string.
* @note The caller must free() the returned buffer.
*/
static char *fun_openssl_sha256_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
@ -110,8 +131,15 @@ static char *fun_openssl_sha256_hex(const unsigned char *data, size_t len) {
#endif
}
/* Compute SHA-512 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Fallback when OpenSSL disabled: empty string. */
/**
* @brief Compute SHA-512 digest and return it as a lowercase hex string.
*
* @param data Pointer to input bytes (may be NULL if len==0).
* @param len Number of input bytes.
* @return Newly-allocated hex string on success; NULL on failure. When
* FUN_WITH_OPENSSL is disabled, returns an allocated empty string.
* @note The caller must free() the returned buffer.
*/
static char *fun_openssl_sha512_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
@ -152,10 +180,19 @@ static char *fun_openssl_sha512_hex(const unsigned char *data, size_t len) {
#endif
}
/* Compute RIPEMD-160 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Note: On OpenSSL 3.x, RIPEMD160 may require the legacy provider; if unavailable,
* EVP_ripemd160() can return NULL. In that case we return NULL and the VM opcode
* will fall back to empty string behavior. When OpenSSL is disabled, return empty string. */
/**
* @brief Compute RIPEMD-160 digest and return it as a lowercase hex string.
*
* On some OpenSSL builds (e.g., 3.x without legacy provider), RIPEMD-160 may
* be unavailable and EVP_ripemd160() can return NULL.
*
* @param data Pointer to input bytes (may be NULL if len==0).
* @param len Number of input bytes.
* @return Newly-allocated hex string on success; NULL if the digest is
* unavailable or another error occurs. When FUN_WITH_OPENSSL is
* disabled, returns an allocated empty string.
* @note The caller must free() the returned buffer.
*/
static char *fun_openssl_ripemd160_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;

View file

@ -1,12 +1,18 @@
/*
/**
* 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
*/
/**
* @file pcre2.c
* @brief PCRE2 configuration header and includes for regex-related opcodes.
*
* Added: 2025-12-11 (2025-12-11 migrated from src/vm/libsql/common.c)
* Ensures PCRE2 code unit width is defined consistently prior to including
* <pcre2.h> when FUN_WITH_PCRE2 is enabled.
*/
/* Ensure PCRE2 is configured consistently across the whole translation unit.

View file

@ -1,18 +1,20 @@
/*
/**
* 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-11 (2025-12-11 migrated from src/vm.c)
*/
/*
PCSC helpers: registries and helper functions.
Included the file scope from vm.c.
*/
/**
* @file pcsc.c
* @brief PC/SC smartcard helper registries and lookup utilities.
*
* Provides small fixed-size registries for PC/SC contexts and card handles,
* plus allocation and lookup helpers used by VM opcodes when FUN_WITH_PCSC
* is enabled.
*/
#ifdef FUN_WITH_PCSC
#if defined(__has_include)
@ -44,6 +46,11 @@ typedef struct {
static pcsc_ctx_entry g_pcsc_ctx[8];
static pcsc_card_entry g_pcsc_card[32];
/**
* @brief Allocate a free context slot in the PC/SC registry.
*
* @return A 1-based slot id on success, or 0 if no free slot is available.
*/
static int pcsc_alloc_ctx_slot(void) {
for (int i = 0; i < (int)(sizeof(g_pcsc_ctx) / sizeof(g_pcsc_ctx[0])); ++i) {
if (!g_pcsc_ctx[i].in_use) {
@ -55,6 +62,11 @@ static int pcsc_alloc_ctx_slot(void) {
return 0;
}
/**
* @brief Allocate a free card slot in the PC/SC registry.
*
* @return A 1-based slot id on success, or 0 if no free slot is available.
*/
static int pcsc_alloc_card_slot(void) {
for (int i = 0; i < (int)(sizeof(g_pcsc_card) / sizeof(g_pcsc_card[0])); ++i) {
if (!g_pcsc_card[i].in_use) {
@ -67,6 +79,12 @@ static int pcsc_alloc_card_slot(void) {
return 0;
}
/**
* @brief Lookup a context slot by id.
*
* @param id 1-based context id previously returned by pcsc_alloc_ctx_slot().
* @return Pointer to the registry entry if valid and in use; NULL otherwise.
*/
static pcsc_ctx_entry *pcsc_get_ctx(int id) {
if (id <= 0) return NULL;
int idx = id - 1;
@ -75,6 +93,12 @@ static pcsc_ctx_entry *pcsc_get_ctx(int id) {
return &g_pcsc_ctx[idx];
}
/**
* @brief Lookup a card slot by id.
*
* @param id 1-based card id previously returned by pcsc_alloc_card_slot().
* @return Pointer to the registry entry if valid and in use; NULL otherwise.
*/
static pcsc_card_entry *pcsc_get_card(int id) {
if (id <= 0) return NULL;
int idx = id - 1;

View file

@ -1,29 +1,56 @@
/*
/**
* 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-11 (2025-12-11 migrated from src/vm/sqlite/common.c)
*/
/**
* SQLite handle registry and helpers
* @file sqlite.c
* @brief SQLite handle registry and helper utilities for the Fun VM extension.
*
* This module provides a very small registry for SQLite database handles when
* the project is compiled with FUN_WITH_SQLITE. It assigns small integer
* identifiers to opened sqlite3 connections and allows retrieval or removal of
* those entries. The registry itself does not open or close SQLite databases
* it only stores pointers provided by the caller.
*
* Thread-safety: This registry is not thread-safe. Callers must ensure
* external synchronization if used from multiple threads.
*/
#ifdef FUN_WITH_SQLITE
#include <sqlite3.h>
/**
* @brief Node in a singly-linked list of registered SQLite handles.
*/
typedef struct SqlHandle {
int id;
sqlite3 *db;
struct SqlHandle *next;
} SqlHandle;
/** Global head of the SQLite handle list. */
static SqlHandle *g_sql_handles = NULL;
/** Next positive identifier to assign to a newly added handle. */
static int g_sql_next_id = 1;
/**
* @brief Add a sqlite3 handle to the registry.
*
* The function allocates a new list node, assigns a fresh positive id, and
* prepends it to the internal registry list.
*
* @param db Valid pointer to an opened sqlite3 connection.
* @return Pointer to the newly created SqlHandle entry on success; NULL on
* allocation failure. The returned pointer remains owned by the
* registry; do not free it directly.
* @note This function does not take ownership of the sqlite3 connection in the
* sense of closing it; removal from the registry will not call
* sqlite3_close().
*/
static SqlHandle *sql_reg_add(sqlite3 *db) {
SqlHandle *h = (SqlHandle *)calloc(1, sizeof(SqlHandle));
if (!h) return NULL;
@ -34,12 +61,27 @@ static SqlHandle *sql_reg_add(sqlite3 *db) {
return h;
}
/**
* @brief Look up a registered SQLite handle by id.
*
* @param id Positive identifier previously returned by sql_reg_add().
* @return Pointer to the SqlHandle entry if found; NULL otherwise.
*/
static SqlHandle *sql_reg_get(int id) {
for (SqlHandle *p = g_sql_handles; p; p = p->next)
if (p->id == id) return p;
return NULL;
}
/**
* @brief Remove a SQLite handle entry from the registry.
*
* Deletes the list node associated with the given id.
*
* @param id Positive identifier of the entry to remove.
* @note This function does not close the underlying sqlite3 connection; the
* caller is responsible for calling sqlite3_close() if appropriate.
*/
static void sql_reg_del(int id) {
SqlHandle **pp = &g_sql_handles;
while (*pp) {

View file

@ -1,22 +1,48 @@
/*
/**
* 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-11 (2025-12-11 migrated from src/vm/libsql/common.c)
*/
/**
* @file xml2.c
* @brief Lightweight libxml2 handle registry for Fun VM extension helpers.
*
* This module provides small fixed-size registries for libxml2 objects when
* compiled with FUN_WITH_XML2. Documents and nodes can be associated with
* small integer handles to make them easier to pass around inside the
* interpreter and its C API boundaries. The document registry owns the
* underlying xmlDoc, while the node registry does not own the xmlNode nodes
* are freed when their owning document is freed.
*
* Limits: The registries are fixed-size (docs: 64, nodes: 256). Handle value 0
* is reserved and indicates failure/invalid.
*
* Thread-safety: Not thread-safe. Coordinate access externally if needed.
*/
#ifdef FUN_WITH_XML2
#include <libxml/parser.h>
#include <libxml/tree.h>
/**
* @brief Slot describing a registered XML document.
*
* The registry owns the xmlDocPtr and will free it when the handle is
* released via xml_doc_free_handle().
*/
typedef struct {
xmlDocPtr doc;
int in_use;
} XmlDocSlot;
/**
* @brief Slot describing a registered XML node.
*
* The registry does not own the node; it is managed by its document. Releasing
* a node handle does not free the node memory.
*/
typedef struct {
xmlNodePtr node;
int in_use;
@ -25,6 +51,14 @@ typedef struct {
static XmlDocSlot g_xml_docs[64];
static XmlNodeSlot g_xml_nodes[256];
/**
* @brief Allocate a document handle for the given xmlDoc pointer.
*
* @param d Valid xmlDocPtr to register. Ownership is transferred to the
* registry, which will xmlFreeDoc() it when the handle is freed.
* @return Positive handle in the range [1, 63] on success; 0 if no slot is
* available.
*/
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) {
@ -35,10 +69,22 @@ static int xml_doc_alloc(xmlDocPtr d) {
}
return 0;
}
/**
* @brief Retrieve a registered xmlDoc by handle.
*
* @param h Handle previously returned by xml_doc_alloc().
* @return xmlDocPtr if the handle is valid and in use; NULL otherwise.
*/
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;
}
/**
* @brief Free a document handle and the underlying xmlDoc.
*
* @param h Handle to release.
* @return 1 if the handle was valid and has been released; 0 otherwise.
*/
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);
@ -47,6 +93,14 @@ static int xml_doc_free_handle(int h) {
return 1;
}
/**
* @brief Allocate a node handle for the given xmlNode pointer.
*
* @param n Valid xmlNodePtr to register. Ownership is NOT transferred;
* nodes are managed by their owning document.
* @return Positive handle in the range [1, 255] on success; 0 if no slot is
* available.
*/
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) {
@ -57,10 +111,25 @@ static int xml_node_alloc(xmlNodePtr n) {
}
return 0;
}
/**
* @brief Retrieve a registered xmlNode by handle.
*
* @param h Handle previously returned by xml_node_alloc().
* @return xmlNodePtr if the handle is valid and in use; NULL otherwise.
*/
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;
}
/**
* @brief Free a node handle without freeing the underlying node.
*
* Nodes are owned by their document; releasing the document invalidates any
* associated node handles.
*
* @param h Handle to release.
* @return 1 if the handle was valid and has been released; 0 otherwise.
*/
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 */