Skip to content

Commit 15dab06

Browse files
committed
add options to load functions, fix stacktrace for files run in moon
also don't show nothing if rewriting stacktrace fails
1 parent bb63354 commit 15dab06

File tree

7 files changed

+63
-32
lines changed

7 files changed

+63
-32
lines changed

bin/moon

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local help = [=[Usage: %s [options] [script [args]]
1717
]=]
1818

1919
local function print_err(...)
20-
msg = table.concat({...}, "\t")
20+
local msg = table.concat({...}, "\t")
2121
io.stderr:write(msg .. "\n")
2222
end
2323

@@ -49,7 +49,7 @@ local new_arg = {
4949

5050
local moonscript_chunk, lua_parse_error
5151
local passed, err = pcall(function()
52-
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname)
52+
moonscript_chunk, lua_parse_error = moonscript.loadfile(script_fname, { implicitly_return_root = false })
5353
end)
5454

5555
if not passed then
@@ -70,16 +70,25 @@ getfenv(moonscript_chunk).arg = new_arg
7070

7171
if not opts.d then
7272
local err, trace
73+
7374
xpcall(function() moonscript_chunk(unpack(new_arg)) end, function(_err)
7475
err = _err
7576
trace = debug.traceback("", 2)
7677
end)
7778

7879
if err then
79-
local traceback = util.trim(trace)
80-
traceback = moonscript.errors.truncate_traceback(traceback)
81-
print_err(moonscript.errors.rewrite_traceback(traceback, err))
82-
os.exit(1)
80+
local truncated = moonscript.errors.truncate_traceback(util.trim(trace))
81+
local rewritten = moonscript.errors.rewrite_traceback(truncated, err)
82+
83+
if rewritten then
84+
print_err(rewritten)
85+
else
86+
-- faield to rewrite, show original
87+
print_err(table.concat({
88+
err,
89+
util.trim(trace)
90+
}, "\n"))
91+
end
8392
end
8493
else
8594
moonscript_chunk(unpack(new_arg))

moonscript/compile.lua

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,9 @@ RootBlock = (function()
587587
return "RootBlock<>"
588588
end,
589589
root_stms = function(self, stms)
590-
stms = transform.Statement.transformers.root_stms(self, stms)
590+
if not (self.options.implicitly_return_root == false) then
591+
stms = transform.Statement.transformers.root_stms(self, stms)
592+
end
591593
local _list_0 = stms
592594
for _index_0 = 1, #_list_0 do
593595
local s = _list_0[_index_0]
@@ -607,9 +609,10 @@ RootBlock = (function()
607609
setmetatable(_base_0, _parent_0.__base)
608610
end
609611
local _class_0 = setmetatable({
610-
__init = function(self, ...)
612+
__init = function(self, options)
613+
self.options = options
611614
self.root = self
612-
return _parent_0.__init(self, ...)
615+
return _parent_0.__init(self)
613616
end,
614617
__base = _base_0,
615618
__name = "RootBlock",
@@ -654,11 +657,12 @@ value = function(value)
654657
end
655658
return out
656659
end
657-
tree = function(tree, scope)
658-
if scope == nil then
659-
scope = RootBlock()
660+
tree = function(tree, options)
661+
if options == nil then
662+
options = { }
660663
end
661664
assert(tree, "missing tree")
665+
local scope = (options.scope or RootBlock)(options)
662666
local runner = coroutine.create(function()
663667
return scope:root_stms(tree)
664668
end)

moonscript/compile.moon

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -340,14 +340,15 @@ class Block
340340
@stms fn lines
341341

342342
class RootBlock extends Block
343-
new: (...) =>
343+
new: (@options) =>
344344
@root = self
345-
super ...
345+
super!
346346

347347
__tostring: => "RootBlock<>"
348348

349349
root_stms: (stms) =>
350-
stms = transform.Statement.transformers.root_stms self, stms
350+
unless @options.implicitly_return_root == false
351+
stms = transform.Statement.transformers.root_stms self, stms
351352
@stm s for s in *stms
352353

353354
render: =>
@@ -372,9 +373,11 @@ value = (value) ->
372373
out = \render!
373374
out
374375

375-
tree = (tree, scope=RootBlock!) ->
376+
tree = (tree, options={}) ->
376377
assert tree, "missing tree"
377378

379+
scope = (options.scope or RootBlock) options
380+
378381
runner = coroutine.create ->
379382
scope\root_stms tree
380383

moonscript/errors.lua

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,14 @@ rewrite_traceback = function(text, err)
8585
end
8686
err = rewrite_single(err)
8787
local match = g:match(text)
88+
if not (match) then
89+
return nil
90+
end
8891
for i, trace in ipairs(match) do
8992
match[i] = rewrite_single(trace)
9093
end
9194
return concat({
92-
"moon:" .. err,
95+
"moon: " .. err,
9396
header_text,
9497
"\t" .. concat(match, "\n\t")
9598
}, "\n")

moonscript/errors.moon

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,14 @@ rewrite_traceback = (text, err) ->
7171

7272
err = rewrite_single err
7373
match = g\match text
74+
75+
return nil unless match
76+
7477
for i, trace in ipairs match
7578
match[i] = rewrite_single trace
7679

7780
concat {
78-
"moon:" .. err
81+
"moon: " .. err
7982
header_text
8083
"\t" .. concat match, "\n\t"
8184
}, "\n"

moonscript/init.lua

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@ create_moonpath = function(package_path)
2020
end
2121
return concat(paths, ";")
2222
end
23-
to_lua = function(text)
23+
to_lua = function(text, options)
24+
if options == nil then
25+
options = { }
26+
end
2427
if "string" ~= type(text) then
2528
local t = type(text)
2629
error("expecting string (got " .. t .. ")", 2)
@@ -29,7 +32,7 @@ to_lua = function(text)
2932
if not tree then
3033
error(err, 2)
3134
end
32-
local code, ltable, pos = compile.tree(tree)
35+
local code, ltable, pos = compile.tree(tree, options)
3336
if not code then
3437
error(compile.format_error(ltable, pos, text), 2)
3538
end
@@ -65,9 +68,12 @@ end
6568
if not _G.moon_no_loader then
6669
init_loader()
6770
end
68-
loadstring = function(str, chunk_name)
71+
loadstring = function(str, chunk_name, options)
72+
if options == nil then
73+
options = nil
74+
end
6975
local passed, code, ltable = pcall(function()
70-
return to_lua(str)
76+
return to_lua(str, options)
7177
end)
7278
if not passed then
7379
error(chunk_name .. ": " .. code, 2)
@@ -77,16 +83,19 @@ loadstring = function(str, chunk_name)
7783
end
7884
return lua.loadstring(code, chunk_name or "=(moonscript.loadstring)")
7985
end
80-
loadfile = function(fname)
86+
loadfile = function(fname, options)
87+
if options == nil then
88+
options = nil
89+
end
8190
local file, err = io.open(fname)
8291
if not file then
8392
return nil, err
8493
end
8594
local text = assert(file:read("*a"))
8695
file:close()
87-
return loadstring(text, fname)
96+
return loadstring(text, fname, options)
8897
end
89-
dofile = function(fname)
98+
dofile = function(fname, options)
9099
local f = assert(loadfile(fname))
91100
return f()
92101
end

moonscript/init.moon

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ create_moonpath = (package_path) ->
2424
if p then paths[i] = p..".moon"
2525
concat paths, ";"
2626

27-
to_lua = (text) ->
27+
to_lua = (text, options={}) ->
2828
if "string" != type text
2929
t = type text
3030
error "expecting string (got ".. t ..")", 2
@@ -33,7 +33,7 @@ to_lua = (text) ->
3333
if not tree
3434
error err, 2
3535

36-
code, ltable, pos = compile.tree tree
36+
code, ltable, pos = compile.tree tree, options
3737
if not code
3838
error compile.format_error(ltable, pos, text), 2
3939

@@ -63,23 +63,23 @@ init_loader = ->
6363

6464
init_loader! if not _G.moon_no_loader
6565

66-
loadstring = (str, chunk_name) ->
67-
passed, code, ltable = pcall -> to_lua str
66+
loadstring = (str, chunk_name, options=nil) ->
67+
passed, code, ltable = pcall -> to_lua str, options
6868
if not passed
6969
error chunk_name .. ": " .. code, 2
7070

7171
line_tables[chunk_name] = ltable if chunk_name
7272
lua.loadstring code, chunk_name or "=(moonscript.loadstring)"
7373

74-
loadfile = (fname) ->
74+
loadfile = (fname, options=nil) ->
7575
file, err = io.open fname
7676
return nil, err if not file
7777
text = assert file\read "*a"
7878
file\close!
79-
loadstring text, fname
79+
loadstring text, fname, options
8080

8181
-- throws errros
82-
dofile = (fname) ->
82+
dofile = (fname, options) ->
8383
f = assert loadfile fname
8484
f!
8585

0 commit comments

Comments
 (0)