pocketpy/examples/2_module_and_binding.cpp
Mahbub Alam 6f8bf82e9a
Examples for beginners (#169)
* Add examples folder

* Draft change

* Add draft changes

* Add examples

* Refactor examples

---------

Co-authored-by: Mahbub Alam <alam.mahbub214@gmail.com>
2023-10-24 17:59:58 +08:00

37 lines
935 B
C++

/**
* This example demonstrate the process of creating a python module and
* bind a function to it, as well as the procedure for calling the function.
*/
#include "pocketpy.h"
using namespace pkpy;
int main(){
// Create a virtual machine
VM* vm = new VM();
// Create a module
PyObject* math_module = vm->new_module("math");
// Bind a function named "add" to the module
vm->bind(math_module, "add(a: int, b: int) -> 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 "add" function
PyObject* f_sum = math_module->attr("add");
PyObject* result = vm->call(f_sum, py_var(vm, 4), py_var(vm, 5));
std::cout << "Sum: " << py_cast<int>(vm, result) << std::endl; // 9
// Dispose the virtual machine
delete vm;
return 0;
}