mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
up
This commit is contained in:
parent
9269f6f68a
commit
ee905c84da
@ -4,7 +4,7 @@
|
|||||||
#include "error.h"
|
#include "error.h"
|
||||||
#include "vm.h"
|
#include "vm.h"
|
||||||
|
|
||||||
class Compiler;
|
struct Compiler;
|
||||||
|
|
||||||
typedef void (Compiler::*GrammarFn)();
|
typedef void (Compiler::*GrammarFn)();
|
||||||
typedef void (Compiler::*CompilerAction)();
|
typedef void (Compiler::*CompilerAction)();
|
||||||
@ -169,7 +169,7 @@ struct Compiler {
|
|||||||
} else {
|
} else {
|
||||||
parser->set_next_token(TK("@num"), vm->PyInt(std::stoll(m[0], &size, base)));
|
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& _){
|
}catch(std::exception& _){
|
||||||
syntaxError("invalid number literal");
|
syntaxError("invalid number literal");
|
||||||
|
@ -3,9 +3,9 @@
|
|||||||
#include "safestl.h"
|
#include "safestl.h"
|
||||||
|
|
||||||
struct CodeObject;
|
struct CodeObject;
|
||||||
|
struct Frame;
|
||||||
struct BaseRef;
|
struct BaseRef;
|
||||||
class VM;
|
class VM;
|
||||||
class Frame;
|
|
||||||
|
|
||||||
//typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::Args&);
|
//typedef PyVar (*_CppFuncRaw)(VM*, const pkpy::Args&);
|
||||||
typedef std::function<PyVar(VM*, const pkpy::Args&)> _CppFuncRaw;
|
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);
|
return vm->_exec(code, vm->top_frame()->_module, vm->top_frame()->_locals);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bindFunc<1>(mod, "dumps", [](VM* vm, const pkpy::Args& args) {
|
vm->bindFunc<1>(mod, "dumps", CPP_LAMBDA(vm->call(args[0], __json__)));
|
||||||
return vm->asJson(args[0]);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void __add_module_math(VM* vm){
|
void __add_module_math(VM* vm){
|
||||||
@ -778,10 +776,8 @@ extern "C" {
|
|||||||
_Str _stderr = s_err->str();
|
_Str _stderr = s_err->str();
|
||||||
_StrStream ss;
|
_StrStream ss;
|
||||||
ss << '{' << "\"stdout\": " << _stdout.__escape(false);
|
ss << '{' << "\"stdout\": " << _stdout.__escape(false);
|
||||||
ss << ", ";
|
ss << ", " << "\"stderr\": " << _stderr.__escape(false) << '}';
|
||||||
ss << "\"stderr\": " << _stderr.__escape(false) << '}';
|
s_out->str(""); s_err->str("");
|
||||||
s_out->str("");
|
|
||||||
s_err->str("");
|
|
||||||
return strdup(ss.str().c_str());
|
return strdup(ss.str().c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,8 +2,6 @@
|
|||||||
|
|
||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
|
|
||||||
class Frame;
|
|
||||||
|
|
||||||
struct BaseRef {
|
struct BaseRef {
|
||||||
virtual PyVar get(VM*, Frame*) const = 0;
|
virtual PyVar get(VM*, Frame*) const = 0;
|
||||||
virtual void set(VM*, Frame*, PyVar) const = 0;
|
virtual void set(VM*, Frame*, PyVar) const = 0;
|
||||||
|
@ -29,15 +29,14 @@ public:
|
|||||||
return std::vector<PyVar>::operator[](i);
|
return std::vector<PyVar>::operator[](i);
|
||||||
}
|
}
|
||||||
|
|
||||||
// define constructors the same as std::vector
|
|
||||||
using std::vector<PyVar>::vector;
|
using std::vector<PyVar>::vector;
|
||||||
};
|
};
|
||||||
|
|
||||||
typedef emhash8::HashMap<_Str, PyVar> PyVarDict;
|
typedef emhash8::HashMap<_Str, PyVar> PyVarDict;
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
const int MAX_POOLING_N = 10;
|
const int kMaxPoolSize = 10;
|
||||||
static thread_local std::vector<PyVar*>* _poolArgs = new std::vector<PyVar*>[MAX_POOLING_N];
|
static thread_local std::vector<PyVar*>* _poolArgs = new std::vector<PyVar*>[kMaxPoolSize];
|
||||||
|
|
||||||
class Args {
|
class Args {
|
||||||
PyVar* _args;
|
PyVar* _args;
|
||||||
@ -49,7 +48,7 @@ namespace pkpy {
|
|||||||
this->_size = 0;
|
this->_size = 0;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if(n >= MAX_POOLING_N || _poolArgs[n].empty()){
|
if(n >= kMaxPoolSize || _poolArgs[n].empty()){
|
||||||
this->_args = new PyVar[n];
|
this->_args = new PyVar[n];
|
||||||
this->_size = n;
|
this->_size = n;
|
||||||
}else{
|
}else{
|
||||||
@ -61,7 +60,7 @@ namespace pkpy {
|
|||||||
|
|
||||||
void __tryRelease(){
|
void __tryRelease(){
|
||||||
if(_size == 0 || _args == nullptr) return;
|
if(_size == 0 || _args == nullptr) return;
|
||||||
if(_size >= MAX_POOLING_N || _poolArgs[_size].size() > 32){
|
if(_size >= kMaxPoolSize || _poolArgs[_size].size() > 32){
|
||||||
delete[] _args;
|
delete[] _args;
|
||||||
}else{
|
}else{
|
||||||
for(int i = 0; i < _size; i++) _args[i].reset();
|
for(int i = 0; i < _size; i++) _args[i].reset();
|
||||||
@ -70,9 +69,7 @@ namespace pkpy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Args(size_t n){
|
Args(size_t n){ __tryAlloc(n); }
|
||||||
__tryAlloc(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
Args(const Args& other){
|
Args(const Args& other){
|
||||||
__tryAlloc(other._size);
|
__tryAlloc(other._size);
|
||||||
@ -88,9 +85,7 @@ namespace pkpy {
|
|||||||
|
|
||||||
Args(PyVarList&& other) noexcept {
|
Args(PyVarList&& other) noexcept {
|
||||||
__tryAlloc(other.size());
|
__tryAlloc(other.size());
|
||||||
for(int i=0; i<_size; i++){
|
for(int i=0; i<_size; i++) _args[i] = std::move(other[i]);
|
||||||
_args[i] = std::move(other[i]);
|
|
||||||
}
|
|
||||||
other.clear();
|
other.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -124,16 +119,14 @@ namespace pkpy {
|
|||||||
|
|
||||||
memcpy((void*)(_args+1), (void*)old_args, sizeof(PyVar)*old_size);
|
memcpy((void*)(_args+1), (void*)old_args, sizeof(PyVar)*old_size);
|
||||||
memset((void*)old_args, 0, 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;
|
delete[] old_args;
|
||||||
}else{
|
}else{
|
||||||
_poolArgs[old_size].push_back(old_args);
|
_poolArgs[old_size].push_back(old_args);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
~Args(){
|
~Args(){ __tryRelease(); }
|
||||||
__tryRelease();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Args& noArg(){
|
const Args& noArg(){
|
||||||
|
9
src/vm.h
9
src/vm.h
@ -22,7 +22,6 @@
|
|||||||
|
|
||||||
class VM {
|
class VM {
|
||||||
std::vector<PyVar> _small_integers; // [-5, 256]
|
std::vector<PyVar> _small_integers; // [-5, 256]
|
||||||
protected:
|
|
||||||
std::stack< std::unique_ptr<Frame> > callstack;
|
std::stack< std::unique_ptr<Frame> > callstack;
|
||||||
PyVar __py2py_call_signal;
|
PyVar __py2py_call_signal;
|
||||||
|
|
||||||
@ -373,8 +372,8 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
PyVar asStr(const PyVar& obj){
|
PyVar asStr(const PyVar& obj){
|
||||||
PyVarOrNull str_fn = getattr(obj, __str__, false);
|
PyVarOrNull f = getattr(obj, __str__, false);
|
||||||
if(str_fn != nullptr) return call(str_fn);
|
if(f != nullptr) return call(f);
|
||||||
return asRepr(obj);
|
return asRepr(obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,10 +387,6 @@ public:
|
|||||||
return call(obj, __repr__);
|
return call(obj, __repr__);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyVar asJson(const PyVar& obj){
|
|
||||||
return call(obj, __json__);
|
|
||||||
}
|
|
||||||
|
|
||||||
const PyVar& asBool(const PyVar& obj){
|
const PyVar& asBool(const PyVar& obj){
|
||||||
if(obj->is_type(_tp_bool)) return obj;
|
if(obj->is_type(_tp_bool)) return obj;
|
||||||
if(obj == None) return False;
|
if(obj == None) return False;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user