Improve the walk up test to prove that DustJS is doing dynamic scoping, not lexical scoping.

This commit is contained in:
Tom Alexander 2020-05-03 16:13:29 -04:00
parent 9adb88d132
commit 45facfed0d
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
4 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,33 @@
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:
```js
{
"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.

View File

@ -6,6 +6,6 @@
"people": [
{"name": "Alice", "job": "Chief Swinger"},
{"name": "Bob", "job": "Chief Swayer"},
{"name": "Chris", "job": "Barista", "company": "GenericCoffee"}
{"name": "Chris", "job": "Barista", "company": "GenericCoffee", "email": "thecoffeeguy@generic.coffee"}
]
}

View File

@ -1,4 +1,5 @@
Directory for {company}:{~n}
{#people}
{name}: {job} at {company} (email: {globals.email}){~n}
Testing walking after entering a parent context {#globals}job: {job}, email: {email}, company: {company}{/globals}{~n}
{/people}

View File

@ -181,6 +181,7 @@ impl<'a> DustRenderer<'a> {
fn walk_path<'a>(
context: &'a dyn ContextElement,
path: &Vec<&str>,
breadcrumbs: Vec<&'a dyn ContextElement>,
) -> Result<&'a dyn ContextElement, RenderError<'a>> {
let mut output = context;