Skip to content
Draft
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
3 changes: 3 additions & 0 deletions Core/GDCore/Project/EventsFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class GD_CORE_API EventsFunction {

/**
* \brief Return the parameters of the function that are used in the events.
*
* \warning `ActionWithOperator` function are muted by this function. Make sure
* to use the right functions container to avoid strange side effects.
*
* \note During code/extension generation, new parameters are added
* to the generated function, like "runtimeScene" and "eventsFunctionContext".
Expand Down
536 changes: 383 additions & 153 deletions GDJS/GDJS/Events/CodeGeneration/EventsCodeGenerator.cpp

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions GDJS/GDJS/Events/CodeGeneration/EventsCodeGenerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ class EventsCodeGenerator : public gd::EventsCodeGenerator {
gdjs::EventsCodeGenerator& codeGenerator,
gd::String fullyQualifiedFunctionName,
gd::String functionArgumentsCode,
gd::String contextClassCode,
gd::String functionPreEventsCode,
const gd::EventsList& events,
gd::String functionPostEventsCode,
Expand Down Expand Up @@ -409,6 +410,16 @@ class EventsCodeGenerator : public gd::EventsCodeGenerator {
int firstParameterIndex,
bool addsSceneParameter);

/**
* \brief Generate the affectation from parameters to class attributes.
*
* \note runtimeScene is always added as the first parameter, and
* parentEventsFunctionContext as the last parameter.
*/
gd::String GenerateEventsFunctionParametersToAttribues(
const gd::ParameterMetadataContainer &parameters, int firstParameterIndex,
bool addsSceneParameter);

/**
* \brief Generate the "eventsFunctionContext" object that allow a free
* function to provides access objects, object creation and access to
Expand Down Expand Up @@ -475,6 +486,9 @@ class EventsCodeGenerator : public gd::EventsCodeGenerator {
gd::String &objectsGettersMap,
gd::String &objectArraysMap,
gd::String &behaviorNamesMap,
const gd::String &constructorAdditionalCode = "",
const gd::String &reinitializeAdditionalCode = "",
const gd::String& clearAdditionalCode = "",
const gd::String &thisObjectName = "",
const gd::String &thisBehaviorName = "");
};
Expand Down
21 changes: 12 additions & 9 deletions GDJS/Runtime/gd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,21 @@ namespace gdjs {
* @returns {Array}
*/
export const objectsListsToArray = function (
objectsLists: Hashtable<RuntimeObject>
objectsLists: Hashtable<RuntimeObject>,
result: Array<RuntimeObject> = []
): Array<RuntimeObject> {
var lists = gdjs.staticArray(gdjs.objectsListsToArray);
const lists = gdjs.staticArray(gdjs.objectsListsToArray);
objectsLists.values(lists);

var result: Array<RuntimeObject> = [];
for (var i = 0; i < lists.length; ++i) {
var arr = lists[i];
for (var k = 0; k < arr.length; ++k) {
result.push(arr[k]);
let resultIndex = 0;
for (let i = 0; i < lists.length; ++i) {
const arr = lists[i];
for (let k = 0; k < arr.length; ++k) {
result[resultIndex] = arr[k];
resultIndex++;
}
}
result.length = resultIndex;
return result;
};

Expand All @@ -524,8 +527,8 @@ namespace gdjs {
* @param dst The destination array
*/
export const copyArray = function <T>(src: Array<T>, dst: Array<T>): void {
var len = src.length;
for (var i = 0; i < len; ++i) {
const len = src.length;
for (let i = 0; i < len; ++i) {
dst[i] = src[i];
}
dst.length = len;
Expand Down
6 changes: 4 additions & 2 deletions GDevelop.js/TestUtils/CodeGenerationHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,8 @@ function generateCompiledEventsForSerializedEventsBasedExtension(
gd,
serializedEventsFunctionsExtension,
gdjs,
runtimeScene
runtimeScene,
options
) {
const project = new gd.ProjectHelper.createNewGDJSProject();
const extension = project.insertNewEventsFunctionsExtension(
Expand Down Expand Up @@ -294,7 +295,8 @@ function generateCompiledEventsForSerializedEventsBasedExtension(
project,
extension,
behavior,
gdjs
gdjs,
options
);
}

Expand Down
8 changes: 4 additions & 4 deletions GDevelop.js/__tests__/GDJS.js
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ describe('libGD.js - GDJS related tests', function () {

// Check that the context for the events function is here...
expect(code).toMatch('function(runtimeScene, eventsFunctionContext)');
expect(code).toMatch('var eventsFunctionContext =');
expect(code).toMatch('const eventsFunctionContext =');

// Check that the parameters, with the (optional) context of the parent function,
// are all here
Expand All @@ -544,8 +544,8 @@ describe('libGD.js - GDJS related tests', function () {
expect(code).toMatch('"MySprite": MySprite');

// ...and arguments should be able to get queried too:
expect(code).toMatch('if (argName === "MyNumber") return MyNumber;');
expect(code).toMatch('if (argName === "MyString") return MyString;');
expect(code).toMatch('if (argName === "MyNumber") return this.MyNumber;');
expect(code).toMatch('if (argName === "MyString") return this.MyString;');

// GetArgumentAsString("MyString") should be generated code to query and cast as a string
// the argument
Expand Down Expand Up @@ -635,7 +635,7 @@ describe('libGD.js - GDJS related tests', function () {

// Check that the context for the events function is here...
expect(code).toMatch('function(runtimeScene, eventsFunctionContext)');
expect(code).toMatch('var eventsFunctionContext =');
expect(code).toMatch('const eventsFunctionContext =');

// Check that the parameters, with the (optional) context of the parent function,
// are all here
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('libGD.js - GDJS Behavior Code Generation integration tests', function
eventsFunctionsExtension,
eventsBasedBehavior,
gdjs,
{logCode: false}
{ logCode: false }
);
project.delete();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3578,7 +3578,8 @@ describe('libGD.js - GDJS Async Code Generation integration tests', function ()
gd,
require('./extensions/EBAsyncAction.json'),
gdjs,
runtimeScene
runtimeScene,
{ logCode: true }
);

const {
Expand Down Expand Up @@ -3618,7 +3619,8 @@ describe('libGD.js - GDJS Async Code Generation integration tests', function ()
gd,
require('./extensions/EBAsyncAction.json'),
gdjs,
runtimeScene
runtimeScene,
{ logCode: false }
);

const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ describe('libGD.js - GDJS Object Code Generation integration tests', function ()
});

describe('SceneInstancesCount', () => {
const prepareCompiledEvents = () => {
const prepareCompiledEvents = (options = {}) => {
const eventsSerializerElement = gd.Serializer.fromJSObject([
{
type: 'BuiltinCommonInstructions::Standard',
Expand Down Expand Up @@ -62,7 +62,8 @@ describe('libGD.js - GDJS Object Code Generation integration tests', function ()
const runCompiledEvents = generateCompiledEventsForEventsFunction(
gd,
project,
eventsFunction
eventsFunction,
options.logCode
);

eventsFunction.delete();
Expand All @@ -71,7 +72,7 @@ describe('libGD.js - GDJS Object Code Generation integration tests', function ()
};

it('counts instances from the scene in a function, when no instances are passed as parameters', () => {
const { runCompiledEvents } = prepareCompiledEvents();
const { runCompiledEvents } = prepareCompiledEvents({ logCode: false });
const { gdjs, runtimeScene } = makeMinimalGDJSMock();
runtimeScene.getOnceTriggers().startNewFrame();

Expand Down Expand Up @@ -106,7 +107,7 @@ describe('libGD.js - GDJS Object Code Generation integration tests', function ()
});

it('counts instances from the scene in a function, when some instances are passed as parameters', () => {
const { runCompiledEvents } = prepareCompiledEvents();
const { runCompiledEvents } = prepareCompiledEvents({ logCode: false });
const { gdjs, runtimeScene } = makeMinimalGDJSMock();
runtimeScene.getOnceTriggers().startNewFrame();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,7 @@ describe('libGD.js - GDJS Code Generation integration tests', function () {
},
],
{
logCode: true,
logCode: false,
}
);
expect(runtimeScene.getVariables().has('Counter')).toBe(true);
Expand Down Expand Up @@ -787,7 +787,7 @@ describe('libGD.js - GDJS Code Generation integration tests', function () {
},
],
{
logCode: true,
logCode: false,
}
);
expect(runtimeScene.getVariables().has('Counter')).toBe(true);
Expand Down Expand Up @@ -882,7 +882,7 @@ describe('libGD.js - GDJS Code Generation integration tests', function () {
},
],
{
logCode: true,
logCode: false,
}
);
expect(runtimeScene.getVariables().has('Counter')).toBe(true);
Expand Down