This commit is contained in:
blueloveTH 2022-12-05 01:42:58 +08:00
parent 9909e16649
commit b24b6599ab
3 changed files with 22 additions and 36 deletions

View File

@ -2,8 +2,8 @@
#include "pocketpy.h" #include "pocketpy.h"
//#define PK_DEBUG_TIME #define PK_DEBUG_TIME
#define PK_DEBUG_THREADED_REPL //#define PK_DEBUG_THREADED
struct Timer{ struct Timer{
const char* title; const char* title;
@ -65,7 +65,7 @@ void _tvm_dispatch(ThreadedVM* vm){
int main(int argc, char** argv){ int main(int argc, char** argv){
if(argc == 1){ if(argc == 1){
#ifndef PK_DEBUG_THREADED_REPL #ifndef PK_DEBUG_THREADED
VM* vm = pkpy_new_vm(true); VM* vm = pkpy_new_vm(true);
#else #else
ThreadedVM* vm = pkpy_new_tvm(true); ThreadedVM* vm = pkpy_new_tvm(true);
@ -76,7 +76,7 @@ int main(int argc, char** argv){
std::string line; std::string line;
std::getline(std::cin, line); std::getline(std::cin, line);
int result = pkpy_repl_input(&repl, line.c_str()); int result = pkpy_repl_input(&repl, line.c_str());
#ifdef PK_DEBUG_THREADED_REPL #ifdef PK_DEBUG_THREADED
if(result == (int)EXEC_DONE){ if(result == (int)EXEC_DONE){
_tvm_dispatch(vm); _tvm_dispatch(vm);
pkpy_tvm_reset_state(vm); pkpy_tvm_reset_state(vm);
@ -103,19 +103,23 @@ int main(int argc, char** argv){
code = compile(vm, src.c_str(), filename); code = compile(vm, src.c_str(), filename);
}); });
if(code == nullptr) return 1; if(code == nullptr) return 1;
//std::cout << code->toString() << std::endl;
// Timer("Running time").run([=]{ //std::cout << code->toString() << std::endl;
// vm->exec(code);
// });
// for(auto& kv : _strIntern) // for(auto& kv : _strIntern)
// std::cout << kv.first << ", "; // std::cout << kv.first << ", ";
#ifdef PK_DEBUG_THREADED
Timer("Running time").run([=]{ Timer("Running time").run([=]{
vm->execAsync(code); vm->execAsync(code);
_tvm_dispatch(vm); _tvm_dispatch(vm);
}); });
#else
Timer("Running time").run([=]{
vm->exec(code);
});
#endif
pkpy_delete(vm); pkpy_delete(vm);
return 0; return 0;
} }

View File

@ -2,33 +2,20 @@
#include "__stl__.h" #include "__stl__.h"
// sooo slow! do not use this
namespace pkpy{ namespace pkpy{
template <typename T> template <typename T>
class shared_ptr { class shared_ptr {
int* count; int* count;
T* ptr; T* ptr;
const std::function<void(T*)>* deleter = nullptr;
void _delete(){ inline void _delete(){
delete count; delete count;
if(deleter != nullptr) deleter->operator()(ptr); delete ptr;
else delete ptr;
} }
public: public:
shared_ptr() : count(nullptr), ptr(nullptr) {} shared_ptr() : count(nullptr), ptr(nullptr) {}
shared_ptr(T* ptr) : count(new int(1)), ptr(ptr) {} shared_ptr(T* ptr) : count(new int(1)), ptr(ptr) {}
shared_ptr(T* ptr, const std::function<void(T*)>*) : count(new int(1)), ptr(ptr), deleter(deleter) {}
shared_ptr(const shared_ptr& other) : count(other.count), ptr(other.ptr) {
if (count) {
++(*count);
}
}
shared_ptr(shared_ptr&& other) : count(other.count), ptr(other.ptr) {
other.count = nullptr;
other.ptr = nullptr;
}
~shared_ptr() { ~shared_ptr() {
if (count && --(*count) == 0) _delete(); if (count && --(*count) == 0) _delete();
} }
@ -54,12 +41,11 @@ namespace pkpy{
if (count && --(*count) == 0) _delete(); if (count && --(*count) == 0) _delete();
count = other.count; count = other.count;
ptr = other.ptr; ptr = other.ptr;
if (count) { if (count) ++(*count);
++(*count);
}
} }
return *this; return *this;
} }
shared_ptr& operator=(shared_ptr&& other) { shared_ptr& operator=(shared_ptr&& other) {
if (this != &other) { if (this != &other) {
if (count && --(*count) == 0) _delete(); if (count && --(*count) == 0) _delete();
@ -83,9 +69,6 @@ namespace pkpy{
int use_count() const { int use_count() const {
return count ? *count : 0; return count ? *count : 0;
} }
explicit operator bool() const {
return ptr != nullptr;
}
}; };
template <typename T, typename... Args> template <typename T, typename... Args>

View File

@ -1150,12 +1150,11 @@ public:
PyVarOrNull exec(const _Code& code, PyVar _module = nullptr) override { PyVarOrNull exec(const _Code& code, PyVar _module = nullptr) override {
if(_state == THREAD_READY) return VM::exec(code, _module); if(_state == THREAD_READY) return VM::exec(code, _module);
UNREACHABLE(); auto callstackBackup = std::move(callstack);
// auto callstackBackup = std::move(callstack); callstack.clear();
// callstack.clear(); PyVarOrNull ret = VM::exec(code, _module);
// PyVarOrNull ret = VM::exec(code, _module); callstack = std::move(callstackBackup);
// callstack = std::move(callstackBackup); return ret;
// return ret;
} }
void resetState(){ void resetState(){