blueloveTH 1 год назад
Родитель
Сommit
189c4de298

+ 0 - 3
include/pocketpy/interpreter/cffi.hpp

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

+ 1 - 1
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)

+ 0 - 1
include/pocketpy/modules/linalg.hpp

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

+ 5 - 0
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) {}

+ 14 - 0
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<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

+ 6 - 6
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<std::string_view>& 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<std::string_view> path_cpnts = path.split('.');
         // 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_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<Type>();
         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));
 
     // 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<Type>(), true);
     this->StopIteration = new_type_object(nullptr, "StopIteration", tp_exception, true);
 
     this->builtins = new_module("builtins");

+ 1 - 1
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);
     });