mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
2f5b34fe46
commit
d23401f77b
@ -9,7 +9,7 @@ pipeline = [
|
|||||||
["config.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
|
["config.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
|
||||||
["obj.h", "dict.h", "codeobject.h", "frame.h"],
|
["obj.h", "dict.h", "codeobject.h", "frame.h"],
|
||||||
["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
|
["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
|
||||||
["_generated.h", "cffi.h", "iter.h", "base64.h", "re.h", "linalg.h", "easing.h", "requests.h", "io.h"],
|
["_generated.h", "cffi.h", "iter.h", "base64.h", "random.h", "re.h", "linalg.h", "easing.h", "requests.h", "io.h"],
|
||||||
["export.h", "pocketpy.h"]
|
["export.h", "pocketpy.h"]
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ label: requests
|
|||||||
---
|
---
|
||||||
|
|
||||||
!!!
|
!!!
|
||||||
This module is experimental. To enable it, download `httplib.h` from [here](https://github.com/yhirose/cpp-httplib) and place it in the same directory as `pocketpy.h`.
|
This module is experimental. To enable it, download `httplib.h` from [here](https://github.com/yhirose/cpp-httplib) and place it in the same directory as `pocketpy.h`. Also set `PK_MODULE_REQUESTS` to `1` in `config.h`.
|
||||||
|
|
||||||
SSL is not supported.
|
SSL is not supported.
|
||||||
!!!
|
!!!
|
||||||
|
14
src/base64.h
14
src/base64.h
@ -1,7 +1,8 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common.h"
|
#if PK_MODULE_BASE64
|
||||||
#include "vm.h"
|
|
||||||
|
#include "cffi.h"
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
@ -191,3 +192,12 @@ inline void add_module_base64(VM* vm){
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
ADD_MODULE_PLACEHOLDER(base64)
|
||||||
|
|
||||||
|
#endif
|
@ -16,7 +16,6 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <random>
|
|
||||||
#include <initializer_list>
|
#include <initializer_list>
|
||||||
#include <variant>
|
#include <variant>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
@ -139,4 +138,6 @@ inline PyObject* const PY_NULL = (PyObject*)0b000011; // tagged null
|
|||||||
inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
|
inline PyObject* const PY_OP_CALL = (PyObject*)0b100011;
|
||||||
inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
|
inline PyObject* const PY_OP_YIELD = (PyObject*)0b110011;
|
||||||
|
|
||||||
|
#define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } }
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
13
src/config.h
13
src/config.h
@ -8,13 +8,13 @@
|
|||||||
|
|
||||||
/*************** feature settings ***************/
|
/*************** feature settings ***************/
|
||||||
|
|
||||||
// Whether to compile os-related modules
|
// Whether to compile os-related modules or not
|
||||||
#define PK_ENABLE_OS 1
|
#define PK_ENABLE_OS 1
|
||||||
// Enable this if you are working with multi-threading (experimental)
|
// Enable this if you are working with multi-threading (experimental)
|
||||||
// This triggers necessary locks to make the VM thread-safe
|
// This triggers necessary locks to make the VM thread-safe
|
||||||
#define PK_ENABLE_THREAD 0
|
#define PK_ENABLE_THREAD 0
|
||||||
|
|
||||||
// Whether to use `std::function` to do bindings
|
// Whether to use `std::function` to do bindings or not
|
||||||
// By default, functions to be binded must be a C function pointer without capture
|
// By default, functions to be binded must be a C function pointer without capture
|
||||||
// However, someone thinks it's not convenient.
|
// However, someone thinks it's not convenient.
|
||||||
// By setting this to 1, capturing lambdas can be binded,
|
// By setting this to 1, capturing lambdas can be binded,
|
||||||
@ -80,4 +80,13 @@ inline const float kTypeAttrLoadFactor = 0.5f;
|
|||||||
#undef PK_ENABLE_COMPUTED_GOTO
|
#undef PK_ENABLE_COMPUTED_GOTO
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
/*************** module settings ***************/
|
||||||
|
|
||||||
|
#define PK_MODULE_RE 1
|
||||||
|
#define PK_MODULE_RANDOM 1
|
||||||
|
#define PK_MODULE_BASE64 1
|
||||||
|
#define PK_MODULE_LINALG 1
|
||||||
|
#define PK_MODULE_EASING 1
|
||||||
|
#define PK_MODULE_REQUESTS 0
|
||||||
|
|
||||||
#endif
|
#endif
|
11
src/easing.h
11
src/easing.h
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#if PK_MODULE_EASING
|
||||||
|
|
||||||
#include "cffi.h"
|
#include "cffi.h"
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
@ -252,5 +254,12 @@ inline void add_module_easing(VM* vm){
|
|||||||
#undef EASE
|
#undef EASE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
ADD_MODULE_PLACEHOLDER(easing)
|
||||||
|
|
||||||
|
#endif
|
11
src/linalg.h
11
src/linalg.h
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <cmath>
|
#if PK_MODULE_LINALG
|
||||||
|
|
||||||
#include "cffi.h"
|
#include "cffi.h"
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
@ -674,3 +675,11 @@ inline void add_module_linalg(VM* vm){
|
|||||||
static_assert(sizeof(Py_<PyMat3x3>) <= 64);
|
static_assert(sizeof(Py_<PyMat3x3>) <= 64);
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
ADD_MODULE_PLACEHOLDER(linalg)
|
||||||
|
|
||||||
|
#endif
|
@ -15,6 +15,7 @@
|
|||||||
#include "export.h"
|
#include "export.h"
|
||||||
#include "vm.h"
|
#include "vm.h"
|
||||||
#include "re.h"
|
#include "re.h"
|
||||||
|
#include "random.h"
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
@ -1264,54 +1265,6 @@ inline void add_module_dis(VM* vm){
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Random{
|
|
||||||
PY_CLASS(Random, random, Random)
|
|
||||||
std::mt19937 gen;
|
|
||||||
|
|
||||||
Random(){
|
|
||||||
gen.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
|
||||||
}
|
|
||||||
|
|
||||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
|
||||||
vm->bind_default_constructor<Random>(type);
|
|
||||||
|
|
||||||
vm->bind_method<1>(type, "seed", [](VM* vm, ArgsView args) {
|
|
||||||
Random& self = _CAST(Random&, args[0]);
|
|
||||||
self.gen.seed(CAST(i64, args[1]));
|
|
||||||
return vm->None;
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind_method<2>(type, "randint", [](VM* vm, ArgsView args) {
|
|
||||||
Random& self = _CAST(Random&, args[0]);
|
|
||||||
i64 a = CAST(i64, args[1]);
|
|
||||||
i64 b = CAST(i64, args[2]);
|
|
||||||
std::uniform_int_distribution<i64> dis(a, b);
|
|
||||||
return VAR(dis(self.gen));
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind_method<0>(type, "random", [](VM* vm, ArgsView args) {
|
|
||||||
Random& self = _CAST(Random&, args[0]);
|
|
||||||
std::uniform_real_distribution<f64> dis(0.0, 1.0);
|
|
||||||
return VAR(dis(self.gen));
|
|
||||||
});
|
|
||||||
|
|
||||||
vm->bind_method<2>(type, "uniform", [](VM* vm, ArgsView args) {
|
|
||||||
Random& self = _CAST(Random&, args[0]);
|
|
||||||
f64 a = CAST(f64, args[1]);
|
|
||||||
f64 b = CAST(f64, args[2]);
|
|
||||||
std::uniform_real_distribution<f64> dis(a, b);
|
|
||||||
return VAR(dis(self.gen));
|
|
||||||
});
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
inline void add_module_random(VM* vm){
|
|
||||||
PyObject* mod = vm->new_module("random");
|
|
||||||
Random::register_class(vm, mod);
|
|
||||||
CodeObject_ code = vm->compile(kPythonLibs["random"], "random.py", EXEC_MODE);
|
|
||||||
vm->_exec(code, mod);
|
|
||||||
}
|
|
||||||
|
|
||||||
inline void add_module_gc(VM* vm){
|
inline void add_module_gc(VM* vm){
|
||||||
PyObject* mod = vm->new_module("gc");
|
PyObject* mod = vm->new_module("gc");
|
||||||
vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect())));
|
vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect())));
|
||||||
|
68
src/random.h
Normal file
68
src/random.h
Normal file
@ -0,0 +1,68 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#if PK_MODULE_RANDOM
|
||||||
|
|
||||||
|
#include <random>
|
||||||
|
|
||||||
|
#include "cffi.h"
|
||||||
|
#include "_generated.h"
|
||||||
|
|
||||||
|
namespace pkpy{
|
||||||
|
|
||||||
|
struct Random{
|
||||||
|
PY_CLASS(Random, random, Random)
|
||||||
|
std::mt19937 gen;
|
||||||
|
|
||||||
|
Random(){
|
||||||
|
gen.seed(std::chrono::high_resolution_clock::now().time_since_epoch().count());
|
||||||
|
}
|
||||||
|
|
||||||
|
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||||
|
vm->bind_default_constructor<Random>(type);
|
||||||
|
|
||||||
|
vm->bind_method<1>(type, "seed", [](VM* vm, ArgsView args) {
|
||||||
|
Random& self = _CAST(Random&, args[0]);
|
||||||
|
self.gen.seed(CAST(i64, args[1]));
|
||||||
|
return vm->None;
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind_method<2>(type, "randint", [](VM* vm, ArgsView args) {
|
||||||
|
Random& self = _CAST(Random&, args[0]);
|
||||||
|
i64 a = CAST(i64, args[1]);
|
||||||
|
i64 b = CAST(i64, args[2]);
|
||||||
|
std::uniform_int_distribution<i64> dis(a, b);
|
||||||
|
return VAR(dis(self.gen));
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind_method<0>(type, "random", [](VM* vm, ArgsView args) {
|
||||||
|
Random& self = _CAST(Random&, args[0]);
|
||||||
|
std::uniform_real_distribution<f64> dis(0.0, 1.0);
|
||||||
|
return VAR(dis(self.gen));
|
||||||
|
});
|
||||||
|
|
||||||
|
vm->bind_method<2>(type, "uniform", [](VM* vm, ArgsView args) {
|
||||||
|
Random& self = _CAST(Random&, args[0]);
|
||||||
|
f64 a = CAST(f64, args[1]);
|
||||||
|
f64 b = CAST(f64, args[2]);
|
||||||
|
std::uniform_real_distribution<f64> dis(a, b);
|
||||||
|
return VAR(dis(self.gen));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
inline void add_module_random(VM* vm){
|
||||||
|
PyObject* mod = vm->new_module("random");
|
||||||
|
Random::register_class(vm, mod);
|
||||||
|
CodeObject_ code = vm->compile(kPythonLibs["random"], "random.py", EXEC_MODE);
|
||||||
|
vm->_exec(code, mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace pkpy
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
ADD_MODULE_PLACEHOLDER(random)
|
||||||
|
|
||||||
|
#endif
|
10
src/re.h
10
src/re.h
@ -1,5 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#if PK_MODULE_RE
|
||||||
|
|
||||||
#include "cffi.h"
|
#include "cffi.h"
|
||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
@ -82,3 +84,11 @@ inline void add_module_re(VM* vm){
|
|||||||
}
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#include "common.h"
|
||||||
|
|
||||||
|
ADD_MODULE_PLACEHOLDER(re)
|
||||||
|
|
||||||
|
#endif
|
@ -1,12 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
#include "obj.h"
|
|
||||||
#include "vm.h"
|
|
||||||
#include "_generated.h"
|
|
||||||
|
|
||||||
#if __has_include("httplib.h")
|
#if PK_MODULE_REQUESTS
|
||||||
#include "httplib.h"
|
#include "httplib.h"
|
||||||
|
#include "cffi.h"
|
||||||
|
#include "_generated.h"
|
||||||
|
|
||||||
namespace pkpy {
|
namespace pkpy {
|
||||||
|
|
||||||
@ -98,6 +97,6 @@ inline void add_module_requests(VM* vm){
|
|||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
inline void add_module_requests(void* vm){ }
|
ADD_MODULE_PLACEHOLDER(requests)
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user