diff --git a/build.py b/build.py index 50499eb2..d0e979f7 100644 --- a/build.py +++ b/build.py @@ -12,13 +12,14 @@ linux_common = "-Wfatal-errors --std=c++17 -O2 -Wall -Wno-sign-compare -Wno-unus linux_cmd = "clang++ -o pocketpy src/main.cpp " + linux_common linux_lib_cmd = "clang++ -fPIC -shared -o pocketpy.so src/tmp.cpp " + linux_common +class LibBuildEnv: + def __enter__(self): + with open("src/tmp.cpp", "w", encoding='utf-8') as f: + f.write('#include "pocketpy.h"') -def lib_pre_build(): - with open("src/tmp.cpp", "w", encoding='utf-8') as f: - f.write('#include "pocketpy.h"') - -def lib_post_build(): - os.remove("src/tmp.cpp") + def __exit__(self, *args): + if os.path.exists("src/tmp.cpp"): + os.remove("src/tmp.cpp") windows_common = "CL -std:c++17 /utf-8 -GR- -EHsc -O2" windows_cmd = windows_common + " -Fe:pocketpy src/main.cpp" @@ -30,18 +31,16 @@ if sys.argv.__len__() == 1: if "windows" in sys.argv: if "-lib" in sys.argv: - lib_pre_build() - os.system(windows_lib_cmd) - lib_post_build() + with LibBuildEnv(): + os.system(windows_lib_cmd) else: os.system(windows_cmd) DONE() if "linux" in sys.argv: if "-lib" in sys.argv: - lib_pre_build() - os.system(linux_lib_cmd) - lib_post_build() + with LibBuildEnv(): + os.system(linux_lib_cmd) else: os.system(linux_cmd) DONE() diff --git a/docs/features/ub.md b/docs/features/ub.md index 6eadedac..da7f8120 100644 --- a/docs/features/ub.md +++ b/docs/features/ub.md @@ -7,4 +7,6 @@ These are the undefined behaviours of pkpy. The behaviour of pkpy is undefined i 1. Delete a builtin object. For example, `del int.__add__`. 2. Call an unbound method with the wrong type of `self`. For example, `int.__add__('1', 2)`. -3. Use goto statement to jump out of a context block. \ No newline at end of file +3. Use goto statement to jump out of a context block. +4. Type `T`'s `__new__` returns an object that is not an instance of `T`. +5. Call `__new__` with a type that is not a subclass of `type`. \ No newline at end of file diff --git a/docs/quick-start/bind.md b/docs/quick-start/bind.md index 3f2c4518..ebd08bc6 100644 --- a/docs/quick-start/bind.md +++ b/docs/quick-start/bind.md @@ -9,7 +9,7 @@ In `VM` class, there are 4 methods to bind native function. + `VM::bind_func` + `VM::bind_builtin_func` + `VM::bind_method` -+ `VM::bind_static_method` ++ `VM::bind_constructor` They are all template methods, the template argument is a `int` number, indicating the argument count. For variadic arguments, use `-1`. For methods, `ARGC` do not include `self`. diff --git a/docs/quick-start/wrap.md b/docs/quick-start/wrap.md index 7747b79c..816d0b5c 100644 --- a/docs/quick-start/wrap.md +++ b/docs/quick-start/wrap.md @@ -25,9 +25,9 @@ struct Vector2 { Vector2(float x, float y) : x(x), y(y) {} static void _register(VM* vm, PyObject* mod, PyObject* type){ - vm->bind_static_method<2>(type, "__new__", [](VM* vm, ArgsView args){ - float x = vm->num_to_float(args[0]); - float y = vm->num_to_float(args[1]); + 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]); return VAR_T(Vector2, x, y); }); diff --git a/python/builtins.py b/python/builtins.py index a3275a6d..e12e0f8f 100644 --- a/python/builtins.py +++ b/python/builtins.py @@ -108,8 +108,6 @@ def str@strip(self, chars=None): return self[i:j+1] ##### list ##### - -list.__new__ = lambda iterable: [x for x in iterable] list.__repr__ = lambda self: '[' + ', '.join([repr(i) for i in self]) + ']' tuple.__repr__ = lambda self: '(' + ', '.join([repr(i) for i in self]) + ')' list.__json__ = lambda self: '[' + ', '.join([i.__json__() for i in self]) + ']'