Advertisement
SkeletalScripts

idk Server sided crap

Mar 28th, 2020
840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 196.00 KB | None | 0 0
  1. -- Gui to Lua
  2. -- Version: 3.2
  3.  
  4. -- Instances:
  5.  
  6.  
  7. -- Module Scripts:
  8.  
  9. local fake_module_scripts = {}
  10.  
  11. do -- nil.Loadstring
  12.     local script = Instance.new('ModuleScript', nil)
  13.     script.Name = "Loadstring"
  14.     local function module_script()
  15.         --[[
  16.             Credit to einsteinK.
  17.             Credit to Stravant for LBI.
  18.            
  19.             Credit to the creators of all the other modules used in this.
  20.            
  21.             Sceleratis was here and decided modify some things.
  22.            
  23.             einsteinK was here again to fix a bug in LBI for if-statements
  24.         --]]
  25.        
  26.         local waitDeps = {
  27.             'Rerubi';
  28.             'LuaK';
  29.             'LuaP';
  30.             'LuaU';
  31.             'LuaX';
  32.             'LuaY';
  33.             'LuaZ';
  34.         }
  35.        
  36.         for i,v in pairs(waitDeps) do script:WaitForChild(v) end
  37.        
  38.         local luaX = require(script.LuaX)
  39.         local luaY = require(script.LuaY)
  40.         local luaZ = require(script.LuaZ)
  41.         local luaU = require(script.LuaU)
  42.         local rerubi = require(script.Rerubi)
  43.        
  44.         luaX:init()
  45.         local LuaState = {}
  46.        
  47.         getfenv().script = nil
  48.        
  49.         return function(str,env)
  50.             local f,writer,buff,name
  51.             local env = env or getfenv(2)
  52.             local name = (env.script and env.script:GetFullName())
  53.             local ran,error = pcall(function()
  54.                 local zio = luaZ:init(luaZ:make_getS(str), nil)
  55.                 if not zio then return error() end
  56.                 local func = luaY:parser(LuaState, zio, nil, name or "nil")
  57.                 writer, buff = luaU:make_setS()
  58.                 luaU:dump(LuaState, func, writer, buff)
  59.                 f = rerubi(buff.data, env)
  60.             end)
  61.            
  62.             if ran then
  63.                 return f,buff.data
  64.             else
  65.                 return nil,error
  66.             end
  67.         end
  68.     end
  69.     fake_module_scripts[script] = module_script
  70. end
  71. do -- nil.LuaZ
  72.     local script = Instance.new('ModuleScript', nil)
  73.     script.Name = "LuaZ"
  74.     local function module_script()
  75.         --[[--------------------------------------------------------------------
  76.        
  77.           lzio.lua
  78.           Lua buffered streams in Lua
  79.           This file is part of Yueliang.
  80.        
  81.           Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  82.           The COPYRIGHT file describes the conditions
  83.           under which this software may be distributed.
  84.        
  85.           See the ChangeLog for more information.
  86.        
  87.         ----------------------------------------------------------------------]]
  88.        
  89.         --[[--------------------------------------------------------------------
  90.         -- Notes:
  91.         -- * EOZ is implemented as a string, "EOZ"
  92.         -- * Format of z structure (ZIO)
  93.         --     z.n       -- bytes still unread
  94.         --     z.p       -- last read position position in buffer
  95.         --     z.reader  -- chunk reader function
  96.         --     z.data    -- additional data
  97.         -- * Current position, p, is now last read index instead of a pointer
  98.         --
  99.         -- Not implemented:
  100.         -- * luaZ_lookahead: used only in lapi.c:lua_load to detect binary chunk
  101.         -- * luaZ_read: used only in lundump.c:ezread to read +1 bytes
  102.         -- * luaZ_openspace: dropped; let Lua handle buffers as strings (used in
  103.         --   lundump.c:LoadString & lvm.c:luaV_concat)
  104.         -- * luaZ buffer macros: dropped; buffers are handled as strings
  105.         -- * lauxlib.c:getF reader implementation has an extraline flag to
  106.         --   skip over a shbang (#!) line, this is not implemented here
  107.         --
  108.         -- Added:
  109.         -- (both of the following are vaguely adapted from lauxlib.c)
  110.         -- * luaZ:make_getS: create Reader from a string
  111.         -- * luaZ:make_getF: create Reader that reads from a file
  112.         --
  113.         -- Changed in 5.1.x:
  114.         -- * Chunkreader renamed to Reader (ditto with Chunkwriter)
  115.         -- * Zio struct: no more name string, added Lua state for reader
  116.         --   (however, Yueliang readers do not require a Lua state)
  117.         ----------------------------------------------------------------------]]
  118.        
  119.         local luaZ = {}
  120.        
  121.         ------------------------------------------------------------------------
  122.         -- * reader() should return a string, or nil if nothing else to parse.
  123.         --   Additional data can be set only during stream initialization
  124.         -- * Readers are handled in lauxlib.c, see luaL_load(file|buffer|string)
  125.         -- * LUAL_BUFFERSIZE=BUFSIZ=512 in make_getF() (located in luaconf.h)
  126.         -- * Original Reader typedef:
  127.         --   const char * (*lua_Reader) (lua_State *L, void *ud, size_t *sz);
  128.         -- * This Lua chunk reader implementation:
  129.         --   returns string or nil, no arguments to function
  130.         ------------------------------------------------------------------------
  131.        
  132.         ------------------------------------------------------------------------
  133.         -- create a chunk reader from a source string
  134.         ------------------------------------------------------------------------
  135.         function luaZ:make_getS(buff)
  136.           local b = buff
  137.           return function() -- chunk reader anonymous function here
  138.             if not b then return nil end
  139.             local data = b
  140.             b = nil
  141.             return data
  142.           end
  143.         end
  144.        
  145.         ------------------------------------------------------------------------
  146.         -- create a chunk reader from a source file
  147.         ------------------------------------------------------------------------
  148.         --[[
  149.         function luaZ:make_getF(filename)
  150.           local LUAL_BUFFERSIZE = 512
  151.           local h = io.open(filename, "r")
  152.           if not h then return nil end
  153.           return function() -- chunk reader anonymous function here
  154.             if not h or io.type(h) == "closed file" then return nil end
  155.             local buff = h:read(LUAL_BUFFERSIZE)
  156.             if not buff then h:close(); h = nil end
  157.             return buff
  158.           end
  159.         end
  160.         --]]
  161.         ------------------------------------------------------------------------
  162.         -- creates a zio input stream
  163.         -- returns the ZIO structure, z
  164.         ------------------------------------------------------------------------
  165.         function luaZ:init(reader, data, name)
  166.           if not reader then return end
  167.           local z = {}
  168.           z.reader = reader
  169.           z.data = data or ""
  170.           z.name = name
  171.           -- set up additional data for reading
  172.           if not data or data == "" then z.n = 0 else z.n = #data end
  173.           z.p = 0
  174.           return z
  175.         end
  176.        
  177.         ------------------------------------------------------------------------
  178.         -- fill up input buffer
  179.         ------------------------------------------------------------------------
  180.         function luaZ:fill(z)
  181.           local buff = z.reader()
  182.           z.data = buff
  183.           if not buff or buff == "" then return "EOZ" end
  184.           z.n, z.p = #buff - 1, 1
  185.           return string.sub(buff, 1, 1)
  186.         end
  187.        
  188.         ------------------------------------------------------------------------
  189.         -- get next character from the input stream
  190.         -- * local n, p are used to optimize code generation
  191.         ------------------------------------------------------------------------
  192.         function luaZ:zgetc(z)
  193.           local n, p = z.n, z.p + 1
  194.           if n > 0 then
  195.             z.n, z.p = n - 1, p
  196.             return string.sub(z.data, p, p)
  197.           else
  198.             return self:fill(z)
  199.           end
  200.         end
  201.        
  202.         return luaZ
  203.     end
  204.     fake_module_scripts[script] = module_script
  205. end
  206. do -- nil.LuaX
  207.     local script = Instance.new('ModuleScript', nil)
  208.     script.Name = "LuaX"
  209.     local function module_script()
  210.         --[[--------------------------------------------------------------------
  211.        
  212.           llex.lua
  213.           Lua lexical analyzer in Lua
  214.           This file is part of Yueliang.
  215.        
  216.           Copyright (c) 2005-2006 Kein-Hong Man <khman@users.sf.net>
  217.           The COPYRIGHT file describes the conditions
  218.           under which this software may be distributed.
  219.        
  220.           See the ChangeLog for more information.
  221.        
  222.         ----------------------------------------------------------------------]]
  223.        
  224.         --[[--------------------------------------------------------------------
  225.         -- Notes:
  226.         -- * intended to 'imitate' llex.c code; performance is not a concern
  227.         -- * tokens are strings; code structure largely retained
  228.         -- * deleted stuff (compared to llex.c) are noted, comments retained
  229.         -- * nextc() returns the currently read character to simplify coding
  230.         --   here; next() in llex.c does not return anything
  231.         -- * compatibility code is marked with "--#" comments
  232.         --
  233.         -- Added:
  234.         -- * luaX:chunkid (function luaO_chunkid from lobject.c)
  235.         -- * luaX:str2d (function luaO_str2d from lobject.c)
  236.         -- * luaX.LUA_QS used in luaX:lexerror (from luaconf.h)
  237.         -- * luaX.LUA_COMPAT_LSTR in luaX:read_long_string (from luaconf.h)
  238.         -- * luaX.MAX_INT used in luaX:inclinenumber (from llimits.h)
  239.         --
  240.         -- To use the lexer:
  241.         -- (1) luaX:init() to initialize the lexer
  242.         -- (2) luaX:setinput() to set the input stream to lex
  243.         -- (3) call luaX:next() or luaX:luaX:lookahead() to get tokens,
  244.         --     until "TK_EOS": luaX:next()
  245.         -- * since EOZ is returned as a string, be careful when regexp testing
  246.         --
  247.         -- Not implemented:
  248.         -- * luaX_newstring: not required by this Lua implementation
  249.         -- * buffer MAX_SIZET size limit (from llimits.h) test not implemented
  250.         --   in the interest of performance
  251.         -- * locale-aware number handling is largely redundant as Lua's
  252.         --   tonumber() function is already capable of this
  253.         --
  254.         -- Changed in 5.1.x:
  255.         -- * TK_NAME token order moved down
  256.         -- * string representation for TK_NAME, TK_NUMBER, TK_STRING changed
  257.         -- * token struct renamed to lower case (LS -> ls)
  258.         -- * LexState struct: removed nestlevel, added decpoint
  259.         -- * error message functions have been greatly simplified
  260.         -- * token2string renamed to luaX_tokens, exposed in llex.h
  261.         -- * lexer now handles all kinds of newlines, including CRLF
  262.         -- * shbang first line handling removed from luaX:setinput;
  263.         --   it is now done in lauxlib.c (luaL_loadfile)
  264.         -- * next(ls) macro renamed to nextc(ls) due to new luaX_next function
  265.         -- * EXTRABUFF and MAXNOCHECK removed due to lexer changes
  266.         -- * checkbuffer(ls, len) macro deleted
  267.         -- * luaX:read_numeral now has 3 support functions: luaX:trydecpoint,
  268.         --   luaX:buffreplace and (luaO_str2d from lobject.c) luaX:str2d
  269.         -- * luaX:read_numeral is now more promiscuous in slurping characters;
  270.         --   hexadecimal numbers was added, locale-aware decimal points too
  271.         -- * luaX:skip_sep is new; used by luaX:read_long_string
  272.         -- * luaX:read_long_string handles new-style long blocks, with some
  273.         --   optional compatibility code
  274.         -- * luaX:llex: parts changed to support new-style long blocks
  275.         -- * luaX:llex: readname functionality has been folded in
  276.         -- * luaX:llex: removed test for control characters
  277.         --
  278.         --------------------------------------------------------------------]]
  279.        
  280.         local luaZ = require(script.Parent.LuaZ)
  281.        
  282.         local luaX = {}
  283.        
  284.         -- FIRST_RESERVED is not required as tokens are manipulated as strings
  285.         -- TOKEN_LEN deleted; maximum length of a reserved word not needed
  286.        
  287.         ------------------------------------------------------------------------
  288.         -- "ORDER RESERVED" deleted; enumeration in one place: luaX.RESERVED
  289.         ------------------------------------------------------------------------
  290.        
  291.         -- terminal symbols denoted by reserved words: TK_AND to TK_WHILE
  292.         -- other terminal symbols: TK_NAME to TK_EOS
  293.         luaX.RESERVED = [[
  294.         TK_AND and
  295.         TK_BREAK break
  296.         TK_DO do
  297.         TK_ELSE else
  298.         TK_ELSEIF elseif
  299.         TK_END end
  300.         TK_FALSE false
  301.         TK_FOR for
  302.         TK_FUNCTION function
  303.         TK_IF if
  304.         TK_IN in
  305.         TK_LOCAL local
  306.         TK_NIL nil
  307.         TK_NOT not
  308.         TK_OR or
  309.         TK_REPEAT repeat
  310.         TK_RETURN return
  311.         TK_THEN then
  312.         TK_TRUE true
  313.         TK_UNTIL until
  314.         TK_WHILE while
  315.         TK_CONCAT ..
  316.         TK_DOTS ...
  317.         TK_EQ ==
  318.         TK_GE >=
  319.         TK_LE <=
  320.         TK_NE ~=
  321.         TK_NAME <name>
  322.         TK_NUMBER <number>
  323.         TK_STRING <string>
  324.         TK_EOS <eof>]]
  325.        
  326.         -- NUM_RESERVED is not required; number of reserved words
  327.        
  328.         --[[--------------------------------------------------------------------
  329.         -- Instead of passing seminfo, the Token struct (e.g. ls.t) is passed
  330.         -- so that lexer functions can use its table element, ls.t.seminfo
  331.         --
  332.         -- SemInfo (struct no longer needed, a mixed-type value is used)
  333.         --
  334.         -- Token (struct of ls.t and ls.lookahead):
  335.         --   token  -- token symbol
  336.         --   seminfo  -- semantics information
  337.         --
  338.         -- LexState (struct of ls; ls is initialized by luaX:setinput):
  339.         --   current  -- current character (charint)
  340.         --   linenumber  -- input line counter
  341.         --   lastline  -- line of last token 'consumed'
  342.         --   t  -- current token (table: struct Token)
  343.         --   lookahead  -- look ahead token (table: struct Token)
  344.         --   fs  -- 'FuncState' is private to the parser
  345.         --   L -- LuaState
  346.         --   z  -- input stream
  347.         --   buff  -- buffer for tokens
  348.         --   source  -- current source name
  349.         --   decpoint -- locale decimal point
  350.         --   nestlevel  -- level of nested non-terminals
  351.         ----------------------------------------------------------------------]]
  352.        
  353.         -- luaX.tokens (was luaX_tokens) is now a hash; see luaX:init
  354.        
  355.         luaX.MAXSRC = 80
  356.         luaX.MAX_INT = 2147483645       -- constants from elsewhere (see above)
  357.         luaX.LUA_QS = "'%s'"
  358.         luaX.LUA_COMPAT_LSTR = 1
  359.         --luaX.MAX_SIZET = 4294967293
  360.        
  361.         ------------------------------------------------------------------------
  362.         -- initialize lexer
  363.         -- * original luaX_init has code to create and register token strings
  364.         -- * luaX.tokens: TK_* -> token
  365.         -- * luaX.enums:  token -> TK_* (used in luaX:llex)
  366.         ------------------------------------------------------------------------
  367.         function luaX:init()
  368.           local tokens, enums = {}, {}
  369.           for v in string.gmatch(self.RESERVED, "[^\n]+") do
  370.             local _, _, tok, str = string.find(v, "(%S+)%s+(%S+)")
  371.             tokens[tok] = str
  372.             enums[str] = tok
  373.           end
  374.           self.tokens = tokens
  375.           self.enums = enums
  376.         end
  377.        
  378.         ------------------------------------------------------------------------
  379.         -- returns a suitably-formatted chunk name or id
  380.         -- * from lobject.c, used in llex.c and ldebug.c
  381.         -- * the result, out, is returned (was first argument)
  382.         ------------------------------------------------------------------------
  383.         function luaX:chunkid(source, bufflen)
  384.           local out
  385.           local first = string.sub(source, 1, 1)
  386.           if first == "=" then
  387.             out = string.sub(source, 2, bufflen)  -- remove first char
  388.           else  -- out = "source", or "...source"
  389.             if first == "@" then
  390.               source = string.sub(source, 2)  -- skip the '@'
  391.               bufflen = bufflen - #" '...' "
  392.               local l = #source
  393.               out = ""
  394.               if l > bufflen then
  395.                 source = string.sub(source, 1 + l - bufflen)  -- get last part of file name
  396.                 out = out.."..."
  397.               end
  398.               out = out..source
  399.             else  -- out = [string "string"]
  400.               local len = string.find(source, "[\n\r]")  -- stop at first newline
  401.               len = len and (len - 1) or #source
  402.               bufflen = bufflen - #(" [string \"...\"] ")
  403.               if len > bufflen then len = bufflen end
  404.               out = "[string \""
  405.               if len < #source then  -- must truncate?
  406.                 out = out..string.sub(source, 1, len).."..."
  407.               else
  408.                 out = out..source
  409.               end
  410.               out = out.."\"]"
  411.             end
  412.           end
  413.           return out
  414.         end
  415.        
  416.         --[[--------------------------------------------------------------------
  417.         -- Support functions for lexer
  418.         -- * all lexer errors eventually reaches lexerror:
  419.              syntaxerror -> lexerror
  420.         ----------------------------------------------------------------------]]
  421.        
  422.         ------------------------------------------------------------------------
  423.         -- look up token and return keyword if found (also called by parser)
  424.         ------------------------------------------------------------------------
  425.         function luaX:token2str(ls, token)
  426.           if string.sub(token, 1, 3) ~= "TK_" then
  427.             if string.find(token, "%c") then
  428.               return string.format("char(%d)", string.byte(token))
  429.             end
  430.             return token
  431.           else
  432.           end
  433.             return self.tokens[token]
  434.         end
  435.        
  436.         ------------------------------------------------------------------------
  437.         -- throws a lexer error
  438.         -- * txtToken has been made local to luaX:lexerror
  439.         -- * can't communicate LUA_ERRSYNTAX, so it is unimplemented
  440.         ------------------------------------------------------------------------
  441.         function luaX:lexerror(ls, msg, token)
  442.           local function txtToken(ls, token)
  443.             if token == "TK_NAME" or
  444.                token == "TK_STRING" or
  445.                token == "TK_NUMBER" then
  446.               return ls.buff
  447.             else
  448.               return self:token2str(ls, token)
  449.             end
  450.           end
  451.           local buff = self:chunkid(ls.source, self.MAXSRC)
  452.           local msg = string.format("%s:%d: %s", buff, ls.linenumber, msg)
  453.           if token then
  454.             msg = string.format("%s near "..self.LUA_QS, msg, txtToken(ls, token))
  455.           end
  456.           -- luaD_throw(ls->L, LUA_ERRSYNTAX)
  457.           error(msg)
  458.         end
  459.        
  460.         ------------------------------------------------------------------------
  461.         -- throws a syntax error (mainly called by parser)
  462.         -- * ls.t.token has to be set by the function calling luaX:llex
  463.         --   (see luaX:next and luaX:lookahead elsewhere in this file)
  464.         ------------------------------------------------------------------------
  465.         function luaX:syntaxerror(ls, msg)
  466.           self:lexerror(ls, msg, ls.t.token)
  467.         end
  468.        
  469.         ------------------------------------------------------------------------
  470.         -- move on to next line
  471.         ------------------------------------------------------------------------
  472.         function luaX:currIsNewline(ls)
  473.           return ls.current == "\n" or ls.current == "\r"
  474.         end
  475.        
  476.         function luaX:inclinenumber(ls)
  477.           local old = ls.current
  478.           -- lua_assert(currIsNewline(ls))
  479.           self:nextc(ls)  -- skip '\n' or '\r'
  480.           if self:currIsNewline(ls) and ls.current ~= old then
  481.             self:nextc(ls)  -- skip '\n\r' or '\r\n'
  482.           end
  483.           ls.linenumber = ls.linenumber + 1
  484.           if ls.linenumber >= self.MAX_INT then
  485.             self:syntaxerror(ls, "chunk has too many lines")
  486.           end
  487.         end
  488.        
  489.         ------------------------------------------------------------------------
  490.         -- initializes an input stream for lexing
  491.         -- * if ls (the lexer state) is passed as a table, then it is filled in,
  492.         --   otherwise it has to be retrieved as a return value
  493.         -- * LUA_MINBUFFER not used; buffer handling not required any more
  494.         ------------------------------------------------------------------------
  495.         function luaX:setinput(L, ls, z, source)
  496.           if not ls then ls = {} end  -- create struct
  497.           if not ls.lookahead then ls.lookahead = {} end
  498.           if not ls.t then ls.t = {} end
  499.           ls.decpoint = "."
  500.           ls.L = L
  501.           ls.lookahead.token = "TK_EOS"  -- no look-ahead token
  502.           ls.z = z
  503.           ls.fs = nil
  504.           ls.linenumber = 1
  505.           ls.lastline = 1
  506.           ls.source = source
  507.           self:nextc(ls)  -- read first char
  508.         end
  509.        
  510.         --[[--------------------------------------------------------------------
  511.         -- LEXICAL ANALYZER
  512.         ----------------------------------------------------------------------]]
  513.        
  514.         ------------------------------------------------------------------------
  515.         -- checks if current character read is found in the set 'set'
  516.         ------------------------------------------------------------------------
  517.         function luaX:check_next(ls, set)
  518.           if not string.find(set, ls.current, 1, 1) then
  519.             return false
  520.           end
  521.           self:save_and_next(ls)
  522.           return true
  523.         end
  524.        
  525.         ------------------------------------------------------------------------
  526.         -- retrieve next token, checking the lookahead buffer if necessary
  527.         -- * note that the macro next(ls) in llex.c is now luaX:nextc
  528.         -- * utilized used in lparser.c (various places)
  529.         ------------------------------------------------------------------------
  530.         function luaX:next(ls)
  531.           ls.lastline = ls.linenumber
  532.           if ls.lookahead.token ~= "TK_EOS" then  -- is there a look-ahead token?
  533.             -- this must be copy-by-value
  534.             ls.t.seminfo = ls.lookahead.seminfo  -- use this one
  535.             ls.t.token = ls.lookahead.token
  536.             ls.lookahead.token = "TK_EOS"  -- and discharge it
  537.           else
  538.             ls.t.token = self:llex(ls, ls.t)  -- read next token
  539.           end
  540.         end
  541.        
  542.         ------------------------------------------------------------------------
  543.         -- fill in the lookahead buffer
  544.         -- * utilized used in lparser.c:constructor
  545.         ------------------------------------------------------------------------
  546.         function luaX:lookahead(ls)
  547.           -- lua_assert(ls.lookahead.token == "TK_EOS")
  548.           ls.lookahead.token = self:llex(ls, ls.lookahead)
  549.         end
  550.        
  551.         ------------------------------------------------------------------------
  552.         -- gets the next character and returns it
  553.         -- * this is the next() macro in llex.c; see notes at the beginning
  554.         ------------------------------------------------------------------------
  555.         function luaX:nextc(ls)
  556.           local c = luaZ:zgetc(ls.z)
  557.           ls.current = c
  558.           return c
  559.         end
  560.        
  561.         ------------------------------------------------------------------------
  562.         -- saves the given character into the token buffer
  563.         -- * buffer handling code removed, not used in this implementation
  564.         -- * test for maximum token buffer length not used, makes things faster
  565.         ------------------------------------------------------------------------
  566.        
  567.         function luaX:save(ls, c)
  568.           local buff = ls.buff
  569.           -- if you want to use this, please uncomment luaX.MAX_SIZET further up
  570.           --if #buff > self.MAX_SIZET then
  571.           --  self:lexerror(ls, "lexical element too long")
  572.           --end
  573.           ls.buff = buff..c
  574.         end
  575.        
  576.         ------------------------------------------------------------------------
  577.         -- save current character into token buffer, grabs next character
  578.         -- * like luaX:nextc, returns the character read for convenience
  579.         ------------------------------------------------------------------------
  580.         function luaX:save_and_next(ls)
  581.           self:save(ls, ls.current)
  582.           return self:nextc(ls)
  583.         end
  584.        
  585.         ------------------------------------------------------------------------
  586.         -- LUA_NUMBER
  587.         -- * luaX:read_numeral is the main lexer function to read a number
  588.         -- * luaX:str2d, luaX:buffreplace, luaX:trydecpoint are support functions
  589.         ------------------------------------------------------------------------
  590.        
  591.         ------------------------------------------------------------------------
  592.         -- string to number converter (was luaO_str2d from lobject.c)
  593.         -- * returns the number, nil if fails (originally returns a boolean)
  594.         -- * conversion function originally lua_str2number(s,p), a macro which
  595.         --   maps to the strtod() function by default (from luaconf.h)
  596.         ------------------------------------------------------------------------
  597.         function luaX:str2d(s)
  598.           local result = tonumber(s)
  599.           if result then return result end
  600.           -- conversion failed
  601.           if string.lower(string.sub(s, 1, 2)) == "0x" then  -- maybe an hexadecimal constant?
  602.             result = tonumber(s, 16)
  603.             if result then return result end  -- most common case
  604.             -- Was: invalid trailing characters?
  605.             -- In C, this function then skips over trailing spaces.
  606.             -- true is returned if nothing else is found except for spaces.
  607.             -- If there is still something else, then it returns a false.
  608.             -- All this is not necessary using Lua's tonumber.
  609.           end
  610.           return nil
  611.         end
  612.        
  613.         ------------------------------------------------------------------------
  614.         -- single-character replacement, for locale-aware decimal points
  615.         ------------------------------------------------------------------------
  616.         function luaX:buffreplace(ls, from, to)
  617.           local result, buff = "", ls.buff
  618.           for p = 1, #buff do
  619.             local c = string.sub(buff, p, p)
  620.             if c == from then c = to end
  621.             result = result..c
  622.           end
  623.           ls.buff = result
  624.         end
  625.        
  626.         ------------------------------------------------------------------------
  627.         -- Attempt to convert a number by translating '.' decimal points to
  628.         -- the decimal point character used by the current locale. This is not
  629.         -- needed in Yueliang as Lua's tonumber() is already locale-aware.
  630.         -- Instead, the code is here in case the user implements localeconv().
  631.         ------------------------------------------------------------------------
  632.         function luaX:trydecpoint(ls, Token)
  633.           -- format error: try to update decimal point separator
  634.           local old = ls.decpoint
  635.           -- translate the following to Lua if you implement localeconv():
  636.           -- struct lconv *cv = localeconv();
  637.           -- ls->decpoint = (cv ? cv->decimal_point[0] : '.');
  638.           self:buffreplace(ls, old, ls.decpoint)  -- try updated decimal separator
  639.           local seminfo = self:str2d(ls.buff)
  640.           Token.seminfo = seminfo
  641.           if not seminfo then
  642.             -- format error with correct decimal point: no more options
  643.             self:buffreplace(ls, ls.decpoint, ".")  -- undo change (for error message)
  644.             self:lexerror(ls, "malformed number", "TK_NUMBER")
  645.           end
  646.         end
  647.        
  648.         ------------------------------------------------------------------------
  649.         -- main number conversion function
  650.         -- * "^%w$" needed in the scan in order to detect "EOZ"
  651.         ------------------------------------------------------------------------
  652.         function luaX:read_numeral(ls, Token)
  653.           -- lua_assert(string.find(ls.current, "%d"))
  654.           repeat
  655.             self:save_and_next(ls)
  656.           until string.find(ls.current, "%D") and ls.current ~= "."
  657.           if self:check_next(ls, "Ee") then  -- 'E'?
  658.             self:check_next(ls, "+-")  -- optional exponent sign
  659.           end
  660.           while string.find(ls.current, "^%w$") or ls.current == "_" do
  661.             self:save_and_next(ls)
  662.           end
  663.           self:buffreplace(ls, ".", ls.decpoint)  -- follow locale for decimal point
  664.           local seminfo = self:str2d(ls.buff)
  665.           Token.seminfo = seminfo
  666.           if not seminfo then  -- format error?
  667.             self:trydecpoint(ls, Token) -- try to update decimal point separator
  668.           end
  669.         end
  670.        
  671.         ------------------------------------------------------------------------
  672.         -- count separators ("=") in a long string delimiter
  673.         -- * used by luaX:read_long_string
  674.         ------------------------------------------------------------------------
  675.         function luaX:skip_sep(ls)
  676.           local count = 0
  677.           local s = ls.current
  678.           -- lua_assert(s == "[" or s == "]")
  679.           self:save_and_next(ls)
  680.           while ls.current == "=" do
  681.             self:save_and_next(ls)
  682.             count = count + 1
  683.           end
  684.           return (ls.current == s) and count or (-count) - 1
  685.         end
  686.        
  687.         ------------------------------------------------------------------------
  688.         -- reads a long string or long comment
  689.         ------------------------------------------------------------------------
  690.         function luaX:read_long_string(ls, Token, sep)
  691.           local cont = 0
  692.           self:save_and_next(ls)  -- skip 2nd '['
  693.           if self:currIsNewline(ls) then  -- string starts with a newline?
  694.             self:inclinenumber(ls)  -- skip it
  695.           end
  696.           while true do
  697.             local c = ls.current
  698.             if c == "EOZ" then
  699.               self:lexerror(ls, Token and "unfinished long string" or
  700.                             "unfinished long comment", "TK_EOS")
  701.             elseif c == "[" then
  702.               --# compatibility code start
  703.               if self.LUA_COMPAT_LSTR then
  704.                 if self:skip_sep(ls) == sep then
  705.                   self:save_and_next(ls)  -- skip 2nd '['
  706.                   cont = cont + 1
  707.                   --# compatibility code start
  708.                   if self.LUA_COMPAT_LSTR == 1 then
  709.                     if sep == 0 then
  710.                       self:lexerror(ls, "nesting of [[...]] is deprecated", "[")
  711.                     end
  712.                   end
  713.                   --# compatibility code end
  714.                 end
  715.               end
  716.               --# compatibility code end
  717.             elseif c == "]" then
  718.               if self:skip_sep(ls) == sep then
  719.                 self:save_and_next(ls)  -- skip 2nd ']'
  720.                 --# compatibility code start
  721.                 if self.LUA_COMPAT_LSTR and self.LUA_COMPAT_LSTR == 2 then
  722.                   cont = cont - 1
  723.                   if sep == 0 and cont >= 0 then break end
  724.                 end
  725.                 --# compatibility code end
  726.                 break
  727.               end
  728.             elseif self:currIsNewline(ls) then
  729.               self:save(ls, "\n")
  730.               self:inclinenumber(ls)
  731.               if not Token then ls.buff = "" end -- avoid wasting space
  732.             else  -- default
  733.               if Token then
  734.                 self:save_and_next(ls)
  735.               else
  736.                 self:nextc(ls)
  737.               end
  738.             end--if c
  739.           end--while
  740.           if Token then
  741.             local p = 3 + sep
  742.             Token.seminfo = string.sub(ls.buff, p, -p)
  743.           end
  744.         end
  745.        
  746.         ------------------------------------------------------------------------
  747.         -- reads a string
  748.         -- * has been restructured significantly compared to the original C code
  749.         ------------------------------------------------------------------------
  750.        
  751.         function luaX:read_string(ls, del, Token)
  752.           self:save_and_next(ls)
  753.           while ls.current ~= del do
  754.             local c = ls.current
  755.             if c == "EOZ" then
  756.               self:lexerror(ls, "unfinished string", "TK_EOS")
  757.             elseif self:currIsNewline(ls) then
  758.               self:lexerror(ls, "unfinished string", "TK_STRING")
  759.             elseif c == "\\" then
  760.               c = self:nextc(ls)  -- do not save the '\'
  761.               if self:currIsNewline(ls) then  -- go through
  762.                 self:save(ls, "\n")
  763.                 self:inclinenumber(ls)
  764.               elseif c ~= "EOZ" then -- will raise an error next loop
  765.                 -- escapes handling greatly simplified here:
  766.                 local i = string.find("abfnrtv", c, 1, 1)
  767.                 if i then
  768.                   self:save(ls, string.sub("\a\b\f\n\r\t\v", i, i))
  769.                   self:nextc(ls)
  770.                 elseif not string.find(c, "%d") then
  771.                   self:save_and_next(ls)  -- handles \\, \", \', and \?
  772.                 else  -- \xxx
  773.                   c, i = 0, 0
  774.                   repeat
  775.                     c = 10 * c + ls.current
  776.                     self:nextc(ls)
  777.                     i = i + 1
  778.                   until i >= 3 or not string.find(ls.current, "%d")
  779.                   if c > 255 then  -- UCHAR_MAX
  780.                     self:lexerror(ls, "escape sequence too large", "TK_STRING")
  781.                   end
  782.                   self:save(ls, string.char(c))
  783.                 end
  784.               end
  785.             else
  786.               self:save_and_next(ls)
  787.             end--if c
  788.           end--while
  789.           self:save_and_next(ls)  -- skip delimiter
  790.           Token.seminfo = string.sub(ls.buff, 2, -2)
  791.         end
  792.        
  793.         ------------------------------------------------------------------------
  794.         -- main lexer function
  795.         ------------------------------------------------------------------------
  796.         function luaX:llex(ls, Token)
  797.           ls.buff = ""
  798.           while true do
  799.             local c = ls.current
  800.             ----------------------------------------------------------------
  801.             if self:currIsNewline(ls) then
  802.               self:inclinenumber(ls)
  803.             ----------------------------------------------------------------
  804.             elseif c == "-" then
  805.               c = self:nextc(ls)
  806.               if c ~= "-" then return "-" end
  807.               -- else is a comment
  808.               local sep = -1
  809.               if self:nextc(ls) == '[' then
  810.                 sep = self:skip_sep(ls)
  811.                 ls.buff = ""  -- 'skip_sep' may dirty the buffer
  812.               end
  813.               if sep >= 0 then
  814.                 self:read_long_string(ls, nil, sep)  -- long comment
  815.                 ls.buff = ""
  816.               else  -- else short comment
  817.                 while not self:currIsNewline(ls) and ls.current ~= "EOZ" do
  818.                   self:nextc(ls)
  819.                 end
  820.               end
  821.             ----------------------------------------------------------------
  822.             elseif c == "[" then
  823.               local sep = self:skip_sep(ls)
  824.               if sep >= 0 then
  825.                 self:read_long_string(ls, Token, sep)
  826.                 return "TK_STRING"
  827.               elseif sep == -1 then
  828.                 return "["
  829.               else
  830.                 self:lexerror(ls, "invalid long string delimiter", "TK_STRING")
  831.               end
  832.             ----------------------------------------------------------------
  833.             elseif c == "=" then
  834.               c = self:nextc(ls)
  835.               if c ~= "=" then return "="
  836.               else self:nextc(ls); return "TK_EQ" end
  837.             ----------------------------------------------------------------
  838.             elseif c == "<" then
  839.               c = self:nextc(ls)
  840.               if c ~= "=" then return "<"
  841.               else self:nextc(ls); return "TK_LE" end
  842.             ----------------------------------------------------------------
  843.             elseif c == ">" then
  844.               c = self:nextc(ls)
  845.               if c ~= "=" then return ">"
  846.               else self:nextc(ls); return "TK_GE" end
  847.             ----------------------------------------------------------------
  848.             elseif c == "~" then
  849.               c = self:nextc(ls)
  850.               if c ~= "=" then return "~"
  851.               else self:nextc(ls); return "TK_NE" end
  852.             ----------------------------------------------------------------
  853.             elseif c == "\"" or c == "'" then
  854.               self:read_string(ls, c, Token)
  855.               return "TK_STRING"
  856.             ----------------------------------------------------------------
  857.             elseif c == "." then
  858.               c = self:save_and_next(ls)
  859.               if self:check_next(ls, ".") then
  860.                 if self:check_next(ls, ".") then
  861.                   return "TK_DOTS"   -- ...
  862.                 else return "TK_CONCAT"   -- ..
  863.                 end
  864.               elseif not string.find(c, "%d") then
  865.                 return "."
  866.               else
  867.                 self:read_numeral(ls, Token)
  868.                 return "TK_NUMBER"
  869.               end
  870.             ----------------------------------------------------------------
  871.             elseif c == "EOZ" then
  872.               return "TK_EOS"
  873.             ----------------------------------------------------------------
  874.             else  -- default
  875.               if string.find(c, "%s") then
  876.                 -- lua_assert(self:currIsNewline(ls))
  877.                 self:nextc(ls)
  878.               elseif string.find(c, "%d") then
  879.                 self:read_numeral(ls, Token)
  880.                 return "TK_NUMBER"
  881.               elseif string.find(c, "[_%a]") then
  882.                 -- identifier or reserved word
  883.                 repeat
  884.                   c = self:save_and_next(ls)
  885.                 until c == "EOZ" or not string.find(c, "[_%w]")
  886.                 local ts = ls.buff
  887.                 local tok = self.enums[ts]
  888.                 if tok then return tok end  -- reserved word?
  889.                 Token.seminfo = ts
  890.                 return "TK_NAME"
  891.               else
  892.                 self:nextc(ls)
  893.                 return c  -- single-char tokens (+ - / ...)
  894.               end
  895.             ----------------------------------------------------------------
  896.             end--if c
  897.           end--while
  898.         end
  899.        
  900.         return luaX
  901.     end
  902.     fake_module_scripts[script] = module_script
  903. end
  904. do -- nil.LuaY
  905.     local script = Instance.new('ModuleScript', nil)
  906.     script.Name = "LuaY"
  907.     local function module_script()
  908.         --[[--------------------------------------------------------------------
  909.        
  910.           lparser.lua
  911.           Lua 5 parser in Lua
  912.           This file is part of Yueliang.
  913.        
  914.           Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  915.           The COPYRIGHT file describes the conditions
  916.           under which this software may be distributed.
  917.        
  918.           See the ChangeLog for more information.
  919.        
  920.         ----------------------------------------------------------------------]]
  921.        
  922.         --[[--------------------------------------------------------------------
  923.         -- Notes:
  924.         -- * some unused C code that were not converted are kept as comments
  925.         -- * LUA_COMPAT_VARARG option changed into a comment block
  926.         -- * for value/size specific code added, look for 'NOTE: '
  927.         --
  928.         -- Not implemented:
  929.         -- * luaX_newstring not needed by this Lua implementation
  930.         -- * luaG_checkcode() in assert is not currently implemented
  931.         --
  932.         -- Added:
  933.         -- * some constants added from various header files
  934.         -- * luaY.LUA_QS used in error_expected, check_match (from luaconf.h)
  935.         -- * luaY:LUA_QL needed for error messages (from luaconf.h)
  936.         -- * luaY:growvector (from lmem.h) -- skeleton only, limit checking
  937.         -- * luaY.SHRT_MAX (from <limits.h>) for registerlocalvar
  938.         -- * luaY:newproto (from lfunc.c)
  939.         -- * luaY:int2fb (from lobject.c)
  940.         -- * NOTE: HASARG_MASK, for implementing a VARARG_HASARG bit operation
  941.         -- * NOTE: value-specific code for VARARG_NEEDSARG to replace a bitop
  942.         --
  943.         -- Changed in 5.1.x:
  944.         -- * various code changes are not detailed...
  945.         -- * names of constants may have changed, e.g. added a LUAI_ prefix
  946.         -- * struct expkind: added VKNUM, VVARARG; VCALL's info changed?
  947.         -- * struct expdesc: added nval
  948.         -- * struct FuncState: upvalues data type changed to upvaldesc
  949.         -- * macro hasmultret is new
  950.         -- * function checklimit moved to parser from lexer
  951.         -- * functions anchor_token, errorlimit, checknext are new
  952.         -- * checknext is new, equivalent to 5.0.x's check, see check too
  953.         -- * luaY:next and luaY:lookahead moved to lexer
  954.         -- * break keyword no longer skipped in luaY:breakstat
  955.         -- * function new_localvarstr replaced by new_localvarliteral
  956.         -- * registerlocalvar limits local variables to SHRT_MAX
  957.         -- * create_local deleted, new_localvarliteral used instead
  958.         -- * constant LUAI_MAXUPVALUES increased to 60
  959.         -- * constants MAXPARAMS, LUA_MAXPARSERLEVEL, MAXSTACK removed
  960.         -- * function interface changed: singlevaraux, singlevar
  961.         -- * enterlevel and leavelevel uses nCcalls to track call depth
  962.         -- * added a name argument to main entry function, luaY:parser
  963.         -- * function luaY_index changed to yindex
  964.         -- * luaY:int2fb()'s table size encoding format has been changed
  965.         -- * luaY:log2() no longer needed for table constructors
  966.         -- * function code_params deleted, functionality folded in parlist
  967.         -- * vararg flags handling (is_vararg) changes; also see VARARG_*
  968.         -- * LUA_COMPATUPSYNTAX section for old-style upvalues removed
  969.         -- * repeatstat() calls chunk() instead of block()
  970.         -- * function interface changed: cond, test_then_block
  971.         -- * while statement implementation considerably simplified; MAXEXPWHILE
  972.         --   and EXTRAEXP no longer required, no limits to the complexity of a
  973.         --   while condition
  974.         -- * repeat, forbody statement implementation has major changes,
  975.         --   mostly due to new scoping behaviour of local variables
  976.         -- * OPR_MULT renamed to OPR_MUL
  977.         ----------------------------------------------------------------------]]
  978.        
  979.         --requires luaP, luaX, luaK
  980.         local luaY = {}
  981.         local luaX = require(script.Parent.LuaX)
  982.         local luaK = require(script.Parent.LuaK)(luaY)
  983.         local luaP = require(script.Parent.LuaP)
  984.        
  985.         --[[--------------------------------------------------------------------
  986.         -- Expression descriptor
  987.         -- * expkind changed to string constants; luaY:assignment was the only
  988.         --   function to use a relational operator with this enumeration
  989.         -- VVOID       -- no value
  990.         -- VNIL        -- no value
  991.         -- VTRUE       -- no value
  992.         -- VFALSE      -- no value
  993.         -- VK          -- info = index of constant in 'k'
  994.         -- VKNUM       -- nval = numerical value
  995.         -- VLOCAL      -- info = local register
  996.         -- VUPVAL,     -- info = index of upvalue in 'upvalues'
  997.         -- VGLOBAL     -- info = index of table; aux = index of global name in 'k'
  998.         -- VINDEXED    -- info = table register; aux = index register (or 'k')
  999.         -- VJMP        -- info = instruction pc
  1000.         -- VRELOCABLE  -- info = instruction pc
  1001.         -- VNONRELOC   -- info = result register
  1002.         -- VCALL       -- info = instruction pc
  1003.         -- VVARARG     -- info = instruction pc
  1004.         } ----------------------------------------------------------------------]]
  1005.        
  1006.         --[[--------------------------------------------------------------------
  1007.         -- * expdesc in Lua 5.1.x has a union u and another struct s; this Lua
  1008.         --   implementation ignores all instances of u and s usage
  1009.         -- struct expdesc:
  1010.         --   k  -- (enum: expkind)
  1011.         --   info, aux -- (int, int)
  1012.         --   nval -- (lua_Number)
  1013.         --   t  -- patch list of 'exit when true'
  1014.         --   f  -- patch list of 'exit when false'
  1015.         ----------------------------------------------------------------------]]
  1016.        
  1017.         --[[--------------------------------------------------------------------
  1018.         -- struct upvaldesc:
  1019.         --   k  -- (lu_byte)
  1020.         --   info -- (lu_byte)
  1021.         ----------------------------------------------------------------------]]
  1022.        
  1023.         --[[--------------------------------------------------------------------
  1024.         -- state needed to generate code for a given function
  1025.         -- struct FuncState:
  1026.         --   f  -- current function header (table: Proto)
  1027.         --   h  -- table to find (and reuse) elements in 'k' (table: Table)
  1028.         --   prev  -- enclosing function (table: FuncState)
  1029.         --   ls  -- lexical state (table: LexState)
  1030.         --   L  -- copy of the Lua state (table: lua_State)
  1031.         --   bl  -- chain of current blocks (table: BlockCnt)
  1032.         --   pc  -- next position to code (equivalent to 'ncode')
  1033.         --   lasttarget   -- 'pc' of last 'jump target'
  1034.         --   jpc  -- list of pending jumps to 'pc'
  1035.         --   freereg  -- first free register
  1036.         --   nk  -- number of elements in 'k'
  1037.         --   np  -- number of elements in 'p'
  1038.         --   nlocvars  -- number of elements in 'locvars'
  1039.         --   nactvar  -- number of active local variables
  1040.         --   upvalues[LUAI_MAXUPVALUES]  -- upvalues (table: upvaldesc)
  1041.         --   actvar[LUAI_MAXVARS]  -- declared-variable stack
  1042.         ----------------------------------------------------------------------]]
  1043.        
  1044.         ------------------------------------------------------------------------
  1045.         -- constants used by parser
  1046.         -- * picks up duplicate values from luaX if required
  1047.         ------------------------------------------------------------------------
  1048.        
  1049.         luaY.LUA_QS = luaX.LUA_QS or "'%s'"  -- (from luaconf.h)
  1050.        
  1051.         luaY.SHRT_MAX = 32767 -- (from <limits.h>)
  1052.         luaY.LUAI_MAXVARS = 200  -- (luaconf.h)
  1053.         luaY.LUAI_MAXUPVALUES = 60  -- (luaconf.h)
  1054.         luaY.MAX_INT = luaX.MAX_INT or 2147483645  -- (from llimits.h)
  1055.           -- * INT_MAX-2 for 32-bit systems
  1056.         luaY.LUAI_MAXCCALLS = 200  -- (from luaconf.h)
  1057.        
  1058.         luaY.VARARG_HASARG = 1  -- (from lobject.h)
  1059.         -- NOTE: HASARG_MASK is value-specific
  1060.         luaY.HASARG_MASK = 2 -- this was added for a bitop in parlist()
  1061.         luaY.VARARG_ISVARARG = 2
  1062.         -- NOTE: there is some value-specific code that involves VARARG_NEEDSARG
  1063.         luaY.VARARG_NEEDSARG = 4
  1064.        
  1065.         luaY.LUA_MULTRET = -1  -- (lua.h)
  1066.        
  1067.         --[[--------------------------------------------------------------------
  1068.         -- other functions
  1069.         ----------------------------------------------------------------------]]
  1070.        
  1071.         ------------------------------------------------------------------------
  1072.         -- LUA_QL describes how error messages quote program elements.
  1073.         -- CHANGE it if you want a different appearance. (from luaconf.h)
  1074.         ------------------------------------------------------------------------
  1075.         function luaY:LUA_QL(x)
  1076.           return "'"..x.."'"
  1077.         end
  1078.        
  1079.         ------------------------------------------------------------------------
  1080.         -- this is a stripped-down luaM_growvector (from lmem.h) which is a
  1081.         -- macro based on luaM_growaux (in lmem.c); all the following does is
  1082.         -- reproduce the size limit checking logic of the original function
  1083.         -- so that error behaviour is identical; all arguments preserved for
  1084.         -- convenience, even those which are unused
  1085.         -- * set the t field to nil, since this originally does a sizeof(t)
  1086.         -- * size (originally a pointer) is never updated, their final values
  1087.         --   are set by luaY:close_func(), so overall things should still work
  1088.         ------------------------------------------------------------------------
  1089.         function luaY:growvector(L, v, nelems, size, t, limit, e)
  1090.           if nelems >= limit then
  1091.             error(e)  -- was luaG_runerror
  1092.           end
  1093.         end
  1094.        
  1095.         ------------------------------------------------------------------------
  1096.         -- initialize a new function prototype structure (from lfunc.c)
  1097.         -- * used only in open_func()
  1098.         ------------------------------------------------------------------------
  1099.         function luaY:newproto(L)
  1100.           local f = {} -- Proto
  1101.           -- luaC_link(L, obj2gco(f), LUA_TPROTO); /* GC */
  1102.           f.k = {}
  1103.           f.sizek = 0
  1104.           f.p = {}
  1105.           f.sizep = 0
  1106.           f.code = {}
  1107.           f.sizecode = 0
  1108.           f.sizelineinfo = 0
  1109.           f.sizeupvalues = 0
  1110.           f.nups = 0
  1111.           f.upvalues = {}
  1112.           f.numparams = 0
  1113.           f.is_vararg = 0
  1114.           f.maxstacksize = 0
  1115.           f.lineinfo = {}
  1116.           f.sizelocvars = 0
  1117.           f.locvars = {}
  1118.           f.lineDefined = 0
  1119.           f.lastlinedefined = 0
  1120.           f.source = nil
  1121.           return f
  1122.         end
  1123.        
  1124.         ------------------------------------------------------------------------
  1125.         -- converts an integer to a "floating point byte", represented as
  1126.         -- (eeeeexxx), where the real value is (1xxx) * 2^(eeeee - 1) if
  1127.         -- eeeee != 0 and (xxx) otherwise.
  1128.         ------------------------------------------------------------------------
  1129.         function luaY:int2fb(x)
  1130.           local e = 0  -- exponent
  1131.           while x >= 16 do
  1132.             x = math.floor((x + 1) / 2)
  1133.             e = e + 1
  1134.           end
  1135.           if x < 8 then
  1136.             return x
  1137.           else
  1138.             return ((e + 1) * 8) + (x - 8)
  1139.           end
  1140.         end
  1141.        
  1142.         --[[--------------------------------------------------------------------
  1143.         -- parser functions
  1144.         ----------------------------------------------------------------------]]
  1145.        
  1146.         ------------------------------------------------------------------------
  1147.         -- true of the kind of expression produces multiple return values
  1148.         ------------------------------------------------------------------------
  1149.         function luaY:hasmultret(k)
  1150.           return k == "VCALL" or k == "VVARARG"
  1151.         end
  1152.        
  1153.         ------------------------------------------------------------------------
  1154.         -- convenience function to access active local i, returns entry
  1155.         ------------------------------------------------------------------------
  1156.         function luaY:getlocvar(fs, i)
  1157.           return fs.f.locvars[ fs.actvar[i] ]
  1158.         end
  1159.        
  1160.         ------------------------------------------------------------------------
  1161.         -- check a limit, string m provided as an error message
  1162.         ------------------------------------------------------------------------
  1163.         function luaY:checklimit(fs, v, l, m)
  1164.           if v > l then self:errorlimit(fs, l, m) end
  1165.         end
  1166.        
  1167.         --[[--------------------------------------------------------------------
  1168.         -- nodes for block list (list of active blocks)
  1169.         -- struct BlockCnt:
  1170.         --   previous  -- chain (table: BlockCnt)
  1171.         --   breaklist  -- list of jumps out of this loop
  1172.         --   nactvar  -- # active local variables outside the breakable structure
  1173.         --   upval  -- true if some variable in the block is an upvalue (boolean)
  1174.         --   isbreakable  -- true if 'block' is a loop (boolean)
  1175.         ----------------------------------------------------------------------]]
  1176.        
  1177.         ------------------------------------------------------------------------
  1178.         -- prototypes for recursive non-terminal functions
  1179.         ------------------------------------------------------------------------
  1180.         -- prototypes deleted; not required in Lua
  1181.        
  1182.         ------------------------------------------------------------------------
  1183.         -- reanchor if last token is has a constant string, see close_func()
  1184.         -- * used only in close_func()
  1185.         ------------------------------------------------------------------------
  1186.         function luaY:anchor_token(ls)
  1187.           if ls.t.token == "TK_NAME" or ls.t.token == "TK_STRING" then
  1188.             -- not relevant to Lua implementation of parser
  1189.             -- local ts = ls.t.seminfo
  1190.             -- luaX_newstring(ls, getstr(ts), ts->tsv.len); /* C */
  1191.           end
  1192.         end
  1193.        
  1194.         ------------------------------------------------------------------------
  1195.         -- throws a syntax error if token expected is not there
  1196.         ------------------------------------------------------------------------
  1197.         function luaY:error_expected(ls, token)
  1198.           luaX:syntaxerror(ls,
  1199.             string.format(self.LUA_QS.." expected", luaX:token2str(ls, token)))
  1200.         end
  1201.        
  1202.         ------------------------------------------------------------------------
  1203.         -- prepares error message for display, for limits exceeded
  1204.         -- * used only in checklimit()
  1205.         ------------------------------------------------------------------------
  1206.         function luaY:errorlimit(fs, limit, what)
  1207.           local msg = (fs.f.linedefined == 0) and
  1208.             string.format("main function has more than %d %s", limit, what) or
  1209.             string.format("function at line %d has more than %d %s",
  1210.                           fs.f.linedefined, limit, what)
  1211.           luaX:lexerror(fs.ls, msg, 0)
  1212.         end
  1213.        
  1214.         ------------------------------------------------------------------------
  1215.         -- tests for a token, returns outcome
  1216.         -- * return value changed to boolean
  1217.         ------------------------------------------------------------------------
  1218.         function luaY:testnext(ls, c)
  1219.           if ls.t.token == c then
  1220.             luaX:next(ls)
  1221.             return true
  1222.           else
  1223.             return false
  1224.           end
  1225.         end
  1226.        
  1227.         ------------------------------------------------------------------------
  1228.         -- check for existence of a token, throws error if not found
  1229.         ------------------------------------------------------------------------
  1230.         function luaY:check(ls, c)
  1231.           if ls.t.token ~= c then
  1232.             self:error_expected(ls, c)
  1233.           end
  1234.         end
  1235.        
  1236.         ------------------------------------------------------------------------
  1237.         -- verify existence of a token, then skip it
  1238.         ------------------------------------------------------------------------
  1239.         function luaY:checknext(ls, c)
  1240.           self:check(ls, c)
  1241.           luaX:next(ls)
  1242.         end
  1243.        
  1244.         ------------------------------------------------------------------------
  1245.         -- throws error if condition not matched
  1246.         ------------------------------------------------------------------------
  1247.         function luaY:check_condition(ls, c, msg)
  1248.           if not c then luaX:syntaxerror(ls, msg) end
  1249.         end
  1250.        
  1251.         ------------------------------------------------------------------------
  1252.         -- verifies token conditions are met or else throw error
  1253.         ------------------------------------------------------------------------
  1254.         function luaY:check_match(ls, what, who, where)
  1255.           if not self:testnext(ls, what) then
  1256.             if where == ls.linenumber then
  1257.               self:error_expected(ls, what)
  1258.             else
  1259.               luaX:syntaxerror(ls, string.format(
  1260.                 self.LUA_QS.." expected (to close "..self.LUA_QS.." at line %d)",
  1261.                 luaX:token2str(ls, what), luaX:token2str(ls, who), where))
  1262.             end
  1263.           end
  1264.         end
  1265.        
  1266.         ------------------------------------------------------------------------
  1267.         -- expect that token is a name, return the name
  1268.         ------------------------------------------------------------------------
  1269.         function luaY:str_checkname(ls)
  1270.           self:check(ls, "TK_NAME")
  1271.           local ts = ls.t.seminfo
  1272.           luaX:next(ls)
  1273.           return ts
  1274.         end
  1275.        
  1276.         ------------------------------------------------------------------------
  1277.         -- initialize a struct expdesc, expression description data structure
  1278.         ------------------------------------------------------------------------
  1279.         function luaY:init_exp(e, k, i)
  1280.           e.f, e.t = luaK.NO_JUMP, luaK.NO_JUMP
  1281.           e.k = k
  1282.           e.info = i
  1283.         end
  1284.        
  1285.         ------------------------------------------------------------------------
  1286.         -- adds given string s in string pool, sets e as VK
  1287.         ------------------------------------------------------------------------
  1288.         function luaY:codestring(ls, e, s)
  1289.           self:init_exp(e, "VK", luaK:stringK(ls.fs, s))
  1290.         end
  1291.        
  1292.         ------------------------------------------------------------------------
  1293.         -- consume a name token, adds it to string pool, sets e as VK
  1294.         ------------------------------------------------------------------------
  1295.         function luaY:checkname(ls, e)
  1296.           self:codestring(ls, e, self:str_checkname(ls))
  1297.         end
  1298.        
  1299.         ------------------------------------------------------------------------
  1300.         -- creates struct entry for a local variable
  1301.         -- * used only in new_localvar()
  1302.         ------------------------------------------------------------------------
  1303.         function luaY:registerlocalvar(ls, varname)
  1304.           local fs = ls.fs
  1305.           local f = fs.f
  1306.           self:growvector(ls.L, f.locvars, fs.nlocvars, f.sizelocvars,
  1307.                           nil, self.SHRT_MAX, "too many local variables")
  1308.           -- loop to initialize empty f.locvar positions not required
  1309.           f.locvars[fs.nlocvars] = {} -- LocVar
  1310.           f.locvars[fs.nlocvars].varname = varname
  1311.           -- luaC_objbarrier(ls.L, f, varname) /* GC */
  1312.           local nlocvars = fs.nlocvars
  1313.           fs.nlocvars = fs.nlocvars + 1
  1314.           return nlocvars
  1315.         end
  1316.        
  1317.         ------------------------------------------------------------------------
  1318.         -- creates a new local variable given a name and an offset from nactvar
  1319.         -- * used in fornum(), forlist(), parlist(), body()
  1320.         ------------------------------------------------------------------------
  1321.         function luaY:new_localvarliteral(ls, v, n)
  1322.           self:new_localvar(ls, v, n)
  1323.         end
  1324.        
  1325.         ------------------------------------------------------------------------
  1326.         -- register a local variable, set in active variable list
  1327.         ------------------------------------------------------------------------
  1328.         function luaY:new_localvar(ls, name, n)
  1329.           local fs = ls.fs
  1330.           self:checklimit(fs, fs.nactvar + n + 1, self.LUAI_MAXVARS, "local variables")
  1331.           fs.actvar[fs.nactvar + n] = self:registerlocalvar(ls, name)
  1332.         end
  1333.        
  1334.         ------------------------------------------------------------------------
  1335.         -- adds nvars number of new local variables, set debug information
  1336.         ------------------------------------------------------------------------
  1337.         function luaY:adjustlocalvars(ls, nvars)
  1338.           local fs = ls.fs
  1339.           fs.nactvar = fs.nactvar + nvars
  1340.           for i = nvars, 1, -1 do
  1341.             self:getlocvar(fs, fs.nactvar - i).startpc = fs.pc
  1342.           end
  1343.         end
  1344.        
  1345.         ------------------------------------------------------------------------
  1346.         -- removes a number of locals, set debug information
  1347.         ------------------------------------------------------------------------
  1348.         function luaY:removevars(ls, tolevel)
  1349.           local fs = ls.fs
  1350.           while fs.nactvar > tolevel do
  1351.             fs.nactvar = fs.nactvar - 1
  1352.             self:getlocvar(fs, fs.nactvar).endpc = fs.pc
  1353.           end
  1354.         end
  1355.        
  1356.         ------------------------------------------------------------------------
  1357.         -- returns an existing upvalue index based on the given name, or
  1358.         -- creates a new upvalue struct entry and returns the new index
  1359.         -- * used only in singlevaraux()
  1360.         ------------------------------------------------------------------------
  1361.         function luaY:indexupvalue(fs, name, v)
  1362.           local f = fs.f
  1363.           for i = 0, f.nups - 1 do
  1364.             if fs.upvalues[i].k == v.k and fs.upvalues[i].info == v.info then
  1365.               assert(f.upvalues[i] == name)
  1366.               return i
  1367.             end
  1368.           end
  1369.           -- new one
  1370.           self:checklimit(fs, f.nups + 1, self.LUAI_MAXUPVALUES, "upvalues")
  1371.           self:growvector(fs.L, f.upvalues, f.nups, f.sizeupvalues,
  1372.                           nil, self.MAX_INT, "")
  1373.           -- loop to initialize empty f.upvalues positions not required
  1374.           f.upvalues[f.nups] = name
  1375.           -- luaC_objbarrier(fs->L, f, name); /* GC */
  1376.           assert(v.k == "VLOCAL" or v.k == "VUPVAL")
  1377.           -- this is a partial copy; only k & info fields used
  1378.           fs.upvalues[f.nups] = { k = v.k, info = v.info }
  1379.           local nups = f.nups
  1380.           f.nups = f.nups + 1
  1381.           return nups
  1382.         end
  1383.        
  1384.         ------------------------------------------------------------------------
  1385.         -- search the local variable namespace of the given fs for a match
  1386.         -- * used only in singlevaraux()
  1387.         ------------------------------------------------------------------------
  1388.         function luaY:searchvar(fs, n)
  1389.           for i = fs.nactvar - 1, 0, -1 do
  1390.             if n == self:getlocvar(fs, i).varname then
  1391.               return i
  1392.             end
  1393.           end
  1394.           return -1  -- not found
  1395.         end
  1396.        
  1397.         ------------------------------------------------------------------------
  1398.         -- * mark upvalue flags in function states up to a given level
  1399.         -- * used only in singlevaraux()
  1400.         ------------------------------------------------------------------------
  1401.         function luaY:markupval(fs, level)
  1402.           local bl = fs.bl
  1403.           while bl and bl.nactvar > level do bl = bl.previous end
  1404.           if bl then bl.upval = true end
  1405.         end
  1406.        
  1407.         ------------------------------------------------------------------------
  1408.         -- handle locals, globals and upvalues and related processing
  1409.         -- * search mechanism is recursive, calls itself to search parents
  1410.         -- * used only in singlevar()
  1411.         ------------------------------------------------------------------------
  1412.         function luaY:singlevaraux(fs, n, var, base)
  1413.           if fs == nil then  -- no more levels?
  1414.             self:init_exp(var, "VGLOBAL", luaP.NO_REG)  -- default is global variable
  1415.             return "VGLOBAL"
  1416.           else
  1417.             local v = self:searchvar(fs, n)  -- look up at current level
  1418.             if v >= 0 then
  1419.               self:init_exp(var, "VLOCAL", v)
  1420.               if base == 0 then
  1421.                 self:markupval(fs, v)  -- local will be used as an upval
  1422.               end
  1423.               return "VLOCAL"
  1424.             else  -- not found at current level; try upper one
  1425.               if self:singlevaraux(fs.prev, n, var, 0) == "VGLOBAL" then
  1426.                 return "VGLOBAL"
  1427.               end
  1428.               var.info = self:indexupvalue(fs, n, var)  -- else was LOCAL or UPVAL
  1429.               var.k = "VUPVAL"  -- upvalue in this level
  1430.               return "VUPVAL"
  1431.             end--if v
  1432.           end--if fs
  1433.         end
  1434.        
  1435.         ------------------------------------------------------------------------
  1436.         -- consume a name token, creates a variable (global|local|upvalue)
  1437.         -- * used in prefixexp(), funcname()
  1438.         ------------------------------------------------------------------------
  1439.         function luaY:singlevar(ls, var)
  1440.           local varname = self:str_checkname(ls)
  1441.           local fs = ls.fs
  1442.           if self:singlevaraux(fs, varname, var, 1) == "VGLOBAL" then
  1443.             var.info = luaK:stringK(fs, varname)  -- info points to global name
  1444.           end
  1445.         end
  1446.        
  1447.         ------------------------------------------------------------------------
  1448.         -- adjust RHS to match LHS in an assignment
  1449.         -- * used in assignment(), forlist(), localstat()
  1450.         ------------------------------------------------------------------------
  1451.         function luaY:adjust_assign(ls, nvars, nexps, e)
  1452.           local fs = ls.fs
  1453.           local extra = nvars - nexps
  1454.           if self:hasmultret(e.k) then
  1455.             extra = extra + 1  -- includes call itself
  1456.             if extra <= 0 then extra = 0 end
  1457.             luaK:setreturns(fs, e, extra)  -- last exp. provides the difference
  1458.             if extra > 1 then luaK:reserveregs(fs, extra - 1) end
  1459.           else
  1460.             if e.k ~= "VVOID" then luaK:exp2nextreg(fs, e) end  -- close last expression
  1461.             if extra > 0 then
  1462.               local reg = fs.freereg
  1463.               luaK:reserveregs(fs, extra)
  1464.               luaK:_nil(fs, reg, extra)
  1465.             end
  1466.           end
  1467.         end
  1468.        
  1469.         ------------------------------------------------------------------------
  1470.         -- tracks and limits parsing depth, assert check at end of parsing
  1471.         ------------------------------------------------------------------------
  1472.         function luaY:enterlevel(ls)
  1473.           ls.L.nCcalls = ls.L.nCcalls + 1
  1474.           if ls.L.nCcalls > self.LUAI_MAXCCALLS then
  1475.             luaX:lexerror(ls, "chunk has too many syntax levels", 0)
  1476.           end
  1477.         end
  1478.        
  1479.         ------------------------------------------------------------------------
  1480.         -- tracks parsing depth, a pair with luaY:enterlevel()
  1481.         ------------------------------------------------------------------------
  1482.         function luaY:leavelevel(ls)
  1483.           ls.L.nCcalls = ls.L.nCcalls - 1
  1484.         end
  1485.        
  1486.         ------------------------------------------------------------------------
  1487.         -- enters a code unit, initializes elements
  1488.         ------------------------------------------------------------------------
  1489.         function luaY:enterblock(fs, bl, isbreakable)
  1490.           bl.breaklist = luaK.NO_JUMP
  1491.           bl.isbreakable = isbreakable
  1492.           bl.nactvar = fs.nactvar
  1493.           bl.upval = false
  1494.           bl.previous = fs.bl
  1495.           fs.bl = bl
  1496.           assert(fs.freereg == fs.nactvar)
  1497.         end
  1498.        
  1499.         ------------------------------------------------------------------------
  1500.         -- leaves a code unit, close any upvalues
  1501.         ------------------------------------------------------------------------
  1502.         function luaY:leaveblock(fs)
  1503.           local bl = fs.bl
  1504.           fs.bl = bl.previous
  1505.           self:removevars(fs.ls, bl.nactvar)
  1506.           if bl.upval then
  1507.             luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  1508.           end
  1509.           -- a block either controls scope or breaks (never both)
  1510.           assert(not bl.isbreakable or not bl.upval)
  1511.           assert(bl.nactvar == fs.nactvar)
  1512.           fs.freereg = fs.nactvar  -- free registers
  1513.           luaK:patchtohere(fs, bl.breaklist)
  1514.         end
  1515.        
  1516.         ------------------------------------------------------------------------
  1517.         -- implement the instantiation of a function prototype, append list of
  1518.         -- upvalues after the instantiation instruction
  1519.         -- * used only in body()
  1520.         ------------------------------------------------------------------------
  1521.         function luaY:pushclosure(ls, func, v)
  1522.           local fs = ls.fs
  1523.           local f = fs.f
  1524.           self:growvector(ls.L, f.p, fs.np, f.sizep, nil,
  1525.                           luaP.MAXARG_Bx, "constant table overflow")
  1526.           -- loop to initialize empty f.p positions not required
  1527.           f.p[fs.np] = func.f
  1528.           fs.np = fs.np + 1
  1529.           -- luaC_objbarrier(ls->L, f, func->f); /* C */
  1530.           self:init_exp(v, "VRELOCABLE", luaK:codeABx(fs, "OP_CLOSURE", 0, fs.np - 1))
  1531.           for i = 0, func.f.nups - 1 do
  1532.             local o = (func.upvalues[i].k == "VLOCAL") and "OP_MOVE" or "OP_GETUPVAL"
  1533.             luaK:codeABC(fs, o, 0, func.upvalues[i].info, 0)
  1534.           end
  1535.         end
  1536.        
  1537.         ------------------------------------------------------------------------
  1538.         -- opening of a function
  1539.         ------------------------------------------------------------------------
  1540.         function luaY:open_func(ls, fs)
  1541.           local L = ls.L
  1542.           local f = self:newproto(ls.L)
  1543.           fs.f = f
  1544.           fs.prev = ls.fs  -- linked list of funcstates
  1545.           fs.ls = ls
  1546.           fs.L = L
  1547.           ls.fs = fs
  1548.           fs.pc = 0
  1549.           fs.lasttarget = -1
  1550.           fs.jpc = luaK.NO_JUMP
  1551.           fs.freereg = 0
  1552.           fs.nk = 0
  1553.           fs.np = 0
  1554.           fs.nlocvars = 0
  1555.           fs.nactvar = 0
  1556.           fs.bl = nil
  1557.           f.source = ls.source
  1558.           f.maxstacksize = 2  -- registers 0/1 are always valid
  1559.           fs.h = {}  -- constant table; was luaH_new call
  1560.           -- anchor table of constants and prototype (to avoid being collected)
  1561.           -- sethvalue2s(L, L->top, fs->h); incr_top(L); /* C */
  1562.           -- setptvalue2s(L, L->top, f); incr_top(L);
  1563.         end
  1564.        
  1565.         ------------------------------------------------------------------------
  1566.         -- closing of a function
  1567.         ------------------------------------------------------------------------
  1568.         function luaY:close_func(ls)
  1569.           local L = ls.L
  1570.           local fs = ls.fs
  1571.           local f = fs.f
  1572.           self:removevars(ls, 0)
  1573.           luaK:ret(fs, 0, 0)  -- final return
  1574.           -- luaM_reallocvector deleted for f->code, f->lineinfo, f->k, f->p,
  1575.           -- f->locvars, f->upvalues; not required for Lua table arrays
  1576.           f.sizecode = fs.pc
  1577.           f.sizelineinfo = fs.pc
  1578.           f.sizek = fs.nk
  1579.           f.sizep = fs.np
  1580.           f.sizelocvars = fs.nlocvars
  1581.           f.sizeupvalues = f.nups
  1582.           --assert(luaG_checkcode(f))  -- currently not implemented
  1583.           assert(fs.bl == nil)
  1584.           ls.fs = fs.prev
  1585.           -- the following is not required for this implementation; kept here
  1586.           -- for completeness
  1587.           -- L->top -= 2;  /* remove table and prototype from the stack */
  1588.           -- last token read was anchored in defunct function; must reanchor it
  1589.           if fs then self:anchor_token(ls) end
  1590.         end
  1591.        
  1592.         ------------------------------------------------------------------------
  1593.         -- parser initialization function
  1594.         -- * note additional sub-tables needed for LexState, FuncState
  1595.         ------------------------------------------------------------------------
  1596.         function luaY:parser(L, z, buff, name)
  1597.           local lexstate = {}  -- LexState
  1598.                 lexstate.t = {}
  1599.                 lexstate.lookahead = {}
  1600.           local funcstate = {}  -- FuncState
  1601.                 funcstate.upvalues = {}
  1602.                 funcstate.actvar = {}
  1603.           -- the following nCcalls initialization added for convenience
  1604.           L.nCcalls = 0
  1605.           lexstate.buff = buff
  1606.           luaX:setinput(L, lexstate, z, name)
  1607.           self:open_func(lexstate, funcstate)
  1608.           funcstate.f.is_vararg = self.VARARG_ISVARARG  -- main func. is always vararg
  1609.           luaX:next(lexstate)  -- read first token
  1610.           self:chunk(lexstate)
  1611.           self:check(lexstate, "TK_EOS")
  1612.           self:close_func(lexstate)
  1613.           assert(funcstate.prev == nil)
  1614.           assert(funcstate.f.nups == 0)
  1615.           assert(lexstate.fs == nil)
  1616.           return funcstate.f
  1617.         end
  1618.        
  1619.         --[[--------------------------------------------------------------------
  1620.         -- GRAMMAR RULES
  1621.         ----------------------------------------------------------------------]]
  1622.        
  1623.         ------------------------------------------------------------------------
  1624.         -- parse a function name suffix, for function call specifications
  1625.         -- * used in primaryexp(), funcname()
  1626.         ------------------------------------------------------------------------
  1627.         function luaY:field(ls, v)
  1628.           -- field -> ['.' | ':'] NAME
  1629.           local fs = ls.fs
  1630.           local key = {}  -- expdesc
  1631.           luaK:exp2anyreg(fs, v)
  1632.           luaX:next(ls)  -- skip the dot or colon
  1633.           self:checkname(ls, key)
  1634.           luaK:indexed(fs, v, key)
  1635.         end
  1636.        
  1637.         ------------------------------------------------------------------------
  1638.         -- parse a table indexing suffix, for constructors, expressions
  1639.         -- * used in recfield(), primaryexp()
  1640.         ------------------------------------------------------------------------
  1641.         function luaY:yindex(ls, v)
  1642.           -- index -> '[' expr ']'
  1643.           luaX:next(ls)  -- skip the '['
  1644.           self:expr(ls, v)
  1645.           luaK:exp2val(ls.fs, v)
  1646.           self:checknext(ls, "]")
  1647.         end
  1648.        
  1649.         --[[--------------------------------------------------------------------
  1650.         -- Rules for Constructors
  1651.         ----------------------------------------------------------------------]]
  1652.        
  1653.         --[[--------------------------------------------------------------------
  1654.         -- struct ConsControl:
  1655.         --   v  -- last list item read (table: struct expdesc)
  1656.         --   t  -- table descriptor (table: struct expdesc)
  1657.         --   nh  -- total number of 'record' elements
  1658.         --   na  -- total number of array elements
  1659.         --   tostore  -- number of array elements pending to be stored
  1660.         ----------------------------------------------------------------------]]
  1661.        
  1662.         ------------------------------------------------------------------------
  1663.         -- parse a table record (hash) field
  1664.         -- * used in constructor()
  1665.         ------------------------------------------------------------------------
  1666.         function luaY:recfield(ls, cc)
  1667.           -- recfield -> (NAME | '['exp1']') = exp1
  1668.           local fs = ls.fs
  1669.           local reg = ls.fs.freereg
  1670.           local key, val = {}, {}  -- expdesc
  1671.           if ls.t.token == "TK_NAME" then
  1672.             self:checklimit(fs, cc.nh, self.MAX_INT, "items in a constructor")
  1673.             self:checkname(ls, key)
  1674.           else  -- ls->t.token == '['
  1675.             self:yindex(ls, key)
  1676.           end
  1677.           cc.nh = cc.nh + 1
  1678.           self:checknext(ls, "=")
  1679.           local rkkey = luaK:exp2RK(fs, key)
  1680.           self:expr(ls, val)
  1681.           luaK:codeABC(fs, "OP_SETTABLE", cc.t.info, rkkey, luaK:exp2RK(fs, val))
  1682.           fs.freereg = reg  -- free registers
  1683.         end
  1684.        
  1685.         ------------------------------------------------------------------------
  1686.         -- emit a set list instruction if enough elements (LFIELDS_PER_FLUSH)
  1687.         -- * used in constructor()
  1688.         ------------------------------------------------------------------------
  1689.         function luaY:closelistfield(fs, cc)
  1690.           if cc.v.k == "VVOID" then return end  -- there is no list item
  1691.           luaK:exp2nextreg(fs, cc.v)
  1692.           cc.v.k = "VVOID"
  1693.           if cc.tostore == luaP.LFIELDS_PER_FLUSH then
  1694.             luaK:setlist(fs, cc.t.info, cc.na, cc.tostore)  -- flush
  1695.             cc.tostore = 0  -- no more items pending
  1696.           end
  1697.         end
  1698.        
  1699.         ------------------------------------------------------------------------
  1700.         -- emit a set list instruction at the end of parsing list constructor
  1701.         -- * used in constructor()
  1702.         ------------------------------------------------------------------------
  1703.         function luaY:lastlistfield(fs, cc)
  1704.           if cc.tostore == 0 then return end
  1705.           if self:hasmultret(cc.v.k) then
  1706.             luaK:setmultret(fs, cc.v)
  1707.             luaK:setlist(fs, cc.t.info, cc.na, self.LUA_MULTRET)
  1708.             cc.na = cc.na - 1  -- do not count last expression (unknown number of elements)
  1709.           else
  1710.             if cc.v.k ~= "VVOID" then
  1711.               luaK:exp2nextreg(fs, cc.v)
  1712.             end
  1713.             luaK:setlist(fs, cc.t.info, cc.na, cc.tostore)
  1714.           end
  1715.         end
  1716.        
  1717.         ------------------------------------------------------------------------
  1718.         -- parse a table list (array) field
  1719.         -- * used in constructor()
  1720.         ------------------------------------------------------------------------
  1721.         function luaY:listfield(ls, cc)
  1722.           self:expr(ls, cc.v)
  1723.           self:checklimit(ls.fs, cc.na, self.MAX_INT, "items in a constructor")
  1724.           cc.na = cc.na + 1
  1725.           cc.tostore = cc.tostore + 1
  1726.         end
  1727.        
  1728.         ------------------------------------------------------------------------
  1729.         -- parse a table constructor
  1730.         -- * used in funcargs(), simpleexp()
  1731.         ------------------------------------------------------------------------
  1732.         function luaY:constructor(ls, t)
  1733.           -- constructor -> '{' [ field { fieldsep field } [ fieldsep ] ] '}'
  1734.           -- field -> recfield | listfield
  1735.           -- fieldsep -> ',' | ';'
  1736.           local fs = ls.fs
  1737.           local line = ls.linenumber
  1738.           local pc = luaK:codeABC(fs, "OP_NEWTABLE", 0, 0, 0)
  1739.           local cc = {}  -- ConsControl
  1740.                 cc.v = {}
  1741.           cc.na, cc.nh, cc.tostore = 0, 0, 0
  1742.           cc.t = t
  1743.           self:init_exp(t, "VRELOCABLE", pc)
  1744.           self:init_exp(cc.v, "VVOID", 0)  -- no value (yet)
  1745.           luaK:exp2nextreg(ls.fs, t)  -- fix it at stack top (for gc)
  1746.           self:checknext(ls, "{")
  1747.           repeat
  1748.             assert(cc.v.k == "VVOID" or cc.tostore > 0)
  1749.             if ls.t.token == "}" then break end
  1750.             self:closelistfield(fs, cc)
  1751.             local c = ls.t.token
  1752.        
  1753.             if c == "TK_NAME" then  -- may be listfields or recfields
  1754.               luaX:lookahead(ls)
  1755.               if ls.lookahead.token ~= "=" then  -- expression?
  1756.                 self:listfield(ls, cc)
  1757.               else
  1758.                 self:recfield(ls, cc)
  1759.               end
  1760.             elseif c == "[" then  -- constructor_item -> recfield
  1761.               self:recfield(ls, cc)
  1762.             else  -- constructor_part -> listfield
  1763.               self:listfield(ls, cc)
  1764.             end
  1765.           until not self:testnext(ls, ",") and not self:testnext(ls, ";")
  1766.           self:check_match(ls, "}", "{", line)
  1767.           self:lastlistfield(fs, cc)
  1768.           luaP:SETARG_B(fs.f.code[pc], self:int2fb(cc.na)) -- set initial array size
  1769.           luaP:SETARG_C(fs.f.code[pc], self:int2fb(cc.nh)) -- set initial table size
  1770.         end
  1771.        
  1772.         -- }======================================================================
  1773.        
  1774.         ------------------------------------------------------------------------
  1775.         -- parse the arguments (parameters) of a function declaration
  1776.         -- * used in body()
  1777.         ------------------------------------------------------------------------
  1778.         function luaY:parlist(ls)
  1779.           -- parlist -> [ param { ',' param } ]
  1780.           local fs = ls.fs
  1781.           local f = fs.f
  1782.           local nparams = 0
  1783.           f.is_vararg = 0
  1784.           if ls.t.token ~= ")" then  -- is 'parlist' not empty?
  1785.             repeat
  1786.               local c = ls.t.token
  1787.               if c == "TK_NAME" then  -- param -> NAME
  1788.                 self:new_localvar(ls, self:str_checkname(ls), nparams)
  1789.                 nparams = nparams + 1
  1790.               elseif c == "TK_DOTS" then  -- param -> `...'
  1791.                 luaX:next(ls)
  1792.         -- [[
  1793.         -- #if defined(LUA_COMPAT_VARARG)
  1794.                 -- use `arg' as default name
  1795.                 self:new_localvarliteral(ls, "arg", nparams)
  1796.                 nparams = nparams + 1
  1797.                 f.is_vararg = self.VARARG_HASARG + self.VARARG_NEEDSARG
  1798.         -- #endif
  1799.         --]]
  1800.                 f.is_vararg = f.is_vararg + self.VARARG_ISVARARG
  1801.               else
  1802.                 luaX:syntaxerror(ls, "<name> or "..self:LUA_QL("...").." expected")
  1803.               end
  1804.             until f.is_vararg ~= 0 or not self:testnext(ls, ",")
  1805.           end--if
  1806.           self:adjustlocalvars(ls, nparams)
  1807.           -- NOTE: the following works only when HASARG_MASK is 2!
  1808.           f.numparams = fs.nactvar - (f.is_vararg % self.HASARG_MASK)
  1809.           luaK:reserveregs(fs, fs.nactvar)  -- reserve register for parameters
  1810.         end
  1811.        
  1812.         ------------------------------------------------------------------------
  1813.         -- parse function declaration body
  1814.         -- * used in simpleexp(), localfunc(), funcstat()
  1815.         ------------------------------------------------------------------------
  1816.         function luaY:body(ls, e, needself, line)
  1817.           -- body ->  '(' parlist ')' chunk END
  1818.           local new_fs = {}  -- FuncState
  1819.                 new_fs.upvalues = {}
  1820.                 new_fs.actvar = {}
  1821.           self:open_func(ls, new_fs)
  1822.           new_fs.f.lineDefined = line
  1823.           self:checknext(ls, "(")
  1824.           if needself then
  1825.             self:new_localvarliteral(ls, "self", 0)
  1826.             self:adjustlocalvars(ls, 1)
  1827.           end
  1828.           self:parlist(ls)
  1829.           self:checknext(ls, ")")
  1830.           self:chunk(ls)
  1831.           new_fs.f.lastlinedefined = ls.linenumber
  1832.           self:check_match(ls, "TK_END", "TK_FUNCTION", line)
  1833.           self:close_func(ls)
  1834.           self:pushclosure(ls, new_fs, e)
  1835.         end
  1836.        
  1837.         ------------------------------------------------------------------------
  1838.         -- parse a list of comma-separated expressions
  1839.         -- * used is multiple locations
  1840.         ------------------------------------------------------------------------
  1841.         function luaY:explist1(ls, v)
  1842.           -- explist1 -> expr { ',' expr }
  1843.           local n = 1  -- at least one expression
  1844.           self:expr(ls, v)
  1845.           while self:testnext(ls, ",") do
  1846.             luaK:exp2nextreg(ls.fs, v)
  1847.             self:expr(ls, v)
  1848.             n = n + 1
  1849.           end
  1850.           return n
  1851.         end
  1852.        
  1853.         ------------------------------------------------------------------------
  1854.         -- parse the parameters of a function call
  1855.         -- * contrast with parlist(), used in function declarations
  1856.         -- * used in primaryexp()
  1857.         ------------------------------------------------------------------------
  1858.         function luaY:funcargs(ls, f)
  1859.           local fs = ls.fs
  1860.           local args = {}  -- expdesc
  1861.           local nparams
  1862.           local line = ls.linenumber
  1863.           local c = ls.t.token
  1864.           if c == "(" then  -- funcargs -> '(' [ explist1 ] ')'
  1865.             if line ~= ls.lastline then
  1866.               luaX:syntaxerror(ls, "ambiguous syntax (function call x new statement)")
  1867.             end
  1868.             luaX:next(ls)
  1869.             if ls.t.token == ")" then  -- arg list is empty?
  1870.               args.k = "VVOID"
  1871.             else
  1872.               self:explist1(ls, args)
  1873.               luaK:setmultret(fs, args)
  1874.             end
  1875.             self:check_match(ls, ")", "(", line)
  1876.           elseif c == "{" then  -- funcargs -> constructor
  1877.             self:constructor(ls, args)
  1878.           elseif c == "TK_STRING" then  -- funcargs -> STRING
  1879.             self:codestring(ls, args, ls.t.seminfo)
  1880.             luaX:next(ls)  -- must use 'seminfo' before 'next'
  1881.           else
  1882.             luaX:syntaxerror(ls, "function arguments expected")
  1883.             return
  1884.           end
  1885.           assert(f.k == "VNONRELOC")
  1886.           local base = f.info  -- base register for call
  1887.           if self:hasmultret(args.k) then
  1888.             nparams = self.LUA_MULTRET  -- open call
  1889.           else
  1890.             if args.k ~= "VVOID" then
  1891.               luaK:exp2nextreg(fs, args)  -- close last argument
  1892.             end
  1893.             nparams = fs.freereg - (base + 1)
  1894.           end
  1895.           self:init_exp(f, "VCALL", luaK:codeABC(fs, "OP_CALL", base, nparams + 1, 2))
  1896.           luaK:fixline(fs, line)
  1897.           fs.freereg = base + 1  -- call remove function and arguments and leaves
  1898.                                  -- (unless changed) one result
  1899.         end
  1900.        
  1901.         --[[--------------------------------------------------------------------
  1902.         -- Expression parsing
  1903.         ----------------------------------------------------------------------]]
  1904.        
  1905.         ------------------------------------------------------------------------
  1906.         -- parses an expression in parentheses or a single variable
  1907.         -- * used in primaryexp()
  1908.         ------------------------------------------------------------------------
  1909.         function luaY:prefixexp(ls, v)
  1910.           -- prefixexp -> NAME | '(' expr ')'
  1911.           local c = ls.t.token
  1912.           if c == "(" then
  1913.             local line = ls.linenumber
  1914.             luaX:next(ls)
  1915.             self:expr(ls, v)
  1916.             self:check_match(ls, ")", "(", line)
  1917.             luaK:dischargevars(ls.fs, v)
  1918.           elseif c == "TK_NAME" then
  1919.             self:singlevar(ls, v)
  1920.           else
  1921.             luaX:syntaxerror(ls, "unexpected symbol")
  1922.           end--if c
  1923.           return
  1924.         end
  1925.        
  1926.         ------------------------------------------------------------------------
  1927.         -- parses a prefixexp (an expression in parentheses or a single variable)
  1928.         -- or a function call specification
  1929.         -- * used in simpleexp(), assignment(), exprstat()
  1930.         ------------------------------------------------------------------------
  1931.         function luaY:primaryexp(ls, v)
  1932.           -- primaryexp ->
  1933.           --    prefixexp { '.' NAME | '[' exp ']' | ':' NAME funcargs | funcargs }
  1934.           local fs = ls.fs
  1935.           self:prefixexp(ls, v)
  1936.           while true do
  1937.             local c = ls.t.token
  1938.             if c == "." then  -- field
  1939.               self:field(ls, v)
  1940.             elseif c == "[" then  -- '[' exp1 ']'
  1941.               local key = {}  -- expdesc
  1942.               luaK:exp2anyreg(fs, v)
  1943.               self:yindex(ls, key)
  1944.               luaK:indexed(fs, v, key)
  1945.             elseif c == ":" then  -- ':' NAME funcargs
  1946.               local key = {}  -- expdesc
  1947.               luaX:next(ls)
  1948.               self:checkname(ls, key)
  1949.               luaK:_self(fs, v, key)
  1950.               self:funcargs(ls, v)
  1951.             elseif c == "(" or c == "TK_STRING" or c == "{" then  -- funcargs
  1952.               luaK:exp2nextreg(fs, v)
  1953.               self:funcargs(ls, v)
  1954.             else
  1955.               return
  1956.             end--if c
  1957.           end--while
  1958.         end
  1959.        
  1960.         ------------------------------------------------------------------------
  1961.         -- parses general expression types, constants handled here
  1962.         -- * used in subexpr()
  1963.         ------------------------------------------------------------------------
  1964.         function luaY:simpleexp(ls, v)
  1965.           -- simpleexp -> NUMBER | STRING | NIL | TRUE | FALSE | ... |
  1966.           --              constructor | FUNCTION body | primaryexp
  1967.           local c = ls.t.token
  1968.           if c == "TK_NUMBER" then
  1969.             self:init_exp(v, "VKNUM", 0)
  1970.             v.nval = ls.t.seminfo
  1971.           elseif c == "TK_STRING" then
  1972.             self:codestring(ls, v, ls.t.seminfo)
  1973.           elseif c == "TK_NIL" then
  1974.             self:init_exp(v, "VNIL", 0)
  1975.           elseif c == "TK_TRUE" then
  1976.             self:init_exp(v, "VTRUE", 0)
  1977.           elseif c == "TK_FALSE" then
  1978.             self:init_exp(v, "VFALSE", 0)
  1979.           elseif c == "TK_DOTS" then  -- vararg
  1980.             local fs = ls.fs
  1981.             self:check_condition(ls, fs.f.is_vararg ~= 0,
  1982.                             "cannot use "..self:LUA_QL("...").." outside a vararg function");
  1983.             -- NOTE: the following substitutes for a bitop, but is value-specific
  1984.             local is_vararg = fs.f.is_vararg
  1985.             if is_vararg >= self.VARARG_NEEDSARG then
  1986.               fs.f.is_vararg = is_vararg - self.VARARG_NEEDSARG  -- don't need 'arg'
  1987.             end
  1988.             self:init_exp(v, "VVARARG", luaK:codeABC(fs, "OP_VARARG", 0, 1, 0))
  1989.           elseif c == "{" then  -- constructor
  1990.             self:constructor(ls, v)
  1991.             return
  1992.           elseif c == "TK_FUNCTION" then
  1993.             luaX:next(ls)
  1994.             self:body(ls, v, false, ls.linenumber)
  1995.             return
  1996.           else
  1997.             self:primaryexp(ls, v)
  1998.             return
  1999.           end--if c
  2000.           luaX:next(ls)
  2001.         end
  2002.        
  2003.         ------------------------------------------------------------------------
  2004.         -- Translates unary operators tokens if found, otherwise returns
  2005.         -- OPR_NOUNOPR. getunopr() and getbinopr() are used in subexpr().
  2006.         -- * used in subexpr()
  2007.         ------------------------------------------------------------------------
  2008.         function luaY:getunopr(op)
  2009.           if op == "TK_NOT" then
  2010.             return "OPR_NOT"
  2011.           elseif op == "-" then
  2012.             return "OPR_MINUS"
  2013.           elseif op == "#" then
  2014.             return "OPR_LEN"
  2015.           else
  2016.             return "OPR_NOUNOPR"
  2017.           end
  2018.         end
  2019.        
  2020.         ------------------------------------------------------------------------
  2021.         -- Translates binary operator tokens if found, otherwise returns
  2022.         -- OPR_NOBINOPR. Code generation uses OPR_* style tokens.
  2023.         -- * used in subexpr()
  2024.         ------------------------------------------------------------------------
  2025.         luaY.getbinopr_table = {
  2026.           ["+"] = "OPR_ADD",
  2027.           ["-"] = "OPR_SUB",
  2028.           ["*"] = "OPR_MUL",
  2029.           ["/"] = "OPR_DIV",
  2030.           ["%"] = "OPR_MOD",
  2031.           ["^"] = "OPR_POW",
  2032.           ["TK_CONCAT"] = "OPR_CONCAT",
  2033.           ["TK_NE"] = "OPR_NE",
  2034.           ["TK_EQ"] = "OPR_EQ",
  2035.           ["<"] = "OPR_LT",
  2036.           ["TK_LE"] = "OPR_LE",
  2037.           [">"] = "OPR_GT",
  2038.           ["TK_GE"] = "OPR_GE",
  2039.           ["TK_AND"] = "OPR_AND",
  2040.           ["TK_OR"] = "OPR_OR",
  2041.         }
  2042.         function luaY:getbinopr(op)
  2043.           local opr = self.getbinopr_table[op]
  2044.           if opr then return opr else return "OPR_NOBINOPR" end
  2045.         end
  2046.        
  2047.         ------------------------------------------------------------------------
  2048.         -- the following priority table consists of pairs of left/right values
  2049.         -- for binary operators (was a static const struct); grep for ORDER OPR
  2050.         -- * the following struct is replaced:
  2051.         --   static const struct {
  2052.         --     lu_byte left;  /* left priority for each binary operator */
  2053.         --     lu_byte right; /* right priority */
  2054.         --   } priority[] = {  /* ORDER OPR */
  2055.         ------------------------------------------------------------------------
  2056.         luaY.priority = {
  2057.           {6, 6}, {6, 6}, {7, 7}, {7, 7}, {7, 7}, -- `+' `-' `/' `%'
  2058.           {10, 9}, {5, 4},                 -- power and concat (right associative)
  2059.           {3, 3}, {3, 3},                  -- equality
  2060.           {3, 3}, {3, 3}, {3, 3}, {3, 3},  -- order
  2061.           {2, 2}, {1, 1}                   -- logical (and/or)
  2062.         }
  2063.        
  2064.         luaY.UNARY_PRIORITY = 8  -- priority for unary operators
  2065.        
  2066.         ------------------------------------------------------------------------
  2067.         -- Parse subexpressions. Includes handling of unary operators and binary
  2068.         -- operators. A subexpr is given the rhs priority level of the operator
  2069.         -- immediately left of it, if any (limit is -1 if none,) and if a binop
  2070.         -- is found, limit is compared with the lhs priority level of the binop
  2071.         -- in order to determine which executes first.
  2072.         ------------------------------------------------------------------------
  2073.        
  2074.         ------------------------------------------------------------------------
  2075.         -- subexpr -> (simpleexp | unop subexpr) { binop subexpr }
  2076.         -- where 'binop' is any binary operator with a priority higher than 'limit'
  2077.         -- * for priority lookups with self.priority[], 1=left and 2=right
  2078.         -- * recursively called
  2079.         -- * used in expr()
  2080.         ------------------------------------------------------------------------
  2081.         function luaY:subexpr(ls, v, limit)
  2082.           self:enterlevel(ls)
  2083.           local uop = self:getunopr(ls.t.token)
  2084.           if uop ~= "OPR_NOUNOPR" then
  2085.             luaX:next(ls)
  2086.             self:subexpr(ls, v, self.UNARY_PRIORITY)
  2087.             luaK:prefix(ls.fs, uop, v)
  2088.           else
  2089.             self:simpleexp(ls, v)
  2090.           end
  2091.           -- expand while operators have priorities higher than 'limit'
  2092.           local op = self:getbinopr(ls.t.token)
  2093.           while op ~= "OPR_NOBINOPR" and self.priority[luaK.BinOpr[op] + 1][1] > limit do
  2094.             local v2 = {}  -- expdesc
  2095.             luaX:next(ls)
  2096.             luaK:infix(ls.fs, op, v)
  2097.             -- read sub-expression with higher priority
  2098.             local nextop = self:subexpr(ls, v2, self.priority[luaK.BinOpr[op] + 1][2])
  2099.             luaK:posfix(ls.fs, op, v, v2)
  2100.             op = nextop
  2101.           end
  2102.           self:leavelevel(ls)
  2103.           return op  -- return first untreated operator
  2104.         end
  2105.        
  2106.         ------------------------------------------------------------------------
  2107.         -- Expression parsing starts here. Function subexpr is entered with the
  2108.         -- left operator (which is non-existent) priority of -1, which is lower
  2109.         -- than all actual operators. Expr information is returned in parm v.
  2110.         -- * used in multiple locations
  2111.         ------------------------------------------------------------------------
  2112.         function luaY:expr(ls, v)
  2113.           self:subexpr(ls, v, 0)
  2114.         end
  2115.        
  2116.         -- }====================================================================
  2117.        
  2118.         --[[--------------------------------------------------------------------
  2119.         -- Rules for Statements
  2120.         ----------------------------------------------------------------------]]
  2121.        
  2122.         ------------------------------------------------------------------------
  2123.         -- checks next token, used as a look-ahead
  2124.         -- * returns boolean instead of 0|1
  2125.         -- * used in retstat(), chunk()
  2126.         ------------------------------------------------------------------------
  2127.         function luaY:block_follow(token)
  2128.           if token == "TK_ELSE" or token == "TK_ELSEIF" or token == "TK_END"
  2129.              or token == "TK_UNTIL" or token == "TK_EOS" then
  2130.             return true
  2131.           else
  2132.             return false
  2133.           end
  2134.         end
  2135.        
  2136.         ------------------------------------------------------------------------
  2137.         -- parse a code block or unit
  2138.         -- * used in multiple functions
  2139.         ------------------------------------------------------------------------
  2140.         function luaY:block(ls)
  2141.           -- block -> chunk
  2142.           local fs = ls.fs
  2143.           local bl = {}  -- BlockCnt
  2144.           self:enterblock(fs, bl, false)
  2145.           self:chunk(ls)
  2146.           assert(bl.breaklist == luaK.NO_JUMP)
  2147.           self:leaveblock(fs)
  2148.         end
  2149.        
  2150.         ------------------------------------------------------------------------
  2151.         -- structure to chain all variables in the left-hand side of an
  2152.         -- assignment
  2153.         -- struct LHS_assign:
  2154.         --   prev  -- (table: struct LHS_assign)
  2155.         --   v  -- variable (global, local, upvalue, or indexed) (table: expdesc)
  2156.         ------------------------------------------------------------------------
  2157.        
  2158.         ------------------------------------------------------------------------
  2159.         -- check whether, in an assignment to a local variable, the local variable
  2160.         -- is needed in a previous assignment (to a table). If so, save original
  2161.         -- local value in a safe place and use this safe copy in the previous
  2162.         -- assignment.
  2163.         -- * used in assignment()
  2164.         ------------------------------------------------------------------------
  2165.         function luaY:check_conflict(ls, lh, v)
  2166.           local fs = ls.fs
  2167.           local extra = fs.freereg  -- eventual position to save local variable
  2168.           local conflict = false
  2169.           while lh do
  2170.             if lh.v.k == "VINDEXED" then
  2171.               if lh.v.info == v.info then  -- conflict?
  2172.                 conflict = true
  2173.                 lh.v.info = extra  -- previous assignment will use safe copy
  2174.               end
  2175.               if lh.v.aux == v.info then  -- conflict?
  2176.                 conflict = true
  2177.                 lh.v.aux = extra  -- previous assignment will use safe copy
  2178.               end
  2179.             end
  2180.             lh = lh.prev
  2181.           end
  2182.           if conflict then
  2183.             luaK:codeABC(fs, "OP_MOVE", fs.freereg, v.info, 0)  -- make copy
  2184.             luaK:reserveregs(fs, 1)
  2185.           end
  2186.         end
  2187.        
  2188.         ------------------------------------------------------------------------
  2189.         -- parse a variable assignment sequence
  2190.         -- * recursively called
  2191.         -- * used in exprstat()
  2192.         ------------------------------------------------------------------------
  2193.         function luaY:assignment(ls, lh, nvars)
  2194.           local e = {}  -- expdesc
  2195.           -- test was: VLOCAL <= lh->v.k && lh->v.k <= VINDEXED
  2196.           local c = lh.v.k
  2197.           self:check_condition(ls, c == "VLOCAL" or c == "VUPVAL" or c == "VGLOBAL"
  2198.                                or c == "VINDEXED", "syntax error")
  2199.           if self:testnext(ls, ",") then  -- assignment -> ',' primaryexp assignment
  2200.             local nv = {}  -- LHS_assign
  2201.                   nv.v = {}
  2202.             nv.prev = lh
  2203.             self:primaryexp(ls, nv.v)
  2204.             if nv.v.k == "VLOCAL" then
  2205.               self:check_conflict(ls, lh, nv.v)
  2206.             end
  2207.             self:checklimit(ls.fs, nvars, self.LUAI_MAXCCALLS - ls.L.nCcalls,
  2208.                             "variables in assignment")
  2209.             self:assignment(ls, nv, nvars + 1)
  2210.           else  -- assignment -> '=' explist1
  2211.             self:checknext(ls, "=")
  2212.             local nexps = self:explist1(ls, e)
  2213.             if nexps ~= nvars then
  2214.               self:adjust_assign(ls, nvars, nexps, e)
  2215.               if nexps > nvars then
  2216.                 ls.fs.freereg = ls.fs.freereg - (nexps - nvars)  -- remove extra values
  2217.               end
  2218.             else
  2219.               luaK:setoneret(ls.fs, e)  -- close last expression
  2220.               luaK:storevar(ls.fs, lh.v, e)
  2221.               return  -- avoid default
  2222.             end
  2223.           end
  2224.           self:init_exp(e, "VNONRELOC", ls.fs.freereg - 1)  -- default assignment
  2225.           luaK:storevar(ls.fs, lh.v, e)
  2226.         end
  2227.        
  2228.         ------------------------------------------------------------------------
  2229.         -- parse condition in a repeat statement or an if control structure
  2230.         -- * used in repeatstat(), test_then_block()
  2231.         ------------------------------------------------------------------------
  2232.         function luaY:cond(ls)
  2233.           -- cond -> exp
  2234.           local v = {}  -- expdesc
  2235.           self:expr(ls, v)  -- read condition
  2236.           if v.k == "VNIL" then v.k = "VFALSE" end  -- 'falses' are all equal here
  2237.           luaK:goiftrue(ls.fs, v)
  2238.           return v.f
  2239.         end
  2240.        
  2241.         ------------------------------------------------------------------------
  2242.         -- parse a break statement
  2243.         -- * used in statements()
  2244.         ------------------------------------------------------------------------
  2245.         function luaY:breakstat(ls)
  2246.           -- stat -> BREAK
  2247.           local fs = ls.fs
  2248.           local bl = fs.bl
  2249.           local upval = false
  2250.           while bl and not bl.isbreakable do
  2251.             if bl.upval then upval = true end
  2252.             bl = bl.previous
  2253.           end
  2254.           if not bl then
  2255.             luaX:syntaxerror(ls, "no loop to break")
  2256.           end
  2257.           if upval then
  2258.             luaK:codeABC(fs, "OP_CLOSE", bl.nactvar, 0, 0)
  2259.           end
  2260.           bl.breaklist = luaK:concat(fs, bl.breaklist, luaK:jump(fs))
  2261.         end
  2262.        
  2263.         ------------------------------------------------------------------------
  2264.         -- parse a while-do control structure, body processed by block()
  2265.         -- * with dynamic array sizes, MAXEXPWHILE + EXTRAEXP limits imposed by
  2266.         --   the function's implementation can be removed
  2267.         -- * used in statements()
  2268.         ------------------------------------------------------------------------
  2269.         function luaY:whilestat(ls, line)
  2270.           -- whilestat -> WHILE cond DO block END
  2271.           local fs = ls.fs
  2272.           local bl = {}  -- BlockCnt
  2273.           luaX:next(ls)  -- skip WHILE
  2274.           local whileinit = luaK:getlabel(fs)
  2275.           local condexit = self:cond(ls)
  2276.           self:enterblock(fs, bl, true)
  2277.           self:checknext(ls, "TK_DO")
  2278.           self:block(ls)
  2279.           luaK:patchlist(fs, luaK:jump(fs), whileinit)
  2280.           self:check_match(ls, "TK_END", "TK_WHILE", line)
  2281.           self:leaveblock(fs)
  2282.           luaK:patchtohere(fs, condexit)  -- false conditions finish the loop
  2283.         end
  2284.        
  2285.         ------------------------------------------------------------------------
  2286.         -- parse a repeat-until control structure, body parsed by chunk()
  2287.         -- * used in statements()
  2288.         ------------------------------------------------------------------------
  2289.         function luaY:repeatstat(ls, line)
  2290.           -- repeatstat -> REPEAT block UNTIL cond
  2291.           local fs = ls.fs
  2292.           local repeat_init = luaK:getlabel(fs)
  2293.           local bl1, bl2 = {}, {}  -- BlockCnt
  2294.           self:enterblock(fs, bl1, true)  -- loop block
  2295.           self:enterblock(fs, bl2, false)  -- scope block
  2296.           luaX:next(ls)  -- skip REPEAT
  2297.           self:chunk(ls)
  2298.           self:check_match(ls, "TK_UNTIL", "TK_REPEAT", line)
  2299.           local condexit = self:cond(ls)  -- read condition (inside scope block)
  2300.           if not bl2.upval then  -- no upvalues?
  2301.             self:leaveblock(fs)  -- finish scope
  2302.             luaK:patchlist(ls.fs, condexit, repeat_init)  -- close the loop
  2303.           else  -- complete semantics when there are upvalues
  2304.             self:breakstat(ls)  -- if condition then break
  2305.             luaK:patchtohere(ls.fs, condexit)  -- else...
  2306.             self:leaveblock(fs)  -- finish scope...
  2307.             luaK:patchlist(ls.fs, luaK:jump(fs), repeat_init)  -- and repeat
  2308.           end
  2309.           self:leaveblock(fs)  -- finish loop
  2310.         end
  2311.        
  2312.         ------------------------------------------------------------------------
  2313.         -- parse the single expressions needed in numerical for loops
  2314.         -- * used in fornum()
  2315.         ------------------------------------------------------------------------
  2316.         function luaY:exp1(ls)
  2317.           local e = {}  -- expdesc
  2318.           self:expr(ls, e)
  2319.           local k = e.k
  2320.           luaK:exp2nextreg(ls.fs, e)
  2321.           return k
  2322.         end
  2323.        
  2324.         ------------------------------------------------------------------------
  2325.         -- parse a for loop body for both versions of the for loop
  2326.         -- * used in fornum(), forlist()
  2327.         ------------------------------------------------------------------------
  2328.         function luaY:forbody(ls, base, line, nvars, isnum)
  2329.           -- forbody -> DO block
  2330.           local bl = {}  -- BlockCnt
  2331.           local fs = ls.fs
  2332.           self:adjustlocalvars(ls, 3)  -- control variables
  2333.           self:checknext(ls, "TK_DO")
  2334.           local prep = isnum and luaK:codeAsBx(fs, "OP_FORPREP", base, luaK.NO_JUMP)
  2335.                              or luaK:jump(fs)
  2336.           self:enterblock(fs, bl, false)  -- scope for declared variables
  2337.           self:adjustlocalvars(ls, nvars)
  2338.           luaK:reserveregs(fs, nvars)
  2339.           self:block(ls)
  2340.           self:leaveblock(fs)  -- end of scope for declared variables
  2341.           luaK:patchtohere(fs, prep)
  2342.           local endfor = isnum and luaK:codeAsBx(fs, "OP_FORLOOP", base, luaK.NO_JUMP)
  2343.                                or luaK:codeABC(fs, "OP_TFORLOOP", base, 0, nvars)
  2344.           luaK:fixline(fs, line)  -- pretend that `OP_FOR' starts the loop
  2345.           luaK:patchlist(fs, isnum and endfor or luaK:jump(fs), prep + 1)
  2346.         end
  2347.        
  2348.         ------------------------------------------------------------------------
  2349.         -- parse a numerical for loop, calls forbody()
  2350.         -- * used in forstat()
  2351.         ------------------------------------------------------------------------
  2352.         function luaY:fornum(ls, varname, line)
  2353.           -- fornum -> NAME = exp1,exp1[,exp1] forbody
  2354.           local fs = ls.fs
  2355.           local base = fs.freereg
  2356.           self:new_localvarliteral(ls, "(for index)", 0)
  2357.           self:new_localvarliteral(ls, "(for limit)", 1)
  2358.           self:new_localvarliteral(ls, "(for step)", 2)
  2359.           self:new_localvar(ls, varname, 3)
  2360.           self:checknext(ls, '=')
  2361.           self:exp1(ls)  -- initial value
  2362.           self:checknext(ls, ",")
  2363.           self:exp1(ls)  -- limit
  2364.           if self:testnext(ls, ",") then
  2365.             self:exp1(ls)  -- optional step
  2366.           else  -- default step = 1
  2367.             luaK:codeABx(fs, "OP_LOADK", fs.freereg, luaK:numberK(fs, 1))
  2368.             luaK:reserveregs(fs, 1)
  2369.           end
  2370.           self:forbody(ls, base, line, 1, true)
  2371.         end
  2372.        
  2373.         ------------------------------------------------------------------------
  2374.         -- parse a generic for loop, calls forbody()
  2375.         -- * used in forstat()
  2376.         ------------------------------------------------------------------------
  2377.         function luaY:forlist(ls, indexname)
  2378.           -- forlist -> NAME {,NAME} IN explist1 forbody
  2379.           local fs = ls.fs
  2380.           local e = {}  -- expdesc
  2381.           local nvars = 0
  2382.           local base = fs.freereg
  2383.           -- create control variables
  2384.           self:new_localvarliteral(ls, "(for generator)", nvars)
  2385.           nvars = nvars + 1
  2386.           self:new_localvarliteral(ls, "(for state)", nvars)
  2387.           nvars = nvars + 1
  2388.           self:new_localvarliteral(ls, "(for control)", nvars)
  2389.           nvars = nvars + 1
  2390.           -- create declared variables
  2391.           self:new_localvar(ls, indexname, nvars)
  2392.           nvars = nvars + 1
  2393.           while self:testnext(ls, ",") do
  2394.             self:new_localvar(ls, self:str_checkname(ls), nvars)
  2395.             nvars = nvars + 1
  2396.           end
  2397.           self:checknext(ls, "TK_IN")
  2398.           local line = ls.linenumber
  2399.           self:adjust_assign(ls, 3, self:explist1(ls, e), e)
  2400.           luaK:checkstack(fs, 3)  -- extra space to call generator
  2401.           self:forbody(ls, base, line, nvars - 3, false)
  2402.         end
  2403.        
  2404.         ------------------------------------------------------------------------
  2405.         -- initial parsing for a for loop, calls fornum() or forlist()
  2406.         -- * used in statements()
  2407.         ------------------------------------------------------------------------
  2408.         function luaY:forstat(ls, line)
  2409.           -- forstat -> FOR (fornum | forlist) END
  2410.           local fs = ls.fs
  2411.           local bl = {}  -- BlockCnt
  2412.           self:enterblock(fs, bl, true)  -- scope for loop and control variables
  2413.           luaX:next(ls)  -- skip `for'
  2414.           local varname = self:str_checkname(ls)  -- first variable name
  2415.           local c = ls.t.token
  2416.           if c == "=" then
  2417.             self:fornum(ls, varname, line)
  2418.           elseif c == "," or c == "TK_IN" then
  2419.             self:forlist(ls, varname)
  2420.           else
  2421.             luaX:syntaxerror(ls, self:LUA_QL("=").." or "..self:LUA_QL("in").." expected")
  2422.           end
  2423.           self:check_match(ls, "TK_END", "TK_FOR", line)
  2424.           self:leaveblock(fs)  -- loop scope (`break' jumps to this point)
  2425.         end
  2426.        
  2427.         ------------------------------------------------------------------------
  2428.         -- parse part of an if control structure, including the condition
  2429.         -- * used in ifstat()
  2430.         ------------------------------------------------------------------------
  2431.         function luaY:test_then_block(ls)
  2432.           -- test_then_block -> [IF | ELSEIF] cond THEN block
  2433.           luaX:next(ls)  -- skip IF or ELSEIF
  2434.           local condexit = self:cond(ls)
  2435.           self:checknext(ls, "TK_THEN")
  2436.           self:block(ls)  -- `then' part
  2437.           return condexit
  2438.         end
  2439.        
  2440.         ------------------------------------------------------------------------
  2441.         -- parse an if control structure
  2442.         -- * used in statements()
  2443.         ------------------------------------------------------------------------
  2444.         function luaY:ifstat(ls, line)
  2445.           -- ifstat -> IF cond THEN block {ELSEIF cond THEN block} [ELSE block] END
  2446.           local fs = ls.fs
  2447.           local escapelist = luaK.NO_JUMP
  2448.           local flist = self:test_then_block(ls)  -- IF cond THEN block
  2449.           while ls.t.token == "TK_ELSEIF" do
  2450.             escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  2451.             luaK:patchtohere(fs, flist)
  2452.             flist = self:test_then_block(ls)  -- ELSEIF cond THEN block
  2453.           end
  2454.           if ls.t.token == "TK_ELSE" then
  2455.             escapelist = luaK:concat(fs, escapelist, luaK:jump(fs))
  2456.             luaK:patchtohere(fs, flist)
  2457.             luaX:next(ls)  -- skip ELSE (after patch, for correct line info)
  2458.             self:block(ls)  -- 'else' part
  2459.           else
  2460.             escapelist = luaK:concat(fs, escapelist, flist)
  2461.           end
  2462.           luaK:patchtohere(fs, escapelist)
  2463.           self:check_match(ls, "TK_END", "TK_IF", line)
  2464.         end
  2465.        
  2466.         ------------------------------------------------------------------------
  2467.         -- parse a local function statement
  2468.         -- * used in statements()
  2469.         ------------------------------------------------------------------------
  2470.         function luaY:localfunc(ls)
  2471.           local v, b = {}, {}  -- expdesc
  2472.           local fs = ls.fs
  2473.           self:new_localvar(ls, self:str_checkname(ls), 0)
  2474.           self:init_exp(v, "VLOCAL", fs.freereg)
  2475.           luaK:reserveregs(fs, 1)
  2476.           self:adjustlocalvars(ls, 1)
  2477.           self:body(ls, b, false, ls.linenumber)
  2478.           luaK:storevar(fs, v, b)
  2479.           -- debug information will only see the variable after this point!
  2480.           self:getlocvar(fs, fs.nactvar - 1).startpc = fs.pc
  2481.         end
  2482.        
  2483.         ------------------------------------------------------------------------
  2484.         -- parse a local variable declaration statement
  2485.         -- * used in statements()
  2486.         ------------------------------------------------------------------------
  2487.         function luaY:localstat(ls)
  2488.           -- stat -> LOCAL NAME {',' NAME} ['=' explist1]
  2489.           local nvars = 0
  2490.           local nexps
  2491.           local e = {}  -- expdesc
  2492.           repeat
  2493.             self:new_localvar(ls, self:str_checkname(ls), nvars)
  2494.             nvars = nvars + 1
  2495.           until not self:testnext(ls, ",")
  2496.           if self:testnext(ls, "=") then
  2497.             nexps = self:explist1(ls, e)
  2498.           else
  2499.             e.k = "VVOID"
  2500.             nexps = 0
  2501.           end
  2502.           self:adjust_assign(ls, nvars, nexps, e)
  2503.           self:adjustlocalvars(ls, nvars)
  2504.         end
  2505.        
  2506.         ------------------------------------------------------------------------
  2507.         -- parse a function name specification
  2508.         -- * used in funcstat()
  2509.         ------------------------------------------------------------------------
  2510.         function luaY:funcname(ls, v)
  2511.           -- funcname -> NAME {field} [':' NAME]
  2512.           local needself = false
  2513.           self:singlevar(ls, v)
  2514.           while ls.t.token == "." do
  2515.             self:field(ls, v)
  2516.           end
  2517.           if ls.t.token == ":" then
  2518.             needself = true
  2519.             self:field(ls, v)
  2520.           end
  2521.           return needself
  2522.         end
  2523.        
  2524.         ------------------------------------------------------------------------
  2525.         -- parse a function statement
  2526.         -- * used in statements()
  2527.         ------------------------------------------------------------------------
  2528.         function luaY:funcstat(ls, line)
  2529.           -- funcstat -> FUNCTION funcname body
  2530.           local v, b = {}, {}  -- expdesc
  2531.           luaX:next(ls)  -- skip FUNCTION
  2532.           local needself = self:funcname(ls, v)
  2533.           self:body(ls, b, needself, line)
  2534.           luaK:storevar(ls.fs, v, b)
  2535.           luaK:fixline(ls.fs, line)  -- definition 'happens' in the first line
  2536.         end
  2537.        
  2538.         ------------------------------------------------------------------------
  2539.         -- parse a function call with no returns or an assignment statement
  2540.         -- * used in statements()
  2541.         ------------------------------------------------------------------------
  2542.         function luaY:exprstat(ls)
  2543.           -- stat -> func | assignment
  2544.           local fs = ls.fs
  2545.           local v = {}  -- LHS_assign
  2546.                 v.v = {}
  2547.           self:primaryexp(ls, v.v)
  2548.           if v.v.k == "VCALL" then  -- stat -> func
  2549.             luaP:SETARG_C(luaK:getcode(fs, v.v), 1)  -- call statement uses no results
  2550.           else  -- stat -> assignment
  2551.             v.prev = nil
  2552.             self:assignment(ls, v, 1)
  2553.           end
  2554.         end
  2555.        
  2556.         ------------------------------------------------------------------------
  2557.         -- parse a return statement
  2558.         -- * used in statements()
  2559.         ------------------------------------------------------------------------
  2560.         function luaY:retstat(ls)
  2561.           -- stat -> RETURN explist
  2562.           local fs = ls.fs
  2563.           local e = {}  -- expdesc
  2564.           local first, nret  -- registers with returned values
  2565.           luaX:next(ls)  -- skip RETURN
  2566.           if self:block_follow(ls.t.token) or ls.t.token == ";" then
  2567.             first, nret = 0, 0  -- return no values
  2568.           else
  2569.             nret = self:explist1(ls, e)  -- optional return values
  2570.             if self:hasmultret(e.k) then
  2571.               luaK:setmultret(fs, e)
  2572.               if e.k == "VCALL" and nret == 1 then  -- tail call?
  2573.                 luaP:SET_OPCODE(luaK:getcode(fs, e), "OP_TAILCALL")
  2574.                 assert(luaP:GETARG_A(luaK:getcode(fs, e)) == fs.nactvar)
  2575.               end
  2576.               first = fs.nactvar
  2577.               nret = self.LUA_MULTRET  -- return all values
  2578.             else
  2579.               if nret == 1 then  -- only one single value?
  2580.                 first = luaK:exp2anyreg(fs, e)
  2581.               else
  2582.                 luaK:exp2nextreg(fs, e)  -- values must go to the 'stack'
  2583.                 first = fs.nactvar  -- return all 'active' values
  2584.                 assert(nret == fs.freereg - first)
  2585.               end
  2586.             end--if
  2587.           end--if
  2588.           luaK:ret(fs, first, nret)
  2589.         end
  2590.        
  2591.         ------------------------------------------------------------------------
  2592.         -- initial parsing for statements, calls a lot of functions
  2593.         -- * returns boolean instead of 0|1
  2594.         -- * used in chunk()
  2595.         ------------------------------------------------------------------------
  2596.         function luaY:statement(ls)
  2597.           local line = ls.linenumber  -- may be needed for error messages
  2598.           local c = ls.t.token
  2599.           if c == "TK_IF" then  -- stat -> ifstat
  2600.             self:ifstat(ls, line)
  2601.             return false
  2602.           elseif c == "TK_WHILE" then  -- stat -> whilestat
  2603.             self:whilestat(ls, line)
  2604.             return false
  2605.           elseif c == "TK_DO" then  -- stat -> DO block END
  2606.             luaX:next(ls)  -- skip DO
  2607.             self:block(ls)
  2608.             self:check_match(ls, "TK_END", "TK_DO", line)
  2609.             return false
  2610.           elseif c == "TK_FOR" then  -- stat -> forstat
  2611.             self:forstat(ls, line)
  2612.             return false
  2613.           elseif c == "TK_REPEAT" then  -- stat -> repeatstat
  2614.             self:repeatstat(ls, line)
  2615.             return false
  2616.           elseif c == "TK_FUNCTION" then  -- stat -> funcstat
  2617.             self:funcstat(ls, line)
  2618.             return false
  2619.           elseif c == "TK_LOCAL" then  -- stat -> localstat
  2620.             luaX:next(ls)  -- skip LOCAL
  2621.             if self:testnext(ls, "TK_FUNCTION") then  -- local function?
  2622.               self:localfunc(ls)
  2623.             else
  2624.               self:localstat(ls)
  2625.             end
  2626.             return false
  2627.           elseif c == "TK_RETURN" then  -- stat -> retstat
  2628.             self:retstat(ls)
  2629.             return true  -- must be last statement
  2630.           elseif c == "TK_BREAK" then  -- stat -> breakstat
  2631.             luaX:next(ls)  -- skip BREAK
  2632.             self:breakstat(ls)
  2633.             return true  -- must be last statement
  2634.           else
  2635.             self:exprstat(ls)
  2636.             return false  -- to avoid warnings
  2637.           end--if c
  2638.         end
  2639.        
  2640.         ------------------------------------------------------------------------
  2641.         -- parse a chunk, which consists of a bunch of statements
  2642.         -- * used in parser(), body(), block(), repeatstat()
  2643.         ------------------------------------------------------------------------
  2644.         function luaY:chunk(ls)
  2645.           -- chunk -> { stat [';'] }
  2646.           local islast = false
  2647.           self:enterlevel(ls)
  2648.           while not islast and not self:block_follow(ls.t.token) do
  2649.             islast = self:statement(ls)
  2650.             self:testnext(ls, ";")
  2651.             assert(ls.fs.f.maxstacksize >= ls.fs.freereg and
  2652.                        ls.fs.freereg >= ls.fs.nactvar)
  2653.             ls.fs.freereg = ls.fs.nactvar  -- free registers
  2654.           end
  2655.           self:leavelevel(ls)
  2656.         end
  2657.        
  2658.         -- }======================================================================
  2659.         return luaY
  2660.     end
  2661.     fake_module_scripts[script] = module_script
  2662. end
  2663. do -- nil.LuaK
  2664.     local script = Instance.new('ModuleScript', nil)
  2665.     script.Name = "LuaK"
  2666.     local function module_script()
  2667.         --[[--------------------------------------------------------------------
  2668.        
  2669.           lcode.lua
  2670.           Lua 5 code generator in Lua
  2671.           This file is part of Yueliang.
  2672.        
  2673.           Copyright (c) 2005-2007 Kein-Hong Man <khman@users.sf.net>
  2674.           The COPYRIGHT file describes the conditions
  2675.           under which this software may be distributed.
  2676.        
  2677.           See the ChangeLog for more information.
  2678.        
  2679.         ----------------------------------------------------------------------]]
  2680.        
  2681.         --[[--------------------------------------------------------------------
  2682.         -- Notes:
  2683.         -- * one function manipulate a pointer argument with a simple data type
  2684.         --   (can't be emulated by a table, ambiguous), now returns that value:
  2685.         --   luaK:concat(fs, l1, l2)
  2686.         -- * luaM_growvector uses the faux luaY:growvector, for limit checking
  2687.         -- * some function parameters changed to boolean, additional code
  2688.         --   translates boolean back to 1/0 for instruction fields
  2689.         --
  2690.         -- Not implemented:
  2691.         -- * NOTE there is a failed assert in luaK:addk, a porting problem
  2692.         --
  2693.         -- Added:
  2694.         -- * constant MAXSTACK from llimits.h
  2695.         -- * luaK:ttisnumber(o) (from lobject.h)
  2696.         -- * luaK:nvalue(o) (from lobject.h)
  2697.         -- * luaK:setnilvalue(o) (from lobject.h)
  2698.         -- * luaK:setnvalue(o, x) (from lobject.h)
  2699.         -- * luaK:setbvalue(o, x) (from lobject.h)
  2700.         -- * luaK:sethvalue(o, x) (from lobject.h), parameter L deleted
  2701.         -- * luaK:setsvalue(o, x) (from lobject.h), parameter L deleted
  2702.         -- * luaK:numadd, luaK:numsub, luaK:nummul, luaK:numdiv, luaK:nummod,
  2703.         --   luaK:numpow, luaK:numunm, luaK:numisnan (from luaconf.h)
  2704.         -- * copyexp(e1, e2) added in luaK:posfix to copy expdesc struct
  2705.         --
  2706.         -- Changed in 5.1.x:
  2707.         -- * enum BinOpr has a new entry, OPR_MOD
  2708.         -- * enum UnOpr has a new entry, OPR_LEN
  2709.         -- * binopistest, unused in 5.0.x, has been deleted
  2710.         -- * macro setmultret is new
  2711.         -- * functions isnumeral, luaK_ret, boolK are new
  2712.         -- * funcion nilK was named nil_constant in 5.0.x
  2713.         -- * function interface changed: need_value, patchtestreg, concat
  2714.         -- * TObject now a TValue
  2715.         -- * functions luaK_setreturns, luaK_setoneret are new
  2716.         -- * function luaK:setcallreturns deleted, to be replaced by:
  2717.         --   luaK:setmultret, luaK:ret, luaK:setreturns, luaK:setoneret
  2718.         -- * functions constfolding, codearith, codecomp are new
  2719.         -- * luaK:codebinop has been deleted
  2720.         -- * function luaK_setlist is new
  2721.         -- * OPR_MULT renamed to OPR_MUL
  2722.         ----------------------------------------------------------------------]]
  2723.        
  2724.         -- requires luaP, luaX, luaY
  2725.         local luaK = {}
  2726.         local luaP = require(script.Parent.LuaP)
  2727.         local luaX = require(script.Parent.LuaX)
  2728.        
  2729.         ------------------------------------------------------------------------
  2730.         -- constants used by code generator
  2731.         ------------------------------------------------------------------------
  2732.         -- maximum stack for a Lua function
  2733.         luaK.MAXSTACK = 250  -- (from llimits.h)
  2734.        
  2735.         --[[--------------------------------------------------------------------
  2736.         -- other functions
  2737.         ----------------------------------------------------------------------]]
  2738.        
  2739.         ------------------------------------------------------------------------
  2740.         -- emulation of TValue macros (these are from lobject.h)
  2741.         -- * TValue is a table since lcode passes references around
  2742.         -- * tt member field removed, using Lua's type() instead
  2743.         -- * for setsvalue, sethvalue, parameter L (deleted here) in lobject.h
  2744.         --   is used in an assert for testing, see checkliveness(g,obj)
  2745.         ------------------------------------------------------------------------
  2746.         function luaK:ttisnumber(o)
  2747.           if o then return type(o.value) == "number" else return false end
  2748.         end
  2749.         function luaK:nvalue(o) return o.value end
  2750.         function luaK:setnilvalue(o) o.value = nil end
  2751.         function luaK:setsvalue(o, x) o.value = x end
  2752.         luaK.setnvalue = luaK.setsvalue
  2753.         luaK.sethvalue = luaK.setsvalue
  2754.         luaK.setbvalue = luaK.setsvalue
  2755.        
  2756.         ------------------------------------------------------------------------
  2757.         -- The luai_num* macros define the primitive operations over numbers.
  2758.         -- * this is not the entire set of primitive operations from luaconf.h
  2759.         -- * used in luaK:constfolding()
  2760.         ------------------------------------------------------------------------
  2761.         function luaK:numadd(a, b) return a + b end
  2762.         function luaK:numsub(a, b) return a - b end
  2763.         function luaK:nummul(a, b) return a * b end
  2764.         function luaK:numdiv(a, b) return a / b end
  2765.         function luaK:nummod(a, b) return a % b end
  2766.           -- ((a) - floor((a)/(b))*(b)) /* actual, for reference */
  2767.         function luaK:numpow(a, b) return a ^ b end
  2768.         function luaK:numunm(a) return -a end
  2769.         function luaK:numisnan(a) return not a == a end
  2770.           -- a NaN cannot equal another NaN
  2771.        
  2772.         --[[--------------------------------------------------------------------
  2773.         -- code generator functions
  2774.         ----------------------------------------------------------------------]]
  2775.        
  2776.         ------------------------------------------------------------------------
  2777.         -- Marks the end of a patch list. It is an invalid value both as an absolute
  2778.         -- address, and as a list link (would link an element to itself).
  2779.         ------------------------------------------------------------------------
  2780.         luaK.NO_JUMP = -1
  2781.        
  2782.         ------------------------------------------------------------------------
  2783.         -- grep "ORDER OPR" if you change these enums
  2784.         ------------------------------------------------------------------------
  2785.         luaK.BinOpr = {
  2786.           OPR_ADD = 0, OPR_SUB = 1, OPR_MUL = 2, OPR_DIV = 3, OPR_MOD = 4, OPR_POW = 5,
  2787.           OPR_CONCAT = 6,
  2788.           OPR_NE = 7, OPR_EQ = 8,
  2789.           OPR_LT = 9, OPR_LE = 10, OPR_GT = 11, OPR_GE = 12,
  2790.           OPR_AND = 13, OPR_OR = 14,
  2791.           OPR_NOBINOPR = 15,
  2792.         }
  2793.        
  2794.         -- * UnOpr is used by luaK:prefix's op argument, but not directly used
  2795.         --   because the function receives the symbols as strings, e.g. "OPR_NOT"
  2796.         luaK.UnOpr = {
  2797.           OPR_MINUS = 0, OPR_NOT = 1, OPR_LEN = 2, OPR_NOUNOPR = 3
  2798.         }
  2799.        
  2800.         ------------------------------------------------------------------------
  2801.         -- returns the instruction object for given e (expdesc), was a macro
  2802.         ------------------------------------------------------------------------
  2803.         function luaK:getcode(fs, e)
  2804.           return fs.f.code[e.info]
  2805.         end
  2806.        
  2807.         ------------------------------------------------------------------------
  2808.         -- codes an instruction with a signed Bx (sBx) field, was a macro
  2809.         -- * used in luaK:jump(), (lparser) luaY:forbody()
  2810.         ------------------------------------------------------------------------
  2811.         function luaK:codeAsBx(fs, o, A, sBx)
  2812.           return self:codeABx(fs, o, A, sBx + luaP.MAXARG_sBx)
  2813.         end
  2814.        
  2815.         ------------------------------------------------------------------------
  2816.         -- set the expdesc e instruction for multiple returns, was a macro
  2817.         ------------------------------------------------------------------------
  2818.         function luaK:setmultret(fs, e)
  2819.           self:setreturns(fs, e, luaY.LUA_MULTRET)
  2820.         end
  2821.        
  2822.         ------------------------------------------------------------------------
  2823.         -- there is a jump if patch lists are not identical, was a macro
  2824.         -- * used in luaK:exp2reg(), luaK:exp2anyreg(), luaK:exp2val()
  2825.         ------------------------------------------------------------------------
  2826.         function luaK:hasjumps(e)
  2827.           return e.t ~= e.f
  2828.         end
  2829.        
  2830.         ------------------------------------------------------------------------
  2831.         -- true if the expression is a constant number (for constant folding)
  2832.         -- * used in constfolding(), infix()
  2833.         ------------------------------------------------------------------------
  2834.         function luaK:isnumeral(e)
  2835.           return e.k == "VKNUM" and e.t == self.NO_JUMP and e.f == self.NO_JUMP
  2836.         end
  2837.        
  2838.         ------------------------------------------------------------------------
  2839.         -- codes loading of nil, optimization done if consecutive locations
  2840.         -- * used in luaK:discharge2reg(), (lparser) luaY:adjust_assign()
  2841.         ------------------------------------------------------------------------
  2842.         function luaK:_nil(fs, from, n)
  2843.           if fs.pc > fs.lasttarget then  -- no jumps to current position?
  2844.             if fs.pc == 0 then  -- function start?
  2845.               if from >= fs.nactvar then
  2846.                 return  -- positions are already clean
  2847.               end
  2848.             else
  2849.               local previous = fs.f.code[fs.pc - 1]
  2850.               if luaP:GET_OPCODE(previous) == "OP_LOADNIL" then
  2851.                 local pfrom = luaP:GETARG_A(previous)
  2852.                 local pto = luaP:GETARG_B(previous)
  2853.                 if pfrom <= from and from <= pto + 1 then  -- can connect both?
  2854.                   if from + n - 1 > pto then
  2855.                     luaP:SETARG_B(previous, from + n - 1)
  2856.                   end
  2857.                   return
  2858.                 end
  2859.               end
  2860.             end
  2861.           end
  2862.           self:codeABC(fs, "OP_LOADNIL", from, from + n - 1, 0)  -- else no optimization
  2863.         end
  2864.        
  2865.         ------------------------------------------------------------------------
  2866.         --
  2867.         -- * used in multiple locations
  2868.         ------------------------------------------------------------------------
  2869.         function luaK:jump(fs)
  2870.           local jpc = fs.jpc  -- save list of jumps to here
  2871.           fs.jpc = self.NO_JUMP
  2872.           local j = self:codeAsBx(fs, "OP_JMP", 0, self.NO_JUMP)
  2873.           j = self:concat(fs, j, jpc)  -- keep them on hold
  2874.           return j
  2875.         end
  2876.        
  2877.         ------------------------------------------------------------------------
  2878.         -- codes a RETURN instruction
  2879.         -- * used in luaY:close_func(), luaY:retstat()
  2880.         ------------------------------------------------------------------------
  2881.         function luaK:ret(fs, first, nret)
  2882.           self:codeABC(fs, "OP_RETURN", first, nret + 1, 0)
  2883.         end
  2884.        
  2885.         ------------------------------------------------------------------------
  2886.         --
  2887.         -- * used in luaK:jumponcond(), luaK:codecomp()
  2888.         ------------------------------------------------------------------------
  2889.         function luaK:condjump(fs, op, A, B, C)
  2890.           self:codeABC(fs, op, A, B, C)
  2891.           return self:jump(fs)
  2892.         end
  2893.        
  2894.         ------------------------------------------------------------------------
  2895.         --
  2896.         -- * used in luaK:patchlistaux(), luaK:concat()
  2897.         ------------------------------------------------------------------------
  2898.         function luaK:fixjump(fs, pc, dest)
  2899.           local jmp = fs.f.code[pc]
  2900.           local offset = dest - (pc + 1)
  2901.           assert(dest ~= self.NO_JUMP)
  2902.           if math.abs(offset) > luaP.MAXARG_sBx then
  2903.             luaX:syntaxerror(fs.ls, "control structure too long")
  2904.           end
  2905.           luaP:SETARG_sBx(jmp, offset)
  2906.         end
  2907.        
  2908.         ------------------------------------------------------------------------
  2909.         -- returns current 'pc' and marks it as a jump target (to avoid wrong
  2910.         -- optimizations with consecutive instructions not in the same basic block).
  2911.         -- * used in multiple locations
  2912.         -- * fs.lasttarget tested only by luaK:_nil() when optimizing OP_LOADNIL
  2913.         ------------------------------------------------------------------------
  2914.         function luaK:getlabel(fs)
  2915.           fs.lasttarget = fs.pc
  2916.           return fs.pc
  2917.         end
  2918.        
  2919.         ------------------------------------------------------------------------
  2920.         --
  2921.         -- * used in luaK:need_value(), luaK:removevalues(), luaK:patchlistaux(),
  2922.         --   luaK:concat()
  2923.         ------------------------------------------------------------------------
  2924.         function luaK:getjump(fs, pc)
  2925.           local offset = luaP:GETARG_sBx(fs.f.code[pc])
  2926.           if offset == self.NO_JUMP then  -- point to itself represents end of list
  2927.             return self.NO_JUMP  -- end of list
  2928.           else
  2929.             return (pc + 1) + offset  -- turn offset into absolute position
  2930.           end
  2931.         end
  2932.        
  2933.         ------------------------------------------------------------------------
  2934.         --
  2935.         -- * used in luaK:need_value(), luaK:patchtestreg(), luaK:invertjump()
  2936.         ------------------------------------------------------------------------
  2937.         function luaK:getjumpcontrol(fs, pc)
  2938.           local pi = fs.f.code[pc]
  2939.           local ppi = fs.f.code[pc - 1]
  2940.           if pc >= 1 and luaP:testTMode(luaP:GET_OPCODE(ppi)) ~= 0 then
  2941.             return ppi
  2942.           else
  2943.             return pi
  2944.           end
  2945.         end
  2946.        
  2947.         ------------------------------------------------------------------------
  2948.         -- check whether list has any jump that do not produce a value
  2949.         -- (or produce an inverted value)
  2950.         -- * return value changed to boolean
  2951.         -- * used only in luaK:exp2reg()
  2952.         ------------------------------------------------------------------------
  2953.         function luaK:need_value(fs, list)
  2954.           while list ~= self.NO_JUMP do
  2955.             local i = self:getjumpcontrol(fs, list)
  2956.             if luaP:GET_OPCODE(i) ~= "OP_TESTSET" then return true end
  2957.             list = self:getjump(fs, list)
  2958.           end
  2959.           return false  -- not found
  2960.         end
  2961.        
  2962.         ------------------------------------------------------------------------
  2963.         --
  2964.         -- * used in luaK:removevalues(), luaK:patchlistaux()
  2965.         ------------------------------------------------------------------------
  2966.         function luaK:patchtestreg(fs, node, reg)
  2967.           local i = self:getjumpcontrol(fs, node)
  2968.           if luaP:GET_OPCODE(i) ~= "OP_TESTSET" then
  2969.             return false  -- cannot patch other instructions
  2970.           end
  2971.           if reg ~= luaP.NO_REG and reg ~= luaP:GETARG_B(i) then
  2972.             luaP:SETARG_A(i, reg)
  2973.           else  -- no register to put value or register already has the value
  2974.             -- due to use of a table as i, i cannot be replaced by another table
  2975.             -- so the following is required; there is no change to ARG_C
  2976.             luaP:SET_OPCODE(i, "OP_TEST")
  2977.             local b = luaP:GETARG_B(i)
  2978.             luaP:SETARG_A(i, b)
  2979.             luaP:SETARG_B(i, 0)
  2980.             -- *i = CREATE_ABC(OP_TEST, GETARG_B(*i), 0, GETARG_C(*i)); /* C */
  2981.           end
  2982.           return true
  2983.         end
  2984.        
  2985.         ------------------------------------------------------------------------
  2986.         --
  2987.         -- * used only in luaK:codenot()
  2988.         ------------------------------------------------------------------------
  2989.         function luaK:removevalues(fs, list)
  2990.           while list ~= self.NO_JUMP do
  2991.             self:patchtestreg(fs, list, luaP.NO_REG)
  2992.             list = self:getjump(fs, list)
  2993.           end
  2994.         end
  2995.        
  2996.         ------------------------------------------------------------------------
  2997.         --
  2998.         -- * used in luaK:dischargejpc(), luaK:patchlist(), luaK:exp2reg()
  2999.         ------------------------------------------------------------------------
  3000.         function luaK:patchlistaux(fs, list, vtarget, reg, dtarget)
  3001.           while list ~= self.NO_JUMP do
  3002.             local _next = self:getjump(fs, list)
  3003.             if self:patchtestreg(fs, list, reg) then
  3004.               self:fixjump(fs, list, vtarget)
  3005.             else
  3006.               self:fixjump(fs, list, dtarget)  -- jump to default target
  3007.             end
  3008.             list = _next
  3009.           end
  3010.         end
  3011.        
  3012.         ------------------------------------------------------------------------
  3013.         --
  3014.         -- * used only in luaK:code()
  3015.         ------------------------------------------------------------------------
  3016.         function luaK:dischargejpc(fs)
  3017.           self:patchlistaux(fs, fs.jpc, fs.pc, luaP.NO_REG, fs.pc)
  3018.           fs.jpc = self.NO_JUMP
  3019.         end
  3020.        
  3021.         ------------------------------------------------------------------------
  3022.         --
  3023.         -- * used in (lparser) luaY:whilestat(), luaY:repeatstat(), luaY:forbody()
  3024.         ------------------------------------------------------------------------
  3025.         function luaK:patchlist(fs, list, target)
  3026.           if target == fs.pc then
  3027.             self:patchtohere(fs, list)
  3028.           else
  3029.             assert(target < fs.pc)
  3030.             self:patchlistaux(fs, list, target, luaP.NO_REG, target)
  3031.           end
  3032.         end
  3033.        
  3034.         ------------------------------------------------------------------------
  3035.         --
  3036.         -- * used in multiple locations
  3037.         ------------------------------------------------------------------------
  3038.         function luaK:patchtohere(fs, list)
  3039.           self:getlabel(fs)
  3040.           fs.jpc = self:concat(fs, fs.jpc, list)
  3041.         end
  3042.        
  3043.         ------------------------------------------------------------------------
  3044.         -- * l1 was a pointer, now l1 is returned and callee assigns the value
  3045.         -- * used in multiple locations
  3046.         ------------------------------------------------------------------------
  3047.         function luaK:concat(fs, l1, l2)
  3048.           if l2 == self.NO_JUMP then return l1
  3049.           elseif l1 == self.NO_JUMP then
  3050.             return l2
  3051.           else
  3052.             local list = l1
  3053.             local _next = self:getjump(fs, list)
  3054.             while _next ~= self.NO_JUMP do  -- find last element
  3055.               list = _next
  3056.               _next = self:getjump(fs, list)
  3057.             end
  3058.             self:fixjump(fs, list, l2)
  3059.           end
  3060.           return l1
  3061.         end
  3062.        
  3063.         ------------------------------------------------------------------------
  3064.         --
  3065.         -- * used in luaK:reserveregs(), (lparser) luaY:forlist()
  3066.         ------------------------------------------------------------------------
  3067.         function luaK:checkstack(fs, n)
  3068.           local newstack = fs.freereg + n
  3069.           if newstack > fs.f.maxstacksize then
  3070.             if newstack >= self.MAXSTACK then
  3071.               luaX:syntaxerror(fs.ls, "function or expression too complex")
  3072.             end
  3073.             fs.f.maxstacksize = newstack
  3074.           end
  3075.         end
  3076.        
  3077.         ------------------------------------------------------------------------
  3078.         --
  3079.         -- * used in multiple locations
  3080.         ------------------------------------------------------------------------
  3081.         function luaK:reserveregs(fs, n)
  3082.           self:checkstack(fs, n)
  3083.           fs.freereg = fs.freereg + n
  3084.         end
  3085.        
  3086.         ------------------------------------------------------------------------
  3087.         --
  3088.         -- * used in luaK:freeexp(), luaK:dischargevars()
  3089.         ------------------------------------------------------------------------
  3090.         function luaK:freereg(fs, reg)
  3091.           if not luaP:ISK(reg) and reg >= fs.nactvar then
  3092.             fs.freereg = fs.freereg - 1
  3093.             assert(reg == fs.freereg)
  3094.           end
  3095.         end
  3096.        
  3097.         ------------------------------------------------------------------------
  3098.         --
  3099.         -- * used in multiple locations
  3100.         ------------------------------------------------------------------------
  3101.         function luaK:freeexp(fs, e)
  3102.           if e.k == "VNONRELOC" then
  3103.             self:freereg(fs, e.info)
  3104.           end
  3105.         end
  3106.        
  3107.         ------------------------------------------------------------------------
  3108.         -- * TODO NOTE implementation is not 100% correct, since the assert fails
  3109.         -- * luaH_set, setobj deleted; direct table access used instead
  3110.         -- * used in luaK:stringK(), luaK:numberK(), luaK:boolK(), luaK:nilK()
  3111.         ------------------------------------------------------------------------
  3112.         function luaK:addk(fs, k, v)
  3113.           local L = fs.L
  3114.           local idx = fs.h[k.value]
  3115.           --TValue *idx = luaH_set(L, fs->h, k); /* C */
  3116.           local f = fs.f
  3117.           if self:ttisnumber(idx) then
  3118.             --TODO this assert currently FAILS (last tested for 5.0.2)
  3119.             --assert(fs.f.k[self:nvalue(idx)] == v)
  3120.             --assert(luaO_rawequalObj(&fs->f->k[cast_int(nvalue(idx))], v)); /* C */
  3121.             return self:nvalue(idx)
  3122.           else -- constant not found; create a new entry
  3123.             idx = {}
  3124.             self:setnvalue(idx, fs.nk)
  3125.             fs.h[k.value] = idx
  3126.             -- setnvalue(idx, cast_num(fs->nk)); /* C */
  3127.             luaY:growvector(L, f.k, fs.nk, f.sizek, nil,
  3128.                             luaP.MAXARG_Bx, "constant table overflow")
  3129.             -- loop to initialize empty f.k positions not required
  3130.             f.k[fs.nk] = v
  3131.             -- setobj(L, &f->k[fs->nk], v); /* C */
  3132.             -- luaC_barrier(L, f, v); /* GC */
  3133.             local nk = fs.nk
  3134.             fs.nk = fs.nk + 1
  3135.             return nk
  3136.           end
  3137.        
  3138.         end
  3139.        
  3140.         ------------------------------------------------------------------------
  3141.         -- creates and sets a string object
  3142.         -- * used in (lparser) luaY:codestring(), luaY:singlevar()
  3143.         ------------------------------------------------------------------------
  3144.         function luaK:stringK(fs, s)
  3145.           local o = {}  -- TValue
  3146.           self:setsvalue(o, s)
  3147.           return self:addk(fs, o, o)
  3148.         end
  3149.        
  3150.         ------------------------------------------------------------------------
  3151.         -- creates and sets a number object
  3152.         -- * used in luaK:prefix() for negative (or negation of) numbers
  3153.         -- * used in (lparser) luaY:simpleexp(), luaY:fornum()
  3154.         ------------------------------------------------------------------------
  3155.         function luaK:numberK(fs, r)
  3156.           local o = {}  -- TValue
  3157.           self:setnvalue(o, r)
  3158.           return self:addk(fs, o, o)
  3159.         end
  3160.        
  3161.         ------------------------------------------------------------------------
  3162.         -- creates and sets a boolean object
  3163.         -- * used only in luaK:exp2RK()
  3164.         ------------------------------------------------------------------------
  3165.         function luaK:boolK(fs, b)
  3166.           local o = {}  -- TValue
  3167.           self:setbvalue(o, b)
  3168.           return self:addk(fs, o, o)
  3169.         end
  3170.        
  3171.         ------------------------------------------------------------------------
  3172.         -- creates and sets a nil object
  3173.         -- * used only in luaK:exp2RK()
  3174.         ------------------------------------------------------------------------
  3175.         function luaK:nilK(fs)
  3176.           local k, v = {}, {}  -- TValue
  3177.           self:setnilvalue(v)
  3178.           -- cannot use nil as key; instead use table itself to represent nil
  3179.           self:sethvalue(k, fs.h)
  3180.           return self:addk(fs, k, v)
  3181.         end
  3182.        
  3183.         ------------------------------------------------------------------------
  3184.         --
  3185.         -- * used in luaK:setmultret(), (lparser) luaY:adjust_assign()
  3186.         ------------------------------------------------------------------------
  3187.         function luaK:setreturns(fs, e, nresults)
  3188.           if e.k == "VCALL" then  -- expression is an open function call?
  3189.             luaP:SETARG_C(self:getcode(fs, e), nresults + 1)
  3190.           elseif e.k == "VVARARG" then
  3191.             luaP:SETARG_B(self:getcode(fs, e), nresults + 1);
  3192.             luaP:SETARG_A(self:getcode(fs, e), fs.freereg);
  3193.             luaK:reserveregs(fs, 1)
  3194.           end
  3195.         end
  3196.        
  3197.         ------------------------------------------------------------------------
  3198.         --
  3199.         -- * used in luaK:dischargevars(), (lparser) luaY:assignment()
  3200.         ------------------------------------------------------------------------
  3201.         function luaK:setoneret(fs, e)
  3202.           if e.k == "VCALL" then  -- expression is an open function call?
  3203.             e.k = "VNONRELOC"
  3204.             e.info = luaP:GETARG_A(self:getcode(fs, e))
  3205.           elseif e.k == "VVARARG" then
  3206.             luaP:SETARG_B(self:getcode(fs, e), 2)
  3207.             e.k = "VRELOCABLE"  -- can relocate its simple result
  3208.           end
  3209.         end
  3210.        
  3211.         ------------------------------------------------------------------------
  3212.         --
  3213.         -- * used in multiple locations
  3214.         ------------------------------------------------------------------------
  3215.         function luaK:dischargevars(fs, e)
  3216.           local k = e.k
  3217.           if k == "VLOCAL" then
  3218.             e.k = "VNONRELOC"
  3219.           elseif k == "VUPVAL" then
  3220.             e.info = self:codeABC(fs, "OP_GETUPVAL", 0, e.info, 0)
  3221.             e.k = "VRELOCABLE"
  3222.           elseif k == "VGLOBAL" then
  3223.             e.info = self:codeABx(fs, "OP_GETGLOBAL", 0, e.info)
  3224.             e.k = "VRELOCABLE"
  3225.           elseif k == "VINDEXED" then
  3226.             self:freereg(fs, e.aux)
  3227.             self:freereg(fs, e.info)
  3228.             e.info = self:codeABC(fs, "OP_GETTABLE", 0, e.info, e.aux)
  3229.             e.k = "VRELOCABLE"
  3230.           elseif k == "VVARARG" or k == "VCALL" then
  3231.             self:setoneret(fs, e)
  3232.           else
  3233.             -- there is one value available (somewhere)
  3234.           end
  3235.         end
  3236.        
  3237.         ------------------------------------------------------------------------
  3238.         --
  3239.         -- * used only in luaK:exp2reg()
  3240.         ------------------------------------------------------------------------
  3241.         function luaK:code_label(fs, A, b, jump)
  3242.           self:getlabel(fs)  -- those instructions may be jump targets
  3243.           return self:codeABC(fs, "OP_LOADBOOL", A, b, jump)
  3244.         end
  3245.        
  3246.         ------------------------------------------------------------------------
  3247.         --
  3248.         -- * used in luaK:discharge2anyreg(), luaK:exp2reg()
  3249.         ------------------------------------------------------------------------
  3250.         function luaK:discharge2reg(fs, e, reg)
  3251.           self:dischargevars(fs, e)
  3252.           local k = e.k
  3253.           if k == "VNIL" then
  3254.             self:_nil(fs, reg, 1)
  3255.           elseif k == "VFALSE" or k == "VTRUE" then
  3256.             self:codeABC(fs, "OP_LOADBOOL", reg, (e.k == "VTRUE") and 1 or 0, 0)
  3257.           elseif k == "VK" then
  3258.             self:codeABx(fs, "OP_LOADK", reg, e.info)
  3259.           elseif k == "VKNUM" then
  3260.             self:codeABx(fs, "OP_LOADK", reg, self:numberK(fs, e.nval))
  3261.           elseif k == "VRELOCABLE" then
  3262.             local pc = self:getcode(fs, e)
  3263.             luaP:SETARG_A(pc, reg)
  3264.           elseif k == "VNONRELOC" then
  3265.             if reg ~= e.info then
  3266.               self:codeABC(fs, "OP_MOVE", reg, e.info, 0)
  3267.             end
  3268.           else
  3269.             assert(e.k == "VVOID" or e.k == "VJMP")
  3270.             return  -- nothing to do...
  3271.           end
  3272.           e.info = reg
  3273.           e.k = "VNONRELOC"
  3274.         end
  3275.        
  3276.         ------------------------------------------------------------------------
  3277.         --
  3278.         -- * used in luaK:jumponcond(), luaK:codenot()
  3279.         ------------------------------------------------------------------------
  3280.         function luaK:discharge2anyreg(fs, e)
  3281.           if e.k ~= "VNONRELOC" then
  3282.             self:reserveregs(fs, 1)
  3283.             self:discharge2reg(fs, e, fs.freereg - 1)
  3284.           end
  3285.         end
  3286.        
  3287.         ------------------------------------------------------------------------
  3288.         --
  3289.         -- * used in luaK:exp2nextreg(), luaK:exp2anyreg(), luaK:storevar()
  3290.         ------------------------------------------------------------------------
  3291.         function luaK:exp2reg(fs, e, reg)
  3292.           self:discharge2reg(fs, e, reg)
  3293.           if e.k == "VJMP" then
  3294.             e.t = self:concat(fs, e.t, e.info)  -- put this jump in 't' list
  3295.           end
  3296.           if self:hasjumps(e) then
  3297.             local final  -- position after whole expression
  3298.             local p_f = self.NO_JUMP  -- position of an eventual LOAD false
  3299.             local p_t = self.NO_JUMP  -- position of an eventual LOAD true
  3300.             if self:need_value(fs, e.t) or self:need_value(fs, e.f) then
  3301.               local fj = (e.k == "VJMP") and self.NO_JUMP or self:jump(fs)
  3302.               p_f = self:code_label(fs, reg, 0, 1)
  3303.               p_t = self:code_label(fs, reg, 1, 0)
  3304.               self:patchtohere(fs, fj)
  3305.             end
  3306.             final = self:getlabel(fs)
  3307.             self:patchlistaux(fs, e.f, final, reg, p_f)
  3308.             self:patchlistaux(fs, e.t, final, reg, p_t)
  3309.           end
  3310.           e.f, e.t = self.NO_JUMP, self.NO_JUMP
  3311.           e.info = reg
  3312.           e.k = "VNONRELOC"
  3313.         end
  3314.        
  3315.         ------------------------------------------------------------------------
  3316.         --
  3317.         -- * used in multiple locations
  3318.         ------------------------------------------------------------------------
  3319.         function luaK:exp2nextreg(fs, e)
  3320.           self:dischargevars(fs, e)
  3321.           self:freeexp(fs, e)
  3322.           self:reserveregs(fs, 1)
  3323.           self:exp2reg(fs, e, fs.freereg - 1)
  3324.         end
  3325.        
  3326.         ------------------------------------------------------------------------
  3327.         --
  3328.         -- * used in multiple locations
  3329.         ------------------------------------------------------------------------
  3330.         function luaK:exp2anyreg(fs, e)
  3331.           self:dischargevars(fs, e)
  3332.           if e.k == "VNONRELOC" then
  3333.             if not self:hasjumps(e) then  -- exp is already in a register
  3334.               return e.info
  3335.             end
  3336.             if e.info >= fs.nactvar then  -- reg. is not a local?
  3337.               self:exp2reg(fs, e, e.info)  -- put value on it
  3338.               return e.info
  3339.             end
  3340.           end
  3341.           self:exp2nextreg(fs, e)  -- default
  3342.           return e.info
  3343.         end
  3344.        
  3345.         ------------------------------------------------------------------------
  3346.         --
  3347.         -- * used in luaK:exp2RK(), luaK:prefix(), luaK:posfix()
  3348.         -- * used in (lparser) luaY:yindex()
  3349.         ------------------------------------------------------------------------
  3350.         function luaK:exp2val(fs, e)
  3351.           if self:hasjumps(e) then
  3352.             self:exp2anyreg(fs, e)
  3353.           else
  3354.             self:dischargevars(fs, e)
  3355.           end
  3356.         end
  3357.        
  3358.         ------------------------------------------------------------------------
  3359.         --
  3360.         -- * used in multiple locations
  3361.         ------------------------------------------------------------------------
  3362.         function luaK:exp2RK(fs, e)
  3363.           self:exp2val(fs, e)
  3364.           local k = e.k
  3365.           if k == "VKNUM" or k == "VTRUE" or k == "VFALSE" or k == "VNIL" then
  3366.             if fs.nk <= luaP.MAXINDEXRK then  -- constant fit in RK operand?
  3367.               -- converted from a 2-deep ternary operator expression
  3368.               if e.k == "VNIL" then
  3369.                 e.info = self:nilK(fs)
  3370.               else
  3371.                 e.info = (e.k == "VKNUM") and self:numberK(fs, e.nval)
  3372.                                           or self:boolK(fs, e.k == "VTRUE")
  3373.               end
  3374.               e.k = "VK"
  3375.               return luaP:RKASK(e.info)
  3376.             end
  3377.           elseif k == "VK" then
  3378.             if e.info <= luaP.MAXINDEXRK then  -- constant fit in argC?
  3379.               return luaP:RKASK(e.info)
  3380.             end
  3381.           else
  3382.             -- default
  3383.           end
  3384.           -- not a constant in the right range: put it in a register
  3385.           return self:exp2anyreg(fs, e)
  3386.         end
  3387.        
  3388.         ------------------------------------------------------------------------
  3389.         --
  3390.         -- * used in (lparser) luaY:assignment(), luaY:localfunc(), luaY:funcstat()
  3391.         ------------------------------------------------------------------------
  3392.         function luaK:storevar(fs, var, ex)
  3393.           local k = var.k
  3394.           if k == "VLOCAL" then
  3395.             self:freeexp(fs, ex)
  3396.             self:exp2reg(fs, ex, var.info)
  3397.             return
  3398.           elseif k == "VUPVAL" then
  3399.             local e = self:exp2anyreg(fs, ex)
  3400.             self:codeABC(fs, "OP_SETUPVAL", e, var.info, 0)
  3401.           elseif k == "VGLOBAL" then
  3402.             local e = self:exp2anyreg(fs, ex)
  3403.             self:codeABx(fs, "OP_SETGLOBAL", e, var.info)
  3404.           elseif k == "VINDEXED" then
  3405.             local e = self:exp2RK(fs, ex)
  3406.             self:codeABC(fs, "OP_SETTABLE", var.info, var.aux, e)
  3407.           else
  3408.             assert(0)  -- invalid var kind to store
  3409.           end
  3410.           self:freeexp(fs, ex)
  3411.         end
  3412.        
  3413.         ------------------------------------------------------------------------
  3414.         --
  3415.         -- * used only in (lparser) luaY:primaryexp()
  3416.         ------------------------------------------------------------------------
  3417.         function luaK:_self(fs, e, key)
  3418.           self:exp2anyreg(fs, e)
  3419.           self:freeexp(fs, e)
  3420.           local func = fs.freereg
  3421.           self:reserveregs(fs, 2)
  3422.           self:codeABC(fs, "OP_SELF", func, e.info, self:exp2RK(fs, key))
  3423.           self:freeexp(fs, key)
  3424.           e.info = func
  3425.           e.k = "VNONRELOC"
  3426.         end
  3427.        
  3428.         ------------------------------------------------------------------------
  3429.         --
  3430.         -- * used in luaK:goiftrue(), luaK:codenot()
  3431.         ------------------------------------------------------------------------
  3432.         function luaK:invertjump(fs, e)
  3433.           local pc = self:getjumpcontrol(fs, e.info)
  3434.           assert(luaP:testTMode(luaP:GET_OPCODE(pc)) ~= 0 and
  3435.                      luaP:GET_OPCODE(pc) ~= "OP_TESTSET" and
  3436.                      luaP:GET_OPCODE(pc) ~= "OP_TEST")
  3437.           luaP:SETARG_A(pc, (luaP:GETARG_A(pc) == 0) and 1 or 0)
  3438.         end
  3439.        
  3440.         ------------------------------------------------------------------------
  3441.         --
  3442.         -- * used in luaK:goiftrue(), luaK:goiffalse()
  3443.         ------------------------------------------------------------------------
  3444.         function luaK:jumponcond(fs, e, cond)
  3445.           if e.k == "VRELOCABLE" then
  3446.             local ie = self:getcode(fs, e)
  3447.             if luaP:GET_OPCODE(ie) == "OP_NOT" then
  3448.               fs.pc = fs.pc - 1  -- remove previous OP_NOT
  3449.               return self:condjump(fs, "OP_TEST", luaP:GETARG_B(ie), 0, cond and 0 or 1)
  3450.             end
  3451.             -- else go through
  3452.           end
  3453.           self:discharge2anyreg(fs, e)
  3454.           self:freeexp(fs, e)
  3455.           return self:condjump(fs, "OP_TESTSET", luaP.NO_REG, e.info, cond and 1 or 0)
  3456.         end
  3457.        
  3458.         ------------------------------------------------------------------------
  3459.         --
  3460.         -- * used in luaK:infix(), (lparser) luaY:cond()
  3461.         ------------------------------------------------------------------------
  3462.         function luaK:goiftrue(fs, e)
  3463.           local pc  -- pc of last jump
  3464.           self:dischargevars(fs, e)
  3465.           local k = e.k
  3466.           if k == "VK" or k == "VKNUM" or k == "VTRUE" then
  3467.             pc = self.NO_JUMP  -- always true; do nothing
  3468.           elseif k == "VFALSE" then
  3469.             pc = self:jump(fs)  -- always jump
  3470.           elseif k == "VJMP" then
  3471.             self:invertjump(fs, e)
  3472.             pc = e.info
  3473.           else
  3474.             pc = self:jumponcond(fs, e, false)
  3475.           end
  3476.           e.f = self:concat(fs, e.f, pc)  -- insert last jump in `f' list
  3477.           self:patchtohere(fs, e.t)
  3478.           e.t = self.NO_JUMP
  3479.         end
  3480.        
  3481.         ------------------------------------------------------------------------
  3482.         --
  3483.         -- * used in luaK:infix()
  3484.         ------------------------------------------------------------------------
  3485.         function luaK:goiffalse(fs, e)
  3486.           local pc  -- pc of last jump
  3487.           self:dischargevars(fs, e)
  3488.           local k = e.k
  3489.           if k == "VNIL" or k == "VFALSE"then
  3490.             pc = self.NO_JUMP  -- always false; do nothing
  3491.           elseif k == "VTRUE" then
  3492.             pc = self:jump(fs)  -- always jump
  3493.           elseif k == "VJMP" then
  3494.             pc = e.info
  3495.           else
  3496.             pc = self:jumponcond(fs, e, true)
  3497.           end
  3498.           e.t = self:concat(fs, e.t, pc)  -- insert last jump in `t' list
  3499.           self:patchtohere(fs, e.f)
  3500.           e.f = self.NO_JUMP
  3501.         end
  3502.        
  3503.         ------------------------------------------------------------------------
  3504.         --
  3505.         -- * used only in luaK:prefix()
  3506.         ------------------------------------------------------------------------
  3507.         function luaK:codenot(fs, e)
  3508.           self:dischargevars(fs, e)
  3509.           local k = e.k
  3510.           if k == "VNIL" or k == "VFALSE" then
  3511.             e.k = "VTRUE"
  3512.           elseif k == "VK" or k == "VKNUM" or k == "VTRUE" then
  3513.             e.k = "VFALSE"
  3514.           elseif k == "VJMP" then
  3515.             self:invertjump(fs, e)
  3516.           elseif k == "VRELOCABLE" or k == "VNONRELOC" then
  3517.             self:discharge2anyreg(fs, e)
  3518.             self:freeexp(fs, e)
  3519.             e.info = self:codeABC(fs, "OP_NOT", 0, e.info, 0)
  3520.             e.k = "VRELOCABLE"
  3521.           else
  3522.             assert(0)  -- cannot happen
  3523.           end
  3524.           -- interchange true and false lists
  3525.           e.f, e.t = e.t, e.f
  3526.           self:removevalues(fs, e.f)
  3527.           self:removevalues(fs, e.t)
  3528.         end
  3529.        
  3530.         ------------------------------------------------------------------------
  3531.         --
  3532.         -- * used in (lparser) luaY:field(), luaY:primaryexp()
  3533.         ------------------------------------------------------------------------
  3534.         function luaK:indexed(fs, t, k)
  3535.           t.aux = self:exp2RK(fs, k)
  3536.           t.k = "VINDEXED"
  3537.         end
  3538.        
  3539.         ------------------------------------------------------------------------
  3540.         --
  3541.         -- * used only in luaK:codearith()
  3542.         ------------------------------------------------------------------------
  3543.         function luaK:constfolding(op, e1, e2)
  3544.           local r
  3545.           if not self:isnumeral(e1) or not self:isnumeral(e2) then return false end
  3546.           local v1 = e1.nval
  3547.           local v2 = e2.nval
  3548.           if op == "OP_ADD" then
  3549.             r = self:numadd(v1, v2)
  3550.           elseif op == "OP_SUB" then
  3551.             r = self:numsub(v1, v2)
  3552.           elseif op == "OP_MUL" then
  3553.             r = self:nummul(v1, v2)
  3554.           elseif op == "OP_DIV" then
  3555.             if v2 == 0 then return false end  -- do not attempt to divide by 0
  3556.             r = self:numdiv(v1, v2)
  3557.           elseif op == "OP_MOD" then
  3558.             if v2 == 0 then return false end  -- do not attempt to divide by 0
  3559.             r = self:nummod(v1, v2)
  3560.           elseif op == "OP_POW" then
  3561.             r = self:numpow(v1, v2)
  3562.           elseif op == "OP_UNM" then
  3563.             r = self:numunm(v1)
  3564.           elseif op == "OP_LEN" then
  3565.             return false  -- no constant folding for 'len'
  3566.           else
  3567.             assert(0)
  3568.             r = 0
  3569.           end
  3570.           if self:numisnan(r) then return false end  -- do not attempt to produce NaN
  3571.           e1.nval = r
  3572.           return true
  3573.         end
  3574.        
  3575.         ------------------------------------------------------------------------
  3576.         --
  3577.         -- * used in luaK:prefix(), luaK:posfix()
  3578.         ------------------------------------------------------------------------
  3579.         function luaK:codearith(fs, op, e1, e2)
  3580.           if self:constfolding(op, e1, e2) then
  3581.             return
  3582.           else
  3583.             local o2 = (op ~= "OP_UNM" and op ~= "OP_LEN") and self:exp2RK(fs, e2) or 0
  3584.             local o1 = self:exp2RK(fs, e1)
  3585.             if o1 > o2 then
  3586.               self:freeexp(fs, e1)
  3587.               self:freeexp(fs, e2)
  3588.             else
  3589.               self:freeexp(fs, e2)
  3590.               self:freeexp(fs, e1)
  3591.             end
  3592.             e1.info = self:codeABC(fs, op, 0, o1, o2)
  3593.             e1.k = "VRELOCABLE"
  3594.           end
  3595.         end
  3596.        
  3597.         ------------------------------------------------------------------------
  3598.         --
  3599.         -- * used only in luaK:posfix()
  3600.         ------------------------------------------------------------------------
  3601.         function luaK:codecomp(fs, op, cond, e1, e2)
  3602.           local o1 = self:exp2RK(fs, e1)
  3603.           local o2 = self:exp2RK(fs, e2)
  3604.           self:freeexp(fs, e2)
  3605.           self:freeexp(fs, e1)
  3606.           if cond == 0 and op ~= "OP_EQ" then
  3607.             -- exchange args to replace by `<' or `<='
  3608.             o1, o2 = o2, o1  -- o1 <==> o2
  3609.             cond = 1
  3610.           end
  3611.           e1.info = self:condjump(fs, op, cond, o1, o2)
  3612.           e1.k = "VJMP"
  3613.         end
  3614.        
  3615.         ------------------------------------------------------------------------
  3616.         --
  3617.         -- * used only in (lparser) luaY:subexpr()
  3618.         ------------------------------------------------------------------------
  3619.         function luaK:prefix(fs, op, e)
  3620.           local e2 = {}  -- expdesc
  3621.           e2.t, e2.f = self.NO_JUMP, self.NO_JUMP
  3622.           e2.k = "VKNUM"
  3623.           e2.nval = 0
  3624.           if op == "OPR_MINUS" then
  3625.             if not self:isnumeral(e) then
  3626.               self:exp2anyreg(fs, e)  -- cannot operate on non-numeric constants
  3627.             end
  3628.             self:codearith(fs, "OP_UNM", e, e2)
  3629.           elseif op == "OPR_NOT" then
  3630.             self:codenot(fs, e)
  3631.           elseif op == "OPR_LEN" then
  3632.             self:exp2anyreg(fs, e)  -- cannot operate on constants
  3633.             self:codearith(fs, "OP_LEN", e, e2)
  3634.           else
  3635.             assert(0)
  3636.           end
  3637.         end
  3638.        
  3639.         ------------------------------------------------------------------------
  3640.         --
  3641.         -- * used only in (lparser) luaY:subexpr()
  3642.         ------------------------------------------------------------------------
  3643.         function luaK:infix(fs, op, v)
  3644.           if op == "OPR_AND" then
  3645.             self:goiftrue(fs, v)
  3646.           elseif op == "OPR_OR" then
  3647.             self:goiffalse(fs, v)
  3648.           elseif op == "OPR_CONCAT" then
  3649.             self:exp2nextreg(fs, v)  -- operand must be on the 'stack'
  3650.           elseif op == "OPR_ADD" or op == "OPR_SUB" or
  3651.                  op == "OPR_MUL" or op == "OPR_DIV" or
  3652.                  op == "OPR_MOD" or op == "OPR_POW" then
  3653.             if not self:isnumeral(v) then self:exp2RK(fs, v) end
  3654.           else
  3655.             self:exp2RK(fs, v)
  3656.           end
  3657.         end
  3658.        
  3659.         ------------------------------------------------------------------------
  3660.         --
  3661.         -- * used only in (lparser) luaY:subexpr()
  3662.         ------------------------------------------------------------------------
  3663.         -- table lookups to simplify testing
  3664.         luaK.arith_op = {
  3665.           OPR_ADD = "OP_ADD", OPR_SUB = "OP_SUB", OPR_MUL = "OP_MUL",
  3666.           OPR_DIV = "OP_DIV", OPR_MOD = "OP_MOD", OPR_POW = "OP_POW",
  3667.         }
  3668.         luaK.comp_op = {
  3669.           OPR_EQ = "OP_EQ", OPR_NE = "OP_EQ", OPR_LT = "OP_LT",
  3670.           OPR_LE = "OP_LE", OPR_GT = "OP_LT", OPR_GE = "OP_LE",
  3671.         }
  3672.         luaK.comp_cond = {
  3673.           OPR_EQ = 1, OPR_NE = 0, OPR_LT = 1,
  3674.           OPR_LE = 1, OPR_GT = 0, OPR_GE = 0,
  3675.         }
  3676.         function luaK:posfix(fs, op, e1, e2)
  3677.           -- needed because e1 = e2 doesn't copy values...
  3678.           -- * in 5.0.x, only k/info/aux/t/f copied, t for AND, f for OR
  3679.           --   but here, all elements are copied for completeness' sake
  3680.           local function copyexp(e1, e2)
  3681.             e1.k = e2.k
  3682.             e1.info = e2.info; e1.aux = e2.aux
  3683.             e1.nval = e2.nval
  3684.             e1.t = e2.t; e1.f = e2.f
  3685.           end
  3686.           if op == "OPR_AND" then
  3687.             assert(e1.t == self.NO_JUMP)  -- list must be closed
  3688.             self:dischargevars(fs, e2)
  3689.             e2.f = self:concat(fs, e2.f, e1.f)
  3690.             copyexp(e1, e2)
  3691.           elseif op == "OPR_OR" then
  3692.             assert(e1.f == self.NO_JUMP)  -- list must be closed
  3693.             self:dischargevars(fs, e2)
  3694.             e2.t = self:concat(fs, e2.t, e1.t)
  3695.             copyexp(e1, e2)
  3696.           elseif op == "OPR_CONCAT" then
  3697.             self:exp2val(fs, e2)
  3698.             if e2.k == "VRELOCABLE" and luaP:GET_OPCODE(self:getcode(fs, e2)) == "OP_CONCAT" then
  3699.               assert(e1.info == luaP:GETARG_B(self:getcode(fs, e2)) - 1)
  3700.               self:freeexp(fs, e1)
  3701.               luaP:SETARG_B(self:getcode(fs, e2), e1.info)
  3702.               e1.k = "VRELOCABLE"
  3703.               e1.info = e2.info
  3704.             else
  3705.               self:exp2nextreg(fs, e2)  -- operand must be on the 'stack'
  3706.               self:codearith(fs, "OP_CONCAT", e1, e2)
  3707.             end
  3708.           else
  3709.             -- the following uses a table lookup in place of conditionals
  3710.             local arith = self.arith_op[op]
  3711.             if arith then
  3712.               self:codearith(fs, arith, e1, e2)
  3713.             else
  3714.               local comp = self.comp_op[op]
  3715.               if comp then
  3716.                 self:codecomp(fs, comp, self.comp_cond[op], e1, e2)
  3717.               else
  3718.                 assert(0)
  3719.               end
  3720.             end--if arith
  3721.           end--if op
  3722.         end
  3723.        
  3724.         ------------------------------------------------------------------------
  3725.         -- adjusts debug information for last instruction written, in order to
  3726.         -- change the line where item comes into existence
  3727.         -- * used in (lparser) luaY:funcargs(), luaY:forbody(), luaY:funcstat()
  3728.         ------------------------------------------------------------------------
  3729.         function luaK:fixline(fs, line)
  3730.           fs.f.lineinfo[fs.pc - 1] = line
  3731.         end
  3732.        
  3733.         ------------------------------------------------------------------------
  3734.         -- general function to write an instruction into the instruction buffer,
  3735.         -- sets debug information too
  3736.         -- * used in luaK:codeABC(), luaK:codeABx()
  3737.         -- * called directly by (lparser) luaY:whilestat()
  3738.         ------------------------------------------------------------------------
  3739.         function luaK:code(fs, i, line)
  3740.           local f = fs.f
  3741.           self:dischargejpc(fs)  -- 'pc' will change
  3742.           -- put new instruction in code array
  3743.           luaY:growvector(fs.L, f.code, fs.pc, f.sizecode, nil,
  3744.                           luaY.MAX_INT, "code size overflow")
  3745.           f.code[fs.pc] = i
  3746.           -- save corresponding line information
  3747.           luaY:growvector(fs.L, f.lineinfo, fs.pc, f.sizelineinfo, nil,
  3748.                           luaY.MAX_INT, "code size overflow")
  3749.           f.lineinfo[fs.pc] = line
  3750.           local pc = fs.pc
  3751.           fs.pc = fs.pc + 1
  3752.           return pc
  3753.         end
  3754.        
  3755.         ------------------------------------------------------------------------
  3756.         -- writes an instruction of type ABC
  3757.         -- * calls luaK:code()
  3758.         ------------------------------------------------------------------------
  3759.         function luaK:codeABC(fs, o, a, b, c)
  3760.           assert(luaP:getOpMode(o) == luaP.OpMode.iABC)
  3761.           assert(luaP:getBMode(o) ~= luaP.OpArgMask.OpArgN or b == 0)
  3762.           assert(luaP:getCMode(o) ~= luaP.OpArgMask.OpArgN or c == 0)
  3763.           return self:code(fs, luaP:CREATE_ABC(o, a, b, c), fs.ls.lastline)
  3764.         end
  3765.        
  3766.         ------------------------------------------------------------------------
  3767.         -- writes an instruction of type ABx
  3768.         -- * calls luaK:code(), called by luaK:codeAsBx()
  3769.         ------------------------------------------------------------------------
  3770.         function luaK:codeABx(fs, o, a, bc)
  3771.           assert(luaP:getOpMode(o) == luaP.OpMode.iABx or
  3772.                      luaP:getOpMode(o) == luaP.OpMode.iAsBx)
  3773.           assert(luaP:getCMode(o) == luaP.OpArgMask.OpArgN)
  3774.           return self:code(fs, luaP:CREATE_ABx(o, a, bc), fs.ls.lastline)
  3775.         end
  3776.        
  3777.         ------------------------------------------------------------------------
  3778.         --
  3779.         -- * used in (lparser) luaY:closelistfield(), luaY:lastlistfield()
  3780.         ------------------------------------------------------------------------
  3781.         function luaK:setlist(fs, base, nelems, tostore)
  3782.           local c = math.floor((nelems - 1)/luaP.LFIELDS_PER_FLUSH) + 1
  3783.           local b = (tostore == luaY.LUA_MULTRET) and 0 or tostore
  3784.           assert(tostore ~= 0)
  3785.           if c <= luaP.MAXARG_C then
  3786.             self:codeABC(fs, "OP_SETLIST", base, b, c)
  3787.           else
  3788.             self:codeABC(fs, "OP_SETLIST", base, b, 0)
  3789.             self:code(fs, luaP:CREATE_Inst(c), fs.ls.lastline)
  3790.           end
  3791.           fs.freereg = base + 1  -- free registers with list values
  3792.         end
  3793.        
  3794.         return function(a) luaY = a return luaK end
  3795.     end
  3796.     fake_module_scripts[script] = module_script
  3797. end
  3798. do -- nil.LuaU
  3799.     local script = Instance.new('ModuleScript', nil)
  3800.     script.Name = "LuaU"
  3801.     local function module_script()
  3802.         --[[--------------------------------------------------------------------
  3803.        
  3804.           ldump.lua
  3805.           Save precompiled Lua chunks
  3806.           This file is part of Yueliang.
  3807.        
  3808.           Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
  3809.           The COPYRIGHT file describes the conditions
  3810.           under which this software may be distributed.
  3811.        
  3812.           See the ChangeLog for more information.
  3813.        
  3814.         ----------------------------------------------------------------------]]
  3815.        
  3816.         --[[--------------------------------------------------------------------
  3817.         -- Notes:
  3818.         -- * WARNING! byte order (little endian) and data type sizes for header
  3819.         --   signature values hard-coded; see luaU:header
  3820.         -- * chunk writer generators are included, see below
  3821.         -- * one significant difference is that instructions are still in table
  3822.         --   form (with OP/A/B/C/Bx fields) and luaP:Instruction() is needed to
  3823.         --   convert them into 4-char strings
  3824.         --
  3825.         -- Not implemented:
  3826.         -- * DumpVar, DumpMem has been removed
  3827.         -- * DumpVector folded into folded into DumpDebug, DumpCode
  3828.         --
  3829.         -- Added:
  3830.         -- * for convenience, the following two functions have been added:
  3831.         --   luaU:make_setS: create a chunk writer that writes to a string
  3832.         --   luaU:make_setF: create a chunk writer that writes to a file
  3833.         --   (lua.h contains a typedef for lua_Writer/lua_Chunkwriter, and
  3834.         --    a Lua-based implementation exists, writer() in lstrlib.c)
  3835.         -- * luaU:ttype(o) (from lobject.h)
  3836.         -- * for converting number types to its binary equivalent:
  3837.         --   luaU:from_double(x): encode double value for writing
  3838.         --   luaU:from_int(x): encode integer value for writing
  3839.         --     (error checking is limited for these conversion functions)
  3840.         --     (double conversion does not support denormals or NaNs)
  3841.         --
  3842.         -- Changed in 5.1.x:
  3843.         -- * the dumper was mostly rewritten in Lua 5.1.x, so notes on the
  3844.         --   differences between 5.0.x and 5.1.x is limited
  3845.         -- * LUAC_VERSION bumped to 0x51, LUAC_FORMAT added
  3846.         -- * developer is expected to adjust LUAC_FORMAT in order to identify
  3847.         --   non-standard binary chunk formats
  3848.         -- * header signature code is smaller, has been simplified, and is
  3849.         --   tested as a single unit; its logic is shared with the undumper
  3850.         -- * no more endian conversion, invalid endianness mean rejection
  3851.         -- * opcode field sizes are no longer exposed in the header
  3852.         -- * code moved to front of a prototype, followed by constants
  3853.         -- * debug information moved to the end of the binary chunk, and the
  3854.         --   relevant functions folded into a single function
  3855.         -- * luaU:dump returns a writer status code
  3856.         -- * chunk writer now implements status code because dumper uses it
  3857.         -- * luaU:endianness removed
  3858.         ----------------------------------------------------------------------]]
  3859.        
  3860.         --requires luaP
  3861.         local luaU = {}
  3862.         local luaP = require(script.Parent.LuaP)
  3863.        
  3864.         -- mark for precompiled code ('<esc>Lua') (from lua.h)
  3865.         luaU.LUA_SIGNATURE = "\27Lua"
  3866.        
  3867.         -- constants used by dumper (from lua.h)
  3868.         luaU.LUA_TNUMBER  = 3
  3869.         luaU.LUA_TSTRING  = 4
  3870.         luaU.LUA_TNIL     = 0
  3871.         luaU.LUA_TBOOLEAN = 1
  3872.         luaU.LUA_TNONE    = -1
  3873.        
  3874.         -- constants for header of binary files (from lundump.h)
  3875.         luaU.LUAC_VERSION    = 0x51     -- this is Lua 5.1
  3876.         luaU.LUAC_FORMAT     = 0        -- this is the official format
  3877.         luaU.LUAC_HEADERSIZE = 12       -- size of header of binary files
  3878.        
  3879.         --[[--------------------------------------------------------------------
  3880.         -- Additional functions to handle chunk writing
  3881.         -- * to use make_setS and make_setF, see test_ldump.lua elsewhere
  3882.         ----------------------------------------------------------------------]]
  3883.        
  3884.         ------------------------------------------------------------------------
  3885.         -- create a chunk writer that writes to a string
  3886.         -- * returns the writer function and a table containing the string
  3887.         -- * to get the final result, look in buff.data
  3888.         ------------------------------------------------------------------------
  3889.         function luaU:make_setS()
  3890.           local buff = {}
  3891.                 buff.data = ""
  3892.           local writer =
  3893.             function(s, buff)  -- chunk writer
  3894.               if not s then return 0 end
  3895.               buff.data = buff.data..s
  3896.               return 0
  3897.             end
  3898.           return writer, buff
  3899.         end
  3900.        
  3901.         ------------------------------------------------------------------------
  3902.         -- create a chunk writer that writes to a file
  3903.         -- * returns the writer function and a table containing the file handle
  3904.         -- * if a nil is passed, then writer should close the open file
  3905.         ------------------------------------------------------------------------
  3906.        
  3907.         --[[
  3908.         function luaU:make_setF(filename)
  3909.           local buff = {}
  3910.                 buff.h = io.open(filename, "wb")
  3911.           if not buff.h then return nil end
  3912.           local writer =
  3913.             function(s, buff)  -- chunk writer
  3914.               if not buff.h then return 0 end
  3915.               if not s then
  3916.                 if buff.h:close() then return 0 end
  3917.               else
  3918.                 if buff.h:write(s) then return 0 end
  3919.               end
  3920.               return 1
  3921.             end
  3922.           return writer, buff
  3923.         end--]]
  3924.        
  3925.         ------------------------------------------------------------------------
  3926.         -- works like the lobject.h version except that TObject used in these
  3927.         -- scripts only has a 'value' field, no 'tt' field (native types used)
  3928.         ------------------------------------------------------------------------
  3929.         function luaU:ttype(o)
  3930.           local tt = type(o.value)
  3931.           if tt == "number" then return self.LUA_TNUMBER
  3932.           elseif tt == "string" then return self.LUA_TSTRING
  3933.           elseif tt == "nil" then return self.LUA_TNIL
  3934.           elseif tt == "boolean" then return self.LUA_TBOOLEAN
  3935.           else
  3936.             return self.LUA_TNONE  -- the rest should not appear
  3937.           end
  3938.         end
  3939.        
  3940.         -----------------------------------------------------------------------
  3941.         -- converts a IEEE754 double number to an 8-byte little-endian string
  3942.         -- * luaU:from_double() and luaU:from_int() are adapted from ChunkBake
  3943.         -- * supports +/- Infinity, but not denormals or NaNs
  3944.         -----------------------------------------------------------------------
  3945.         function luaU:from_double(x)
  3946.           local function grab_byte(v)
  3947.             local c = v % 256
  3948.             return (v - c) / 256, string.char(c)
  3949.           end
  3950.           local sign = 0
  3951.           if x < 0 then sign = 1; x = -x end
  3952.           local mantissa, exponent = math.frexp(x)
  3953.           if x == 0 then -- zero
  3954.             mantissa, exponent = 0, 0
  3955.           elseif x == 1/0 then
  3956.             mantissa, exponent = 0, 2047
  3957.           else
  3958.             mantissa = (mantissa * 2 - 1) * math.ldexp(0.5, 53)
  3959.             exponent = exponent + 1022
  3960.           end
  3961.           local v, byte = "" -- convert to bytes
  3962.           x = math.floor(mantissa)
  3963.           for i = 1,6 do
  3964.             x, byte = grab_byte(x); v = v..byte -- 47:0
  3965.           end
  3966.           x, byte = grab_byte(exponent * 16 + x); v = v..byte -- 55:48
  3967.           x, byte = grab_byte(sign * 128 + x); v = v..byte -- 63:56
  3968.           return v
  3969.         end
  3970.        
  3971.         -----------------------------------------------------------------------
  3972.         -- converts a number to a little-endian 32-bit integer string
  3973.         -- * input value assumed to not overflow, can be signed/unsigned
  3974.         -----------------------------------------------------------------------
  3975.         function luaU:from_int(x)
  3976.           local v = ""
  3977.           x = math.floor(x)
  3978.           if x < 0 then x = 4294967296 + x end  -- ULONG_MAX+1
  3979.           for i = 1, 4 do
  3980.             local c = x % 256
  3981.             v = v..string.char(c); x = math.floor(x / 256)
  3982.           end
  3983.           return v
  3984.         end
  3985.        
  3986.         --[[--------------------------------------------------------------------
  3987.         -- Functions to make a binary chunk
  3988.         -- * many functions have the size parameter removed, since output is
  3989.         --   in the form of a string and some sizes are implicit or hard-coded
  3990.         ----------------------------------------------------------------------]]
  3991.        
  3992.         --[[--------------------------------------------------------------------
  3993.         -- struct DumpState:
  3994.         --   L  -- lua_State (not used in this script)
  3995.         --   writer  -- lua_Writer (chunk writer function)
  3996.         --   data  -- void* (chunk writer context or data already written)
  3997.         --   strip  -- if true, don't write any debug information
  3998.         --   status  -- if non-zero, an error has occured
  3999.         ----------------------------------------------------------------------]]
  4000.        
  4001.         ------------------------------------------------------------------------
  4002.         -- dumps a block of bytes
  4003.         -- * lua_unlock(D.L), lua_lock(D.L) unused
  4004.         ------------------------------------------------------------------------
  4005.         function luaU:DumpBlock(b, D)
  4006.           if D.status == 0 then
  4007.             -- lua_unlock(D->L);
  4008.             D.status = D.write(b, D.data)
  4009.             -- lua_lock(D->L);
  4010.           end
  4011.         end
  4012.        
  4013.         ------------------------------------------------------------------------
  4014.         -- dumps a char
  4015.         ------------------------------------------------------------------------
  4016.         function luaU:DumpChar(y, D)
  4017.           self:DumpBlock(string.char(y), D)
  4018.         end
  4019.        
  4020.         ------------------------------------------------------------------------
  4021.         -- dumps a 32-bit signed or unsigned integer (for int) (hard-coded)
  4022.         ------------------------------------------------------------------------
  4023.         function luaU:DumpInt(x, D)
  4024.           self:DumpBlock(self:from_int(x), D)
  4025.         end
  4026.        
  4027.         ------------------------------------------------------------------------
  4028.         -- dumps a lua_Number (hard-coded as a double)
  4029.         ------------------------------------------------------------------------
  4030.         function luaU:DumpNumber(x, D)
  4031.           self:DumpBlock(self:from_double(x), D)
  4032.         end
  4033.        
  4034.         ------------------------------------------------------------------------
  4035.         -- dumps a Lua string (size type is hard-coded)
  4036.         ------------------------------------------------------------------------
  4037.         function luaU:DumpString(s, D)
  4038.           if s == nil then
  4039.             self:DumpInt(0, D)
  4040.           else
  4041.             s = s.."\0"  -- include trailing '\0'
  4042.             self:DumpInt(#s, D)
  4043.             self:DumpBlock(s, D)
  4044.           end
  4045.         end
  4046.        
  4047.         ------------------------------------------------------------------------
  4048.         -- dumps instruction block from function prototype
  4049.         ------------------------------------------------------------------------
  4050.         function luaU:DumpCode(f, D)
  4051.           local n = f.sizecode
  4052.           --was DumpVector
  4053.           self:DumpInt(n, D)
  4054.           for i = 0, n - 1 do
  4055.             self:DumpBlock(luaP:Instruction(f.code[i]), D)
  4056.           end
  4057.         end
  4058.        
  4059.         ------------------------------------------------------------------------
  4060.         -- dump constant pool from function prototype
  4061.         -- * bvalue(o), nvalue(o) and rawtsvalue(o) macros removed
  4062.         ------------------------------------------------------------------------
  4063.         function luaU:DumpConstants(f, D)
  4064.           local n = f.sizek
  4065.           self:DumpInt(n, D)
  4066.           for i = 0, n - 1 do
  4067.             local o = f.k[i]  -- TValue
  4068.             local tt = self:ttype(o)
  4069.             self:DumpChar(tt, D)
  4070.             if tt == self.LUA_TNIL then
  4071.             elseif tt == self.LUA_TBOOLEAN then
  4072.               self:DumpChar(o.value and 1 or 0, D)
  4073.             elseif tt == self.LUA_TNUMBER then
  4074.               self:DumpNumber(o.value, D)
  4075.             elseif tt == self.LUA_TSTRING then
  4076.               self:DumpString(o.value, D)
  4077.             else
  4078.               --lua_assert(0)  -- cannot happen
  4079.             end
  4080.           end
  4081.           n = f.sizep
  4082.           self:DumpInt(n, D)
  4083.           for i = 0, n - 1 do
  4084.             self:DumpFunction(f.p[i], f.source, D)
  4085.           end
  4086.         end
  4087.        
  4088.         ------------------------------------------------------------------------
  4089.         -- dump debug information
  4090.         ------------------------------------------------------------------------
  4091.         function luaU:DumpDebug(f, D)
  4092.           local n
  4093.           n = D.strip and 0 or f.sizelineinfo           -- dump line information
  4094.           --was DumpVector
  4095.           self:DumpInt(n, D)
  4096.           for i = 0, n - 1 do
  4097.             self:DumpInt(f.lineinfo[i], D)
  4098.           end
  4099.           n = D.strip and 0 or f.sizelocvars            -- dump local information
  4100.           self:DumpInt(n, D)
  4101.           for i = 0, n - 1 do
  4102.             self:DumpString(f.locvars[i].varname, D)
  4103.             self:DumpInt(f.locvars[i].startpc, D)
  4104.             self:DumpInt(f.locvars[i].endpc, D)
  4105.           end
  4106.           n = D.strip and 0 or f.sizeupvalues           -- dump upvalue information
  4107.           self:DumpInt(n, D)
  4108.           for i = 0, n - 1 do
  4109.             self:DumpString(f.upvalues[i], D)
  4110.           end
  4111.         end
  4112.        
  4113.         ------------------------------------------------------------------------
  4114.         -- dump child function prototypes from function prototype
  4115.         ------------------------------------------------------------------------
  4116.         function luaU:DumpFunction(f, p, D)
  4117.           local source = f.source
  4118.           if source == p or D.strip then source = nil end
  4119.           self:DumpString(source, D)
  4120.           self:DumpInt(f.lineDefined, D)
  4121.           self:DumpInt(f.lastlinedefined, D)
  4122.           self:DumpChar(f.nups, D)
  4123.           self:DumpChar(f.numparams, D)
  4124.           self:DumpChar(f.is_vararg, D)
  4125.           self:DumpChar(f.maxstacksize, D)
  4126.           self:DumpCode(f, D)
  4127.           self:DumpConstants(f, D)
  4128.           self:DumpDebug(f, D)
  4129.         end
  4130.        
  4131.         ------------------------------------------------------------------------
  4132.         -- dump Lua header section (some sizes hard-coded)
  4133.         ------------------------------------------------------------------------
  4134.         function luaU:DumpHeader(D)
  4135.           local h = self:header()
  4136.           assert(#h == self.LUAC_HEADERSIZE) -- fixed buffer now an assert
  4137.           self:DumpBlock(h, D)
  4138.         end
  4139.        
  4140.         ------------------------------------------------------------------------
  4141.         -- make header (from lundump.c)
  4142.         -- returns the header string
  4143.         ------------------------------------------------------------------------
  4144.         function luaU:header()
  4145.          local x = 1
  4146.          return self.LUA_SIGNATURE..
  4147.                 string.char(
  4148.                   self.LUAC_VERSION,
  4149.                   self.LUAC_FORMAT,
  4150.                   x,                    -- endianness (1=little)
  4151.                   4,                    -- sizeof(int)
  4152.                   4,                    -- sizeof(size_t)
  4153.                   4,                    -- sizeof(Instruction)
  4154.                   8,                    -- sizeof(lua_Number)
  4155.                   0)                    -- is lua_Number integral?
  4156.         end
  4157.        
  4158.         ------------------------------------------------------------------------
  4159.         -- dump Lua function as precompiled chunk
  4160.         -- (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
  4161.         -- * w, data are created from make_setS, make_setF
  4162.         ------------------------------------------------------------------------
  4163.         function luaU:dump(L, f, w, data, strip)
  4164.           local D = {}  -- DumpState
  4165.           D.L = L
  4166.           D.write = w
  4167.           D.data = data
  4168.           D.strip = strip
  4169.           D.status = 0
  4170.           self:DumpHeader(D)
  4171.           self:DumpFunction(f, nil, D)
  4172.           -- added: for a chunk writer writing to a file, this final call with
  4173.           -- nil data is to indicate to the writer to close the file
  4174.           D.write(nil, D.data)
  4175.           return D.status
  4176.         end
  4177.        
  4178.         return luaU
  4179.     end
  4180.     fake_module_scripts[script] = module_script
  4181. end
  4182. do -- nil.LuaP
  4183.     local script = Instance.new('ModuleScript', nil)
  4184.     script.Name = "LuaP"
  4185.     local function module_script()
  4186.         --[[--------------------------------------------------------------------
  4187.        
  4188.           lopcodes.lua
  4189.           Lua 5 virtual machine opcodes in Lua
  4190.           This file is part of Yueliang.
  4191.        
  4192.           Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
  4193.           The COPYRIGHT file describes the conditions
  4194.           under which this software may be distributed.
  4195.        
  4196.           See the ChangeLog for more information.
  4197.        
  4198.         ----------------------------------------------------------------------]]
  4199.        
  4200.         --[[--------------------------------------------------------------------
  4201.         -- Notes:
  4202.         -- * an Instruction is a table with OP, A, B, C, Bx elements; this
  4203.         --   makes the code easy to follow and should allow instruction handling
  4204.         --   to work with doubles and ints
  4205.         -- * WARNING luaP:Instruction outputs instructions encoded in little-
  4206.         --   endian form and field size and positions are hard-coded
  4207.         --
  4208.         -- Not implemented:
  4209.         -- *
  4210.         --
  4211.         -- Added:
  4212.         -- * luaP:CREATE_Inst(c): create an inst from a number (for OP_SETLIST)
  4213.         -- * luaP:Instruction(i): convert field elements to a 4-char string
  4214.         -- * luaP:DecodeInst(x): convert 4-char string into field elements
  4215.         --
  4216.         -- Changed in 5.1.x:
  4217.         -- * POS_OP added, instruction field positions changed
  4218.         -- * some symbol names may have changed, e.g. LUAI_BITSINT
  4219.         -- * new operators for RK indices: BITRK, ISK(x), INDEXK(r), RKASK(x)
  4220.         -- * OP_MOD, OP_LEN is new
  4221.         -- * OP_TEST is now OP_TESTSET, OP_TEST is new
  4222.         -- * OP_FORLOOP, OP_TFORLOOP adjusted, OP_FORPREP is new
  4223.         -- * OP_TFORPREP deleted
  4224.         -- * OP_SETLIST and OP_SETLISTO merged and extended
  4225.         -- * OP_VARARG is new
  4226.         -- * many changes to implementation of OpMode data
  4227.         ----------------------------------------------------------------------]]
  4228.        
  4229.         local luaP = {}
  4230.        
  4231.         --[[
  4232.         ===========================================================================
  4233.           We assume that instructions are unsigned numbers.
  4234.           All instructions have an opcode in the first 6 bits.
  4235.           Instructions can have the following fields:
  4236.                 'A' : 8 bits
  4237.                 'B' : 9 bits
  4238.                 'C' : 9 bits
  4239.                 'Bx' : 18 bits ('B' and 'C' together)
  4240.                 'sBx' : signed Bx
  4241.        
  4242.           A signed argument is represented in excess K; that is, the number
  4243.           value is the unsigned value minus K. K is exactly the maximum value
  4244.           for that argument (so that -max is represented by 0, and +max is
  4245.           represented by 2*max), which is half the maximum for the corresponding
  4246.           unsigned argument.
  4247.         ===========================================================================
  4248.         --]]
  4249.        
  4250.         luaP.OpMode = { iABC = 0, iABx = 1, iAsBx = 2 }  -- basic instruction format
  4251.        
  4252.         ------------------------------------------------------------------------
  4253.         -- size and position of opcode arguments.
  4254.         -- * WARNING size and position is hard-coded elsewhere in this script
  4255.         ------------------------------------------------------------------------
  4256.         luaP.SIZE_C  = 9
  4257.         luaP.SIZE_B  = 9
  4258.         luaP.SIZE_Bx = luaP.SIZE_C + luaP.SIZE_B
  4259.         luaP.SIZE_A  = 8
  4260.        
  4261.         luaP.SIZE_OP = 6
  4262.        
  4263.         luaP.POS_OP = 0
  4264.         luaP.POS_A  = luaP.POS_OP + luaP.SIZE_OP
  4265.         luaP.POS_C  = luaP.POS_A + luaP.SIZE_A
  4266.         luaP.POS_B  = luaP.POS_C + luaP.SIZE_C
  4267.         luaP.POS_Bx = luaP.POS_C
  4268.        
  4269.         ------------------------------------------------------------------------
  4270.         -- limits for opcode arguments.
  4271.         -- we use (signed) int to manipulate most arguments,
  4272.         -- so they must fit in LUAI_BITSINT-1 bits (-1 for sign)
  4273.         ------------------------------------------------------------------------
  4274.         -- removed "#if SIZE_Bx < BITS_INT-1" test, assume this script is
  4275.         -- running on a Lua VM with double or int as LUA_NUMBER
  4276.        
  4277.         luaP.MAXARG_Bx  = math.ldexp(1, luaP.SIZE_Bx) - 1
  4278.         luaP.MAXARG_sBx = math.floor(luaP.MAXARG_Bx / 2)  -- 'sBx' is signed
  4279.        
  4280.         luaP.MAXARG_A = math.ldexp(1, luaP.SIZE_A) - 1
  4281.         luaP.MAXARG_B = math.ldexp(1, luaP.SIZE_B) - 1
  4282.         luaP.MAXARG_C = math.ldexp(1, luaP.SIZE_C) - 1
  4283.        
  4284.         -- creates a mask with 'n' 1 bits at position 'p'
  4285.         -- MASK1(n,p) deleted, not required
  4286.         -- creates a mask with 'n' 0 bits at position 'p'
  4287.         -- MASK0(n,p) deleted, not required
  4288.        
  4289.         --[[--------------------------------------------------------------------
  4290.           Visual representation for reference:
  4291.        
  4292.            31    |    |     |            0      bit position
  4293.             +-----+-----+-----+----------+
  4294.             |  B  |  C  |  A  |  Opcode  |      iABC format
  4295.             +-----+-----+-----+----------+
  4296.             -  9  -  9  -  8  -    6     -      field sizes
  4297.             +-----+-----+-----+----------+
  4298.             |   [s]Bx   |  A  |  Opcode  |      iABx | iAsBx format
  4299.             +-----+-----+-----+----------+
  4300.        
  4301.         ----------------------------------------------------------------------]]
  4302.        
  4303.         ------------------------------------------------------------------------
  4304.         -- the following macros help to manipulate instructions
  4305.         -- * changed to a table object representation, very clean compared to
  4306.         --   the [nightmare] alternatives of using a number or a string
  4307.         -- * Bx is a separate element from B and C, since there is never a need
  4308.         --   to split Bx in the parser or code generator
  4309.         ------------------------------------------------------------------------
  4310.        
  4311.         -- these accept or return opcodes in the form of string names
  4312.         function luaP:GET_OPCODE(i) return self.ROpCode[i.OP] end
  4313.         function luaP:SET_OPCODE(i, o) i.OP = self.OpCode[o] end
  4314.        
  4315.         function luaP:GETARG_A(i) return i.A end
  4316.         function luaP:SETARG_A(i, u) i.A = u end
  4317.        
  4318.         function luaP:GETARG_B(i) return i.B end
  4319.         function luaP:SETARG_B(i, b) i.B = b end
  4320.        
  4321.         function luaP:GETARG_C(i) return i.C end
  4322.         function luaP:SETARG_C(i, b) i.C = b end
  4323.        
  4324.         function luaP:GETARG_Bx(i) return i.Bx end
  4325.         function luaP:SETARG_Bx(i, b) i.Bx = b end
  4326.        
  4327.         function luaP:GETARG_sBx(i) return i.Bx - self.MAXARG_sBx end
  4328.         function luaP:SETARG_sBx(i, b) i.Bx = b + self.MAXARG_sBx end
  4329.        
  4330.         function luaP:CREATE_ABC(o,a,b,c)
  4331.           return {OP = self.OpCode[o], A = a, B = b, C = c}
  4332.         end
  4333.        
  4334.         function luaP:CREATE_ABx(o,a,bc)
  4335.           return {OP = self.OpCode[o], A = a, Bx = bc}
  4336.         end
  4337.        
  4338.         ------------------------------------------------------------------------
  4339.         -- create an instruction from a number (for OP_SETLIST)
  4340.         ------------------------------------------------------------------------
  4341.         function luaP:CREATE_Inst(c)
  4342.           local o = c % 64
  4343.           c = (c - o) / 64
  4344.           local a = c % 256
  4345.           c = (c - a) / 256
  4346.           return self:CREATE_ABx(o, a, c)
  4347.         end
  4348.        
  4349.         ------------------------------------------------------------------------
  4350.         -- returns a 4-char string little-endian encoded form of an instruction
  4351.         ------------------------------------------------------------------------
  4352.         function luaP:Instruction(i)
  4353.           if i.Bx then
  4354.             -- change to OP/A/B/C format
  4355.             i.C = i.Bx % 512
  4356.             i.B = (i.Bx - i.C) / 512
  4357.           end
  4358.           local I = i.A * 64 + i.OP
  4359.           local c0 = I % 256
  4360.           I = i.C * 64 + (I - c0) / 256  -- 6 bits of A left
  4361.           local c1 = I % 256
  4362.           I = i.B * 128 + (I - c1) / 256  -- 7 bits of C left
  4363.           local c2 = I % 256
  4364.           local c3 = (I - c2) / 256
  4365.           return string.char(c0, c1, c2, c3)
  4366.         end
  4367.        
  4368.         ------------------------------------------------------------------------
  4369.         -- decodes a 4-char little-endian string into an instruction struct
  4370.         ------------------------------------------------------------------------
  4371.         function luaP:DecodeInst(x)
  4372.           local byte = string.byte
  4373.           local i = {}
  4374.           local I = byte(x, 1)
  4375.           local op = I % 64
  4376.           i.OP = op
  4377.           I = byte(x, 2) * 4 + (I - op) / 64  -- 2 bits of c0 left
  4378.           local a = I % 256
  4379.           i.A = a
  4380.           I = byte(x, 3) * 4 + (I - a) / 256  -- 2 bits of c1 left
  4381.           local c = I % 512
  4382.           i.C = c
  4383.           i.B = byte(x, 4) * 2 + (I - c) / 512 -- 1 bits of c2 left
  4384.           local opmode = self.OpMode[tonumber(string.sub(self.opmodes[op + 1], 7, 7))]
  4385.           if opmode ~= "iABC" then
  4386.             i.Bx = i.B * 512 + i.C
  4387.           end
  4388.           return i
  4389.         end
  4390.        
  4391.         ------------------------------------------------------------------------
  4392.         -- Macros to operate RK indices
  4393.         -- * these use arithmetic instead of bit ops
  4394.         ------------------------------------------------------------------------
  4395.        
  4396.         -- this bit 1 means constant (0 means register)
  4397.         luaP.BITRK = math.ldexp(1, luaP.SIZE_B - 1)
  4398.        
  4399.         -- test whether value is a constant
  4400.         function luaP:ISK(x) return x >= self.BITRK end
  4401.        
  4402.         -- gets the index of the constant
  4403.         function luaP:INDEXK(x) return x - self.BITRK end
  4404.        
  4405.         luaP.MAXINDEXRK = luaP.BITRK - 1
  4406.        
  4407.         -- code a constant index as a RK value
  4408.         function luaP:RKASK(x) return x + self.BITRK end
  4409.        
  4410.         ------------------------------------------------------------------------
  4411.         -- invalid register that fits in 8 bits
  4412.         ------------------------------------------------------------------------
  4413.         luaP.NO_REG = luaP.MAXARG_A
  4414.        
  4415.         ------------------------------------------------------------------------
  4416.         -- R(x) - register
  4417.         -- Kst(x) - constant (in constant table)
  4418.         -- RK(x) == if ISK(x) then Kst(INDEXK(x)) else R(x)
  4419.         ------------------------------------------------------------------------
  4420.        
  4421.         ------------------------------------------------------------------------
  4422.         -- grep "ORDER OP" if you change these enums
  4423.         ------------------------------------------------------------------------
  4424.        
  4425.         --[[--------------------------------------------------------------------
  4426.         Lua virtual machine opcodes (enum OpCode):
  4427.         ------------------------------------------------------------------------
  4428.         name          args    description
  4429.         ------------------------------------------------------------------------
  4430.         OP_MOVE       A B     R(A) := R(B)
  4431.         OP_LOADK      A Bx    R(A) := Kst(Bx)
  4432.         OP_LOADBOOL   A B C   R(A) := (Bool)B; if (C) pc++
  4433.         OP_LOADNIL    A B     R(A) := ... := R(B) := nil
  4434.         OP_GETUPVAL   A B     R(A) := UpValue[B]
  4435.         OP_GETGLOBAL  A Bx    R(A) := Gbl[Kst(Bx)]
  4436.         OP_GETTABLE   A B C   R(A) := R(B)[RK(C)]
  4437.         OP_SETGLOBAL  A Bx    Gbl[Kst(Bx)] := R(A)
  4438.         OP_SETUPVAL   A B     UpValue[B] := R(A)
  4439.         OP_SETTABLE   A B C   R(A)[RK(B)] := RK(C)
  4440.         OP_NEWTABLE   A B C   R(A) := {} (size = B,C)
  4441.         OP_SELF       A B C   R(A+1) := R(B); R(A) := R(B)[RK(C)]
  4442.         OP_ADD        A B C   R(A) := RK(B) + RK(C)
  4443.         OP_SUB        A B C   R(A) := RK(B) - RK(C)
  4444.         OP_MUL        A B C   R(A) := RK(B) * RK(C)
  4445.         OP_DIV        A B C   R(A) := RK(B) / RK(C)
  4446.         OP_MOD        A B C   R(A) := RK(B) % RK(C)
  4447.         OP_POW        A B C   R(A) := RK(B) ^ RK(C)
  4448.         OP_UNM        A B     R(A) := -R(B)
  4449.         OP_NOT        A B     R(A) := not R(B)
  4450.         OP_LEN        A B     R(A) := length of R(B)
  4451.         OP_CONCAT     A B C   R(A) := R(B).. ... ..R(C)
  4452.         OP_JMP        sBx     pc+=sBx
  4453.         OP_EQ         A B C   if ((RK(B) == RK(C)) ~= A) then pc++
  4454.         OP_LT         A B C   if ((RK(B) <  RK(C)) ~= A) then pc++
  4455.         OP_LE         A B C   if ((RK(B) <= RK(C)) ~= A) then pc++
  4456.         OP_TEST       A C     if not (R(A) <=> C) then pc++
  4457.         OP_TESTSET    A B C   if (R(B) <=> C) then R(A) := R(B) else pc++
  4458.         OP_CALL       A B C   R(A), ... ,R(A+C-2) := R(A)(R(A+1), ... ,R(A+B-1))
  4459.         OP_TAILCALL   A B C   return R(A)(R(A+1), ... ,R(A+B-1))
  4460.         OP_RETURN     A B     return R(A), ... ,R(A+B-2)  (see note)
  4461.         OP_FORLOOP    A sBx   R(A)+=R(A+2);
  4462.                               if R(A) <?= R(A+1) then { pc+=sBx; R(A+3)=R(A) }
  4463.         OP_FORPREP    A sBx   R(A)-=R(A+2); pc+=sBx
  4464.         OP_TFORLOOP   A C     R(A+3), ... ,R(A+2+C) := R(A)(R(A+1), R(A+2));
  4465.                               if R(A+3) ~= nil then R(A+2)=R(A+3) else pc++
  4466.         OP_SETLIST    A B C   R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B
  4467.         OP_CLOSE      A       close all variables in the stack up to (>=) R(A)
  4468.         OP_CLOSURE    A Bx    R(A) := closure(KPROTO[Bx], R(A), ... ,R(A+n))
  4469.         OP_VARARG     A B     R(A), R(A+1), ..., R(A+B-1) = vararg
  4470.         ----------------------------------------------------------------------]]
  4471.        
  4472.         luaP.opnames = {}  -- opcode names
  4473.         luaP.OpCode = {}   -- lookup name -> number
  4474.         luaP.ROpCode = {}  -- lookup number -> name
  4475.        
  4476.         ------------------------------------------------------------------------
  4477.         -- ORDER OP
  4478.         ------------------------------------------------------------------------
  4479.         local i = 0
  4480.         for v in string.gmatch([[
  4481.         MOVE LOADK LOADBOOL LOADNIL GETUPVAL
  4482.         GETGLOBAL GETTABLE SETGLOBAL SETUPVAL SETTABLE
  4483.         NEWTABLE SELF ADD SUB MUL
  4484.         DIV MOD POW UNM NOT
  4485.         LEN CONCAT JMP EQ LT
  4486.         LE TEST TESTSET CALL TAILCALL
  4487.         RETURN FORLOOP FORPREP TFORLOOP SETLIST
  4488.         CLOSE CLOSURE VARARG
  4489.         ]], "%S+") do
  4490.           local n = "OP_"..v
  4491.           luaP.opnames[i] = v
  4492.           luaP.OpCode[n] = i
  4493.           luaP.ROpCode[i] = n
  4494.           i = i + 1
  4495.         end
  4496.         luaP.NUM_OPCODES = i
  4497.        
  4498.         --[[
  4499.         ===========================================================================
  4500.           Notes:
  4501.           (*) In OP_CALL, if (B == 0) then B = top. C is the number of returns - 1,
  4502.               and can be 0: OP_CALL then sets 'top' to last_result+1, so
  4503.               next open instruction (OP_CALL, OP_RETURN, OP_SETLIST) may use 'top'.
  4504.           (*) In OP_VARARG, if (B == 0) then use actual number of varargs and
  4505.               set top (like in OP_CALL with C == 0).
  4506.           (*) In OP_RETURN, if (B == 0) then return up to 'top'
  4507.           (*) In OP_SETLIST, if (B == 0) then B = 'top';
  4508.               if (C == 0) then next 'instruction' is real C
  4509.           (*) For comparisons, A specifies what condition the test should accept
  4510.               (true or false).
  4511.           (*) All 'skips' (pc++) assume that next instruction is a jump
  4512.         ===========================================================================
  4513.         --]]
  4514.        
  4515.         --[[--------------------------------------------------------------------
  4516.           masks for instruction properties. The format is:
  4517.           bits 0-1: op mode
  4518.           bits 2-3: C arg mode
  4519.           bits 4-5: B arg mode
  4520.           bit 6: instruction set register A
  4521.           bit 7: operator is a test
  4522.        
  4523.           for OpArgMask:
  4524.           OpArgN - argument is not used
  4525.           OpArgU - argument is used
  4526.           OpArgR - argument is a register or a jump offset
  4527.           OpArgK - argument is a constant or register/constant
  4528.         ----------------------------------------------------------------------]]
  4529.        
  4530.         -- was enum OpArgMask
  4531.         luaP.OpArgMask = { OpArgN = 0, OpArgU = 1, OpArgR = 2, OpArgK = 3 }
  4532.        
  4533.         ------------------------------------------------------------------------
  4534.         -- e.g. to compare with symbols, luaP:getOpMode(...) == luaP.OpCode.iABC
  4535.         -- * accepts opcode parameter as strings, e.g. "OP_MOVE"
  4536.         ------------------------------------------------------------------------
  4537.        
  4538.         function luaP:getOpMode(m)
  4539.           return self.opmodes[self.OpCode[m]] % 4
  4540.         end
  4541.        
  4542.         function luaP:getBMode(m)
  4543.           return math.floor(self.opmodes[self.OpCode[m]] / 16) % 4
  4544.         end
  4545.        
  4546.         function luaP:getCMode(m)
  4547.           return math.floor(self.opmodes[self.OpCode[m]] / 4) % 4
  4548.         end
  4549.        
  4550.         function luaP:testAMode(m)
  4551.           return math.floor(self.opmodes[self.OpCode[m]] / 64) % 2
  4552.         end
  4553.        
  4554.         function luaP:testTMode(m)
  4555.           return math.floor(self.opmodes[self.OpCode[m]] / 128)
  4556.         end
  4557.        
  4558.         -- luaP_opnames[] is set above, as the luaP.opnames table
  4559.        
  4560.         -- number of list items to accumulate before a SETLIST instruction
  4561.         luaP.LFIELDS_PER_FLUSH = 50
  4562.        
  4563.         ------------------------------------------------------------------------
  4564.         -- build instruction properties array
  4565.         -- * deliberately coded to look like the C equivalent
  4566.         ------------------------------------------------------------------------
  4567.         local function opmode(t, a, b, c, m)
  4568.           local luaP = luaP
  4569.           return t * 128 + a * 64 +
  4570.                  luaP.OpArgMask[b] * 16 + luaP.OpArgMask[c] * 4 + luaP.OpMode[m]
  4571.         end
  4572.        
  4573.         -- ORDER OP
  4574.         luaP.opmodes = {
  4575.         -- T A B C mode opcode
  4576.           opmode(0, 1, "OpArgK", "OpArgN", "iABx"),     -- OP_LOADK
  4577.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_LOADBOOL
  4578.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_LOADNIL
  4579.           opmode(0, 1, "OpArgU", "OpArgN", "iABC"),     -- OP_GETUPVAL
  4580.           opmode(0, 1, "OpArgK", "OpArgN", "iABx"),     -- OP_GETGLOBAL
  4581.           opmode(0, 1, "OpArgR", "OpArgK", "iABC"),     -- OP_GETTABLE
  4582.           opmode(0, 0, "OpArgK", "OpArgN", "iABx"),     -- OP_SETGLOBAL
  4583.           opmode(0, 0, "OpArgU", "OpArgN", "iABC"),     -- OP_SETUPVAL
  4584.           opmode(0, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_SETTABLE
  4585.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_NEWTABLE
  4586.           opmode(0, 1, "OpArgR", "OpArgK", "iABC"),     -- OP_SELF
  4587.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_ADD
  4588.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_SUB
  4589.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_MUL
  4590.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_DIV
  4591.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_MOD
  4592.           opmode(0, 1, "OpArgK", "OpArgK", "iABC"),     -- OP_POW
  4593.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_UNM
  4594.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_NOT
  4595.           opmode(0, 1, "OpArgR", "OpArgN", "iABC"),     -- OP_LEN
  4596.           opmode(0, 1, "OpArgR", "OpArgR", "iABC"),     -- OP_CONCAT
  4597.           opmode(0, 0, "OpArgR", "OpArgN", "iAsBx"),    -- OP_JMP
  4598.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_EQ
  4599.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_LT
  4600.           opmode(1, 0, "OpArgK", "OpArgK", "iABC"),     -- OP_LE
  4601.           opmode(1, 1, "OpArgR", "OpArgU", "iABC"),     -- OP_TEST
  4602.           opmode(1, 1, "OpArgR", "OpArgU", "iABC"),     -- OP_TESTSET
  4603.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_CALL
  4604.           opmode(0, 1, "OpArgU", "OpArgU", "iABC"),     -- OP_TAILCALL
  4605.           opmode(0, 0, "OpArgU", "OpArgN", "iABC"),     -- OP_RETURN
  4606.           opmode(0, 1, "OpArgR", "OpArgN", "iAsBx"),    -- OP_FORLOOP
  4607.           opmode(0, 1, "OpArgR", "OpArgN", "iAsBx"),    -- OP_FORPREP
  4608.           opmode(1, 0, "OpArgN", "OpArgU", "iABC"),     -- OP_TFORLOOP
  4609.           opmode(0, 0, "OpArgU", "OpArgU", "iABC"),     -- OP_SETLIST
  4610.           opmode(0, 0, "OpArgN", "OpArgN", "iABC"),     -- OP_CLOSE
  4611.           opmode(0, 1, "OpArgU", "OpArgN", "iABx"),     -- OP_CLOSURE
  4612.           opmode(0, 1, "OpArgU", "OpArgN", "iABC"),     -- OP_VARARG
  4613.         }
  4614.         -- an awkward way to set a zero-indexed table...
  4615.         luaP.opmodes[0] =
  4616.           opmode(0, 1, "OpArgR", "OpArgN", "iABC")      -- OP_MOVE
  4617.        
  4618.         return luaP
  4619.     end
  4620.     fake_module_scripts[script] = module_script
  4621. end
  4622. do -- nil.Rerubi
  4623.     local script = Instance.new('ModuleScript', nil)
  4624.     script.Name = "Rerubi"
  4625.     local function module_script()
  4626.         local Concat    = table.concat;
  4627.         local Select    = select;
  4628.         local _Byte     = string.byte;
  4629.         local Sub       = string.sub;
  4630.         local Opcode    = { -- Opcode types.
  4631.             'ABC',  'ABx',  'ABC',  'ABC';
  4632.             'ABC',  'ABx',  'ABC',  'ABx';
  4633.             'ABC',  'ABC',  'ABC',  'ABC';
  4634.             'ABC',  'ABC',  'ABC',  'ABC';
  4635.             'ABC',  'ABC',  'ABC',  'ABC';
  4636.             'ABC',  'ABC',  'AsBx', 'ABC';
  4637.             'ABC',  'ABC',  'ABC',  'ABC';
  4638.             'ABC',  'ABC',  'ABC',  'AsBx';
  4639.             'AsBx', 'ABC',  'ABC',  'ABC';
  4640.             'ABx',  'ABC';
  4641.         };
  4642.        
  4643.         -- rlbi author -> Rerumu
  4644.        
  4645.         --[[
  4646.             Features;
  4647.                 * Almost complete rework/rewrite
  4648.                 * Fast and performant
  4649.                 * Fixes to upvalues
  4650.                 * C Stack overflow fixes in opcodes
  4651.                 * Fixed missing/broken returns
  4652.                 * Numeric constant 0 is properly handled
  4653.                 * Formatted in a more readable manner
  4654.                 * Tailcalls and stack issues have been fixed
  4655.                 * CLOSE implemented
  4656.                 * SETLIST (extended) implemented
  4657.                 * VARARG fixes
  4658.         --]]
  4659.        
  4660.         local function gBit(Bit, Start, End) -- No tail-calls, yay.
  4661.             if End then -- Thanks to cntkillme for giving input on this shorter, better approach.
  4662.                 local Res   = (Bit / 2 ^ (Start - 1)) % 2 ^ ((End - 1) - (Start - 1) + 1);
  4663.        
  4664.                 return Res - Res % 1;
  4665.             else
  4666.                 local Plc = 2 ^ (Start - 1);
  4667.        
  4668.                 if (Bit % (Plc + Plc) >= Plc) then
  4669.                     return 1;
  4670.                 else
  4671.                     return 0;
  4672.                 end;
  4673.             end;
  4674.         end;
  4675.        
  4676.         local function GetMeaning(ByteString)
  4677.             local Pos   = 1;
  4678.             local gSizet;
  4679.             local gInt;
  4680.        
  4681.             local function gBits8() -- Get the next byte in the stream.
  4682.                 local F = _Byte(ByteString, Pos, Pos);
  4683.        
  4684.                 Pos = Pos + 1;
  4685.        
  4686.                 return F;
  4687.             end;
  4688.        
  4689.             local function gBits32()
  4690.                 local W, X, Y, Z    = _Byte(ByteString, Pos, Pos + 3);
  4691.        
  4692.                 Pos = Pos + 4;
  4693.        
  4694.                 return (Z * 16777216) + (Y * 65536) + (X * 256) + W;
  4695.             end;
  4696.        
  4697.             local function gBits64()
  4698.                 return gBits32() * 4294967296 + gBits32();
  4699.             end;
  4700.        
  4701.             local function gFloat()
  4702.                 local A,= gBits32(), gBits32();
  4703.        
  4704.                 if ((A + B) == 0) then
  4705.                     return 0; -- Float 0 tends to be very messy, so this is a temp fix until I figure out what's up.
  4706.                 else
  4707.                     return (-2 * gBit(B, 32) + 1) * (2 ^ (gBit(B, 21, 31) - 1023)) * ((gBit(B, 1, 20) * (2^32) + A) / (2 ^ 52) + 1);
  4708.                 end;
  4709.             end;
  4710.        
  4711.             local function gString(Len)
  4712.                 local Str;
  4713.        
  4714.                 if Len then
  4715.                     Str = Sub(ByteString, Pos, Pos + Len - 1);
  4716.        
  4717.                     Pos = Pos + Len;
  4718.                 else
  4719.                     Len = gSizet();
  4720.        
  4721.                     if (Len == 0) then return; end;
  4722.        
  4723.                     Str = Sub(ByteString, Pos, Pos + Len - 1);
  4724.        
  4725.                     Pos = Pos + Len;
  4726.                 end;
  4727.        
  4728.                 return Str;
  4729.             end;
  4730.        
  4731.             local function ChunkDecode()
  4732.                 local Instr = {};
  4733.                 local Const = {};
  4734.                 local Proto = {};
  4735.                 local Chunk = {
  4736.                     Instr   = Instr; -- Instructions
  4737.                     Const   = Const; -- Constants
  4738.                     Proto   = Proto; -- Prototypes
  4739.                     Lines   = {}; -- Lines
  4740.                     Name    = gString(); -- Grab name string.
  4741.                     FirstL  = gInt(); -- First line.
  4742.                     LastL   = gInt(); -- Last line.
  4743.                     Upvals  = gBits8(); -- Upvalue count.
  4744.                     Args    = gBits8(); -- Arg count.
  4745.                     Vargs   = gBits8(); -- Vararg type.
  4746.                     Stack   = gBits8(); -- Stack.
  4747.                 };
  4748.        
  4749.                 if Chunk.Name then
  4750.                     Chunk.Name  = Sub(Chunk.Name, 1, -2);
  4751.                 end;
  4752.        
  4753.                 for Idx = 1, gInt() do -- Loading instructions to the chunk.
  4754.                     local Data  = gBits32();
  4755.                     local Opco  = gBit(Data, 1, 6);
  4756.                     local Type  = Opcode[Opco + 1];
  4757.                     local Inst;
  4758.        
  4759.                     if Type then
  4760.                         Inst    = {
  4761.                             Enum    = Opco;
  4762.                             gBit(Data, 7, 14); -- Register A.
  4763.                         };
  4764.        
  4765.                         if (Type == 'ABC') then -- Most common, basic instruction type.
  4766.                             Inst[2] = gBit(Data, 24, 32);
  4767.                             Inst[3] = gBit(Data, 15, 23);
  4768.                         elseif (Type == 'ABx') then
  4769.                             Inst[2] = gBit(Data, 15, 32);
  4770.                         elseif (Type == 'AsBx') then
  4771.                             Inst[2] = gBit(Data, 15, 32) - 131071;
  4772.                         end;
  4773.                     else
  4774.                         Inst    = Data; -- Extended SETLIST
  4775.                     end;
  4776.        
  4777.                     Instr[Idx]  = Inst;
  4778.                 end;
  4779.        
  4780.                 for Idx = 1, gInt() do -- Load constants.
  4781.                     local Type  = gBits8();
  4782.                     local Cons;
  4783.        
  4784.                     if (Type == 1) then -- Boolean
  4785.                         Cons    = (gBits8() ~= 0);
  4786.                     elseif (Type == 3) then -- Float/Double
  4787.                         Cons    = gFloat();
  4788.                     elseif (Type == 4) then
  4789.                         Cons    = Sub(gString(), 1, -2);
  4790.                     end;
  4791.        
  4792.                     Const[Idx - 1]  = Cons;
  4793.                 end;
  4794.        
  4795.                 for Idx = 1, gInt() do -- Nested function prototypes.
  4796.                     Proto[Idx - 1]  = ChunkDecode();
  4797.                 end;
  4798.        
  4799.                 do -- Debugging
  4800.                     local Lines = Chunk.Lines;
  4801.        
  4802.                     for Idx = 1, gInt() do
  4803.                         Lines[Idx]  = gBits32();
  4804.                     end;
  4805.        
  4806.                     for Idx = 1, gInt() do -- Locals in stack.
  4807.                         gString(); -- Name of local.
  4808.                         gBits32(); -- Starting point.
  4809.                         gBits32(); -- End point.
  4810.                     end;
  4811.        
  4812.                     for Idx = 1, gInt() do -- Upvalues.
  4813.                         gString(); -- Name of upvalue.
  4814.                     end;
  4815.                 end;
  4816.        
  4817.                 return Chunk; -- Finished chunk.
  4818.             end;
  4819.        
  4820.             do -- Most of this chunk I was too lazy to reformat or change
  4821.                 assert(gString(4) == "\27Lua", "Lua bytecode expected.");
  4822.                 assert(gBits8() == 0x51, "Only Lua 5.1 is supported.");
  4823.        
  4824.                 gBits8(); -- Probably version control.
  4825.                 gBits8(); -- Is small endians.
  4826.        
  4827.                 local IntSize   = gBits8(); -- Int size
  4828.                 local Sizet     = gBits8(); -- size_t
  4829.        
  4830.                 if (IntSize == 4) then
  4831.                     gInt    = gBits32;
  4832.                 elseif (IntSize == 8) then
  4833.                     gInt    = gBits64;
  4834.                 else
  4835.                     error('Integer size not supported', 2);
  4836.                 end;
  4837.        
  4838.                 if (Sizet == 4) then
  4839.                     gSizet  = gBits32;
  4840.                 elseif (Sizet == 8) then
  4841.                     gSizet  = gBits64;
  4842.                 else
  4843.                     error('Sizet size not supported', 2);
  4844.                 end;
  4845.        
  4846.                 assert(gString(3) == "\4\8\0", "Unsupported bytecode target platform");
  4847.             end;
  4848.        
  4849.             return ChunkDecode();
  4850.         end;
  4851.        
  4852.         local function _Returns(...)
  4853.             return Select('#', ...), {...};
  4854.         end;
  4855.        
  4856.         local function Wrap(Chunk, Env, Upvalues)
  4857.             local Instr = Chunk.Instr;
  4858.             local Const = Chunk.Const;
  4859.             local Proto = Chunk.Proto;
  4860.        
  4861.             local function OnError(Err, Position) -- Handle your errors in whatever way.
  4862.                 local Name  = Chunk.Name or 'Code';
  4863.                 local Line  = Chunk.Lines[Position] or '?';
  4864.                 local Err   = Err:match'^.+:%s*(.+)' or Err;
  4865.        
  4866.                 error(string.format('%s (%s): %s', Name, Line, Err), 0);
  4867.             end;
  4868.        
  4869.             return function(...) -- Returned function to run bytecode chunk (Don't be stupid, you can't setfenv this to work your way).
  4870.                 local Upvalues  = Upvalues;
  4871.                 local Instr     = Instr;
  4872.                 local Const     = Const;
  4873.                 local Proto     = Proto;
  4874.        
  4875.                 local InstrPoint, Top   = 1, -1;
  4876.                 local Vararg, Varargsz  = {}, Select('#', ...) - 1;
  4877.        
  4878.                 local GStack    = {};
  4879.                 local Lupvals   = {};
  4880.                 local Stack     = setmetatable({}, {
  4881.                     __index     = GStack;
  4882.                     __newindex  = function(_, Key, Value)
  4883.                         if (Key > Top) and Value then
  4884.                             Top = Key;
  4885.                         end;
  4886.        
  4887.                         GStack[Key] = Value;
  4888.                     end;
  4889.                 });
  4890.        
  4891.                 local function Loop()
  4892.                     local Instr = Instr;
  4893.                     local Inst, Enum, A, B;
  4894.        
  4895.                     while true do
  4896.                         Inst        = Instr[InstrPoint];
  4897.                         Enum        = Inst.Enum;
  4898.                         InstrPoint  = InstrPoint + 1;
  4899.        
  4900.                         if (Enum == 0) then -- MOVE
  4901.                             Stack[Inst[1]]  = Stack[Inst[2]];
  4902.                         elseif (Enum == 1) then -- LOADK
  4903.                             Stack[Inst[1]]  = Const[Inst[2]];
  4904.                         elseif (Enum == 2) then -- LOADBOOL
  4905.                             Stack[Inst[1]]  = (Inst[2] ~= 0);
  4906.        
  4907.                             if (Inst[3] ~= 0) then
  4908.                                 InstrPoint  = InstrPoint + 1;
  4909.                             end;
  4910.                         elseif (Enum == 3) then -- LOADNIL
  4911.                             local Stk   = Stack;
  4912.        
  4913.                             for Idx = Inst[1], Inst[2] do
  4914.                                 Stk[Idx]    = nil;
  4915.                             end;
  4916.                         elseif (Enum == 4) then -- GETUPVAL
  4917.                             Stack[Inst[1]]  = Upvalues[Inst[2]];
  4918.                         elseif (Enum == 5) then -- GETGLOBAL
  4919.                             Stack[Inst[1]]  = Env[Const[Inst[2]]];
  4920.                         elseif (Enum == 6) then -- GETTABLE
  4921.                             local C     = Inst[3];
  4922.                             local Stk   = Stack;
  4923.        
  4924.                             if (C > 255) then
  4925.                                 C   = Const[C - 256];
  4926.                             else
  4927.                                 C   = Stk[C];
  4928.                             end;
  4929.        
  4930.                             Stk[Inst[1]]    = Stk[Inst[2]][C];
  4931.                         elseif (Enum == 7) then -- SETGLOBAL
  4932.                             Env[Const[Inst[2]]] = Stack[Inst[1]];
  4933.                         elseif (Enum == 8) then -- SETUPVAL
  4934.                             Upvalues[Inst[2]]   = Stack[Inst[1]];
  4935.                         elseif (Enum == 9) then -- SETTABLE
  4936.                             local B,= Inst[2], Inst[3];
  4937.                             local Stk   = Stack;
  4938.        
  4939.                             if (B > 255) then
  4940.                                 B   = Const[B - 256];
  4941.                             else
  4942.                                 B   = Stk[B];
  4943.                             end;
  4944.        
  4945.                             if (C > 255) then
  4946.                                 C   = Const[C - 256];
  4947.                             else
  4948.                                 C   = Stk[C];
  4949.                             end;
  4950.        
  4951.                             Stk[Inst[1]][B] = C;
  4952.                         elseif (Enum == 10) then -- NEWTABLE
  4953.                             Stack[Inst[1]]  = {};
  4954.                         elseif (Enum == 11) then -- SELF
  4955.                             local A     = Inst[1];
  4956.                             local B     = Inst[2];
  4957.                             local C     = Inst[3];
  4958.                             local Stk   = Stack;
  4959.        
  4960.                             B = Stk[B];
  4961.        
  4962.                             if (C > 255) then
  4963.                                 C   = Const[C - 256];
  4964.                             else
  4965.                                 C   = Stk[C];
  4966.                             end;
  4967.        
  4968.                             Stk[A + 1]  = B;
  4969.                             Stk[A]      = B[C];
  4970.                         elseif (Enum == 12) then -- ADD
  4971.                             local B = Inst[2];
  4972.                             local C = Inst[3];
  4973.                             local Stk, Con  = Stack, Const;
  4974.        
  4975.                             if (B > 255) then
  4976.                                 B   = Const[B - 256];
  4977.                             else
  4978.                                 B   = Stk[B];
  4979.                             end;
  4980.        
  4981.                             if (C > 255) then
  4982.                                 C   = Const[C - 256];
  4983.                             else
  4984.                                 C   = Stk[C];
  4985.                             end;
  4986.        
  4987.                             Stk[Inst[1]]    = B + C;
  4988.                         elseif (Enum == 13) then -- SUB
  4989.                             local B = Inst[2];
  4990.                             local C = Inst[3];
  4991.                             local Stk, Con  = Stack, Const;
  4992.        
  4993.                             if (B > 255) then
  4994.                                 B   = Const[B - 256];
  4995.                             else
  4996.                                 B   = Stk[B];
  4997.                             end;
  4998.        
  4999.                             if (C > 255) then
  5000.                                 C   = Const[C - 256];
  5001.                             else
  5002.                                 C   = Stk[C];
  5003.                             end;
  5004.        
  5005.                             Stk[Inst[1]]    = B - C;
  5006.                         elseif (Enum == 14) then -- MUL
  5007.                             local B = Inst[2];
  5008.                             local C = Inst[3];
  5009.                             local Stk, Con  = Stack, Const;
  5010.        
  5011.                             if (B > 255) then
  5012.                                 B   = Const[B - 256];
  5013.                             else
  5014.                                 B   = Stk[B];
  5015.                             end;
  5016.        
  5017.                             if (C > 255) then
  5018.                                 C   = Const[C - 256];
  5019.                             else
  5020.                                 C   = Stk[C];
  5021.                             end;
  5022.        
  5023.                             Stk[Inst[1]]    = B * C;
  5024.                         elseif (Enum == 15) then -- DIV
  5025.                             local B = Inst[2];
  5026.                             local C = Inst[3];
  5027.                             local Stk, Con  = Stack, Const;
  5028.        
  5029.                             if (B > 255) then
  5030.                                 B   = Const[B - 256];
  5031.                             else
  5032.                                 B   = Stk[B];
  5033.                             end;
  5034.        
  5035.                             if (C > 255) then
  5036.                                 C   = Const[C - 256];
  5037.                             else
  5038.                                 C   = Stk[C];
  5039.                             end;
  5040.        
  5041.                             Stk[Inst[1]]    = B / C;
  5042.                         elseif (Enum == 16) then -- MOD
  5043.                             local B = Inst[2];
  5044.                             local C = Inst[3];
  5045.                             local Stk, Con  = Stack, Const;
  5046.        
  5047.                             if (B > 255) then
  5048.                                 B   = Const[B - 256];
  5049.                             else
  5050.                                 B   = Stk[B];
  5051.                             end;
  5052.        
  5053.                             if (C > 255) then
  5054.                                 C   = Const[C - 256];
  5055.                             else
  5056.                                 C   = Stk[C];
  5057.                             end;
  5058.        
  5059.                             Stk[Inst[1]]    = B % C;
  5060.                         elseif (Enum == 17) then -- POW
  5061.                             local B = Inst[2];
  5062.                             local C = Inst[3];
  5063.                             local Stk, Con  = Stack, Const;
  5064.        
  5065.                             if (B > 255) then
  5066.                                 B   = Const[B - 256];
  5067.                             else
  5068.                                 B   = Stk[B];
  5069.                             end;
  5070.        
  5071.                             if (C > 255) then
  5072.                                 C   = Const[C - 256];
  5073.                             else
  5074.                                 C   = Stk[C];
  5075.                             end;
  5076.        
  5077.                             Stk[Inst[1]]    = B ^ C;
  5078.                         elseif (Enum == 18) then -- UNM
  5079.                             Stack[Inst[1]]  = -Stack[Inst[2]];
  5080.                         elseif (Enum == 19) then -- NOT
  5081.                             Stack[Inst[1]]  = (not Stack[Inst[2]]);
  5082.                         elseif (Enum == 20) then -- LEN
  5083.                             Stack[Inst[1]]  = #Stack[Inst[2]];
  5084.                         elseif (Enum == 21) then -- CONCAT
  5085.                             local Stk   = Stack;
  5086.                             local B     = Inst[2];
  5087.                             local K     = {Stack[B]};
  5088.        
  5089.                             for Idx = B + 1, Inst[3] do
  5090.                                 K[#K + 1]   = Stk[Idx];
  5091.                             end;
  5092.        
  5093.                             Stack[Inst[1]]  = Concat(K);
  5094.                         elseif (Enum == 22) then -- JUMP
  5095.                             InstrPoint  = InstrPoint + Inst[2];
  5096.                         elseif (Enum == 23) then -- EQ
  5097.                             local A = Inst[1] ~= 0;
  5098.                             local B = Inst[2];
  5099.                             local C = Inst[3];
  5100.                             local Stk, Con  = Stack, Const;
  5101.        
  5102.                             if (B > 255) then
  5103.                                 B   = Const[B - 256];
  5104.                             else
  5105.                                 B   = Stk[B];
  5106.                             end;
  5107.        
  5108.                             if (C > 255) then
  5109.                                 C   = Const[C - 256];
  5110.                             else
  5111.                                 C   = Stk[C];
  5112.                             end;
  5113.        
  5114.                             if (B == C) ~= A then
  5115.                                 InstrPoint  = InstrPoint + 1;
  5116.                             end;
  5117.                         elseif (Enum == 24) then -- LT
  5118.                             local A = Inst[1] ~= 0;
  5119.                             local B = Inst[2];
  5120.                             local C = Inst[3];
  5121.                             local Stk, Con  = Stack, Const;
  5122.        
  5123.                             if (B > 255) then
  5124.                                 B   = Const[B - 256];
  5125.                             else
  5126.                                 B   = Stk[B];
  5127.                             end;
  5128.        
  5129.                             if (C > 255) then
  5130.                                 C   = Const[C - 256];
  5131.                             else
  5132.                                 C   = Stk[C];
  5133.                             end;
  5134.        
  5135.                             if (B < C) ~= A then
  5136.                                 InstrPoint  = InstrPoint + 1;
  5137.                             end;
  5138.                         elseif (Enum == 25) then -- LE
  5139.                             local A = Inst[1] ~= 0;
  5140.                             local B = Inst[2];
  5141.                             local C = Inst[3];
  5142.                             local Stk, Con  = Stack, Const;
  5143.        
  5144.                             if (B > 255) then
  5145.                                 B   = Const[B - 256];
  5146.                             else
  5147.                                 B   = Stk[B];
  5148.                             end;
  5149.        
  5150.                             if (C > 255) then
  5151.                                 C   = Const[C - 256];
  5152.                             else
  5153.                                 C   = Stk[C];
  5154.                             end;
  5155.        
  5156.                             if (B <= C) ~= A then
  5157.                                 InstrPoint  = InstrPoint + 1;
  5158.                             end;
  5159.                         elseif (Enum == 26) then -- TEST
  5160.                             if (not not Stack[Inst[1]]) == (Inst[3] == 0) then
  5161.                                 InstrPoint  = InstrPoint + 1;
  5162.                             end;
  5163.                         elseif (Enum == 27) then -- TESTSET
  5164.                             local B = Stack[Inst[2]];
  5165.        
  5166.                             if (not not B) == (Inst[3] == 0) then
  5167.                                 InstrPoint  = InstrPoint + 1;
  5168.                             else
  5169.                                 Stack[Inst[1]] = B;
  5170.                             end;
  5171.                         elseif (Enum == 28) then -- CALL
  5172.                             local A = Inst[1];
  5173.                             local B = Inst[2];
  5174.                             local C = Inst[3];
  5175.                             local Stk   = Stack;
  5176.                             local Args, Results;
  5177.                             local Limit, Loop;
  5178.        
  5179.                             Args    = {};
  5180.        
  5181.                             if (B ~= 1) then
  5182.                                 if (B ~= 0) then
  5183.                                     Limit = A + B - 1;
  5184.                                 else
  5185.                                     Limit = Top;
  5186.                                 end;
  5187.        
  5188.                                 Loop    = 0;
  5189.        
  5190.                                 for Idx = A + 1, Limit do
  5191.                                     Loop = Loop + 1;
  5192.        
  5193.                                     Args[Loop] = Stk[Idx];
  5194.                                 end;
  5195.        
  5196.                                 Limit, Results = _Returns(Stk[A](unpack(Args, 1, Limit - A)));
  5197.                             else
  5198.                                 Limit, Results = _Returns(Stk[A]());
  5199.                             end;
  5200.        
  5201.                             Top = A - 1;
  5202.        
  5203.                             if (C ~= 1) then
  5204.                                 if (C ~= 0) then
  5205.                                     Limit = A + C - 2;
  5206.                                 else
  5207.                                     Limit = Limit + A;
  5208.                                 end;
  5209.        
  5210.                                 Loop    = 0;
  5211.        
  5212.                                 for Idx = A, Limit do
  5213.                                     Loop = Loop + 1;
  5214.        
  5215.                                     Stk[Idx] = Results[Loop];
  5216.                                 end;
  5217.                             end;
  5218.                         elseif (Enum == 29) then -- TAILCALL
  5219.                             local A = Inst[1];
  5220.                             local B = Inst[2];
  5221.                             local C = Inst[3];
  5222.                             local Stk   = Stack;
  5223.                             local Args, Results;
  5224.                             local Limit, Loop;
  5225.        
  5226.                             Args = {};
  5227.        
  5228.                             if (B ~= 1) then
  5229.                                 if (B ~= 0) then
  5230.                                     Limit = A + B - 1;
  5231.                                 else
  5232.                                     Limit = Top;
  5233.                                 end
  5234.        
  5235.                                 Loop = 0;
  5236.        
  5237.                                 for Idx = A + 1, Limit do
  5238.                                     Loop = Loop + 1;
  5239.        
  5240.                                     Args[#Args + 1] = Stk[Idx];
  5241.                                 end
  5242.        
  5243.                                 Results = {Stk[A](unpack(Args, 1, Limit - A))};
  5244.                             else
  5245.                                 Results = {Stk[A]()};
  5246.                             end;
  5247.        
  5248.                             return Results;
  5249.                         elseif (Enum == 30) then -- RETURN
  5250.                             local A = Inst[1];
  5251.                             local B = Inst[2];
  5252.                             local Stk   = Stack;
  5253.                             local Loop, Output;
  5254.                             local Limit;
  5255.        
  5256.                             if (B == 1) then
  5257.                                 return;
  5258.                             elseif (B == 0) then
  5259.                                 Limit   = Top;
  5260.                             else
  5261.                                 Limit   = A + B - 2;
  5262.                             end;
  5263.        
  5264.                             Output = {};
  5265.        
  5266.                             local Loop  = 0;
  5267.        
  5268.                             for Idx = A, Limit do
  5269.                                 Loop    = Loop + 1;
  5270.        
  5271.                                 Output[Loop] = Stk[Idx];
  5272.                             end;
  5273.        
  5274.                             return Output;
  5275.                         elseif (Enum == 31) then -- FORLOOP
  5276.                             local A     = Inst[1];
  5277.                             local Stk   = Stack;
  5278.        
  5279.                             local Step  = Stk[A + 2];
  5280.                             local Index = Stk[A] + Step;
  5281.        
  5282.                             Stk[A]  = Index;
  5283.        
  5284.                             if (Step > 0) then
  5285.                                 if Index <= Stk[A + 1] then
  5286.                                     InstrPoint  = InstrPoint + Inst[2];
  5287.        
  5288.                                     Stk[A + 3] = Index;
  5289.                                 end;
  5290.                             else
  5291.                                 if Index >= Stk[A + 1] then
  5292.                                     InstrPoint  = InstrPoint + Inst[2];
  5293.        
  5294.                                     Stk[A + 3] = Index;
  5295.                                 end
  5296.                             end
  5297.                         elseif (Enum == 32) then -- FORPREP
  5298.                             local A     = Inst[1];
  5299.                             local Stk   = Stack;
  5300.        
  5301.                             Stk[A]  = Stk[A] - Stk[A + 2];
  5302.        
  5303.                             InstrPoint  = InstrPoint + Inst[2];
  5304.                         elseif (Enum == 33) then -- TFORLOOP
  5305.                             local A     = Inst[1];
  5306.                             local B     = Inst[2];
  5307.                             local C     = Inst[3];
  5308.                             local Stk   = Stack;
  5309.        
  5310.                             local Offset    = A + 2;
  5311.                             local Result    = {Stk[A](Stk[A + 1], Stk[A + 2])};
  5312.        
  5313.                             for Idx = 1, C do
  5314.                                 Stack[Offset + Idx] = Result[Idx];
  5315.                             end;
  5316.        
  5317.                             if (Stk[A + 3] ~= nil) then
  5318.                                 Stk[A + 2]  = Stk[A + 3];
  5319.                             else
  5320.                                 InstrPoint  = InstrPoint + 1;
  5321.                             end;
  5322.                         elseif (Enum == 34) then -- SETLIST
  5323.                             local A     = Inst[1];
  5324.                             local B     = Inst[2];
  5325.                             local C     = Inst[3];
  5326.                             local Stk   = Stack;
  5327.        
  5328.                             if (C == 0) then
  5329.                                 InstrPoint  = InstrPoint + 1;
  5330.                                 C           = Instr[InstrPoint]; -- This implementation was ambiguous! Will eventually re-test.
  5331.                             end;
  5332.        
  5333.                             local Offset    = (C - 1) * 50;
  5334.                             local T         = Stk[A]; -- Assuming T is the newly created table.
  5335.        
  5336.                             if (B == 0) then
  5337.                                 B   = Top;
  5338.                             end;
  5339.        
  5340.                             for Idx = 1, B do
  5341.                                 T[Offset + Idx] = Stk[A + Idx];
  5342.                             end;
  5343.                         elseif (Enum == 35) then -- CLOSE
  5344.                             local A     = Inst[1];
  5345.                             local Cls   = {}; -- Slight doubts on any issues this may cause
  5346.        
  5347.                             for Idx = 1, #Lupvals do
  5348.                                 local List = Lupvals[Idx];
  5349.        
  5350.                                 for Idz = 0, #List do
  5351.                                     local Upv   = List[Idz];
  5352.                                     local Stk   = Upv[1];
  5353.                                     local Pos   = Upv[2];
  5354.        
  5355.                                     if (Stk == Stack) and (Pos >= A) then
  5356.                                         Cls[Pos]    = Stk[Pos];
  5357.                                         Upv[1]      = Cls; -- @memcorrupt credit me for the spoonfeed
  5358.                                     end;
  5359.                                 end;
  5360.                             end;
  5361.                         elseif (Enum == 36) then -- CLOSURE
  5362.                             local Proto = Proto[Inst[2]];
  5363.                             local Instr = Instr;
  5364.                             local Stk   = Stack;
  5365.        
  5366.                             local Indexes;
  5367.                             local NewUvals;
  5368.        
  5369.                             if (Proto.Upvals ~= 0) then
  5370.                                 Indexes     = {};
  5371.                                 NewUvals    = setmetatable({}, {
  5372.                                         __index = function(_, Key)
  5373.                                             local Val   = Indexes[Key];
  5374.        
  5375.                                             return Val[1][Val[2]];
  5376.                                         end,
  5377.                                         __newindex = function(_, Key, Value)
  5378.                                             local Val   = Indexes[Key];
  5379.        
  5380.                                             Val[1][Val[2]]  = Value;
  5381.                                         end;
  5382.                                     }
  5383.                                 );
  5384.        
  5385.                                 for Idx = 1, Proto.Upvals do
  5386.                                     local Mvm   = Instr[InstrPoint];
  5387.        
  5388.                                     if (Mvm.Enum == 0) then -- MOVE
  5389.                                         Indexes[Idx - 1] = {Stk, Mvm[2]};
  5390.                                     elseif (Mvm.Enum == 4) then -- GETUPVAL
  5391.                                         Indexes[Idx - 1] = {Upvalues, Mvm[2]};
  5392.                                     end;
  5393.        
  5394.                                     InstrPoint  = InstrPoint + 1;
  5395.                                 end;
  5396.        
  5397.                                 Lupvals[#Lupvals + 1]   = Indexes;
  5398.                             end;
  5399.        
  5400.                             Stk[Inst[1]]            = Wrap(Proto, Env, NewUvals);
  5401.                         elseif (Enum == 37) then -- VARARG
  5402.                             local A = Inst[1];
  5403.                             local B = Inst[2];
  5404.                             local Stk, Vararg   = Stack, Vararg;
  5405.        
  5406.                             for Idx = A, A + (B > 0 and B - 1 or Varargsz) do
  5407.                                 Stk[Idx]    = Vararg[Idx - A];
  5408.                             end;
  5409.                         end;
  5410.                     end;
  5411.                 end;
  5412.        
  5413.                 local Args  = {...};
  5414.        
  5415.                 for Idx = 0, Varargsz do
  5416.                     Stack[Idx] = Args[Idx + 1];
  5417.        
  5418.                     if (Idx >= Chunk.Args) then
  5419.                         Vararg[Idx - Chunk.Args] = Args[Idx + 1];
  5420.                     end;
  5421.                 end;
  5422.        
  5423.                 local A, B      = pcall(Loop); -- Pcalling to allow yielding
  5424.        
  5425.                 if A then -- We're always expecting this to come out true (because errorless code)
  5426.                     if B then -- So I flipped the conditions.
  5427.                         return unpack(B);
  5428.                     end;
  5429.        
  5430.                     return;
  5431.                 else
  5432.                     OnError(B, InstrPoint - 1); -- Didn't get time to test the `-1` honestly, but I assume it works properly
  5433.                 end;
  5434.             end;
  5435.         end;
  5436.        
  5437.         return function(BCode, Env) -- lua_function LoadBytecode (string BCode, table Env)
  5438.             local Buffer    = GetMeaning(BCode);
  5439.        
  5440.             return Wrap(Buffer, Env or getfenv(0)), Buffer;
  5441.         end;
  5442.     end
  5443.     fake_module_scripts[script] = module_script
  5444. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement