Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luaproc.newproc() can now take a lua function as an argument #9

Closed

Conversation

scythe
Copy link

@scythe scythe commented Jun 26, 2015

This pull request modifies the behavior of luaproc.newproc to handle the case where it is passed a pure Lua function as an argument. The advantages of passing a Lua function reference instead of a chunk of Lua code are:

  • Syntax checking happens when the file is loaded, rather than when the process is created. This is especially useful if the process is not created until some time has passed after startup.
  • Syntax highlighting is available in most editors for functions at top-level, but not for functions in longstrings. Most linting and static analysis tools, e.g. luacheck, also do not handle functions in longstrings.
  • If the function is very long and/or many processes are to be created, using a precompiled chunk saves a significant amount of time parsing/compiling the Lua code.
  • Passing a function to luaproc.newproc() mirrors the behavior of coroutine.create() and is more intuitive for new users.

Of note, using string.dump() like this: luaproc.newproc(string.dump(myfunction)) does not work now and did not work before either, because lua_loadstring() assumes that the chunk it is passed is null-terminated, which means that a bytecode chunk passed from string.dump() would often be cut off and generate a binary string: truncated precompiled chunk error. The rewritten luaproc_create_newproc() uses strlen() to get the length of code for lua_loadbuffer(), which also expects null-termination, but I didn't think it was a concern because passing a function directly is simpler and it works, and obviously nobody was using string.dump() anyway or they would have noticed the errors.

tests/hello.lua was also modified to demonstrate both the new and old behavior.

Thanks for continuing to maintain this so many years later 👍

@scythe
Copy link
Author

scythe commented Jun 27, 2015

Update:

  • Add a macro #if LUA_VERSION_NUM >= 503 to use the new 4-argument lua_dump() on Lua 5.3.
  • Aesthetic stuff, move stuff to follow the sectional layout of luaproc.c, some comments, formatting.

@askyrme
Copy link
Owner

askyrme commented Aug 10, 2015

Thanks for the suggestion. Passing functions as arguments to newproc indeed has advantages and we are interested in incorporating this functionality in the next upcoming release. We are reviewing your patch and will get back to you once we have finished evaluating how to merge its functionality into luaproc.

@scythe
Copy link
Author

scythe commented Aug 17, 2015

Cool, thanks. I read about your experiment with transactional memory, shame it didn't work out; I guess there's some updates behind the scenes that we haven't seen here yet?

Leave a comment here if you want me to fix anything.

@askyrme
Copy link
Owner

askyrme commented Oct 3, 2015

Mason, thank you once again for your suggestions. I just pushed a new version of luaproc that allows luaproc.newproc to receive Lua functions as arguments; binary strings also work fine now. I used your patch as a basis for the implementation, but I changed it to (a) handle function upvalues and (b) use the standard Lua string buffer structure -- luaL_Buffer -- to avoid handling memory allocation from luaproc.

@scythe
Copy link
Author

scythe commented Oct 6, 2015

Awesome! Congratulations on the new release.

@scythe scythe closed this Oct 6, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants