From dd27071b2241ed3b2044d10efe85725d91470378 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 24 Apr 2023 17:18:43 +0800 Subject: [PATCH] fix https://github.com/blueloveTH/pocketpy/issues/51 --- src/common.h | 6 +++--- src/io.h | 2 +- src/main.cpp | 2 +- src/pocketpy.h | 11 +++++++---- src/vm.h | 4 +++- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/common.h b/src/common.h index 39575557..c3a4fe55 100644 --- a/src/common.h +++ b/src/common.h @@ -43,10 +43,10 @@ #define DEBUG_NO_AUTO_GC 0 #define DEBUG_GC_STATS 0 -#if (defined(__ANDROID__) && __ANDROID_API__ <= 22) || defined(__EMSCRIPTEN__) -#define PK_ENABLE_FILEIO 0 +#if (defined(__ANDROID__) && __ANDROID_API__ <= 22) +#define PK_ENABLE_OS 0 #else -#define PK_ENABLE_FILEIO 1 +#define PK_ENABLE_OS 1 #endif // This is the maximum number of arguments in a function declaration diff --git a/src/io.h b/src/io.h index a5101fa7..c0e8534a 100644 --- a/src/io.h +++ b/src/io.h @@ -4,7 +4,7 @@ #include "cffi.h" #include "common.h" -#if PK_ENABLE_FILEIO +#if PK_ENABLE_OS #include #include diff --git a/src/main.cpp b/src/main.cpp index f8599109..cee05cfc 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #ifndef __EMSCRIPTEN__ int main(int argc, char** argv){ - pkpy::VM* vm = pkpy_new_vm(true); + pkpy::VM* vm = pkpy_new_vm(true, true); vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::ArgsView args){ return VAR(pkpy::getline()); }); diff --git a/src/pocketpy.h b/src/pocketpy.h index 4da662dd..bf93a0bb 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -828,12 +828,15 @@ inline void VM::post_init(){ add_module_math(this); add_module_re(this); add_module_dis(this); - add_module_io(this); - add_module_os(this); add_module_c(this); add_module_gc(this); add_module_random(this); + if(enable_os){ + add_module_io(this); + add_module_os(this); + } + for(const char* name: {"this", "functools", "collections", "heapq", "bisect"}){ _lazy_modules[name] = kPythonLibs[name]; } @@ -970,8 +973,8 @@ extern "C" { __EXPORT /// Create a virtual machine. - pkpy::VM* pkpy_new_vm(bool use_stdio){ - return PKPY_ALLOCATE(pkpy::VM, use_stdio); + pkpy::VM* pkpy_new_vm(bool use_stdio, bool enable_os){ + return PKPY_ALLOCATE(pkpy::VM, use_stdio, enable_os); } __EXPORT diff --git a/src/vm.h b/src/vm.h index b5119baa..ca0a8c1e 100644 --- a/src/vm.h +++ b/src/vm.h @@ -104,7 +104,9 @@ public: Type tp_slice, tp_range, tp_module; Type tp_super, tp_exception, tp_bytes; - VM(bool use_stdio) : heap(this){ + const bool enable_os; + + VM(bool use_stdio, bool enable_os) : heap(this), enable_os(enable_os) { this->vm = this; this->_stdout = use_stdio ? &std::cout : &_stdout_buffer; this->_stderr = use_stdio ? &std::cerr : &_stderr_buffer;