diff --git a/src/io.h b/src/io.h index 2f686705..0c055718 100644 --- a/src/io.h +++ b/src/io.h @@ -10,6 +10,17 @@ #include namespace pkpy{ + +inline int _ = set_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) @@ -83,16 +94,6 @@ 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){ diff --git a/src/vm.h b/src/vm.h index 90331cd2..3d6db7eb 100644 --- a/src/vm.h +++ b/src/vm.h @@ -26,6 +26,7 @@ namespace pkpy{ typedef Bytes (*ReadFileCwdFunc)(const Str& name); inline ReadFileCwdFunc _read_file_cwd = [](const Str& name) { return Bytes(); }; +inline int set_read_file_cwd(ReadFileCwdFunc func) { _read_file_cwd = func; return 0; } #define DEF_NATIVE_2(ctype, ptype) \ template<> inline ctype py_cast(VM* vm, PyObject* obj) { \