aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruce Hill <bruce@bruce-hill.com>2019-01-11 15:27:11 -0800
committerBruce Hill <bruce@bruce-hill.com>2019-01-11 15:27:41 -0800
commit43e45b145279946f01876efe36e0e7acad4ce8c3 (patch)
tree29ea5f774bc7299a7b4eba65cdadf190f6c12a20
parent0f0fb2256a46a8808794d7d4746d112278da3730 (diff)
Tweaked code to use both _ENV and _G for better compatibility of
precompiled Lua between Lua 5.2+ and LuaJIT
-rw-r--r--bootstrap.lua13
-rw-r--r--bootstrap.moon14
-rw-r--r--nomsu_environment.lua12
-rw-r--r--nomsu_environment.moon12
4 files changed, 20 insertions, 31 deletions
diff --git a/bootstrap.lua b/bootstrap.lua
index 844bcca..10d6f3b 100644
--- a/bootstrap.lua
+++ b/bootstrap.lua
@@ -38,7 +38,6 @@ compile_error = function(source, err_msg, hint)
return error(err_str, 0)
end
local MAX_LINE = 80
-local ENVNAME = jit and "_G" or "_ENV"
local compile_actions = {
[""] = function(self, fn, ...)
local lua = LuaCode()
@@ -118,16 +117,16 @@ local compile_actions = {
}))
end,
["1 as lua"] = function(self, code)
- return LuaCode(tostring(ENVNAME) .. ":compile(", self:compile(code), ")")
+ return LuaCode("_ENV:compile(", self:compile(code), ")")
end,
["use"] = function(self, path)
- return LuaCode(tostring(ENVNAME) .. ":use(" .. tostring(self:compile(path)) .. ")")
+ return LuaCode("_ENV:use(" .. tostring(self:compile(path)) .. ")")
end,
["export"] = function(self, path)
- return LuaCode(tostring(ENVNAME) .. ":export(" .. tostring(self:compile(path)) .. ")")
+ return LuaCode("_ENV:export(" .. tostring(self:compile(path)) .. ")")
end,
["run"] = function(self, path)
- return LuaCode(tostring(ENVNAME) .. ":run(" .. tostring(self:compile(path)) .. ")")
+ return LuaCode("_ENV:run(" .. tostring(self:compile(path)) .. ")")
end,
["test"] = function(self, body)
if not (body.type == 'Block') then
@@ -154,10 +153,10 @@ local compile_actions = {
return LuaCode("_VERSION")
end,
["nomsu environment"] = function(self)
- return LuaCode(ENVNAME)
+ return LuaCode("_ENV")
end,
["nomsu environment name"] = function(self)
- return LuaCode(ENVNAME:as_lua())
+ return LuaCode('"_ENV"')
end
}
return compile_actions
diff --git a/bootstrap.moon b/bootstrap.moon
index 66c8553..f5b5900 100644
--- a/bootstrap.moon
+++ b/bootstrap.moon
@@ -24,8 +24,6 @@ compile_error = (source, err_msg, hint=nil)->
error(err_str, 0)
MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
---ENVNAME = jit and "getfenv(1)" or "_ENV"
-ENVNAME = jit and "_G" or "_ENV"
compile_actions = {
[""]: (fn, ...)=>
lua = LuaCode!
@@ -96,16 +94,16 @@ compile_actions = {
@compile(SyntaxTree{type:"Action", "lua", ">", code})
["1 as lua"]: (code)=>
- LuaCode("#{ENVNAME}:compile(", @compile(code), ")")
+ LuaCode("_ENV:compile(", @compile(code), ")")
["use"]: (path)=>
- LuaCode("#{ENVNAME}:use(#{@compile(path)})")
+ LuaCode("_ENV:use(#{@compile(path)})")
["export"]: (path)=>
- LuaCode("#{ENVNAME}:export(#{@compile(path)})")
+ LuaCode("_ENV:export(#{@compile(path)})")
["run"]: (path)=>
- LuaCode("#{ENVNAME}:run(#{@compile(path)})")
+ LuaCode("_ENV:run(#{@compile(path)})")
["test"]: (body)=>
unless body.type == 'Block'
@@ -118,8 +116,8 @@ compile_actions = {
["is jit"]: (code)=> LuaCode("jit")
["Lua version"]: (code)=> LuaCode("_VERSION")
- ["nomsu environment"]: ()=> LuaCode(ENVNAME)
- ["nomsu environment name"]: ()=> LuaCode(ENVNAME\as_lua!)
+ ["nomsu environment"]: ()=> LuaCode("_ENV")
+ ["nomsu environment name"]: ()=> LuaCode('"_ENV"')
}
return compile_actions
diff --git a/nomsu_environment.lua b/nomsu_environment.lua
index 7295676..9181d92 100644
--- a/nomsu_environment.lua
+++ b/nomsu_environment.lua
@@ -238,9 +238,7 @@ nomsu_environment = setmetatable({
code = NomsuCode:from(Source(path, 1, #code), code)
end
_currently_running_files:add(path)
- assert(mod[jit and "_G" or "_ENV"] == mod)
mod:run(code)
- assert(mod[jit and "_G" or "_ENV"] == mod)
_currently_running_files:pop()
package.loaded[package_name] = mod
package.loaded[path] = mod
@@ -248,17 +246,14 @@ nomsu_environment = setmetatable({
end,
use = function(self, package_name)
local mod = self:load_module(package_name)
- assert(mod[jit and "_G" or "_ENV"] == mod)
local imports = assert(_module_imports[self])
for k, v in pairs(mod) do
imports[k] = v
end
- assert(mod[jit and "_G" or "_ENV"] == mod)
return mod
end,
export = function(self, package_name)
local mod = self:load_module(package_name)
- assert(mod[jit and "_G" or "_ENV"] == mod)
local imports = assert(_module_imports[self])
for k, v in pairs(_module_imports[mod]) do
imports[k] = v
@@ -268,7 +263,6 @@ nomsu_environment = setmetatable({
self[k] = v
end
end
- assert(mod[jit and "_G" or "_ENV"] == mod)
return mod
end,
run = function(self, to_run)
@@ -365,7 +359,8 @@ nomsu_environment = setmetatable({
end
_module_imports[env] = _tbl_0
end
- env[jit and "_G" or "_ENV"] = env
+ env._ENV = env
+ env._G = env
setmetatable(env, getmetatable(nomsu_environment))
return env
end
@@ -374,7 +369,8 @@ nomsu_environment = setmetatable({
return _module_imports[self][k]
end
})
-nomsu_environment[jit and "_G" or "_ENV"] = nomsu_environment
+nomsu_environment._ENV = nomsu_environment
+nomsu_environment._G = nomsu_environment
nomsu_environment.COMPILE_RULES = require('bootstrap')
_module_imports[nomsu_environment] = { }
SOURCE_MAP = nomsu_environment.SOURCE_MAP
diff --git a/nomsu_environment.moon b/nomsu_environment.moon
index 70f2848..f82bb4e 100644
--- a/nomsu_environment.moon
+++ b/nomsu_environment.moon
@@ -125,9 +125,7 @@ nomsu_environment = setmetatable({
else
code = NomsuCode\from(Source(path, 1, #code), code)
_currently_running_files\add path
- assert(mod[jit and "_G" or "_ENV"] == mod)
mod\run(code)
- assert(mod[jit and "_G" or "_ENV"] == mod)
_currently_running_files\pop!
package.loaded[package_name] = mod
package.loaded[path] = mod
@@ -135,23 +133,19 @@ nomsu_environment = setmetatable({
use: (package_name)=>
mod = @load_module(package_name)
- assert(mod[jit and "_G" or "_ENV"] == mod)
imports = assert _module_imports[@]
for k,v in pairs(mod)
imports[k] = v
- assert(mod[jit and "_G" or "_ENV"] == mod)
return mod
export: (package_name)=>
mod = @load_module(package_name)
- assert(mod[jit and "_G" or "_ENV"] == mod)
imports = assert _module_imports[@]
for k,v in pairs(_module_imports[mod])
imports[k] = v
for k,v in pairs(mod)
if k != "_G" and k != "_ENV"
@[k] = v
- assert(mod[jit and "_G" or "_ENV"] == mod)
return mod
run: (to_run)=>
@@ -218,13 +212,15 @@ nomsu_environment = setmetatable({
new_environment: ->
env = {}
_module_imports[env] = {k,v for k,v in pairs(nomsu_environment)}
- env[jit and "_G" or "_ENV"] = env
+ env._ENV = env
+ env._G = env
setmetatable(env, getmetatable(nomsu_environment))
return env
}, {
__index: (k)=> _module_imports[@][k]
})
-nomsu_environment[jit and "_G" or "_ENV"] = nomsu_environment
+nomsu_environment._ENV = nomsu_environment
+nomsu_environment._G = nomsu_environment
nomsu_environment.COMPILE_RULES = require('bootstrap')
_module_imports[nomsu_environment] = {}