rce-incorporated / Fiu

Luau bytecode interpreter for Luau
MIT License
90 stars 16 forks source link

Generalised iteration is not supported. #1

Closed TheGreatSageEqualToHeaven closed 1 year ago

TheGreatSageEqualToHeaven commented 1 year ago

Luau has generalised iteration syntax that would be hard to implemented into the interpreter.

for i,v in {1,2,3,4,5} do 
      print(i,v)
end
TheGreatSageEqualToHeaven commented 1 year ago

There is more middle-ground that could be used to implement generalised iteration syntax efficiently

local iterator
if generalisedIterations[inst] then 
    iterator = generalisedIterations[inst]
else 
    local function generalisedIteration()
        for r1,r2,r3,r4,r5,r6,r7,r8,r9,r10, --[[to register limit]] in stack[inst.A] do 
            coroutine.yield({r1,r2,r3,r4,r5,r6,r7,r8,r9,r10, --[[to register limit]]})
        end
    end

    generalisedIterations[inst] = generalisedIteration
    iterator = generalisedIteration
end

-- FORGLOOP
TheGreatSageEqualToHeaven commented 1 year ago

Here is a working example

env = getfenv()
local function iterator()
    for r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30, r31, r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46, r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60, r61, r62, r63, r64, r65, r66, r67, r68, r69, r70, r71, r72, r73, r74, r75, r76, r77, r78, r79, r80, r81, r82, r83, r84, r85, r86, r87, r88, r89, r90, r91, r92, r93, r94, r95, r96, r97, r98, r99, r100, r101, r102, r103, r104, r105, r106, r107, r108, r109, r110, r111, r112, r113, r114, r115, r116, r117, r118, r119, r120, r121, r122, r123, r124, r125, r126, r127, r128, r129, r130, r131, r132, r133, r134, r135, r136, r137, r138, r139, r140, r141, r142, r143, r144, r145, r146, r147, r148, r149, r150, r151, r152, r153, r154, r155, r156, r157, r158, r159, r160, r161, r162, r163, r164, r165, r166, r167, r168, r169, r170, r171, r172, r173, r174, r175, r176, r177, r178, r179, r180, r181, r182, r183, r184, r185, r186, r187, r188, r189, r190, r191, r192, r193, r194, r195, r196, r197, r198, r199, r200 in __FIU__GENERALIZED_ITERATION__ do 
        coroutine.yield({r1, r2, r3, r4, r5, r6, r7, r8, r9, r10, r11, r12, r13, r14, r15, r16, r17, r18, r19, r20, r21, r22, r23, r24, r25, r26, r27, r28, r29, r30, r31, r32, r33, r34, r35, r36, r37, r38, r39, r40, r41, r42, r43, r44, r45, r46, r47, r48, r49, r50, r51, r52, r53, r54, r55, r56, r57, r58, r59, r60, r61, r62, r63, r64, r65, r66, r67, r68, r69, r70, r71, r72, r73, r74, r75, r76, r77, r78, r79, r80, r81, r82, r83, r84, r85, r86, r87, r88, r89, r90, r91, r92, r93, r94, r95, r96, r97, r98, r99, r100, r101, r102, r103, r104, r105, r106, r107, r108, r109, r110, r111, r112, r113, r114, r115, r116, r117, r118, r119, r120, r121, r122, r123, r124, r125, r126, r127, r128, r129, r130, r131, r132, r133, r134, r135, r136, r137, r138, r139, r140, r141, r142, r143, r144, r145, r146, r147, r148, r149, r150, r151, r152, r153, r154, r155, r156, r157, r158, r159, r160, r161, r162, r163, r164, r165, r166, r167, r168, r169, r170, r171, r172, r173, r174, r175, r176, r177, r178, r179, r180, r181, r182, r183, r184, r185, r186, r187, r188, r189, r190, r191, r192, r193, r194, r195, r196, r197, r198, r199, r200})
    end
end

setfenv(iterator, setmetatable({__FIU__GENERALIZED_ITERATION__ = {1,2,3,4,5}}, {__index = env}))

local thread = coroutine.create(iterator)
print(coroutine.resume(thread))
TheGreatSageEqualToHeaven commented 1 year ago

Generialised iteration can be supported now.