Built-in functions

Squirrel 2.2

Built-in functions

The squirrel virtual machine has a set of built utility functions.

Global symbols

array(size,[fill])

create and returns array of a specified size.if the optional parameter fill is specified its value will be used to fill the new array's slots. If the fill paramter is omitted null is used instead.

seterrorhandler(func)

sets the runtime error handler

setdebughook(hook_func)

sets the debug hook

enabledebuginfo(enable)

enable/disable the debug line information generation at compile time. enable != null enables . enable == null disables.

getroottable()

returns the root table of the VM.

setroottable(table)

sets the root table of the VM.

getconsttable()

returns the const table of the VM.

setconsttable(table)

sets the const table of the VM.

assert(exp)

throws an exception if exp is null

print(x)

prints x in the standard output

compilestring(string,[buffername])

compiles a string containing a squirrel script into a function and returns it

local compiledscript=compilestring("::print(\"ciao\")");
//run the script				
compiledscript();
				

collectgarbage()

calls the garbage collector and returns the number of reference cycles found(and deleted)

type(obj)

return the 'raw' type of an object without invoking the metatmethod '_typeof'.

getstackinfos(level)

returns the stack informations of a given call stack level. returns a table formatted as follow:

{
	func="DoStuff",	//function name
	
	src="test.nut",	//source file
	
	line=10,		//line number
	
	locals = {		//a table containing the local variables
	
		a=10,
		
		testy="I'm a string"
	}
}
				

level = 0 is the current function, level = 1 is the caller and so on. If the stack level doesn't exist the function returns null.

newthread(threadfunc)

creates a new cooperative thread object(coroutine) and returns it

_version_

string values describing the version of VM and compiler.

_charsize_

size in bytes of the internal VM rapresentation for characters(1 for ASCII builds 2 for UNICODE builds).

_intsize_

size in bytes of the internal VM rapresentation for integers(4 for 32bits builds 8 for 64bits builds).

_floatsize_

size in bytes of the internal VM rapresentation for floats(4 for single precision builds 8 for double precision builds).

Default delegates

Except null and userdata every squirrel object has a default delegate containing a set of functions to manipulate and retrieve information from the object itself.

Integer

tofloat()

convert the number to float and returns it

tostring()

converts the number to string and returns it

tointeger()

returns the value of the integer(dummy function)

tochar()

returns a string containing a single character rapresented by the integer.

weakref()

dummy function, returns the integer itself.

Float

tofloat()

returns the value of the float(dummy function)

tointeger()

converts the number to integer and returns it

tostring()

converts the number to string and returns it

tochar()

returns a string containing a single character rapresented by the integer part of the float.

weakref()

dummy function, returns the float itself.

Bool

tofloat()

returns 1.0 for true 0.0 for false

tointeger()

returns 1 for true 0 for false

tostring()

returns "true" for true "false" for false

weakref()

dummy function, returns the bool itself.

String

len()

returns the string length

tointeger()

converts the string to integer and returns it

tofloat()

converts the string to float and returns it

tostring()

returns the string(dummy function)

slice(start,[end])

returns a section of the string as new string. Copies from start to the end (not included). If start is negative the index is calculated as length + start, if end is negative the index is calculated as length + start. If end is omitted end is equal to the string length.

find(substr,[startidx])

search a sub string(substr) starting from the index startidx and returns the index of its first occurrence. If startidx is omitted the search operation starts from the beginning of the string. The function returns null if substr is not found.

tolower()

returns a lowercase copy of the string.

toupper()

returns a uppercase copy of the string.

weakref()

returns a weak reference to the object.

Table

len()

returns the number of slots contained in a table

rawget(key)

tries to get a value from the slot ‘key’ without employ delegation

rawset(key,val)

sets the slot ‘key’ with the value ‘val’ without employing delegation. If the slot do not exists , it will be created.

rawdelete()

deletes the slot key without emplying delegetion and retunrs his value. if the slo does not exists returns always null.

rawin(key)

returns true if the slot ‘key’ exists. the function has the same eddect as the operator 'in' but does not employ delegation.

weakref()

returns a weak reference to the object.

tostring()

tries to invoke the _tostring metamethod, if failed. returns "(table : pointer)".

clear()

removes all the slot from the table

Array

len()

returns the length of the array

append(val)

appends the value ‘val’ at the end of the array

push(val)

appends the value ‘val’ at the end of the array

extend(array)

Extends the array by appending all the items in the given array.

pop()

removes a value from the back of the array and returns it.

top()

returns the value of the array with the higher index

insert(idx,val)

inserst the value ‘val’ at the position ‘idx’ in the array

remove(idx)

removes the value at the position ‘idx’ in the array

resize(size,[fill])

resizes the array, if the optional parameter fill is specified its value will be used to fill the new array's slots(if the size specified is bigger than the previous size) . If the fill paramter is omitted null is used instead.

sort([compare_func])

sorts the array. a custom compare function can be optionally passed.The function prototype as to be the following.

function custom_compare(a,b)
{
	if(a>b) return 1
	else if(a<b) return -1
	return 0;
}
					

reverse()

reverse the elements of the array in place

slice(start,[end])

returns a section of the array as new array. Copies from start to the end (not included). If start is negative the index is calculated as length + start, if end is negative the index is calculated as length + start. If end is omitted end is equal to the array length.

weakref()

returns a weak reference to the object.

tostring()

returns the string "(array : pointer)".

clear()

removes all the items from the array

Function

call(_this,args…)

calls the function with the specified environment object(’this’) and parameters

pcall(_this,args…)

calls the function with the specified environment object(’this’) and parameters, this function will not invoke the error callback in case of failure(pcall stays for 'protected call')

acall(array_args)

calls the function with the specified environment object(’this’) and parameters. The function accepts an array containing the parameters that will be passed to the called function.

pacall(array_args)

calls the function with the specified environment object(’this’) and parameters. The function accepts an array containing the parameters that will be passed to the called function.This function will not invoke the error callback in case of failure(pacall stays for 'protected array call')

weakref()

returns a weak reference to the object.

tostring()

returns the string "(closure : pointer)".

tostring()

returns the string "(closure : pointer)".

bindenv(env)

clones the function(aka closure) and bind the enviroment object to it(table,class or instance). the this parameter of the newly create function will always be set to env. Note that the created function holds a weak reference to its environment object so cannot be used to control its lifetime.

Class

instance()

returns a new instance of the class. this function does not invoke the instance constructor. The constructor must be explicitly called( eg. class_inst.constructor(class_inst) ).

getattributes(membername)

returns the attributes of the specified member. if the parameter member is null the function returns the class level attributes.

getattributes(membername,attr)

sets the attribute of the specified member and returns the previous attribute value. if the parameter member is null the function sets the class level attributes.

rawin(key)

returns true if the slot ‘key’ exists. the function has the same eddect as the operator 'in' but does not employ delegation.

weakref()

returns a weak reference to the object.

tostring()

returns the string "(class : pointer)".

Class Instance

getclass()

returns the class that created the instance.

rawin(key)

returns true if the slot ‘key’ exists. the function has the same eddect as the operator 'in' but does not employ delegation.

weakref()

returns a weak reference to the object.

tostring()

tries to invoke the _tostring metamethod, if failed. returns "(insatnce : pointer)".

Generator

getstatus()

returns the status of the generator as string : “running”, ”dead” or ”suspended”.

weakref()

returns a weak reference to the object.

tostring()

returns the string "(generator : pointer)".

Thread

call(...)

starts the thread with the specified parameters

wakeup([wakeupval])

wakes up a suspended thread, accepts a optional parameter that will be used as return value for the function that suspended the thread(usually suspend())

getstatus()

returns the status of the thread ("idle","running","suspended")

weakref()

returns a weak reference to the object.

tostring()

returns the string "(thread : pointer)".

Weak Reference

ref()

returns the object that the weak reference is pointing at, null if the object that was point at was destroyed.

weakref()

returns a weak reference to the object.

tostring()

returns the string "(weakref : pointer)".