#
Miscellaneous
#
The scope lock of gc
Sometimes you need to use the following code to prevent the gc from collecting objects.
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_next
.
void some_func(VM* vm){
PyObject* obj = VAR(List(5));
// unsafe
PyObject iter = vm->py_iter(obj);
PyObject next = vm->py_next(iter);
}
Because users can have an overload of __next__
, this call 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.
void some_func(VM* vm){
PyObject* obj = VAR(List(5));
// safe
auto _lock = vm->heap.gc_scope_lock();
PyObject iter = vm->py_iter(obj);
PyObject next = vm->py_next(iter);
}