The wings API. More...
#include <stdint.h>
#include <stdbool.h>
Go to the source code of this file.
Data Structures | |
struct | Wg_Config |
The configuration used to initialise an interpreter. More... | |
Typedefs | |
typedef struct Wg_Context | Wg_Context |
An opaque type representing the state of an interpreter. More... | |
typedef struct Wg_Obj | Wg_Obj |
An opaque type representing an object in the interpreter. More... | |
typedef int64_t | Wg_int |
The underlying data type of an integer object. More... | |
typedef uint64_t | Wg_uint |
The unsigned version of Wg_int. More... | |
typedef double | Wg_float |
The underlying data type of a float object. More... | |
typedef Wg_Obj *(* | Wg_Function) (Wg_Context *context, Wg_Obj **argv, int argc) |
The signature of a native function registered into the interpreter as a function object. More... | |
typedef void(* | Wg_Finalizer) (void *userdata) |
The signature of an object finalizer. More... | |
typedef void(* | Wg_PrintFunction) (const char *message, int len, void *userdata) |
The signature of the print function used by the interpreter for printing. More... | |
typedef void(* | Wg_ErrorCallback) (const char *message) |
The signature of the fatal error callback. More... | |
typedef bool(* | Wg_IterationCallback) (Wg_Obj *obj, void *userdata) |
The signature of an iteration callback used by Wg_Iterate(). More... | |
typedef bool(* | Wg_ModuleLoader) (Wg_Context *context) |
The signature of a module loader. More... | |
typedef struct Wg_Config | Wg_Config |
The configuration used to initialise an interpreter. More... | |
typedef enum Wg_UnOp | Wg_UnOp |
The unary operation to be used Wg_UnaryOp. More... | |
typedef enum Wg_BinOp | Wg_BinOp |
The binary operation to be used by Wg_BinaryOp. More... | |
typedef enum Wg_Exc | Wg_Exc |
The type exception to be raised by Wg_RaiseException. More... | |
Functions | |
Wg_Context * | Wg_CreateContext (const Wg_Config *config) |
Create an instance of an interpreter. More... | |
void | Wg_DestroyContext (Wg_Context *context) |
Free a context created with Wg_CreateContext(). More... | |
void | Wg_DefaultConfig (Wg_Config *config) |
Get the default configuration. More... | |
bool | Wg_Execute (Wg_Context *context, const char *script, const char *prettyName) |
Execute a script. More... | |
Wg_Obj * | Wg_ExecuteExpression (Wg_Context *context, const char *script, const char *prettyName) |
Execute a script containing an expression. More... | |
Wg_Obj * | Wg_Compile (Wg_Context *context, const char *script, const char *prettyName) |
Compile a script into a function object. More... | |
Wg_Obj * | Wg_CompileExpression (Wg_Context *context, const char *script, const char *prettyName) |
Compile an expression into a function object. More... | |
void | Wg_SetErrorCallback (Wg_ErrorCallback callback) |
Set a callback for programmer errors. More... | |
const char * | Wg_GetErrorMessage (Wg_Context *context) |
Get the current exception message. More... | |
Wg_Obj * | Wg_GetException (Wg_Context *context) |
Get the current exception object. More... | |
void | Wg_RaiseException (Wg_Context *context, Wg_Exc type, const char *message) |
Create and raise an exception. More... | |
void | Wg_RaiseExceptionClass (Wg_Obj *klass, const char *message) |
Create and raise an exception using a class object. More... | |
void | Wg_RaiseExceptionObject (Wg_Obj *obj) |
Raise an existing exception object. More... | |
void | Wg_ReraiseExceptionObject (Wg_Obj *obj) |
Raise an existing exception object without affecting the stack trace. More... | |
void | Wg_RaiseArgumentCountError (Wg_Context *context, int given, int expected) |
Raise a TypeError with a formatted message. More... | |
void | Wg_RaiseArgumentTypeError (Wg_Context *context, int index, const char *expected) |
Raise a TypeError with a formatted message. More... | |
void | Wg_RaiseAttributeError (const Wg_Obj *obj, const char *attribute) |
Raise a AttributeError with a formatted message. More... | |
void | Wg_RaiseKeyError (Wg_Context *context, Wg_Obj *key) |
Raise a KeyError with a formatted message. More... | |
void | Wg_RaiseNameError (Wg_Context *context, const char *name) |
Raise a NameError with a formatted message. More... | |
void | Wg_ClearException (Wg_Context *context) |
Clear the current exception. More... | |
Wg_Context * | Wg_GetContextFromObject (Wg_Obj *obj) |
Get the context associated with an object. More... | |
Wg_Obj * | Wg_IsInstance (const Wg_Obj *instance, Wg_Obj *const *types, int typesLen) |
Check if an object's class derives from any of the specified classes. More... | |
Wg_Obj * | Wg_GetGlobal (Wg_Context *context, const char *name) |
Get a global variable in the current module namespace. More... | |
void | Wg_SetGlobal (Wg_Context *context, const char *name, Wg_Obj *value) |
Set a global variable in the current module namespace. More... | |
void | Wg_Print (const Wg_Context *context, const char *message, int len) |
Print a message. More... | |
void | Wg_PrintString (const Wg_Context *context, const char *message) |
Print a string message. More... | |
void | Wg_CollectGarbage (Wg_Context *context) |
Force run the garbage collector and free all unreachable objects. More... | |
void | Wg_IncRef (Wg_Obj *obj) |
Increment the reference count of an object. A positive reference count prevents the object from being garbage collected. More... | |
void | Wg_DecRef (Wg_Obj *obj) |
Decrement the reference count of an object. A positive reference count prevents the object from being garbage collected. More... | |
Wg_Obj * | Wg_None (Wg_Context *context) |
Get the None singleton value. More... | |
Wg_Obj * | Wg_NewBool (Wg_Context *context, bool value) |
Instantiate a boolean object. More... | |
Wg_Obj * | Wg_NewInt (Wg_Context *context, Wg_int value) |
Instantiate an integer object. More... | |
Wg_Obj * | Wg_NewFloat (Wg_Context *context, Wg_float value) |
Instantiate a float object. More... | |
Wg_Obj * | Wg_NewString (Wg_Context *context, const char *value) |
Instantiate a string object. More... | |
Wg_Obj * | Wg_NewStringBuffer (Wg_Context *context, const char *buffer, int len) |
Instantiate a string object from a buffer. More... | |
Wg_Obj * | Wg_NewTuple (Wg_Context *context, Wg_Obj **argv, int argc) |
Instantiate a tuple object. More... | |
Wg_Obj * | Wg_NewList (Wg_Context *context, Wg_Obj **argv, int argc) |
Instantiate a list object. More... | |
Wg_Obj * | Wg_NewDictionary (Wg_Context *context, Wg_Obj **keys, Wg_Obj **values, int len) |
Instantiate a dictionary object. More... | |
Wg_Obj * | Wg_NewSet (Wg_Context *context, Wg_Obj **argv, int argc) |
Instantiate a set object. More... | |
Wg_Obj * | Wg_NewFunction (Wg_Context *context, Wg_Function fptr, void *userdata, const char *prettyName) |
Instantiate a function object. More... | |
Wg_Obj * | Wg_BindMethod (Wg_Obj *klass, const char *name, Wg_Function fptr, void *userdata) |
Instantiate a function object and bind it to a class. More... | |
Wg_Obj * | Wg_NewClass (Wg_Context *context, const char *name, Wg_Obj **bases, int basesLen) |
Instantiate a class object. More... | |
bool | Wg_IsNone (const Wg_Obj *obj) |
Check if an object is None. More... | |
bool | Wg_IsBool (const Wg_Obj *obj) |
Check if an object is a boolean. More... | |
bool | Wg_IsInt (const Wg_Obj *obj) |
Check if an object is an integer. More... | |
bool | Wg_IsIntOrFloat (const Wg_Obj *obj) |
Check if an object is a float. More... | |
bool | Wg_IsString (const Wg_Obj *obj) |
Check if an object is a string. More... | |
bool | Wg_IsTuple (const Wg_Obj *obj) |
Check if an object is a tuple. More... | |
bool | Wg_IsList (const Wg_Obj *obj) |
Check if an object is a list. More... | |
bool | Wg_IsDictionary (const Wg_Obj *obj) |
Check if an object is a dictionary. More... | |
bool | Wg_IsSet (const Wg_Obj *obj) |
Check if an object is a set. More... | |
bool | Wg_IsFunction (const Wg_Obj *obj) |
Check if an object is a function. More... | |
bool | Wg_IsClass (const Wg_Obj *obj) |
Check if an object is a class. More... | |
bool | Wg_GetBool (const Wg_Obj *obj) |
Get the value from a boolean object. More... | |
Wg_int | Wg_GetInt (const Wg_Obj *obj) |
Get the value from an integer object. More... | |
Wg_float | Wg_GetFloat (const Wg_Obj *obj) |
Get the float value from an integer or float object. More... | |
const char * | Wg_GetString (const Wg_Obj *obj, int *len) |
Get the value from a string object. More... | |
void | Wg_SetUserdata (Wg_Obj *obj, void *userdata) |
Set the userdata for an object. More... | |
bool | Wg_TryGetUserdata (const Wg_Obj *obj, const char *type, void **userdata) |
Get the userdata from an object if it is of the expected type. More... | |
void | Wg_RegisterFinalizer (Wg_Obj *obj, Wg_Finalizer finalizer, void *userdata) |
Register a finalizer to run when an object is garbage collected. More... | |
bool | Wg_HasAttribute (Wg_Obj *obj, const char *attribute) |
Check if an object has an attribute. More... | |
Wg_Obj * | Wg_GetAttribute (Wg_Obj *obj, const char *attribute) |
Get an attribute of an object. If the attribute does not exist, an AttributeError is raised. If the attribute is an unbound method object, a new method object is allocated with obj bound. If this allocation fails, a MemoryError is raised. More... | |
Wg_Obj * | Wg_GetAttributeNoExcept (Wg_Obj *obj, const char *attribute) |
Get an attribute of an object. Unlike, Wg_GetAttribute(), this function does not raise exceptions. More... | |
void | Wg_SetAttribute (Wg_Obj *obj, const char *attribute, Wg_Obj *value) |
Set an attribute of an object. More... | |
Wg_Obj * | Wg_GetAttributeFromBase (Wg_Obj *obj, const char *attribute, Wg_Obj *baseClass) |
Get an attribute of an object, skipping attributes that belong to the most derived layer. More... | |
bool | Wg_Iterate (Wg_Obj *obj, void *userdata, Wg_IterationCallback callback) |
Iterate over an iterable object. More... | |
bool | Wg_Unpack (Wg_Obj *obj, int count, Wg_Obj **values) |
Helper function to unpack an iterable object into an array of objects. More... | |
Wg_Obj * | Wg_GetKwargs (Wg_Context *context) |
Get the keyword arguments dictionary passed to the current function. More... | |
void * | Wg_GetFunctionUserdata (Wg_Context *context) |
Get the userdata associated with the current function. More... | |
Wg_Obj * | Wg_Call (Wg_Obj *callable, Wg_Obj **argv, int argc, Wg_Obj *kwargs) |
Call a callable object. More... | |
Wg_Obj * | Wg_CallMethod (Wg_Obj *obj, const char *method, Wg_Obj **argv, int argc, Wg_Obj *kwargs) |
Call a method on a object. More... | |
Wg_Obj * | Wg_CallMethodFromBase (Wg_Obj *obj, const char *method, Wg_Obj **argv, int argc, Wg_Obj *kwargs, Wg_Obj *baseClass) |
Call a method on a object, skipping methods which belong to the most derived layer. More... | |
bool | Wg_ParseKwargs (Wg_Obj *dict, const char *const *keys, int keysLen, Wg_Obj **values) |
Get the values from a kwargs parameter. More... | |
Wg_Obj * | Wg_GetIndex (Wg_Obj *obj, Wg_Obj *index) |
Index an object. More... | |
Wg_Obj * | Wg_SetIndex (Wg_Obj *obj, Wg_Obj *index, Wg_Obj *value) |
Set an index of an object. More... | |
Wg_Obj * | Wg_UnaryOp (Wg_UnOp op, Wg_Obj *arg) |
Perform a unary operation. More... | |
Wg_Obj * | Wg_BinaryOp (Wg_BinOp op, Wg_Obj *lhs, Wg_Obj *rhs) |
Perform a binary operation. More... | |
void | Wg_RegisterModule (Wg_Context *context, const char *name, Wg_ModuleLoader loader) |
Register a callback to be called when a module with the given name is imported. In the callback, new members can be added to the module. More... | |
Wg_Obj * | Wg_ImportModule (Wg_Context *context, const char *module, const char *alias) |
Import a module. More... | |
Wg_Obj * | Wg_ImportFromModule (Wg_Context *context, const char *module, const char *name, const char *alias) |
Import a specific name from a module. More... | |
bool | Wg_ImportAllFromModule (Wg_Context *context, const char *module) |
Import all names from a module. More... | |
The wings API.
The configuration used to initialise an interpreter.
The configuration cannot be changed after the interpreter is initialised.
typedef struct Wg_Context Wg_Context |
An opaque type representing the state of an interpreter.
typedef void(* Wg_ErrorCallback) (const char *message) |
The signature of the fatal error callback.
message | A null terminated string containing the error message. |
typedef void(* Wg_Finalizer) (void *userdata) |
The signature of an object finalizer.
userdata | The userdata specified when this callback was registered. |
typedef double Wg_float |
The underlying data type of a float object.
typedef Wg_Obj *(* Wg_Function) (Wg_Context *context, Wg_Obj **argv, int argc) |
The signature of a native function registered into the interpreter as a function object.
To get the keyword arguments passed to the function, call Wg_GetKwargs().
context | The associated context. |
argv | An array of objects passed to the function. |
argc | The length of the argv array. |
typedef bool(* Wg_IterationCallback) (Wg_Obj *obj, void *userdata) |
The signature of an iteration callback used by Wg_Iterate().
The yielded object is protected from garbage collection for the duration of this function.
obj | The object yielded by iteration. |
userdata | The userdata specified in Wg_Iterate. |
typedef bool(* Wg_ModuleLoader) (Wg_Context *context) |
The signature of a module loader.
context | The associated context. |
typedef void(* Wg_PrintFunction) (const char *message, int len, void *userdata) |
The signature of the print function used by the interpreter for printing.
message | An array of bytes to be printed. |
len | The length of the message in bytes. |
userdata | The userdata specified when this callback was registered. |
enum Wg_BinOp |
The binary operation to be used by Wg_BinaryOp.
enum Wg_Exc |
The type exception to be raised by Wg_RaiseException.
enum Wg_UnOp |
The unary operation to be used Wg_UnaryOp.
Perform a binary operation.
op | The binary operation to perform. |
lhs | The left hand side operand. |
rhs | The right hand side operand. |
Wg_Obj * Wg_BindMethod | ( | Wg_Obj * | klass, |
const char * | name, | ||
Wg_Function | fptr, | ||
void * | userdata | ||
) |
Instantiate a function object and bind it to a class.
To get the userdata in the function, call Wg_GetFunctionUserdata(). To get the keyword arguments passed to the function, call Wg_GetKwargs().
klass | The class to bind the method to. |
name | The name of the method. |
fptr | The native function to be bound. |
userdata | The userdata to pass to the function when it is called. |
Call a callable object.
Calling a class object will instantiate the class.
The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.
callable | The object to call. |
argv | An array of arguments to pass to the callable object. If argc is 0 then this can be NULL. |
argc | The length of the argv array. |
kwargs | A dictionary object containing the keyword arguments or NULL if none. |
Wg_Obj * Wg_CallMethod | ( | Wg_Obj * | obj, |
const char * | method, | ||
Wg_Obj ** | argv, | ||
int | argc, | ||
Wg_Obj * | kwargs | ||
) |
Call a method on a object.
This is equivalent to calling Wg_GetAttribute() and then Wg_Call().
The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.
obj | The object to call the method on. |
method | The method to call. |
argv | An array of arguments to pass to the callable object. If argc is 0 then this can be NULL. |
argc | The length of the argv array. |
kwargs | A dictionary object containing the keyword arguments or NULL if none. |
Wg_Obj * Wg_CallMethodFromBase | ( | Wg_Obj * | obj, |
const char * | method, | ||
Wg_Obj ** | argv, | ||
int | argc, | ||
Wg_Obj * | kwargs, | ||
Wg_Obj * | baseClass | ||
) |
Call a method on a object, skipping methods which belong to the most derived layer.
This is equivalent to calling Wg_GetAttributeFromBase() and then Wg_Call().
This is useful if the method is shadowed by the derived class. If multiple bases contain this attribute, the first attribute found is returned.
The kwargs parameter must be a dictionary with string keys, otherwise a TypeError is raised.
obj | The object to call the method on. |
method | The method to call. |
argv | An array of arguments to pass to the callable object. If argc is 0 then this can be NULL. |
argc | The length of the argv array. |
kwargs | A dictionary object containing the keyword arguments or NULL if none. |
baseClass | The base class to search in, or NULL to search in all bases. |
void Wg_ClearException | ( | Wg_Context * | context | ) |
Clear the current exception.
context | The associated context. |
void Wg_CollectGarbage | ( | Wg_Context * | context | ) |
Force run the garbage collector and free all unreachable objects.
context | The associated context. |
Wg_Obj * Wg_Compile | ( | Wg_Context * | context, |
const char * | script, | ||
const char * | prettyName | ||
) |
Compile a script into a function object.
An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.
context | The associated context. |
script | The script to compile. |
prettyName | The name to run the script under, or NULL to use a default name. |
Wg_Obj * Wg_CompileExpression | ( | Wg_Context * | context, |
const char * | script, | ||
const char * | prettyName | ||
) |
Compile an expression into a function object.
An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.
context | The associated context. |
script | The expression to compile. |
prettyName | The name to run the script under, or NULL to use a default name. |
Wg_Context * Wg_CreateContext | ( | const Wg_Config * | config | ) |
Create an instance of an interpreter.
The returned context must be freed with Wg_DestroyContext(). This function does not fail.
config | The configuration to use, or NULL to use the default configuration. |
void Wg_DecRef | ( | Wg_Obj * | obj | ) |
Decrement the reference count of an object. A positive reference count prevents the object from being garbage collected.
The reference count must not be negative.
obj | The object whose reference count is to be decremented. |
void Wg_DefaultConfig | ( | Wg_Config * | config | ) |
Get the default configuration.
[out] | config | The returned default configuration. |
void Wg_DestroyContext | ( | Wg_Context * | context | ) |
Free a context created with Wg_CreateContext().
context | The context to free. |
bool Wg_Execute | ( | Wg_Context * | context, |
const char * | script, | ||
const char * | prettyName | ||
) |
Execute a script.
An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.
context | The associated context. |
script | The script to execute. |
prettyName | The name to run the script under, or NULL to use a default name. |
Wg_Obj * Wg_ExecuteExpression | ( | Wg_Context * | context, |
const char * | script, | ||
const char * | prettyName | ||
) |
Execute a script containing an expression.
An exception will be raised if there are any errors and should be handled with Wg_ClearException() or propagated.
context | The associated context. |
script | The expression to execute. |
prettyName | The name to run the script under, or NULL to use a default name. |
Get an attribute of an object. If the attribute does not exist, an AttributeError is raised. If the attribute is an unbound method object, a new method object is allocated with obj bound. If this allocation fails, a MemoryError is raised.
obj | The object to get the attribute from. |
attribute | The attribute to get. |
Get an attribute of an object, skipping attributes that belong to the most derived layer.
This is useful if the attribute is shadowed by the derived class. If multiple bases contain this attribute, the first attribute found is returned.
obj | The object to get the attribute from. |
attribute | The attribute to get. |
baseClass | The base class to search in, or NULL to search in all bases. |
Get an attribute of an object. Unlike, Wg_GetAttribute(), this function does not raise exceptions.
obj | The object to get the attribute from. |
attribute | The attribute to get. |
bool Wg_GetBool | ( | const Wg_Obj * | obj | ) |
Get the value from a boolean object.
obj | The object to get the value from. |
Wg_Context * Wg_GetContextFromObject | ( | Wg_Obj * | obj | ) |
Get the context associated with an object.
obj | The object. |
const char * Wg_GetErrorMessage | ( | Wg_Context * | context | ) |
Get the current exception message.
context | The associated context. |
Wg_Obj * Wg_GetException | ( | Wg_Context * | context | ) |
Get the current exception object.
context | The associated context. |
Get the float value from an integer or float object.
obj | The object to get the value from. |
void * Wg_GetFunctionUserdata | ( | Wg_Context * | context | ) |
Get the userdata associated with the current function.
Wg_Obj * Wg_GetGlobal | ( | Wg_Context * | context, |
const char * | name | ||
) |
Get a global variable in the current module namespace.
context | The associated context. |
name | The name of the global variable. |
Index an object.
This calls the __getitem__ method.
obj | The object to index. |
index | The index. |
Get the value from an integer object.
obj | The object to get the value from. |
Wg_Obj * Wg_GetKwargs | ( | Wg_Context * | context | ) |
Get the keyword arguments dictionary passed to the current function.
context | The associated context. |
const char * Wg_GetString | ( | const Wg_Obj * | obj, |
int * | len | ||
) |
Get the value from a string object.
obj | The object to get the value from. | |
[out] | len | The length of the string. This parameter may be NULL. |
bool Wg_HasAttribute | ( | Wg_Obj * | obj, |
const char * | attribute | ||
) |
Check if an object has an attribute.
obj | The object to check. |
attribute | The attribute to check. |
bool Wg_ImportAllFromModule | ( | Wg_Context * | context, |
const char * | module | ||
) |
Import all names from a module.
If the names are imported successfully, the objects bound to the names are bound to global variables with the same names.
context | The associated context. |
module | The name of the module to import. |
Wg_Obj * Wg_ImportFromModule | ( | Wg_Context * | context, |
const char * | module, | ||
const char * | name, | ||
const char * | alias | ||
) |
Import a specific name from a module.
If the name is imported successfully, the object bound to the name is bound to a global variable with the same name (or the alias provided).
context | The associated context. |
module | The name of the module to import from. |
name | The name to import. |
alias | The alias to import the name under, or NULL to use the same name. |
Wg_Obj * Wg_ImportModule | ( | Wg_Context * | context, |
const char * | module, | ||
const char * | alias | ||
) |
Import a module.
If the module has already been imported before, the module is not reloaded.
If the module is imported successfully, the module object is bound to a global variable with the same name (or the alias provided).
context | The associated context. |
module | The name of the module to import. |
alias | The alias to import the module under, or NULL to use the same name. |
void Wg_IncRef | ( | Wg_Obj * | obj | ) |
Increment the reference count of an object. A positive reference count prevents the object from being garbage collected.
obj | The object whose reference count is to be incremented. |
bool Wg_IsBool | ( | const Wg_Obj * | obj | ) |
Check if an object is a boolean.
obj | The object to inspect. |
bool Wg_IsClass | ( | const Wg_Obj * | obj | ) |
Check if an object is a class.
obj | The object to inspect. |
bool Wg_IsDictionary | ( | const Wg_Obj * | obj | ) |
Check if an object is a dictionary.
obj | The object to inspect. |
bool Wg_IsFunction | ( | const Wg_Obj * | obj | ) |
Check if an object is a function.
obj | The object to inspect. |
Check if an object's class derives from any of the specified classes.
instance | The object to be checked. |
types | An array of class objects to be checked against. This can be NULL if typesLen is 0. |
typesLen | The length of the types array. |
bool Wg_IsInt | ( | const Wg_Obj * | obj | ) |
Check if an object is an integer.
obj | The object to inspect. |
bool Wg_IsIntOrFloat | ( | const Wg_Obj * | obj | ) |
Check if an object is a float.
obj | The object to inspect. |
bool Wg_IsList | ( | const Wg_Obj * | obj | ) |
Check if an object is a list.
obj | The object to inspect. |
bool Wg_IsNone | ( | const Wg_Obj * | obj | ) |
Check if an object is None.
obj | The object to inspect. |
bool Wg_IsSet | ( | const Wg_Obj * | obj | ) |
Check if an object is a set.
obj | The object to inspect. |
bool Wg_IsString | ( | const Wg_Obj * | obj | ) |
Check if an object is a string.
obj | The object to inspect. |
bool Wg_IsTuple | ( | const Wg_Obj * | obj | ) |
Check if an object is a tuple.
obj | The object to inspect. |
bool Wg_Iterate | ( | Wg_Obj * | obj, |
void * | userdata, | ||
Wg_IterationCallback | callback | ||
) |
Iterate over an iterable object.
The object must be iterable, otherwise an exception is raised.
The object is protected from garbage collection until the function returns.
obj | The object to iterate over. |
userdata | The userdata to be passed to the callback function. |
callback | A function to be called for each value yielded by iteration. See Wg_IterationCallback for more details on this function. |
Wg_Obj * Wg_NewBool | ( | Wg_Context * | context, |
bool | value | ||
) |
Instantiate a boolean object.
Unlike the other Wg_NewXXX functions, this function always succeeds.
context | The associated context. |
value | The value of the object. |
Wg_Obj * Wg_NewClass | ( | Wg_Context * | context, |
const char * | name, | ||
Wg_Obj ** | bases, | ||
int | basesLen | ||
) |
Instantiate a class object.
Methods can be added later with Wg_BindMethod() and data members can be added to instances inside the __init__ method with Wg_SetAttribute().
If no bases are specified, the object class is implicitly used as a base.
The name of the class cannot begin with two underscores.
context | The associated context. |
name | The name of the class. |
bases | An array of class objects to be used as a base. This can be NULL if basesLen is 0. |
basesLen | The length of the bases array. |
Wg_Obj * Wg_NewDictionary | ( | Wg_Context * | context, |
Wg_Obj ** | keys, | ||
Wg_Obj ** | values, | ||
int | len | ||
) |
Instantiate a dictionary object.
The keys must be hashable, otherwise a TypeError is raised.
context | The associated context. |
keys | An array of keys to initialise the dictionary with. This can be NULL if argc is 0. |
values | An array of values to initialise the dictionary with. This can be NULL if argc is 0. |
len | The length of the keys and values arrays. |
Wg_Obj * Wg_NewFloat | ( | Wg_Context * | context, |
Wg_float | value | ||
) |
Instantiate a float object.
context | The associated context. |
value | The value of the object. |
Wg_Obj * Wg_NewFunction | ( | Wg_Context * | context, |
Wg_Function | fptr, | ||
void * | userdata, | ||
const char * | prettyName | ||
) |
Instantiate a function object.
To get the userdata in the function, call Wg_GetFunctionUserdata(). To get the keyword arguments passed to the function, call Wg_GetKwargs().
context | The associated context. |
fptr | The native function to be bound. |
userdata | The userdata to pass to the function when it is called. |
prettyName | The name of the function, or NULL to use a default name. |
Wg_Obj * Wg_NewInt | ( | Wg_Context * | context, |
Wg_int | value | ||
) |
Instantiate an integer object.
context | The associated context. |
value | The value of the object. |
Wg_Obj * Wg_NewList | ( | Wg_Context * | context, |
Wg_Obj ** | argv, | ||
int | argc | ||
) |
Instantiate a list object.
context | The associated context. |
argv | An array of objects to initialise the list with. This can be NULL if argc is 0. |
argc | The length of the argv array. |
Wg_Obj * Wg_NewSet | ( | Wg_Context * | context, |
Wg_Obj ** | argv, | ||
int | argc | ||
) |
Instantiate a set object.
context | The associated context. |
argv | An array of objects to initialise the set with. This can be NULL if argc is 0. |
argc | The length of the argv array. |
Wg_Obj * Wg_NewString | ( | Wg_Context * | context, |
const char * | value | ||
) |
Instantiate a string object.
context | The associated context. |
value | A null terminated string, or NULL for an empty string. |
Wg_Obj * Wg_NewStringBuffer | ( | Wg_Context * | context, |
const char * | buffer, | ||
int | len | ||
) |
Instantiate a string object from a buffer.
context | The associated context. |
buffer | The buffer. |
len | The length of the buffer. |
Wg_Obj * Wg_NewTuple | ( | Wg_Context * | context, |
Wg_Obj ** | argv, | ||
int | argc | ||
) |
Instantiate a tuple object.
context | The associated context. |
argv | An array of objects to initialise the tuple with. This can be NULL if argc is 0. |
argc | The length of the argv array. |
Wg_Obj * Wg_None | ( | Wg_Context * | context | ) |
Get the None singleton value.
context | The associated context. |
Get the values from a kwargs parameter.
dict | The dictionary to get the values from. If this is NULL, then an empty dictionary is assumed. | |
keys | The keys to look up. | |
keysLen | The length of the keys array. | |
[out] | values | The values in the dictionary corresponding to the keys. The values are given in the order that the keys are given and will be NULL for keys that were not found. |
void Wg_Print | ( | const Wg_Context * | context, |
const char * | message, | ||
int | len | ||
) |
Print a message.
This function uses the print function specified in the configuration.
context | The associated context. |
message | A byte array containing the message. This can NULL if len is 0. |
len | The length of the message byte array. |
void Wg_PrintString | ( | const Wg_Context * | context, |
const char * | message | ||
) |
void Wg_RaiseArgumentCountError | ( | Wg_Context * | context, |
int | given, | ||
int | expected | ||
) |
Raise a TypeError with a formatted message.
If an exception is already set, the old exception will be overwritten.
context | The associated context. |
given | The number of arguments given. |
expected | The number of arguments expected, or -1 if this is not a fixed number. |
void Wg_RaiseArgumentTypeError | ( | Wg_Context * | context, |
int | index, | ||
const char * | expected | ||
) |
Raise a TypeError with a formatted message.
If an exception is already set, the old exception will be overwritten.
context | The associated context. |
index | The parameter index of the invalid argument. |
expected | A string describing the expected type. |
void Wg_RaiseAttributeError | ( | const Wg_Obj * | obj, |
const char * | attribute | ||
) |
Raise a AttributeError with a formatted message.
If an exception is already set, the old exception will be overwritten.
obj | The object missing the attribute. |
attribute | The missing attribute. |
void Wg_RaiseException | ( | Wg_Context * | context, |
Wg_Exc | type, | ||
const char * | message | ||
) |
Create and raise an exception.
If an exception is already set, the old exception will be overwritten.
context | The associated context. |
type | The exception type. |
message | The error message, or NULL for an empty string. |
void Wg_RaiseExceptionClass | ( | Wg_Obj * | klass, |
const char * | message | ||
) |
Create and raise an exception using a class object.
The class must derive from BaseException, otherwise a TypeError is raised.
If an exception is already set, the old exception will be overwritten.
klass | The exception class. |
message | The error message, or NULL for an empty string. |
void Wg_RaiseExceptionObject | ( | Wg_Obj * | obj | ) |
Raise an existing exception object.
The object's type must derive from BaseException, otherwise a TypeError is raised.
If an exception is already set, the old exception will be overwritten.
obj | The exception object to raise. The type must be a subclass of BaseException. |
void Wg_RaiseKeyError | ( | Wg_Context * | context, |
Wg_Obj * | key | ||
) |
Raise a KeyError with a formatted message.
If an exception is already set, the old exception will be overwritten.
context | The associated context. |
key | The key that caused the KeyError, or NULL to leave unspecified. |
void Wg_RaiseNameError | ( | Wg_Context * | context, |
const char * | name | ||
) |
Raise a NameError with a formatted message.
If an exception is already set, the old exception will be overwritten.
context | The associated context. |
name | The name that was not found. |
void Wg_RegisterFinalizer | ( | Wg_Obj * | obj, |
Wg_Finalizer | finalizer, | ||
void * | userdata | ||
) |
Register a finalizer to run when an object is garbage collected.
Multiple finalizers may be registered.
obj | The object to register the finalizer for. |
finalizer | The finalizer function. |
userdata | The userdata to pass to the finalizer function. |
void Wg_RegisterModule | ( | Wg_Context * | context, |
const char * | name, | ||
Wg_ModuleLoader | loader | ||
) |
Register a callback to be called when a module with the given name is imported. In the callback, new members can be added to the module.
context | The associated context. |
name | The name of the module to register the callback for. |
loader | The callback to register. The callback should return false on failure and otherwise true. |
void Wg_ReraiseExceptionObject | ( | Wg_Obj * | obj | ) |
Raise an existing exception object without affecting the stack trace.
The object's type must derive from BaseException, otherwise a TypeError is raised.
If an exception is already set, the old exception will be overwritten.
obj | The exception object to raise. The type must be a subclass of BaseException. |
Set an attribute of an object.
obj | The object to set the attribute for. |
attribute | The attribute to set. |
value | The attribute value. |
void Wg_SetErrorCallback | ( | Wg_ErrorCallback | callback | ) |
Set a callback for programmer errors.
This function is threadsafe.
callback | The callback to use, or NULL to default to abort(). |
void Wg_SetGlobal | ( | Wg_Context * | context, |
const char * | name, | ||
Wg_Obj * | value | ||
) |
Set a global variable in the current module namespace.
context | The associated context. |
name | The name of the global variable. |
value | The value to set. |
Set an index of an object.
This calls the __setitem__ method.
obj | The object to index. |
index | The index. |
value | The value to set. |
void Wg_SetUserdata | ( | Wg_Obj * | obj, |
void * | userdata | ||
) |
Set the userdata for an object.
If the userdata requires cleanup (e.g. freeing memory), Wg_RegisterFinalizer() may be useful for automatically performing the cleanup when it the object is no longer needed.
obj | The object to set the userdata for. |
userdata | The userdata to set. |
bool Wg_TryGetUserdata | ( | const Wg_Obj * | obj, |
const char * | type, | ||
void ** | userdata | ||
) |
Get the userdata from an object if it is of the expected type.
obj | The object to get the value from. | |
type | The type to match. | |
[out] | userdata | The userdata. This parameter may be NULL. |
Perform a unary operation.
op | The unary operation to perform. |
arg | The operand. |
Helper function to unpack an iterable object into an array of objects.
If the number of objects yielded by the iterator does not match the count parameter, a ValueError is raised.
obj | The object to iterate over. | |
count | The expected number of values. | |
[out] | values | The unpacked objects. |