This commit is contained in:
blueloveTH 2024-08-16 16:40:06 +08:00
parent ef9c5c98cc
commit daf974657c

View File

@ -51,6 +51,8 @@ typedef py_TValue* py_GlobalRef;
typedef py_TValue* py_StackRef; typedef py_TValue* py_StackRef;
/// An item reference to a container object. It invalidates when the container is modified. /// An item reference to a container object. It invalidates when the container is modified.
typedef py_TValue* py_ItemRef; typedef py_TValue* py_ItemRef;
/// An output reference for returning a value.
typedef py_TValue* py_OutRef;
/// Native function signature. /// Native function signature.
/// @param argc number of arguments. /// @param argc number of arguments.
@ -109,54 +111,57 @@ PK_EXPORT bool py_compile(const char* source,
bool is_dynamic) PY_RAISE PY_RETURN; bool is_dynamic) PY_RAISE PY_RETURN;
/// Python equivalent to `globals()`. /// Python equivalent to `globals()`.
PK_EXPORT void py_newglobals(py_Ref); PK_EXPORT void py_newglobals(py_OutRef);
/// Python equivalent to `locals()`. /// Python equivalent to `locals()`.
/// @return a temporary object, which expires on the associated function return. /// @return a temporary object, which expires on the associated function return.
PK_EXPORT void py_newlocals(py_Ref); PK_EXPORT void py_newlocals(py_OutRef);
/************* Values Creation *************/ /************* Values Creation *************/
/// Create an `int` object. /// Create an `int` object.
PK_EXPORT void py_newint(py_Ref, py_i64); PK_EXPORT void py_newint(py_OutRef, py_i64);
/// Create a `float` object. /// Create a `float` object.
PK_EXPORT void py_newfloat(py_Ref, py_f64); PK_EXPORT void py_newfloat(py_OutRef, py_f64);
/// Create a `bool` object. /// Create a `bool` object.
PK_EXPORT void py_newbool(py_Ref, bool); PK_EXPORT void py_newbool(py_OutRef, bool);
/// Create a `str` object from a null-terminated string (utf-8). /// Create a `str` object from a null-terminated string (utf-8).
PK_EXPORT void py_newstr(py_Ref, const char*); PK_EXPORT void py_newstr(py_OutRef, const char*);
/// Create a `str` object from a char array (utf-8). /// Create a `str` object from a char array (utf-8).
PK_EXPORT void py_newstrn(py_Ref, const char*, int); PK_EXPORT void py_newstrn(py_OutRef, const char*, int);
/// Create a `bytes` object with `n` UNINITIALIZED bytes. /// Create a `bytes` object with `n` UNINITIALIZED bytes.
PK_EXPORT unsigned char* py_newbytes(py_Ref, int n); PK_EXPORT unsigned char* py_newbytes(py_OutRef, int n);
/// Create a `None` object. /// Create a `None` object.
PK_EXPORT void py_newnone(py_Ref); PK_EXPORT void py_newnone(py_OutRef);
/// Create a `NotImplemented` object. /// Create a `NotImplemented` object.
PK_EXPORT void py_newnotimplemented(py_Ref out); PK_EXPORT void py_newnotimplemented(py_OutRef);
/// Create a `...` object. /// Create a `...` object.
PK_EXPORT void py_newellipsis(py_Ref out); PK_EXPORT void py_newellipsis(py_OutRef);
/// Create a `nil` object. `nil` is an invalid representation of an object. /// Create a `nil` object. `nil` is an invalid representation of an object.
/// Don't use it unless you know what you are doing. /// Don't use it unless you know what you are doing.
PK_EXPORT void py_newnil(py_Ref); PK_EXPORT void py_newnil(py_OutRef);
/// Create a `tuple` with `n` UNINITIALIZED elements. /// Create a `tuple` with `n` UNINITIALIZED elements.
/// You should initialize all elements before using it. /// You should initialize all elements before using it.
PK_EXPORT void py_newtuple(py_Ref, int n); PK_EXPORT void py_newtuple(py_OutRef, int n);
/// Create an empty `list`. /// Create an empty `list`.
PK_EXPORT void py_newlist(py_Ref); PK_EXPORT void py_newlist(py_OutRef);
/// Create a `list` with `n` UNINITIALIZED elements. /// Create a `list` with `n` UNINITIALIZED elements.
/// You should initialize all elements before using it. /// You should initialize all elements before using it.
PK_EXPORT void py_newlistn(py_Ref, int n); PK_EXPORT void py_newlistn(py_OutRef, int n);
/// Create an empty `dict`. /// Create an empty `dict`.
PK_EXPORT void py_newdict(py_Ref); PK_EXPORT void py_newdict(py_OutRef);
/// Create an UNINITIALIZED `slice` object. /// Create an UNINITIALIZED `slice` object.
/// You should use `py_setslot()` to set `start`, `stop`, and `step`. /// You should use `py_setslot()` to set `start`, `stop`, and `step`.
PK_EXPORT void py_newslice(py_Ref); PK_EXPORT void py_newslice(py_OutRef);
/// Create a `nativefunc` object. /// Create a `nativefunc` object.
PK_EXPORT void py_newnativefunc(py_Ref out, py_CFunction); PK_EXPORT void py_newnativefunc(py_OutRef, py_CFunction);
/// Create a `function` object. /// Create a `function` object.
PK_EXPORT py_Name PK_EXPORT py_Name py_newfunction(py_OutRef out,
py_newfunction(py_Ref out, const char* sig, py_CFunction f, const char* docstring, int slots); const char* sig,
py_CFunction f,
const char* docstring,
int slots);
/// Create a `boundmethod` object. /// Create a `boundmethod` object.
PK_EXPORT void py_newboundmethod(py_Ref out, py_Ref self, py_Ref func); PK_EXPORT void py_newboundmethod(py_OutRef out, py_Ref self, py_Ref func);
/************* Name Convertions *************/ /************* Name Convertions *************/
@ -189,7 +194,7 @@ PK_EXPORT py_Type py_newtype(const char* name,
/// @param slots number of slots. Use `-1` to create a `__dict__`. /// @param slots number of slots. Use `-1` to create a `__dict__`.
/// @param udsize size of your userdata. /// @param udsize size of your userdata.
/// @return pointer to the userdata. /// @return pointer to the userdata.
PK_EXPORT void* py_newobject(py_Ref out, py_Type type, int slots, int udsize); PK_EXPORT void* py_newobject(py_OutRef out, py_Type type, int slots, int udsize);
/************* Type Cast *************/ /************* Type Cast *************/
@ -298,7 +303,8 @@ PK_EXPORT py_ItemRef py_emplacedict(py_Ref self, py_Name name);
/// Apply a function to all items in the object's `__dict__`. /// Apply a function to all items in the object's `__dict__`.
/// Return `true` if the function is successful for all items. /// Return `true` if the function is successful for all items.
/// NOTE: Be careful if `f` modifies the object's `__dict__`. /// NOTE: Be careful if `f` modifies the object's `__dict__`.
PK_EXPORT bool py_applydict(py_Ref self, bool (*f)(py_Name name, py_Ref val, void* ctx), void* ctx) PY_RAISE; PK_EXPORT bool
py_applydict(py_Ref self, bool (*f)(py_Name name, py_Ref val, void* ctx), void* ctx) PY_RAISE;
/// Get the i-th slot of the object. /// Get the i-th slot of the object.
/// The object must have slots and `i` must be in valid range. /// The object must have slots and `i` must be in valid range.
@ -466,7 +472,7 @@ PK_EXPORT void py_clearexc(py_StackRef p0);
#define UnboundLocalError(n) \ #define UnboundLocalError(n) \
py_exception(tp_UnboundLocalError, "local variable '%n' referenced before assignment", (n)) py_exception(tp_UnboundLocalError, "local variable '%n' referenced before assignment", (n))
PK_EXPORT bool StopIteration(); PK_EXPORT bool StopIteration() PY_RAISE;
PK_EXPORT bool KeyError(py_Ref key) PY_RAISE; PK_EXPORT bool KeyError(py_Ref key) PY_RAISE;
/************* Operators *************/ /************* Operators *************/
@ -474,7 +480,6 @@ PK_EXPORT bool KeyError(py_Ref key) PY_RAISE;
/// Python equivalent to `bool(val)`. /// Python equivalent to `bool(val)`.
/// 1: true, 0: false, -1: error /// 1: true, 0: false, -1: error
PK_EXPORT int py_bool(py_Ref val) PY_RAISE; PK_EXPORT int py_bool(py_Ref val) PY_RAISE;
/// Compare two objects. /// Compare two objects.
/// 1: lhs == rhs, 0: lhs != rhs, -1: error /// 1: lhs == rhs, 0: lhs != rhs, -1: error
PK_EXPORT int py_equal(py_Ref lhs, py_Ref rhs) PY_RAISE; PK_EXPORT int py_equal(py_Ref lhs, py_Ref rhs) PY_RAISE;