duster/js/test_cases/walk_up/README.md

1.2 KiB

Through experimentation it seems that you can walk up to access higher levels in the context. Interestingly enough, it seems that walking up to a higher context does not unwind the context stack but instead seems to add the higher level context element to the bottom. For example:

{
  "foo": {
    "f1": "f",
    "f2": "ff"
  },
  "bar": {
    "b1": "b",
    "b2": "bb"
  }
}

if we walk down into bar and then into foo then our variable look ups appear to follow this pattern:

(attempts to read from the context in-order starting with the first line)

Starting access context:
{"foo":{"f1":"f","f2":"ff"},"bar":{"b1":"b","b2":"bb"}}

After walk "bar":
{"b1":"b","b2":"bb"}
{"foo":{"f1":"f","f2":"ff"},"bar":{"b1":"b","b2":"bb"}}

After walk "foo":
{"f1":"f","f2":"ff"}
{"b1":"b","b2":"bb"}
{"foo":{"f1":"f","f2":"ff"},"bar":{"b1":"b","b2":"bb"}}

This appears to be using dynamic scoping instead of lexical scoping. For example, in lexical scoping a read of "b1" would fail after that final walk because you're inside the "foo" context which does not have any "b1" in or above it, however, since this is using dynamic scoping its using the invocations to build a scope tree rather than their original position.