Phasor 3.1.1
Stack VM based Programming Language
Loading...
Searching...
No Matches
PhasorFFI.h
Go to the documentation of this file.
1#ifndef PHASOR_FFI_HPP
2#define PHASOR_FFI_HPP
3
4#ifdef __cplusplus
5#include <cstdint>
6#include <cstddef>
7#else
8#include <stdint.h>
9#include <stddef.h>
10#include <stdbool.h>
11#endif
12
13/*
14 * Phasor Foreign Function Interface
15 *
16 * This header provides the necessary definitions for creating third-party
17 * plugins for the Phasor scripting engine. It is designed to be a stable,
18 * C-compatible interface.
19 *
20 * USAGE:
21 * 1. Define PHASOR_FFI_BUILD_DLL.
22 * 2. Include this header in your C or C++ source file.
23 * 3. Implement the entry point function:
24 * void phasor_plugin_entry(const PhasorAPI* api, PhasorVM* vm);
25 * 4. Inside this function, use the provided `api` object to register
26 * your own native functions.
27 * 5. Compile your code as a shared library (.dll, .so, .dylib).
28 */
29
30#if defined(_WIN32) && defined(PHASOR_FFI_BUILD_DLL)
31#define PHASOR_FFI_EXPORT __declspec(dllexport)
32#elif defined(__GNUC__) || defined(__clang__)
33#define PHASOR_FFI_EXPORT __attribute__((visibility("default")))
34#else
35#define PHASOR_FFI_EXPORT
36#endif
37
38#ifdef __cplusplus
39extern "C"
40{
41#endif
42
44 typedef struct PhasorVM PhasorVM;
45
47 typedef enum
48 {
55 // Note: Structs are not directly supported in this version of the FFI
56 // for simplicity, but can be added in the future.
58
60 typedef struct PhasorValue PhasorValue;
61
64 {
66 union {
67 bool b;
68 int64_t i;
69 double f;
70 const char *s; // Note: For strings returned from the VM, this is valid.
71 // For strings passed to the VM, the VM makes a copy.
72 struct
73 {
75 size_t count;
76 } a;
77 } as;
78 };
79
80 // -----------------------------------------------------------------------------
81 // Value Manipulation Functions (static inline for self-containment)
82 // -----------------------------------------------------------------------------
83
84 // Helper functions to create PhasorValue instances.
86 {
87 PhasorValue val;
89 return val;
90 }
91
92 static inline PhasorValue phasor_make_bool(bool b)
93 {
94 PhasorValue val;
96 val.as.b = b;
97 return val;
98 }
99
100 static inline PhasorValue phasor_make_int(int64_t i)
101 {
102 PhasorValue val;
103 val.type = PHASOR_TYPE_INT;
104 val.as.i = i;
105 return val;
106 }
107
108 static inline PhasorValue phasor_make_float(double f)
109 {
110 PhasorValue val;
112 val.as.f = f;
113 return val;
114 }
115
116 static inline PhasorValue phasor_make_string(const char *s)
117 {
118 PhasorValue val;
120 val.as.s = s;
121 return val;
122 }
123
124 static inline PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
125 {
126 PhasorValue val;
128 val.as.a.elements = elements;
129 val.as.a.count = count;
130 return val;
131 }
132
133 // Helper functions to check the type of a PhasorValue.
134 static inline bool phasor_is_null(PhasorValue val)
135 {
136 return val.type == PHASOR_TYPE_NULL;
137 }
138 static inline bool phasor_is_bool(PhasorValue val)
139 {
140 return val.type == PHASOR_TYPE_BOOL;
141 }
142 static inline bool phasor_is_int(PhasorValue val)
143 {
144 return val.type == PHASOR_TYPE_INT;
145 }
146 static inline bool phasor_is_float(PhasorValue val)
147 {
148 return val.type == PHASOR_TYPE_FLOAT;
149 }
150 static inline bool phasor_is_string(PhasorValue val)
151 {
152 return val.type == PHASOR_TYPE_STRING;
153 }
154 static inline bool phasor_is_array(PhasorValue val)
155 {
156 return val.type == PHASOR_TYPE_ARRAY;
157 }
158 static inline bool phasor_is_number(PhasorValue val)
159 {
160 return phasor_is_int(val) || phasor_is_float(val);
161 }
162
163 // Helper functions to get the underlying value from a PhasorValue.
164 // Note: These do not perform type checking. Use the `phasor_is_*` functions first.
165 static inline bool phasor_to_bool(PhasorValue val)
166 {
167 return val.as.b;
168 }
169 static inline int64_t phasor_to_int(PhasorValue val)
170 {
171 return val.as.i;
172 }
173 static inline double phasor_to_float(PhasorValue val)
174 {
175 if (phasor_is_int(val))
176 return (double)val.as.i;
177 return val.as.f;
178 }
179 static inline const char *phasor_to_string(PhasorValue val)
180 {
181 return val.as.s;
182 }
183
184 // -----------------------------------------------------------------------------
185 // FFI API Definitions
186 // -----------------------------------------------------------------------------
187
189 typedef PhasorValue (*PhasorNativeFunction)(PhasorVM *vm, int argc, const PhasorValue *argv);
190
192 typedef void (*PhasorRegisterFunction)(PhasorVM *vm, const char *name, PhasorNativeFunction func);
193
200
201 // -----------------------------------------------------------------------------
202 // Plugin Entry Point
203 // -----------------------------------------------------------------------------
204
219
220#ifdef __cplusplus
221} // extern "C"
222#endif
223
224#endif // PHASOR_FFI_HPP
static double phasor_to_float(PhasorValue val)
Definition PhasorFFI.h:173
static bool phasor_is_bool(PhasorValue val)
Definition PhasorFFI.h:138
PhasorValue(* PhasorNativeFunction)(PhasorVM *vm, int argc, const PhasorValue *argv)
Signature for a native C function that can be registered with the Phasor VM.
Definition PhasorFFI.h:189
static bool phasor_to_bool(PhasorValue val)
Definition PhasorFFI.h:165
static bool phasor_is_string(PhasorValue val)
Definition PhasorFFI.h:150
static const char * phasor_to_string(PhasorValue val)
Definition PhasorFFI.h:179
static bool phasor_is_number(PhasorValue val)
Definition PhasorFFI.h:158
struct PhasorValue PhasorValue
Forward declare for self-reference in the union.
Definition PhasorFFI.h:60
static int64_t phasor_to_int(PhasorValue val)
Definition PhasorFFI.h:169
void(* PhasorRegisterFunction)(PhasorVM *vm, const char *name, PhasorNativeFunction func)
Function pointer type for the function that registers a native function with the VM.
Definition PhasorFFI.h:192
PHASOR_FFI_EXPORT void phasor_plugin_entry(const PhasorAPI *api, PhasorVM *vm)
The one and only entry point for a Phasor plugin.
Definition main.c:182
static PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
Definition PhasorFFI.h:124
static PhasorValue phasor_make_float(double f)
Definition PhasorFFI.h:108
static PhasorValue phasor_make_string(const char *s)
Definition PhasorFFI.h:116
static bool phasor_is_int(PhasorValue val)
Definition PhasorFFI.h:142
static PhasorValue phasor_make_null()
Definition PhasorFFI.h:85
static bool phasor_is_array(PhasorValue val)
Definition PhasorFFI.h:154
static PhasorValue phasor_make_bool(bool b)
Definition PhasorFFI.h:92
static bool phasor_is_null(PhasorValue val)
Definition PhasorFFI.h:134
static PhasorValue phasor_make_int(int64_t i)
Definition PhasorFFI.h:100
struct PhasorVM PhasorVM
Phasor Virtual Machine pointer.
Definition PhasorFFI.h:44
static bool phasor_is_float(PhasorValue val)
Definition PhasorFFI.h:146
#define PHASOR_FFI_EXPORT
Definition PhasorFFI.h:35
PhasorValueType
PhasorValue types.
Definition PhasorFFI.h:48
@ PHASOR_TYPE_FLOAT
Definition PhasorFFI.h:52
@ PHASOR_TYPE_NULL
Definition PhasorFFI.h:49
@ PHASOR_TYPE_ARRAY
Definition PhasorFFI.h:54
@ PHASOR_TYPE_STRING
Definition PhasorFFI.h:53
@ PHASOR_TYPE_BOOL
Definition PhasorFFI.h:50
@ PHASOR_TYPE_INT
Definition PhasorFFI.h:51
The collection of API functions that the Phasor host provides to the plugin.
Definition PhasorFFI.h:196
PhasorRegisterFunction register_function
Registers a native C function with the given name.
Definition PhasorFFI.h:198
Represents a value in the Phasor VM.
Definition PhasorFFI.h:64
struct PhasorValue::@257106260015165034006071221344325333366100147341::@324103060207145140324203224012174236216253240107 a
const PhasorValue * elements
Definition PhasorFFI.h:74
const char * s
Definition PhasorFFI.h:70
int64_t i
Definition PhasorFFI.h:68
PhasorValueType type
Definition PhasorFFI.h:65
union PhasorValue::@257106260015165034006071221344325333366100147341 as
size_t count
Definition PhasorFFI.h:75
double f
Definition PhasorFFI.h:69