1
0
Fork 0
forked from fun/fun

Renamed ./src/external to ./src/extensions. No code logic changes. (0.40.5)

This commit is contained in:
Johannes Findeisen 2026-04-19 00:02:36 +02:00
commit bb704a3778
25 changed files with 81 additions and 414 deletions

34
src/extensions/curl.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-11 (2025-12-11 migrated from src/vm/libsql/common.c)
*/
/* Ensure libcurl headers and helpers are defined at file scope (not inside vm_run) */
#ifdef FUN_WITH_CURL
#include <curl/curl.h>
typedef struct {
char *d;
size_t n;
} FunCurlBuf;
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;
char *p = (char *)realloc(b->d, b->n + add + 1);
if (!p) return 0;
memcpy(p + b->n, ptr, add);
b->d = p;
b->n += add;
b->d[b->n] = '\0';
return add;
}
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);
}
#endif

28
src/extensions/ini.c Normal file
View file

@ -0,0 +1,28 @@
/*
* 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)
*/
#ifdef FUN_WITH_INI
#if defined(__has_include)
#if __has_include(<iniparser/iniparser.h>)
#include <iniparser/dictionary.h>
#include <iniparser/iniparser.h>
#elif __has_include(<iniparser.h>)
#include <dictionary.h>
#include <iniparser.h>
#else
#error "iniparser headers not found"
#endif
#else
#include <iniparser/dictionary.h>
#include <iniparser/iniparser.h>
#endif
#include "vm/ini/handles.h"
#endif

118
src/extensions/json.c Normal file
View file

@ -0,0 +1,118 @@
/*
* 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)
*/
/* json-c helpers and VM opcode cases (included from vm.c) */
#ifdef FUN_WITH_JSON
#include "value.h"
#include "vm.h"
#include <json-c/json.h>
#include <string.h>
/* --- Conversion helpers between json-c and Fun Value --- */
static Value json_to_fun(json_object *j) {
if (!j) return make_nil();
enum json_type t = json_object_get_type(j);
switch (t) {
case json_type_null:
return make_nil();
case json_type_boolean:
return make_bool(json_object_get_boolean(j));
case json_type_double:
return make_float(json_object_get_double(j));
case json_type_int:
return make_int((int64_t)json_object_get_int64(j));
case json_type_string:
return make_string(json_object_get_string(j));
case json_type_array: {
size_t n = json_object_array_length(j);
if (n == 0) {
return make_array_from_values(NULL, 0);
}
Value *vals = (Value *)malloc(sizeof(Value) * n);
if (!vals) return make_array_from_values(NULL, 0);
for (size_t i = 0; i < n; ++i) {
json_object *item = json_object_array_get_idx(j, (int)i);
vals[i] = json_to_fun(item);
}
Value arr = make_array_from_values(vals, (int)n);
for (size_t i = 0; i < n; ++i)
free_value(vals[i]);
free(vals);
return arr;
}
case json_type_object: {
Value map = make_map_empty();
json_object_object_foreach(j, key, val) {
(void)map_set(&map, key, json_to_fun(val));
}
return map;
}
default:
return make_nil();
}
}
static json_object *fun_to_json(const Value *v) {
switch (v->type) {
case VAL_NIL:
return json_object_new_null();
case VAL_BOOL:
return json_object_new_boolean(v->i ? 1 : 0);
case VAL_INT:
return json_object_new_int64(v->i);
case VAL_FLOAT:
return json_object_new_double(v->d);
case VAL_STRING:
return json_object_new_string(v->s ? v->s : "");
case VAL_ARRAY: {
json_object *arr = json_object_new_array();
int n = array_length(v);
for (int i = 0; i < n; ++i) {
Value item;
if (array_get_copy(v, i, &item)) {
json_object_array_add(arr, fun_to_json(&item));
free_value(item);
} else {
json_object_array_add(arr, json_object_new_null());
}
}
return arr;
}
case VAL_MAP: {
json_object *obj = json_object_new_object();
/* We don't have an iterator API; use keys() helper */
Value keys = map_keys_array(v);
int kn = array_length(&keys);
for (int i = 0; i < kn; ++i) {
Value k;
if (!array_get_copy(&keys, i, &k)) continue;
if (k.type == VAL_STRING && k.s) {
Value val;
if (map_get_copy(v, k.s, &val)) {
json_object_object_add(obj, k.s, fun_to_json(&val));
free_value(val);
} else {
json_object_object_add(obj, k.s, json_object_new_null());
}
}
free_value(k);
}
free_value(keys);
return obj;
}
default:
/* Fallback: stringify unsupported types */
return json_object_new_string("<unsupported>");
}
}
#endif

197
src/extensions/openssl.c Normal file
View file

@ -0,0 +1,197 @@
/*
* 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
*
* Added: 2026-02-19
*/
/*
* OpenSSL integration helpers (MD5, RIPEMD-160, SHA-256, SHA-512)
*/
#ifdef FUN_WITH_OPENSSL
#include <openssl/evp.h>
/* Always use EVP_MD_get_size; if headers don't declare it, provide a
* forward declaration to allow linking against OpenSSL libcrypto. */
#ifndef EVP_MD_get_size
int EVP_MD_get_size(const EVP_MD *md);
#endif
#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. */
static char *fun_openssl_md5_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_OPENSSL
const EVP_MD *md = EVP_md5();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char *)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
// EVP_Digest handles zero-length fine as well
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) {
free(digest);
return NULL;
}
char *hex = (char *)malloc((size_t)dlen * 2 + 1);
if (!hex) {
free(digest);
return NULL;
}
for (int i = 0; i < dlen; ++i) {
hex[2 * i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2 * i + 1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char *)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}
/* Compute SHA-256 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Fallback when OpenSSL disabled: empty string. */
static char *fun_openssl_sha256_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_OPENSSL
const EVP_MD *md = EVP_sha256();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char *)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) {
free(digest);
return NULL;
}
char *hex = (char *)malloc((size_t)dlen * 2 + 1);
if (!hex) {
free(digest);
return NULL;
}
for (int i = 0; i < dlen; ++i) {
hex[2 * i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2 * i + 1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char *)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}
/* Compute SHA-512 hex of input buffer; returns malloc'ed C string (lowercase hex).
* Fallback when OpenSSL disabled: empty string. */
static char *fun_openssl_sha512_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_OPENSSL
const EVP_MD *md = EVP_sha512();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char *)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) {
free(digest);
return NULL;
}
char *hex = (char *)malloc((size_t)dlen * 2 + 1);
if (!hex) {
free(digest);
return NULL;
}
for (int i = 0; i < dlen; ++i) {
hex[2 * i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2 * i + 1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char *)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#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. */
static char *fun_openssl_ripemd160_hex(const unsigned char *data, size_t len) {
static const char hexdig[] = "0123456789abcdef";
if (!data && len != 0) return NULL;
#ifdef FUN_WITH_OPENSSL
const EVP_MD *md = EVP_ripemd160();
if (!md) return NULL;
int dlen = EVP_MD_get_size(md);
if (dlen <= 0) return NULL;
unsigned char *digest = (unsigned char *)malloc((size_t)dlen);
if (!digest) return NULL;
unsigned int out_len = 0;
int ok;
if (len == 0) {
ok = EVP_Digest(NULL, 0, digest, &out_len, md, NULL);
} else {
ok = EVP_Digest(data, len, digest, &out_len, md, NULL);
}
if (ok != 1 || (int)out_len != dlen) {
free(digest);
return NULL;
}
char *hex = (char *)malloc((size_t)dlen * 2 + 1);
if (!hex) {
free(digest);
return NULL;
}
for (int i = 0; i < dlen; ++i) {
hex[2 * i] = hexdig[(digest[i] >> 4) & 0xF];
hex[2 * i + 1] = hexdig[digest[i] & 0xF];
}
hex[dlen * 2] = '\0';
free(digest);
return hex;
#else
char *hex = (char *)malloc(1);
if (hex) hex[0] = '\0';
return hex;
#endif
}

22
src/extensions/pcre2.c Normal file
View file

@ -0,0 +1,22 @@
/*
* 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)
*/
/* Ensure PCRE2 is configured consistently across the whole translation unit.
* vm.c includes many opcode implementation .c files; some use PCRE2. For PCRE2
* headers to expose the correct typedefs (e.g., pcre2_code, PCRE2_SPTR), the
* PCRE2_CODE_UNIT_WIDTH macro must be defined before the first inclusion of
* <pcre2.h>. We do this once here when PCRE2 support is enabled. */
#ifdef FUN_WITH_PCRE2
#ifndef PCRE2_CODE_UNIT_WIDTH
#define PCRE2_CODE_UNIT_WIDTH 8
#endif
#include <pcre2.h>
#endif

85
src/extensions/pcsc.c Normal file
View file

@ -0,0 +1,85 @@
/*
* 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.
*/
#ifdef FUN_WITH_PCSC
#if defined(__has_include)
#if __has_include(<PCSC/winscard.h>)
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#elif __has_include(<winscard.h>)
#include <winscard.h>
#else
#error "FUN_WITH_PCSC is enabled but PCSC headers were not found"
#endif
#else
#include <PCSC/winscard.h>
#include <PCSC/wintypes.h>
#endif
#include <string.h>
typedef struct {
SCARDCONTEXT ctx;
int in_use;
} pcsc_ctx_entry;
typedef struct {
SCARDHANDLE h;
DWORD proto;
int in_use;
} pcsc_card_entry;
static pcsc_ctx_entry g_pcsc_ctx[8];
static pcsc_card_entry g_pcsc_card[32];
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) {
g_pcsc_ctx[i].in_use = 1;
g_pcsc_ctx[i].ctx = 0;
return i + 1;
}
}
return 0;
}
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) {
g_pcsc_card[i].in_use = 1;
g_pcsc_card[i].h = 0;
g_pcsc_card[i].proto = 0;
return i + 1;
}
}
return 0;
}
static pcsc_ctx_entry *pcsc_get_ctx(int id) {
if (id <= 0) return NULL;
int idx = id - 1;
if (idx < 0 || idx >= (int)(sizeof(g_pcsc_ctx) / sizeof(g_pcsc_ctx[0]))) return NULL;
if (!g_pcsc_ctx[idx].in_use) return NULL;
return &g_pcsc_ctx[idx];
}
static pcsc_card_entry *pcsc_get_card(int id) {
if (id <= 0) return NULL;
int idx = id - 1;
if (idx < 0 || idx >= (int)(sizeof(g_pcsc_card) / sizeof(g_pcsc_card[0]))) return NULL;
if (!g_pcsc_card[idx].in_use) return NULL;
return &g_pcsc_card[idx];
}
#endif

55
src/extensions/sqlite.c Normal file
View file

@ -0,0 +1,55 @@
/*
* 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
*/
#ifdef FUN_WITH_SQLITE
#include <sqlite3.h>
typedef struct SqlHandle {
int id;
sqlite3 *db;
struct SqlHandle *next;
} SqlHandle;
static SqlHandle *g_sql_handles = NULL;
static int g_sql_next_id = 1;
static SqlHandle *sql_reg_add(sqlite3 *db) {
SqlHandle *h = (SqlHandle *)calloc(1, sizeof(SqlHandle));
if (!h) return NULL;
h->id = g_sql_next_id++;
h->db = db;
h->next = g_sql_handles;
g_sql_handles = h;
return h;
}
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;
}
static void sql_reg_del(int id) {
SqlHandle **pp = &g_sql_handles;
while (*pp) {
if ((*pp)->id == id) {
SqlHandle *d = *pp;
*pp = d->next;
free(d);
return;
}
pp = &(*pp)->next;
}
}
#endif

71
src/extensions/xml2.c Normal file
View file

@ -0,0 +1,71 @@
/*
* 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)
*/
#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