1

I work on image processing application in Embarcadero C++ Builder XE10.2 that executes Lua scripts. I use LuaJIT with FFI to share image data. Everything works fine. I've downloaded ZeroBrane studio and tried to see if I can debug scripts executed from "host" C++ application, so I've included

package.path = package.path .. ";C:/Portable_App/ZeroBraneStudio/lualibs/mobdebug/?.lua"
package.cpath = package.cpath .. ";C:/Portable_App/ZeroBraneStudio/bin/clibs/?.dll"
require("mobdebug").start()

before any function in the script is called. However, when the script is loaded and executed (on C++ side):

FResult = lua_pcall(FLs, 0, 0, 0);

host program crashes with "floating point division by zero" exception. It crashes on

require("mobdebug").start()

Without this line script works OK. Any clue?

S. Petrić
  • 80
  • 10

1 Answers1

1

It's not possible to tell what may be going wrong based on the provided information, but you can try to get the stack trace (using this SO answers), which should provide more information about what's leading to the error.

The only division that I'm aware of is in the serialization code that uses tostring(1/0) code to generate platform-independent NaN values. Would this lead to "floating point division by zero" error in your Lua configuration?

(Update to include the solution mentioned in comments) The issue was related to BCC compiler settings on how to handle FPU exceptions. One way is to manipulate FP control: _clear87(); _control87(MCW_EM, MCW_EM); or to set arithmetic exception mask: SetExceptionMask(exAllArithmeticExceptions);.

Paul Kulchenko
  • 25,884
  • 3
  • 38
  • 56
  • Yes. Instead of `requre("mobdebug").start() `, I've added `tostring(1/0) ` and immediately after calling `lual_loadfile ` from C++ program, exception is thrown. – S. Petrić Dec 10 '18 at 22:32
  • 1
    What version of LuaJIT is this? I get `inf` on Windows for both `tostring(1/0)` and `tostring(1.0/0)` running LuaJIT 2.0.4 – Paul Kulchenko Dec 10 '18 at 23:09
  • 1
    I have LuaJIT 2.0.5., but problem was in BCC compiler settings, i..e. how it will handle FPU exceptions. I had to disable FP exceptions before doing anything with Lua scripts. One way is to manipulate FP control word:`_clear87(); _control87(MCW_EM, MCW_EM);` or to set arithmetic exception mask: `SetExceptionMask(exAllArithmeticExceptions);`...and it works! Thanks a lot. – S. Petrić Dec 11 '18 at 09:45
  • BTW, I had to add following paths to lua script: `package.path = package.path .. ";C:/Portable_App/ZeroBraneStudio/lualibs/mobdebug/?.lua" package.path = package.path .. ";C:/Portable_App/ZeroBraneStudio/lualibs/?.lua" package.cpath = package.cpath .. ";C:/Portable_App/ZeroBraneStudio/bin/clibs/?.dll" require("mobdebug").start()` – S. Petrić Dec 11 '18 at 09:47
  • Yes, if you start the script/application outside of the ZeroBrane Studio IDE, you'll have to set it's `package.path/cpath` values to load mobdebug and luasocket libraries. Another option is to set `LUA_PATH` and `LUA_CPATH` as described in the documentation: https://studio.zerobrane.com/doc-remote-debugging – Paul Kulchenko Dec 11 '18 at 16:34