Sol2: No documentation over writing `require()`able libraries

Created on 16 Sep 2016  路  4Comments  路  Source: ThePhD/sol2

I'd rather not try flooding my program with global variables and only run the code necessary when needed; is there any way to create a library that I can use with require() using Sol2 or should I use just Lua?

Documentation

Most helpful comment

You can do that. You just need to provide a function for sol::state::require. As an example...

#include <sol.hpp>
#include <cassert>

struct some_class {
    int bark = 2012;
};

sol::table open_mylib(sol::this_state s) {
    sol::state_view lua(s);

    sol::table module = lua.create_table();
    module["func"] = []() { /* super cool function here */ };
    // register a class too
    module.new_usertype<some_class>("some_class", 
        "bark", &some_class::bark
    );

    return module;
}

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::package);
    // sol::c_call takes functions at the template level
    // and turns it into a lua_CFunction
    // alternatively, one can use sol::c_call<sol::wrap<callable_struct, callable_struct{}>> to make the call
    // if it's a constexpr struct
    lua.require("my_lib", sol::c_call<decltype(&open_mylib), &open_mylib>);

    // do ALL THE THINGS YOU LIKE
    lua.script(R"(
s = my_lib.some_class.new()
s.bark = 20;
)");
    some_class& s = lua["s"];
    assert(s.bark == 20);
    return 0;
}

This is a COMPLETE example. I will probably add it to the documentation, among other examples. But this is how you'd use sol to "make" something. Tested working, so I'm going to close this. You should be able to use this to do whatever you like.

All 4 comments

The closest I can give you is http://sol2.readthedocs.io/en/latest/api/state.html#state-require-function.

That will allow you to write C++ code to include a module of whatever you like into your Lua state. You can load it through a C Function, an arbitrary function, or something else. If you mean "I want to make a DLL that has a C library that gets loaded with require()", that's mega beyond the scope of Sol.

As a side note, if you make a neat little thing out of this I could probably add an example for require stuff and also add a section in the tutorials / docs for it as well, so people know what's up.

The issue is I want to write a Lua module using C++ and Sol2 instead of C. I'll try poking around at it later and see what I can produce.

You can do that. You just need to provide a function for sol::state::require. As an example...

#include <sol.hpp>
#include <cassert>

struct some_class {
    int bark = 2012;
};

sol::table open_mylib(sol::this_state s) {
    sol::state_view lua(s);

    sol::table module = lua.create_table();
    module["func"] = []() { /* super cool function here */ };
    // register a class too
    module.new_usertype<some_class>("some_class", 
        "bark", &some_class::bark
    );

    return module;
}

int main() {
    sol::state lua;
    lua.open_libraries(sol::lib::package);
    // sol::c_call takes functions at the template level
    // and turns it into a lua_CFunction
    // alternatively, one can use sol::c_call<sol::wrap<callable_struct, callable_struct{}>> to make the call
    // if it's a constexpr struct
    lua.require("my_lib", sol::c_call<decltype(&open_mylib), &open_mylib>);

    // do ALL THE THINGS YOU LIKE
    lua.script(R"(
s = my_lib.some_class.new()
s.bark = 20;
)");
    some_class& s = lua["s"];
    assert(s.bark == 20);
    return 0;
}

This is a COMPLETE example. I will probably add it to the documentation, among other examples. But this is how you'd use sol to "make" something. Tested working, so I'm going to close this. You should be able to use this to do whatever you like.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

TheMaverickProgrammer picture TheMaverickProgrammer  路  6Comments

Erwsaym picture Erwsaym  路  4Comments

morsisko picture morsisko  路  3Comments

sagunkho picture sagunkho  路  4Comments

mrgreywater picture mrgreywater  路  4Comments