This commit is contained in:
blueloveTH 2022-11-21 20:03:45 +08:00
parent 710748f58d
commit 4c4a7fb471
4 changed files with 38 additions and 8 deletions

View File

@ -2,7 +2,7 @@
#include "pocketpy.h"
#define PK_DEBUG_TIME
//#define PK_DEBUG_TIME
struct Timer{
const char* title;

View File

@ -554,6 +554,17 @@ void __initializeBuiltinFunctions(VM* _vm) {
_vm->bindMethod("ellipsis", "__repr__", [](VM* vm, const pkpy::ArgList& args) {
return vm->PyStr("Ellipsis");
});
_vm->bindMethod("_native_function", "__call__", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkType(args[0], vm->_tp_native_function);
const _CppFunc& _self = vm->PyNativeFunction_AS_C(args[0]);
return _self(vm, args.subList(1));
});
_vm->bindMethod("function", "__call__", [](VM* vm, const pkpy::ArgList& args) {
vm->__checkType(args[0], vm->_tp_function);
return vm->call(args[0], args.subList(1));
});
}
#include "builtins.h"

View File

@ -86,19 +86,19 @@ namespace pkpy {
if(_size >= MAX_POOLING_N || _poolArgList[_size].size() > 32){
delete[] _args;
}else{
for(int i = 0; i < _size; i++) _args[i].reset();
for(uint8_t i = 0; i < _size; i++) _args[i].reset();
_poolArgList[_size].push_back(_args);
}
}
public:
ArgList(int n){
__tryAlloc(n);
ArgList(uint8_t n){
if(n != 0) __tryAlloc(n);
}
ArgList(const ArgList& other){
__tryAlloc(other._size);
for(int i=0; i<_size; i++){
for(uint8_t i=0; i<_size; i++){
_args[i] = other._args[i];
}
}
@ -112,7 +112,7 @@ namespace pkpy {
ArgList(PyVarList&& other){
__tryAlloc(other.size());
for(int i=0; i<_size; i++){
for(uint8_t i=0; i<_size; i++){
_args[i] = std::move(other[i]);
}
other.clear();
@ -121,7 +121,7 @@ namespace pkpy {
// deprecated, this is very slow, do not use it!!!
ArgList(std::initializer_list<PyVar> args){
__tryAlloc(args.size());
int i = 0;
uint8_t i = 0;
for(auto& arg: args) this->_args[i++] = arg;
}
@ -151,6 +151,15 @@ namespace pkpy {
return _size;
}
ArgList subList(uint8_t start) const {
if(start >= _size) return ArgList(0);
ArgList ret(_size - start);
for(uint8_t i=start; i<_size; i++){
ret[i-start] = _args[i];
}
return ret;
}
~ArgList(){
__tryRelease();
}

View File

@ -22,3 +22,13 @@ a = [1, 2, 3]
b = &a
b->append(4)
assert a == [1, 2, 3, 4]
def add(a, b):
return a+b
p = &add
assert p->__call__(1, 2) == 3
fun = lambda :6
p = &fun
assert p->__call__() == 6