Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
PhasorFFI.h
Go to the documentation of this file.
1// Copyright 2026 Daniel McGuire
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5// http://www.apache.org/licenses/LICENSE-2.0
6// Unless required by applicable law or agreed to in writing, software
7// distributed under the License is distributed on an "AS IS" BASIS,
8// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9// See the License for the specific language governing permissions and
10// limitations under the License.
11
12// README
13//
14// See below:
15
16#ifndef PHASOR_FFI_HPP
17#define PHASOR_FFI_HPP
18
19#ifdef __cplusplus
20#include <cstdint>
21#include <cstddef>
22#else
23#include <stdint.h>
24#include <stddef.h>
25#include <stdbool.h>
26#endif
27
28/*
29 * Phasor Foreign Function Interface
30 *
31 * This header provides the necessary definitions for creating third-party
32 * plugins for the Phasor scripting engine. It is designed to be a stable,
33 * C-compatible interface.
34 *
35 * USAGE:
36 * 1. Define PHASOR_FFI_BUILD_DLL.
37 * 2. Include this header in your C or C++ source file.
38 * 3. Implement the entry point function:
39 * void phasor_plugin_entry(const PhasorAPI* api, PhasorVM* vm);
40 * 4. Inside this function, use the provided `api` object to register
41 * your own native functions.
42 * 5. Compile your code as a shared library (.dll, .so, .dylib).
43 */
44
45#if defined(_WIN32) && defined(PHASOR_FFI_BUILD_DLL)
46#define PHASOR_FFI_EXPORT __declspec(dllexport)
47#elif defined(__GNUC__) || defined(__clang__)
48#define PHASOR_FFI_EXPORT __attribute__((visibility("default")))
49#else
50#define PHASOR_FFI_EXPORT
51#endif
52
53#ifdef __cplusplus
54extern "C"
55{
56#endif
57
59 typedef struct PhasorVM PhasorVM;
60
62 typedef enum
63 {
70 // Note: Structs are not directly supported in this version of the FFI
71 // for simplicity, but can be added in the future.
73
75 typedef struct PhasorValue PhasorValue;
76
79 {
81 union {
82 bool b;
83 int64_t i;
84 double f;
85 const char *s; // Note: For strings returned from the VM, this is valid.
86 // For strings passed to the VM, the VM makes a copy.
87 struct
88 {
90 size_t count;
91 } a;
92 } as;
93 };
94
95 // -----------------------------------------------------------------------------
96 // Value Manipulation Functions (static inline for self-containment)
97 // -----------------------------------------------------------------------------
98
99 // Helper functions to create PhasorValue instances.
101 {
102 PhasorValue val;
104 return val;
105 }
106
107 static inline PhasorValue phasor_make_bool(bool b)
108 {
109 PhasorValue val;
111 val.as.b = b;
112 return val;
113 }
114
115 static inline PhasorValue phasor_make_int(int64_t i)
116 {
117 PhasorValue val;
118 val.type = PHASOR_TYPE_INT;
119 val.as.i = i;
120 return val;
121 }
122
123 static inline PhasorValue phasor_make_float(double f)
124 {
125 PhasorValue val;
127 val.as.f = f;
128 return val;
129 }
130
131 static inline PhasorValue phasor_make_string(const char *s)
132 {
133 PhasorValue val;
135 val.as.s = s;
136 return val;
137 }
138
139 static inline PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
140 {
141 PhasorValue val;
143 val.as.a.elements = elements;
144 val.as.a.count = count;
145 return val;
146 }
147
148 // Helper functions to check the type of a PhasorValue.
149 static inline bool phasor_is_null(PhasorValue val)
150 {
151 return val.type == PHASOR_TYPE_NULL;
152 }
153 static inline bool phasor_is_bool(PhasorValue val)
154 {
155 return val.type == PHASOR_TYPE_BOOL;
156 }
157 static inline bool phasor_is_int(PhasorValue val)
158 {
159 return val.type == PHASOR_TYPE_INT;
160 }
161 static inline bool phasor_is_float(PhasorValue val)
162 {
163 return val.type == PHASOR_TYPE_FLOAT;
164 }
165 static inline bool phasor_is_string(PhasorValue val)
166 {
167 return val.type == PHASOR_TYPE_STRING;
168 }
169 static inline bool phasor_is_array(PhasorValue val)
170 {
171 return val.type == PHASOR_TYPE_ARRAY;
172 }
173 static inline bool phasor_is_number(PhasorValue val)
174 {
175 return phasor_is_int(val) || phasor_is_float(val);
176 }
177
178 // Helper functions to get the underlying value from a PhasorValue.
179 // Note: These do not perform type checking. Use the `phasor_is_*` functions first.
180 static inline bool phasor_to_bool(PhasorValue val)
181 {
182 return val.as.b;
183 }
184 static inline int64_t phasor_to_int(PhasorValue val)
185 {
186 return val.as.i;
187 }
188 static inline double phasor_to_float(PhasorValue val)
189 {
190 if (phasor_is_int(val))
191 return (double)val.as.i;
192 return val.as.f;
193 }
194 static inline const char *phasor_to_string(PhasorValue val)
195 {
196 return val.as.s;
197 }
198
199 // -----------------------------------------------------------------------------
200 // FFI API Definitions
201 // -----------------------------------------------------------------------------
202
204 typedef PhasorValue (*PhasorNativeFunction)(PhasorVM *vm, int argc, const PhasorValue *argv);
205
207 typedef void (*PhasorRegisterFunction)(PhasorVM *vm, const char *name, PhasorNativeFunction func);
208
215
216 // -----------------------------------------------------------------------------
217 // Plugin Entry Point
218 // -----------------------------------------------------------------------------
219
234
235#ifdef __cplusplus
236} // extern "C"
237#endif
238
239#endif // PHASOR_FFI_HPP
static double phasor_to_float(PhasorValue val)
Definition PhasorFFI.h:188
static bool phasor_is_bool(PhasorValue val)
Definition PhasorFFI.h:153
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:204
static bool phasor_to_bool(PhasorValue val)
Definition PhasorFFI.h:180
static bool phasor_is_string(PhasorValue val)
Definition PhasorFFI.h:165
static const char * phasor_to_string(PhasorValue val)
Definition PhasorFFI.h:194
static bool phasor_is_number(PhasorValue val)
Definition PhasorFFI.h:173
struct PhasorValue PhasorValue
Forward declare for self-reference in the union.
Definition PhasorFFI.h:75
static int64_t phasor_to_int(PhasorValue val)
Definition PhasorFFI.h:184
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:207
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:40
static PhasorValue phasor_make_array(const PhasorValue *elements, size_t count)
Definition PhasorFFI.h:139
static PhasorValue phasor_make_float(double f)
Definition PhasorFFI.h:123
static PhasorValue phasor_make_string(const char *s)
Definition PhasorFFI.h:131
static bool phasor_is_int(PhasorValue val)
Definition PhasorFFI.h:157
static PhasorValue phasor_make_null()
Definition PhasorFFI.h:100
static bool phasor_is_array(PhasorValue val)
Definition PhasorFFI.h:169
static PhasorValue phasor_make_bool(bool b)
Definition PhasorFFI.h:107
static bool phasor_is_null(PhasorValue val)
Definition PhasorFFI.h:149
static PhasorValue phasor_make_int(int64_t i)
Definition PhasorFFI.h:115
struct PhasorVM PhasorVM
Phasor Virtual Machine pointer.
Definition PhasorFFI.h:59
static bool phasor_is_float(PhasorValue val)
Definition PhasorFFI.h:161
#define PHASOR_FFI_EXPORT
Definition PhasorFFI.h:50
PhasorValueType
PhasorValue types.
Definition PhasorFFI.h:63
@ PHASOR_TYPE_FLOAT
Definition PhasorFFI.h:67
@ PHASOR_TYPE_NULL
Definition PhasorFFI.h:64
@ PHASOR_TYPE_ARRAY
Definition PhasorFFI.h:69
@ PHASOR_TYPE_STRING
Definition PhasorFFI.h:68
@ PHASOR_TYPE_BOOL
Definition PhasorFFI.h:65
@ PHASOR_TYPE_INT
Definition PhasorFFI.h:66
static uint64_t s[2]
Definition random.cpp:6
The collection of API functions that the Phasor host provides to the plugin.
Definition PhasorFFI.h:211
PhasorRegisterFunction register_function
Registers a native C function with the given name.
Definition PhasorFFI.h:213
Represents a value in the Phasor VM.
Definition PhasorFFI.h:79
struct PhasorValue::@257106260015165034006071221344325333366100147341::@324103060207145140324203224012174236216253240107 a
const PhasorValue * elements
Definition PhasorFFI.h:89
const char * s
Definition PhasorFFI.h:85
int64_t i
Definition PhasorFFI.h:83
PhasorValueType type
Definition PhasorFFI.h:80
union PhasorValue::@257106260015165034006071221344325333366100147341 as
size_t count
Definition PhasorFFI.h:90
double f
Definition PhasorFFI.h:84