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

View File

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

View File

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