mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 20:40:18 +00:00
more safe!!
This commit is contained in:
parent
833bef370a
commit
b13009dc8b
@ -2,7 +2,7 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
|
|||||||
OPCODES_TEXT = f.read()
|
OPCODES_TEXT = f.read()
|
||||||
|
|
||||||
pipeline = [
|
pipeline = [
|
||||||
["__stl__.h", "str.h", "builtins.h", "error.h"],
|
["__stl__.h", "str.h", "safestl.h", "builtins.h", "error.h"],
|
||||||
["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"],
|
["obj.h", "iter.h", "parser.h", "pointer.h", "codeobject.h"],
|
||||||
["vm.h", "compiler.h", "repl.h"],
|
["vm.h", "compiler.h", "repl.h"],
|
||||||
["pocketpy.h"]
|
["pocketpy.h"]
|
||||||
|
@ -139,7 +139,7 @@ public:
|
|||||||
return code->src->snapshot(line);
|
return code->src->snapshot(line);
|
||||||
}
|
}
|
||||||
|
|
||||||
int stackSize() const {
|
inline int stackSize() const {
|
||||||
return s_data.size();
|
return s_data.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,6 +148,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline PyVar __pop(){
|
inline PyVar __pop(){
|
||||||
|
if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
|
||||||
PyVar v = s_data.back();
|
PyVar v = s_data.back();
|
||||||
s_data.pop_back();
|
s_data.pop_back();
|
||||||
return v;
|
return v;
|
||||||
@ -160,10 +161,11 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
inline PyVar topValue(VM* vm){
|
inline PyVar topValue(VM* vm){
|
||||||
|
if(s_data.empty()) throw std::runtime_error("s_data.empty() is true");
|
||||||
return __deref_pointer(vm, s_data.back());
|
return __deref_pointer(vm, s_data.back());
|
||||||
}
|
}
|
||||||
|
|
||||||
inline PyVar topNValue(VM* vm, int n=-1){
|
inline PyVar __topValueN(VM* vm, int n=-1){
|
||||||
return __deref_pointer(vm, s_data[s_data.size() + n]);
|
return __deref_pointer(vm, s_data[s_data.size() + n]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "__stl__.h"
|
#include "safestl.h"
|
||||||
#include "str.h"
|
|
||||||
|
|
||||||
class NeedMoreLines {
|
class NeedMoreLines {
|
||||||
public:
|
public:
|
||||||
|
34
src/obj.h
34
src/obj.h
@ -1,7 +1,6 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "__stl__.h"
|
#include "safestl.h"
|
||||||
#include "str.h"
|
|
||||||
|
|
||||||
typedef int64_t _Int;
|
typedef int64_t _Int;
|
||||||
typedef double _Float;
|
typedef double _Float;
|
||||||
@ -13,43 +12,12 @@ const _Float _FLOAT_INF_NEG = -INFINITY;
|
|||||||
|
|
||||||
#define PK_VERSION "0.2.3"
|
#define PK_VERSION "0.2.3"
|
||||||
|
|
||||||
class PyObject;
|
|
||||||
class CodeObject;
|
class CodeObject;
|
||||||
class BasePointer;
|
class BasePointer;
|
||||||
class VM;
|
class VM;
|
||||||
|
|
||||||
class PkExportedResource {};
|
class PkExportedResource {};
|
||||||
|
|
||||||
typedef std::shared_ptr<PyObject> PyVar;
|
|
||||||
typedef PyVar PyVarOrNull;
|
|
||||||
|
|
||||||
class PyVarList: public std::vector<PyVar> {
|
|
||||||
PyVar& at(size_t) = delete;
|
|
||||||
|
|
||||||
inline void __checkIndex(size_t i) const {
|
|
||||||
if (i >= size()){
|
|
||||||
auto msg = "std::vector index out of range, " + std::to_string(i) + " not in [0, " + std::to_string(size()) + ")";
|
|
||||||
throw std::out_of_range(msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
public:
|
|
||||||
PyVar& operator[](size_t i) {
|
|
||||||
__checkIndex(i);
|
|
||||||
return std::vector<PyVar>::operator[](i);
|
|
||||||
}
|
|
||||||
|
|
||||||
const PyVar& operator[](size_t i) const {
|
|
||||||
__checkIndex(i);
|
|
||||||
return std::vector<PyVar>::operator[](i);
|
|
||||||
}
|
|
||||||
|
|
||||||
// define constructors the same as std::vector
|
|
||||||
using std::vector<PyVar>::vector;
|
|
||||||
};
|
|
||||||
|
|
||||||
typedef std::unordered_map<_Str, PyVar> PyVarDict;
|
|
||||||
typedef std::shared_ptr<const BasePointer> _Pointer;
|
typedef std::shared_ptr<const BasePointer> _Pointer;
|
||||||
|
|
||||||
typedef PyVar (*_CppFunc)(VM*, PyVarList);
|
typedef PyVar (*_CppFunc)(VM*, PyVarList);
|
||||||
typedef std::shared_ptr<CodeObject> _Code;
|
typedef std::shared_ptr<CodeObject> _Code;
|
||||||
|
|
||||||
|
54
src/safestl.h
Normal file
54
src/safestl.h
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "__stl__.h"
|
||||||
|
#include "str.h"
|
||||||
|
|
||||||
|
class PyObject;
|
||||||
|
typedef std::shared_ptr<PyObject> PyVar;
|
||||||
|
typedef PyVar PyVarOrNull;
|
||||||
|
|
||||||
|
class PyVarList: public std::vector<PyVar> {
|
||||||
|
PyVar& at(size_t) = delete;
|
||||||
|
|
||||||
|
inline void __checkIndex(size_t i) const {
|
||||||
|
if (i >= size()){
|
||||||
|
auto msg = "std::vector index out of range, " + std::to_string(i) + " not in [0, " + std::to_string(size()) + ")";
|
||||||
|
throw std::out_of_range(msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public:
|
||||||
|
PyVar& operator[](size_t i) {
|
||||||
|
__checkIndex(i);
|
||||||
|
return std::vector<PyVar>::operator[](i);
|
||||||
|
}
|
||||||
|
|
||||||
|
const PyVar& operator[](size_t i) const {
|
||||||
|
__checkIndex(i);
|
||||||
|
return std::vector<PyVar>::operator[](i);
|
||||||
|
}
|
||||||
|
|
||||||
|
// define constructors the same as std::vector
|
||||||
|
using std::vector<PyVar>::vector;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
class PyVarDict: public std::unordered_map<_Str, PyVar> {
|
||||||
|
PyVar& at(const _Str&) = delete;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PyVar& operator[](const _Str& key) {
|
||||||
|
return std::unordered_map<_Str, PyVar>::operator[](key);
|
||||||
|
}
|
||||||
|
|
||||||
|
const PyVar& operator[](const _Str& key) const {
|
||||||
|
auto it = find(key);
|
||||||
|
if (it == end()){
|
||||||
|
auto msg = "std::unordered_map key not found, '" + key.str() + "'";
|
||||||
|
throw std::out_of_range(msg);
|
||||||
|
}
|
||||||
|
return it->second;
|
||||||
|
}
|
||||||
|
|
||||||
|
// define constructors the same as std::unordered_map
|
||||||
|
using std::unordered_map<_Str, PyVar>::unordered_map;
|
||||||
|
};
|
2
src/vm.h
2
src/vm.h
@ -124,7 +124,7 @@ private:
|
|||||||
} break;
|
} break;
|
||||||
case OP_LIST_APPEND: {
|
case OP_LIST_APPEND: {
|
||||||
PyVar obj = frame->popValue(this);
|
PyVar obj = frame->popValue(this);
|
||||||
PyVar list = frame->topNValue(this, -2);
|
PyVar list = frame->__topValueN(this, -2);
|
||||||
fastCall(list, "append", {list, obj});
|
fastCall(list, "append", {list, obj});
|
||||||
} break;
|
} break;
|
||||||
case OP_STORE_FUNCTION:
|
case OP_STORE_FUNCTION:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user