Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
PhasorFFI.hpp
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// The FFI is used by the VM, these declarations should only be used for loading external C plugins
15
16#pragma once
17
18#if defined(_WIN32)
19#include <windows.h>
20#else
21#include <dlfcn.h>
22#endif
23
24#include <functional>
25#include <filesystem>
26#include <vector>
27#include <string>
28
29#include "PhasorFFI.h"
30#include "Value.hpp"
31
32namespace Phasor
33{
34class VM;
35}
36
37using FFIFunction = void (*)(const PhasorAPI *api, PhasorVM *vm);
39namespace Phasor
40{
41
48struct Plugin
49{
50#if defined(_WIN32)
51 HMODULE handle;
52#else
53 void *handle;
54#endif
55 std::string path;
57 std::function<void()> shutdown;
58};
59
64 const std::vector<Phasor::Value> &args);
65
73void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func);
74
81class FFI
82{
83 public:
89 explicit FFI(const std::filesystem::path &pluginFolder, VM *vm);
90
95
101 bool addPlugin(const std::filesystem::path &pluginPath);
102
103 private:
107 bool native_add_plugin(const std::vector<Value> &args, VM *vm);
108
115 bool loadPlugin(const std::filesystem::path &library, VM *vm);
116
122 std::vector<std::string> scanPlugins(const std::filesystem::path &folder);
123
127 void unloadAll();
128
129 std::vector<Plugin> plugins_;
130 std::filesystem::path pluginFolder_;
131 VM *vm_;
132};
133
134} // namespace Phasor
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
struct PhasorVM PhasorVM
Phasor Virtual Machine pointer.
Definition PhasorFFI.h:59
VM * vm_
Pointer to the Phasor VM.
Definition ffi.hpp:119
void unloadAll()
Unloads all currently loaded plugins and clears internal state.
std::filesystem::path pluginFolder_
Plugin search folder.
Definition ffi.hpp:118
~FFI()
Destructor. Unloads all loaded plugins.
std::vector< Plugin > plugins_
Loaded plugins.
Definition ffi.hpp:117
std::vector< std::string > scanPlugins(const std::filesystem::path &folder)
Scans a folder for plugin libraries.
bool loadPlugin(const std::filesystem::path &library, VM *vm)
Loads a single plugin from a library file.
bool addPlugin(const std::filesystem::path &pluginPath)
Adds a single plugin from the specified path.
bool native_add_plugin(const std::vector< Value > &args, VM *vm)
Native function to load a plugin at runtime.
FFI(const std::filesystem::path &pluginFolder, VM *vm)
Constructs the FFI manager and loads plugins.
Virtual Machine.
Definition VM.hpp:33
A value in the Phasor VM.
Definition Value.hpp:58
void(*)(const PhasorAPI *api, PhasorVM *vm) FFIFunction
Definition ffi.hpp:25
The Phasor Programming Language and Runtime.
Definition AST.hpp:12
void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func)
The concrete implementation of the PhasorRegisterFunction API call.
Definition api.cpp:124
Phasor::Value c_native_func_wrapper(PhasorNativeFunction c_func, Phasor::VM *vm, const std::vector< Phasor::Value > &args)
The "trampoline" that wraps a C function from a plugin.
Definition api.cpp:107
The collection of API functions that the Phasor host provides to the plugin.
Definition PhasorFFI.h:211
void * handle
POSIX handle for the loaded library.
Definition ffi.hpp:41
std::string path
Path to the plugin file.
Definition ffi.hpp:43
std::function< void()> shutdown
Optional shutdown callback.
Definition ffi.hpp:45
FFIFunction init
Plugin initialization function.
Definition ffi.hpp:44