From 41e70d4f9826c9ba128054ff09badab6b47de09b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 6 Dec 2023 12:59:31 +0800 Subject: [PATCH] update docs --- docs/quick-start/call.md | 26 +++++++++++++++++++++++++- docs/quick-start/misc.md | 23 +++++++++++++++++++++++ docs/retype.yml | 2 +- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 docs/quick-start/misc.md diff --git a/docs/quick-start/call.md b/docs/quick-start/call.md index 06ec0ef4..2e3e06ad 100644 --- a/docs/quick-start/call.md +++ b/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(vm, i64); -``` \ No newline at end of file +``` + +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()); +``` + diff --git a/docs/quick-start/misc.md b/docs/quick-start/misc.md new file mode 100644 index 00000000..687b2e9b --- /dev/null +++ b/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. diff --git a/docs/retype.yml b/docs/retype.yml index 72d85706..6bc0fb8b 100644 --- a/docs/retype.yml +++ b/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: