From cdd413a2a82bc01adc0cd75f8ff4ca47bab701df Mon Sep 17 00:00:00 2001 From: leaf corcoran Date: Tue, 19 Mar 2013 09:22:27 -0700 Subject: [PATCH] load* functions return nil and error instead of throwing error fixes #87 --- moonscript/init.lua | 18 ++++++++---------- moonscript/init.moon | 16 ++++++++-------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/moonscript/init.lua b/moonscript/init.lua index 7612c56..2488d6d 100644 --- a/moonscript/init.lua +++ b/moonscript/init.lua @@ -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")) diff --git a/moonscript/init.moon b/moonscript/init.moon index aa7d2ed..2c9fe41 100644 --- a/moonscript/init.moon +++ b/moonscript/init.moon @@ -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, ...