diff --git a/include/pocketpy/collections.h b/include/pocketpy/collections.h new file mode 100644 index 00000000..255fe903 --- /dev/null +++ b/include/pocketpy/collections.h @@ -0,0 +1,24 @@ +#pragma once + +#include "obj.h" +#include "common.h" +#include "memory.h" +#include "str.h" +#include "cffi.h" + +namespace pkpy +{ + struct PyDeque + { + PY_CLASS(PyDeque, collections, deque); + + // some fields can be defined here + int len; + + PyDeque() = default; + void printHelloWorld(); + static void _register(VM *vm, PyObject *mod, PyObject *type); + }; + + void add_module_mycollections(VM *vm); +} // namespace pkpy \ No newline at end of file diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index a16e6454..26acf9a1 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -7,6 +7,7 @@ #include "base64.h" #include "cffi.h" #include "linalg.h" +#include "collections.h" #include "easing.h" #include "io.h" #include "vm.h" @@ -14,6 +15,7 @@ #include "random.h" #include "bindings.h" + namespace pkpy { void init_builtins(VM* _vm); diff --git a/src/collections.cpp b/src/collections.cpp new file mode 100644 index 00000000..7fa02824 --- /dev/null +++ b/src/collections.cpp @@ -0,0 +1,33 @@ +#include "pocketpy/collections.h" + +namespace pkpy +{ + void PyDeque::_register(VM *vm, PyObject *mod, PyObject *type) + { + vm->bind_default_constructor(type); + + vm->bind(type, "__len__(self) -> int", + [](VM *vm, ArgsView args){ + PyDeque& self = _CAST(PyDeque&, args[0]); + return VAR(self.len); + }); + + vm->bind(type, "printHelloWorld(self) -> None", + [](VM *vm, ArgsView args){ + PyDeque& self = _CAST(PyDeque&, args[0]); + self.printHelloWorld(); + return vm->None; + }); + } + + void PyDeque::printHelloWorld() + { + printf("Hello World!\n"); + } + + void add_module_mycollections(VM *vm) + { + PyObject *mycollections = vm->new_module("collections"); + PyDeque::register_class(vm, mycollections); + } +} // namespace pkpypkpy diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index c42ead36..086e5772 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1744,6 +1744,7 @@ void VM::post_init(){ add_module_linalg(this); add_module_easing(this); + add_module_mycollections(this); #ifdef PK_USE_BOX2D add_module_box2d(this);