1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
|
.. _builtin_functions:
==================
Built-in Functions
==================
.. index::
single: Built-in Functions
pair: Global Symbols; Built-in Functions
^^^^^^^^^^^^^^
Global Symbols
^^^^^^^^^^^^^^
.. js:function:: 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.
.. js:function:: seterrorhandler(func)
sets the runtime error handler
.. js:function:: callee()
returns the currently running closure
.. js:function:: setdebughook(hook_func)
sets the debug hook
.. js:function:: enabledebuginfo(enable)
enable/disable the debug line information generation at compile time. enable != null enables . enable == null disables.
.. js:function:: getroottable()
returns the root table of the VM.
.. js:function:: setroottable(table)
sets the root table of the VM. And returns the previous root table.
.. js:function:: getconsttable()
returns the const table of the VM.
.. js:function:: setconsttable(table)
sets the const table of the VM. And returns the previous const table.
.. js:function:: assert(exp)
throws an exception if exp is null
.. js:function:: print(x)
prints x in the standard output
.. js:function:: error(x)
prints x in the standard error output
.. js:function:: 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();
.. js:function:: collectgarbage()
runs the garbage collector and returns the number of reference cycles found(and deleted) This function only works on garbage collector builds.
.. js:function:: resurrectunreachable()
runs the garbage collector and returns an array containing all unreachable object found. If no unreachable object is found, null is returned instead. This function is meant to help debugging reference cycles. This function only works on garbage collector builds.
.. js:function:: type(obj)
return the 'raw' type of an object without invoking the metatmethod '_typeof'.
.. js:function:: 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.
.. js:function:: newthread(threadfunc)
creates a new cooperative thread object(coroutine) and returns it
.. js:data:: _versionnumber_
integer values describing the version of VM and compiler. eg. for Squirrel 3.0.1 this value will be 301
.. js:data:: _version_
string values describing the version of VM and compiler.
.. js:data:: _charsize_
size in bytes of the internal VM rapresentation for characters(1 for ASCII builds 2 for UNICODE builds).
.. js:data:: _intsize_
size in bytes of the internal VM rapresentation for integers(4 for 32bits builds 8 for 64bits builds).
.. js:data:: _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
^^^^^^^^
.. js:function:: integer.tofloat()
convert the number to float and returns it
.. js:function:: integer.tostring()
converts the number to string and returns it
.. js:function:: integer.tointeger()
returns the value of the integer(dummy function)
.. js:function:: integer.tochar()
returns a string containing a single character rapresented by the integer.
.. js:function:: integer.weakref()
dummy function, returns the integer itself.
^^^^^
Float
^^^^^
.. js:function:: float.tofloat()
returns the value of the float(dummy function)
.. js:function:: float.tointeger()
converts the number to integer and returns it
.. js:function:: float.tostring()
converts the number to string and returns it
.. js:function:: float.tochar()
returns a string containing a single character rapresented by the integer part of the float.
.. js:function:: float.weakref()
dummy function, returns the float itself.
^^^^
Bool
^^^^
.. js:function:: bool.tofloat()
returns 1.0 for true 0.0 for false
.. js:function:: bool.tointeger()
returns 1 for true 0 for false
.. js:function:: bool.tostring()
returns "true" for true "false" for false
.. js:function:: bool.weakref()
dummy function, returns the bool itself.
^^^^^^
String
^^^^^^
.. js:function:: string.len()
returns the string length
.. js:function:: string.tointeger([base])
converts the string to integer and returns it.An optional parameter base can be specified, if a base is not specified it defaults to base 10
.. js:function:: string.tofloat()
converts the string to float and returns it
.. js:function:: string.tostring()
returns the string(dummy function)
.. js:function:: string.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 + end. If end is omitted end is equal to the string length.
.. js:function:: string.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.
.. js:function:: string.tolower()
returns a lowercase copy of the string.
.. js:function:: string.toupper()
returns a uppercase copy of the string.
.. js:function:: string.weakref()
returns a weak reference to the object.
^^^^^
Table
^^^^^
.. js:function:: table.len()
returns the number of slots contained in a table
.. js:function:: table.rawget(key)
tries to get a value from the slot 'key' without employing delegation
.. js:function:: table.rawset(key,val)
sets the slot 'key' with the value 'val' without employing delegation. If the slot does not exists , it will be created.
.. js:function:: table.rawdelete()
deletes the slot key without emplying delegetion and retunrs his value. if the slo does not exists returns always null.
.. js:function:: table.rawin(key)
returns true if the slot 'key' exists. the function has the same eddect as the operator 'in' but does not employ delegation.
.. js:function:: table.weakref()
returns a weak reference to the object.
.. js:function:: table.tostring()
tries to invoke the _tostring metamethod, if failed. returns "(table : pointer)".
.. js:function:: table.clear()
removes all the slot from the table
.. js:function:: table.setdelegate(table)
sets the delegate of the table, to remove a delegate 'null' must be passed to the function. The function returns the table itself (eg. a.setdelegate(b) in this case 'a' is the return value).
.. js:function:: table.getdelegate()
returns the table's delegate or null if no delegate was set.
^^^^^^
Array
^^^^^^
.. js:function:: array.len()
returns the length of the array
.. js:function:: array.append(val)
appends the value 'val' at the end of the array
.. js:function:: array.push(val)
appends the value 'val' at the end of the array
.. js:function:: array.extend(array)
Extends the array by appending all the items in the given array.
.. js:function:: array.pop()
removes a value from the back of the array and returns it.
.. js:function:: array.top()
returns the value of the array with the higher index
.. js:function:: array.insert(idx,val)
inserst the value 'val' at the position 'idx' in the array
.. js:function:: array.remove(idx)
removes the value at the position 'idx' in the array
.. js:function:: 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.
.. js:function:: array.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;
}
a more compact version of a custom compare can be written using a lambda expression and the operator <=> ::
arr.sort(@(a,b) a <=> b);
.. js:function:: array.reverse()
reverse the elements of the array in place
.. js:function:: array.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 + end. If end is omitted end is equal to the array length.
.. js:function:: array.weakref()
returns a weak reference to the object.
.. js:function:: array.tostring()
returns the string "(array : pointer)".
.. js:function:: array.clear()
removes all the items from the array
.. js:function:: array.map(func(a))
creates a new array of the same size. for each element in the original array invokes the function 'func' and assigns the return value of the function to the corresponding element of the newly created array.
.. js:function:: array.apply(func(a))
for each element in the array invokes the function 'func' and replace the original value of the element with the return value of the function.
.. js:function:: array.reduce(func(prevval,curval))
Reduces an array to a single value. For each element in the array invokes the function 'func' passing the initial value (or value from the previous callback call) and the value of the current element. the return value of the function is then used as 'prevval' for the next element. Given an array of length 0, returns null. Given an array of length 1, returns the first element. Given an array with 2 or more elements calls the function with the first two elements as the parameters, gets that result, then calls the function with that result and the third element, gets that result, calls the function with that result and the fourth parameter and so on until all element have been processed. Finally returns the return value of the last invocation of func.
.. js:function:: array.filter(func(index,val))
Creates a new array with all elements that pass the test implemented by the provided function. In detail, it creates a new array, for each element in the original array invokes the specified function passing the index of the element and it's value; if the function returns 'true', then the value of the corresponding element is added on the newly created array.
.. js:function:: array.find(value)
Performs a linear search for the value in the array. Returns the index of the value if it was found null otherwise.
^^^^^^^^
Function
^^^^^^^^
.. js:function:: array.call(_this,args...)
calls the function with the specified environment object('this') and parameters
.. js:function:: array.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')
.. js:function:: array.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.Where array_args has to contain the required 'this' object at the [0] position.
.. js:function:: array.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.Where array_args has to contain the required 'this' object at the [0] position. This function will not invoke the error callback in case of failure(pacall stays for 'protected array call')
.. js:function:: array.weakref()
returns a weak reference to the object.
.. js:function:: array.tostring()
returns the string "(closure : pointer)".
.. js:function:: array.setroot(table)
sets the root table of a closure
.. js:function:: array.getroot()
returns the root table of the closure
.. js:function:: array.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.
.. js:function:: array.getinfos()
returns a table containing informations about the function, like parameters, name and source name; ::
//the data is returned as a table is in form
//pure squirrel function
{
native = false
name = "zefuncname"
src = "/somthing/something.nut"
parameters = ["a","b","c"]
defparams = [1,"def"]
varargs = 2
}
//native C function
{
native = true
name = "zefuncname"
paramscheck = 2
typecheck = [83886082,83886384] //this is the typemask (see C defines OT_INTEGER,OT_FLOAT etc...)
}
^^^^^
Class
^^^^^
.. js:function:: 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) ).
.. js:function:: class.getattributes(membername)
returns the attributes of the specified member. if the parameter member is null the function returns the class level attributes.
.. js:function:: class.setattributes(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.
.. js:function:: class.rawin(key)
returns true if the slot 'key' exists. the function has the same eddect as the operator 'in' but does not employ delegation.
.. js:function:: class.weakref()
returns a weak reference to the object.
.. js:function:: class.tostring()
returns the string "(class : pointer)".
.. js:function:: class.rawget(key)
tries to get a value from the slot 'key' without employing delegation
.. js:function:: class.rawset(key,val)
sets the slot 'key' with the value 'val' without employing delegation. If the slot does not exists , it will be created.
.. js:function:: class.newmember(key,val,[attrs],[bstatic])
sets/adds the slot 'key' with the value 'val' and attributes 'attrs' and if present invokes the _newmember metamethod. If bstatic is true the slot will be added as static. If the slot does not exists , it will be created.
.. js:function:: class.rawnewmember(key,val,[attrs],[bstatic])
sets/adds the slot 'key' with the value 'val' and attributes 'attrs'.If bstatic is true the slot will be added as static. If the slot does not exists , it will be created. It doesn't invoke any metamethod.
^^^^^^^^^^^^^^
Class Instance
^^^^^^^^^^^^^^
.. js:function:: instance.getclass()
returns the class that created the instance.
.. js:function:: instance.rawin(key)
:param key: ze key
returns true if the slot 'key' exists. the function has the same eddect as the operator 'in' but does not employ delegation.
.. js:function:: instance.weakref()
returns a weak reference to the object.
.. js:function:: instance.tostring()
tries to invoke the _tostring metamethod, if failed. returns "(insatnce : pointer)".
.. js:function:: instance.rawget(key)
tries to get a value from the slot 'key' without employing delegation
.. js:function:: instance.rawset(key,val)
sets the slot 'key' with the value 'val' without employing delegation. If the slot does not exists , it will be created.
^^^^^^^^^^^^^^
Generator
^^^^^^^^^^^^^^
.. js:function:: generator.getstatus()
returns the status of the generator as string : "running", "dead" or "suspended".
.. js:function:: generator.weakref()
returns a weak reference to the object.
.. js:function:: generator.tostring()
returns the string "(generator : pointer)".
^^^^^^^^^^^^^^
Thread
^^^^^^^^^^^^^^
.. js:function:: thread.call(...)
starts the thread with the specified parameters
.. js:function:: thread.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())
.. js:function:: thread.wakeupthrow(objtothrow,[propagateerror = true])
wakes up a suspended thread, throwing an exception in the awaken thread, throwing the object 'objtothrow'.
.. js:function:: thread.getstatus()
returns the status of the thread ("idle","running","suspended")
.. js:function:: thread.weakref()
returns a weak reference to the object.
.. js:function:: thread.tostring()
returns the string "(thread : pointer)".
.. js:function:: thread.getstackinfos(stacklevel)
returns the stack frame informations at the given stack level (0 is the current function 1 is the caller and so on).
^^^^^^^^^^^^^^
Weak Reference
^^^^^^^^^^^^^^
.. js:function:: weakreference.ref()
returns the object that the weak reference is pointing at, null if the object that was point at was destroyed.
.. js:function:: weakreference.weakref()
returns a weak reference to the object.
.. js:function:: weakreference.tostring()
returns the string "(weakref : pointer)".
|