Phasor 2.2.0
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. Include this header in your C or C++ source file.
22* 2. Implement the entry point function:
23* void phasor_plugin_entry(const PhasorAPI* api, PhasorVM* vm);
24* 3. Inside this function, use the provided `api` object to register
25* your own native functions.
26* 4. Compile your code as a shared library (.dll, .so, .dylib).
27*/
28
29#if defined(_WIN32) && defined(PHASOR_FFI_BUILD_DLL)
30 #define PHASOR_FFI_EXPORT __declspec(dllexport)
31#elif defined(__GNUC__) || defined(__clang__)
32 #define PHASOR_FFI_EXPORT __attribute__((visibility("default")))
33#else
34 #define PHASOR_FFI_EXPORT
35#endif
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41// Opaque pointer to the Phasor Virtual Machine.
42typedef struct PhasorVM PhasorVM;
43
44// An enumeration of possible types a PhasorValue can hold.
45typedef enum {
52 // Note: Structs are not directly supported in this version of the FFI
53 // for simplicity, but can be added in the future.
55
56// Forward declare for self-reference in the union
57typedef struct PhasorValue PhasorValue;
58
59// Represents a value in the Phasor VM.
60// It's a tagged union holding one of several possible types.
63 union {
64 bool b;
65 int64_t i;
66 double f;
67 const char* s; // Note: For strings returned from the VM, this is valid.
68 // For strings passed to the VM, the VM makes a copy.
69 struct {
71 size_t count;
72 } a;
73 } as;
74};
75
76// -----------------------------------------------------------------------------
77// Value Manipulation Functions (static inline for self-containment)
78// -----------------------------------------------------------------------------
79
80// Helper functions to create PhasorValue instances.
82 PhasorValue val;
84 return val;
85}
86
87static inline PhasorValue phasor_make_bool(bool b) {
88 PhasorValue val;
90 val.as.b = b;
91 return val;
92}
93
94static inline PhasorValue phasor_make_int(int64_t i) {
95 PhasorValue val;
97 val.as.i = i;
98 return val;
99}
100
101static inline PhasorValue phasor_make_float(double f) {
102 PhasorValue val;
104 val.as.f = f;
105 return val;
106}
107
108static inline PhasorValue phasor_make_string(const char* s) {
109 PhasorValue val;
111 val.as.s = s;
112 return val;
113}
114
115static inline PhasorValue phasor_make_array(const PhasorValue* elements, size_t count) {
116 PhasorValue val;
118 val.as.a.elements = elements;
119 val.as.a.count = count;
120 return val;
121}
122
123// Helper functions to check the type of a PhasorValue.
124static inline bool phasor_is_null(PhasorValue val) { return val.type == PHASOR_TYPE_NULL; }
125static inline bool phasor_is_bool(PhasorValue val) { return val.type == PHASOR_TYPE_BOOL; }
126static inline bool phasor_is_int(PhasorValue val) { return val.type == PHASOR_TYPE_INT; }
127static inline bool phasor_is_float(PhasorValue val) { return val.type == PHASOR_TYPE_FLOAT; }
128static inline bool phasor_is_string(PhasorValue val) { return val.type == PHASOR_TYPE_STRING; }
129static inline bool phasor_is_array(PhasorValue val) { return val.type == PHASOR_TYPE_ARRAY; }
130static inline bool phasor_is_number(PhasorValue val) { return phasor_is_int(val) || phasor_is_float(val); }
131
132// Helper functions to get the underlying value from a PhasorValue.
133// Note: These do not perform type checking. Use the `phasor_is_*` functions first.
134static inline bool phasor_to_bool(PhasorValue val) { return val.as.b; }
135static inline int64_t phasor_to_int(PhasorValue val) { return val.as.i; }
136static inline double phasor_to_float(PhasorValue val) {
137 if (phasor_is_int(val)) return (double)val.as.i;
138 return val.as.f;
139}
140static inline const char* phasor_to_string(PhasorValue val) { return val.as.s; }
141
142
143// -----------------------------------------------------------------------------
144// FFI API Definitions
145// -----------------------------------------------------------------------------
146
147// Signature for a native C function that can be registered with the Phasor VM.
148typedef PhasorValue (*PhasorNativeFunction)(PhasorVM* vm, int argc, const PhasorValue* argv);
149
150// Function pointer type for the function that registers a native function with the VM.
151typedef void (*PhasorRegisterFunction)(PhasorVM* vm, const char* name, PhasorNativeFunction func);
152
153
154// The collection of API functions that the Phasor host provides to the plugin.
155typedef struct {
156 // Registers a native C function with the given name.
158} PhasorAPI;
159
160
161// -----------------------------------------------------------------------------
162// Plugin Entry Point
163// -----------------------------------------------------------------------------
164
179
180
181#ifdef __cplusplus
182} // extern "C"
183#endif
184
185#endif // PHASOR_FFI_HPP
static double phasor_to_float(PhasorValue val)
Definition PhasorFFI.h:136
static bool phasor_is_bool(PhasorValue val)
Definition PhasorFFI.h:125
PhasorValue(* PhasorNativeFunction)(PhasorVM *vm, int argc, const PhasorValue *argv)
Definition PhasorFFI.h:148
static bool phasor_to_bool(PhasorValue val)
Definition PhasorFFI.h:134
static bool phasor_is_string(PhasorValue val)
Definition PhasorFFI.h:128
static const char * phasor_to_string(PhasorValue val)
Definition PhasorFFI.h:140
static bool phasor_is_number(PhasorValue val)
Definition PhasorFFI.h:130
struct PhasorValue PhasorValue
Definition PhasorFFI.h:57
static int64_t phasor_to_int(PhasorValue val)
Definition PhasorFFI.h:135
void(* PhasorRegisterFunction)(PhasorVM *vm, const char *name, PhasorNativeFunction func)
Definition PhasorFFI.h:151
PHASOR_FFI_EXPORT void phasor_plugin_entry(const PhasorAPI *api, PhasorVM *vm)
The one and only entry point for a Phasor plugin.
static PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
Definition PhasorFFI.h:115
static PhasorValue phasor_make_float(double f)
Definition PhasorFFI.h:101
static PhasorValue phasor_make_string(const char *s)
Definition PhasorFFI.h:108
static bool phasor_is_int(PhasorValue val)
Definition PhasorFFI.h:126
static PhasorValue phasor_make_null()
Definition PhasorFFI.h:81
static bool phasor_is_array(PhasorValue val)
Definition PhasorFFI.h:129
static PhasorValue phasor_make_bool(bool b)
Definition PhasorFFI.h:87
static bool phasor_is_null(PhasorValue val)
Definition PhasorFFI.h:124
static PhasorValue phasor_make_int(int64_t i)
Definition PhasorFFI.h:94
struct PhasorVM PhasorVM
Definition PhasorFFI.h:42
static bool phasor_is_float(PhasorValue val)
Definition PhasorFFI.h:127
#define PHASOR_FFI_EXPORT
Definition PhasorFFI.h:34
PhasorValueType
Definition PhasorFFI.h:45
@ PHASOR_TYPE_FLOAT
Definition PhasorFFI.h:49
@ PHASOR_TYPE_NULL
Definition PhasorFFI.h:46
@ PHASOR_TYPE_ARRAY
Definition PhasorFFI.h:51
@ PHASOR_TYPE_STRING
Definition PhasorFFI.h:50
@ PHASOR_TYPE_BOOL
Definition PhasorFFI.h:47
@ PHASOR_TYPE_INT
Definition PhasorFFI.h:48
PhasorRegisterFunction register_function
Definition PhasorFFI.h:157
struct PhasorValue::@257106260015165034006071221344325333366100147341::@324103060207145140324203224012174236216253240107 a
const PhasorValue * elements
Definition PhasorFFI.h:70
const char * s
Definition PhasorFFI.h:67
int64_t i
Definition PhasorFFI.h:65
PhasorValueType type
Definition PhasorFFI.h:62
union PhasorValue::@257106260015165034006071221344325333366100147341 as
size_t count
Definition PhasorFFI.h:71
double f
Definition PhasorFFI.h:66