diff options
| author | Bruce Hill <bitbucket@bruce-hill.com> | 2017-10-09 04:31:41 -0700 |
|---|---|---|
| committer | Bruce Hill <bitbucket@bruce-hill.com> | 2017-10-09 04:31:41 -0700 |
| commit | 9e1aaf2d5c43e5e3804ba9f6e3b29bf790cb75c8 (patch) | |
| tree | aa76826978444d227bc921da36609ff5e49aa307 | |
| parent | e737333d2708d40c1dd8366d4e65583fc796df2a (diff) | |
Moved timeout code into compiler and out of a lib.
| -rw-r--r-- | lib/timeout.nom | 8 | ||||
| -rw-r--r-- | nomsu.lua | 19 | ||||
| -rwxr-xr-x | nomsu.moon | 12 |
3 files changed, 25 insertions, 14 deletions
diff --git a/lib/timeout.nom b/lib/timeout.nom deleted file mode 100644 index dc5212d..0000000 --- a/lib/timeout.nom +++ /dev/null @@ -1,8 +0,0 @@ -require "lib/core.nom" -rule [cap at %num instructions] =: - lua code ".." - |print("capped at", \(%num)) - |local f = function() - | nomsu:error("Execution quota exceeded. Your code took too long.") - |end - |debug.sethook(f, "", \(%num)) @@ -332,7 +332,17 @@ do end return tree end, - run = function(self, src, filename) + run = function(self, src, filename, max_operations) + if max_operations == nil then + max_operations = nil + end + if max_operations then + local timeout + timeout = function() + return self:error("Execution quota exceeded. Your code took too long.") + end + debug.sethook(timeout, "", max_operations) + end local tree = self:parse(src, filename) assert(tree, "Tree failed to compile: " .. tostring(src)) assert(tree.type == "File", "Attempt to run non-file: " .. tostring(tree.type)) @@ -388,6 +398,9 @@ do %s return ret; end);]]):format(concat(buffer, "\n")) + if max_operations then + debug.sethook() + end return return_value, lua_code end, tree_to_value = function(self, tree, vars) @@ -896,7 +909,7 @@ if arg then end end if not args.input or args.flags["-i"] then - c:run('require "lib/core.nom"') + c:run('require "lib/core.nom"', "stdin") while true do local buff = "" while true do @@ -911,7 +924,7 @@ if arg then break end local ok, ret = pcall(function() - return c:run(buff) + return c:run(buff, "stdin") end) if ok and ret ~= nil then print("= " .. repr(ret)) @@ -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) |
