the_stack.rst 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. .. _embedding_the_stack:
  2. ==========
  3. The Stack
  4. ==========
  5. Squirrel exchanges values with the virtual machine through a stack. This mechanism has
  6. been inherited from the language Lua.
  7. For instance to call a Squirrel function from C it is necessary to push the function and the
  8. arguments in the stack and then invoke the function; also when Squirrel calls a C
  9. function the parameters will be in the stack as well.
  10. -------------
  11. Stack indexes
  12. -------------
  13. Many API functions can arbitrarily refer to any element in the stack through an index.
  14. The stack indexes follow those conventions:
  15. * 1 is the stack base
  16. * Negative indexes are considered an offset from top of the stack. For instance -1 isthe top of the stack.
  17. * 0 is an invalid index
  18. Here an example (let's pretend that this table is the VM stack)
  19. +------------+--------------------+--------------------+
  20. | **STACK** | **positive index** | **negative index** |
  21. +============+====================+====================+
  22. | "test" | 4 | -1(top) |
  23. +------------+--------------------+--------------------+
  24. | 1 | 3 | -2 |
  25. +------------+--------------------+--------------------+
  26. | 0.5 | 2 | -3 |
  27. +------------+--------------------+--------------------+
  28. | "foo" | 1(base) | -4 |
  29. +------------+--------------------+--------------------+
  30. In this case, the function *sq_gettop* would return 4;
  31. ------------------
  32. Stack manipulation
  33. ------------------
  34. The API offers several functions to push and retrieve data from the Squirrel stack.
  35. To push a value that is already present in the stack in the top position::
  36. void sq_push(HSQUIRRELVM v,SQInteger idx);
  37. To pop an arbitrary number of elements::
  38. void sq_pop(HSQUIRRELVM v,SQInteger nelemstopop);
  39. To remove an element from the stack::
  40. void sq_remove(HSQUIRRELVM v,SQInteger idx);
  41. To retrieve the top index (and size) of the current
  42. virtual stack you must call *sq_gettop* ::
  43. SQInteger sq_gettop(HSQUIRRELVM v);
  44. To force the stack to a certain size you can call *sq_settop* ::
  45. void sq_settop(HSQUIRRELVM v,SQInteger newtop);
  46. If the newtop is bigger than the previous one, the new positions in the stack will be
  47. filled with null values.
  48. The following function pushes a C value into the stack::
  49. void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len);
  50. void sq_pushfloat(HSQUIRRELVM v,SQFloat f);
  51. void sq_pushinteger(HSQUIRRELVM v,SQInteger n);
  52. void sq_pushuserpointer(HSQUIRRELVM v,SQUserPointer p);
  53. void sq_pushbool(HSQUIRRELVM v,SQBool b);
  54. this function pushes a null into the stack::
  55. void sq_pushnull(HSQUIRRELVM v);
  56. returns the type of the value in a arbitrary position in the stack::
  57. SQObjectType sq_gettype(HSQUIRRELVM v,SQInteger idx);
  58. the result can be one of the following values: ::
  59. OT_NULL,OT_INTEGER,OT_FLOAT,OT_STRING,OT_TABLE,OT_ARRAY,OT_USERDATA,
  60. OT_CLOSURE,OT_NATIVECLOSURE,OT_GENERATOR,OT_USERPOINTER,OT_BOOL,OT_INSTANCE,OT_CLASS,OT_WEAKREF
  61. The following functions convert a squirrel value in the stack to a C value::
  62. SQRESULT sq_getstring(HSQUIRRELVM v,SQInteger idx,const SQChar **c);
  63. SQRESULT sq_getstringandsize(HSQUIRRELVM v,SQInteger idx,const SQChar **c,SQInteger size);
  64. SQRESULT sq_getinteger(HSQUIRRELVM v,SQInteger idx,SQInteger *i);
  65. SQRESULT sq_getfloat(HSQUIRRELVM v,SQInteger idx,SQFloat *f);
  66. SQRESULT sq_getuserpointer(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p);
  67. SQRESULT sq_getuserdata(HSQUIRRELVM v,SQInteger idx,SQUserPointer *p,SQUserPointer *typetag);
  68. SQRESULT sq_getbool(HSQUIRRELVM v,SQInteger idx,SQBool *p);
  69. The function sq_cmp compares 2 values from the stack and returns their relation (like strcmp() in ANSI C).::
  70. SQInteger sq_cmp(HSQUIRRELVM v);