Fun API Documentation 0.42.1
The programming language that makes you have fun!
Loading...
Searching...
No Matches
redis.c File Reference

Hiredis handle registry and reply mapping helpers for the Fun VM. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Detailed Description

Hiredis handle registry and reply mapping helpers for the Fun VM.

This translation unit provides two small building blocks used by the Redis opcodes (implemented under src/vm/redis/*.c) and included from src/vm.c:

1) A process-local registry that assigns monotonically increasing positive integer identifiers to hiredis connection pointers (redisContext*). VM opcodes pass integer ids on the stack instead of raw pointers, keeping the bytecode portable and preventing accidental misuse of pointers.

2) Utilities to convert hiredis reply objects (redisReply) into Fun VM Value instances, recursively mapping arrays and supporting basic numeric and string types.

Build-time feature flag

The code is compiled only when the CMake option FUN_WITH_REDIS is enabled (i.e., the preprocessor symbol FUN_WITH_REDIS is defined). When disabled, this file contributes no symbols and the corresponding opcodes are compiled into stubs that return neutral values.

Ownership and lifetime

  • The registry does NOT open or close Redis connections by itself; it merely stores pointers created elsewhere (e.g., via redisConnectWithTimeout()).
  • Adding an entry does not transfer ownership of the redisContext. Callers remain responsible for invoking redisFree() at the appropriate time.
  • Removing an entry from the registry does NOT free the connection; it only forgets the mapping between id and pointer. The connect/close opcodes take care of proper ownership transitions.
  • Integer identifiers are monotonically increasing per process. Once a handle id is deleted, it will not be reused within the same process lifetime.

Error handling

Functions here perform basic validation/allocations only. Allocation failures return NULL (for lookups/additions) or are silently ignored (for deletions of non-existent ids). No hiredis API calls are made here, so no hiredis error codes are produced by this module itself.

Thread-safety

The registry is a simple singly-linked list with no synchronization. It is NOT thread-safe. If the VM uses Redis from multiple threads, the caller must provide external synchronization around calls to these helpers.

Example

// Open a hiredis connection elsewhere:
struct timeval tv = { .tv_sec = 2, .tv_usec = 0 };
redisContext *ctx = redisConnectWithTimeout("127.0.0.1", 6379, tv);
if (ctx && !ctx->err) {
// Register and get an id:
RedisHandle *h = redis_reg_add(ctx);
int id = h ? h->id : -1;
// Later look it up:
RedisHandle *same = redis_reg_get(id);
if (same) {
// use same->ctx with hiredis APIs
}
// When finished, drop the registry entry and free manually:
redis_reg_del(id);
redisFree(ctx);
}

Definition in file redis.c.