This commit is contained in:
blueloveTH 2023-05-24 19:01:47 +08:00
parent 5dd6fe2387
commit 3b66008e50
9 changed files with 125 additions and 98 deletions

View File

@ -9,7 +9,8 @@ pipeline = [
["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"], ["common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h", "lexer.h"],
["obj.h", "dict.h", "codeobject.h", "frame.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h"],
["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"], ["gc.h", "vm.h", "ceval.h", "expr.h", "compiler.h", "repl.h"],
["_generated.h", "cffi.h", "iter.h", "base64.h", "linalg.h", "easing.h", "requests.h", "io.h", "pocketpy.h"] ["_generated.h", "cffi.h", "iter.h", "base64.h", "linalg.h", "easing.h", "requests.h", "io.h"],
["export.h", "pocketpy.h"]
] ]
copied = set() copied = set()

View File

@ -1,3 +1,4 @@
#include "pocketpy.h"
#include "pocketpy_c.h" #include "pocketpy_c.h"
using namespace pkpy; using namespace pkpy;

View File

@ -1,14 +1,13 @@
#ifndef POCKETPY_C_H #ifndef POCKETPY_C_H
#define POCKETPY_C_H #define POCKETPY_C_H
#include "pocketpy.h"
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "export.h"
typedef struct pkpy_vm_handle pkpy_vm; typedef struct pkpy_vm_handle pkpy_vm;

View File

@ -100,10 +100,12 @@ void gc_marker_ex(CVM* vm) {
for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj); for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj);
} }
static OutputHandler stdout_handler = nullptr;
static OutputHandler stderr_handler = nullptr;
static void noop_output_handler(VM* vm, const Str& str) { void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler){
(void) vm; ::stdout_handler = stdout_handler;
(void) str; ::stderr_handler = stderr_handler;
} }
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) { pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
@ -113,8 +115,14 @@ pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex; vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex;
if (!use_stdio) { if (!use_stdio) {
vm->_stdout = noop_output_handler; vm->_stdout = [](VM* vm, const Str& s){
vm->_stderr = noop_output_handler; std::string str = s.str();
if (stdout_handler != nullptr) stdout_handler((pkpy_vm*)vm, str.c_str());
};
vm->_stderr = [](VM* vm, const Str& s){
std::string str = s.str();
if (stderr_handler != nullptr) stderr_handler((pkpy_vm*)vm, str.c_str());
};
} }
return (pkpy_vm*) vm; return (pkpy_vm*) vm;

View File

@ -7,6 +7,7 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "export.h"
typedef struct pkpy_vm_handle pkpy_vm; typedef struct pkpy_vm_handle pkpy_vm;
@ -19,93 +20,95 @@ typedef struct pkpy_vm_handle pkpy_vm;
//if pkpy_clear_error returns true it means there was an error and it was cleared, //if pkpy_clear_error returns true it means there was an error and it was cleared,
//it will provide a string summary of the error in the message parameter (if it is not NULL) //it will provide a string summary of the error in the message parameter (if it is not NULL)
//if null is passed in as message, and it will just print the message to stderr //if null is passed in as message, and it will just print the message to stderr
bool pkpy_clear_error(pkpy_vm*, char** message); PK_EXPORT bool pkpy_clear_error(pkpy_vm*, char** message);
//NOTE you are responsible for freeing message //NOTE you are responsible for freeing message
//this will cause the vm to enter an error state and report the given message //this will cause the vm to enter an error state and report the given message
//when queried //when queried
//note that at the moment this is more like a panic than throwing an error //note that at the moment this is more like a panic than throwing an error
//the user will not be able to catch it with python code //the user will not be able to catch it with python code
bool pkpy_error(pkpy_vm*, const char* message); PK_EXPORT bool pkpy_error(pkpy_vm*, const char* message);
PK_EXPORT pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os);
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os); PK_EXPORT bool pkpy_vm_run(pkpy_vm*, const char* source);
bool pkpy_vm_run(pkpy_vm*, const char* source); PK_EXPORT void pkpy_vm_destroy(pkpy_vm*);
void pkpy_vm_destroy(pkpy_vm*);
typedef int (*pkpy_function)(pkpy_vm*); typedef int (*pkpy_function)(pkpy_vm*);
bool pkpy_pop(pkpy_vm*, int n); PK_EXPORT bool pkpy_pop(pkpy_vm*, int n);
//push the item at index onto the top of the stack (as well as leaving it where //push the item at index onto the top of the stack (as well as leaving it where
//it is on the stack) //it is on the stack)
bool pkpy_push(pkpy_vm*, int index); PK_EXPORT bool pkpy_push(pkpy_vm*, int index);
bool pkpy_push_function(pkpy_vm*, pkpy_function); PK_EXPORT bool pkpy_push_function(pkpy_vm*, pkpy_function);
bool pkpy_push_int(pkpy_vm*, int); PK_EXPORT bool pkpy_push_int(pkpy_vm*, int);
bool pkpy_push_float(pkpy_vm*, double); PK_EXPORT bool pkpy_push_float(pkpy_vm*, double);
bool pkpy_push_bool(pkpy_vm*, bool); PK_EXPORT bool pkpy_push_bool(pkpy_vm*, bool);
bool pkpy_push_string(pkpy_vm*, const char*); PK_EXPORT bool pkpy_push_string(pkpy_vm*, const char*);
bool pkpy_push_stringn(pkpy_vm*, const char*, int length); PK_EXPORT bool pkpy_push_stringn(pkpy_vm*, const char*, int length);
bool pkpy_push_voidp(pkpy_vm*, void*); PK_EXPORT bool pkpy_push_voidp(pkpy_vm*, void*);
bool pkpy_push_none(pkpy_vm*); PK_EXPORT bool pkpy_push_none(pkpy_vm*);
bool pkpy_set_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_set_global(pkpy_vm*, const char* name);
bool pkpy_get_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_get_global(pkpy_vm*, const char* name);
//first push callable you want to call //first push callable you want to call
//then push the arguments to send //then push the arguments to send
//argc is the number of arguments that was pushed (not counting the callable) //argc is the number of arguments that was pushed (not counting the callable)
bool pkpy_call(pkpy_vm*, int argc); PK_EXPORT bool pkpy_call(pkpy_vm*, int argc);
//first push the object the method belongs to (self) //first push the object the method belongs to (self)
//then push the the argments //then push the the argments
//argc is the number of arguments that was pushed (not counting the callable or self) //argc is the number of arguments that was pushed (not counting the callable or self)
//name is the name of the method to call on the object //name is the name of the method to call on the object
bool pkpy_call_method(pkpy_vm*, const char* name, int argc); PK_EXPORT bool pkpy_call_method(pkpy_vm*, const char* name, int argc);
//we will break with the lua api here //we will break with the lua api here
//lua uses 1 as the index to the first pushed element for all of these functions //lua uses 1 as the index to the first pushed element for all of these functions
//but we will start counting at zero to match python //but we will start counting at zero to match python
//we will allow negative numbers to count backwards from the top //we will allow negative numbers to count backwards from the top
bool pkpy_to_int(pkpy_vm*, int index, int* ret); PK_EXPORT bool pkpy_to_int(pkpy_vm*, int index, int* ret);
bool pkpy_to_float(pkpy_vm*, int index, double* ret); PK_EXPORT bool pkpy_to_float(pkpy_vm*, int index, double* ret);
bool pkpy_to_bool(pkpy_vm*, int index, bool* ret); PK_EXPORT bool pkpy_to_bool(pkpy_vm*, int index, bool* ret);
bool pkpy_to_voidp(pkpy_vm*, int index, void** ret); PK_EXPORT bool pkpy_to_voidp(pkpy_vm*, int index, void** ret);
//this method provides a strong reference, you are responsible for freeing the //this method provides a strong reference, you are responsible for freeing the
//string when you are done with it //string when you are done with it
bool pkpy_to_string(pkpy_vm*, int index, char** ret); PK_EXPORT bool pkpy_to_string(pkpy_vm*, int index, char** ret);
//this method provides a weak reference, it is only valid until the //this method provides a weak reference, it is only valid until the
//next api call //next api call
//it is not null terminated //it is not null terminated
bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size); PK_EXPORT bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size);
//these do not follow the same error semantics as above, their return values //these do not follow the same error semantics as above, their return values
//just say whether the check succeeded or not, or else return the value asked for //just say whether the check succeeded or not, or else return the value asked for
bool pkpy_is_int(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_int(pkpy_vm*, int index);
bool pkpy_is_float(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_float(pkpy_vm*, int index);
bool pkpy_is_bool(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_bool(pkpy_vm*, int index);
bool pkpy_is_string(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_string(pkpy_vm*, int index);
bool pkpy_is_voidp(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_voidp(pkpy_vm*, int index);
bool pkpy_is_none(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_none(pkpy_vm*, int index);
//will return true if global exists //will return true if global exists
bool pkpy_check_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_check_global(pkpy_vm*, const char* name);
//will return true if the vm is currently in an error state //will return true if the vm is currently in an error state
bool pkpy_check_error(pkpy_vm*); PK_EXPORT bool pkpy_check_error(pkpy_vm*);
//will return true if at least free empty slots remain on the stack //will return true if at least free empty slots remain on the stack
bool pkpy_check_stack(pkpy_vm*, int free); PK_EXPORT bool pkpy_check_stack(pkpy_vm*, int free);
//returns the number of elements on the stack //returns the number of elements on the stack
int pkpy_stack_size(pkpy_vm*); PK_EXPORT int pkpy_stack_size(pkpy_vm*);
typedef void (*OutputHandler)(pkpy_vm*, const char*);
PK_EXPORT void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -100,10 +100,12 @@ void gc_marker_ex(CVM* vm) {
for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj); for(PyObject* obj: *vm->c_data) if(obj!=nullptr) OBJ_MARK(obj);
} }
static OutputHandler stdout_handler = nullptr;
static OutputHandler stderr_handler = nullptr;
static void noop_output_handler(VM* vm, const Str& str) { void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler){
(void) vm; ::stdout_handler = stdout_handler;
(void) str; ::stderr_handler = stderr_handler;
} }
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) { pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
@ -113,8 +115,14 @@ pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os) {
vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex; vm->_gc_marker_ex = (void (*)(VM*)) gc_marker_ex;
if (!use_stdio) { if (!use_stdio) {
vm->_stdout = noop_output_handler; vm->_stdout = [](VM* vm, const Str& s){
vm->_stderr = noop_output_handler; std::string str = s.str();
if (stdout_handler != nullptr) stdout_handler((pkpy_vm*)vm, str.c_str());
};
vm->_stderr = [](VM* vm, const Str& s){
std::string str = s.str();
if (stderr_handler != nullptr) stderr_handler((pkpy_vm*)vm, str.c_str());
};
} }
return (pkpy_vm*) vm; return (pkpy_vm*) vm;

View File

@ -7,6 +7,7 @@ extern "C" {
#include <stdbool.h> #include <stdbool.h>
#include <stdint.h> #include <stdint.h>
#include "export.h"
typedef struct pkpy_vm_handle pkpy_vm; typedef struct pkpy_vm_handle pkpy_vm;
@ -19,93 +20,95 @@ typedef struct pkpy_vm_handle pkpy_vm;
//if pkpy_clear_error returns true it means there was an error and it was cleared, //if pkpy_clear_error returns true it means there was an error and it was cleared,
//it will provide a string summary of the error in the message parameter (if it is not NULL) //it will provide a string summary of the error in the message parameter (if it is not NULL)
//if null is passed in as message, and it will just print the message to stderr //if null is passed in as message, and it will just print the message to stderr
bool pkpy_clear_error(pkpy_vm*, char** message); PK_EXPORT bool pkpy_clear_error(pkpy_vm*, char** message);
//NOTE you are responsible for freeing message //NOTE you are responsible for freeing message
//this will cause the vm to enter an error state and report the given message //this will cause the vm to enter an error state and report the given message
//when queried //when queried
//note that at the moment this is more like a panic than throwing an error //note that at the moment this is more like a panic than throwing an error
//the user will not be able to catch it with python code //the user will not be able to catch it with python code
bool pkpy_error(pkpy_vm*, const char* message); PK_EXPORT bool pkpy_error(pkpy_vm*, const char* message);
PK_EXPORT pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os);
pkpy_vm* pkpy_vm_create(bool use_stdio, bool enable_os); PK_EXPORT bool pkpy_vm_run(pkpy_vm*, const char* source);
bool pkpy_vm_run(pkpy_vm*, const char* source); PK_EXPORT void pkpy_vm_destroy(pkpy_vm*);
void pkpy_vm_destroy(pkpy_vm*);
typedef int (*pkpy_function)(pkpy_vm*); typedef int (*pkpy_function)(pkpy_vm*);
bool pkpy_pop(pkpy_vm*, int n); PK_EXPORT bool pkpy_pop(pkpy_vm*, int n);
//push the item at index onto the top of the stack (as well as leaving it where //push the item at index onto the top of the stack (as well as leaving it where
//it is on the stack) //it is on the stack)
bool pkpy_push(pkpy_vm*, int index); PK_EXPORT bool pkpy_push(pkpy_vm*, int index);
bool pkpy_push_function(pkpy_vm*, pkpy_function); PK_EXPORT bool pkpy_push_function(pkpy_vm*, pkpy_function);
bool pkpy_push_int(pkpy_vm*, int); PK_EXPORT bool pkpy_push_int(pkpy_vm*, int);
bool pkpy_push_float(pkpy_vm*, double); PK_EXPORT bool pkpy_push_float(pkpy_vm*, double);
bool pkpy_push_bool(pkpy_vm*, bool); PK_EXPORT bool pkpy_push_bool(pkpy_vm*, bool);
bool pkpy_push_string(pkpy_vm*, const char*); PK_EXPORT bool pkpy_push_string(pkpy_vm*, const char*);
bool pkpy_push_stringn(pkpy_vm*, const char*, int length); PK_EXPORT bool pkpy_push_stringn(pkpy_vm*, const char*, int length);
bool pkpy_push_voidp(pkpy_vm*, void*); PK_EXPORT bool pkpy_push_voidp(pkpy_vm*, void*);
bool pkpy_push_none(pkpy_vm*); PK_EXPORT bool pkpy_push_none(pkpy_vm*);
bool pkpy_set_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_set_global(pkpy_vm*, const char* name);
bool pkpy_get_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_get_global(pkpy_vm*, const char* name);
//first push callable you want to call //first push callable you want to call
//then push the arguments to send //then push the arguments to send
//argc is the number of arguments that was pushed (not counting the callable) //argc is the number of arguments that was pushed (not counting the callable)
bool pkpy_call(pkpy_vm*, int argc); PK_EXPORT bool pkpy_call(pkpy_vm*, int argc);
//first push the object the method belongs to (self) //first push the object the method belongs to (self)
//then push the the argments //then push the the argments
//argc is the number of arguments that was pushed (not counting the callable or self) //argc is the number of arguments that was pushed (not counting the callable or self)
//name is the name of the method to call on the object //name is the name of the method to call on the object
bool pkpy_call_method(pkpy_vm*, const char* name, int argc); PK_EXPORT bool pkpy_call_method(pkpy_vm*, const char* name, int argc);
//we will break with the lua api here //we will break with the lua api here
//lua uses 1 as the index to the first pushed element for all of these functions //lua uses 1 as the index to the first pushed element for all of these functions
//but we will start counting at zero to match python //but we will start counting at zero to match python
//we will allow negative numbers to count backwards from the top //we will allow negative numbers to count backwards from the top
bool pkpy_to_int(pkpy_vm*, int index, int* ret); PK_EXPORT bool pkpy_to_int(pkpy_vm*, int index, int* ret);
bool pkpy_to_float(pkpy_vm*, int index, double* ret); PK_EXPORT bool pkpy_to_float(pkpy_vm*, int index, double* ret);
bool pkpy_to_bool(pkpy_vm*, int index, bool* ret); PK_EXPORT bool pkpy_to_bool(pkpy_vm*, int index, bool* ret);
bool pkpy_to_voidp(pkpy_vm*, int index, void** ret); PK_EXPORT bool pkpy_to_voidp(pkpy_vm*, int index, void** ret);
//this method provides a strong reference, you are responsible for freeing the //this method provides a strong reference, you are responsible for freeing the
//string when you are done with it //string when you are done with it
bool pkpy_to_string(pkpy_vm*, int index, char** ret); PK_EXPORT bool pkpy_to_string(pkpy_vm*, int index, char** ret);
//this method provides a weak reference, it is only valid until the //this method provides a weak reference, it is only valid until the
//next api call //next api call
//it is not null terminated //it is not null terminated
bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size); PK_EXPORT bool pkpy_to_stringn(pkpy_vm*, int index, const char** ret, int* size);
//these do not follow the same error semantics as above, their return values //these do not follow the same error semantics as above, their return values
//just say whether the check succeeded or not, or else return the value asked for //just say whether the check succeeded or not, or else return the value asked for
bool pkpy_is_int(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_int(pkpy_vm*, int index);
bool pkpy_is_float(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_float(pkpy_vm*, int index);
bool pkpy_is_bool(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_bool(pkpy_vm*, int index);
bool pkpy_is_string(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_string(pkpy_vm*, int index);
bool pkpy_is_voidp(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_voidp(pkpy_vm*, int index);
bool pkpy_is_none(pkpy_vm*, int index); PK_EXPORT bool pkpy_is_none(pkpy_vm*, int index);
//will return true if global exists //will return true if global exists
bool pkpy_check_global(pkpy_vm*, const char* name); PK_EXPORT bool pkpy_check_global(pkpy_vm*, const char* name);
//will return true if the vm is currently in an error state //will return true if the vm is currently in an error state
bool pkpy_check_error(pkpy_vm*); PK_EXPORT bool pkpy_check_error(pkpy_vm*);
//will return true if at least free empty slots remain on the stack //will return true if at least free empty slots remain on the stack
bool pkpy_check_stack(pkpy_vm*, int free); PK_EXPORT bool pkpy_check_stack(pkpy_vm*, int free);
//returns the number of elements on the stack //returns the number of elements on the stack
int pkpy_stack_size(pkpy_vm*); PK_EXPORT int pkpy_stack_size(pkpy_vm*);
typedef void (*OutputHandler)(pkpy_vm*, const char*);
PK_EXPORT void pkpy_set_output_handlers(pkpy_vm*, OutputHandler stdout_handler, OutputHandler stderr_handler);
#ifdef __cplusplus #ifdef __cplusplus

16
src/export.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef PK_EXPORT
#ifdef _WIN32
#define PK_EXPORT __declspec(dllexport)
#elif __APPLE__
#define PK_EXPORT __attribute__((visibility("default"))) __attribute__((used))
#elif __EMSCRIPTEN__
#include <emscripten.h>
#define PK_EXPORT EMSCRIPTEN_KEEPALIVE
#else
#define PK_EXPORT
#endif
#define PK_LEGACY_EXPORT PK_EXPORT inline
#endif

View File

@ -12,6 +12,7 @@
#include "requests.h" #include "requests.h"
#include "io.h" #include "io.h"
#include "_generated.h" #include "_generated.h"
#include "export.h"
#include "vm.h" #include "vm.h"
namespace pkpy { namespace pkpy {
@ -1001,19 +1002,6 @@ inline void init_builtins(VM* _vm) {
Generator::register_class(_vm, _vm->builtins); Generator::register_class(_vm, _vm->builtins);
} }
#ifdef _WIN32
#define PK_EXPORT __declspec(dllexport)
#elif __APPLE__
#define PK_EXPORT __attribute__((visibility("default"))) __attribute__((used))
#elif __EMSCRIPTEN__
#include <emscripten.h>
#define PK_EXPORT EMSCRIPTEN_KEEPALIVE
#else
#define PK_EXPORT
#endif
#define PK_LEGACY_EXPORT PK_EXPORT inline
inline void add_module_time(VM* vm){ inline void add_module_time(VM* vm){
PyObject* mod = vm->new_module("time"); PyObject* mod = vm->new_module("time");
vm->bind_func<0>(mod, "time", [](VM* vm, ArgsView args) { vm->bind_func<0>(mod, "time", [](VM* vm, ArgsView args) {