diff --git a/README.md b/README.md index b049681f..6bbbf02c 100644 --- a/README.md +++ b/README.md @@ -64,7 +64,7 @@ Check [C-API](https://pocketpy.dev/c-api/vm/) for references. For further custom int main(){ // Create a virtual machine - auto vm = pkpy_new_vm(true); + auto vm = pkpy_new_vm(); // Hello world! pkpy_vm_exec(vm, "print('Hello world!')"); diff --git a/README_zh.md b/README_zh.md index e3d9eff1..a3acf1c2 100644 --- a/README_zh.md +++ b/README_zh.md @@ -54,7 +54,7 @@ PocketPy是一个轻量级的Python解释器,为嵌入至游戏引擎而设计 int main(){ // 创建一个虚拟机 - auto vm = pkpy_new_vm(true); + auto vm = pkpy_new_vm(); // Hello world! pkpy_vm_exec(vm, "print('Hello world!')"); diff --git a/docs/plugins/c.md b/docs/plugins/c.md index 412991a4..e60ce7b9 100644 --- a/docs/plugins/c.md +++ b/docs/plugins/c.md @@ -15,7 +15,7 @@ https://github.com/blueloveTH/pocketpy/releases/latest int main(){ // Create a virtual machine - auto vm = pkpy_new_vm(true); + auto vm = pkpy_new_vm(); // Hello world! pkpy_vm_exec(vm, "print('Hello world!')"); diff --git a/docs/quick-start/03_attr.md b/docs/quick-start/attr.md similarity index 100% rename from docs/quick-start/03_attr.md rename to docs/quick-start/attr.md diff --git a/docs/quick-start/05_bind.md b/docs/quick-start/bind.md similarity index 93% rename from docs/quick-start/05_bind.md rename to docs/quick-start/bind.md index 11ad6abc..78705391 100644 --- a/docs/quick-start/05_bind.md +++ b/docs/quick-start/bind.md @@ -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 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. ```cpp -VM* vm = pkpy_new_vm(true); +VM* vm = pkpy_new_vm(); vm->bind_builtin_func<0>("input", [](VM* vm, ArgsView args){ static std::string line; std::getline(std::cin, line); diff --git a/docs/quick-start/04_call.md b/docs/quick-start/call.md similarity index 100% rename from docs/quick-start/04_call.md rename to docs/quick-start/call.md diff --git a/docs/quick-start/01_installation.md b/docs/quick-start/installation.md similarity index 100% rename from docs/quick-start/01_installation.md rename to docs/quick-start/installation.md diff --git a/docs/quick-start/02_interop.md b/docs/quick-start/interop.md similarity index 100% rename from docs/quick-start/02_interop.md rename to docs/quick-start/interop.md diff --git a/docs/quick-start/overview.md b/docs/quick-start/overview.md new file mode 100644 index 00000000..bbe9bb82 --- /dev/null +++ b/docs/quick-start/overview.md @@ -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(); +``` \ No newline at end of file diff --git a/docs/quick-start/06_wrap.md b/docs/quick-start/wrap.md similarity index 100% rename from docs/quick-start/06_wrap.md rename to docs/quick-start/wrap.md diff --git a/src/main.cpp b/src/main.cpp index cee05cfc..282d4522 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,7 +6,7 @@ #ifndef __EMSCRIPTEN__ 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){ return VAR(pkpy::getline()); }); diff --git a/src/pocketpy.h b/src/pocketpy.h index bf93a0bb..e3e27414 100644 --- a/src/pocketpy.h +++ b/src/pocketpy.h @@ -973,7 +973,7 @@ extern "C" { __EXPORT /// 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); } diff --git a/src/test.cpp b/src/test.cpp deleted file mode 100644 index 1f2627c2..00000000 --- a/src/test.cpp +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/src/vm.h b/src/vm.h index ca0a8c1e..7f89d41a 100644 --- a/src/vm.h +++ b/src/vm.h @@ -106,7 +106,7 @@ public: 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->_stdout = use_stdio ? &std::cout : &_stdout_buffer; this->_stderr = use_stdio ? &std::cerr : &_stderr_buffer; diff --git a/web/index.js b/web/index.js index 2621b788..27029a0a 100644 --- a/web/index.js +++ b/web/index.js @@ -113,7 +113,7 @@ var Module = { term.write(text + "\r\n"); }, '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]); term.write(need_more_lines ? "... " : ">>> "); },