diff --git a/docs/quick-start/wrap.md b/docs/quick-start/wrap.md index 816d0b5c..4a885bb3 100644 --- a/docs/quick-start/wrap.md +++ b/docs/quick-start/wrap.md @@ -26,8 +26,8 @@ struct Vector2 { static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){ - float x = vm->num_to_float(args[1]); - float y = vm->num_to_float(args[2]); + float x = VAR_F(args[1]); + float y = VAR_F(args[2]); return VAR_T(Vector2, x, y); }); @@ -52,13 +52,13 @@ struct Vector2 { vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){ Vector2& self = CAST(Vector2&, args[0]); - f64 other = vm->num_to_float(args[1]); + f64 other = VAR_F(args[1]); return VAR_T(Vector2, self.x * other, self.y * other); }); vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){ Vector2& self = CAST(Vector2&, args[0]); - f64 other = vm->num_to_float(args[1]); + f64 other = VAR_F(args[1]); return VAR_T(Vector2, self.x / other, self.y / other); }); diff --git a/src/base64.h b/src/base64.h index da2cde84..11315e9c 100644 --- a/src/base64.h +++ b/src/base64.h @@ -7,9 +7,9 @@ namespace pkpy { // https://github.com/zhicheng/base64/blob/master/base64.c -#define BASE64_PAD '=' -#define BASE64DE_FIRST '+' -#define BASE64DE_LAST 'z' +inline static const char BASE64_PAD = '='; +inline static const char BASE64DE_FIRST = '+'; +inline static const char BASE64DE_LAST = 'z'; /* BASE 64 encode table */ static const char base64en[] = { diff --git a/src/ceval.h b/src/ceval.h index fcf1fcaf..85d27ab0 100644 --- a/src/ceval.h +++ b/src/ceval.h @@ -607,4 +607,8 @@ __NEXT_STEP:; #undef POPX #undef STACK_VIEW +#undef DISPATCH +#undef TARGET +#undef DISPATCH_OP_CALL + } // namespace pkpy \ No newline at end of file diff --git a/src/linalg.h b/src/linalg.h index a55d110c..0920d3de 100644 --- a/src/linalg.h +++ b/src/linalg.h @@ -344,8 +344,8 @@ struct PyVec2: Vec2 { static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){ - float x = vm->num_to_float(args[1]); - float y = vm->num_to_float(args[2]); + float x = VAR_F(args[1]); + float y = VAR_F(args[2]); return VAR(Vec2(x, y)); }); @@ -387,9 +387,9 @@ struct PyVec3: Vec3 { static void _register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_constructor<4>(type, [](VM* vm, ArgsView args){ - float x = vm->num_to_float(args[1]); - float y = vm->num_to_float(args[2]); - float z = vm->num_to_float(args[3]); + float x = VAR_F(args[1]); + float y = VAR_F(args[2]); + float z = VAR_F(args[3]); return VAR(Vec3(x, y, z)); }); @@ -435,7 +435,7 @@ struct PyMat3x3: Mat3x3{ if(args.size() == 1+0) return VAR_T(PyMat3x3, Mat3x3::zeros()); if(args.size() == 1+9){ Mat3x3 mat; - for(int i=0; i<9; i++) mat.v[i] = vm->num_to_float(args[1+i]); + for(int i=0; i<9; i++) mat.v[i] = VAR_F(args[1+i]); return VAR_T(PyMat3x3, mat); } if(args.size() == 1+1){ @@ -446,7 +446,7 @@ struct PyMat3x3: Mat3x3{ List& b = CAST(List&, a[i]); if(b.size() != 3) vm->ValueError("Mat3x3.__new__ takes 3x3 list"); for(int j=0; j<3; j++){ - mat.m[i][j] = vm->num_to_float(b[j]); + mat.m[i][j] = VAR_F(b[j]); } } return VAR_T(PyMat3x3, mat); @@ -512,7 +512,7 @@ struct PyMat3x3: Mat3x3{ vm->IndexError("index out of range"); return vm->None; } - self.m[i][j] = vm->num_to_float(args[2]); + self.m[i][j] = VAR_F(args[2]); return vm->None; }); @@ -552,13 +552,13 @@ struct PyMat3x3: Mat3x3{ vm->bind_method<1>(type, "__mul__", [](VM* vm, ArgsView args){ PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - f64 other = vm->num_to_float(args[1]); + f64 other = VAR_F(args[1]); return VAR_T(PyMat3x3, self * other); }); vm->bind_method<1>(type, "__truediv__", [](VM* vm, ArgsView args){ PyMat3x3& self = _CAST(PyMat3x3&, args[0]); - f64 other = vm->num_to_float(args[1]); + f64 other = VAR_F(args[1]); return VAR_T(PyMat3x3, self / other); }); @@ -628,7 +628,7 @@ struct PyMat3x3: Mat3x3{ }); vm->bind_func<1>(type, "rotate", [](VM* vm, ArgsView args){ - f64 angle = vm->num_to_float(args[0]); + f64 angle = VAR_F(args[0]); return VAR_T(PyMat3x3, Mat3x3::rotate(angle)); }); @@ -639,7 +639,7 @@ struct PyMat3x3: Mat3x3{ vm->bind_func<3>(type, "trs", [](VM* vm, ArgsView args){ PyVec2& t = CAST(PyVec2&, args[0]); - f64 r = vm->num_to_float(args[1]); + f64 r = VAR_F(args[1]); PyVec2& s = CAST(PyVec2&, args[2]); return VAR_T(PyMat3x3, Mat3x3::trs(t, r, s)); }); diff --git a/src/obj.h b/src/obj.h index e597de3b..71b0f9e7 100644 --- a/src/obj.h +++ b/src/obj.h @@ -263,7 +263,7 @@ __T _py_cast(VM* vm, PyObject* obj) { #define CAST(T, x) py_cast(vm, x) #define _CAST(T, x) _py_cast(vm, x) -#define FLOAT(x) vm->num_to_float(x) +#define VAR_F(x) vm->num_to_float(x) /*****************************************************************/ template<> diff --git a/src/pocketpy.h b/src/pocketpy.h index a2b6072b..561dbebd 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -46,14 +46,14 @@ inline CodeObject_ VM::compile(Str source, Str filename, CompileMode mode, bool _vm->bind_method<1>("int", #name, [](VM* vm, ArgsView args){ \ if(is_int(args[1])) return VAR(_CAST(i64, args[0]) op _CAST(i64, args[1])); \ if(is_float(args[1])) return VAR(vm->num_to_float(args[0]) op _CAST(f64, args[1])); \ - if constexpr(is_eq) return VAR(args[0] op args[1]); \ + if constexpr(is_eq) return VAR(args[0] op args[1]); \ vm->TypeError("unsupported operand type(s) for " #op ); \ return vm->None; \ }); \ _vm->bind_method<1>("float", #name, [](VM* vm, ArgsView args){ \ if(is_float(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(f64, args[1])); \ if(is_int(args[1])) return VAR(_CAST(f64, args[0]) op _CAST(i64, args[1])); \ - if constexpr(is_eq) return VAR(args[0] op args[1]); \ + if constexpr(is_eq) return VAR(args[0] op args[1]); \ vm->TypeError("unsupported operand type(s) for " #op ); \ return vm->None; \ }); @@ -228,13 +228,13 @@ inline void init_builtins(VM* _vm) { _vm->bind_method<0>("NoneType", "__json__", CPP_LAMBDA(VAR("null"))); _vm->bind_method<1>("int", "__truediv__", [](VM* vm, ArgsView args) { - f64 rhs = vm->num_to_float(args[1]); + f64 rhs = VAR_F(args[1]); if (rhs == 0) vm->ZeroDivisionError(); return VAR(_CAST(i64, args[0]) / rhs); }); _vm->bind_method<1>("float", "__truediv__", [](VM* vm, ArgsView args) { - f64 rhs = vm->num_to_float(args[1]); + f64 rhs = VAR_F(args[1]); if (rhs == 0) vm->ZeroDivisionError(); return VAR(_CAST(f64, args[0]) / rhs); }); @@ -254,7 +254,7 @@ inline void init_builtins(VM* _vm) { if(flag) return VAR((f64)(1.0 / ret)); return VAR(ret); }else{ - return VAR((f64)std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))); + return VAR((f64)std::pow(VAR_F(args[0]), VAR_F(args[1]))); } }; @@ -799,7 +799,7 @@ inline void add_module_time(VM* vm){ }); vm->bind_func<1>(mod, "sleep", [](VM* vm, ArgsView args) { - f64 seconds = FLOAT(args[0]); + f64 seconds = VAR_F(args[0]); auto begin = std::chrono::high_resolution_clock::now(); while(true){ auto now = std::chrono::high_resolution_clock::now(); @@ -850,15 +850,15 @@ inline void add_module_math(VM* vm){ mod->attr().set("inf", VAR(std::numeric_limits::infinity())); mod->attr().set("nan", VAR(std::numeric_limits::quiet_NaN())); - vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "ceil", CPP_LAMBDA(VAR((i64)std::ceil(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "fabs", CPP_LAMBDA(VAR(std::fabs(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "floor", CPP_LAMBDA(VAR((i64)std::floor(VAR_F(args[0]))))); vm->bind_func<1>(mod, "fsum", [](VM* vm, ArgsView args) { List& list = CAST(List&, args[0]); double sum = 0; double c = 0; for(PyObject* arg : list){ - double x = vm->num_to_float(arg); + double x = VAR_F(arg); double y = x - c; double t = sum + y; c = (t - sum) - y; @@ -879,29 +879,29 @@ inline void add_module_math(VM* vm){ return VAR(a); }); - vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "isfinite", CPP_LAMBDA(VAR(std::isfinite(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "isinf", CPP_LAMBDA(VAR(std::isinf(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "isnan", CPP_LAMBDA(VAR(std::isnan(VAR_F(args[0]))))); - vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "exp", CPP_LAMBDA(VAR(std::exp(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "log", CPP_LAMBDA(VAR(std::log(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "log2", CPP_LAMBDA(VAR(std::log2(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "log10", CPP_LAMBDA(VAR(std::log10(VAR_F(args[0]))))); - vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(vm->num_to_float(args[0]), vm->num_to_float(args[1]))))); - vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(vm->num_to_float(args[0]))))); + vm->bind_func<2>(mod, "pow", CPP_LAMBDA(VAR(std::pow(VAR_F(args[0]), VAR_F(args[1]))))); + vm->bind_func<1>(mod, "sqrt", CPP_LAMBDA(VAR(std::sqrt(VAR_F(args[0]))))); - vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(vm->num_to_float(args[0]))))); - vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(vm->num_to_float(args[0]), vm->num_to_float(args[1]))))); + vm->bind_func<1>(mod, "acos", CPP_LAMBDA(VAR(std::acos(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "asin", CPP_LAMBDA(VAR(std::asin(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "atan", CPP_LAMBDA(VAR(std::atan(VAR_F(args[0]))))); + vm->bind_func<2>(mod, "atan2", CPP_LAMBDA(VAR(std::atan2(VAR_F(args[0]), VAR_F(args[1]))))); - vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(vm->num_to_float(args[0]))))); - vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(vm->num_to_float(args[0]))))); + vm->bind_func<1>(mod, "cos", CPP_LAMBDA(VAR(std::cos(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "sin", CPP_LAMBDA(VAR(std::sin(VAR_F(args[0]))))); + vm->bind_func<1>(mod, "tan", CPP_LAMBDA(VAR(std::tan(VAR_F(args[0]))))); - vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(vm->num_to_float(args[0]) * 180 / 3.1415926535897932384))); - vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(vm->num_to_float(args[0]) * 3.1415926535897932384 / 180))); + vm->bind_func<1>(mod, "degrees", CPP_LAMBDA(VAR(VAR_F(args[0]) * 180 / 3.1415926535897932384))); + vm->bind_func<1>(mod, "radians", CPP_LAMBDA(VAR(VAR_F(args[0]) * 3.1415926535897932384 / 180))); } inline void add_module_dis(VM* vm){