From 3c480cee116d0d173957edc635f279fa4bda598a Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 4 May 2024 13:25:30 +0800 Subject: [PATCH] remove old `bind_func` --- 3rd/cjson/src/cJSONw.cpp | 10 ++---- src/pocketpy.cpp | 68 ++++++++++++++++++++-------------------- src/vm.cpp | 20 ++++++------ 3 files changed, 45 insertions(+), 53 deletions(-) diff --git a/3rd/cjson/src/cJSONw.cpp b/3rd/cjson/src/cJSONw.cpp index e37e47ec..c829cf17 100644 --- a/3rd/cjson/src/cJSONw.cpp +++ b/3rd/cjson/src/cJSONw.cpp @@ -98,7 +98,7 @@ void add_module_cjson(VM* vm){ hooks.free_fn = pool64_dealloc; cJSON_InitHooks(&hooks); - vm->bind_func<1>(mod, "loads", [](VM* vm, ArgsView args){ + vm->bind_func(mod, "loads", 1, [](VM* vm, ArgsView args){ std::string_view sv; if(is_type(args[0], vm->tp_bytes)){ sv = PK_OBJ_GET(Bytes, args[0]).sv(); @@ -117,14 +117,8 @@ void add_module_cjson(VM* vm){ return output; }); - vm->bind_func<1>(mod, "dumps", [](VM* vm, ArgsView args) { + vm->bind_func(mod, "dumps", 1, [](VM* vm, ArgsView args) { return vm->py_json(args[0]); - // cJSON* cjson = convert_python_object_to_cjson(args[0], vm); - // char* str = cJSON_Print(cjson); - // cJSON_Delete(cjson); - // PyObject* ret = VAR((const char*)str); - // hooks.free_fn(str); - // return ret; }); } diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index c38a6ff5..134dce67 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -467,7 +467,7 @@ void init_builtins(VM* _vm) { return VAR(_CAST(i64, _0) % rhs); }); - _vm->bind_method<0>(VM::tp_int, "bit_length", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_int, "bit_length", 1, [](VM* vm, ArgsView args) { i64 x = _CAST(i64, args[0]); if(x < 0) x = -x; int bits = 0; @@ -563,7 +563,7 @@ void init_builtins(VM* _vm) { for(i64 i = 0; i < n; i++) ss << self.sv(); return VAR(ss.str()); }); - _vm->bind_method<1>(VM::tp_str, "__rmul__", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "__rmul__", 2, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); i64 n = CAST(i64, args[1]); SStream ss; @@ -664,13 +664,13 @@ void init_builtins(VM* _vm) { return VAR(self.index(value, start)); }); - _vm->bind_method<1>(VM::tp_str, "startswith", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "startswith", 2, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); const Str& prefix = CAST(Str&, args[1]); return VAR(self.index(prefix) == 0); }); - _vm->bind_method<1>(VM::tp_str, "endswith", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "endswith", 2, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); const Str& suffix = CAST(Str&, args[1]); int offset = self.length() - suffix.length(); @@ -679,14 +679,14 @@ void init_builtins(VM* _vm) { return VAR(ok); }); - _vm->bind_method<0>(VM::tp_str, "encode", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "encode", 1, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); unsigned char* buffer = new unsigned char[self.length()]; memcpy(buffer, self.data, self.length()); return VAR(Bytes(buffer, self.length())); }); - _vm->bind_method<1>(VM::tp_str, "join", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "join", 2, [](VM* vm, ArgsView args) { auto _lock = vm->heap.gc_scope_lock(); const Str& self = _CAST(Str&, args[0]); SStream ss; @@ -701,12 +701,12 @@ void init_builtins(VM* _vm) { return VAR(ss.str()); }); - _vm->bind_method<0>(VM::tp_str, "lower", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "lower", 1, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); return VAR(self.lower()); }); - _vm->bind_method<0>(VM::tp_str, "upper", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_str, "upper", 1, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); return VAR(self.upper()); }); @@ -844,7 +844,7 @@ void init_builtins(VM* _vm) { return vm->False; }); - _vm->bind_method<1>(VM::tp_list, "count", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "count", 2, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); int count = 0; for(PyObject* i: self) if(vm->py_eq(i, args[1])) count++; @@ -873,7 +873,7 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<1>(VM::tp_list, "remove", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "remove", 2, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); PyObject* obj = args[1]; for(int i=0; iNone; }); - _vm->bind_method<-1>(VM::tp_list, "pop", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "pop", -1, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); if(args.size() == 1+0){ if(self.empty()) vm->IndexError("pop from empty list"); @@ -903,13 +903,13 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<1>(VM::tp_list, "append", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "append", 2, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); self.push_back(args[1]); return vm->None; }); - _vm->bind_method<1>(VM::tp_list, "extend", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "extend", 2, [](VM* vm, ArgsView args) { auto _lock = vm->heap.gc_scope_lock(); List& self = _CAST(List&, args[0]); PyObject* it = vm->py_iter(args[1]); // strong ref @@ -922,7 +922,7 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<0>(VM::tp_list, "reverse", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "reverse", 1, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); std::reverse(self.begin(), self.end()); return vm->None; @@ -937,7 +937,7 @@ void init_builtins(VM* _vm) { for(int i = 0; i < n; i++) result.extend(self); return VAR(std::move(result)); }); - _vm->bind_method<1>(VM::tp_list, "__rmul__", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "__rmul__", 2, [](VM* vm, ArgsView args) { const List& self = _CAST(List&, args[0]); if(!is_int(args[1])) return vm->NotImplemented; int n = _CAST(int, args[1]); @@ -947,7 +947,7 @@ void init_builtins(VM* _vm) { return VAR(std::move(result)); }); - _vm->bind_method<2>(VM::tp_list, "insert", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "insert", 3, [](VM* vm, ArgsView args) { List& self = _CAST(List&, args[0]); int index = CAST(int, args[1]); if(index < 0) index += self.size(); @@ -957,12 +957,12 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<0>(VM::tp_list, "clear", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_list, "clear", 1, [](VM* vm, ArgsView args) { _CAST(List&, args[0]).clear(); return vm->None; }); - _vm->bind_method<0>(VM::tp_list, "copy", PK_LAMBDA(VAR(_CAST(List, args[0])))); + _vm->bind_func(VM::tp_list, "copy", 1, PK_LAMBDA(VAR(_CAST(List, args[0])))); #define BIND_RICH_CMP(name, op, _t, _T) \ _vm->bind__##name##__(_vm->_t, [](VM* vm, PyObject* lhs, PyObject* rhs){ \ @@ -1033,7 +1033,7 @@ void init_builtins(VM* _vm) { return vm->False; }); - _vm->bind_method<1>(VM::tp_tuple, "count", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_tuple, "count", 2, [](VM* vm, ArgsView args) { Tuple& self = _CAST(Tuple&, args[0]); int count = 0; for(PyObject* i: self) if(vm->py_eq(i, args[1])) count++; @@ -1163,7 +1163,7 @@ void init_builtins(VM* _vm) { return (i64)_CAST(Bytes&, _0).size(); }); - _vm->bind_method<0>(VM::tp_bytes, "decode", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_bytes, "decode", 1, [](VM* vm, ArgsView args) { const Bytes& self = _CAST(Bytes&, args[0]); // TODO: check encoding is utf-8 return VAR(Str(self.str())); @@ -1200,21 +1200,21 @@ void init_builtins(VM* _vm) { }); // tp_mappingproxy - _vm->bind_method<0>(VM::tp_mappingproxy, "keys", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_mappingproxy, "keys", 1, [](VM* vm, ArgsView args) { MappingProxy& self = _CAST(MappingProxy&, args[0]); List keys; for(StrName name : self.attr().keys()) keys.push_back(VAR(name.sv())); return VAR(std::move(keys)); }); - _vm->bind_method<0>(VM::tp_mappingproxy, "values", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_mappingproxy, "values", 1, [](VM* vm, ArgsView args) { MappingProxy& self = _CAST(MappingProxy&, args[0]); List values; for(auto [k, v] : self.attr().items()) values.push_back(v); return VAR(std::move(values)); }); - _vm->bind_method<0>(VM::tp_mappingproxy, "items", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_mappingproxy, "items", 1, [](VM* vm, ArgsView args) { MappingProxy& self = _CAST(MappingProxy&, args[0]); List items; for(auto [k, v] : self.attr().items()){ @@ -1280,7 +1280,7 @@ void init_builtins(VM* _vm) { return vm->heap.gcnew(cls_t, vm); }); - _vm->bind_method<-1>(VM::tp_dict, __init__, [](VM* vm, ArgsView args){ + _vm->bind_func(VM::tp_dict, __init__, -1, [](VM* vm, ArgsView args){ if(args.size() == 1+0) return vm->None; if(args.size() == 1+1){ auto _lock = vm->heap.gc_scope_lock(); @@ -1338,7 +1338,7 @@ void init_builtins(VM* _vm) { if(!ok) vm->KeyError(_1); }); - _vm->bind_method<-1>(VM::tp_dict, "pop", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "pop", -1, [](VM* vm, ArgsView args) { if(args.size() != 2 && args.size() != 3){ vm->TypeError("pop() expected 1 or 2 arguments"); return vm->None; @@ -1365,7 +1365,7 @@ void init_builtins(VM* _vm) { return vm->py_iter(VAR(self.keys())); }); - _vm->bind_method<-1>(VM::tp_dict, "get", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "get", -1, [](VM* vm, ArgsView args) { Dict& self = _CAST(Dict&, args[0]); if(args.size() == 1+1){ PyObject* ret = self.try_get(args[1]); @@ -1380,33 +1380,33 @@ void init_builtins(VM* _vm) { return vm->None; }); - _vm->bind_method<0>(VM::tp_dict, "keys", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "keys", 1, [](VM* vm, ArgsView args) { const Dict& self = _CAST(Dict&, args[0]); return VAR(self.keys()); }); - _vm->bind_method<0>(VM::tp_dict, "values", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "values", 1, [](VM* vm, ArgsView args) { const Dict& self = _CAST(Dict&, args[0]); return VAR(self.values()); }); - _vm->bind_method<0>(VM::tp_dict, "items", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "items", 1, [](VM* vm, ArgsView args) { return vm->new_user_object(args[0]); }); - _vm->bind_method<1>(VM::tp_dict, "update", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "update", 2, [](VM* vm, ArgsView args) { Dict& self = _CAST(Dict&, args[0]); const Dict& other = CAST(Dict&, args[1]); self.update(other); return vm->None; }); - _vm->bind_method<0>(VM::tp_dict, "copy", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "copy", 1, [](VM* vm, ArgsView args) { const Dict& self = _CAST(Dict&, args[0]); return VAR(self); }); - _vm->bind_method<0>(VM::tp_dict, "clear", [](VM* vm, ArgsView args) { + _vm->bind_func(VM::tp_dict, "clear", 1, [](VM* vm, ArgsView args) { Dict& self = _CAST(Dict&, args[0]); self.clear(); return vm->None; @@ -1513,7 +1513,7 @@ void init_builtins(VM* _vm) { void VM::__post_init_builtin_types(){ init_builtins(this); - bind_method<-1>(tp_module, "__init__", [](VM* vm, ArgsView args) { + bind_func(tp_module, __init__, -1, [](VM* vm, ArgsView args) { vm->NotImplementedError(); return vm->None; }); @@ -1523,7 +1523,7 @@ void VM::__post_init_builtin_types(){ return vm->py_import(_S(path, ".", name.sv()), false); }; - bind_method<1>(tp_property, "setter", [](VM* vm, ArgsView args) { + bind_func(tp_property, "setter", 2, [](VM* vm, ArgsView args) { Property& self = _CAST(Property&, args[0]); // The setter's name is not necessary to be the same as the property's name // However, for cpython compatibility, we recommend to use the same name diff --git a/src/vm.cpp b/src/vm.cpp index c94ea579..42439e27 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -1351,7 +1351,7 @@ void _gc_mark_namedict(NameDict* t){ void VM::bind__getitem__(Type type, PyObject* (*f)(VM*, PyObject*, PyObject*)){ _all_types[type].m__getitem__ = f; - PyObject* nf = bind_method<1>(type, __getitem__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __getitem__, 2, [](VM* vm, ArgsView args){ return lambda_get_userdata(args.begin())(vm, args[0], args[1]); }); PK_OBJ_GET(NativeFunc, nf).set_userdata(f); @@ -1359,7 +1359,7 @@ void VM::bind__getitem__(Type type, PyObject* (*f)(VM*, PyObject*, PyObject*)){ void VM::bind__setitem__(Type type, void (*f)(VM*, PyObject*, PyObject*, PyObject*)){ _all_types[type].m__setitem__ = f; - PyObject* nf = bind_method<2>(type, __setitem__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __setitem__, 3, [](VM* vm, ArgsView args){ lambda_get_userdata(args.begin())(vm, args[0], args[1], args[2]); return vm->None; }); @@ -1368,7 +1368,7 @@ void VM::bind__setitem__(Type type, void (*f)(VM*, PyObject*, PyObject*, PyObjec void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){ _all_types[type].m__delitem__ = f; - PyObject* nf = bind_method<1>(type, __delitem__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __delitem__, 2, [](VM* vm, ArgsView args){ lambda_get_userdata(args.begin())(vm, args[0], args[1]); return vm->None; }); @@ -1386,7 +1386,7 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){ void VM::bind__next__(Type type, unsigned (*f)(VM*, PyObject*)){ \ _all_types[type].m__next__ = f; \ - PyObject* nf = bind_method<0>(_t(type), __next__, [](VM* vm, ArgsView args){ \ + PyObject* nf = bind_func(type, __next__, 1, [](VM* vm, ArgsView args){ \ int n = lambda_get_userdata(args.begin())(vm, args[0]);\ return vm->__pack_next_retval(n); \ }); \ @@ -1394,7 +1394,7 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){ } void VM::bind__next__(Type type, PyObject* (*f)(VM*, PyObject*)){ - PyObject* nf = bind_method<0>(_t(type), __next__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __next__, 1, [](VM* vm, ArgsView args){ auto f = lambda_get_userdata(args.begin()); return f(vm, args[0]); }); @@ -1404,7 +1404,7 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){ #define BIND_UNARY_SPECIAL(name) \ void VM::bind##name(Type type, PyObject* (*f)(VM*, PyObject*)){ \ _all_types[type].m##name = f; \ - PyObject* nf = bind_method<0>(_t(type), name, [](VM* vm, ArgsView args){ \ + PyObject* nf = bind_func(type, name, 1, [](VM* vm, ArgsView args){ \ return lambda_get_userdata(args.begin())(vm, args[0]);\ }); \ PK_OBJ_GET(NativeFunc, nf).set_userdata(f); \ @@ -1418,9 +1418,8 @@ void VM::bind__delitem__(Type type, void (*f)(VM*, PyObject*, PyObject*)){ #undef BIND_UNARY_SPECIAL void VM::bind__hash__(Type type, i64 (*f)(VM*, PyObject*)){ - PyObject* obj = _t(type); _all_types[type].m__hash__ = f; - PyObject* nf = bind_method<0>(obj, __hash__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __hash__, 1, [](VM* vm, ArgsView args){ i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(ret); }); @@ -1428,9 +1427,8 @@ void VM::bind__hash__(Type type, i64 (*f)(VM*, PyObject*)){ } void VM::bind__len__(Type type, i64 (*f)(VM*, PyObject*)){ - PyObject* obj = _t(type); _all_types[type].m__len__ = f; - PyObject* nf = bind_method<0>(obj, __len__, [](VM* vm, ArgsView args){ + PyObject* nf = bind_func(type, __len__, 1, [](VM* vm, ArgsView args){ i64 ret = lambda_get_userdata(args.begin())(vm, args[0]); return VAR(ret); }); @@ -1441,7 +1439,7 @@ void VM::bind__len__(Type type, i64 (*f)(VM*, PyObject*)){ #define BIND_BINARY_SPECIAL(name) \ void VM::bind##name(Type type, BinaryFuncC f){ \ _all_types[type].m##name = f; \ - PyObject* nf = bind_method<1>(type, name, [](VM* vm, ArgsView args){ \ + PyObject* nf = bind_func(type, name, 2, [](VM* vm, ArgsView args){ \ return lambda_get_userdata(args.begin())(vm, args[0], args[1]);\ }); \ PK_OBJ_GET(NativeFunc, nf).set_userdata(f); \