Tweaked code to use both _ENV and _G for better compatibility of
precompiled Lua between Lua 5.2+ and LuaJIT
This commit is contained in:
parent
0f0fb2256a
commit
43e45b1452
@ -38,7 +38,6 @@ compile_error = function(source, err_msg, hint)
|
|||||||
return error(err_str, 0)
|
return error(err_str, 0)
|
||||||
end
|
end
|
||||||
local MAX_LINE = 80
|
local MAX_LINE = 80
|
||||||
local ENVNAME = jit and "_G" or "_ENV"
|
|
||||||
local compile_actions = {
|
local compile_actions = {
|
||||||
[""] = function(self, fn, ...)
|
[""] = function(self, fn, ...)
|
||||||
local lua = LuaCode()
|
local lua = LuaCode()
|
||||||
@ -118,16 +117,16 @@ local compile_actions = {
|
|||||||
}))
|
}))
|
||||||
end,
|
end,
|
||||||
["1 as lua"] = function(self, code)
|
["1 as lua"] = function(self, code)
|
||||||
return LuaCode(tostring(ENVNAME) .. ":compile(", self:compile(code), ")")
|
return LuaCode("_ENV:compile(", self:compile(code), ")")
|
||||||
end,
|
end,
|
||||||
["use"] = function(self, path)
|
["use"] = function(self, path)
|
||||||
return LuaCode(tostring(ENVNAME) .. ":use(" .. tostring(self:compile(path)) .. ")")
|
return LuaCode("_ENV:use(" .. tostring(self:compile(path)) .. ")")
|
||||||
end,
|
end,
|
||||||
["export"] = function(self, path)
|
["export"] = function(self, path)
|
||||||
return LuaCode(tostring(ENVNAME) .. ":export(" .. tostring(self:compile(path)) .. ")")
|
return LuaCode("_ENV:export(" .. tostring(self:compile(path)) .. ")")
|
||||||
end,
|
end,
|
||||||
["run"] = function(self, path)
|
["run"] = function(self, path)
|
||||||
return LuaCode(tostring(ENVNAME) .. ":run(" .. tostring(self:compile(path)) .. ")")
|
return LuaCode("_ENV:run(" .. tostring(self:compile(path)) .. ")")
|
||||||
end,
|
end,
|
||||||
["test"] = function(self, body)
|
["test"] = function(self, body)
|
||||||
if not (body.type == 'Block') then
|
if not (body.type == 'Block') then
|
||||||
@ -154,10 +153,10 @@ local compile_actions = {
|
|||||||
return LuaCode("_VERSION")
|
return LuaCode("_VERSION")
|
||||||
end,
|
end,
|
||||||
["nomsu environment"] = function(self)
|
["nomsu environment"] = function(self)
|
||||||
return LuaCode(ENVNAME)
|
return LuaCode("_ENV")
|
||||||
end,
|
end,
|
||||||
["nomsu environment name"] = function(self)
|
["nomsu environment name"] = function(self)
|
||||||
return LuaCode(ENVNAME:as_lua())
|
return LuaCode('"_ENV"')
|
||||||
end
|
end
|
||||||
}
|
}
|
||||||
return compile_actions
|
return compile_actions
|
||||||
|
@ -24,8 +24,6 @@ compile_error = (source, err_msg, hint=nil)->
|
|||||||
error(err_str, 0)
|
error(err_str, 0)
|
||||||
|
|
||||||
MAX_LINE = 80 -- For beautification purposes, try not to make lines much longer than this value
|
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 = {
|
compile_actions = {
|
||||||
[""]: (fn, ...)=>
|
[""]: (fn, ...)=>
|
||||||
lua = LuaCode!
|
lua = LuaCode!
|
||||||
@ -96,16 +94,16 @@ compile_actions = {
|
|||||||
@compile(SyntaxTree{type:"Action", "lua", ">", code})
|
@compile(SyntaxTree{type:"Action", "lua", ">", code})
|
||||||
|
|
||||||
["1 as lua"]: (code)=>
|
["1 as lua"]: (code)=>
|
||||||
LuaCode("#{ENVNAME}:compile(", @compile(code), ")")
|
LuaCode("_ENV:compile(", @compile(code), ")")
|
||||||
|
|
||||||
["use"]: (path)=>
|
["use"]: (path)=>
|
||||||
LuaCode("#{ENVNAME}:use(#{@compile(path)})")
|
LuaCode("_ENV:use(#{@compile(path)})")
|
||||||
|
|
||||||
["export"]: (path)=>
|
["export"]: (path)=>
|
||||||
LuaCode("#{ENVNAME}:export(#{@compile(path)})")
|
LuaCode("_ENV:export(#{@compile(path)})")
|
||||||
|
|
||||||
["run"]: (path)=>
|
["run"]: (path)=>
|
||||||
LuaCode("#{ENVNAME}:run(#{@compile(path)})")
|
LuaCode("_ENV:run(#{@compile(path)})")
|
||||||
|
|
||||||
["test"]: (body)=>
|
["test"]: (body)=>
|
||||||
unless body.type == 'Block'
|
unless body.type == 'Block'
|
||||||
@ -118,8 +116,8 @@ compile_actions = {
|
|||||||
|
|
||||||
["is jit"]: (code)=> LuaCode("jit")
|
["is jit"]: (code)=> LuaCode("jit")
|
||||||
["Lua version"]: (code)=> LuaCode("_VERSION")
|
["Lua version"]: (code)=> LuaCode("_VERSION")
|
||||||
["nomsu environment"]: ()=> LuaCode(ENVNAME)
|
["nomsu environment"]: ()=> LuaCode("_ENV")
|
||||||
["nomsu environment name"]: ()=> LuaCode(ENVNAME\as_lua!)
|
["nomsu environment name"]: ()=> LuaCode('"_ENV"')
|
||||||
}
|
}
|
||||||
|
|
||||||
return compile_actions
|
return compile_actions
|
||||||
|
@ -238,9 +238,7 @@ nomsu_environment = setmetatable({
|
|||||||
code = NomsuCode:from(Source(path, 1, #code), code)
|
code = NomsuCode:from(Source(path, 1, #code), code)
|
||||||
end
|
end
|
||||||
_currently_running_files:add(path)
|
_currently_running_files:add(path)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
mod:run(code)
|
mod:run(code)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
_currently_running_files:pop()
|
_currently_running_files:pop()
|
||||||
package.loaded[package_name] = mod
|
package.loaded[package_name] = mod
|
||||||
package.loaded[path] = mod
|
package.loaded[path] = mod
|
||||||
@ -248,17 +246,14 @@ nomsu_environment = setmetatable({
|
|||||||
end,
|
end,
|
||||||
use = function(self, package_name)
|
use = function(self, package_name)
|
||||||
local mod = self:load_module(package_name)
|
local mod = self:load_module(package_name)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
local imports = assert(_module_imports[self])
|
local imports = assert(_module_imports[self])
|
||||||
for k, v in pairs(mod) do
|
for k, v in pairs(mod) do
|
||||||
imports[k] = v
|
imports[k] = v
|
||||||
end
|
end
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
return mod
|
return mod
|
||||||
end,
|
end,
|
||||||
export = function(self, package_name)
|
export = function(self, package_name)
|
||||||
local mod = self:load_module(package_name)
|
local mod = self:load_module(package_name)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
local imports = assert(_module_imports[self])
|
local imports = assert(_module_imports[self])
|
||||||
for k, v in pairs(_module_imports[mod]) do
|
for k, v in pairs(_module_imports[mod]) do
|
||||||
imports[k] = v
|
imports[k] = v
|
||||||
@ -268,7 +263,6 @@ nomsu_environment = setmetatable({
|
|||||||
self[k] = v
|
self[k] = v
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
return mod
|
return mod
|
||||||
end,
|
end,
|
||||||
run = function(self, to_run)
|
run = function(self, to_run)
|
||||||
@ -365,7 +359,8 @@ nomsu_environment = setmetatable({
|
|||||||
end
|
end
|
||||||
_module_imports[env] = _tbl_0
|
_module_imports[env] = _tbl_0
|
||||||
end
|
end
|
||||||
env[jit and "_G" or "_ENV"] = env
|
env._ENV = env
|
||||||
|
env._G = env
|
||||||
setmetatable(env, getmetatable(nomsu_environment))
|
setmetatable(env, getmetatable(nomsu_environment))
|
||||||
return env
|
return env
|
||||||
end
|
end
|
||||||
@ -374,7 +369,8 @@ nomsu_environment = setmetatable({
|
|||||||
return _module_imports[self][k]
|
return _module_imports[self][k]
|
||||||
end
|
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')
|
nomsu_environment.COMPILE_RULES = require('bootstrap')
|
||||||
_module_imports[nomsu_environment] = { }
|
_module_imports[nomsu_environment] = { }
|
||||||
SOURCE_MAP = nomsu_environment.SOURCE_MAP
|
SOURCE_MAP = nomsu_environment.SOURCE_MAP
|
||||||
|
@ -125,9 +125,7 @@ nomsu_environment = setmetatable({
|
|||||||
else
|
else
|
||||||
code = NomsuCode\from(Source(path, 1, #code), code)
|
code = NomsuCode\from(Source(path, 1, #code), code)
|
||||||
_currently_running_files\add path
|
_currently_running_files\add path
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
mod\run(code)
|
mod\run(code)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
_currently_running_files\pop!
|
_currently_running_files\pop!
|
||||||
package.loaded[package_name] = mod
|
package.loaded[package_name] = mod
|
||||||
package.loaded[path] = mod
|
package.loaded[path] = mod
|
||||||
@ -135,23 +133,19 @@ nomsu_environment = setmetatable({
|
|||||||
|
|
||||||
use: (package_name)=>
|
use: (package_name)=>
|
||||||
mod = @load_module(package_name)
|
mod = @load_module(package_name)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
imports = assert _module_imports[@]
|
imports = assert _module_imports[@]
|
||||||
for k,v in pairs(mod)
|
for k,v in pairs(mod)
|
||||||
imports[k] = v
|
imports[k] = v
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
export: (package_name)=>
|
export: (package_name)=>
|
||||||
mod = @load_module(package_name)
|
mod = @load_module(package_name)
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
imports = assert _module_imports[@]
|
imports = assert _module_imports[@]
|
||||||
for k,v in pairs(_module_imports[mod])
|
for k,v in pairs(_module_imports[mod])
|
||||||
imports[k] = v
|
imports[k] = v
|
||||||
for k,v in pairs(mod)
|
for k,v in pairs(mod)
|
||||||
if k != "_G" and k != "_ENV"
|
if k != "_G" and k != "_ENV"
|
||||||
@[k] = v
|
@[k] = v
|
||||||
assert(mod[jit and "_G" or "_ENV"] == mod)
|
|
||||||
return mod
|
return mod
|
||||||
|
|
||||||
run: (to_run)=>
|
run: (to_run)=>
|
||||||
@ -218,13 +212,15 @@ nomsu_environment = setmetatable({
|
|||||||
new_environment: ->
|
new_environment: ->
|
||||||
env = {}
|
env = {}
|
||||||
_module_imports[env] = {k,v for k,v in pairs(nomsu_environment)}
|
_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))
|
setmetatable(env, getmetatable(nomsu_environment))
|
||||||
return env
|
return env
|
||||||
}, {
|
}, {
|
||||||
__index: (k)=> _module_imports[@][k]
|
__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')
|
nomsu_environment.COMPILE_RULES = require('bootstrap')
|
||||||
_module_imports[nomsu_environment] = {}
|
_module_imports[nomsu_environment] = {}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user