CC: Tweaked

CC: Tweaked

57M Downloads

Add/Fix pre-compiled Bytecode support

Duck375 opened this issue ยท 3 comments

commented

Why?

As decribed in http://lua-users.org/wiki/LuaCompilerInLua, lua should be able to replicate work of luac program (Luac is used to generate executable bytecode from raw lua file)
At current moment pure lua implementation of Luac CAN produce executable files, but it's execution is limited up to 1 function, after which it throws error
file.lua:2 vm error: java.lang.ArrayIndexOutOfBoundsException: Index 253 out of bounds for length 2
Using pre-compiled bytecode has it's upsides and downsides.
Example: securing original lua source code on computer, by using pre-compiled file with stripped debug information, with neat addition of faster loading, but need to recompile every time source code is changed, since direct editing bytecode is usually a bad idea.

commented

Using:
Minecraft - 1.19.2
Computer Craft: Tweaked - version 1.101.3

  1. copy code from page (the last code block with support of arguments) and save it
  2. write any program (in my case it was 3 simple prints)
  3. compile it using luac
  4. run it in different ways (tried direct run and loadfile() at first, dofile(), require())
    image
commented

Ahhh, you are using loadfile (or its derivatives). That doesn't work, as files are not loaded in binary mode - see #1246. You could write a wrapper program which contains the actual payload as a (correctly encoded) string, but honestly your best bet is just, well, don't pre-compile files :p.

I've opened an issue on our Lua VM repo to see if we can do any verification of the generated bytecode, and thus catch these issues earlier.

commented

Can you provide some complete reproduction steps here (input file, etc...) and your logs. I cannot reproduce this in my testing - the loaded functions behave as expected.

Note that bytecode files cannot be executed from the shell or with loadfile directly, as files are not loaded with binary mode #1246. Also be aware that the bytecode format is unstable, and may change between versions. I would strongly recommend not going anywhere near it.