This commit is contained in:
blueloveTH 2024-06-02 22:42:40 +08:00
parent d8e72e1466
commit 189c4de298
7 changed files with 27 additions and 12 deletions

View File

@ -86,9 +86,6 @@ struct Struct{
static void _register(VM* vm, PyObject* mod, PyObject* type); static void _register(VM* vm, PyObject* mod, PyObject* type);
}; };
static_assert(py_sizeof<Struct> <= 64);
static_assert(py_sizeof<Tuple> <= 128);
/***********************************************/ /***********************************************/
template<typename Tp> template<typename Tp>
Tp to_void_p(VM* vm, PyVar var){ Tp to_void_p(VM* vm, PyVar var){

View File

@ -227,7 +227,7 @@ public:
PyVar py_iter(PyVar obj); // x -> iter(x) PyVar py_iter(PyVar obj); // x -> iter(x)
PyVar py_next(PyVar); // x -> next(x) PyVar py_next(PyVar); // x -> next(x)
PyVar _py_next(const PyTypeInfo*, PyVar); // x -> next(x) with type info cache PyVar _py_next(const PyTypeInfo*, PyVar); // x -> next(x) with type info cache
PyVar py_import(Str path, bool throw_err=true); // x -> __import__(x) PyObject* py_import(Str path, bool throw_err=true); // x -> __import__(x)
PyVar py_negate(PyVar obj); // x -> -x PyVar py_negate(PyVar obj); // x -> -x
List py_list(PyVar); // x -> list(x) List py_list(PyVar); // x -> list(x)

View File

@ -125,7 +125,6 @@ struct Mat3x3{
void add_module_linalg(VM* vm); void add_module_linalg(VM* vm);
static_assert(py_sizeof<Mat3x3> <= 64);
static_assert(is_pod_v<Vec2>); static_assert(is_pod_v<Vec2>);
static_assert(is_pod_v<Vec3>); static_assert(is_pod_v<Vec3>);
static_assert(is_pod_v<Vec4>); static_assert(is_pod_v<Vec4>);

View File

@ -75,6 +75,11 @@ inline bool is_type(PyVar obj, Type type) {
return obj.type == type; return obj.type == type;
} }
inline bool is_type(PyObject* p, Type type) {
assert(p != nullptr);
return p->type == type;
}
struct MappingProxy{ struct MappingProxy{
PyObject* obj; PyObject* obj;
MappingProxy(PyObject* obj) : obj(obj) {} MappingProxy(PyObject* obj) : obj(obj) {}

View File

@ -1,8 +1,22 @@
#pragma once #pragma once
#include "pocketpy/common/traits.hpp"
#include "pocketpy/objects/builtins.hpp" #include "pocketpy/objects/builtins.hpp"
#include "pocketpy/interpreter/vm.hpp" #include "pocketpy/interpreter/vm.hpp"
#include "pocketpy/interpreter/iter.hpp" #include "pocketpy/interpreter/iter.hpp"
#include "pocketpy/interpreter/bindings.hpp" #include "pocketpy/interpreter/bindings.hpp"
#include "pocketpy/compiler/compiler.hpp" #include "pocketpy/compiler/compiler.hpp"
#include "pocketpy/modules/linalg.hpp"
#include "pocketpy/tools/repl.hpp" #include "pocketpy/tools/repl.hpp"
namespace pkpy{
static_assert(py_sizeof<Mat3x3> <= 64);
static_assert(py_sizeof<Struct> <= 64);
static_assert(py_sizeof<Tuple> <= 80);
static_assert(py_sizeof<RangeIter> <= 64);
static_assert(py_sizeof<RangeIterR> <= 64);
static_assert(py_sizeof<ArrayIter> <= 64);
static_assert(py_sizeof<StringIter> <= 64);
static_assert(py_sizeof<Generator> <= 64);
static_assert(py_sizeof<DictItemsIter> <= 64);
} // namespace pkpy

View File

@ -331,7 +331,7 @@ namespace pkpy{
return res; return res;
} }
PyVar VM::py_import(Str path, bool throw_err){ PyObject* VM::py_import(Str path, bool throw_err){
if(path.empty()) vm->ValueError("empty module name"); if(path.empty()) vm->ValueError("empty module name");
static auto f_join = [](const vector<std::string_view>& cpnts){ static auto f_join = [](const vector<std::string_view>& cpnts){
SStream ss; SStream ss;
@ -367,7 +367,7 @@ namespace pkpy{
// check existing module // check existing module
StrName name(path); StrName name(path);
PyVar ext_mod = _modules.try_get(name); PyVar ext_mod = _modules.try_get(name);
if(ext_mod != nullptr) return ext_mod; if(ext_mod != nullptr) return ext_mod.get();
vector<std::string_view> path_cpnts = path.split('.'); vector<std::string_view> path_cpnts = path.split('.');
// check circular import // check circular import
@ -878,8 +878,8 @@ void VM::__init_builtin_types(){
_all_types.emplace_back(heap._new<Type>(tp_type, tp_object), Type(), nullptr, "object", true); _all_types.emplace_back(heap._new<Type>(tp_type, tp_object), Type(), nullptr, "object", true);
_all_types.emplace_back(heap._new<Type>(tp_type, tp_type), tp_object, nullptr, "type", false); _all_types.emplace_back(heap._new<Type>(tp_type, tp_type), tp_object, nullptr, "type", false);
auto validate = [](Type type, PyVar ret){ auto validate = [](Type type, PyObject* ret){
Type ret_t = PK_OBJ_GET(Type, ret); Type ret_t = ret->as<Type>();
if(ret_t != type) exit(-3); if(ret_t != type) exit(-3);
}; };
@ -915,8 +915,8 @@ void VM::__init_builtin_types(){
validate(tp_stack_memory, new_type_object<StackMemory>(nullptr, "_stack_memory", tp_object, false)); validate(tp_stack_memory, new_type_object<StackMemory>(nullptr, "_stack_memory", tp_object, false));
// SyntaxError and IndentationError must be created here // SyntaxError and IndentationError must be created here
PyVar SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true); PyObject* SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true);
PyVar IndentationError = new_type_object(nullptr, "IndentationError", PK_OBJ_GET(Type, SyntaxError), true); PyObject* IndentationError = new_type_object(nullptr, "IndentationError", SyntaxError->as<Type>(), true);
this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true); this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true);
this->builtins = new_module("builtins"); this->builtins = new_module("builtins");

View File

@ -194,7 +194,7 @@ void __init_builtins(VM* _vm) {
return VAR(vm->py_callable(args[0])); return VAR(vm->py_callable(args[0]));
}); });
_vm->bind_func(_vm->builtins, "__import__", 1, [](VM* vm, ArgsView args) { _vm->bind_func(_vm->builtins, "__import__", 1, [](VM* vm, ArgsView args) -> PyVar{
const Str& name = CAST(Str&, args[0]); const Str& name = CAST(Str&, args[0]);
return vm->py_import(name); return vm->py_import(name);
}); });