local function b(b,V)return function(r)local g=V(r.source,r.index)r.index=r.index+b return g end end local function V(b)local V=b:s_int()local r={}for V=1,V,1 do local g=b:s_ins()local T=bit.band(g,63)local B=OPCODE_T[T]local j=OPCODE_M[T]local e={value=g;op=OPCODE_RM[T];A=bit.band(bit.rshift(g,6),255)}if B=="\065\066\067"then e.B=bit.band(bit.rshift(g,23),511)e.C=bit.band(bit.rshift(g,14),511)e.is_KB=j.b=="\079\112\065\114\103\075"and e.B>255 e.is_KC=j.c=="\079\112\065\114\103\075"and e.C>255 elseif B=="\065\066\120"then e.Bx=bit.band(bit.rshift(g,14),262143)e.is_K=j.b=="\079\112\065\114\103\075"elseif B=="\065\115\066\120"then e.sBx=bit.band(bit.rshift(g,14),262143)-131071 end r[V]=e end return r end local function r(b)local V=b:s_int()local r={}for V=1,V,1 do local g=stm_byte(b)local T if g==1 then T=stm_byte(b)~=0 elseif g==3 then T=b:s_num()elseif g==4 then T=stm_lstring(b)end r[V]=T end return r end local function g(b,V)local r=b:s_int()local g={}for r=1,r,1 do g[r]=stm_lua_func(b,V)end return g end local function T(b)local V=b:s_int()local r={}for V=1,V,1 do r[V]=b:s_int()end return r end local function B(b)local V=b:s_int()local r={}for V=1,V,1 do r[V]={varname=stm_lstring(b),startpc=b:s_int(),endpc=b:s_int()}end return r end local function j(b)local V=b:s_int()local r={}for V=1,V,1 do r[V]=stm_lstring(b)end return r end function stm_lua_func(b,e)local d={}local I=stm_lstring(b)or e d.source=I b:s_int()b:s_int()d.numupvals=stm_byte(b)d.numparams=stm_byte(b)stm_byte(b)stm_byte(b)d.code=V(b)d.const=r(b)d.subs=g(b,I)d.lines=T(b)B(b)j(b)for b,V in ipairs(d.code)do if V.is_K then V.const=d.const[V.Bx+1]else if V.is_KB then V.const_B=d.const[V.B-255]end if V.is_KC then V.const_C=d.const[V.C-255]end end end return d end function stm_lua_bytecode(V)local r local g local T local B local j local e local d local I={index=1;source=V}assert(stm_string(I,4)=="\027\076\117\097","\105\110\118\097\108\105\100 \076\117\097 \115\105\103\110\097\116\117\114\101")assert(stm_byte(I)==81,"\105\110\118\097\108\105\100 \076\117\097 \118\101\114\115\105\111\110")assert(stm_byte(I)==0,"\105\110\118\097\108\105\100 \076\117\097 \102\111\114\109\097\116")g=stm_byte(I)~=0 T=stm_byte(I)B=stm_byte(I)j=stm_byte(I)e=stm_byte(I)d=stm_byte(I)~=0 r=g and rd_int_le or rd_int_be I.s_int=cst_int_rdr(T,r)I.s_szt=cst_int_rdr(B,r)I.s_ins=cst_int_rdr(j,r)if d then I.s_num=cst_int_rdr(e,r)elseif float_types[e]then I.s_num=b(e,float_types[e][g and"\108\105\116\116\108\101"or"\098\105\103"])else error("\117\110\115\117\112\112\111\114\116\101\100 \102\108\111\097\116 \115\105\122\101")end return stm_lua_func(I,"\064\118\105\114\116\117\097\108")end local function e(b,V)for r,g in pairs(b)do if g.index>=V then g.value=g.store[g.index]g.store=g g.index="\118\097\108\117\101"b[r]=nil end end end local function d(b,V,r)local g=b[V]if not g then g={index=V,store=r}b[V]=g end return g end local function I(...)return select("\035",...),{...}end local function W(b,V)local r=b.source local g=b.lines[b.pc-1]local T,B,j=string.match(V or"","\094\040\046-\041\058\040\037\100\043\041\058\037\115\043\040\046\043\041")local e="\037\115\058\037\105\058 \091\037\115\058\037\105\093 \037\115"g=g or"\048"T=T or"\063"B=B or"\048"j=j or V or""error(string.format(e,r,g,T,B,j),0)end local function m(b)local V=b.code local r=b.subs local g=b.env local T=b.upvals local B=b.varargs local j=-1 local W={}local m=b.stack local U=b.pc while true do local E=V[U]local X=E.op U=U+1 if X<18 then if X<8 then if X<3 then if X<1 then for b=E.A,E.B,1 do m[b]=nil end elseif X>1 then local b=T[E.B]m[E.A]=b.store[b.index]else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b+V end elseif X>3 then if X<6 then if X>4 then local b=E.A local V=E.B local r if E.is_KC then r=E.const_C else r=m[E.C]end m[b+1]=m[V]m[b]=m[V][r]else m[E.A]=g[E.const]end elseif X>6 then local b if E.is_KC then b=E.const_C else b=m[E.C]end m[E.A]=m[E.B][b]else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b-V end else m[E.A]=m[E.B]end elseif X>8 then if X<13 then if X<10 then g[E.const]=m[E.A]elseif X>10 then if X<12 then local b=E.A local V=E.B local r=E.C local g local T,B if V==0 then g=j-b else g=V-1 end T,B=I(m[b](unpack(m,b+1,b+g)))if r==0 then j=(b+T)-1 else T=r-1 end for V=1,T,1 do m[(b+V)-1]=B[V]end else local b=T[E.B]b.store[b.index]=m[E.A]end else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b*V end elseif X>13 then if X<16 then if X>14 then local b=E.A local V=E.B local r if V==0 then r=j-b else r=V-1 end e(W,0)return I(m[b](unpack(m,b+1,b+r)))else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A][b]=V end elseif X>16 then m[E.A]={}else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b/V end else m[E.A]=E.const end else local b=E.A local V=m[b+2]local r=m[b]+V local g=m[b+1]local T if V==math.abs(V)then T=r<=g else T=r>=g end if T then m[E.A]=r m[E.A+3]=r U=U+E.sBx end end elseif X>18 then if X<28 then if X<23 then if X<20 then m[E.A]=#m[E.B]elseif X>20 then if X<22 then local b=E.A local V=E.B local r={}local g if V==0 then g=(j-b)+1 else g=V-1 end for V=1,g,1 do r[V]=m[(b+V)-1]end e(W,0)return g,r else local b=m[E.B]for V=E.B+1,E.C,1 do b=b..m[V]end m[E.A]=b end else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b%V end elseif X>23 then if X<26 then if X>24 then e(W,E.A)else local b,r if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then r=E.const_C else r=m[E.C]end if(b==r)==(E.A~=0)then U=U+V[U].sBx end U=U+1 end elseif X>26 then local b,r if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then r=E.const_C else r=m[E.C]end if(b<r)==(E.A~=0)then U=U+V[U].sBx end U=U+1 else local b,V if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then V=E.const_C else V=m[E.C]end m[E.A]=b^V end else m[E.A]=E.B~=0 if E.C~=0 then U=U+1 end end elseif X>28 then if X<33 then if X<30 then local b,r if E.is_KB then b=E.const_B else b=m[E.B]end if E.is_KC then r=E.const_C else r=m[E.C]end if(b<=r)==(E.A~=0)then U=U+V[U].sBx end U=U+1 elseif X>30 then if X<32 then local b=r[E.Bx+1]local B=b.numupvals local j if B~=0 then j={}for b=1,B,1 do local r=V[(U+b)-1]if r.op==OPCODE_RM[0]then j[b-1]=d(W,r.B,m)elseif r.op==OPCODE_RM[4]then j[b-1]=T[r.B]end end U=U+B end m[E.A]=wrap_lua_func(b,g,j)else local b=E.A local V=E.B if not m[V]==(E.C~=0)then U=U+1 else m[b]=m[V]end end else m[E.A]=-m[E.B]end elseif X>33 then if X<36 then if X>34 then local b=E.A local V=E.B if V==0 then V=B.size j=(b+V)-1 end for V=1,V,1 do m[(b+V)-1]=B.list[V]end else local b=E.A local V,r,g V=assert(tonumber(m[b]),"\096\102\111\114\096 \105\110\105\116\105\097\108 \118\097\108\117\101 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")r=assert(tonumber(m[b+1]),"\096\102\111\114\096 \108\105\109\105\116 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")g=assert(tonumber(m[b+2]),"\096\102\111\114\096 \115\116\101\112 \109\117\115\116 \098\101 \097 \110\117\109\098\101\114")m[b]=V-g m[b+1]=r m[b+2]=g U=U+E.sBx end elseif X>36 then local b=E.A local V=E.C local r=E.B local g=m[b]local T if r==0 then r=j-b end if V==0 then V=E[U].value U=U+1 end T=(V-1)*FIELDS_PER_FLUSH for V=1,r,1 do g[V+T]=m[b+V]end else m[E.A]=not m[E.B]end else if not m[E.A]==(E.C~=0)then U=U+1 end end else local b=E.A local V=m[b]local r=m[b+1]local g=m[b+2]local T=b+3 local B m[T+2]=g m[T+1]=r m[T]=V B={V(r,g)}for b=1,E.C,1 do m[(T+b)-1]=B[b]end if m[T]~=nil then m[b+2]=m[T]else U=U+1 end end else U=U+E.sBx end b.pc=U end end function wrap_lua_func(b,V,r)local g=b.code local T=b.subs local B=b.lines local j=b.source local e=b.numparams local function d(...)local b={}local d={}local U=0 local E,X=I(...)local O local n,Z,S for V=1,e,1 do b[V-1]=X[V]end if e<E then U=E-e for b=1,U,1 do d[b]=X[e+b]end end O={varargs={list=d;size=U},code=g;subs=T,lines=B,source=j;env=V,upvals=r;stack=b;pc=1}n,Z,S=pcall(m,O,...)if n then return unpack(S,1,Z)else W(O,Z)end return end return d end return function(b,V)return wrap_lua_func(stm_lua_bytecode(b),V or getfenv(0))end

local function cst_flt_rdr(len, func)
	return function(S)
		local flt = func(S.source, S.index)
		S.index = S.index + len

		return flt
	end
end

local function stm_instructions(S)
	local size = S:s_int()
	local code = {}

	for i = 1, size do
		local ins = S:s_ins()
		local op = bit.band(ins, 0x3F)
		local args = OPCODE_T[op]
		local mode = OPCODE_M[op]
		local data = {value = ins, op = OPCODE_RM[op], A = bit.band(bit.rshift(ins, 6), 0xFF)}

		if args == 'ABC' then
			data.B = bit.band(bit.rshift(ins, 23), 0x1FF)
			data.C = bit.band(bit.rshift(ins, 14), 0x1FF)
			data.is_KB = mode.b == 'OpArgK' and data.B > 0xFF -- post process optimization
			data.is_KC = mode.c == 'OpArgK' and data.C > 0xFF
		elseif args == 'ABx' then
			data.Bx = bit.band(bit.rshift(ins, 14), 0x3FFFF)
			data.is_K = mode.b == 'OpArgK'
		elseif args == 'AsBx' then
			data.sBx = bit.band(bit.rshift(ins, 14), 0x3FFFF) - 131071
		end

		code[i] = data
	end

	return code
end

local function stm_constants(S)
	local size = S:s_int()
	local consts = {}

	for i = 1, size do
		local tt = stm_byte(S)
		local k

		if tt == 1 then
			k = stm_byte(S) ~= 0
		elseif tt == 3 then
			k = S:s_num()
		elseif tt == 4 then
			k = stm_lstring(S)
		end

		consts[i] = k -- offset +1 during instruction decode
	end

	return consts
end

local function stm_subfuncs(S, src)
	local size = S:s_int()
	local sub = {}

	for i = 1, size do
		sub[i] = stm_lua_func(S, src) -- offset +1 in CLOSURE
	end

	return sub
end

local function stm_lineinfo(S)
	local size = S:s_int()
	local lines = {}

	for i = 1, size do lines[i] = S:s_int() end

	return lines
end

local function stm_locvars(S)
	local size = S:s_int()
	local locvars = {}

	for i = 1, size do locvars[i] = {varname = stm_lstring(S), startpc = S:s_int(), endpc = S:s_int()} end

	return locvars
end

local function stm_upvals(S)
	local size = S:s_int()
	local upvals = {}

	for i = 1, size do upvals[i] = stm_lstring(S) end

	return upvals
end

function stm_lua_func(S, psrc)
	local proto = {}
	local src = stm_lstring(S) or psrc -- source is propagated

	proto.source = src -- source name

	S:s_int() -- line defined
	S:s_int() -- last line defined

	proto.numupvals = stm_byte(S) -- num upvalues
	proto.numparams = stm_byte(S) -- num params

	stm_byte(S) -- vararg flag
	stm_byte(S) -- max stack size

	proto.code = stm_instructions(S)
	proto.const = stm_constants(S)
	proto.subs = stm_subfuncs(S, src)
	proto.lines = stm_lineinfo(S)

	stm_locvars(S)
	stm_upvals(S)

	-- post process optimization
	for _, v in ipairs(proto.code) do
		if v.is_K then
			v.const = proto.const[v.Bx + 1] -- offset for 1 based index
		else
			if v.is_KB then v.const_B = proto.const[v.B - 0xFF] end

			if v.is_KC then v.const_C = proto.const[v.C - 0xFF] end
		end
	end

	return proto
end

function stm_lua_bytecode(src)
	-- func reader
	local rdr_func

	-- header flags
	local little
	local size_int
	local size_szt
	local size_ins
	local size_num
	local flag_int

	-- stream object
	local stream = {
		-- data
		index = 1,
		source = src,
	}

	assert(stm_string(stream, 4) == '\27Lua', 'invalid Lua signature')
	assert(stm_byte(stream) == 0x51, 'invalid Lua version')
	assert(stm_byte(stream) == 0, 'invalid Lua format')

	little = stm_byte(stream) ~= 0
	size_int = stm_byte(stream)
	size_szt = stm_byte(stream)
	size_ins = stm_byte(stream)
	size_num = stm_byte(stream)
	flag_int = stm_byte(stream) ~= 0

	rdr_func = little and rd_int_le or rd_int_be
	stream.s_int = cst_int_rdr(size_int, rdr_func)
	stream.s_szt = cst_int_rdr(size_szt, rdr_func)
	stream.s_ins = cst_int_rdr(size_ins, rdr_func)

	if flag_int then
		stream.s_num = cst_int_rdr(size_num, rdr_func)
	elseif float_types[size_num] then
		stream.s_num = cst_flt_rdr(size_num, float_types[size_num][little and 'little' or 'big'])
	else
		error('unsupported float size')
	end

	return stm_lua_func(stream, '@virtual')
end

local function close_lua_upvalues(list, index)
	for i, uv in pairs(list) do
		if uv.index >= index then
			uv.value = uv.store[uv.index] -- store value
			uv.store = uv
			uv.index = 'value' -- self reference
			list[i] = nil
		end
	end
end

local function open_lua_upvalue(list, index, stack)
	local prev = list[index]

	if not prev then
		prev = {index = index, store = stack}
		list[index] = prev
	end

	return prev
end

local function wrap_lua_variadic(...) return select('#', ...), {...} end

local function on_lua_error(exst, err)
	local src = exst.source
	local line = exst.lines[exst.pc - 1]
	local psrc, pline, pmsg = string.match(err or '', '^(.-):(%d+):%s+(.+)')
	local fmt = '%s:%i: [%s:%i] %s'

	line = line or '0'
	psrc = psrc or '?'
	pline = pline or '0'
	pmsg = pmsg or err or ''

	error(string.format(fmt, src, line, psrc, pline, pmsg), 0)
end

local function exec_lua_func(exst)
	-- localize for easy lookup
	local code = exst.code
	local subs = exst.subs
	local env = exst.env
	local upvs = exst.upvals
	local vargs = exst.varargs

	-- state variables
	local stktop = -1
	local openupvs = {}
	local stack = exst.stack
	local pc = exst.pc

	while true do
		local inst = code[pc]
		local op = inst.op
		pc = pc + 1

		if op < 18 then
			if op < 8 then
				if op < 3 then
					if op < 1 then
						--[[LOADNIL]]
						for i = inst.A, inst.B do stack[i] = nil end
					elseif op > 1 then
						--[[GETUPVAL]]
						local uv = upvs[inst.B]

						stack[inst.A] = uv.store[uv.index]
					else
						--[[ADD]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs + rhs
					end
				elseif op > 3 then
					if op < 6 then
						if op > 4 then
							--[[SELF]]
							local A = inst.A
							local B = inst.B
							local index

							if inst.is_KC then
								index = inst.const_C
							else
								index = stack[inst.C]
							end

							stack[A + 1] = stack[B]
							stack[A] = stack[B][index]
						else
							--[[GETGLOBAL]]
							stack[inst.A] = env[inst.const]
						end
					elseif op > 6 then
						--[[GETTABLE]]
						local index

						if inst.is_KC then
							index = inst.const_C
						else
							index = stack[inst.C]
						end

						stack[inst.A] = stack[inst.B][index]
					else
						--[[SUB]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs - rhs
					end
				else --[[MOVE]]
					stack[inst.A] = stack[inst.B]
				end
			elseif op > 8 then
				if op < 13 then
					if op < 10 then
						--[[SETGLOBAL]]
						env[inst.const] = stack[inst.A]
					elseif op > 10 then
						if op < 12 then
							--[[CALL]]
							local A = inst.A
							local B = inst.B
							local C = inst.C
							local params
							local sz_vals, l_vals

							if B == 0 then
								params = stktop - A
							else
								params = B - 1
							end

							sz_vals, l_vals = wrap_lua_variadic(stack[A](unpack(stack, A + 1, A + params)))

							if C == 0 then
								stktop = A + sz_vals - 1
							else
								sz_vals = C - 1
							end

							for i = 1, sz_vals do stack[A + i - 1] = l_vals[i] end
						else
							--[[SETUPVAL]]
							local uv = upvs[inst.B]

							uv.store[uv.index] = stack[inst.A]
						end
					else
						--[[MUL]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs * rhs
					end
				elseif op > 13 then
					if op < 16 then
						if op > 14 then
							--[[TAILCALL]]
							local A = inst.A
							local B = inst.B
							local params

							if B == 0 then
								params = stktop - A
							else
								params = B - 1
							end

							close_lua_upvalues(openupvs, 0)
							return wrap_lua_variadic(stack[A](unpack(stack, A + 1, A + params)))
						else
							--[[SETTABLE]]
							local index, value

							if inst.is_KB then
								index = inst.const_B
							else
								index = stack[inst.B]
							end

							if inst.is_KC then
								value = inst.const_C
							else
								value = stack[inst.C]
							end

							stack[inst.A][index] = value
						end
					elseif op > 16 then
						--[[NEWTABLE]]
						stack[inst.A] = {}
					else
						--[[DIV]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs / rhs
					end
				else
					--[[LOADK]]
					stack[inst.A] = inst.const
				end
			else
				--[[FORLOOP]]
				local A = inst.A
				local step = stack[A + 2]
				local index = stack[A] + step
				local limit = stack[A + 1]
				local loops

				if step == math.abs(step) then
					loops = index <= limit
				else
					loops = index >= limit
				end

				if loops then
					stack[inst.A] = index
					stack[inst.A + 3] = index
					pc = pc + inst.sBx
				end
			end
		elseif op > 18 then
			if op < 28 then
				if op < 23 then
					if op < 20 then
						--[[LEN]]
						stack[inst.A] = #stack[inst.B]
					elseif op > 20 then
						if op < 22 then
							--[[RETURN]]
							local A = inst.A
							local B = inst.B
							local vals = {}
							local size

							if B == 0 then
								size = stktop - A + 1
							else
								size = B - 1
							end

							for i = 1, size do vals[i] = stack[A + i - 1] end

							close_lua_upvalues(openupvs, 0)
							return size, vals
						else
							--[[CONCAT]]
							local str = stack[inst.B]

							for i = inst.B + 1, inst.C do str = str .. stack[i] end

							stack[inst.A] = str
						end
					else
						--[[MOD]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs % rhs
					end
				elseif op > 23 then
					if op < 26 then
						if op > 24 then
							--[[CLOSE]]
							close_lua_upvalues(openupvs, inst.A)
						else
							--[[EQ]]
							local lhs, rhs

							if inst.is_KB then
								lhs = inst.const_B
							else
								lhs = stack[inst.B]
							end

							if inst.is_KC then
								rhs = inst.const_C
							else
								rhs = stack[inst.C]
							end

							if (lhs == rhs) == (inst.A ~= 0) then pc = pc + code[pc].sBx end

							pc = pc + 1
						end
					elseif op > 26 then
						--[[LT]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						if (lhs < rhs) == (inst.A ~= 0) then pc = pc + code[pc].sBx end

						pc = pc + 1
					else
						--[[POW]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						stack[inst.A] = lhs ^ rhs
					end
				else
					--[[LOADBOOL]]
					stack[inst.A] = inst.B ~= 0

					if inst.C ~= 0 then pc = pc + 1 end
				end
			elseif op > 28 then
				if op < 33 then
					if op < 30 then
						--[[LE]]
						local lhs, rhs

						if inst.is_KB then
							lhs = inst.const_B
						else
							lhs = stack[inst.B]
						end

						if inst.is_KC then
							rhs = inst.const_C
						else
							rhs = stack[inst.C]
						end

						if (lhs <= rhs) == (inst.A ~= 0) then pc = pc + code[pc].sBx end

						pc = pc + 1
					elseif op > 30 then
						if op < 32 then
							--[[CLOSURE]]
							local sub = subs[inst.Bx + 1] -- offset for 1 based index
							local nups = sub.numupvals
							local uvlist

							if nups ~= 0 then
								uvlist = {}

								for i = 1, nups do
									local pseudo = code[pc + i - 1]

									if pseudo.op == OPCODE_RM[0] then -- @MOVE
										uvlist[i - 1] = open_lua_upvalue(openupvs, pseudo.B, stack)
									elseif pseudo.op == OPCODE_RM[4] then -- @GETUPVAL
										uvlist[i - 1] = upvs[pseudo.B]
									end
								end

								pc = pc + nups
							end

							stack[inst.A] = wrap_lua_func(sub, env, uvlist)
						else
							--[[TESTSET]]
							local A = inst.A
							local B = inst.B

							if (not stack[B]) == (inst.C ~= 0) then
								pc = pc + 1
							else
								stack[A] = stack[B]
							end
						end
					else
						--[[UNM]]
						stack[inst.A] = -stack[inst.B]
					end
				elseif op > 33 then
					if op < 36 then
						if op > 34 then
							--[[VARARG]]
							local A = inst.A
							local size = inst.B

							if size == 0 then
								size = vargs.size
								stktop = A + size - 1
							end

							for i = 1, size do stack[A + i - 1] = vargs.list[i] end
						else
							--[[FORPREP]]
							local A = inst.A
							local init, limit, step

							init = assert(tonumber(stack[A]), '`for` initial value must be a number')
							limit = assert(tonumber(stack[A + 1]), '`for` limit must be a number')
							step = assert(tonumber(stack[A + 2]), '`for` step must be a number')

							stack[A] = init - step
							stack[A + 1] = limit
							stack[A + 2] = step

							pc = pc + inst.sBx
						end
					elseif op > 36 then
						--[[SETLIST]]
						local A = inst.A
						local C = inst.C
						local size = inst.B
						local tab = stack[A]
						local offset

						if size == 0 then size = stktop - A end

						if C == 0 then
							C = inst[pc].value
							pc = pc + 1
						end

						offset = (C - 1) * FIELDS_PER_FLUSH

						for i = 1, size do tab[i + offset] = stack[A + i] end
					else
						--[[NOT]]
						stack[inst.A] = not stack[inst.B]
					end
				else
					--[[TEST]]
					if (not stack[inst.A]) == (inst.C ~= 0) then pc = pc + 1 end
				end
			else
				--[[TFORLOOP]]
				local A = inst.A
				local func = stack[A]
				local state = stack[A + 1]
				local index = stack[A + 2]
				local base = A + 3
				local vals

				stack[base + 2] = index
				stack[base + 1] = state
				stack[base] = func

				vals = {func(state, index)}

				for i = 1, inst.C do stack[base + i - 1] = vals[i] end

				if stack[base] ~= nil then
					stack[A + 2] = stack[base]
				else
					pc = pc + 1
				end
			end
		else
			--[[JMP]]
			pc = pc + inst.sBx
		end

		exst.pc = pc
	end
end

function wrap_lua_func(state, env, upvals)
	local st_code = state.code
	local st_subs = state.subs
	local st_lines = state.lines
	local st_source = state.source
	local st_numparams = state.numparams

	local function exec_wrap(...)
		local stack = {}
		local varargs = {}
		local sizevarg = 0
		local sz_args, l_args = wrap_lua_variadic(...)

		local exst
		local ok, err, vals

		for i = 1, st_numparams do stack[i - 1] = l_args[i] end

		if st_numparams < sz_args then
			sizevarg = sz_args - st_numparams
			for i = 1, sizevarg do varargs[i] = l_args[st_numparams + i] end
		end

		exst = {
			varargs = {list = varargs, size = sizevarg},
			code = st_code,
			subs = st_subs,
			lines = st_lines,
			source = st_source,
			env = env,
			upvals = upvals,
			stack = stack,
			pc = 1,
		}

		ok, err, vals = pcall(exec_lua_func, exst, ...)

		if ok then
			return unpack(vals, 1, err)
		else
			on_lua_error(exst, err)
		end

		return -- explicit "return nothing"
	end

	return exec_wrap
end

return function(BCode, Env)
	return wrap_lua_func(stm_lua_bytecode(BCode), Env or getfenv(0))
end
