From d23401f77b1799e799a8b358c422fc3fc7299c74 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 11 Jun 2023 21:16:13 +0800 Subject: [PATCH] ... --- amalgamate.py | 2 +- docs/modules/requests.md | 2 +- src/base64.h | 16 ++++++++-- src/common.h | 3 +- src/config.h | 13 ++++++-- src/easing.h | 11 ++++++- src/linalg.h | 13 ++++++-- src/pocketpy.h | 49 +---------------------------- src/random.h | 68 ++++++++++++++++++++++++++++++++++++++++ src/re.h | 10 ++++++ src/requests.h | 9 +++--- 11 files changed, 132 insertions(+), 64 deletions(-) create mode 100644 src/random.h diff --git a/amalgamate.py b/amalgamate.py index a57380c4..4a2b3de1 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -9,7 +9,7 @@ pipeline = [ ["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"], ["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"] ] diff --git a/docs/modules/requests.md b/docs/modules/requests.md index 4533cc15..3219881d 100644 --- a/docs/modules/requests.md +++ b/docs/modules/requests.md @@ -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. !!! diff --git a/src/base64.h b/src/base64.h index 51c5aa54..52c3fe33 100644 --- a/src/base64.h +++ b/src/base64.h @@ -1,7 +1,8 @@ #pragma once -#include "common.h" -#include "vm.h" +#if PK_MODULE_BASE64 + +#include "cffi.h" namespace pkpy { @@ -190,4 +191,13 @@ inline void add_module_base64(VM* vm){ }); } -} // namespace pkpy \ No newline at end of file +} // namespace pkpy + + +#else + +#include "common.h" + +ADD_MODULE_PLACEHOLDER(base64) + +#endif \ No newline at end of file diff --git a/src/common.h b/src/common.h index 90e404fa..13652ab2 100644 --- a/src/common.h +++ b/src/common.h @@ -16,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -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_YIELD = (PyObject*)0b110011; +#define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } } + } // namespace pkpy \ No newline at end of file diff --git a/src/config.h b/src/config.h index db841f39..05ffc2ea 100644 --- a/src/config.h +++ b/src/config.h @@ -8,13 +8,13 @@ /*************** feature settings ***************/ -// Whether to compile os-related modules +// Whether to compile os-related modules or not #define PK_ENABLE_OS 1 // Enable this if you are working with multi-threading (experimental) // This triggers necessary locks to make the VM thread-safe #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 // However, someone thinks it's not convenient. // 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 #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 \ No newline at end of file diff --git a/src/easing.h b/src/easing.h index de364950..9fccb74b 100644 --- a/src/easing.h +++ b/src/easing.h @@ -1,5 +1,7 @@ #pragma once +#if PK_MODULE_EASING + #include "cffi.h" namespace pkpy{ @@ -252,5 +254,12 @@ inline void add_module_easing(VM* vm){ #undef EASE } - } // namespace pkpy + +#else + +#include "common.h" + +ADD_MODULE_PLACEHOLDER(easing) + +#endif \ No newline at end of file diff --git a/src/linalg.h b/src/linalg.h index e2d3a800..960118d9 100644 --- a/src/linalg.h +++ b/src/linalg.h @@ -1,6 +1,7 @@ #pragma once -#include +#if PK_MODULE_LINALG + #include "cffi.h" namespace pkpy{ @@ -673,4 +674,12 @@ inline void add_module_linalg(VM* vm){ static_assert(sizeof(Py_) <= 64); -} // namespace pkpy \ No newline at end of file +} // namespace pkpy + +#else + +#include "common.h" + +ADD_MODULE_PLACEHOLDER(linalg) + +#endif \ No newline at end of file diff --git a/src/pocketpy.h b/src/pocketpy.h index 8a37e70f..bc62198b 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -15,6 +15,7 @@ #include "export.h" #include "vm.h" #include "re.h" +#include "random.h" 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(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 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 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 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){ PyObject* mod = vm->new_module("gc"); vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect()))); diff --git a/src/random.h b/src/random.h new file mode 100644 index 00000000..bb733704 --- /dev/null +++ b/src/random.h @@ -0,0 +1,68 @@ +#pragma once + +#if PK_MODULE_RANDOM + +#include + +#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(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 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 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 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 \ No newline at end of file diff --git a/src/re.h b/src/re.h index 045a7dac..abc33b9f 100644 --- a/src/re.h +++ b/src/re.h @@ -1,5 +1,7 @@ #pragma once +#if PK_MODULE_RE + #include "cffi.h" namespace pkpy{ @@ -82,3 +84,11 @@ inline void add_module_re(VM* vm){ } } // namespace pkpy + +#else + +#include "common.h" + +ADD_MODULE_PLACEHOLDER(re) + +#endif \ No newline at end of file diff --git a/src/requests.h b/src/requests.h index cc4b4423..4afdc8b6 100644 --- a/src/requests.h +++ b/src/requests.h @@ -1,12 +1,11 @@ #pragma once #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 "cffi.h" +#include "_generated.h" namespace pkpy { @@ -98,6 +97,6 @@ inline void add_module_requests(VM* vm){ #else -inline void add_module_requests(void* vm){ } +ADD_MODULE_PLACEHOLDER(requests) #endif \ No newline at end of file