blueloveTH 2 lat temu
rodzic
commit
13debcd722
5 zmienionych plików z 18 dodań i 19 usunięć
  1. 11 12
      build.py
  2. 3 1
      docs/features/ub.md
  3. 1 1
      docs/quick-start/bind.md
  4. 3 3
      docs/quick-start/wrap.md
  5. 0 2
      python/builtins.py

+ 11 - 12
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()

+ 3 - 1
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.
+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`.

+ 1 - 1
docs/quick-start/bind.md

@@ -9,7 +9,7 @@ In `VM` class, there are 4 methods to bind native function.
 + `VM::bind_func<ARGC>`
 + `VM::bind_builtin_func<ARGC>`
 + `VM::bind_method<ARGC>`
-+ `VM::bind_static_method<ARGC>`
++ `VM::bind_constructor<ARGC>`
 
 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`.
 

+ 3 - 3
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);
         });
 

+ 0 - 2
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]) + ']'