-
Notifications
You must be signed in to change notification settings - Fork 308
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
feature/loop without var decl #474
Open
TobiasWienand
wants to merge
9
commits into
googleprojectzero:main
Choose a base branch
from
TobiasWienand:feature/loop-without-var-decl
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fd632f2
Implements compilation of for-in and for-of loop with pre-declared it…
TobiasWienand b85e3c2
Merge remote-tracking branch 'origin/main' into feature/loop-without-…
TobiasWienand 5545802
Refactor for-in and for-of loop iteration in JavaScriptLifter.swift
TobiasWienand 434d594
Refactor of for-in and for-of loops. Makes each type of loop a sepera…
TobiasWienand 89d2d77
Implements global property iterator for loop
TobiasWienand c75890c
Adds a TODO
TobiasWienand 64749d0
Merge remote-tracking branch 'origin/main' into feature/loop-without-…
TobiasWienand 39f77f1
Implements compilation of forof/forin loop with global property as it…
TobiasWienand 2eb58c0
Merge remote-tracking branch 'origin/main' into feature/loop-without-…
TobiasWienand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -412,36 +412,69 @@ public class JavaScriptCompiler { | |
} | ||
|
||
case .forInLoop(let forInLoop): | ||
let initializer = forInLoop.left; | ||
guard !initializer.hasValue else { | ||
throw CompilerError.invalidNodeError("Expected no initial value for the variable declared in a for-in loop") | ||
} | ||
|
||
let initializer = forInLoop.left! | ||
let obj = try compileExpression(forInLoop.right) | ||
// Processing a for-in or for-of loop requires an iterator, which is typically declared in the function header. | ||
// Alternatively, an existing variable can be used, resulting in an identifier instead of a variable declarator. | ||
// If the identifier is not previously declared, it is implicitly created as a global variable. | ||
|
||
switch initializer { | ||
case .variableDeclarator(let variableDeclarator): | ||
guard !variableDeclarator.hasValue else { | ||
throw CompilerError.invalidNodeError("Expected no initial value for the variable declared in a for-in loop") | ||
} | ||
let loopVar = emit(BeginPlainForInLoop(), withInputs: [obj]).innerOutput | ||
try enterNewScope { | ||
map(variableDeclarator.name, to: loopVar) | ||
try compileBody(forInLoop.body) | ||
} | ||
emit(EndForInLoop()) | ||
|
||
let loopVar = emit(BeginForInLoop(), withInputs: [obj]).innerOutput | ||
try enterNewScope { | ||
map(initializer.name, to: loopVar) | ||
try compileBody(forInLoop.body) | ||
case .identifier(let identifier): | ||
let loopVar: Variable | ||
if let existingVar = lookupIdentifier(identifier.name) { | ||
loopVar = existingVar | ||
} else { | ||
loopVar = emit(LoadNamedVariable(identifier.name)).output | ||
map(identifier.name, to: loopVar) | ||
} | ||
emit(BeginForInLoopWithReassignment(), withInputs: [obj, loopVar]) | ||
try enterNewScope { | ||
try compileBody(forInLoop.body) | ||
} | ||
emit(EndForInLoop()) | ||
} | ||
|
||
emit(EndForInLoop()) | ||
|
||
case .forOfLoop(let forOfLoop): | ||
let initializer = forOfLoop.left; | ||
guard !initializer.hasValue else { | ||
throw CompilerError.invalidNodeError("Expected no initial value for the variable declared in a for-of loop") | ||
} | ||
|
||
let initializer = forOfLoop.left! | ||
let obj = try compileExpression(forOfLoop.right) | ||
|
||
let loopVar = emit(BeginForOfLoop(), withInputs: [obj]).innerOutput | ||
try enterNewScope { | ||
map(initializer.name, to: loopVar) | ||
try compileBody(forOfLoop.body) | ||
} | ||
switch initializer { | ||
case .variableDeclarator(let variableDeclarator): | ||
guard !variableDeclarator.hasValue else { | ||
throw CompilerError.invalidNodeError("Expected no initial value for the variable declared in a for-of loop") | ||
} | ||
let loopVar = emit(BeginPlainForOfLoop(), withInputs: [obj]).innerOutput | ||
try enterNewScope { | ||
map(variableDeclarator.name, to: loopVar) | ||
try compileBody(forOfLoop.body) | ||
} | ||
emit(EndForOfLoop()) | ||
|
||
emit(EndForOfLoop()) | ||
case .identifier(let identifier): | ||
let loopVar: Variable | ||
if let existingVar = lookupIdentifier(identifier.name) { | ||
loopVar = existingVar | ||
} else { | ||
loopVar = emit(LoadNamedVariable(identifier.name)).output | ||
map(identifier.name, to: loopVar) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here |
||
} | ||
emit(BeginForOfLoopWithReassignment(), withInputs: [obj, loopVar]) | ||
try enterNewScope { | ||
try compileBody(forOfLoop.body) | ||
} | ||
emit(EndForOfLoop()) | ||
} | ||
|
||
case .breakStatement: | ||
// If we're in both .loop and .switch context, then the loop must be the most recent context | ||
|
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
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need to do this here but not in the case above (line 436)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the case starting in line 438 is used for global variables and the case in line 436 for non-global variables. In both cases, the compiler just has an identfier as input. For global variables, lookupIdentifer will return nil because the identifier is not associted with a FuzzIL variable. Either because the global Variable has not been set but also when it has actually been set before, i.e. with StoreNamedVariable (see example "a" and "b")
Illustration