Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lexical "this" capture #1673

Merged
merged 1 commit into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,21 @@ module('[glimmer-compiler] precompile', ({ test }) => {
let elementName = openElementExpr[1];
assert.strictEqual(elementName, 'rental', 'element name is correct');
});

test('when "this" in in locals, it compiles to GetLexicalSymbol', (assert) => {
let target = { message: 'hello' };
let _wire: ReturnType<typeof compile>;
(function() {
_wire = compile(`{{this.message}}`, ['this'], (source) => eval(source));
}).call(target)
let wire = _wire!;
assert.deepEqual(wire.scope?.(), [target]);
assert.deepEqual(wire.block[0], [[SexpOpcodes.Append,[SexpOpcodes.GetLexicalSymbol,0,["message"]]]])
});

test('when "this" is not in locals, it compiles to GetSymbol', (assert) => {
let wire = compile(`{{this.message}}`, [], (source) => eval(source));
assert.strictEqual(wire.scope, undefined);
assert.deepEqual(wire.block[0], [[SexpOpcodes.Append,[SexpOpcodes.GetSymbol,0,["message"]]]])
});
});
1 change: 0 additions & 1 deletion packages/@glimmer/syntax/lib/v2/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ export class Builder {
isTemplateLocal: boolean,
loc: SourceSpan
): ASTv2.VariableReference {
assert(name !== 'this', `You called builders.var() with 'this'. Call builders.this instead`);
assert(
name[0] !== '@',
`You called builders.var() with '${name}'. Call builders.at('${name}') instead`
Expand Down
4 changes: 4 additions & 0 deletions packages/@glimmer/syntax/lib/v2/normalize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,10 @@ class ExpressionNormalizer {

switch (head.type) {
case 'ThisHead':
if (block.hasBinding('this')) {
let [symbol, isRoot] = table.get('this');
return block.builder.localVar('this', symbol, isRoot, offsets);
}
return builder.self(offsets);
case 'AtHead': {
let symbol = table.allocateNamed(head.name);
Expand Down
Loading