Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LuaJIT Compiler - Getting wrong bytecode when JIT is off. #146

Closed
0rangeFox opened this issue Dec 30, 2021 · 4 comments
Closed

LuaJIT Compiler - Getting wrong bytecode when JIT is off. #146

0rangeFox opened this issue Dec 30, 2021 · 4 comments

Comments

@0rangeFox
Copy link

0rangeFox commented Dec 30, 2021

Hey guys, I would like to know how I can compile to bytecode via C, but I don't want to with JIT. I have tried several ways to disable JIT, but it still seems to be always on.

I have this snippet code to work with Lua C.

class Lua {
public:
    Lua(std::string file) : _file(std::move(file)) {
        this->_lua = luaL_newstate();

        if (!this->_lua)
            throw std::exception();

        luaL_openlibs(this->_lua);

        luaJIT_setmode(this->_lua, -1, LUAJIT_MODE_OFF|LUAJIT_MODE_MAX);

        luaL_loadfile(this->_lua, this->_file.c_str());
        
        this->dump();
    }

private:
    lua_State *_lua;
    std::string _file;

    // https://stackoverflow.com/questions/17597816/lua-dump-in-c
    void dump() {
        FILE* fileTest = fopen("test.luac","wb");
        lua_dump(this->_lua, writer, fileTest);
    }

    static int writer(lua_State *L, const void *p, size_t size, void *u) {
        return (fwrite(p, size, 1, (FILE*) u) != 1) && (size != 0);
    }
};

And the bytecode result I get is:
image

And if I do with the command luac, I get this:
image

Maybe this is related to JIT, or not? Please correct me if I am wrong, I want to understand what I am doing wrong to get those results.

@XmiliaH
Copy link
Contributor

XmiliaH commented Jan 7, 2022

The generated bytecode from LuaJIT is not compatible with Lua. From https://luajit.org/extensions.html#string_dump
Foreign bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded.
To generate bytecode for Lua as luac does one needs to use the PUC Lua library instead of the LuaJIT one.

@zhuizhuhaomeng
Copy link
Contributor

we usually use luajit -bg x.lua x.ljbc to convert Lua to bytecode.
I think luajit can not generate bytecode compatible with luac.

@0rangeFox
Copy link
Author

The generated bytecode from LuaJIT is not compatible with Lua. From https://luajit.org/extensions.html#string_dump
Foreign bytecode (e.g. from Lua 5.1) is incompatible and cannot be loaded.
To generate bytecode for Lua as luac does one needs to use the PUC Lua library instead of the LuaJIT one.

Hey guys, as he said, LuaJIT bytecode isn't compatible with Lua and won't compile for Lua. But I'm disabling LuaJIT so it's suppose to use Lua, right? Could you tell me more about PUC Lua Library? Thanks.

@XmiliaH
Copy link
Contributor

XmiliaH commented Jan 11, 2022

There are two totally different Lua implementations. LuaJIT and PUC Lua (can be found here https://www.lua.org/versions.html). These are two libraries both implementing Lua and the Lua C API but use different internals. This means that when disabling the JIT in LuaJIT one still uses LuaJIT but with the JIT disabled. Disabling the JIT will not switch to a different library (PUC Lua). To use PUC Lua one would need to link against this different library instead of LuaJIT.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants