From 6a01f609a9290fbd80989814a61eb87e0d28d5ae Mon Sep 17 00:00:00 2001 From: Connor Olding Date: Mon, 2 Apr 2018 15:28:00 +0200 Subject: [PATCH] split strictness to its own file --- main.lua | 24 +----------------------- strict.lua | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 23 deletions(-) create mode 100644 strict.lua diff --git a/main.lua b/main.lua index 65f89bb..ac09e90 100644 --- a/main.lua +++ b/main.lua @@ -1,26 +1,4 @@ --- hacks for FCEUX being dumb. -local _error = error -local _assert = assert -error = function(msg, level) - if level == nil then level = 1 end - print() - print(debug.traceback(msg, 1 + level):gsub("\n", "\r\n")) - _error(msg, level) -end -assert = function(cond, msg) - if cond then return cond end - msg = msg or "nondescript" - print() - print(debug.traceback(msg, 2):gsub("\n", "\r\n")) - _error("assertion failed!") -end - --- be strict about globals. -local mt = getmetatable(_G) -if mt == nil then mt = {} setmetatable(_G, mt) end -function mt.__newindex(t, n, v) error("cannot assign undeclared global '" .. tostring(n) .. "'", 2) end -function mt.__index(t, n) error("cannot use undeclared global '" .. tostring(n) .. "'", 2) end -local function globalize(t) for k, v in pairs(t) do rawset(_G, k, v) end end +local globalize = require("strict") -- configuration. diff --git a/strict.lua b/strict.lua new file mode 100644 index 0000000..64a0396 --- /dev/null +++ b/strict.lua @@ -0,0 +1,25 @@ +-- hacks for FCEUX being dumb. +local _error = error +local _assert = assert +error = function(msg, level) + if level == nil then level = 1 end + print() + print(debug.traceback(msg, 1 + level):gsub("\n", "\r\n")) + _error(msg, level) +end +assert = function(cond, msg) + if cond then return cond end + msg = msg or "nondescript" + print() + print(debug.traceback(msg, 2):gsub("\n", "\r\n")) + _error("assertion failed!") +end + +-- be strict about globals. +local mt = getmetatable(_G) +if mt == nil then mt = {} setmetatable(_G, mt) end +function mt.__newindex(t, n, v) error("cannot assign undeclared global '" .. tostring(n) .. "'", 2) end +function mt.__index(t, n) error("cannot use undeclared global '" .. tostring(n) .. "'", 2) end +local function globalize(t) for k, v in pairs(t) do rawset(_G, k, v) end end + +return globalize