This commit is contained in:
blueloveTH 2024-04-26 15:56:10 +08:00
parent 6189aaca94
commit 6fcb215f8e

View File

@ -141,7 +141,7 @@ Access extended python types
```cpp ```cpp
// VoidP was defined by `PY_CLASS` macro // VoidP was defined by `PY_CLASS` macro
PyObject* voidp_t = VoidP::_type(vm); Type voidp_t = VoidP::_type(vm);
``` ```
Check if an object is a python type Check if an object is a python type
@ -161,9 +161,9 @@ PyObject* t = vm->_t(obj); // <class 'int'>
Convert a type object into a type index Convert a type object into a type index
```cpp ```cpp
PyObject* int_t = vm->_t(vm->tp_int); PyObject* int_t = vm->_t(VM::tp_int);
Type t = PK_OBJ_GET(Type, int_t); Type t = PK_OBJ_GET(Type, int_t);
// t == vm->tp_int // t == VM::tp_int
``` ```
## Access attributes ## Access attributes
@ -238,21 +238,21 @@ Convert a python object to string
```cpp ```cpp
PyObject* obj = py_var(vm, 123); PyObject* obj = py_var(vm, 123);
PyObject* s = vm->py_str(obj); // 123 obj = vm->py_str(obj); // 123
``` ```
Get the string representation of a python object Get the string representation of a python object
```cpp ```cpp
PyObject* obj = py_var(vm, "123"); PyObject* obj = py_var(vm, "123");
std::cout << vm->py_repr(obj); // '123' obj = vm->py_repr(obj); // "'123'"
``` ```
Get the JSON representation of a python object Get the JSON representation of a python object
```cpp ```cpp
PyObject* obj = py_var(vm, 123); PyObject* obj = py_var(vm, 123);
std::cout << vm->py_json(obj); // "123" obj = vm->py_json(obj); // "123"
``` ```
Get the hash value of a python object Get the hash value of a python object
@ -275,6 +275,8 @@ Get the next item of an iterator
PyObject* obj = vm->py_next(iter); PyObject* obj = vm->py_next(iter);
if(obj == vm->StopIteration){ if(obj == vm->StopIteration){
// end of iteration // end of iteration
}else{
// process obj
} }
``` ```
@ -295,16 +297,6 @@ vm->bind(obj, "add(a: int, b: int) -> int", [](VM* vm, ArgsView args){
int b = py_cast<int>(vm, args[1]); int b = py_cast<int>(vm, args[1]);
return py_var(vm, a + b); return py_var(vm, a + b);
}); });
// Bind a native function with docstring
vm->bind(obj,
"add(a: int, b: int) -> int",
"add two integers", [](VM* vm, ArgsView args){
int a = py_cast<int>(vm, args[0]);
int b = py_cast<int>(vm, args[1]);
return py_var(vm, a + b);
});
``` ```
Bind a property Bind a property