[Google Blockly] strip user code #57903
Merged
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.
In older Blockly labs (Maze, Artist, Play Lab), there are some differences between the raw generated code that we feed into the JS interpreter and that which we show the student. For example, in these labs, additional code is used to prevent us from attempting to execute infinite loops. The generated code can also include block ids and lab-specific namespaces. Here is an example program on CDO Blockly and the associated "Show Code" modal:
Here is the same program on Google Blockly:
Note that the more complicated-looking code is still what we actually execute. CDO Blockly was accomplishing this cleanup with a call to a custom
strip
function which removes or modifies some stuff through regular expressions. We can actually use the samestrip
function to process code that Google Blockly generates, so I moved it (and some related constants) into our utils file. Some of the constants were actually already defined in both wrappers which felt a bit redundant.The only functional change relates to differences in how block ids are generated.
Previously, it was sufficient to remove block ids by looking for a series of numbers:
.replace(/(,\s*)?'block_id_\d+'\)/g, ')')
The
'block_id_\d+'
part above matches the literal string'block_id_'
followed by\d+
, which matches one or more digits. To make it match any characters (except quotes), I replaced the latter with[^']+
.With the changes in this branch, the code we show matches the CDO Blockly baseline. (Nit: there's a slight indentation difference inside the loop, but I think that's fine.)
Warning!!
The AP CSP Create Performance Task is in progress. The most critical dates are from April 3 - April 30, 2024. Please consider any risk introduced by this PR that could affect our students taking AP CSP. Code.org students taking AP CSP primarily use App Lab for their Create Task, however a small percent use Game Lab. Carefully consider whether your change has any risk of alterering, changing, or breaking anything in these two labs. Even small changes, such as a different button color, are considered significant during this time period. Reach out to the Student Learning team or Curriculum team for more details.
Links
Testing story
In addition to testing the indented behavior with the change, I also made sure I was still able to execute my code in this Maze level (with both versions of Blockly), as well as a couple random projects of other labs. No regressions found. The changes to the code is only for some very specific matched expressions, so this doesn't feel overly risky.
Making this change also required reverting a migration-related Minecraft change: 3c8902b
In order to get Minecraft migrated, I switched from using
Generator.blocksToCode
togetWorkspaceCode()
. This change worked because there was no difference in the two, and was "needed" becauseblocksToCode
wasn't defined yet. However, the generated code here is passed to the interpreter, so it needs to be the raw, un-stripped code. If we remove the infinite loop logic from the generated code given to the interpreter, then it's easy for students to get into frozen state (see pics). For this reason, it makes sense to go back to using the generator function, now that it has been defined.Deployment strategy
Follow-up work
Privacy
Security
Caching
PR Checklist: