mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
...
This commit is contained in:
parent
59c6252503
commit
b9e2a9450c
80
docs/quick-start/modules.md
Normal file
80
docs/quick-start/modules.md
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
---
|
||||||
|
icon: dot
|
||||||
|
label: 'Create modules'
|
||||||
|
order: 50
|
||||||
|
---
|
||||||
|
|
||||||
|
Modules are stored in `vm->_modules` and `vm->_lazy_modules`.
|
||||||
|
They are both dict-like objects.
|
||||||
|
|
||||||
|
### Lazy modules
|
||||||
|
|
||||||
|
A lazy module is a python source file.
|
||||||
|
It is compiled and executed when it is imported.
|
||||||
|
Use `[]` operator to add a lazy module.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
vm->_lazy_modules["test"] = "pi = 3.14";
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
import test
|
||||||
|
print(test.pi) # 3.14
|
||||||
|
```
|
||||||
|
|
||||||
|
### Native modules
|
||||||
|
|
||||||
|
A native module is a module written in c++ or mixed c++/python.
|
||||||
|
Native modules are always compiled and executed when the VM is created.
|
||||||
|
|
||||||
|
To creata a native module,
|
||||||
|
use `vm->new_module(...)`.
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
PyObject* mod = vm->new_module("test");
|
||||||
|
mod->attr().set("pi", VAR(3.14));
|
||||||
|
|
||||||
|
vm->bind_func<2>(mod, "add", [](VM* vm, ArgsView args){
|
||||||
|
i64 a = CAST(i64, args[0]);
|
||||||
|
i64 b = CAST(i64, args[1]);
|
||||||
|
return VAR(a + b);
|
||||||
|
});
|
||||||
|
```
|
||||||
|
|
||||||
|
```python
|
||||||
|
import test
|
||||||
|
print(test.pi) # 3.14
|
||||||
|
print(test.add(1, 2)) # 3
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
|
### Module resolution
|
||||||
|
|
||||||
|
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.
|
||||||
|
2. Search `vm->_lazy_modules`, if found, compile and execute it, then return it.
|
||||||
|
3. Search `vm->_path` and try to load it from file system.
|
||||||
|
|
||||||
|
|
||||||
|
### Filesystem hook
|
||||||
|
|
||||||
|
You can use `set_read_file_cwd` to provide a custom filesystem hook, which is used for `import` (3rd step).
|
||||||
|
The default implementation is:
|
||||||
|
|
||||||
|
```cpp
|
||||||
|
set_read_file_cwd([](const Str& name){
|
||||||
|
std::filesystem::path path(name.sv());
|
||||||
|
bool exists = std::filesystem::exists(path);
|
||||||
|
if(!exists) return Bytes();
|
||||||
|
std::string cname = name.str();
|
||||||
|
FILE* fp = fopen(cname.c_str(), "rb");
|
||||||
|
if(!fp) return Bytes();
|
||||||
|
fseek(fp, 0, SEEK_END);
|
||||||
|
std::vector<char> buffer(ftell(fp));
|
||||||
|
fseek(fp, 0, SEEK_SET);
|
||||||
|
fread(buffer.data(), 1, buffer.size(), fp);
|
||||||
|
fclose(fp);
|
||||||
|
return Bytes(std::move(buffer));
|
||||||
|
});
|
||||||
|
```
|
@ -67,12 +67,6 @@ struct Vector2 {
|
|||||||
Vector2& other = CAST(Vector2&, args[1]);
|
Vector2& other = CAST(Vector2&, args[1]);
|
||||||
return VAR(self.x == other.x && self.y == other.y);
|
return VAR(self.x == other.x && self.y == other.y);
|
||||||
});
|
});
|
||||||
|
|
||||||
vm->bind_method<1>(type, "__ne__", [](VM* vm, ArgsView args){
|
|
||||||
Vector2& self = CAST(Vector2&, args[0]);
|
|
||||||
Vector2& other = CAST(Vector2&, args[1]);
|
|
||||||
return VAR(self.x != other.x || self.y != other.y);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user