Compare commits

..

No commits in common. "f05f48631d41d06e95c911e35715d4650c2ab40b" and "42bbf6fdb48f96c530b15c96e7d1bcef633fba07" have entirely different histories.

10 changed files with 66 additions and 106 deletions

View File

@ -44,11 +44,6 @@ if(PK_ENABLE_OS)
add_definitions(-DPK_ENABLE_OS=1) add_definitions(-DPK_ENABLE_OS=1)
endif() endif()
option(PK_MODULE_WIN32 "" OFF)
if(PK_MODULE_WIN32)
add_definitions(-DPK_MODULE_WIN32=1)
endif()
# PK_IS_MAIN determines whether the project is being used from root # PK_IS_MAIN determines whether the project is being used from root
# or if it is added as a dependency (through add_subdirectory for example). # or if it is added as a dependency (through add_subdirectory for example).
if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}") if ("${CMAKE_SOURCE_DIR}" STREQUAL "${CMAKE_CURRENT_SOURCE_DIR}")
@ -61,22 +56,15 @@ else()
option(PK_BUILD_STATIC_LIB "Build static library" ON) option(PK_BUILD_STATIC_LIB "Build static library" ON)
endif() endif()
option(PK_BUILD_STATIC_MAIN "Build static main" OFF)
if(PK_BUILD_SHARED_LIB) if(PK_BUILD_SHARED_LIB)
message(">> Building shared library") message(">> Building shared library")
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC}) add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
elseif(PK_BUILD_STATIC_LIB) elseif(PK_BUILD_STATIC_LIB)
message(">> Building static library") message(">> Building static library")
add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC}) add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC})
else()
if(PK_BUILD_STATIC_MAIN)
message(">> Building static library + executable")
add_library(${PROJECT_NAME} STATIC ${POCKETPY_SRC})
else() else()
message(">> Building shared library + executable") message(">> Building shared library + executable")
add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC}) add_library(${PROJECT_NAME} SHARED ${POCKETPY_SRC})
endif()
add_executable(main src2/main.c) add_executable(main src2/main.c)
target_link_libraries(main ${PROJECT_NAME}) target_link_libraries(main ${PROJECT_NAME})
endif() endif()
@ -85,7 +73,3 @@ if(UNIX)
target_link_libraries(${PROJECT_NAME} m) target_link_libraries(${PROJECT_NAME} m)
target_link_libraries(${PROJECT_NAME} dl) target_link_libraries(${PROJECT_NAME} dl)
endif() endif()
if(PK_MODULE_WIN32)
target_link_libraries(${PROJECT_NAME} winmm.lib)
endif()

View File

@ -7,36 +7,28 @@ assert os.system("python prebuild.py") == 0
if not os.path.exists("build"): if not os.path.exists("build"):
os.mkdir("build") os.mkdir("build")
# python cmake_build.py [Debug|Release|RelWithDebInfo] ... assert len(sys.argv) <= 2
if len(sys.argv) > 1: if len(sys.argv) == 2:
config = sys.argv[1] config = sys.argv[1]
else: else:
config = 'Release' config = 'Release'
extra_flags = " ".join(sys.argv[2:])
assert config in ['Debug', 'Release', 'RelWithDebInfo'] assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build") os.chdir("build")
code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config} {extra_flags}") code = os.system(f"cmake .. -DPK_ENABLE_OS=ON -DCMAKE_BUILD_TYPE={config}")
assert code == 0 assert code == 0
code = os.system(f"cmake --build . --config {config}") code = os.system(f"cmake --build . --config {config}")
assert code == 0 assert code == 0
if sys.platform == "win32": if sys.platform == "win32":
shutil.copy(f"{config}/main.exe", "../main.exe") shutil.copy(f"{config}/main.exe", "../main.exe")
dll_path = f"{config}/pocketpy.dll" shutil.copy(f"{config}/pocketpy.dll", "../pocketpy.dll")
if os.path.exists(dll_path):
shutil.copy(dll_path, "../pocketpy.dll")
elif sys.platform == "darwin": elif sys.platform == "darwin":
shutil.copy("main", "../main") shutil.copy("main", "../main")
dll_path = "libpocketpy.dylib" shutil.copy("libpocketpy.dylib", "../libpocketpy.dylib")
if os.path.exists(dll_path):
shutil.copy(dll_path, "../libpocketpy.dylib")
else: else:
shutil.copy("main", "../main") shutil.copy("main", "../main")
dll_path = "libpocketpy.so" shutil.copy("libpocketpy.so", "../libpocketpy.so")
if os.path.exists(dll_path):
shutil.copy(dll_path, "../libpocketpy.so")

View File

@ -1,5 +1,7 @@
#pragma once #pragma once
void pk__add_module_pkpy();
void pk__add_module_conio();
void pk__add_module_os(); void pk__add_module_os();
void pk__add_module_sys(); void pk__add_module_sys();
void pk__add_module_math(); void pk__add_module_math();
@ -13,5 +15,3 @@ void pk__add_module_traceback();
void pk__add_module_enum(); void pk__add_module_enum();
void pk__add_module_linalg(); void pk__add_module_linalg();
void pk__add_module_array2d(); void pk__add_module_array2d();
void pk__add_module_win32();

View File

@ -4,60 +4,60 @@
namespace pkbind { namespace pkbind {
class module_ : public object { class module : public object {
PKBIND_TYPE_IMPL(object, module_, tp_module) PKBIND_TYPE_IMPL(object, module, tp_module)
static module_ __main__() { return module_(py_getmodule("__main__"), object::ref_t{}); } static module __main__() { return module(py_getmodule("__main__"), object::ref_t{}); }
static module_ import(const char* name) { static module import(const char* name) {
raise_call<py_import>(name); raise_call<py_import>(name);
return borrow<module_>(py_retval()); return borrow<module>(py_retval());
} }
static module_ create(const char* name) { static module create(const char* name) {
auto m = py_newmodule(name); auto m = py_newmodule(name);
return steal<module_>(m); return steal<module>(m);
} }
module_ def_submodule(const char* name, const char* doc = nullptr) { module def_submodule(const char* name, const char* doc = nullptr) {
// auto package = (attr("__package__").cast<std::string>() += ".") += // auto package = (attr("__package__").cast<std::string>() += ".") +=
// attr("__name__").cast<std::string_view>(); // attr("__name__").cast<std::string_view>();
auto fname = (attr("__name__").cast<std::string>() += ".") += name; auto fname = (attr("__name__").cast<std::string>() += ".") += name;
auto m = py_newmodule(fname.c_str()); auto m = py_newmodule(fname.c_str());
setattr(*this, name, m); setattr(*this, name, m);
return module_(m, object::ref_t{}); return module(m, object::ref_t{});
} }
template <typename Fn, typename... Extras> template <typename Fn, typename... Extras>
module_& def(const char* name, Fn&& fn, const Extras... extras) { module& def(const char* name, Fn&& fn, const Extras... extras) {
impl::bind_function<false, false>(*this, name, std::forward<Fn>(fn), extras...); impl::bind_function<false, false>(*this, name, std::forward<Fn>(fn), extras...);
return *this; return *this;
} }
}; };
using module = module_; using module_ = module;
#define PYBIND11_EMBEDDED_MODULE(name, variable) \ #define PYBIND11_EMBEDDED_MODULE(name, variable) \
static void _pkbind_register_##name(::pkbind::module_& variable); \ static void _pkbind_register_##name(::pkbind::module& variable); \
namespace pkbind::impl { \ namespace pkbind::impl { \
auto _module_##name = [] { \ auto _module_##name = [] { \
::pkbind::action::register_start([] { \ ::pkbind::action::register_start([] { \
auto m = ::pkbind::module_::create(#name); \ auto m = ::pkbind::module(py_newmodule(#name), ::pkbind::object::ref_t{}); \
_pkbind_register_##name(m); \ _pkbind_register_##name(m); \
}); \ }); \
return 1; \ return 1; \
}(); \ }(); \
} \ } \
static void _pkbind_register_##name(::pkbind::module_& variable) static void _pkbind_register_##name(::pkbind::module& variable)
#define PYBIND11_MODULE(name, variable) \ #define PYBIND11_MODULE(name, variable) \
static void _pkbind_register_##name(::pkbind::module_& variable); \ static void _pkbind_register_##name(::pkbind::module& variable); \
extern "C" PK_EXPORT bool py_module_initialize() { \ extern "C" PK_EXPORT bool py_module_initialize() { \
auto m = ::pkbind::module_::create(#name); \ auto m = ::pkbind::module::create(#name); \
_pkbind_register_##name(m); \ _pkbind_register_##name(m); \
py_assign(py_retval(), m.ptr()); \ py_assign(py_retval(), m.ptr()); \
return true; \ return true; \
} \ } \
static void _pkbind_register_##name(::pkbind::module_& variable) static void _pkbind_register_##name(::pkbind::module& variable)
} // namespace pkbind } // namespace pkbind

View File

@ -0,0 +1,2 @@
def _kbhit() -> int: ...
def _getch() -> int: ...

1
include/typings/pkpy.pyi Normal file
View File

@ -0,0 +1 @@
from typing import Any

View File

@ -1,4 +0,0 @@
def _kbhit() -> int: ...
def _getch() -> int: ...
def PlaySoundA(pszSound: str, hmod: int, fdwSound: int) -> bool: ...

View File

@ -204,6 +204,9 @@ void VM__ctor(VM* self) {
pk__add_module_array2d(); pk__add_module_array2d();
// add modules // add modules
pk__add_module_pkpy();
pk__add_module_conio();
pk__add_module_os(); pk__add_module_os();
pk__add_module_sys(); pk__add_module_sys();
pk__add_module_math(); pk__add_module_math();
@ -216,9 +219,6 @@ void VM__ctor(VM* self) {
pk__add_module_traceback(); pk__add_module_traceback();
pk__add_module_enum(); pk__add_module_enum();
// add win32 module
pk__add_module_win32();
// add python builtins // add python builtins
do { do {
bool ok; bool ok;

32
src/modules/pkpy.c Normal file
View File

@ -0,0 +1,32 @@
#include "pocketpy/pocketpy.h"
void pk__add_module_pkpy() { py_newmodule("pkpy"); }
#ifdef _WIN32
#include <conio.h>
static bool conio__kbhit(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _kbhit();
py_newint(py_retval(), ret);
return true;
}
static bool conio__getch(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _getch();
py_newint(py_retval(), ret);
return true;
}
#endif
void pk__add_module_conio() {
py_Ref mod = py_newmodule("conio");
#ifdef _WIN32
py_bindfunc(mod, "_kbhit", conio__kbhit);
py_bindfunc(mod, "_getch", conio__getch);
#endif
}

View File

@ -1,47 +0,0 @@
#include "pocketpy/pocketpy.h"
#if defined(_WIN32) && defined(PK_MODULE_WIN32)
#include <windows.h>
#include <conio.h>
static bool win32__kbhit(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _kbhit();
py_newint(py_retval(), ret);
return true;
}
static bool win32__getch(int argc, py_Ref argv) {
PY_CHECK_ARGC(0);
int ret = _getch();
py_newint(py_retval(), ret);
return true;
}
static bool win32_PlaySoundA(int argc, py_Ref argv) {
PY_CHECK_ARGC(3);
PY_CHECK_ARG_TYPE(0, tp_str);
PY_CHECK_ARG_TYPE(1, tp_int);
PY_CHECK_ARG_TYPE(2, tp_int);
const char* pszSound = py_tostr(argv);
py_i64 hmod = py_toint(py_arg(1));
py_i64 fdwSound = py_toint(py_arg(2));
int ret = PlaySoundA(pszSound, (HMODULE)hmod, fdwSound);
py_newbool(py_retval(), ret);
return true;
}
#endif
void pk__add_module_win32() {
#if defined(_WIN32) && defined(PK_MODULE_WIN32)
py_Ref mod = py_newmodule("win32");
py_bindfunc(mod, "_kbhit", win32__kbhit);
py_bindfunc(mod, "_getch", win32__getch);
py_bindfunc(mod, "PlaySoundA", win32_PlaySoundA);
#endif
}