This commit is contained in:
blueloveTH 2023-06-10 12:18:58 +08:00
parent ddd841f0c6
commit a484879b14
4 changed files with 15 additions and 9 deletions

View File

@ -43,7 +43,7 @@ As you can see, direct access does not take care of derived attributes or method
In most cases, what you need is `getattr` and `setattr`. In most cases, what you need is `getattr` and `setattr`.
These two methods handle all possible cases. These two methods handle all possible cases.
#### `PyObject* VM::getattr(PyObject* obj, StrName name, bool throw_err=true)` #### `PyObject* getattr(PyObject* obj, StrName name, bool throw_err=true)`
This method is equivalent to `getattr` in python. This method is equivalent to `getattr` in python.
If the attribute is not found, it will return `nullptr` If the attribute is not found, it will return `nullptr`
@ -64,7 +64,7 @@ int result = CAST(int, ret);
std::cout << result << std::endl; // 3 std::cout << result << std::endl; // 3
``` ```
#### `void VM::setattr(PyObject*, StrName, PyObject*)` #### `void setattr(PyObject*, StrName, PyObject*)`
This method is equivalent to `setattr` in python. This method is equivalent to `setattr` in python.
It raises `TypeError` if the object does not support attribute assignment. It raises `TypeError` if the object does not support attribute assignment.

View File

@ -10,8 +10,8 @@ You can use `call` to invoke any python callable object,
including functions, methods, classes, etc. including functions, methods, classes, etc.
For methods, `call_method` can be used. For methods, `call_method` can be used.
+ `PyObject* VM::call(PyObject* obj, ...)` + `PyObject* call(PyObject* obj, ...)`
+ `PyObject* VM::call_method(PyObject* obj, StrName name, ...)` + `PyObject* call_method(PyObject* obj, StrName name, ...)`
### Exmaple ### Exmaple

View File

@ -79,7 +79,7 @@ List& list = CAST(List&, obj);
### Check type of `PyObject*` ### Check type of `PyObject*`
Each `PyObject*` has a `Type` field to indicate its type. Each `PyObject*` has a `Type` field to indicate its type.
`Type` is just an integer which is the global index in `VM::_all_types`. `Type` is just an integer which is the global index in `vm->_all_types`.
`VM` class has a set of predefined `Type` constants for quick access. `VM` class has a set of predefined `Type` constants for quick access.
They are prefixed by `tp_`. For example, `tp_object`(object), They are prefixed by `tp_`. For example, `tp_object`(object),
@ -115,5 +115,5 @@ Other variants are designed for specific types and are faster.
You can also use `check_` prefix functions assert the type of a `PyObject*`, You can also use `check_` prefix functions assert the type of a `PyObject*`,
which will throw `TypeError` on failure. which will throw `TypeError` on failure.
+ `void VM::check_type(PyObject* obj, Type type)` + `void check_type(PyObject* obj, Type type)`
+ `void VM::check_non_tagged_type(PyObject* obj, Type type)` + `void check_non_tagged_type(PyObject* obj, Type type)`

View File

@ -53,7 +53,7 @@ When you do `import` a module, the VM will try to find it in the following order
1. Search `vm->_modules`, if found, return it. 1. Search `vm->_modules`, if found, return it.
2. Search `vm->_lazy_modules`, if found, compile and execute it, then return it. 2. Search `vm->_lazy_modules`, if found, compile and execute it, then return it.
3. Search the working directory and try to load it from file system via `vm->_import_handler`. 3. Try `vm->_import_handler`.
### Customized import handler ### Customized import handler
@ -76,4 +76,10 @@ inline Bytes _default_import_handler(const Str& name){
fclose(fp); fclose(fp);
return Bytes(std::move(buffer)); return Bytes(std::move(buffer));
}; };
``` ```
### Import module via cpp
You can use `vm->py_import` to import a module.
This is equivalent to `import` in python.
Return the module object if success.