mirror of
https://github.com/leafo/moonscript.git
synced 2024-11-22 02:44:23 +00:00
load* functions return nil and error instead of throwing error fixes #87
This commit is contained in:
parent
539ae2850f
commit
cdd413a2a8
@ -29,15 +29,15 @@ to_lua = function(text, options)
|
||||
end
|
||||
if "string" ~= type(text) then
|
||||
local t = type(text)
|
||||
error("expecting string (got " .. t .. ")", 2)
|
||||
return nil, "expecting string (got " .. t .. ")", 2
|
||||
end
|
||||
local tree, err = parse.string(text)
|
||||
if not tree then
|
||||
error(err, 2)
|
||||
return nil, err
|
||||
end
|
||||
local code, ltable, pos = compile.tree(tree, options)
|
||||
if not code then
|
||||
error(compile.format_error(ltable, pos, text), 2)
|
||||
return nil, compile.format_error(ltable, pos, text), 2
|
||||
end
|
||||
return code, ltable
|
||||
end
|
||||
@ -73,14 +73,12 @@ end
|
||||
loadstring = function(...)
|
||||
local options, str, chunk_name, mode, env = get_options(...)
|
||||
chunk_name = chunk_name or "=(moonscript.loadstring)"
|
||||
local passed, code, ltable = pcall(function()
|
||||
return to_lua(str, options)
|
||||
end)
|
||||
if not passed then
|
||||
error(chunk_name .. ": " .. code, 2)
|
||||
local code, ltable_or_err = to_lua(str, options)
|
||||
if not (code) then
|
||||
return nil, ltable_or_err
|
||||
end
|
||||
if chunk_name then
|
||||
line_tables[chunk_name] = ltable
|
||||
line_tables[chunk_name] = ltable_or_err
|
||||
end
|
||||
return (lua.loadstring or lua.load)(code, chunk_name, unpack({
|
||||
mode,
|
||||
@ -89,7 +87,7 @@ loadstring = function(...)
|
||||
end
|
||||
loadfile = function(fname, ...)
|
||||
local file, err = io.open(fname)
|
||||
if not file then
|
||||
if not (file) then
|
||||
return nil, err
|
||||
end
|
||||
local text = assert(file:read("*a"))
|
||||
|
@ -23,15 +23,15 @@ create_moonpath = (package_path) ->
|
||||
to_lua = (text, options={}) ->
|
||||
if "string" != type text
|
||||
t = type text
|
||||
error "expecting string (got ".. t ..")", 2
|
||||
return nil, "expecting string (got ".. t ..")", 2
|
||||
|
||||
tree, err = parse.string text
|
||||
if not tree
|
||||
error err, 2
|
||||
return nil, err
|
||||
|
||||
code, ltable, pos = compile.tree tree, options
|
||||
if not code
|
||||
error compile.format_error(ltable, pos, text), 2
|
||||
return nil, compile.format_error(ltable, pos, text), 2
|
||||
|
||||
code, ltable
|
||||
|
||||
@ -63,17 +63,17 @@ loadstring = (...) ->
|
||||
options, str, chunk_name, mode, env = get_options ...
|
||||
chunk_name or= "=(moonscript.loadstring)"
|
||||
|
||||
passed, code, ltable = pcall -> to_lua str, options
|
||||
if not passed
|
||||
error chunk_name .. ": " .. code, 2
|
||||
code, ltable_or_err = to_lua str, options
|
||||
unless code
|
||||
return nil, ltable_or_err
|
||||
|
||||
line_tables[chunk_name] = ltable if chunk_name
|
||||
line_tables[chunk_name] = ltable_or_err if chunk_name
|
||||
-- the unpack prevents us from passing nil
|
||||
(lua.loadstring or lua.load) code, chunk_name, unpack { mode, env }
|
||||
|
||||
loadfile = (fname, ...) ->
|
||||
file, err = io.open fname
|
||||
return nil, err if not file
|
||||
return nil, err unless file
|
||||
text = assert file\read "*a"
|
||||
file\close!
|
||||
loadstring text, fname, ...
|
||||
|
Loading…
Reference in New Issue
Block a user