Repository navigation
Conversation
Proper error handling will be implemented later.
There was a problem hiding this comment.
Devin Review found 4 potential issues.
2 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)
| #ifdef CODEGEN_BACKEND_CPP | ||
| std::error_code error; | ||
| std::filesystem::remove_all(std::filesystem::path(destination) / "src", error); | ||
|
|
There was a problem hiding this comment.
🔴 Project export deletes existing source files
When the export destination already contains src, Engine::save deletes that entire directory before generating any code. Unrelated files in it are lost.
Learn more
The public Project::save takes a destination directory. CppCodeSaver::save organizes generated files under a src subtree, but the export API does not require that the destination or its src directory be owned by the exporter. The cleanup removes all existing contents of that subtree, not just files produced by previous exports.
Example: An application exports to its workspace to keep generated scripts alongside other code. An existing src/main.cpp is deleted before any generated script is written.
Recommended fix: Put generated files in a dedicated exporter-owned directory. Reject a pre-existing directory without an ownership marker, or track and replace only previously generated files. Add a regression test that saves into a destination containing an unrelated source file and verifies that the file survives.
Was this helpful? React with 👍 or 👎 to provide feedback.
| m_saveDestination = destination; | ||
| compile(); | ||
| m_saveDestination.clear(); |
There was a problem hiding this comment.
🔴 Saving leaves dangling event scripts
After project load, Engine::save recompiles scripts and frees the originals. addHatToMap leaves the old script pointers registered. The next green flag dereferences a freed script in startHats.
Learn more
ProjectPrivate::tryLoad already compiles the project. Engine::compile owns each compiled Script through m_scripts, while addGreenFlagScript adds its raw pointer to the green-flag event index. Saving compiles again and replaces the owner, but addHatToMap only appends the replacement pointer; it does not remove the original. Engine::start later starts every registered green-flag script, dereferencing those pointers in startHats. The hazard also exists on LLVM because the save method still recompiles before the backend rejects export.
Example: Load a stopped project with one green-flag script, save to a fresh location, and then press the green flag. The event index now contains the freed original as well as its replacement.
Recommended fix: Export without recompiling into the live engine's runtime registries. Retain export artifacts during compilation or compile for export in isolated state. Reject unsupported backends before changing engine state. Add a load–save–start regression test with a green-flag script.
Was this helpful? React with 👍 or 👎 to provide feedback.
| m_codeGen.setCodeType(m_codeType, returnValue); | ||
| CppCodeSaver::save(m_codeGen, m_ctx, destination); |
There was a problem hiding this comment.
🔴 Exported scripts ignore their conditions
When a Scratch if contains a say block, CppCodeBuilder::save exports the say call without its condition. buildBeginIf emits no branch, so the exported script speaks even when the condition is false.
Learn more
compileIf sends its condition and body to Compiler::moveToIf, which places a branch instruction before the body. The C++ branch and loop handlers advance without emitting control flow. In contrast, compileSay emits a normal function call, which buildFunctionCall translates. The public save path therefore writes source that runs the body without its condition rather than rejecting an incomplete export.
Example: Export a green-flag script containing if <mouse down?> then say [Pressed]. The exported code calls looks_say even when the mouse button is up.
Recommended fix: Implement control-flow emission or reject export when a script contains an instruction the backend cannot emit. Propagate the error through project save and add a regression test that either rejects the example or confirms the exported script stays silent when the condition is false.
Was this helpful? React with 👍 or 👎 to provide feedback.
| if (retType == CppIdentifier::Type::ValueData) | ||
| includeHeader(Header::ValueData); |
There was a problem hiding this comment.
🟡 Generated sensing calls leak strings
When sensing_variable_of_target reads a text variable, it returns an owned string. createCall stores it in a ValueData local and never releases it. Each evaluation leaks a string even when the caller frees the reporter result.
Learn more
When the source of an of block is resolved at runtime, compileOf uses sensing_variable_of_target to read its variables. The helper uses value_assign_copy, which checks out a new string for a string-valued result. Generated native calls assign that result to a local ValueData, which has no destructor. When finalize copies the value again for the reporter return, freeing the returned copy does not release the original string.
Example: A generated reporter reads the status variable of a sprite selected by a runtime expression. If status contains ready, every call leaks the original ready string even when the caller frees the returned value.
Recommended fix: Give owned ValueData temporaries scoped cleanup, or transfer ownership when returning them and release them after their final use otherwise. Keep borrowed ValueData separate so cleanup does not free strings owned by other objects. Exercise string-valued results in the runtime-variable sensing test.
Was this helpful? React with 👍 or 👎 to provide feedback.
Resolves: #687