|
|
@@ -9,7 +9,7 @@ You have two options to integrate pkpy into your project.
|
|
|
#### Use the single header file
|
|
|
|
|
|
Download the `pocketpy.h` on our [GitHub Release](https://github.com/blueloveTH/pocketpy/releases) page.
|
|
|
-And `#include` it in your project.
|
|
|
+And `#include` it in your project. It is recommended to use the latest dev version.
|
|
|
|
|
|
#### Use CMake
|
|
|
|
|
|
@@ -23,6 +23,11 @@ In your CMakelists.txt, add the following lines:
|
|
|
option(PK_BUILD_STATIC_LIB "Build static library" ON)
|
|
|
add_subdirectory(pocketpy)
|
|
|
target_link_libraries(your_target pocketpy)
|
|
|
+
|
|
|
+if(EMSCRIPTEN)
|
|
|
+ # exceptions must be enabled for emscripten
|
|
|
+ set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fexceptions")
|
|
|
+endif()
|
|
|
```
|
|
|
|
|
|
These variables can be set to control the build process:
|
|
|
@@ -39,6 +44,9 @@ To compile it with your project, these flags must be set:
|
|
|
+ `--std=c++17` flag must be set
|
|
|
+ Exception must be enabled
|
|
|
|
|
|
+For emscripten, you must enable exceptions to make pocketpy work properly.
|
|
|
+See https://emscripten.org/docs/porting/exceptions.html.
|
|
|
+
|
|
|
### Example
|
|
|
|
|
|
```cpp
|
|
|
@@ -49,18 +57,33 @@ using namespace pkpy;
|
|
|
int main(){
|
|
|
// Create a virtual machine
|
|
|
VM* vm = new VM();
|
|
|
-
|
|
|
+
|
|
|
// Hello world!
|
|
|
- vm->exec("print('Hello world!')", "main.py", EXEC_MODE);
|
|
|
+ vm->exec("print('Hello world!')");
|
|
|
|
|
|
// Create a list
|
|
|
- vm->exec("a = [1, 2, 3]", "main.py", EXEC_MODE);
|
|
|
+ vm->exec("a = [1, 2, 3]");
|
|
|
|
|
|
// Eval the sum of the list
|
|
|
- PyObject* result = vm->exec("sum(a)", "<eval>", EVAL_MODE);
|
|
|
- std::cout << CAST(int, result); // 6
|
|
|
+ PyObject* result = vm->eval("sum(a)");
|
|
|
+ std::cout << py_cast<int>(vm, result); // 6
|
|
|
+
|
|
|
+ // Bindings
|
|
|
+ vm->bind(vm->_main, "add(a: int, b: int)",
|
|
|
+ [](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);
|
|
|
+ });
|
|
|
+
|
|
|
+ // Call the function
|
|
|
+ PyObject* f_add = vm->_main->attr("add");
|
|
|
+ result = vm->call(f_add, py_var(vm, 3), py_var(vm, 7));
|
|
|
+ std::cout << py_cast<int>(vm, result); // 10
|
|
|
+
|
|
|
+ // Dispose the virtual machine
|
|
|
+ delete vm;
|
|
|
return 0;
|
|
|
-}
|
|
|
```
|
|
|
|
|
|
### Overview
|
|
|
@@ -74,7 +97,7 @@ A process can have multiple `VM` instances. Each `VM` instance is independent fr
|
|
|
|
|
|
!!!
|
|
|
Always use C++ `new` operator to create a `VM` instance.
|
|
|
-DO NOT declare it on the stack.
|
|
|
+DO NOT declare it on the stack. It may cause stack overflow.
|
|
|
!!!
|
|
|
|
|
|
```cpp
|
|
|
@@ -85,7 +108,7 @@ The constructor can take 1 extra parameters.
|
|
|
|
|
|
#### `VM(bool enable_os=true)`
|
|
|
|
|
|
-+ `enable_os`, whether to enable OS-related features or not. This setting controls the availability of priviledged modules such os `io` and `os` as well as builtin function `open`.
|
|
|
++ `enable_os`, whether to enable OS-related features or not. This setting controls the availability of priviledged modules such os `io` and `os` as well as builtin function `open`. **It is designed for sandboxing.**
|
|
|
|
|
|
When you are done with the `VM` instance, use `delete` operator to dispose it.
|
|
|
|