From 189c4de2980beaeaefeb4592c1409f97cf2cf05a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 2 Jun 2024 22:42:40 +0800 Subject: [PATCH] some fix --- include/pocketpy/interpreter/cffi.hpp | 3 --- include/pocketpy/interpreter/vm.hpp | 2 +- include/pocketpy/modules/linalg.hpp | 1 - include/pocketpy/objects/builtins.hpp | 5 +++++ include/pocketpy/pocketpy.hpp | 14 ++++++++++++++ src/interpreter/vm.cpp | 12 ++++++------ src/pocketpy.cpp | 2 +- 7 files changed, 27 insertions(+), 12 deletions(-) diff --git a/include/pocketpy/interpreter/cffi.hpp b/include/pocketpy/interpreter/cffi.hpp index fd9e7456..f1023321 100644 --- a/include/pocketpy/interpreter/cffi.hpp +++ b/include/pocketpy/interpreter/cffi.hpp @@ -86,9 +86,6 @@ struct Struct{ static void _register(VM* vm, PyObject* mod, PyObject* type); }; -static_assert(py_sizeof <= 64); -static_assert(py_sizeof <= 128); - /***********************************************/ template Tp to_void_p(VM* vm, PyVar var){ diff --git a/include/pocketpy/interpreter/vm.hpp b/include/pocketpy/interpreter/vm.hpp index 14b4fc02..6889c2a8 100644 --- a/include/pocketpy/interpreter/vm.hpp +++ b/include/pocketpy/interpreter/vm.hpp @@ -227,7 +227,7 @@ public: PyVar py_iter(PyVar obj); // x -> iter(x) PyVar py_next(PyVar); // x -> next(x) 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 List py_list(PyVar); // x -> list(x) diff --git a/include/pocketpy/modules/linalg.hpp b/include/pocketpy/modules/linalg.hpp index 9973fd17..ee04cddf 100644 --- a/include/pocketpy/modules/linalg.hpp +++ b/include/pocketpy/modules/linalg.hpp @@ -125,7 +125,6 @@ struct Mat3x3{ void add_module_linalg(VM* vm); -static_assert(py_sizeof <= 64); static_assert(is_pod_v); static_assert(is_pod_v); static_assert(is_pod_v); diff --git a/include/pocketpy/objects/builtins.hpp b/include/pocketpy/objects/builtins.hpp index 361631e5..39f3fc22 100644 --- a/include/pocketpy/objects/builtins.hpp +++ b/include/pocketpy/objects/builtins.hpp @@ -75,6 +75,11 @@ inline bool is_type(PyVar obj, Type type) { return obj.type == type; } +inline bool is_type(PyObject* p, Type type) { + assert(p != nullptr); + return p->type == type; +} + struct MappingProxy{ PyObject* obj; MappingProxy(PyObject* obj) : obj(obj) {} diff --git a/include/pocketpy/pocketpy.hpp b/include/pocketpy/pocketpy.hpp index 13f937dd..c790c581 100644 --- a/include/pocketpy/pocketpy.hpp +++ b/include/pocketpy/pocketpy.hpp @@ -1,8 +1,22 @@ #pragma once +#include "pocketpy/common/traits.hpp" #include "pocketpy/objects/builtins.hpp" #include "pocketpy/interpreter/vm.hpp" #include "pocketpy/interpreter/iter.hpp" #include "pocketpy/interpreter/bindings.hpp" #include "pocketpy/compiler/compiler.hpp" +#include "pocketpy/modules/linalg.hpp" #include "pocketpy/tools/repl.hpp" + +namespace pkpy{ + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 80); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); + static_assert(py_sizeof <= 64); +} // namespace pkpy \ No newline at end of file diff --git a/src/interpreter/vm.cpp b/src/interpreter/vm.cpp index 7c1d9f61..331a57f8 100644 --- a/src/interpreter/vm.cpp +++ b/src/interpreter/vm.cpp @@ -331,7 +331,7 @@ namespace pkpy{ 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"); static auto f_join = [](const vector& cpnts){ SStream ss; @@ -367,7 +367,7 @@ namespace pkpy{ // check existing module StrName name(path); PyVar ext_mod = _modules.try_get(name); - if(ext_mod != nullptr) return ext_mod; + if(ext_mod != nullptr) return ext_mod.get(); vector path_cpnts = path.split('.'); // check circular import @@ -878,8 +878,8 @@ void VM::__init_builtin_types(){ _all_types.emplace_back(heap._new(tp_type, tp_object), Type(), nullptr, "object", true); _all_types.emplace_back(heap._new(tp_type, tp_type), tp_object, nullptr, "type", false); - auto validate = [](Type type, PyVar ret){ - Type ret_t = PK_OBJ_GET(Type, ret); + auto validate = [](Type type, PyObject* ret){ + Type ret_t = ret->as(); if(ret_t != type) exit(-3); }; @@ -915,8 +915,8 @@ void VM::__init_builtin_types(){ validate(tp_stack_memory, new_type_object(nullptr, "_stack_memory", tp_object, false)); // SyntaxError and IndentationError must be created here - PyVar SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true); - PyVar IndentationError = new_type_object(nullptr, "IndentationError", PK_OBJ_GET(Type, SyntaxError), true); + PyObject* SyntaxError = new_type_object(nullptr, "SyntaxError", tp_exception, true); + PyObject* IndentationError = new_type_object(nullptr, "IndentationError", SyntaxError->as(), true); this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true); this->builtins = new_module("builtins"); diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 445f9ed8..1059c7ec 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -194,7 +194,7 @@ void __init_builtins(VM* _vm) { 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]); return vm->py_import(name); });