Initial integration of deque in collections

This commit is contained in:
S. Mahmudul Hasan 2023-10-07 04:17:57 -04:00
parent 7312afdad2
commit af4f5ac10f
4 changed files with 60 additions and 0 deletions

View File

@ -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

View File

@ -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);

33
src/collections.cpp Normal file
View File

@ -0,0 +1,33 @@
#include "pocketpy/collections.h"
namespace pkpy
{
void PyDeque::_register(VM *vm, PyObject *mod, PyObject *type)
{
vm->bind_default_constructor<PyDeque>(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

View File

@ -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);