Parcourir la source

add `config.h`

blueloveTH il y a 2 ans
Parent
commit
28c8f68d8d
5 fichiers modifiés avec 72 ajouts et 43 suppressions
  1. 1 1
      amalgamate.py
  2. 10 41
      src/common.h
  3. 55 0
      src/config.h
  4. 1 1
      src/frame.h
  5. 5 0
      src/obj.h

+ 1 - 1
amalgamate.py

@@ -6,7 +6,7 @@ with open("src/opcodes.h", "rt", encoding='utf-8') as f:
 	OPCODES_TEXT = f.read()
 
 pipeline = [
-	["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"],
 	["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"],

+ 10 - 41
src/common.h

@@ -29,37 +29,18 @@
 #include <variant>
 #include <type_traits>
 
-#define PK_VERSION				"1.0.2"
-
-// debug macros
-#define DEBUG_NO_BUILTIN_MODULES    0
-#define DEBUG_EXTRA_CHECK           0
-#define DEBUG_DIS_EXEC              0
-#define DEBUG_CEVAL_STEP            0
-#define DEBUG_FULL_EXCEPTION        0
-#define DEBUG_MEMORY_POOL           0
-#define DEBUG_NO_MEMORY_POOL        0
-#define DEBUG_NO_AUTO_GC            0
-#define DEBUG_GC_STATS              0
-
-// config macros
-#ifndef PK_ENABLE_OS
-
-#ifdef __ANDROID__
-#include <android/ndk-version.h>
-
-#if __NDK_MAJOR__ <= 22
-#define PK_ENABLE_OS 			0
-#else
-#define PK_ENABLE_OS 			1
-#endif
+#include "config.h"
 
-#else
-#define PK_ENABLE_OS 			1
-#endif
+#define PK_VERSION				"1.0.3"
 
+/*******************************************************************************/
+
+#if PK_ENABLE_STD_FUNCTION
+#include <functional>
 #endif
 
+/*******************************************************************************/
+
 #if PK_ENABLE_THREAD
 #define THREAD_LOCAL thread_local
 #include <mutex>
@@ -77,11 +58,6 @@ struct GIL {
 #endif
 
 /*******************************************************************************/
-
-// This is the maximum number of arguments in a function declaration
-// including positional arguments, keyword-only arguments, and varargs
-#define PK_MAX_CO_VARNAMES			255
-
 #if _MSC_VER
 #define PK_ENABLE_COMPUTED_GOTO		0
 #define UNREACHABLE()				__assume(0)
@@ -95,6 +71,8 @@ struct GIL {
 
 #endif
 
+/*******************************************************************************/
+
 namespace pkpy{
 
 namespace std = ::std;
@@ -165,9 +143,6 @@ struct Type {
 
 #define PK_ASSERT(x) if(!(x)) FATAL_ERROR();
 
-inline const float kInstAttrLoadFactor = 0.67f;
-inline const float kTypeAttrLoadFactor = 0.5f;
-
 struct PyObject;
 #define BITS(p) (reinterpret_cast<i64>(p))
 inline bool is_tagged(PyObject* p) noexcept { return (BITS(p) & 0b11) != 0b00; }
@@ -188,10 +163,4 @@ 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;
 
-#ifdef _WIN32
-    inline const char kPlatformSep = '\\';
-#else
-    inline const char kPlatformSep = '/';
-#endif
-
 } // namespace pkpy

+ 55 - 0
src/config.h

@@ -0,0 +1,55 @@
+#pragma once
+
+#ifdef PK_USER_CONFIG
+
+#include "user_config.h"
+
+#else
+
+/*************** feature settings ***************/
+#define PK_ENABLE_OS                1
+#define PK_ENABLE_THREAD            0
+
+// Whether to use `std::function` to do bindings
+// By default, the function to be binded must be a C function pointer with no capture
+// which is fast and simple. However, someone thinks it's not convenient enough.
+// By setting this to 1, capturing lambdas can be binded,
+// but it's slower and may cause severe "code bloat", also needs more time to compile.
+#define PK_ENABLE_STD_FUNCTION      0
+
+/*************** internal settings ***************/
+
+// This is the maximum size of the value stack in void* units, not bytes
+// The actual size is `sizeof(void*) * PK_VM_STACK_SIZE`
+#define PK_VM_STACK_SIZE            32768
+
+// This is the maximum number of arguments in a function declaration
+// including positional arguments, keyword-only arguments, and varargs
+#define PK_MAX_CO_VARNAMES			255
+
+// Hash table load factor (smaller ones mean less collision but more memory)
+inline const float kInstAttrLoadFactor = 0.67f;
+inline const float kTypeAttrLoadFactor = 0.5f;
+
+#ifdef _WIN32
+    inline const char kPlatformSep = '\\';
+#else
+    inline const char kPlatformSep = '/';
+#endif
+
+/*************** debug settings ***************/
+
+// Enable this may help you to find bugs in the VM
+#define DEBUG_EXTRA_CHECK           0
+
+// Do not edit the following settings unless you know what you are doing
+#define DEBUG_NO_BUILTIN_MODULES    0
+#define DEBUG_DIS_EXEC              0
+#define DEBUG_CEVAL_STEP            0
+#define DEBUG_FULL_EXCEPTION        0
+#define DEBUG_MEMORY_POOL           0
+#define DEBUG_NO_MEMORY_POOL        0
+#define DEBUG_NO_AUTO_GC            0
+#define DEBUG_GC_STATS              0
+
+#endif

+ 1 - 1
src/frame.h

@@ -100,7 +100,7 @@ struct ValueStackImpl {
     ValueStackImpl& operator=(ValueStackImpl&&) = delete;
 };
 
-using ValueStack = ValueStackImpl<32768>;
+using ValueStack = ValueStackImpl<PK_VM_STACK_SIZE>;
 
 struct Frame {
     int _ip = -1;

+ 5 - 0
src/obj.h

@@ -11,7 +11,12 @@ struct Frame;
 struct Function;
 class VM;
 
+#if PK_ENABLE_STD_FUNCTION
+using NativeFuncC = std::function<PyObject*(VM*, ArgsView)>;
+#else
 typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
+#endif
+
 typedef int (*LuaStyleFuncC)(VM*);
 
 struct NativeFunc {