diff --git a/src/main.cpp b/src/main.cpp index 286e869e..f8d55f83 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,7 +2,7 @@ #include "pocketpy.h" -#define PK_DEBUG_TIME +//#define PK_DEBUG_TIME struct Timer{ const char* title; diff --git a/src/pocketpy.h b/src/pocketpy.h index 489f03f2..a3cdbda9 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -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" diff --git a/src/safestl.h b/src/safestl.h index a65c8a36..313ad60d 100644 --- a/src/safestl.h +++ b/src/safestl.h @@ -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 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(); } diff --git a/tests/pointer.py b/tests/pointer.py index 89a00bb0..311f7783 100644 --- a/tests/pointer.py +++ b/tests/pointer.py @@ -21,4 +21,14 @@ f() a = [1, 2, 3] b = &a b->append(4) -assert a == [1, 2, 3, 4] \ No newline at end of file +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 \ No newline at end of file