This commit is contained in:
blueloveTH 2023-04-24 17:41:30 +08:00
parent dd27071b22
commit a97149778a
15 changed files with 25 additions and 34 deletions

View File

@ -64,7 +64,7 @@ Check [C-API](https://pocketpy.dev/c-api/vm/) for references. For further custom
int main(){ int main(){
// Create a virtual machine // Create a virtual machine
auto vm = pkpy_new_vm(true); auto vm = pkpy_new_vm();
// Hello world! // Hello world!
pkpy_vm_exec(vm, "print('Hello world!')"); pkpy_vm_exec(vm, "print('Hello world!')");

View File

@ -54,7 +54,7 @@ PocketPy是一个轻量级的Python解释器为嵌入至游戏引擎而设计
int main(){ int main(){
// 创建一个虚拟机 // 创建一个虚拟机
auto vm = pkpy_new_vm(true); auto vm = pkpy_new_vm();
// Hello world! // Hello world!
pkpy_vm_exec(vm, "print('Hello world!')"); pkpy_vm_exec(vm, "print('Hello world!')");

View File

@ -15,7 +15,7 @@ https://github.com/blueloveTH/pocketpy/releases/latest
int main(){ int main(){
// Create a virtual machine // Create a virtual machine
auto vm = pkpy_new_vm(true); auto vm = pkpy_new_vm();
// Hello world! // Hello world!
pkpy_vm_exec(vm, "print('Hello world!')"); pkpy_vm_exec(vm, "print('Hello world!')");

View File

@ -19,7 +19,7 @@ Native functions do not support keyword arguments.
!!! !!!
PkPy uses a universal C function pointer for native functions: pkpy uses a universal C function pointer for native functions:
```cpp ```cpp
typedef PyObject* (*NativeFuncC)(VM*, ArgsView); typedef PyObject* (*NativeFuncC)(VM*, ArgsView);
@ -34,7 +34,7 @@ The return value is a `PyObject*`, which should not be `nullptr`. If there is no
This is an example of binding the `input()` function to the `builtins` module. This is an example of binding the `input()` function to the `builtins` module.
```cpp ```cpp
VM* vm = pkpy_new_vm(true); VM* vm = pkpy_new_vm();
vm->bind_builtin_func<0>("input", [](VM* vm, ArgsView args){ vm->bind_builtin_func<0>("input", [](VM* vm, ArgsView args){
static std::string line; static std::string line;
std::getline(std::cin, line); std::getline(std::cin, line);

View File

@ -0,0 +1,16 @@
---
icon: code
label: 'Overview'
order: 95
---
pkpy's C++ interfaces are organized in an object-oriented way.
All classes are located in `pkpy` namespace.
The most important class is the `VM` class. A `VM` instance is a python virtual machine which holds all necessary runtime states, including callstacks, modules, variables, etc.
You need to use the C++ `new` operator to create a `VM` instance.
```cpp
VM* vm = new VM();
```

View File

@ -6,7 +6,7 @@
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
int main(int argc, char** argv){ int main(int argc, char** argv){
pkpy::VM* vm = pkpy_new_vm(true, true); pkpy::VM* vm = pkpy_new_vm();
vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::ArgsView args){ vm->bind_builtin_func<0>("input", [](pkpy::VM* vm, pkpy::ArgsView args){
return VAR(pkpy::getline()); return VAR(pkpy::getline());
}); });

View File

@ -973,7 +973,7 @@ extern "C" {
__EXPORT __EXPORT
/// Create a virtual machine. /// Create a virtual machine.
pkpy::VM* pkpy_new_vm(bool use_stdio, bool enable_os){ pkpy::VM* pkpy_new_vm(bool use_stdio=true, bool enable_os=true){
return PKPY_ALLOCATE(pkpy::VM, use_stdio, enable_os); return PKPY_ALLOCATE(pkpy::VM, use_stdio, enable_os);
} }

View File

@ -1,25 +0,0 @@
#include "cffi.h"
#include "pocketpy.h"
using namespace pkpy;
float* f(int* a){
*a = 100;
return new float(3.5f);
}
int main(){
VM* vm = pkpy_new_vm(true);
vm->bind_builtin_func<1>("f", NativeProxyFunc(&f));
pkpy_vm_exec(vm, R"(
from c import *
p = cast(malloc(4), "int*")
ret = f(p)
print(p.get()) # 100
print(ret, ret.get()) # 3.5
)");
pkpy_delete(vm);
return 0;
}

View File

@ -106,7 +106,7 @@ public:
const bool enable_os; const bool enable_os;
VM(bool use_stdio, bool enable_os) : heap(this), enable_os(enable_os) { VM(bool use_stdio=true, bool enable_os=true) : heap(this), enable_os(enable_os) {
this->vm = this; this->vm = this;
this->_stdout = use_stdio ? &std::cout : &_stdout_buffer; this->_stdout = use_stdio ? &std::cout : &_stdout_buffer;
this->_stderr = use_stdio ? &std::cerr : &_stderr_buffer; this->_stderr = use_stdio ? &std::cerr : &_stderr_buffer;

View File

@ -113,7 +113,7 @@ var Module = {
term.write(text + "\r\n"); term.write(text + "\r\n");
}, },
'onRuntimeInitialized': function(text) { 'onRuntimeInitialized': function(text) {
var vm = Module.ccall('pkpy_new_vm', 'number', ['boolean'], [true]); var vm = Module.ccall('pkpy_new_vm', 'number', [], []);
repl = Module.ccall('pkpy_new_repl', 'number', ['number'], [vm]); repl = Module.ccall('pkpy_new_repl', 'number', ['number'], [vm]);
term.write(need_more_lines ? "... " : ">>> "); term.write(need_more_lines ? "... " : ">>> ");
}, },