Skip to content

Commit

Permalink
Fix local variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
D8H committed May 3, 2024
1 parent 4ceda58 commit 6f44dbc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
9 changes: 5 additions & 4 deletions GDJS/GDJS/Extensions/Builtin/CommonInstructionsExtension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -876,8 +876,11 @@ void CommonInstructionsExtension::GenerateLocalVariablesInitializationCode(
for (std::size_t i = 0; i < variablesContainer.Count(); i++) {
auto &variable = variablesContainer.Get(i);
code += "{\n";
code += "const variable = variables.getFromIndex(" + gd::String::From(i) + ");\n";
GenerateLocalVariableInitializationCode(variable, code);
code += "variables._declare(" +
EventsCodeGenerator::ConvertToStringExplicit(
variablesContainer.GetNameAt(i)) +
", variable);\n";
code += "}\n";
}
code +=
Expand All @@ -889,9 +892,7 @@ void CommonInstructionsExtension::GenerateLocalVariableInitializationCode(
gd::Variable &variable, gd::String &code, std::size_t depth) {
const gd::String variableCodeName =
"variable" + (depth == 0 ? "" : gd::String::From(depth));
if (depth > 0) {
code += "const " + variableCodeName + " = new gdjs.Variable();\n";
}
code += "const " + variableCodeName + " = new gdjs.Variable();\n";
if (variable.GetType() == gd::Variable::Number) {
code += variableCodeName + ".setNumber(" +
gd::String::From(variable.GetValue()) + ");\n";
Expand Down
15 changes: 15 additions & 0 deletions GDJS/Runtime/variablescontainer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ namespace gdjs {
}
}

/**
* Declare a new variable.
* This should only be used by generated code.
*
* @param name Variable name
* @param newVariable The variable to be declared
*/
_declare(name: string, newVariable: gdjs.Variable): void {
this._variables.put(name, newVariable);
this._variablesArray.push(newVariable);
}

/**
* Add a new variable.
* This can be costly, don't use in performance sensitive paths.
Expand Down Expand Up @@ -205,6 +217,9 @@ namespace gdjs {
add: function () {
return;
},
declare: function () {
return;
},
initFrom: function () {
return;
},
Expand Down
5 changes: 5 additions & 0 deletions GDevelop.js/TestUtils/GDJSMocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ class VariablesContainer {
}
}

_declare(name, newVariable) {
this._variables.put(name, newVariable);
this._indexedVariables.push(newVariable);
}

/**
* @param {string} name
* @returns {Variable}
Expand Down

0 comments on commit 6f44dbc

Please sign in to comment.