blueloveTH 2 vuotta sitten
vanhempi
commit
f9ed405bf5
2 muutettua tiedostoa jossa 12 lisäystä ja 10 poistoa
  1. 11 10
      src/io.h
  2. 1 0
      src/vm.h

+ 11 - 10
src/io.h

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

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