aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2020-08-14 10:52:28 +0000
committers-ol <s+removethis@s-ol.nu>2025-03-02 14:24:49 +0000
commit498890e32b54b47972ea9886473b6b8d9481d602 (patch)
treebff03837984d4d010c5f60318950ac8ac7b2c8bb
parentstart working on language spec (diff)
downloadalive-498890e32b54b47972ea9886473b6b8d9481d602.tar.gz
alive-498890e32b54b47972ea9886473b6b8d9481d602.zip
throw TestPilot errors instead of logging
-rw-r--r--alv/copilot/base.moon15
-rw-r--r--alv/module.moon10
-rw-r--r--spec/test_setup.moon33
3 files changed, 47 insertions, 11 deletions
diff --git a/alv/copilot/base.moon b/alv/copilot/base.moon
index 40600cb..9306e3c 100644
--- a/alv/copilot/base.moon
+++ b/alv/copilot/base.moon
@@ -106,7 +106,9 @@ class Copilot
COPILOT = @
@T += 1
- @poll!
+ ok, err = @poll!
+ if not ok
+ L\error err
root = @last_modules.__root
if root and root.root
@@ -123,17 +125,22 @@ class Copilot
--
-- Call `eval` if there are any, and write changed and newly added modules
-- back to disk.
+ --
+ -- @treturn boolean ok
+ -- @treturn error err
poll: =>
dirty = {}
for name, mod in pairs @last_modules
if mod\poll! > @last_modification
table.insert dirty, mod
- return if #dirty == 0
+ return true if #dirty == 0
@eval dirty
--- try to re-evaluate in response to module changes.
+ -- @treturn boolean ok
+ -- @treturn error err
eval: (dirty) =>
@last_modification = os.time!
L\set_time 'eval'
@@ -146,8 +153,7 @@ class Copilot
for name, mod in pairs @modules
mod\rollback!
@modules = nil
- L\error err
- return
+ return false, err
for name, mod in pairs @last_modules
if not @modules[name]
@@ -158,6 +164,7 @@ class Copilot
@last_modification = os.time!
@last_modules, @modules = @modules, nil
+ true
{
:parse_args
diff --git a/alv/module.moon b/alv/module.moon
index b10b0f9..0144263 100644
--- a/alv/module.moon
+++ b/alv/module.moon
@@ -20,8 +20,9 @@ class Module
-- @function poll
-- @treturn number timestamp of last change
- --- get the module basename.
+ --- get the module name.
-- @function name
+ -- @tparam[default=false] boolean full path
-- @treturn string
--- get the module contents.
@@ -38,11 +39,11 @@ class Module
-- Otherwise, register the module with the `Copilot`. Updates `root`.
eval: =>
@registry\begin_eval!
- @ast = Error.wrap "parsing '#{@file}'", -> program\match @slurp!
+ @ast = Error.wrap "parsing '#{@name true}'", -> program\match @slurp!
assert @ast, Error 'syntax', "failed to parse"
scope = Scope builtins
- @root = Error.wrap "evaluating '#{@file}'", @ast\eval, scope, @registry
+ @root = Error.wrap "evaluating '#{@name true}'", @ast\eval, scope, @registry
--- rollback the last evaluation cycle.
rollback: => @registry\rollback_eval!
@@ -90,7 +91,8 @@ class FSModule extends Module
assert mode == 'file', Error 'io', "not a file: '#{@file}'"
modification
- name: => @file\match '([^/\\]+)$'
+ name: (full) =>
+ if full then @file else @file\match '([^/\\]+)$'
--- Module type for modules loaded from RAM.
-- @type StringModule
diff --git a/spec/test_setup.moon b/spec/test_setup.moon
index c25cb14..965b2e7 100644
--- a/spec/test_setup.moon
+++ b/spec/test_setup.moon
@@ -4,12 +4,22 @@ import Module, StringModule from require 'alv.module'
import Logger from require 'alv.logger'
import Error from require 'alv.error'
import RTNode from require 'alv.rtnode'
+
Logger\init 'error'
+os.time = do
+ t = 0
+ ->
+ t += 1
+ t
+
+export COPILOT
class TestPilot extends Copilot
new: (code) =>
super!
+ COPILOT = @
+
if code
@active_module = StringModule 'main', code
@last_modules.__root = @active_module
@@ -22,6 +32,25 @@ class TestPilot extends Copilot
end_eval: => @active_module.registry\end_eval!
next_tick: => @T += 1
+ --- poll for changes and tick.
+ tick: =>
+ return unless @last_modules.__root
+
+ @T += 1
+
+ ok, err = @poll!
+ if not ok
+ error err
+
+ root = @last_modules.__root
+ if root and root.root
+ L\set_time 'run'
+ ok, error = Error.try "updating", ->
+ root.root\poll_io!
+ root.root\tick!
+ if not ok
+ error
+
require: (name) =>
Error.wrap "loading module '#{name}'", ->
ok, lua = pcall require, "alv-lib.#{name}"
@@ -30,13 +59,11 @@ class TestPilot extends Copilot
else
error Error 'import', "module not found"
-export COPILOT
-
{
:TestPilot
do_setup: ->
- COPILOT = TestPilot!
+ TestPilot!
COPILOT\begin_eval!
do_teardown: ->