mirror of
				https://github.com/SquidDev-CC/CC-Tweaked
				synced 2025-10-31 05:33:00 +00:00 
			
		
		
		
	Simplify our overrides of load/loadstring
- Remove auto-prefixing of load/loadstring - Use Cobalt's normal load implementation, with a simple hook to set _ENV on the environment.
This commit is contained in:
		| @@ -16,22 +16,7 @@ end | |||||||
|  |  | ||||||
| if _VERSION == "Lua 5.1" then | if _VERSION == "Lua 5.1" then | ||||||
|     -- If we're on Lua 5.1, install parts of the Lua 5.2/5.3 API so that programs can be written against it |     -- If we're on Lua 5.1, install parts of the Lua 5.2/5.3 API so that programs can be written against it | ||||||
|     local type = type |  | ||||||
|     local nativeload = load |     local nativeload = load | ||||||
|     local nativeloadstring = loadstring |  | ||||||
|     local nativesetfenv = setfenv |  | ||||||
|  |  | ||||||
|     -- Historically load/loadstring would handle the chunk name as if it has |  | ||||||
|     -- been prefixed with "=". We emulate that behaviour here. |  | ||||||
|     local function prefix(chunkname) |  | ||||||
|         if type(chunkname) ~= "string" then return chunkname end |  | ||||||
|         local head = chunkname:sub(1, 1) |  | ||||||
|         if head == "=" or head == "@" then |  | ||||||
|             return chunkname |  | ||||||
|         else |  | ||||||
|             return "=" .. chunkname |  | ||||||
|         end |  | ||||||
|     end |  | ||||||
|  |  | ||||||
|     function load(x, name, mode, env) |     function load(x, name, mode, env) | ||||||
|         expect(1, x, "function", "string") |         expect(1, x, "function", "string") | ||||||
| @@ -40,29 +25,11 @@ if _VERSION == "Lua 5.1" then | |||||||
|         expect(4, env, "table", "nil") |         expect(4, env, "table", "nil") | ||||||
|  |  | ||||||
|         local ok, p1, p2 = pcall(function() |         local ok, p1, p2 = pcall(function() | ||||||
|             if type(x) == "string" then |             local result, err = nativeload(x, name, mode, env) | ||||||
|                 local result, err = nativeloadstring(x, name) |             if result and env then | ||||||
|                 if result then |  | ||||||
|                     if env then |  | ||||||
|                 env._ENV = env |                 env._ENV = env | ||||||
|                         nativesetfenv(result, env) |  | ||||||
|                     end |  | ||||||
|                     return result |  | ||||||
|                 else |  | ||||||
|                     return nil, err |  | ||||||
|                 end |  | ||||||
|             else |  | ||||||
|                 local result, err = nativeload(x, name) |  | ||||||
|                 if result then |  | ||||||
|                     if env then |  | ||||||
|                         env._ENV = env |  | ||||||
|                         nativesetfenv(result, env) |  | ||||||
|                     end |  | ||||||
|                     return result |  | ||||||
|                 else |  | ||||||
|                     return nil, err |  | ||||||
|                 end |  | ||||||
|             end |             end | ||||||
|  |             return result, err | ||||||
|         end) |         end) | ||||||
|         if ok then |         if ok then | ||||||
|             return p1, p2 |             return p1, p2 | ||||||
| @@ -81,7 +48,7 @@ if _VERSION == "Lua 5.1" then | |||||||
|         math.log10 = nil |         math.log10 = nil | ||||||
|         table.maxn = nil |         table.maxn = nil | ||||||
|     else |     else | ||||||
|         loadstring = function(string, chunkname) return nativeloadstring(string, prefix(chunkname)) end |         loadstring = function(string, chunkname) return nativeload(string, chunkname) end | ||||||
|  |  | ||||||
|         -- Inject a stub for the old bit library |         -- Inject a stub for the old bit library | ||||||
|         _G.bit = { |         _G.bit = { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Jonathan Coates
					Jonathan Coates