Search code examples
love2dmoonscript

How to debug moonscript?


I trying to write some game, based on Love2d framework, compiled from moonscript. Every time when I make a mistake in my code, my application throws error and this error refers to compiled lua-code, but not a moonscript, so I have no idea where exactly this error happens. Tell me please, what a solution in this situation? Thanks.


Solution

  • Moonscript does support source-mapping/error-rewriting, but it is only supported when running in the moon interpreter: https://moonscript.org/reference/command_line.html#error_rewriting

    I think it could be enabled in another lua environment but I am not completely sure what would be involved.

    It would definetely require moonscript to hold on to the source-map tables that are created during compilation, so you couldn't use moonc; instead use the moonscript module to just-in-time compile require'd modules:

    main.lua

    -- attempt to require moonscript,
    -- for development
    pcall(require, 'moonscript')
    
    -- load the main file
    require 'init'
    

    init.moon

    love.draw = ->
      print "test"
    

    with this code and moonscript properly installed you can just run the project using love . as normal. The require 'moonscript' call will change require to compile moonscript modules on-the-fly. The performance penalty is negligible and after all modules have been loaded there is no difference.