pd-lua icon indicating copy to clipboard operation
pd-lua copied to clipboard

Fixes Issue #72: Add Missing Third Parameter to lua_gc Function

Open Va16hav07 opened this issue 10 months ago • 4 comments

This PR fixes Issue #72 by correcting the function call to lua_gc, which was missing the required third parameter.

Problem:

The lua_gc function in Purr Data was called with only two parameters, causing a compilation error when building with Lua 5.3. According to the [official Lua 5.3 documentation](https://www.lua.org/manual/5.3/manual.html#lua_gc), lua_gc requires three parameters:

int lua_gc(lua_State *L, int what, int data);

Solution:

  • Updated the lua_gc function call by adding the missing third parameter (data).
  • Used 0 as the default value for data, ensuring safe execution without unintended side effects.
  • Ensured compatibility with Lua 5.3 to prevent further build failures.

Testing:

  • Successfully built Purr Data after the fix.
  • Verified that the garbage collector functions as expected with the additional parameter.

Additional Context:

This issue likely arose due to an older Lua version where lua_gc required only two parameters. Updating to Lua 5.3 necessitated this fix.

Let me know if any further changes are needed! 🚀

Va16hav07 avatar Feb 19 '25 18:02 Va16hav07

@agraef Please review

Va16hav07 avatar Feb 19 '25 18:02 Va16hav07

Ah yes, that line was recently added by @timothyschoen, probably it was part of Tim's giant changeset that implemented the graphics support.

Have you tried whether this fix still works when compiling against Lua 5.4? I can't seem to find any information about what the 3rd parameter is supposed to be in the Lua 5.3 docs.

agraef avatar Feb 19 '25 20:02 agraef

@agraef I have checked it for Lua 5.4 and added a conditional to ensure compatibility with both Lua 5.4 and Lua 5.3.

Va16hav07 avatar Feb 20 '25 04:02 Va16hav07

It caters to the needs of both Lua 5.3 and 5.4 users.

According to the Lua 5.3 documentation for lua_gc (https://www.lua.org/manual/5.3/manual.html#lua_gc), the function signature is: lua_gc (lua_State *L, int what, int data); The meaning of the data parameter varies with the operation specified by the 'what' argument. For LUA_GCCOLLECT, which initiates a full garbage collection cycle, the documentation states that "the data argument is ignored."

Therefore, when we write: lua_gc(__L(), LUA_GCCOLLECT, 0); we use 0 because:

  • The function requires three parameters as per its signature.
  • For the LUA_GCCOLLECT operation, the value of the third parameter is irrelevant since it is ignored.
  • It is a common programming convention to use 0 for parameters that are ignored.

This explains the compilation error in our earlier code; we failed to provide all required parameters for the function signature, even though the value of the third parameter is unused in this specific operation.

Va16hav07 avatar Feb 20 '25 04:02 Va16hav07