Kaynağa Gözat

update docs

blueloveTH 2 yıl önce
ebeveyn
işleme
41e70d4f98
3 değiştirilmiş dosya ile 49 ekleme ve 2 silme
  1. 25 1
      docs/quick-start/call.md
  2. 23 0
      docs/quick-start/misc.md
  3. 1 1
      docs/retype.yml

+ 25 - 1
docs/quick-start/call.md

@@ -44,4 +44,28 @@ And get the value,
 ```cpp
 PyObject* ret = vm->call_method(obj, "__getitem__", _0);
 std::cout << py_cast<int>(vm, i64);
-```
+```
+
+If you want to call with dynamic number of arguments,
+you should use `vm->vectorcall`. This is a low-level, stack-based API.
+
+1. First push the callable object to the stack.
+2. Push the `self` object to the stack. If there is no `self`, push `PY_NULL`.
+3. Push the arguments to the stack.
+4. Call `vm->vectorcall` with the number of arguments.
+
+```cpp
+PyObject* f_sum = vm->builtins->attr("sum");
+
+List args(N);   // a list of N arguments
+
+vm->s_data.push_back(f_print);
+vm->s_data.push_back(PY_NULL);  // self
+
+for(PyObject* arg : args) {
+    vm->s_data.push_back(arg);
+}
+
+PyObject* ret = vm->vectorcall(args.size());
+```
+

+ 23 - 0
docs/quick-start/misc.md

@@ -0,0 +1,23 @@
+---
+icon: dot
+label: 'Misc'
+order: 0
+---
+
+## The scope lock of gc
+
+Sometimes you need to use the following code to prevent the gc from collecting objects.
+
+```cpp
+auto _lock = vm->heap.gc_scope_lock()
+```
+
+The scope lock is required if you create a PyObject and then try to run python-level bytecodes.
+
+For example, you create a temporary object on the stack and then call `vm->py_str`.
+
+Because users can have an overload of `__str__`, the call process is unsafe.
+
+When the vm is running python-level bytecodes, gc may start and delete your temporary object.
+
+The scope lock prevents this from happening.

+ 1 - 1
docs/retype.yml

@@ -3,7 +3,7 @@ output: .retype
 url: https://pocketpy.dev
 branding:
   title: pocketpy
-  label: v1.3.1
+  label: v1.3.2
   logo: "./static/logo.png"
 favicon: "./static/logo.png"
 meta: