blueloveTH 2 lat temu
rodzic
commit
d23401f77b
11 zmienionych plików z 132 dodań i 64 usunięć
  1. 1 1
      amalgamate.py
  2. 1 1
      docs/modules/requests.md
  3. 13 3
      src/base64.h
  4. 2 1
      src/common.h
  5. 11 2
      src/config.h
  6. 10 1
      src/easing.h
  7. 11 2
      src/linalg.h
  8. 1 48
      src/pocketpy.h
  9. 68 0
      src/random.h
  10. 10 0
      src/re.h
  11. 4 5
      src/requests.h

+ 1 - 1
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"]
 ]
 

+ 1 - 1
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.
 !!!

+ 13 - 3
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
+} // namespace pkpy
+
+
+#else
+
+#include "common.h"
+
+ADD_MODULE_PLACEHOLDER(base64)
+
+#endif

+ 2 - 1
src/common.h

@@ -16,7 +16,6 @@
 #include <map>
 #include <set>
 #include <algorithm>
-#include <random>
 #include <initializer_list>
 #include <variant>
 #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_YIELD = (PyObject*)0b110011;
 
+#define ADD_MODULE_PLACEHOLDER(name) namespace pkpy { inline void add_module_##name(void* vm) { (void)vm; } }
+
 } // namespace pkpy

+ 11 - 2
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

+ 10 - 1
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

+ 11 - 2
src/linalg.h

@@ -1,6 +1,7 @@
 #pragma once
 
-#include <cmath>
+#if PK_MODULE_LINALG
+
 #include "cffi.h"
 
 namespace pkpy{
@@ -673,4 +674,12 @@ inline void add_module_linalg(VM* vm){
 
 static_assert(sizeof(Py_<PyMat3x3>) <= 64);
 
-}   // namespace pkpy
+}   // namespace pkpy
+
+#else
+
+#include "common.h"
+
+ADD_MODULE_PLACEHOLDER(linalg)
+
+#endif

+ 1 - 48
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<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){
     PyObject* mod = vm->new_module("gc");
     vm->bind_func<0>(mod, "collect", CPP_LAMBDA(VAR(vm->heap.collect())));

+ 68 - 0
src/random.h

@@ -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 - 0
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

+ 4 - 5
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