This commit is contained in:
blueloveTH 2023-08-19 16:17:32 +08:00
parent 09fdd158b0
commit bc559e98fe
2 changed files with 44 additions and 14 deletions

View File

@ -9,10 +9,15 @@
/*************** feature settings ***************/ /*************** feature settings ***************/
// Whether to compile os-related modules or not // Whether to compile os-related modules or not
#ifndef PK_ENABLE_OS // can be overrided by cmake
#define PK_ENABLE_OS 1 #define PK_ENABLE_OS 1
#endif
// Enable this if you are working with multi-threading (experimental) // Enable this if you are working with multi-threading (experimental)
// This triggers necessary locks to make the VM thread-safe // This triggers necessary locks to make the VM thread-safe
#ifndef PK_ENABLE_THREAD // can be overrided by cmake
#define PK_ENABLE_THREAD 0 #define PK_ENABLE_THREAD 0
#endif
// Enable this for `vm->_ceval_on_step` // Enable this for `vm->_ceval_on_step`
#define PK_ENABLE_CEVAL_CALLBACK 0 #define PK_ENABLE_CEVAL_CALLBACK 0

View File

@ -3,20 +3,45 @@
namespace pkpy{ 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){ Bytes _default_import_handler(const Str& name){
#if PK_ENABLE_OS #if PK_ENABLE_OS
std::filesystem::path path(name.sv()); std::filesystem::path path(name.sv());
bool exists = std::filesystem::exists(path); bool exists = std::filesystem::exists(path);
if(!exists) return Bytes(); if(!exists) return Bytes();
std::string cname = name.str(); std::string cname = name.str();
FILE* fp = fopen(cname.c_str(), "rb"); FILE* fp = io_fopen(cname.c_str(), "rb");
if(!fp) return Bytes(); if(!fp) return Bytes();
fseek(fp, 0, SEEK_END); io_fseek(fp, 0, SEEK_END);
std::vector<char> buffer(ftell(fp)); std::vector<char> buffer(io_ftell(fp));
fseek(fp, 0, SEEK_SET); io_fseek(fp, 0, SEEK_SET);
size_t sz = fread(buffer.data(), 1, buffer.size(), fp); size_t sz = io_fread(buffer.data(), 1, buffer.size(), fp);
PK_UNUSED(sz); PK_UNUSED(sz);
fclose(fp); io_fclose(fp);
return Bytes(std::move(buffer)); return Bytes(std::move(buffer));
#else #else
return Bytes(); return Bytes();
@ -34,10 +59,10 @@ Bytes _default_import_handler(const Str& name){
vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){
FileIO& io = CAST(FileIO&, args[0]); FileIO& io = CAST(FileIO&, args[0]);
fseek(io.fp, 0, SEEK_END); io_fseek(io.fp, 0, SEEK_END);
std::vector<char> buffer(ftell(io.fp)); std::vector<char> buffer(io_ftell(io.fp));
fseek(io.fp, 0, SEEK_SET); io_fseek(io.fp, 0, SEEK_SET);
size_t sz = fread(buffer.data(), 1, buffer.size(), io.fp); size_t sz = io_fread(buffer.data(), 1, buffer.size(), io.fp);
PK_UNUSED(sz); PK_UNUSED(sz);
Bytes b(std::move(buffer)); Bytes b(std::move(buffer));
if(io.is_text()) return VAR(b.str()); 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]); FileIO& io = CAST(FileIO&, args[0]);
if(io.is_text()){ if(io.is_text()){
Str& s = CAST(Str&, args[1]); Str& s = CAST(Str&, args[1]);
fwrite(s.data, 1, s.length(), io.fp); io_fwrite(s.data, 1, s.length(), io.fp);
}else{ }else{
Bytes& buffer = CAST(Bytes&, args[1]); 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; 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) { 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)); if(!fp) vm->IOError(strerror(errno));
} }
void FileIO::close(){ void FileIO::close(){
if(fp == nullptr) return; if(fp == nullptr) return;
fclose(fp); io_fclose(fp);
fp = nullptr; fp = nullptr;
} }