mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
up
This commit is contained in:
parent
9269f6f68a
commit
ee905c84da
@ -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");
|
||||
|
@ -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<PyVar(VM*, const pkpy::Args&)> _CppFuncRaw;
|
||||
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -29,15 +29,14 @@ public:
|
||||
return std::vector<PyVar>::operator[](i);
|
||||
}
|
||||
|
||||
// define constructors the same as std::vector
|
||||
using std::vector<PyVar>::vector;
|
||||
};
|
||||
|
||||
typedef emhash8::HashMap<_Str, PyVar> PyVarDict;
|
||||
|
||||
namespace pkpy {
|
||||
const int MAX_POOLING_N = 10;
|
||||
static thread_local std::vector<PyVar*>* _poolArgs = new std::vector<PyVar*>[MAX_POOLING_N];
|
||||
const int kMaxPoolSize = 10;
|
||||
static thread_local std::vector<PyVar*>* _poolArgs = new std::vector<PyVar*>[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(){
|
||||
|
9
src/vm.h
9
src/vm.h
@ -22,7 +22,6 @@
|
||||
|
||||
class VM {
|
||||
std::vector<PyVar> _small_integers; // [-5, 256]
|
||||
protected:
|
||||
std::stack< std::unique_ptr<Frame> > 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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user