diff --git a/docs/LuaC-API/call.md b/docs/C-API/call.md similarity index 100% rename from docs/LuaC-API/call.md rename to docs/C-API/call.md diff --git a/docs/LuaC-API/error.md b/docs/C-API/error.md similarity index 100% rename from docs/LuaC-API/error.md rename to docs/C-API/error.md diff --git a/docs/C-API/index.yml b/docs/C-API/index.yml index 9fb3bab8..15195ecd 100644 --- a/docs/C-API/index.yml +++ b/docs/C-API/index.yml @@ -1,3 +1,3 @@ -label: Legacy C-API +label: C-API icon: code order: 1 \ No newline at end of file diff --git a/docs/LuaC-API/introduction.md b/docs/C-API/introduction.md similarity index 57% rename from docs/LuaC-API/introduction.md rename to docs/C-API/introduction.md index 5c2fede1..f3e5aba6 100644 --- a/docs/LuaC-API/introduction.md +++ b/docs/C-API/introduction.md @@ -4,13 +4,17 @@ icon: dot order: 10 --- -We take a lot of inspiration from the lua api for these bindings. -The key difference being most methods return a bool, -true if it succeeded false if it did not. +### What C-API is for -!!! -Special thanks for [@koltenpearson](https://github.com/koltenpearson) for bringing us the Lua Style API implementation. -!!! +The C-APIs are designed for these purposes: + +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 diff --git a/docs/C-API/repl.md b/docs/C-API/repl.md deleted file mode 100644 index a1713fb0..00000000 --- a/docs/C-API/repl.md +++ /dev/null @@ -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. \ No newline at end of file diff --git a/docs/LuaC-API/stack.md b/docs/C-API/stack.md similarity index 100% rename from docs/LuaC-API/stack.md rename to docs/C-API/stack.md diff --git a/docs/LuaC-API/types.md b/docs/C-API/types.md similarity index 100% rename from docs/LuaC-API/types.md rename to docs/C-API/types.md diff --git a/docs/LuaC-API/variables.md b/docs/C-API/variables.md similarity index 100% rename from docs/LuaC-API/variables.md rename to docs/C-API/variables.md diff --git a/docs/C-API/vm.md b/docs/C-API/vm.md deleted file mode 100644 index e38e3a0b..00000000 --- a/docs/C-API/vm.md +++ /dev/null @@ -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. \ No newline at end of file diff --git a/docs/LuaC-API/index.yml b/docs/LuaC-API/index.yml deleted file mode 100644 index 38d435a3..00000000 --- a/docs/LuaC-API/index.yml +++ /dev/null @@ -1,3 +0,0 @@ -label: Lua Style C-API -icon: code -order: 2 \ No newline at end of file diff --git a/docs/retype.yml b/docs/retype.yml index ab7ee2c7..d008e27c 100644 --- a/docs/retype.yml +++ b/docs/retype.yml @@ -3,7 +3,7 @@ output: .retype url: https://pocketpy.dev branding: title: pocketpy - label: v1.0 + label: v1.1 logo: "./static/logo.png" favicon: "./static/logo.png" meta: diff --git a/include/pocketpy/export.h b/include/pocketpy/export.h index 39a253c4..6b4c7674 100644 --- a/include/pocketpy/export.h +++ b/include/pocketpy/export.h @@ -14,31 +14,41 @@ #define PK_EXPORT __declspec(dllexport) #define PK_SUPPORT_DYLIB 1 + #define PK_SYS_PLATFORM "win32" #elif __EMSCRIPTEN__ #include #define PK_EXPORT EMSCRIPTEN_KEEPALIVE #define PK_SUPPORT_DYLIB 0 + #define PK_SYS_PLATFORM "emscripten" #elif __APPLE__ #include #include #define PK_SUPPORT_DYLIB 2 #if TARGET_IPHONE_SIMULATOR - // iOS, tvOS, or watchOS Simulator - #elif TARGET_OS_MACCATALYST - // Mac's Catalyst (ports iOS API into Mac, like UIKit). + // iOS, tvOS, or watchOS Simulator + #define PK_SYS_PLATFORM "ios" #elif TARGET_OS_IPHONE // iOS, tvOS, or watchOS device + #define PK_SYS_PLATFORM "ios" #elif TARGET_OS_MAC // Other kinds of Apple platforms + #define PK_SYS_PLATFORM "darwin" #else # error "Unknown Apple platform" #endif #define PK_EXPORT __attribute__((visibility("default"))) +#elif __ANDROID__ + #include + #define PK_SUPPORT_DYLIB 2 + #define PK_EXPORT __attribute__((visibility("default"))) + #define PK_SYS_PLATFORM "android" #elif __linux__ #include #define PK_SUPPORT_DYLIB 2 #define PK_EXPORT __attribute__((visibility("default"))) + #define PK_SYS_PLATFORM "linux" #else #define PK_EXPORT #define PK_SUPPORT_DYLIB 0 + #define PK_SYS_PLATFORM "unknown" #endif \ No newline at end of file diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index c5e4604b..4296946f 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1271,6 +1271,7 @@ void add_module_sys(VM* vm){ PyObject* mod = vm->new_module("sys"); PyREPL::register_class(vm, mod); vm->setattr(mod, "version", VAR(PK_VERSION)); + vm->setattr(mod, "platform", VAR(PK_SYS_PLATFORM)); PyObject* stdout_ = vm->heap.gcnew(vm->tp_object, {}); PyObject* stderr_ = vm->heap.gcnew(vm->tp_object, {}); diff --git a/src/repl.cpp b/src/repl.cpp index 815f98c3..d9b9033f 100644 --- a/src/repl.cpp +++ b/src/repl.cpp @@ -3,7 +3,7 @@ namespace pkpy { REPL::REPL(VM* vm) : vm(vm){ 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, "Type \"exit()\" to exit." "\n"); }