Phasor 2.2.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 "../Value.hpp"
5#include "../VM/VM.hpp"
6
7#include <vector>
8#include <string>
9
10#if defined(_WIN32)
11#include <windows.h>
12#else
13#include <dlfcn.h>
14#endif
15
16#include <PhasorFFI.h>
17
18typedef void (*FFIFunction)(const PhasorAPI *api, PhasorVM *vm);
19
20namespace Phasor
21{
22
29struct Plugin
30{
31#if defined(_WIN32)
32 HMODULE handle;
33#else
34 void *handle;
35#endif
36 std::string path;
38 std::function<void()> shutdown;
39};
40
47class FFI
48{
49 public:
55 explicit FFI(const std::filesystem::path &pluginFolder, VM *vm);
56
60 ~FFI();
61
67 bool addPlugin(const std::filesystem::path &pluginPath);
68
72 Value native_add_plugin(const std::vector<Value> &args, VM *vm);
73
74 private:
81 bool loadPlugin(const std::filesystem::path &library, VM *vm);
82
88 std::vector<std::string> scanPlugins(const std::filesystem::path &folder);
89
93 void unloadAll();
94
95 std::vector<Plugin> plugins_;
96 std::filesystem::path pluginFolder_;
98};
99
100} // namespace Phasor
struct PhasorVM PhasorVM
Definition PhasorFFI.h:42
void unloadAll()
Unloads all currently loaded plugins and clears internal state.
Definition ffi.cpp:277
std::vector< Plugin > plugins_
Loaded plugins.
Definition ffi.hpp:95
std::filesystem::path pluginFolder_
Plugin search folder.
Definition ffi.hpp:96
~FFI()
Destructor. Unloads all loaded plugins.
Definition ffi.cpp:307
std::vector< std::string > scanPlugins(const std::filesystem::path &folder)
Scans a folder for plugin libraries.
Definition ffi.cpp:219
Value native_add_plugin(const std::vector< Value > &args, VM *vm)
Native function to load a plugin at runtime.
Definition ffi.cpp:312
bool loadPlugin(const std::filesystem::path &library, VM *vm)
Loads a single plugin from a library file.
Definition ffi.cpp:167
bool addPlugin(const std::filesystem::path &pluginPath)
Adds a single plugin from the specified path.
Definition ffi.cpp:211
VM * vm_
Pointer to the Phasor VM.
Definition ffi.hpp:97
FFI(const std::filesystem::path &pluginFolder, VM *vm)
Constructs the FFI manager and loads plugins.
Definition ffi.cpp:290
Virtual Machine.
Definition VM.hpp:18
A value in the Phasor VM.
Definition Value.hpp:33
void(* FFIFunction)(const PhasorAPI *api, PhasorVM *vm)
Definition ffi.hpp:18
The Phasor Programming Language and Runtime.
Definition AST.hpp:8
Represents a loaded plugin.
Definition ffi.hpp:30
void * handle
POSIX handle for the loaded library.
Definition ffi.hpp:34
std::function< void()> shutdown
Optional shutdown callback.
Definition ffi.hpp:38
std::string path
Path to the plugin file.
Definition ffi.hpp:36
FFIFunction init
Plugin initialization function.
Definition ffi.hpp:37