pigpigyyy / Yuescript

A Moonscript dialect compiles to Lua.
http://yuescript.org
MIT License
443 stars 38 forks source link

yue.loadfile should not throw errors #129

Closed johnd0e closed 1 year ago

johnd0e commented 1 year ago

At the moment I'm trying to use yue as drop-in replacement for moonscript. And I found that yue.loadfile behaves differently.

> local yue = require("yue")
> yue.loadfile("not_existent_path")
(yuescript):115: bad argument #1 to 'to_lua' (string expected, got nil)
stack traceback:
        [C]: in function 'to_lua'
        (yuescript):115: in function 'loadfile'
        stdin:1: in main chunk
        [C]: at 0x004021a0

I see here two inconsistecies:

  1. loadfile in moonscript (as well as standard lua loadfile) does not throw errors: it return errors as message instead.
  2. The message itself is not very helpful.
pigpigyyy commented 1 year ago

Just updated a version and forgot fixing this problem. You might use

local yue = require("yue")
local function yue_loadfile(fname, ...)
  local res, err = yue.read_file(fname)
  if not res then
    return nil, err
  end
  return yue.loadstring(res, fname, ...)
end

Before making another release.