From ee905c84dac508ed43bee679ddda05aa5e51210b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 4 Feb 2023 18:01:25 +0800 Subject: [PATCH] up --- src/compiler.h | 4 ++-- src/obj.h | 2 +- src/pocketpy.h | 10 +++------- src/ref.h | 2 -- src/safestl.h | 23 ++++++++--------------- src/vm.h | 9 ++------- 6 files changed, 16 insertions(+), 34 deletions(-) diff --git a/src/compiler.h b/src/compiler.h index 50e017e9..2a35ce23 100644 --- a/src/compiler.h +++ b/src/compiler.h @@ -4,7 +4,7 @@ #include "error.h" #include "vm.h" -class Compiler; +struct Compiler; typedef void (Compiler::*GrammarFn)(); typedef void (Compiler::*CompilerAction)(); @@ -169,7 +169,7 @@ struct Compiler { } else { parser->set_next_token(TK("@num"), vm->PyInt(std::stoll(m[0], &size, base))); } - if (size != m.length()) throw std::runtime_error("length mismatch"); + if (size != m.length()) UNREACHABLE(); } }catch(std::exception& _){ syntaxError("invalid number literal"); diff --git a/src/obj.h b/src/obj.h index 70bbd3f9..ae0ad7ac 100644 --- a/src/obj.h +++ b/src/obj.h @@ -3,9 +3,9 @@ #include "safestl.h" struct CodeObject; +struct Frame; struct BaseRef; class VM; -class Frame; //typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::Args&); typedef std::function _CppFuncRaw; diff --git a/src/pocketpy.h b/src/pocketpy.h index 81c6d7af..a52cafbf 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -534,9 +534,7 @@ void __add_module_json(VM* vm){ return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals); }); - vm->bindFunc<1>(mod, "dumps", [](VM* vm, const pkpy::Args& args) { - return vm->asJson(args[0]); - }); + vm->bindFunc<1>(mod, "dumps", CPP_LAMBDA(vm->call(args[0], __json__))); } void __add_module_math(VM* vm){ @@ -778,10 +776,8 @@ extern "C" { _Str _stderr = s_err->str(); _StrStream ss; ss << '{' << "\"stdout\": " << _stdout.__escape(false); - ss << ", "; - ss << "\"stderr\": " << _stderr.__escape(false) << '}'; - s_out->str(""); - s_err->str(""); + ss << ", " << "\"stderr\": " << _stderr.__escape(false) << '}'; + s_out->str(""); s_err->str(""); return strdup(ss.str().c_str()); } } diff --git a/src/ref.h b/src/ref.h index 288eff12..bb1cd6cd 100644 --- a/src/ref.h +++ b/src/ref.h @@ -2,8 +2,6 @@ #include "obj.h" -class Frame; - struct BaseRef { virtual PyVar get(VM*, Frame*) const = 0; virtual void set(VM*, Frame*, PyVar) const = 0; diff --git a/src/safestl.h b/src/safestl.h index 2db9da9d..1f9cfb15 100644 --- a/src/safestl.h +++ b/src/safestl.h @@ -29,15 +29,14 @@ public: return std::vector::operator[](i); } - // define constructors the same as std::vector using std::vector::vector; }; typedef emhash8::HashMap<_Str, PyVar> PyVarDict; namespace pkpy { - const int MAX_POOLING_N = 10; - static thread_local std::vector* _poolArgs = new std::vector[MAX_POOLING_N]; + const int kMaxPoolSize = 10; + static thread_local std::vector* _poolArgs = new std::vector[kMaxPoolSize]; class Args { PyVar* _args; @@ -49,7 +48,7 @@ namespace pkpy { this->_size = 0; return; } - if(n >= MAX_POOLING_N || _poolArgs[n].empty()){ + if(n >= kMaxPoolSize || _poolArgs[n].empty()){ this->_args = new PyVar[n]; this->_size = n; }else{ @@ -61,7 +60,7 @@ namespace pkpy { void __tryRelease(){ if(_size == 0 || _args == nullptr) return; - if(_size >= MAX_POOLING_N || _poolArgs[_size].size() > 32){ + if(_size >= kMaxPoolSize || _poolArgs[_size].size() > 32){ delete[] _args; }else{ for(int i = 0; i < _size; i++) _args[i].reset(); @@ -70,9 +69,7 @@ namespace pkpy { } public: - Args(size_t n){ - __tryAlloc(n); - } + Args(size_t n){ __tryAlloc(n); } Args(const Args& other){ __tryAlloc(other._size); @@ -88,9 +85,7 @@ namespace pkpy { Args(PyVarList&& other) noexcept { __tryAlloc(other.size()); - for(int i=0; i<_size; i++){ - _args[i] = std::move(other[i]); - } + for(int i=0; i<_size; i++) _args[i] = std::move(other[i]); other.clear(); } @@ -124,16 +119,14 @@ namespace pkpy { memcpy((void*)(_args+1), (void*)old_args, sizeof(PyVar)*old_size); memset((void*)old_args, 0, sizeof(PyVar)*old_size); - if(old_size >= MAX_POOLING_N || _poolArgs[old_size].size() > 32){ + if(old_size >= kMaxPoolSize || _poolArgs[old_size].size() > 32){ delete[] old_args; }else{ _poolArgs[old_size].push_back(old_args); } } - ~Args(){ - __tryRelease(); - } + ~Args(){ __tryRelease(); } }; const Args& noArg(){ diff --git a/src/vm.h b/src/vm.h index 732e83f1..4008046b 100644 --- a/src/vm.h +++ b/src/vm.h @@ -22,7 +22,6 @@ class VM { std::vector _small_integers; // [-5, 256] -protected: std::stack< std::unique_ptr > callstack; PyVar __py2py_call_signal; @@ -373,8 +372,8 @@ public: } PyVar asStr(const PyVar& obj){ - PyVarOrNull str_fn = getattr(obj, __str__, false); - if(str_fn != nullptr) return call(str_fn); + PyVarOrNull f = getattr(obj, __str__, false); + if(f != nullptr) return call(f); return asRepr(obj); } @@ -388,10 +387,6 @@ public: return call(obj, __repr__); } - PyVar asJson(const PyVar& obj){ - return call(obj, __json__); - } - const PyVar& asBool(const PyVar& obj){ if(obj->is_type(_tp_bool)) return obj; if(obj == None) return False;