From 9e1aaf2d5c43e5e3804ba9f6e3b29bf790cb75c8 Mon Sep 17 00:00:00 2001 From: Bruce Hill Date: Mon, 9 Oct 2017 04:31:41 -0700 Subject: Moved timeout code into compiler and out of a lib. --- nomsu.moon | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) (limited to 'nomsu.moon') diff --git a/nomsu.moon b/nomsu.moon index 7c96f81..4728c91 100755 --- a/nomsu.moon +++ b/nomsu.moon @@ -274,7 +274,11 @@ class NomsuCompiler @print_tree tree, " " return tree - run: (src, filename)=> + run: (src, filename, max_operations=nil)=> + if max_operations + timeout = -> + @error "Execution quota exceeded. Your code took too long." + debug.sethook timeout, "", max_operations tree = @parse(src, filename) assert tree, "Tree failed to compile: #{src}" assert tree.type == "File", "Attempt to run non-file: #{tree.type}" @@ -321,6 +325,8 @@ class NomsuCompiler %s return ret; end);]])\format(concat(buffer, "\n")) + if max_operations + debug.sethook! return return_value, lua_code tree_to_value: (tree, vars)=> @@ -635,7 +641,7 @@ if arg if not args.input or args.flags["-i"] -- REPL - c\run('require "lib/core.nom"') + c\run('require "lib/core.nom"', "stdin") while true buff = "" while true @@ -646,7 +652,7 @@ if arg buff ..= line if #buff == 0 break - ok, ret = pcall(-> c\run(buff)) + ok, ret = pcall(-> c\run(buff, "stdin")) if ok and ret != nil print "= "..repr(ret) -- cgit v1.2.3