diff --git a/include/pocketpy/config.h b/include/pocketpy/config.h index 48082b2c..54723171 100644 --- a/include/pocketpy/config.h +++ b/include/pocketpy/config.h @@ -9,10 +9,15 @@ /*************** feature settings ***************/ // Whether to compile os-related modules or not +#ifndef PK_ENABLE_OS // can be overrided by cmake #define PK_ENABLE_OS 1 +#endif + // Enable this if you are working with multi-threading (experimental) // This triggers necessary locks to make the VM thread-safe +#ifndef PK_ENABLE_THREAD // can be overrided by cmake #define PK_ENABLE_THREAD 0 +#endif // Enable this for `vm->_ceval_on_step` #define PK_ENABLE_CEVAL_CALLBACK 0 diff --git a/src/io.cpp b/src/io.cpp index c0251d55..5c61f230 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -3,20 +3,45 @@ namespace pkpy{ +static FILE* io_fopen(const char* name, const char* mode){ +#if _WIN32 + FILE* fp; + errno_t err = fopen_s(&fp, name, mode); + if(err != 0) return nullptr; + return fp; +#else + return fopen(name, mode); +#endif +} + +static size_t io_fread(void* buffer, size_t size, size_t count, FILE* fp){ +#if _WIN32 + return fread_s(buffer, size, size, count, fp); +#else + return fread(buffer, size, count, fp); +#endif +} + +#define io_fclose fclose +#define io_ftell ftell +#define io_fseek fseek +#define io_fwrite fwrite + + Bytes _default_import_handler(const Str& name){ #if PK_ENABLE_OS std::filesystem::path path(name.sv()); bool exists = std::filesystem::exists(path); if(!exists) return Bytes(); std::string cname = name.str(); - FILE* fp = fopen(cname.c_str(), "rb"); + FILE* fp = io_fopen(cname.c_str(), "rb"); if(!fp) return Bytes(); - fseek(fp, 0, SEEK_END); - std::vector buffer(ftell(fp)); - fseek(fp, 0, SEEK_SET); - size_t sz = fread(buffer.data(), 1, buffer.size(), fp); + io_fseek(fp, 0, SEEK_END); + std::vector buffer(io_ftell(fp)); + io_fseek(fp, 0, SEEK_SET); + size_t sz = io_fread(buffer.data(), 1, buffer.size(), fp); PK_UNUSED(sz); - fclose(fp); + io_fclose(fp); return Bytes(std::move(buffer)); #else return Bytes(); @@ -34,10 +59,10 @@ Bytes _default_import_handler(const Str& name){ vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){ FileIO& io = CAST(FileIO&, args[0]); - fseek(io.fp, 0, SEEK_END); - std::vector buffer(ftell(io.fp)); - fseek(io.fp, 0, SEEK_SET); - size_t sz = fread(buffer.data(), 1, buffer.size(), io.fp); + io_fseek(io.fp, 0, SEEK_END); + std::vector buffer(io_ftell(io.fp)); + io_fseek(io.fp, 0, SEEK_SET); + size_t sz = io_fread(buffer.data(), 1, buffer.size(), io.fp); PK_UNUSED(sz); Bytes b(std::move(buffer)); if(io.is_text()) return VAR(b.str()); @@ -48,10 +73,10 @@ Bytes _default_import_handler(const Str& name){ FileIO& io = CAST(FileIO&, args[0]); if(io.is_text()){ Str& s = CAST(Str&, args[1]); - fwrite(s.data, 1, s.length(), io.fp); + io_fwrite(s.data, 1, s.length(), io.fp); }else{ Bytes& buffer = CAST(Bytes&, args[1]); - fwrite(buffer.data(), 1, buffer.size(), io.fp); + io_fwrite(buffer.data(), 1, buffer.size(), io.fp); } return vm->None; }); @@ -72,13 +97,13 @@ Bytes _default_import_handler(const Str& name){ } FileIO::FileIO(VM* vm, std::string file, std::string mode): file(file), mode(mode) { - fp = fopen(file.c_str(), mode.c_str()); + fp = io_fopen(file.c_str(), mode.c_str()); if(!fp) vm->IOError(strerror(errno)); } void FileIO::close(){ if(fp == nullptr) return; - fclose(fp); + io_fclose(fp); fp = nullptr; }