Rollback vm.hpp

This commit is contained in:
ykiko 2024-06-04 19:48:38 +08:00
parent 1adbc0d036
commit 8ba1a9d245
2 changed files with 105 additions and 174 deletions

View File

@ -98,6 +98,6 @@ QualifierAlignment: Custom
QualifierOrder: ["constexpr", "const", "inline", "static", "type"]
SortIncludes: Never
SortUsingDeclarations: LexicographicNumeric
IncludeBlocksStyle: Merge
IncludeBlocks: Merge
WhitespaceSensitiveMacros: ["PK_PROTECTED"]

View File

@ -1,13 +1,13 @@
#pragma once
#include "pocketpy/objects/object.hpp"
#include "pocketpy/interpreter/frame.hpp"
#include "pocketpy/interpreter/gc.hpp"
#include "pocketpy/interpreter/profiler.hpp"
#include "pocketpy/objects/builtins.hpp"
#include "pocketpy/objects/dict.hpp"
#include "pocketpy/objects/error.hpp"
#include "pocketpy/objects/object.hpp"
#include "pocketpy/objects/stackmemory.hpp"
#include "pocketpy/objects/builtins.hpp"
#include "pocketpy/interpreter/gc.hpp"
#include "pocketpy/interpreter/frame.hpp"
#include "pocketpy/interpreter/profiler.hpp"
#include <stdexcept>
@ -240,12 +240,10 @@ public:
List py_list(PyVar); // x -> list(x)
bool py_callable(PyVar obj); // x -> callable(x)
bool py_bool(PyVar obj){ // x -> bool(x)
if(obj.type == tp_bool) { return obj._0; }
if(obj.type == tp_bool) return obj._0;
return __py_bool_non_trivial(obj);
}
i64 py_hash(PyVar obj); // x -> hash(x)
bool py_eq(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs == rhs
@ -253,7 +251,6 @@ public:
bool py_le(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs <= rhs
bool py_gt(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs > rhs
bool py_ge(PyVar lhs, PyVar rhs); // (lhs, rhs) -> lhs >= rhs
bool py_ne(PyVar lhs, PyVar rhs){ // (lhs, rhs) -> lhs != rhs
return !py_eq(lhs, rhs);
}
@ -270,14 +267,8 @@ public:
i64 normalized_index(i64 index, int size);
Str disassemble(CodeObject_ co);
void parse_int_slice(const Slice& s, int length, int& start, int& stop, int& step);
void obj_gc_mark(PyVar obj) {
if(obj.is_ptr) { __obj_gc_mark(obj.get()); }
}
void obj_gc_mark(PyObject* p) {
if(p) { __obj_gc_mark(p); }
}
void obj_gc_mark(PyVar obj) { if(obj.is_ptr) __obj_gc_mark(obj.get()); }
void obj_gc_mark(PyObject* p) { if(p) __obj_gc_mark(p); }
#endif
#if PK_REGION("Name Lookup Methods")
@ -289,8 +280,7 @@ public:
#endif
#if PK_REGION("Source Execution Methods")
CodeObject_
compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope = false);
CodeObject_ compile(std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope=false);
Str precompile(std::string_view source, const Str& filename, CompileMode mode);
PyVar exec(std::string_view source, Str filename, CompileMode mode, PyObject* _module=nullptr);
PyVar exec(std::string_view source);
@ -308,16 +298,14 @@ public:
template<typename... Args>
PyVar call(PyVar callable, Args&&... args){
PUSH(callable);
PUSH(PY_NULL);
PUSH(callable); PUSH(PY_NULL);
__push_varargs(args...);
return vectorcall(sizeof...(args));
}
template<typename... Args>
PyVar call_method(PyVar self, PyVar callable, Args&&... args){
PUSH(callable);
PUSH(self);
PUSH(callable); PUSH(self);
__push_varargs(args...);
return vectorcall(sizeof...(args));
}
@ -331,7 +319,6 @@ public:
#if PK_REGION("Logging Methods")
virtual void stdout_write(const Str& s){ _stdout(s.data, s.size); }
virtual void stderr_write(const Str& s){ _stderr(s.data, s.size); }
#endif
@ -375,22 +362,10 @@ public:
#endif
#if PK_REGION("General Bindings")
PyObject* bind_func(PyObject* obj,
StrName name,
int argc,
NativeFuncC fn,
any userdata = {},
BindType bt = BindType::DEFAULT);
PyObject* bind_func(Type type,
StrName name,
int argc,
NativeFuncC fn,
any userdata = {},
BindType bt = BindType::DEFAULT) {
PyObject* bind_func(PyObject* obj, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT);
PyObject* bind_func(Type type, StrName name, int argc, NativeFuncC fn, any userdata={}, BindType bt=BindType::DEFAULT){
return bind_func(_t(type), name, argc, fn, std::move(userdata), bt);
}
PyObject* bind_property(PyObject*, const char*, NativeFuncC fget, NativeFuncC fset=nullptr);
template<typename T, typename F, bool ReadOnly=false>
PyObject* bind_field(PyObject*, const char*, F T::*);
@ -401,8 +376,7 @@ public:
template<typename Ret, typename T, typename... Params>
PyObject* bind(PyObject*, const char*, Ret(T::*)(Params...), BindType bt=BindType::DEFAULT);
PyObject*
bind(PyObject*, const char*, const char*, NativeFuncC, any userdata = {}, BindType bt = BindType::DEFAULT);
PyObject* bind(PyObject*, const char*, const char*, NativeFuncC, any userdata={}, BindType bt=BindType::DEFAULT);
template<typename Ret, typename... Params>
PyObject* bind(PyObject*, const char*, const char*, Ret(*)(Params...), BindType bt=BindType::DEFAULT);
template<typename Ret, typename T, typename... Params>
@ -411,71 +385,37 @@ public:
#if PK_REGION("Error Reporting Methods")
[[noreturn]] void _error(PyVar);
[[noreturn]] void StackOverflowError() { __builtin_error("StackOverflowError"); }
[[noreturn]] void IOError(const Str& msg) { __builtin_error("IOError", msg); }
[[noreturn]] void NotImplementedError(){ __builtin_error("NotImplementedError"); }
[[noreturn]] void TypeError(const Str& msg){ __builtin_error("TypeError", msg); }
[[noreturn]] void TypeError(Type expected, Type actual) {
TypeError("expected " + _type_name(vm, expected).escape() + ", got " + _type_name(vm, actual).escape());
}
[[noreturn]] void TypeError(Type expected, Type actual) { TypeError("expected " + _type_name(vm, expected).escape() + ", got " + _type_name(vm, actual).escape()); }
[[noreturn]] void IndexError(const Str& msg){ __builtin_error("IndexError", msg); }
[[noreturn]] void ValueError(const Str& msg){ __builtin_error("ValueError", msg); }
[[noreturn]] void RuntimeError(const Str& msg){ __builtin_error("RuntimeError", msg); }
[[noreturn]] void ZeroDivisionError(const Str& msg){ __builtin_error("ZeroDivisionError", msg); }
[[noreturn]] void ZeroDivisionError(){ __builtin_error("ZeroDivisionError", "division by zero"); }
[[noreturn]] void NameError(StrName name) {
__builtin_error("NameError", _S("name ", name.escape() + " is not defined"));
}
[[noreturn]] void UnboundLocalError(StrName name) {
__builtin_error("UnboundLocalError", _S("local variable ", name.escape() + " referenced before assignment"));
}
[[noreturn]] void NameError(StrName name){ __builtin_error("NameError", _S("name ", name.escape() + " is not defined")); }
[[noreturn]] void UnboundLocalError(StrName name){ __builtin_error("UnboundLocalError", _S("local variable ", name.escape() + " referenced before assignment")); }
[[noreturn]] void KeyError(PyVar obj){ __builtin_error("KeyError", obj); }
[[noreturn]] void ImportError(const Str& msg){ __builtin_error("ImportError", msg); }
[[noreturn]] void AssertionError(const Str& msg){ __builtin_error("AssertionError", msg); }
[[noreturn]] void AssertionError(){ __builtin_error("AssertionError"); }
[[noreturn]] void BinaryOptError(const char* op, PyVar _0, PyVar _1);
[[noreturn]] void AttributeError(PyVar obj, StrName name);
[[noreturn]] void AttributeError(const Str& msg){ __builtin_error("AttributeError", msg); }
#endif
#if PK_REGION("Type Checking Methods")
bool isinstance(PyVar obj, Type base);
bool issubclass(Type cls, Type base);
void check_type(PyVar obj, Type type) {
if(!is_type(obj, type)) { TypeError(type, _tp(obj)); }
}
void check_compatible_type(PyVar obj, Type type) {
if(!isinstance(obj, type)) { TypeError(type, _tp(obj)); }
}
void check_type(PyVar obj, Type type){ if(!is_type(obj, type)) TypeError(type, _tp(obj)); }
void check_compatible_type(PyVar obj, Type type){ if(!isinstance(obj, type)) TypeError(type, _tp(obj)); }
Type _tp(PyVar obj){ return obj.type; }
const PyTypeInfo* _tp_info(PyVar obj) { return &_all_types[_tp(obj)]; }
const PyTypeInfo* _tp_info(Type type) { return &_all_types[type]; }
PyObject* _t(PyVar obj){ return _all_types[_tp(obj)].obj; }
PyObject* _t(Type type){ return _all_types[type].obj; }
// equivalent to `obj == NotImplemented` but faster
@ -492,18 +432,12 @@ public:
}
template<typename T>
Type _tp_user() {
return _find_type_in_cxx_typeid_map<T>();
}
Type _tp_user(){ return _find_type_in_cxx_typeid_map<T>(); }
template<typename T>
bool is_user_type(PyVar obj){ return _tp(obj) == _tp_user<T>(); }
template<typename T>
bool is_user_type(PyVar obj) {
return _tp(obj) == _tp_user<T>();
}
template <typename T>
PyObject*
register_user_class(PyObject*, StrName, RegisterFunc, Type base = tp_object, bool subclass_enabled = false);
PyObject* register_user_class(PyObject*, StrName, RegisterFunc, Type base=tp_object, bool subclass_enabled=false);
template<typename T>
PyObject* register_user_class(PyObject*, StrName, Type base=tp_object, bool subclass_enabled=false);
@ -514,11 +448,8 @@ public:
template<typename T, typename ...Args>
PyVar new_object(Type type, Args&&... args){
if constexpr(is_sso_v<T>) {
return PyVar(type, T(std::forward<Args>(args)...));
} else {
return heap.gcnew<T>(type, std::forward<Args>(args)...);
}
if constexpr(is_sso_v<T>) return PyVar(type, T(std::forward<Args>(args)...));
else return heap.gcnew<T>(type, std::forward<Args>(args)...);
}
template<typename T, typename ...Args>