From 70e8a60f8bf42012deac032fdda0d305d843ced7 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 30 Apr 2023 22:01:28 +0800 Subject: [PATCH] ... --- amalgamate.py | 2 +- src/io.h | 30 ++++++++++++------------------ src/requests.h | 2 +- src/vm.h | 3 ++- 4 files changed, 16 insertions(+), 21 deletions(-) diff --git a/amalgamate.py b/amalgamate.py index 73cf30da..e3614989 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -9,7 +9,7 @@ pipeline = [ ["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], ["obj.h", "codeobject.h", "frame.h"], ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"], - ["iter.h", "cffi.h", "requests.h", "io.h", "_generated.h", "pocketpy.h"] + ["_generated.h", "iter.h", "cffi.h", "requests.h", "io.h", "pocketpy.h"] ] copied = set() diff --git a/src/io.h b/src/io.h index b93803eb..2f686705 100644 --- a/src/io.h +++ b/src/io.h @@ -10,17 +10,6 @@ #include namespace pkpy{ - -inline Bytes _read_file_cwd(const Str& name){ - std::filesystem::path path(name.sv()); - bool exists = std::filesystem::exists(path); - if(!exists) return Bytes(); - std::ifstream ifs(path, std::ios::binary); - std::vector buffer(std::istreambuf_iterator(ifs), {}); - ifs.close(); - return Bytes(std::move(buffer)); -} - struct FileIO { PY_CLASS(FileIO, io, FileIO) @@ -94,6 +83,16 @@ struct FileIO { }; inline void add_module_io(VM* vm){ + _read_file_cwd = [](const Str& name){ + std::filesystem::path path(name.sv()); + bool exists = std::filesystem::exists(path); + if(!exists) return Bytes(); + std::ifstream ifs(path, std::ios::binary); + std::vector buffer(std::istreambuf_iterator(ifs), {}); + ifs.close(); + return Bytes(std::move(buffer)); + }; + PyObject* mod = vm->new_module("io"); FileIO::register_class(vm, mod); vm->bind_builtin_func<2>("open", [](VM* vm, ArgsView args){ @@ -182,13 +181,8 @@ inline void add_module_os(VM* vm){ #else namespace pkpy{ -inline void add_module_io(VM* vm){} -inline void add_module_os(VM* vm){} - -inline Bytes _read_file_cwd(const Str& name){ - return Bytes(); -} - +inline void add_module_io(void* vm){} +inline void add_module_os(void* vm){} } // namespace pkpy #endif \ No newline at end of file diff --git a/src/requests.h b/src/requests.h index d510723c..2574b77c 100644 --- a/src/requests.h +++ b/src/requests.h @@ -100,6 +100,6 @@ inline void add_module_requests(VM* vm){ #else -inline void add_module_requests(VM* vm){ } +inline void add_module_requests(void* vm){ } #endif \ No newline at end of file diff --git a/src/vm.h b/src/vm.h index c4ce61d0..90331cd2 100644 --- a/src/vm.h +++ b/src/vm.h @@ -24,7 +24,8 @@ namespace pkpy{ #define POPX() (s_data.popx()) #define STACK_VIEW(n) (s_data.view(n)) -Bytes _read_file_cwd(const Str& name); +typedef Bytes (*ReadFileCwdFunc)(const Str& name); +inline ReadFileCwdFunc _read_file_cwd = [](const Str& name) { return Bytes(); }; #define DEF_NATIVE_2(ctype, ptype) \ template<> inline ctype py_cast(VM* vm, PyObject* obj) { \