blueloveTH 3 năm trước cách đây
mục cha
commit
4c4a7fb471
4 tập tin đã thay đổi với 38 bổ sung8 xóa
  1. 1 1
      src/main.cpp
  2. 11 0
      src/pocketpy.h
  3. 15 6
      src/safestl.h
  4. 11 1
      tests/pointer.py

+ 1 - 1
src/main.cpp

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

+ 11 - 0
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"

+ 15 - 6
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<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();
         }

+ 11 - 1
tests/pointer.py

@@ -21,4 +21,14 @@ f()
 a = [1, 2, 3]
 b = &a
 b->append(4)
-assert a == [1, 2, 3, 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