CommandHelper

CommandHelper

46.5k Downloads

array_get() does not work on BYTE_ARRAYs (despite recent commit "Make CByteArray extend CArray")

LadyCailinBot opened this issue ยท 2 comments

commented

CMDHELPER-2963 - Reported by Anon.anon

This code works as expected:

        @testByte = 0b00111111

        @z_array = byte_array()
        for(@zi = 0, @zi < @z, @zi++){
                ba_put_byte(@z_array, @testByte)
        }

        @y_array = array()
        for(@yi = 0, @yi < @y, @yi++){
                @y_array[] = array_get(array(@z_array))[0]
        }

        msg('type2: ' . typeof(@y_array[0]) . ' size: ' . array_size(@y_array[0]))

This messages: 'type2: BYTE_ARRAY size: 7' (7 is the correct size given the omitted-from-the-code @variables in question)

This does not work as expected:

        @testByte = 0b00111111

        @z_array = byte_array()
        for(@zi = 0, @zi < @z, @zi++){
                ba_put_byte(@z_array, @testByte)
        }

        @y_array = array()
        for(@yi = 0, @yi < @y, @yi++){
                @y_array[] = array_get(@z_array)
        }

        msg('type2: ' . typeof(@y_array[0]) . ' size: ' . array_size(@y_array[0]))

(the difference is in the second for() loop)

This messages: 'type2: BYTE_ARRAY size: 0'

commented

Comment by PseudoKnight

This is an oldie, so the results have changed a bit, but I've found that the primary issue here is that when a deepClone() is called for a CArray, it uses keySet(). However, byte arrays do not support keySet(), so it would need a check there for a byte array to create a copy another way.

commented

This should be fixed now in 74d1485. Actually, a much shorter example that demonstrates the problem is simply array_get(byte_array()). Anyways, I fixed this by overriding deepClone in CByteArray, because there are optimizations that can be done at a much lower level there, and so it makes sense to just override that whole thing, rather than coming up with something clever for keySet. Sorry it took 6 years :D