Phasor 3.3.0
Stack VM based Programming Language
Loading...
Searching...
No Matches
ffi.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <functional>
4#include <filesystem>
5
6#ifndef CMAKE_PCH
7#include <Value.hpp>
8#endif
9#include <vector>
10#include <string>
11
12#if defined(_WIN32)
13#include <windows.h>
14#else
15#include <dlfcn.h>
16#endif
17
18#include <PhasorFFI.h>
19
20namespace Phasor
21{
22class VM;
23}
24
25using FFIFunction = void (*)(const PhasorAPI *api, PhasorVM *vm);
27namespace Phasor
28{
29
36struct Plugin
37{
38#if defined(_WIN32)
39 HMODULE handle;
40#else
41 void *handle;
42#endif
43 std::string path;
45 std::function<void()> shutdown;
46};
47
52 const std::vector<Phasor::Value> &args);
53
61void register_native_c_func(PhasorVM *vm, const char *name, PhasorNativeFunction func);
62
69class FFI
70{
71 public:
77 explicit FFI(const std::filesystem::path &pluginFolder, VM *vm);
78
82 ~FFI();
83
89 bool addPlugin(const std::filesystem::path &pluginPath);
90
91 private:
95 bool native_add_plugin(const std::vector<Value> &args, VM *vm);
96
103 bool loadPlugin(const std::filesystem::path &library, VM *vm);
104
110 std::vector<std::string> scanPlugins(const std::filesystem::path &folder);
111
115 void unloadAll();
116
117 std::vector<Plugin> plugins_;
118 std::filesystem::path pluginFolder_;
120};
121
122} // 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.
Definition ffi.cpp:162
std::filesystem::path pluginFolder_
Plugin search folder.
Definition ffi.hpp:118
~FFI()
Destructor. Unloads all loaded plugins.
Definition ffi.cpp:204
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.
Definition ffi.cpp:90
bool loadPlugin(const std::filesystem::path &library, VM *vm)
Loads a single plugin from a library file.
Definition ffi.cpp:27
bool addPlugin(const std::filesystem::path &pluginPath)
Adds a single plugin from the specified path.
Definition ffi.cpp:78
bool native_add_plugin(const std::vector< Value > &args, VM *vm)
Native function to load a plugin at runtime.
Definition ffi.cpp:213
FFI(const std::filesystem::path &pluginFolder, VM *vm)
Constructs the FFI manager and loads plugins.
Definition ffi.cpp:183
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
Represents a loaded plugin.
Definition ffi.hpp:37
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