From 51eb640192c44cc7947fb736cefdbde27b4e4823 Mon Sep 17 00:00:00 2001 From: scawful Date: Tue, 6 Aug 2024 21:26:03 -0400 Subject: [PATCH] add yaze_ext lib for extensions --- src/CMakeLists.txt | 1 + src/ext/CMakeLists.txt | 16 +++++++++++++ src/ext/extension.cc | 53 ++++++++++++++++++++++++++++++++++++++++++ src/ext/extension.h | 34 +++++++++++++++++++++++++++ 4 files changed, 104 insertions(+) create mode 100644 src/ext/CMakeLists.txt create mode 100644 src/ext/extension.cc create mode 100644 src/ext/extension.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7a671305..5f5ed05a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -74,6 +74,7 @@ if(APPLE) endif() include(app/CMakeLists.txt) +include(ext/CMakeLists.txt) include(py/CMakeLists.txt) # include(cli/CMakeLists.txt) Excluded for now, macOS include breaks action build diff --git a/src/ext/CMakeLists.txt b/src/ext/CMakeLists.txt new file mode 100644 index 00000000..6d55d768 --- /dev/null +++ b/src/ext/CMakeLists.txt @@ -0,0 +1,16 @@ +add_library( + yaze_ext + ext/extension.cc +) + +target_include_directories( + yaze_ext PUBLIC + lib/ + app/ + /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/Headers +) + +target_link_libraries( + yaze_ext PUBLIC + Python +) \ No newline at end of file diff --git a/src/ext/extension.cc b/src/ext/extension.cc new file mode 100644 index 00000000..cbd6c0e8 --- /dev/null +++ b/src/ext/extension.cc @@ -0,0 +1,53 @@ +#include "extension.h" + +#include +#include + +#include + +void* handle = nullptr; +Extension* extension = nullptr; + +Extension* GetExtension() { return nullptr; } + +void loadCExtension(const char* extensionPath) { + handle = dlopen(extensionPath, RTLD_LAZY); + if (!handle) { + std::cerr << "Cannot open extension: " << dlerror() << std::endl; + return; + } + dlerror(); // Clear any existing error + + Extension* (*getExtension)(); + getExtension = (Extension * (*)()) dlsym(handle, "getExtension"); + const char* dlsym_error = dlerror(); + if (dlsym_error) { + std::cerr << "Cannot load symbol 'getExtension': " << dlsym_error + << std::endl; + dlclose(handle); + return; + } + + extension = getExtension(); + extension->initialize(); +} + +void LoadPythonExtension(const char* script_path) { + Py_Initialize(); + FILE* fp = fopen(script_path, "r"); + if (fp) { + PyRun_SimpleFile(fp, script_path); + fclose(fp); + } + + PyObject* pModule = PyImport_AddModule("__main__"); + PyObject* pFunc = PyObject_GetAttrString(pModule, "get_extension"); + + if (pFunc && PyCallable_Check(pFunc)) { + PyObject* pExtension = PyObject_CallObject(pFunc, nullptr); + if (pExtension) { + // Assume the Python extension has the same structure as the C extension + extension = reinterpret_cast(PyLong_AsVoidPtr(pExtension)); + } + } +} \ No newline at end of file diff --git a/src/ext/extension.h b/src/ext/extension.h new file mode 100644 index 00000000..69581d16 --- /dev/null +++ b/src/ext/extension.h @@ -0,0 +1,34 @@ +#ifndef EXTENSION_INTERFACE_H +#define EXTENSION_INTERFACE_H + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct Extension { + const char* name; + const char* version; + + // Initialization function + void (*initialize)(void); + + // Cleanup function + void (*cleanup)(void); + + // Function to extend editor functionality + void (*extendFunctionality)(void* editorContext); +} Extension; + +// Function to get the extension instance +Extension* GetExtension(); + +void LoadCExtension(const char* extension_path); + +// Function to load a Python script as an extension +void LoadPythonExtension(const char* script_path); + +#ifdef __cplusplus +} +#endif + +#endif // EXTENSION_INTERFACE_H \ No newline at end of file