aboutsummaryrefslogtreecommitdiffstats
path: root/conf.d/project/lua.d/init-daemon-00-utils.lua
diff options
context:
space:
mode:
authorRomain Forlot <romain.forlot@iot.bzh>2017-09-20 16:54:14 +0200
committerRomain Forlot <romain.forlot@iot.bzh>2017-12-14 11:00:25 +0100
commit78202d5bd8a8d09a55bb71b9df6d3f54b18184b8 (patch)
treeafbc89a7c104a70e7771939765309e977a5ab96a /conf.d/project/lua.d/init-daemon-00-utils.lua
parentb68f95e384aa35b54e453b5e7d09dabf8d82340c (diff)
Make LUA works
- Added LUA library at link - Make app-template crash build if a checked file fails - Fix old defined variable CONTROL_LUA_PATH to a default one in the binding data directory - Add an example with unit conversion made in LUA Change-Id: I1b4712bde32a38044ad6b14ebd38b6782fd01fe9 Signed-off-by: Romain Forlot <romain.forlot@iot.bzh>
Diffstat (limited to 'conf.d/project/lua.d/init-daemon-00-utils.lua')
-rw-r--r--conf.d/project/lua.d/init-daemon-00-utils.lua86
1 files changed, 86 insertions, 0 deletions
diff --git a/conf.d/project/lua.d/init-daemon-00-utils.lua b/conf.d/project/lua.d/init-daemon-00-utils.lua
new file mode 100644
index 0000000..29d2c70
--- /dev/null
+++ b/conf.d/project/lua.d/init-daemon-00-utils.lua
@@ -0,0 +1,86 @@
+--[[
+ Copyright (C) 2016 "IoT.bzh"
+ Author Fulup Ar Foll <fulup@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+ Note: this file should be called before any other to assert declare function
+ is loaded before anything else.
+
+ References:
+ http://lua-users.org/wiki/DetectingUndefinedVariables
+
+--]]
+
+
+--===================================================
+--= Niklas Frykholm
+-- basically if user tries to create global variable
+-- the system will not let them!!
+-- call GLOBAL_lock(_G)
+--
+--===================================================
+function GLOBAL_lock(t)
+ local mt = getmetatable(t) or {}
+ mt.__newindex = lock_new_index
+ setmetatable(t, mt)
+end
+
+--===================================================
+-- call GLOBAL_unlock(_G)
+-- to change things back to normal.
+--===================================================
+function GLOBAL_unlock(t)
+ local mt = getmetatable(t) or {}
+ mt.__newindex = unlock_new_index
+ setmetatable(t, mt)
+end
+
+function lock_new_index(t, k, v)
+ if (string.sub(k,1,1) ~= "_") then
+ GLOBAL_unlock(_G)
+ error("GLOBALS are locked -- " .. k ..
+ " must be declared local or prefix with '_' for globals.", 2)
+ else
+ rawset(t, k, v)
+ end
+end
+
+function unlock_new_index(t, k, v)
+ rawset(t, k, v)
+end
+
+-- return serialised version of printable table
+function Dump_Table(o)
+ if type(o) == 'table' then
+ local s = '{ '
+ for k,v in pairs(o) do
+ if type(k) ~= 'number' then k = '"'..k..'"' end
+ s = s .. '['..k..'] = ' .. Dump_Table(v) .. ','
+ end
+ return s .. '} '
+ else
+ return tostring(o)
+ end
+end
+
+
+-- simulate C prinf function
+printf = function(s,...)
+ io.write(s:format(...))
+ io.write("\n")
+ return
+end
+
+-- lock global variable
+GLOBAL_lock(_G)