mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 19:40:18 +00:00
...
This commit is contained in:
parent
36632d3c2e
commit
b1eb38c009
@ -112,7 +112,7 @@ struct DoubleLinkedList{
|
|||||||
|
|
||||||
int size() const { return _size; }
|
int size() const { return _size; }
|
||||||
|
|
||||||
void apply(std::function<void(T*)> func){
|
void apply(void (*func)(T*)){
|
||||||
LinkedListNode* p = head.next;
|
LinkedListNode* p = head.next;
|
||||||
while(p != &tail){
|
while(p != &tail){
|
||||||
LinkedListNode* next = p->next;
|
LinkedListNode* next = p->next;
|
||||||
|
@ -11,7 +11,10 @@ struct Frame;
|
|||||||
struct Function;
|
struct Function;
|
||||||
class VM;
|
class VM;
|
||||||
|
|
||||||
typedef std::function<PyObject*(VM*, ArgsView)> NativeFuncRaw;
|
typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
|
||||||
|
typedef std::function<PyObject*(VM*, ArgsView)> NativeFuncCpp;
|
||||||
|
using NativeFuncRaw = std::variant<NativeFuncC, NativeFuncCpp>;
|
||||||
|
|
||||||
typedef shared_ptr<CodeObject> CodeObject_;
|
typedef shared_ptr<CodeObject> CodeObject_;
|
||||||
|
|
||||||
struct NativeFunc {
|
struct NativeFunc {
|
||||||
|
@ -739,10 +739,10 @@ struct Random{
|
|||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
vm->bind_static_method<0>(type, "__new__", CPP_LAMBDA(VAR_T(Random)));
|
vm->bind_static_method<0>(type, "__new__", CPP_LAMBDA(VAR_T(Random)));
|
||||||
vm->bind_method<1>(type, "seed", native_proxy_callable(&Random::seed));
|
vm->bind_cpp_method<1>(type, "seed", native_proxy_callable(&Random::seed));
|
||||||
vm->bind_method<2>(type, "randint", native_proxy_callable(&Random::randint));
|
vm->bind_cpp_method<2>(type, "randint", native_proxy_callable(&Random::randint));
|
||||||
vm->bind_method<0>(type, "random", native_proxy_callable(&Random::random));
|
vm->bind_cpp_method<0>(type, "random", native_proxy_callable(&Random::random));
|
||||||
vm->bind_method<2>(type, "uniform", native_proxy_callable(&Random::uniform));
|
vm->bind_cpp_method<2>(type, "uniform", native_proxy_callable(&Random::uniform));
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -956,7 +956,7 @@ extern "C" {
|
|||||||
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
|
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
|
||||||
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
|
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
|
||||||
pkpy::PyObject* obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod);
|
pkpy::PyObject* obj = vm->_modules.contains(mod) ? vm->_modules[mod] : vm->new_module(mod);
|
||||||
vm->bind_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, pkpy::ArgsView args){
|
vm->bind_cpp_func<-1>(obj, name, [ret_code, f_header](pkpy::VM* vm, pkpy::ArgsView args){
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
ss << f_header;
|
ss << f_header;
|
||||||
for(int i=0; i<args.size(); i++){
|
for(int i=0; i<args.size(); i++){
|
||||||
|
40
src/vm.h
40
src/vm.h
@ -9,7 +9,6 @@
|
|||||||
#include "obj.h"
|
#include "obj.h"
|
||||||
#include "str.h"
|
#include "str.h"
|
||||||
#include "tuplelist.h"
|
#include "tuplelist.h"
|
||||||
#include <tuple>
|
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
@ -231,7 +230,7 @@ public:
|
|||||||
return call_method(self, callable, args...);
|
return call_method(self, callable, args...);
|
||||||
}
|
}
|
||||||
|
|
||||||
PyObject* property(NativeFuncRaw fget){
|
PyObject* property(NativeFuncC fget){
|
||||||
PyObject* p = builtins->attr("property");
|
PyObject* p = builtins->attr("property");
|
||||||
PyObject* method = heap.gcnew(tp_native_func, NativeFunc(fget, 1, false));
|
PyObject* method = heap.gcnew(tp_native_func, NativeFunc(fget, 1, false));
|
||||||
return call(p, method);
|
return call(p, method);
|
||||||
@ -264,12 +263,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bind_func(Str type, Str name, NativeFuncRaw fn) {
|
void bind_func(Str type, Str name, NativeFuncC fn) {
|
||||||
bind_func<ARGC>(_find_type(type), name, fn);
|
bind_func<ARGC>(_find_type(type), name, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bind_method(Str type, Str name, NativeFuncRaw fn) {
|
void bind_method(Str type, Str name, NativeFuncC fn) {
|
||||||
bind_method<ARGC>(_find_type(type), name, fn);
|
bind_method<ARGC>(_find_type(type), name, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -279,12 +278,12 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void _bind_methods(std::vector<Str> types, Str name, NativeFuncRaw fn) {
|
void _bind_methods(std::vector<Str> types, Str name, NativeFuncC fn) {
|
||||||
for(auto& type: types) bind_method<ARGC>(type, name, fn);
|
for(auto& type: types) bind_method<ARGC>(type, name, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bind_builtin_func(Str name, NativeFuncRaw fn) {
|
void bind_builtin_func(Str name, NativeFuncC fn) {
|
||||||
bind_func<ARGC>(builtins, name, fn);
|
bind_func<ARGC>(builtins, name, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -379,9 +378,13 @@ public:
|
|||||||
PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false);
|
PyObject* get_unbound_method(PyObject* obj, StrName name, PyObject** self, bool throw_err=true, bool fallback=false);
|
||||||
void setattr(PyObject* obj, StrName name, PyObject* value);
|
void setattr(PyObject* obj, StrName name, PyObject* value);
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bind_method(PyObject*, Str, NativeFuncRaw);
|
void bind_method(PyObject*, Str, NativeFuncC);
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void bind_func(PyObject*, Str, NativeFuncRaw);
|
void bind_func(PyObject*, Str, NativeFuncC);
|
||||||
|
template<int ARGC>
|
||||||
|
void bind_cpp_method(PyObject*, Str, NativeFuncCpp);
|
||||||
|
template<int ARGC>
|
||||||
|
void bind_cpp_func(PyObject*, Str, NativeFuncCpp);
|
||||||
void _error(Exception);
|
void _error(Exception);
|
||||||
PyObject* _run_top_frame();
|
PyObject* _run_top_frame();
|
||||||
void post_init();
|
void post_init();
|
||||||
@ -392,7 +395,11 @@ inline PyObject* NativeFunc::operator()(VM* vm, ArgsView args) const{
|
|||||||
if(argc != -1 && args_size != argc) {
|
if(argc != -1 && args_size != argc) {
|
||||||
vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size));
|
vm->TypeError(fmt("expected ", argc, " arguments, but got ", args_size));
|
||||||
}
|
}
|
||||||
return f(vm, args);
|
if(std::holds_alternative<NativeFuncC>(f)){
|
||||||
|
return std::get<NativeFuncC>(f)(vm, args);
|
||||||
|
}else{
|
||||||
|
return std::get<NativeFuncCpp>(f)(vm, args);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void CodeObject::optimize(VM* vm){
|
inline void CodeObject::optimize(VM* vm){
|
||||||
@ -1035,13 +1042,24 @@ inline void VM::setattr(PyObject* obj, StrName name, PyObject* value){
|
|||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void VM::bind_method(PyObject* obj, Str name, NativeFuncRaw fn) {
|
void VM::bind_method(PyObject* obj, Str name, NativeFuncC fn) {
|
||||||
check_type(obj, tp_type);
|
check_type(obj, tp_type);
|
||||||
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true)));
|
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<int ARGC>
|
template<int ARGC>
|
||||||
void VM::bind_func(PyObject* obj, Str name, NativeFuncRaw fn) {
|
void VM::bind_cpp_method(PyObject* obj, Str name, NativeFuncCpp fn) {
|
||||||
|
check_type(obj, tp_type);
|
||||||
|
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<int ARGC>
|
||||||
|
void VM::bind_func(PyObject* obj, Str name, NativeFuncC fn) {
|
||||||
|
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false)));
|
||||||
|
}
|
||||||
|
|
||||||
|
template<int ARGC>
|
||||||
|
void VM::bind_cpp_func(PyObject* obj, Str name, NativeFuncCpp fn) {
|
||||||
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false)));
|
obj->attr().set(name, VAR(NativeFunc(fn, ARGC, false)));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user