diff --git a/src/bindings/lua/options.cpp b/src/bindings/lua/options.cpp index 2f1e253159..2e5e7c5423 100644 --- a/src/bindings/lua/options.cpp +++ b/src/bindings/lua/options.cpp @@ -20,6 +20,26 @@ DECL_UDATA(options) static int mk_options(lua_State * L) { options r; + int nargs = lua_gettop(L); + if (nargs % 2 != 0) + throw exception("options expects an even number of arguments"); + for (int i = 1; i < nargs; i+=2) { + name k = to_name_ext(L, i); + auto it = get_option_declarations().find(k); + if (it == get_option_declarations().end()) { + throw exception(sstream() << "unknown option '" << k.to_string().c_str() << "'"); + } else { + option_declaration const & d = it->second; + switch (d.kind()) { + case BoolOption: r = r.update(k, lua_toboolean(L, i+1)); break; + case IntOption: r = r.update(k, static_cast(lua_tointeger(L, i+1))); break; + case UnsignedOption: r = r.update(k, static_cast(lua_tointeger(L, i+1))); break; + case DoubleOption: r = r.update(k, static_cast(lua_tonumber(L, i+1))); break; + case StringOption: r = r.update(k, lua_tostring(L, i+1)); break; + default: throw exception(sstream() << "unsupported option kind for '" << k.to_string().c_str() << "'"); + } + } + } return push_options(L, r); } diff --git a/tests/lua/opt4.lua b/tests/lua/opt4.lua new file mode 100644 index 0000000000..ca30eae602 --- /dev/null +++ b/tests/lua/opt4.lua @@ -0,0 +1,4 @@ +local o = options({"pp", "colors"}, true, {"pp", "unicode"}, false) +print(o) +assert(not pcall(function() options({"pp", "colors"}, true, {"pp", "unicode"}) end)) +assert(not pcall(function() options({"pp", "colors"}, true, {"pp", "unicodee"}, false) end))