From 33b74fc7e4ab5b3567f020c05e4d1d4f7be70f9f Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 10 Feb 2021 23:15:16 +0100 Subject: [PATCH] Adding the module but it's not working yet --- meson.build | 20 +++++++++++ meson_options.txt | 1 + src/pywrenppmodule.cpp | 81 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 src/pywrenppmodule.cpp diff --git a/meson.build b/meson.build index 59282fd..ee501ab 100644 --- a/meson.build +++ b/meson.build @@ -4,6 +4,8 @@ project('wrenpp', 'cpp', default_options: ['buildtype=release', 'cpp_std=c++17', 'b_ndebug=if-release'], ) +pymod = import('python') + wren_dep = dependency('wren', version: '>=0.2.0', fallback: ['wren', 'wren_dep'], default_options: [ @@ -14,6 +16,10 @@ wren_dep = dependency('wren', version: '>=0.2.0', ], static: true, ) +python = pymod.find_installation('python3', + required: get_option('wrenpp_with_python'), + disabler: true +) public_incl = include_directories('include') os = host_machine.system() @@ -65,6 +71,20 @@ wrenpp_dep = declare_dependency( include_directories: public_incl, ) +python.extension_module('py' + meson.project_name(), + sources: [ + 'src/pywrenppmodule.cpp', + ], + dependencies: [ + python.dependency( + version: '>=3.6.0', + required: get_option('wrenpp_with_python'), + ), + wrenpp_dep, + ], + install: true, +) + if get_option('build_examples') subdir('examples') endif diff --git a/meson_options.txt b/meson_options.txt index 23a3a31..88c13fc 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,3 +2,4 @@ option('build_testing', type: 'boolean', value: false) option('build_examples', type: 'boolean', value: false) option('wren_with_rand', type: 'boolean', value: false, yield: true) option('wren_with_meta', type: 'boolean', value: false, yield: true) +option('wrenpp_with_python', type: 'feature', value: 'disabled') diff --git a/src/pywrenppmodule.cpp b/src/pywrenppmodule.cpp new file mode 100644 index 0000000..269dd4d --- /dev/null +++ b/src/pywrenppmodule.cpp @@ -0,0 +1,81 @@ +#define PY_SSIZE_T_CLEAN +#include +#include "wrenpp/vm.hpp" +#include "wrenpp/def_configuration.hpp" +#include "config.h" + +#define CURR_MODULE_NAME WRENPP_NAME + +namespace { +struct VMObject; +int vm_init (VMObject* self, PyObject*, PyObject*); + +class VMConfig : public wren::DefConfiguration { +public: + wren::foreign_method_t foreign_method_fn ( + wren::VM* vm, + const char* module_ptr, + const char* class_name_ptr, + bool is_static, + const char* signature_ptr + ) { + return nullptr; + } +}; + +PyMethodDef WrenppMethods[] = { + //{"eval_script", &eval_script, METH_VARARGS, "Evaluate a Wren script"}, + {nullptr, nullptr, 0, nullptr} +}; + +struct PyModuleDef WrenppModule = { + PyModuleDef_HEAD_INIT, + CURR_MODULE_NAME, //module name + "Python bindings for Wrenpp", //module documentation + -1, //size of per-interpreter state of the module, or -1 if the module keeps state in global variables + WrenppMethods +}; + +struct VMObject { + PyObject_HEAD + wren::VM vm; +}; + +PyTypeObject VMType = { + PyVarObject_HEAD_INIT(NULL, 0) + .tp_name = CURR_MODULE_NAME ".VM", + .tp_basicsize = sizeof(VMObject), + .tp_itemsize = 0, + .tp_flags = Py_TPFLAGS_DEFAULT, + .tp_doc = "Wrenpp VM", + .tp_init = reinterpret_cast(vm_init), + .tp_new = PyType_GenericNew, +}; + +int vm_init (VMObject* self, PyObject* /*args*/, PyObject* /*kwds*/) { + try { + VMConfig config; + new(&self->vm) wren::VM(&config, nullptr); + } + catch (...) { + return -1; + } + return 0; +} +} //unnamed namespace + +PyMODINIT_FUNC PyInit_wrenpp(void) { + if (PyType_Ready(&VMType) < 0) + return nullptr; + PyObject* const m = PyModule_Create(&WrenppModule); + if (not m) + return nullptr; + + Py_INCREF(&VMType); + if (PyModule_AddObject(m, "VM", reinterpret_cast(&VMType)) < 0) { + Py_DECREF(&VMType); + Py_DECREF(m); + return nullptr; + } + return m; +}