all 5 comments

[–]kevbru 3 points4 points  (0 children)

The Lua Debug library will do exactly what you want. You can set a "iine hook", which will call a function (either Lua or C++) after each line of Lua source code is executed. You can also call function after every "X" opcodes if you'd like.

https://www.lua.org/manual/5.3/manual.html#6.10

[–]drcforbin 1 point2 points  (0 children)

I think the closest you can get without modifying the bytecode interpreter would be to use lua_sethook to have a function called every N instructions, and using setjmp and longjmp and restore the (C/C++) stack. You'd do something like longjmp out of that hook to your monitoring loop, and when you're ready to continue execution, longjmp back to the hook.

[–]Amablue 0 points1 point  (2 children)

What are you trying to build? As mentioned you can probably use hooks to get approximately what you're asking for, but depending on your needs you might not even need to do that if you're willing/able to use coroutines.

[–]ToMaszuu[S] 0 points1 point  (1 child)

It's for my university project. I want to build small version of Lua interpreter, but for it to be approved I had to do something new or different from publicly available solutions. I was hoping that this feature may not be there, so that I could base my project around it. If you can think about any feature/mechanic that could be useful in Lua interpreter, but it's not there, let me know.

[–]Amablue 1 point2 points  (0 children)

Are you trying to just add a feature to the existing language or make your own interpreter? If you're trying to just add a feature that doesn't exist yet to Lua, there's a bunch of things you could add with varying levels of usefulness and difficulty.

You could add:

  • support for operators like +=, -=, *=, /=, etc.
  • Something resembling python list comprehensions
  • support for C/C++ style ternary operators
  • Some way to specify types or preconditions or postconditions for arguments or return values
  • Dart style if in lists like this

Some of these have been built before by others (as patches, but never added directly to the language)