This commit is contained in:
blueloveTH 2023-07-14 21:44:44 +08:00
parent 19ee02d2ca
commit cb6d302b2f
14 changed files with 27 additions and 67 deletions

View File

@ -1,3 +1,3 @@
label: Legacy C-API label: C-API
icon: code icon: code
order: 1 order: 1

View File

@ -4,13 +4,17 @@ icon: dot
order: 10 order: 10
--- ---
We take a lot of inspiration from the lua api for these bindings. ### What C-API is for
The key difference being most methods return a bool,
true if it succeeded false if it did not.
!!! The C-APIs are designed for these purposes:
Special thanks for [@koltenpearson](https://github.com/koltenpearson) for bringing us the Lua Style API implementation.
!!! 1. Your target platform does not support C++17. You compile pkpy into a static library and use its exported C-APIs.
2. You want to write a native module that can be imported via `__import__` at runtime. By using C-APIs, the module is portable across different compilers without C++ ABI compatibility issues.
Our C-APIs take a lot of inspiration from the lua C-APIs.
Methods return a `bool` indicating if the operation succeeded or not.
Special thanks for [@koltenpearson](https://github.com/koltenpearson)'s contribution.
## Basic Functions ## Basic Functions

View File

@ -1,12 +0,0 @@
---
title: REPL
icon: dot
order: 8
---
#### `REPL* pkpy_new_repl(VM* vm)`
Create a REPL, using the given virtual machine as the backend.
#### `bool pkpy_repl_input(REPL* r, const char* line)`
Input a source line to an interactive console. Return true if need more lines.

View File

@ -1,40 +0,0 @@
---
title: VM
icon: dot
order: 10
---
#### `VM* pkpy_new_vm()`
Create a virtual machine.
#### `void pkpy_vm_add_module(VM* vm, const char* name, const char* source)`
Add a source module into a virtual machine.
#### `void pkpy_vm_exec(VM* vm, const char* source)`
Run a given source on a virtual machine.
#### `void pkpy_vm_exec_2(pkpy::VM* vm, const char* source, const char* filename, int mode, const char* module)`
Advanced version of `pkpy_vm_exec`.
#### `void pkpy_free(void* p)`
Free a pointer via `free`.
#### `void pkpy_delete_vm(VM* vm)`
Delete a virtual machine.
#### `void pkpy_delete_repl(REPL* repl)`
Delete a REPL.
#### `void pkpy_vm_compile(VM* vm, const char* source, const char* filename, int mode, bool* ok, char** res)`
Compile a source into bytecode and serialize it into a string.
+ `ok`: whether the compilation is successful.
+ `res`: if `ok` is true, `res` is the bytecode string, otherwise it is the error message.

View File

@ -1,3 +0,0 @@
label: Lua Style C-API
icon: code
order: 2

View File

@ -3,7 +3,7 @@ output: .retype
url: https://pocketpy.dev url: https://pocketpy.dev
branding: branding:
title: pocketpy title: pocketpy
label: v1.0 label: v1.1
logo: "./static/logo.png" logo: "./static/logo.png"
favicon: "./static/logo.png" favicon: "./static/logo.png"
meta: meta:

View File

@ -14,31 +14,41 @@
#define PK_EXPORT __declspec(dllexport) #define PK_EXPORT __declspec(dllexport)
#define PK_SUPPORT_DYLIB 1 #define PK_SUPPORT_DYLIB 1
#define PK_SYS_PLATFORM "win32"
#elif __EMSCRIPTEN__ #elif __EMSCRIPTEN__
#include <emscripten.h> #include <emscripten.h>
#define PK_EXPORT EMSCRIPTEN_KEEPALIVE #define PK_EXPORT EMSCRIPTEN_KEEPALIVE
#define PK_SUPPORT_DYLIB 0 #define PK_SUPPORT_DYLIB 0
#define PK_SYS_PLATFORM "emscripten"
#elif __APPLE__ #elif __APPLE__
#include <TargetConditionals.h> #include <TargetConditionals.h>
#include <dlfcn.h> #include <dlfcn.h>
#define PK_SUPPORT_DYLIB 2 #define PK_SUPPORT_DYLIB 2
#if TARGET_IPHONE_SIMULATOR #if TARGET_IPHONE_SIMULATOR
// iOS, tvOS, or watchOS Simulator // iOS, tvOS, or watchOS Simulator
#elif TARGET_OS_MACCATALYST #define PK_SYS_PLATFORM "ios"
// Mac's Catalyst (ports iOS API into Mac, like UIKit).
#elif TARGET_OS_IPHONE #elif TARGET_OS_IPHONE
// iOS, tvOS, or watchOS device // iOS, tvOS, or watchOS device
#define PK_SYS_PLATFORM "ios"
#elif TARGET_OS_MAC #elif TARGET_OS_MAC
// Other kinds of Apple platforms // Other kinds of Apple platforms
#define PK_SYS_PLATFORM "darwin"
#else #else
# error "Unknown Apple platform" # error "Unknown Apple platform"
#endif #endif
#define PK_EXPORT __attribute__((visibility("default"))) #define PK_EXPORT __attribute__((visibility("default")))
#elif __ANDROID__
#include <dlfcn.h>
#define PK_SUPPORT_DYLIB 2
#define PK_EXPORT __attribute__((visibility("default")))
#define PK_SYS_PLATFORM "android"
#elif __linux__ #elif __linux__
#include <dlfcn.h> #include <dlfcn.h>
#define PK_SUPPORT_DYLIB 2 #define PK_SUPPORT_DYLIB 2
#define PK_EXPORT __attribute__((visibility("default"))) #define PK_EXPORT __attribute__((visibility("default")))
#define PK_SYS_PLATFORM "linux"
#else #else
#define PK_EXPORT #define PK_EXPORT
#define PK_SUPPORT_DYLIB 0 #define PK_SUPPORT_DYLIB 0
#define PK_SYS_PLATFORM "unknown"
#endif #endif

View File

@ -1271,6 +1271,7 @@ void add_module_sys(VM* vm){
PyObject* mod = vm->new_module("sys"); PyObject* mod = vm->new_module("sys");
PyREPL::register_class(vm, mod); PyREPL::register_class(vm, mod);
vm->setattr(mod, "version", VAR(PK_VERSION)); vm->setattr(mod, "version", VAR(PK_VERSION));
vm->setattr(mod, "platform", VAR(PK_SYS_PLATFORM));
PyObject* stdout_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {}); PyObject* stdout_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});
PyObject* stderr_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {}); PyObject* stderr_ = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});

View File

@ -3,7 +3,7 @@
namespace pkpy { namespace pkpy {
REPL::REPL(VM* vm) : vm(vm){ REPL::REPL(VM* vm) : vm(vm){
vm->_stdout(vm, "pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") "); vm->_stdout(vm, "pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
vm->_stdout(vm, fmt("[", sizeof(void*)*8, " bit]" "\n")); vm->_stdout(vm, fmt("[", sizeof(void*)*8, " bit] on ", PK_SYS_PLATFORM "\n"));
vm->_stdout(vm, "https://github.com/blueloveTH/pocketpy" "\n"); vm->_stdout(vm, "https://github.com/blueloveTH/pocketpy" "\n");
vm->_stdout(vm, "Type \"exit()\" to exit." "\n"); vm->_stdout(vm, "Type \"exit()\" to exit." "\n");
} }