up

Update vm.h

Update vm.h
This commit is contained in:
blueloveTH 2022-12-01 18:00:01 +08:00
parent 22ed9ae16f
commit 8d152c225f
4 changed files with 23 additions and 5 deletions

View File

@ -22,7 +22,8 @@ struct SourceMetadata {
_Str getLine(int lineno) const { _Str getLine(int lineno) const {
if(lineno == -1) return "<?>"; if(lineno == -1) return "<?>";
const char* _start = lineStarts.at(lineno-1); lineno -= 1;
const char* _start = lineStarts.at(lineno < 0 ? 0 : lineno);
const char* i = _start; const char* i = _start;
while(*i != '\n' && *i != '\0') i++; while(*i != '\n' && *i != '\0') i++;
return _Str(_start, i-_start); return _Str(_start, i-_start);

View File

@ -5,7 +5,7 @@
typedef int64_t _Int; typedef int64_t _Int;
typedef double _Float; typedef double _Float;
#define PK_VERSION "0.3.8" #define PK_VERSION "0.3.9"
class CodeObject; class CodeObject;
class BasePointer; class BasePointer;

View File

@ -597,6 +597,16 @@ void __addModuleTime(VM* vm){
auto now = std::chrono::high_resolution_clock::now(); auto now = std::chrono::high_resolution_clock::now();
return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0); return vm->PyFloat(std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()).count() / 1000000.0);
}); });
vm->bindFunc(mod, "sleep", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkArgSize(args, 1);
if(!vm->isIntOrFloat(args[0])){
vm->typeError("time.sleep() argument must be int or float");
}
double sec = vm->numToFloat(args[0]);
vm->sleepForSecs(sec);
return vm->None;
});
} }
void __addModuleSys(VM* vm){ void __addModuleSys(VM* vm){

View File

@ -49,7 +49,7 @@ protected:
PyVarDict _modules; // loaded modules PyVarDict _modules; // loaded modules
std::map<_Str, _Code> _lazyModules; // lazy loaded modules std::map<_Str, _Code> _lazyModules; // lazy loaded modules
PyVar __py2py_call_signal; PyVar __py2py_call_signal;
bool _stopFlag = false; std::atomic<bool> _stopFlag = false;
void _checkStopFlag(){ void _checkStopFlag(){
if(_stopFlag){ if(_stopFlag){
@ -412,6 +412,14 @@ public:
_stopFlag = true; _stopFlag = true;
} }
void sleepForSecs(_Float sec){
_Int ms = (_Int)(sec * 1000);
for(_Int i=0; i<ms; i+=20){
_checkStopFlag();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
}
PyVar asStr(const PyVar& obj){ PyVar asStr(const PyVar& obj){
PyVarOrNull str_fn = getAttr(obj, __str__, false); PyVarOrNull str_fn = getAttr(obj, __str__, false);
if(str_fn != nullptr) return call(str_fn, {}); if(str_fn != nullptr) return call(str_fn, {});
@ -941,7 +949,6 @@ public:
} }
virtual ~VM() { virtual ~VM() {
callstack.clear();
if(!use_stdio){ if(!use_stdio){
delete _stdout; delete _stdout;
delete _stderr; delete _stderr;
@ -1107,7 +1114,7 @@ class ThreadedVM : public VM {
void __deleteThread(){ void __deleteThread(){
if(_thread != nullptr){ if(_thread != nullptr){
keyboardInterrupt(); if(_state == THREAD_RUNNING || _state == THREAD_SUSPENDED) keyboardInterrupt();
_thread->join(); _thread->join();
delete _thread; delete _thread;
_thread = nullptr; _thread = nullptr;