This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Blue's profile picture

[FIXED] Custom fs.open: java.lang.ArrayIndexOutOfBoundsException: 256

Started by Blue, 15 May 2016 - 05:39 AM
Blue #1
Posted 15 May 2016 - 07:39 AM
Hi, I've been trying to make a custom fs.open() to upload and download files automatically,but when I try to use it,it errors:

CCEmuRedux:

11: vm error: java.lang.ArrayIndexOutOfBoundsException: 256

Mimic:

11: stack overflow

Spoiler
-- indenting doesn't work on the post editor :/
oldfs = fs
newfs = {}
function newfs.open(file,method)
if method == 'r' then
download(file)
end
oldHandle = oldfs.open(file,method)
newHandle = oldHandle
if method == 'w' or method == 'a' then
function newHandle.close()
oldHandle.close()
upload(file)
end
end
return newHandle
end
local h = newfs.open('test','w')
h.write('test')
h.close()
(Pastebin)

Does anybody know how to fix this?
Edited on 15 May 2016 - 07:22 AM
Bomb Bloke #2
Posted 15 May 2016 - 08:09 AM
Remember that you can't assign tables to variables - you're instead assigning pointers which lead to tables. Same goes for functions and coroutines.

So because "newHandle" and "oldHandle" each point to the same table, when you tell newHandle.close() to call oldHandle.close(), you're really telling the function to call itself. The resulting infinite loop of calls blows up the function stack - this causes a Java error within LuaJ, and a similar error message from Mimic.

If you want to create a new table with the same content as an existing one, you'll need to specifically make a new table and copy that content over.

Alternatively, you could just preserve your old close() pointer in a local variable:

local oldClose = handle.close

function handle.close()
  oldClose()
  etc...
Edited on 15 May 2016 - 06:11 AM
Blue #3
Posted 15 May 2016 - 08:28 AM
Remember that you can't assign tables to variables - you're instead assigning pointers which lead to tables. Same goes for functions and coroutines.

So because "newHandle" and "oldHandle" each point to the same table, when you tell newHandle.close() to call oldHandle.close(), you're really telling the function to call itself. The resulting infinite loop of calls blows up the function stack - this causes a Java error within LuaJ, and a similar error message from Mimic.

If you want to create a new table with the same content as an existing one, you'll need to specifically make a new table and copy that content over.

Alternatively, you could just preserve your old close() pointer in a local variable:

local oldClose = handle.close

function handle.close()
  oldClose()
  etc...
Thanks,it works! :D/> I've almost spent a week trying to fix this