0
0

More than 3 years have passed since last update.

Lua on RTX1210 (1) - タイプミスを防ぐ

Posted at

検証環境

RTX1210 BootROM Ver. 1.04
RTX1210 FlashROM Table Ver. 1.00
RTX1210 Rev.14.01.38 (Fri Jul 10 09:41:02 2020)

経緯

YAMAHA RTX1210があったので、Luaで遊んでみる。

strict.lua

Lua初心者なのでYAMAHA公式サイトからstrict.lua をもらってくる。
これによって未初期化グローバル変数への誤った参照や代入を防ぐことができる。
以後、きちんとスコープを意識して local 変数を使わなければいけなくなる。

strict.lua
--
-- strict.lua
-- checks uses of undeclared global variables
-- All global variables must be 'declared' through a regular assignment
-- (even assigning nil will do) in a main chunk before being used
-- anywhere or assigned to inside a function.
--

local getinfo, error, rawset, rawget = debug.getinfo, error, rawset, rawget

local mt = getmetatable(_G)
if mt == nil then
  mt = {}
  setmetatable(_G, mt)
end

mt.__declared = {}

local function what ()
  local d = getinfo(3, "S")
  return d and d.what or "C"
end

mt.__newindex = function (t, n, v)
  if not mt.__declared[n] then
    local w = what()
    if w ~= "main" and w ~= "C" then
      error("assign to undeclared variable '"..n.."'", 2)
    end
    mt.__declared[n] = true
  end
  rawset(t, n, v)
end

mt.__index = function (t, n)
  if not mt.__declared[n] and what() ~= "C" then
    error("variable '"..n.."' is not declared", 2)
  end
  return rawget(t, n)
end

USBなりSFTPなりで転送する。
ごちゃごちゃしないよう、 /lua 以下に保存することにした。
その場合、 PWDLUA_PATH の指定は下記のようになる。

set PWD="/lua"
set LUA_PATH="./\?.lua;"
console character ja.utf8

ついでにコンソールの文字コードも設定した。
luac -p /lua/strict.lua のようにして構文解析するとデバッグが捗る。

次回予告

syslogの限界を感じたので、Slackへのカジュアルな投稿を試してみる。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0