@@ -3,17 +3,15 @@ defmodule FarmbotOS.Lua do
33 Embedded scripting language for "formulas" in the MOVE block,
44 assertion, and general scripting via LUA block.
55 """
6-
7- @ type t ( ) :: tuple ( )
8- @ type table ( ) :: [ { any , any } ]
96 require FarmbotOS.Logger
107 require Logger
118
129 alias FarmbotOS.Lua . {
1310 DataManipulation ,
1411 Firmware ,
1512 Info ,
16- Wait
13+ Wait ,
14+ PinWatcher
1715 }
1816
1917 # this function is used by SysCalls, but isn't a direct requirement.
@@ -48,44 +46,11 @@ defmodule FarmbotOS.Lua do
4846 `extra_vm_args` is a set of extra args to place inside the
4947 Lua sandbox. The extra args are passed to set_table/3
5048 """
51- def perform_lua ( lua_code , extra_vm_args , comment ) do
52- comment = comment || "sequence"
49+ def perform_lua ( lua_code , extra_vm_args , _comment ) do
5350 lua_code = add_implicit_return ( lua_code )
5451 reducer = fn args , vm -> apply ( __MODULE__ , :set_table , [ vm | args ] ) end
5552 vm = Enum . reduce ( extra_vm_args , init ( ) , reducer )
56-
57- case raw_eval ( vm , lua_code ) do
58- { :ok , value } ->
59- { :ok , value }
60-
61- { :error , { :lua_error , error , _lua } } ->
62- { :error , "lua runtime error evaluating expression: #{ inspect ( error ) } " }
63-
64- { :error , { :badmatch , { :error , [ { line , :luerl_parse , parse_error } ] , _ } } } ->
65- FarmbotOS.Logger . error (
66- 1 ,
67- """
68- Failed to parse expression:
69- `#{ comment } .lua:#{ line } `
70-
71- #{ IO . iodata_to_binary ( parse_error ) }
72- """ ,
73- channels: [ :toast ]
74- )
75-
76- { :error ,
77- "failed to parse expression (line:#{ line } ): #{ IO . iodata_to_binary ( parse_error ) } " }
78-
79- { :error , error , backtrace } ->
80- IO . inspect ( backtrace , label: "=== LUA ERROR TRACE" )
81- { :error , error }
82-
83- { :error , error } ->
84- { :error , error }
85-
86- error ->
87- { :error , inspect ( error ) }
88- end
53+ FarmbotOS.Lua.Result . new ( raw_eval ( vm , lua_code ) )
8954 end
9055
9156 def init do
@@ -102,22 +67,22 @@ defmodule FarmbotOS.Lua do
10267 :luerl . eval ( hook , lua )
10368 end
10469
105- def unquote ( :do ) ( lua , hook ) when is_binary ( hook ) do
106- :luerl . do ( hook , lua )
107- catch
108- :error , { :error , reason } ->
109- { { :error , reason } , lua }
70+ # def unquote(:do)(lua, hook) when is_binary(hook) do
71+ # :luerl.do(hook, lua)
72+ # catch
73+ # :error, {:error, reason} ->
74+ # {{:error, reason}, lua}
11075
111- error , reason ->
112- { { :error , { error , reason } } , lua }
113- end
76+ # error, reason ->
77+ # {{:error, {error, reason}}, lua}
78+ # end
11479
11580 # Wrap a function so that it cannot be called when device
11681 # is locked.
11782 def safe ( action , fun ) when is_function ( fun , 2 ) do
11883 fn args , lua ->
11984 if FarmbotOS.Firmware.UARTCoreSupport . locked? ( ) do
120- { [ nil , "Can't #{ action } when locked." ] , :luerl . stop ( lua ) }
85+ { [ nil , "Can't #{ action } when locked." ] , disabled_sandbox ( ) }
12186 else
12287 fun . ( args , lua )
12388 end
@@ -186,7 +151,22 @@ defmodule FarmbotOS.Lua do
186151 update_fbos_config: & DataManipulation . update_fbos_config / 2 ,
187152 update_firmware_config: & DataManipulation . update_firmware_config / 2 ,
188153 wait: & Wait . wait / 2 ,
154+ watch_pin: & PinWatcher . new / 2 ,
189155 write_pin: safe ( "write pin" , & Firmware . write_pin / 2 )
190156 }
191157 end
158+
159+ # WHAT IS GOING ON HERE?:
160+ # * We want to allow some Lua to execute when the bot
161+ # is estopped.
162+ # * If the bot is estopped, we DO NOT want it to perform
163+ # unsafe actions like motor movement.
164+ # * If execution reaches this point, you are estopped
165+ # and you get this far, it means you tried to move
166+ # the bot or perform an unsafe action.
167+ # * For safety, we will revert the Lua VM to an empty
168+ # state.
169+ def disabled_sandbox ( ) do
170+ :luerl . set_table ( [ :estop ] , true , :luerl . init ( ) )
171+ end
192172end
0 commit comments