PocketpyBindings class
Bindings for src/pocketpy.h
.
Regenerate bindings with flutter pub run ffigen --config ffigen.yaml
.
Constructors
- PocketpyBindings(DynamicLibrary dynamicLibrary)
-
The symbols are looked up in
dynamicLibrary
. -
PocketpyBindings.fromLookup(Pointer<
T> lookup<T extends NativeType>(String symbolName) ) -
The symbols are looked up with
lookup
.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
KeyError(
py_Ref key) → bool -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
py_applydict(
py_Ref self, Pointer< NativeFunction< f, Pointer<Bool Function(py_Name name, py_Ref val, Pointer< >Void> ctx)>Void> ctx) → bool -
Apply a function to all items in the object's
__dict__
. Returntrue
if the function is successful for all items. NOTE: Be careful iff
modifies the object's__dict__
. -
py_assign(
py_Ref dst, py_Ref src) → void -
Equivalent to
*dst = *src
. -
py_binaryop(
py_Ref lhs, py_Ref rhs, int op, int rop) → bool -
Perform a binary operation.
The result will be set to
py_retval()
. The stack remains unchanged after the operation. -
py_bind(
py_Ref obj, Pointer< Char> sig, py_CFunction f) → void -
Bind a function to the object via "decl-based" style.
@param obj the target object.
@param sig signature of the function. e.g.
add(x, y)
. @param f function to bind. -
py_bindfunc(
py_Ref obj, Pointer< Char> name, py_CFunction f) → void - Bind a function to the object via "argc-based" style. @param obj the target object. @param name name of the function. @param f function to bind.
-
py_bindmethod(
int type, Pointer< Char> name, py_CFunction f) → void - Bind a method to type via "argc-based" style. @param type the target type. @param name name of the method. @param f function to bind.
-
py_bindproperty(
int type, Pointer< Char> name, py_CFunction getter, py_CFunction setter) → void -
Bind a property to type.
@param type the target type.
@param name name of the property.
@param getter getter function.
@param setter setter function. Use
NULL
if not needed. -
py_bindstaticmethod(
int type, Pointer< Char> name, py_CFunction f) → void - Bind a static method to type via "argc-based" style. @param type the target type. @param name name of the method. @param f function to bind.
-
py_bool(
py_Ref val) → int -
Python equivalent to
bool(val)
. 1: true, 0: false, -1: error -
py_bytes_resize(
py_Ref arg0, int size) → void -
Resize a
bytes
object. It can only be resized down. -
py_call(
py_Ref f, int argc, py_Ref argv) → bool -
Call a function.
It prepares the stack and then performs a
vectorcall(argc, 0, false)
. The result will be set topy_retval()
. The stack remains unchanged after the operation. -
py_callable(
py_Ref val) → bool -
Python equivalent to
callable(val)
. -
py_callbacks(
) → Pointer< py_Callbacks> - Setup the callbacks for the current VM.
-
py_callcfunc(
py_CFunction f, int argc, py_Ref argv) → bool -
Call a
py_CFunction
in a safe way. This function does extra checks to help you debugpy_CFunction
. -
py_castfloat(
py_Ref arg0, Pointer< py_f64> out) → bool -
Cast a
int
orfloat
object in python todouble
. If successful, return true and set the value toout
. Otherwise, return false and raiseTypeError
. -
py_castfloat32(
py_Ref arg0, Pointer< Float> out) → bool -
32-bit version of
py_castfloat
. -
py_castint(
py_Ref arg0, Pointer< py_i64> out) → bool -
Cast a
int
object in python toint64_t
. -
py_checkexc(
bool ignore_handled) → bool - Check if an exception is raised.
-
py_checktype(
py_Ref self, int type) → bool -
Check if the object is an instance of the given type.
Raise
TypeError
if the check fails. -
py_clearexc(
py_StackRef p0) → void -
Clear the current exception.
@param p0 the unwinding point. Use
NULL
if not needed. -
py_compile(
Pointer< Char> source, Pointer<Char> filename, int mode, bool is_dynamic) → bool -
Compile a source string into a code object.
Use python's
exec()
oreval()
to execute it. -
py_currentvm(
) → int - Get the current VM index.
-
py_delattr(
py_Ref self, int name) → bool -
Python equivalent to
delattr(self, name)
. -
py_deldict(
py_Ref self, int name) → bool -
Delete an item from the object's
__dict__
. Returntrue
if the deletion is successful. -
py_delitem(
py_Ref self, py_Ref key) → bool -
Python equivalent to
del self[key]
. -
py_dict_apply(
py_Ref self, Pointer< NativeFunction< f, Pointer<Bool Function(py_Ref key, py_Ref val, Pointer< >Void> ctx)>Void> ctx) → bool - true: success, false: error
-
py_dict_delitem(
py_Ref self, py_Ref key) → int - -1: error, 0: not found, 1: found (and deleted)
-
py_dict_delitem_by_int(
py_Ref self, int key) → int - -1: error, 0: not found, 1: found (and deleted)
-
py_dict_delitem_by_str(
py_Ref self, Pointer< Char> key) → int - -1: error, 0: not found, 1: found (and deleted)
-
py_dict_getitem(
py_Ref self, py_Ref key) → int - -1: error, 0: not found, 1: found
-
py_dict_getitem_by_int(
py_Ref self, int key) → int - -1: error, 0: not found, 1: found
-
py_dict_getitem_by_str(
py_Ref self, Pointer< Char> key) → int - -1: error, 0: not found, 1: found
-
py_dict_len(
py_Ref self) → int - noexcept
-
py_dict_setitem(
py_Ref self, py_Ref key, py_Ref val) → bool - true: success, false: error
-
py_dict_setitem_by_int(
py_Ref self, int key, py_Ref val) → bool - true: success, false: error
-
py_dict_setitem_by_str(
py_Ref self, Pointer< Char> key, py_Ref val) → bool - true: success, false: error
-
py_emplacedict(
py_Ref self, int name) → py_ItemRef -
Prepare an insertion to the object's
__dict__
. -
py_equal(
py_Ref lhs, py_Ref rhs) → int - Compare two objects. 1: lhs == rhs, 0: lhs != rhs, -1: error
-
py_eval(
Pointer< Char> source, py_Ref module) → bool -
Evaluate a source string. Equivalent to
py_exec(source, "<string>", EVAL_MODE, module)
. -
py_exception(
int type, Pointer< Char> fmt) → bool - Raise an exception by type and message. Always return false.
-
py_exec(
Pointer< Char> source, Pointer<Char> filename, int mode, py_Ref module) → bool -
Run a source string.
@param source source string.
@param filename filename (for error messages).
@param mode compile mode. Use
EXEC_MODE
for statementsEVAL_MODE
for expressions. @param module target module. Use NULL for the main module. @returntrue
if the execution is successful orfalse
if an exception is raised. -
py_False(
) → py_GlobalRef -
A shorthand for
False
. -
py_finalize(
) → void - Finalize pocketpy and free all VMs.
-
py_formatexc(
) → Pointer< Char> - Format the current exception and return a null-terminated string. The result should be freed by the caller. The exception will be set as handled.
-
py_getattr(
py_Ref self, int name) → bool -
Python equivalent to
getattr(self, name)
. -
py_getbuiltin(
int name) → py_ItemRef -
Get variable in the
builtins
module. -
py_getdict(
py_Ref self, int name) → py_ItemRef -
Get an item from the object's
__dict__
. ReturnNULL
if not found. -
py_getglobal(
int name) → py_ItemRef -
Get variable in the
__main__
module. -
py_getitem(
py_Ref self, py_Ref key) → bool -
Python equivalent to
self[key]
. -
py_getmodule(
Pointer< Char> path) → py_GlobalRef - Get a module by path.
-
py_getreg(
int i) → py_GlobalRef - Get the i-th register. All registers are located in a contiguous memory.
-
py_getslot(
py_Ref self, int i) → py_ObjectRef -
Get the i-th slot of the object.
The object must have slots and
i
must be in valid range. -
py_gettype(
Pointer< Char> module, int name) → int -
Get type by module and name. e.g.
py_gettype("time", py_name("struct_time"))
. Return0
if not found. -
py_getvmctx(
) → Pointer< Void> - Get the current VM context. This is used for user-defined data.
-
py_hash(
py_Ref arg0, Pointer< py_i64> out) → bool - Get the hash value of the object.
-
py_import(
Pointer< Char> path) → int -
Import a module.
The result will be set to
py_retval()
. -1: error, 0: not found, 1: success -
py_initialize(
) → void - Initialize pocketpy and the default VM.
-
py_inspect_currentfunction(
) → py_StackRef -
Get the current
function
object on the stack. ReturnNULL
if not available. NOTE: This function should be placed at the beginning of your decl-based bindings. -
py_inspect_currentmodule(
) → py_GlobalRef -
Get the current
module
object where the code is executed. ReturnNULL
if not available. -
py_isidentical(
py_Ref arg0, py_Ref arg1) → bool -
Python equivalent to
lhs is rhs
. -
py_isinstance(
py_Ref obj, int type) → bool - Check if the object is an instance of the given type.
-
py_issubclass(
int derived, int base) → bool - Check if the derived type is a subclass of the base type.
-
py_istype(
py_Ref arg0, int arg1) → bool - Check if the object is exactly the given type.
-
py_iter(
py_Ref arg0) → bool - Get the iterator of the object.
-
py_json_dumps(
py_Ref val) → bool -
Python equivalent to
json.dumps(val)
. -
py_json_loads(
Pointer< Char> source) → bool -
Python equivalent to
json.loads(val)
. -
py_len(
py_Ref val) → bool -
Python equivalent to
len(val)
. -
py_less(
py_Ref lhs, py_Ref rhs) → int - Compare two objects. 1: lhs < rhs, 0: lhs >= rhs, -1: error
-
py_list_append(
py_Ref self, py_Ref val) → void -
py_list_clear(
py_Ref self) → void -
py_list_data(
py_Ref self) → py_ItemRef -
py_list_delitem(
py_Ref self, int i) → void -
py_list_emplace(
py_Ref self) → py_ItemRef -
py_list_getitem(
py_Ref self, int i) → py_ItemRef -
py_list_insert(
py_Ref self, int i, py_Ref val) → void -
py_list_len(
py_Ref self) → int -
py_list_setitem(
py_Ref self, int i, py_Ref val) → void -
py_list_swap(
py_Ref self, int i, int j) → void -
py_matchexc(
int type) → bool -
Check if the exception is an instance of the given type.
This function is roughly equivalent to python's
except <T> as e:
block. If match, the exception will be stored inpy_retval()
as handled. -
py_name(
Pointer< Char> arg0) → int - Convert a null-terminated string to a name.
-
py_name2str(
int arg0) → Pointer< Char> - Convert a name to a null-terminated string.
-
py_name2sv(
int arg0) → c11_sv -
Convert a name to a
c11_sv
. -
py_namev(
c11_sv arg0) → int -
Convert a
c11_sv
to a name. -
py_newbool(
py_OutRef arg0, bool arg1) → void -
Create a
bool
object. -
py_newboundmethod(
py_OutRef out, py_Ref self, py_Ref func) → void -
Create a
boundmethod
object. -
py_newbytes(
py_OutRef arg0, int n) → Pointer< UnsignedChar> -
Create a
bytes
object withn
UNINITIALIZED bytes. -
py_newdict(
py_OutRef arg0) → void -
Create an empty
dict
. -
py_newellipsis(
py_OutRef arg0) → void -
Create a
...
object. -
py_newfloat(
py_OutRef arg0, double arg1) → void -
Create a
float
object. -
py_newfunction(
py_OutRef out, Pointer< Char> sig, py_CFunction f, Pointer<Char> docstring, int slots) → int -
Create a
function
object. -
py_newglobals(
py_OutRef arg0) → void -
Python equivalent to
globals()
. -
py_newint(
py_OutRef arg0, int arg1) → void -
Create an
int
object. -
py_newlist(
py_OutRef arg0) → void -
Create an empty
list
. -
py_newlistn(
py_OutRef arg0, int n) → void -
Create a
list
withn
UNINITIALIZED elements. You should initialize all elements before using it. -
py_newlocals(
py_OutRef arg0) → void -
Python equivalent to
locals()
. @return a temporary object, which expires on the associated function return. -
py_newmat3x3(
py_OutRef out) → Pointer< c11_mat3x3> -
py_newmodule(
Pointer< Char> path) → py_GlobalRef - Create a new module.
-
py_newnativefunc(
py_OutRef arg0, py_CFunction arg1) → void -
Create a
nativefunc
object. -
py_newnil(
py_OutRef arg0) → void -
Create a
nil
object.nil
is an invalid representation of an object. Don't use it unless you know what you are doing. -
py_newnone(
py_OutRef arg0) → void -
Create a
None
object. -
py_newnotimplemented(
py_OutRef arg0) → void -
Create a
NotImplemented
object. -
py_newobject(
py_OutRef out, int type, int slots, int udsize) → Pointer< Void> -
Create a new object.
@param out output reference.
@param type type of the object.
@param slots number of slots. Use
-1
to create a__dict__
. @param udsize size of your userdata. @return pointer to the userdata. -
py_newslice(
py_OutRef arg0) → void -
Create an UNINITIALIZED
slice
object. You should usepy_setslot()
to setstart
,stop
, andstep
. -
py_newstr(
py_OutRef arg0, Pointer< Char> arg1) → void -
Create a
str
object from a null-terminated string (utf-8). -
py_newstrn(
py_OutRef arg0, int arg1) → Pointer< Char> -
Create a
str
object withn
UNINITIALIZED bytes plus'\0'
. -
py_newstrv(
py_OutRef arg0, c11_sv arg1) → void -
Create a
str
object from ac11_sv
. -
py_newtuple(
py_OutRef arg0, int n) → void -
Create a
tuple
withn
UNINITIALIZED elements. You should initialize all elements before using it. -
py_newtype(
Pointer< Char> name, int base, py_GlobalRef module, py_Dtor dtor) → int -
Create a new type.
@param name name of the type.
@param base base type.
@param module module where the type is defined. Use
NULL
for built-in types. @param dtor destructor function. UseNULL
if not needed. -
py_newvec2(
py_OutRef out, c11_vec2 arg1) → void - linalg module
-
py_newvec2i(
py_OutRef out, c11_vec2i arg1) → void -
py_newvec3(
py_OutRef out, c11_vec3 arg1) → void -
py_newvec3i(
py_OutRef out, c11_vec3i arg1) → void -
py_next(
py_Ref arg0) → int - Get the next element from the iterator. 1: success, 0: StopIteration, -1: error
-
py_NIL(
) → py_GlobalRef -
A shorthand for
nil
.nil
is not a valid python object. -
py_None(
) → py_GlobalRef -
A shorthand for
None
. -
py_peek(
int i) → py_StackRef -
Get the i-th object from the top of the stack.
i
should be negative, e.g. (-1) means TOS. -
py_pickle_dumps(
py_Ref val) → bool -
Python equivalent to
pickle.dumps(val)
. -
py_pickle_loads(
Pointer< UnsignedChar> data, int size) → bool -
Python equivalent to
pickle.loads(val)
. -
py_pop(
) → void - Pop an object from the stack.
-
py_printexc(
) → void - Print the current exception. The exception will be set as handled.
-
py_push(
py_Ref src) → void - Push the object to the stack.
-
py_pusheval(
Pointer< Char> expr, py_GlobalRef module) → bool - Evaluate an expression and push the result to the stack. This function is used for testing.
-
py_pushmethod(
int name) → bool -
Get the unbound method of the object.
Assume the object is located at the top of the stack.
If return true:
[self] -> [unbound, self]
. If return false:[self] -> [self]
(no change). -
py_pushname(
int name) → void -
Push a
py_Name
to the stack. This is used for keyword arguments. -
py_pushnil(
) → void -
Push a
nil
object to the stack. -
py_pushnone(
) → void -
Push a
None
object to the stack. -
py_pushtmp(
) → py_StackRef - Get a temporary variable from the stack.
-
py_raise(
py_Ref arg0) → bool - Raise an expection object. Always return false.
-
py_replinput(
Pointer< Char> buf, int max_size) → int - An utility function to read a line from stdin for REPL.
-
py_repr(
py_Ref val) → bool -
Python equivalent to
repr(val)
. -
py_resetvm(
) → void - Reset the current VM.
-
py_retval(
) → py_GlobalRef - Get the last return value.
-
py_setattr(
py_Ref self, int name, py_Ref val) → bool -
Python equivalent to
setattr(self, name, val)
. -
py_setdict(
py_Ref self, int name, py_Ref val) → void -
Set an item to the object's
__dict__
. -
py_setglobal(
int name, py_Ref val) → void -
Set variable in the
__main__
module. -
py_setitem(
py_Ref self, py_Ref key, py_Ref val) → bool -
Python equivalent to
self[key] = val
. -
py_setreg(
int i, py_Ref val) → void - Set the i-th register.
-
py_setslot(
py_Ref self, int i, py_Ref val) → void - Set the i-th slot of the object.
-
py_setvmctx(
Pointer< Void> ctx) → void - Set the current VM context. This is used for user-defined data.
-
py_shrink(
int n) → void - Shrink the stack by n.
-
py_smarteval(
Pointer< Char> source, py_Ref module) → bool -
Evaluate a source string with smart interpretation.
Example:
py_newstr(py_r0(), "abc");
py_smarteval("len(_)", NULL, py_r0());
int res = py_toint(py_retval());
// res will be 3
. -
py_smartexec(
Pointer< Char> source, py_Ref module) → bool -
Run a source string with smart interpretation.
Example:
py_newstr(py_r0(), "abc");
py_newint(py_r1(), 123);
py_smartexec("print(_0, _1)", NULL, py_r0(), py_r1());
// "abc 123" will be printed
. -
py_str(
py_Ref val) → bool -
Python equivalent to
str(val)
. -
py_switchvm(
int index) → void -
Switch to a VM.
@param index index of the VM ranging from 0 to 16 (exclusive).
0
is the default VM. -
py_sys_setargv(
int argc, Pointer< Pointer< argv) → voidChar> > -
Set
sys.argv
. Used for storing command-line arguments. -
py_tobool(
py_Ref arg0) → bool -
Convert a
bool
object in python tobool
. -
py_tobytes(
py_Ref arg0, Pointer< Int> size) → Pointer<UnsignedChar> -
Convert a
bytes
object in python to char array. -
py_tofloat(
py_Ref arg0) → double -
Convert a
float
object in python todouble
. -
py_toint(
py_Ref arg0) → int -
Convert an
int
object in python toint64_t
. -
py_tomat3x3(
py_Ref self) → Pointer< c11_mat3x3> -
py_tostr(
py_Ref arg0) → Pointer< Char> -
Convert a
str
object in python to null-terminated string. -
py_tostrn(
py_Ref arg0, Pointer< Int> size) → Pointer<Char> -
Convert a
str
object in python to char array. -
py_tosv(
py_Ref arg0) → c11_sv -
Convert a
str
object in python toc11_sv
. -
py_totype(
py_Ref arg0) → int -
Convert a
type
object in python topy_Type
. -
py_touserdata(
py_Ref arg0) → Pointer< Void> - Convert a user-defined object to its userdata.
-
py_tovec2(
py_Ref self) → c11_vec2 -
py_tovec2i(
py_Ref self) → c11_vec2i -
py_tovec3(
py_Ref self) → c11_vec3 -
py_tovec3i(
py_Ref self) → c11_vec3i -
py_tpcall(
int type, int argc, py_Ref argv) → bool - Call a type to create a new instance.
-
py_tpfindmagic(
int arg0, int name) → py_GlobalRef -
Search the magic method from the given type to the base type.
Return
NULL
if not found. -
py_tpfindname(
int arg0, int name) → py_ItemRef -
Search the name from the given type to the base type.
Return
NULL
if not found. -
py_tpgetmagic(
int type, int name) → py_GlobalRef -
Get the magic method from the given type only.
The returned reference is always valid. However, its value may be
nil
. -
py_tpname(
int type) → Pointer< Char> - Get the type name.
-
py_tpobject(
int type) → py_GlobalRef - Get the type object of the given type.
-
py_True(
) → py_GlobalRef -
A shorthand for
True
. -
py_tuple_data(
py_Ref self) → py_ObjectRef - Unchecked Functions
-
py_tuple_getitem(
py_Ref self, int i) → py_ObjectRef -
py_tuple_len(
py_Ref self) → int -
py_tuple_setitem(
py_Ref self, int i, py_Ref val) → void -
py_typeof(
py_Ref self) → int - Get the type of the object.
-
py_vectorcall(
int argc, int kwargc) → bool -
Call a callable object via pocketpy's calling convention.
You need to prepare the stack using this form:
callable, self/nil, arg1, arg2, ..., k1, v1, k2, v2, ...
argc
is the number of positional arguments excludingself
.kwargc
is the number of keyword arguments, i.e. the number of key-value pairs. The result will be set topy_retval()
. The stack size will be reduced by2 + argc + kwargc * 2
. -
StopIteration(
) → bool -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited