mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 12:00:18 +00:00
...
This commit is contained in:
parent
94c17ac0b7
commit
4ccf4d304b
@ -441,10 +441,9 @@ __NEXT_STEP:;
|
|||||||
Str source;
|
Str source;
|
||||||
auto it = _lazy_modules.find(name);
|
auto it = _lazy_modules.find(name);
|
||||||
if(it == _lazy_modules.end()){
|
if(it == _lazy_modules.end()){
|
||||||
bool ok = false;
|
Bytes b = _read_file_cwd(fmt(name, ".py"));
|
||||||
Bytes b = _read_file_cwd(fmt(name, ".py"), &ok);
|
if(!b) _error("ImportError", fmt("module ", name.escape(), " not found"));
|
||||||
source = Str(b._data);
|
source = Str(b.str());
|
||||||
if(!ok) _error("ImportError", fmt("module ", name.escape(), " not found"));
|
|
||||||
}else{
|
}else{
|
||||||
source = it->second;
|
source = it->second;
|
||||||
_lazy_modules.erase(it);
|
_lazy_modules.erase(it);
|
||||||
|
30
src/io.h
30
src/io.h
@ -11,18 +11,14 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
inline Bytes _read_file_cwd(const Str& name, bool* ok){
|
inline Bytes _read_file_cwd(const Str& name){
|
||||||
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){
|
if(!exists) return Bytes();
|
||||||
*ok = false;
|
|
||||||
return Bytes();
|
|
||||||
}
|
|
||||||
std::ifstream ifs(path);
|
std::ifstream ifs(path);
|
||||||
std::string buffer((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
|
std::vector<char> buffer(std::istreambuf_iterator<char>(ifs), {});
|
||||||
ifs.close();
|
ifs.close();
|
||||||
*ok = true;
|
return Bytes(std::move(buffer));
|
||||||
return Bytes({std::move(buffer)});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct FileIO {
|
struct FileIO {
|
||||||
@ -56,10 +52,15 @@ struct FileIO {
|
|||||||
|
|
||||||
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]);
|
||||||
Bytes buffer;
|
std::vector<char> buffer;
|
||||||
io._fs >> buffer._data;
|
while(true){
|
||||||
if(io.is_text()) return VAR(Str(buffer._data));
|
char c = io._fs.get();
|
||||||
return VAR(buffer);
|
if(io._fs.eof()) break;
|
||||||
|
buffer.push_back(c);
|
||||||
|
}
|
||||||
|
Bytes b(std::move(buffer));
|
||||||
|
if(io.is_text()) return VAR(Str(b.str()));
|
||||||
|
return VAR(std::move(b));
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "write", [](VM* vm, ArgsView args){
|
vm->bind_method<1>(type, "write", [](VM* vm, ArgsView args){
|
||||||
@ -67,7 +68,7 @@ struct FileIO {
|
|||||||
if(io.is_text()) io._fs << CAST(Str&, args[1]);
|
if(io.is_text()) io._fs << CAST(Str&, args[1]);
|
||||||
else{
|
else{
|
||||||
Bytes& buffer = CAST(Bytes&, args[1]);
|
Bytes& buffer = CAST(Bytes&, args[1]);
|
||||||
io._fs << buffer._data;
|
io._fs.write(buffer.data(), buffer.size());
|
||||||
}
|
}
|
||||||
return vm->None;
|
return vm->None;
|
||||||
});
|
});
|
||||||
@ -175,8 +176,7 @@ namespace pkpy{
|
|||||||
inline void add_module_io(VM* vm){}
|
inline void add_module_io(VM* vm){}
|
||||||
inline void add_module_os(VM* vm){}
|
inline void add_module_os(VM* vm){}
|
||||||
|
|
||||||
inline Bytes _read_file_cwd(const Str& name, bool* ok){
|
inline Bytes _read_file_cwd(const Str& name){
|
||||||
*ok = false;
|
|
||||||
return Bytes();
|
return Bytes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
18
src/obj.h
18
src/obj.h
@ -58,13 +58,25 @@ struct Range {
|
|||||||
};
|
};
|
||||||
|
|
||||||
struct Bytes{
|
struct Bytes{
|
||||||
std::string _data;
|
std::vector<char> _data;
|
||||||
|
bool _ok;
|
||||||
|
|
||||||
int size() const noexcept { return _data.size(); }
|
int size() const noexcept { return _data.size(); }
|
||||||
int operator[](int i) const noexcept { return (int)(uint8_t)_data[i]; }
|
int operator[](int i) const noexcept { return (int)(uint8_t)_data[i]; }
|
||||||
|
const char* data() const noexcept { return _data.data(); }
|
||||||
|
|
||||||
bool operator==(const Bytes& rhs) const noexcept { return _data == rhs._data; }
|
bool operator==(const Bytes& rhs) const noexcept {
|
||||||
bool operator!=(const Bytes& rhs) const noexcept { return _data != rhs._data; }
|
return _data == rhs._data;
|
||||||
|
}
|
||||||
|
bool operator!=(const Bytes& rhs) const noexcept {
|
||||||
|
return _data != rhs._data;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string str() const noexcept { return std::string(_data.begin(), _data.end()); }
|
||||||
|
|
||||||
|
Bytes() : _data(), _ok(false) {}
|
||||||
|
Bytes(std::vector<char>&& data) : _data(std::move(data)), _ok(true) {}
|
||||||
|
operator bool() const noexcept { return _ok; }
|
||||||
};
|
};
|
||||||
|
|
||||||
using Super = std::pair<PyObject*, Type>;
|
using Super = std::pair<PyObject*, Type>;
|
||||||
|
@ -343,8 +343,8 @@ inline void init_builtins(VM* _vm) {
|
|||||||
_vm->bind_method<0>("str", "__iter__", CPP_LAMBDA(vm->PyIter(StringIter(vm, args[0]))));
|
_vm->bind_method<0>("str", "__iter__", CPP_LAMBDA(vm->PyIter(StringIter(vm, args[0]))));
|
||||||
|
|
||||||
_vm->bind_method<0>("str", "__repr__", [](VM* vm, ArgsView args) {
|
_vm->bind_method<0>("str", "__repr__", [](VM* vm, ArgsView args) {
|
||||||
const Str& _self = CAST(Str&, args[0]);
|
const Str& self = CAST(Str&, args[0]);
|
||||||
return VAR(_self.escape());
|
return VAR(self.escape());
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<0>("str", "__json__", [](VM* vm, ArgsView args) {
|
_vm->bind_method<0>("str", "__json__", [](VM* vm, ArgsView args) {
|
||||||
@ -414,7 +414,9 @@ inline void init_builtins(VM* _vm) {
|
|||||||
|
|
||||||
_vm->bind_method<0>("str", "encode", [](VM* vm, ArgsView args) {
|
_vm->bind_method<0>("str", "encode", [](VM* vm, ArgsView args) {
|
||||||
const Str& self = CAST(Str&, args[0]);
|
const Str& self = CAST(Str&, args[0]);
|
||||||
return VAR(Bytes{self.str()});
|
std::vector<char> buffer(self.length());
|
||||||
|
memcpy(buffer.data(), self.data, self.length());
|
||||||
|
return VAR(Bytes(std::move(buffer)));
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<1>("str", "join", [](VM* vm, ArgsView args) {
|
_vm->bind_method<1>("str", "join", [](VM* vm, ArgsView args) {
|
||||||
@ -606,7 +608,7 @@ inline void init_builtins(VM* _vm) {
|
|||||||
_vm->bind_method<0>("bytes", "decode", [](VM* vm, ArgsView args) {
|
_vm->bind_method<0>("bytes", "decode", [](VM* vm, ArgsView args) {
|
||||||
const Bytes& self = CAST(Bytes&, args[0]);
|
const Bytes& self = CAST(Bytes&, args[0]);
|
||||||
// TODO: check encoding is utf-8
|
// TODO: check encoding is utf-8
|
||||||
return VAR(Str(self._data));
|
return VAR(Str(self.str()));
|
||||||
});
|
});
|
||||||
|
|
||||||
_vm->bind_method<1>("bytes", "__eq__", [](VM* vm, ArgsView args) {
|
_vm->bind_method<1>("bytes", "__eq__", [](VM* vm, ArgsView args) {
|
||||||
|
@ -207,10 +207,10 @@ struct Str{
|
|||||||
case '\r': ss << "\\r"; break;
|
case '\r': ss << "\\r"; break;
|
||||||
case '\t': ss << "\\t"; break;
|
case '\t': ss << "\\t"; break;
|
||||||
default:
|
default:
|
||||||
if (c >= 32 && c <= 126) {
|
if ('\x00' <= c && c <= '\x1f') {
|
||||||
ss << c;
|
ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)c;
|
||||||
} else {
|
} else {
|
||||||
ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << (int)(uint8_t)c;
|
ss << c;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
src/vm.h
2
src/vm.h
@ -24,7 +24,7 @@ namespace pkpy{
|
|||||||
#define POPX() (s_data.popx())
|
#define POPX() (s_data.popx())
|
||||||
#define STACK_VIEW(n) (s_data.view(n))
|
#define STACK_VIEW(n) (s_data.view(n))
|
||||||
|
|
||||||
Bytes _read_file_cwd(const Str& name, bool* ok);
|
Bytes _read_file_cwd(const Str& name);
|
||||||
|
|
||||||
#define DEF_NATIVE_2(ctype, ptype) \
|
#define DEF_NATIVE_2(ctype, ptype) \
|
||||||
template<> inline ctype py_cast<ctype>(VM* vm, PyObject* obj) { \
|
template<> inline ctype py_cast<ctype>(VM* vm, PyObject* obj) { \
|
||||||
|
Loading…
x
Reference in New Issue
Block a user