-
Notifications
You must be signed in to change notification settings - Fork 0
Wi API Reference
If you're using Wi API to create a library, your library must have a wi_foreign_init function marked as WI_FOREIGN_INIT with 1 parameter being wi_state* state.
Expands to:
-
#ifdef _WIN32:__declspec(dllexport) -
#else:
Used to mark all public API functions
Expands to:
-
#ifdef _WIN32:__declspec(dllexport) -
#else:
Used to mark foreign library entry point (wi_foreign_init)
Wi's number type
Opaque Wi object handle
The result of running Wi code
-
WI_RUN_OK: No errors occurred -
WI_RUN_ERROR: A runtime error or a compile error occurred, also used when out of memory -
WI_RUN_ABORT: Execution was aborted early viawi_state_abort
Opaque Wi state handle
Foreign (C) function pointer, called from Wi scripts
Userdata finalizer - function called when the userdata gets collected by GC
Function called in the require statement. Use this in a custom virtual filesystem (your app, for example).
Must return Wi code
Function used to check whether a required file exists, called at compile time.
Must return whether a file exists
Define the standard library in a state
Parameters:
-
wi_state* state: Wi state instance
Define a foreign (C) function in the state
Parameters:
-
wi_state* state: Wi state instance -
const char* name: Function name -
wi_foreign_fn fn: Pointer to the C function implementation -
int arity: Function's arity (number of arguments it expects) -
bool is_variadic: Whether function is variadic or not
Define an object in the state (global)
Parameters:
-
wi_state* state: Wi state instance -
const char* name: Object name
Returns: wi_object* Pointer to the created object
Set a real field on an object
Parameters:
-
wi_state* state: Wi state instance -
wi_object* object: Target object -
const char* name: Field name -
wi_real real: Value to set
Set a boolean field on an object
Parameters:
-
wi_state* state: Wi state instance -
wi_object* object: Target object -
const char* name: Field name -
bool boolean: Value to set
Set a string field on an object
Parameters:
-
wi_state* state: Wi state instance -
wi_object* object: Target object -
const char* name: Field name -
char* string: Value to set
Set userdata as a field on an object
Parameters:
-
wi_state* state: Wi state instance -
wi_object* object: Target object -
const char* field_name: Field name -
const char* name: Userdata name, used for type-checking -
void* userdata: Pointer to userdata -
wi_userdata_finalizer_fn finalizer: Userdata finalizer
Set a foreign (C) function as a field on an object
Parameters:
-
wi_state* state: Wi state instance -
wi_object* object: Target object -
const char* name: Field name -
wi_foreign_fn fn: Pointer to the C function implementation -
int arity: Function's arity (number of arguments it expects) -
bool is_variadic: Whether function is variadic or not
Create a new Wi state instance
Parameters:
-
wi_conf conf: Wi configuration, seewi_conf.hfor more
Returns: wi_state* Created Wi state instance
Delete a Wi state instance and free all associated memory
Parameters:
-
wi_state* state: Wi state instance
Get the error message from the last compile or runtime error
Parameters:
-
wi_state* state: Wi state instance
Returns: const char* Error message, NULL if none occurred
Set the require load callback
Parameters:
-
wi_state* state: Wi state instance -
wi_load_require_fn fn: Load callback function
Set the require existence check callback
Parameters:
-
wi_state* state: Wi state instance -
wi_require_exists_fn fn: Existence check callback function
Set the command line arguments that will be available to Wi scripts via os.args
Parameters:
-
wi_state* state: Wi state instance -
int argc: Number of arguments -
const char** argv: Array of argument strings
Throw a runtime error in the state
Parameters:
-
wi_state* state: Wi state instance -
const char* format:printfformat string -
...: Format arguments
Request the state to stop execution, returning WI_RUN_ABORT from wi_state_run. Must only be called while a script is running (e.g., from a foreign (C) function). Calling it outside wi_state_run is undefined behavior
Parameters:
-
wi_state* state: Wi state instance
Request the state to stop execution as soon as possible, returning WI_RUN_ABORT from wi_state_run. In contrast to wi_state_abort, this function is safe to call asynchronously (e.g., from a signal handler or another thread)
Parameters:
-
wi_state* state: Wi state instance
Execute Wi code
Parameters:
-
wi_state* state: Wi state instance -
const char* file_path: Path to the script, used for error messages -
const char* src: Code string
Returns: wi_run_result Run result
Example:
wi_find_function(state, "sum"); /* Find the global function "sum" and push it onto the stack */
wi_push_real(state, 10); /* Push argument 1 */ wi_push_real(state, 20); /* Push argument 2 */
wi_call(state, 2); /* Call function with `2` arguments, is protected */
wi_real sum = wi_check_real(state); /* Get the function result, with type-checking */
printf("%g\n", sum); /* Print it */Find a global function and push it onto the stack
Parameters:
-
wi_state* state: Wi state instance -
const char *name: Function name
Returns: bool
Check if the value at the stack top is a real value
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Check if the value at the stack top is a null value
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Check if the value at the stack top is a boolean value
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Check if the value at the stack top is a string value
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Check if the value at the stack top is userdata
Parameters:
-
wi_state* state: Wi state instance -
const char* name: Userdata name, used for type-checking
Returns: bool
Push a real value onto the stack
Parameters:
-
wi_state* state: Wi state instance -
wi_real real: Real
Push a null value onto the stack
Parameters:
-
wi_state* state: Wi state instance
Push a boolean value onto the stack
Parameters:
-
wi_state* state: Wi state instance -
bool boolean: Boolean
Push a string value onto the stack
Parameters:
-
wi_state* state: Wi state instance -
const char* string: String
Push userdata onto the stack
Parameters:
-
wi_state* state: Wi state instance -
const char* name: Userdata name, used for type-checking -
void* userdata: Pointer to userdata -
wi_userdata_finalizer_fn finalizer: Userdata finalizer
Pop a real value from the stack
Parameters:
-
wi_state* state: Wi state instance
Returns: wi_real
Pop a null value from the stack
Parameters:
-
wi_state* state: Wi state instance
Pop a boolean value from the stack
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Pop a string value from the stack
Parameters:
-
wi_state* state: Wi state instance -
int* len: Optional pointer to store the string length, can beNULL
Returns: char*
Pop userdata from the stack
Parameters:
-
wi_state* state: Wi state instance
Returns: void*
Pop a real value from the stack with type-checking
Parameters:
-
wi_state* state: Wi state instance
Returns: wi_real
Pop a boolean value from the stack with type-checking
Parameters:
-
wi_state* state: Wi state instance
Returns: bool
Pop a string value from the stack with type-checking
Parameters:
-
wi_state* state: Wi state instance -
int* len: Optional pointer to store the string length, can beNULL
Returns: char*
Pop userdata from the stack with type-checking
Parameters:
-
wi_state* state: Wi state instance -
const char* name: Userdata name, used for type-checking
Returns: void*
Call a Wi function, is protected.
Leaves the result on the stack top, which needs to be explicitly popped via one of wi_pop_X or wi_check_X functions
Parameters:
-
wi_state* state: Wi state instance -
uint8_t arg_count: Argument count -
char** error: Optional pointer to store the error message (if any), can beNULL, must be freed manually
Returns: bool
Used in C functions to get arguments from the Wi caller and to set the return value. Slot 0 is reserved for the return value.
Check if a slot contains a real value
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool
Check if a slot contains a null value
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool
Check if a slot contains a boolean value
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool
Check if a slot contains a string value
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool
Check if a slot contains userdata
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool
Store a real value in a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Store a null value in a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Store a boolean value in a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Store a string value in a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Store userdata in a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count]) -
const char* name: Userdata name, used for type-checking -
void* userdata: Pointer to userdata -
wi_userdata_finalizer_fn finalizer: Userdata finalizer
Get a real value from a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: wi_real Real stored in a slot
Get a boolean value from a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool Boolean stored in a slot
Get a string value from a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count]) -
int* len: Optional pointer to store the string length, can beNULL
Returns: char* String stored in a slot
Get userdata from a slot
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: void* Userdata stored in a slot
Get a real value from a slot with type-checking
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: wi_real Real stored in a slot
Get a boolean value from a slot with type-checking
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count])
Returns: bool Boolean stored in a slot
Get a string value from a slot with type-checking
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count]) -
int* len: Optional pointer to store the string length, can beNULL
Returns: char* String stored in a slot
Get userdata from a slot with type-checking
Parameters:
-
wi_state* state: Wi state instance -
int slot: Slot index (0-[arg_count]) -
const char* name: Userdata name, used for type-checking
Returns: void* Userdata stored in a slot
Expands to: 0
Default configuration (all flags disabled)
Expands to: "6.0.0-beta"
Wi version as a string
Value: 6
Value: 0
Value: 0
Value: 65535
Maximum number of constants in a function
Value: 65535
Maximum jump offset
Value: 65535
Maximum loop offset
Value: 255
Maximum number of local variables in a function
Value: 255
Maximum number of upvalues in a function (closure)
Value: 10485760
Initial heap size before first collection (10MB)
Value: 2
Heap growth factor per garbage collection run
Value: 200
Maximum depth of nested wi_state_call and of recoveries
Value: 20
Minimum number of values on the VM stack
Value: 1000000
Maximum number of values on the VM stack
Configuration flags for the Wi state
-
WI_CONF_PRINT_CODE: Print bytecode after compilation -
WI_CONF_STRESS_GC: Run garbage collection on every allocation -
WI_CONF_LOG_GC: Log garbage collection
Configuration bitmask type
Set a configuration flag
Parameters:
-
wi_conf* conf: Configuration bitmask -
wi_conf_flag flag: Configuration flag
Check if a configuration flag is set
Parameters:
-
wi_conf* conf: Configuration bitmask -
wi_conf_flag flag: Configuration flag
Returns: bool