execution_context.rst 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. .. _executioncontext:
  2. =======================
  3. Execution Context
  4. =======================
  5. .. index::
  6. single: execution context
  7. The execution context is the union of the function stack frame and the function
  8. environment object(this) and the function root(root table).
  9. The stack frame is the portion of stack where the local variables declared in its body are
  10. stored.
  11. The environment object is an implicit parameter that is automatically passed by the
  12. function caller (see see :ref:`functions <functions>`).
  13. The root table is a table associated to the function during its creation.
  14. The root table value of a function is the root table of the VM at the function creation.
  15. The root table of function can also be changed after creation with closure.setroot().
  16. During the execution, the body of a function can only transparently refer to his execution
  17. context.
  18. This mean that a single identifier can refer to a local variable, to an environment object slot
  19. or to the slot of the closure root table;
  20. The environment object can be explicitly accessed by the keyword ``this``.
  21. The closure root table can be explicitly accessed through the operator ``::`` (see :ref:`Variables <variables>`).
  22. .. _variables:
  23. -----------------
  24. Variables
  25. -----------------
  26. There are two types of variables in Squirrel, local variables and tables/arrays slots.
  27. Because global variables(variables stored in the root of a closure) are stored in a table, they are table slots.
  28. A single identifier refers to a local variable or a slot in the environment object.::
  29. derefexp := id;
  30. ::
  31. _table["foo"]
  32. _array[10]
  33. with tables we can also use the '.' syntax::
  34. derefexp := exp '.' id
  35. ::
  36. _table.foo
  37. Squirrel first checks if an identifier is a local variable (function arguments are local
  38. variables) if not looks up the environment object (this) and finally looksup
  39. to the closure root.
  40. For instance:::
  41. function testy(arg)
  42. {
  43. local a=10;
  44. print(a);
  45. return arg;
  46. }
  47. in this case 'foo' will be equivalent to 'this.foo' or this["foo"].
  48. Global variables are stored in a table called the root table. Usually in the global scope the
  49. environment object is the root table, but to explicitly access the closure root of the function from
  50. another scope, the slot name must be prefixed with ``'::'`` (``::foo``).
  51. For instance:::
  52. function testy(arg)
  53. {
  54. local a=10;
  55. return arg+::foo;
  56. }
  57. accesses the variable 'foo' in the closure root table.
  58. Since Squirrel 3.1 each function has a weak reference to a specific root table, this can differ from the current VM root table.::
  59. function test() {
  60. foo = 10;
  61. }
  62. is equivalent to write::
  63. function test() {
  64. if("foo" in this) {
  65. this.foo = 10;
  66. }else {
  67. ::foo = 10;
  68. }
  69. }