mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
update docs
This commit is contained in:
parent
ec0290ab39
commit
41e70d4f98
@ -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
docs/quick-start/misc.md
Normal file
23
docs/quick-start/misc.md
Normal file
@ -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.
|
@ -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:
|
||||
|
Loading…
x
Reference in New Issue
Block a user