This commit is contained in:
blueloveTH 2023-04-30 22:01:28 +08:00
parent a04cdb4cad
commit 70e8a60f8b
4 changed files with 16 additions and 21 deletions

View File

@ -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()

View File

@ -10,17 +10,6 @@
#include <filesystem>
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<char> buffer(std::istreambuf_iterator<char>(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<char> buffer(std::istreambuf_iterator<char>(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

View File

@ -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

View File

@ -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<ctype>(VM* vm, PyObject* obj) { \