-
Notifications
You must be signed in to change notification settings - Fork 190
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
dynamically scoped variable accessors
This adds the expressison syntax `{{-get-dynamic-var "yourVariableName"}}` and the statement syntax `{{#-with-dynamic-var "yourVariableName" someValue}}...{{/with-dynamic-var}}`.
- Loading branch information
Showing
9 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
packages/glimmer-runtime/lib/compiled/expressions/get-dynamic-var.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import VM from '../../vm/append'; | ||
import { CompiledExpression } from '../expressions'; | ||
import { ValueReference } from './value'; | ||
import { UNDEFINED_REFERENCE } from '../../references'; | ||
|
||
export default class CompiledGetDynamicVar extends CompiledExpression<any> { | ||
public type = "get-dynamic-var"; | ||
public varName: string; | ||
|
||
constructor({ varName }: { varName: string }) { | ||
super(); | ||
this.varName = varName; | ||
} | ||
|
||
evaluate(vm: VM): ValueReference<any> { | ||
let scope = vm.dynamicScope(); | ||
if (scope.hasOwnProperty(this.varName)) { | ||
return scope[this.varName]; | ||
} else { | ||
return UNDEFINED_REFERENCE; | ||
} | ||
} | ||
|
||
toJSON(): string { | ||
return `get-dynamic-var(${this.varName})`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
packages/glimmer-runtime/lib/syntax/builtins/with-dynamic-var.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { | ||
Statement as StatementSyntax | ||
} from '../../syntax'; | ||
|
||
import OpcodeBuilderDSL from '../../compiled/opcodes/builder'; | ||
import * as Syntax from '../core'; | ||
import Environment from '../../environment'; | ||
import { default as VM } from '../../vm/append'; | ||
import { DynamicScope } from '../../environment'; | ||
import { EvaluatedArgs } from '../../compiled/expressions/args'; | ||
|
||
export default class WithDynamicVarSyntax extends StatementSyntax { | ||
type = "with-dynamic-var-statement"; | ||
|
||
public args: Syntax.Args; | ||
public templates: Syntax.Templates; | ||
public isStatic = false; | ||
|
||
constructor({ args, templates }: { args: Syntax.Args, templates: Syntax.Templates }) { | ||
super(); | ||
this.args = args; | ||
this.templates = templates; | ||
} | ||
|
||
compile(dsl: OpcodeBuilderDSL, env: Environment) { | ||
let callback = (_vm: VM, _scope: DynamicScope) => { | ||
let vm = _vm as any; | ||
let scope = _scope as any; | ||
|
||
let args: EvaluatedArgs = vm.frame.getArgs(); | ||
|
||
scope[args.positional.values[0].inner] = args.positional.values[1]; | ||
}; | ||
|
||
let { args, templates } = this; | ||
|
||
dsl.unit({ templates }, dsl => { | ||
dsl.putArgs(args); | ||
dsl.setupDynamicScope(callback); | ||
dsl.evaluate('default'); | ||
dsl.popDynamicScope(); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters