diff --git a/docs/source/api/function.rst b/docs/source/api/function.rst index 1d54244f..27946117 100644 --- a/docs/source/api/function.rst +++ b/docs/source/api/function.rst @@ -41,7 +41,7 @@ The following C++ code will call this function from this file and retrieve the r The call ``woof(20)`` generates a :ref:`function_result`, which is then implicitly converted to an ``double`` after being called. The intermediate temporary ``function_result`` is then destructed, popping the Lua function call results off the Lua stack. -You can also return multiple values by using std::tuple, or if you need to bind them to pre-existing variables use ``sol::bond``: +You can also return multiple values by using ``std::tuple``, or if you need to bind them to pre-existing variables use ``sol::tie``: .. code-block:: cpp :linenos: @@ -54,9 +54,9 @@ You can also return multiple values by using std::tuple, or if you need to bind std::tuple abc = f(); // 10, 11, 12 from Lua // or int a, b, c; - sol::bond(a, b, c) = f(); // a = 10, b = 11, c = 12 from Lua + sol::tie(a, b, c) = f(); // a = 10, b = 11, c = 12 from Lua -This makes it much easier to work with multiple return values. Using ``std::tie`` from the C++ standard will result in dangling references or bad behavior because of the very poor way in which C++ tuples were implemented: please use ``sol::bond( ... )`` instead to satisfy any multi-return needs. +This makes it much easier to work with multiple return values. Using ``std::tie`` from the C++ standard will result in dangling references or bad behavior because of the very poor way in which C++ tuples/``std::tie`` were specified and implemented: please use ``sol::tie( ... )`` instead to satisfy any multi-return needs. .. _function-result-warning: diff --git a/docs/source/api/state.rst b/docs/source/api/state.rst index 94660361..03c30e37 100644 --- a/docs/source/api/state.rst +++ b/docs/source/api/state.rst @@ -53,10 +53,12 @@ This function takes a number of :ref:`sol::lib` as arguments and opens .. code-block:: cpp :caption: function: script / script_file - void script(const std::string& code); - void script_file(const std::string& filename); + sol::function_result script(const std::string& code); + sol::function_result script_file(const std::string& filename); -These functions run the desired blob of either code that is in a string, or code that comes from a filename, on the ``lua_State*``. It will not run isolated: any scripts or code run will affect code in the ``lua_State*`` the object uses as well. Code ran in this fashion is not isolated. If you need isolation, consider creating a new state or traditional Lua sandboxing techniques. +These functions run the desired blob of either code that is in a string, or code that comes from a filename, on the ``lua_State*``. It will not run isolated: any scripts or code run will affect code in the ``lua_State*`` the object uses as well (unless ``local`` is applied to a variable declaration, as specified by the Lua language). Code ran in this fashion is not isolated. If you need isolation, consider creating a new state or traditional Lua sandboxing techniques. + +If your script returns a value, you can capture it from the returned :ref:`function_result`. .. code-block:: cpp :caption: function: require / require_file diff --git a/sol/function_result.hpp b/sol/function_result.hpp index 6e6f8d0d..e7c0e6c1 100644 --- a/sol/function_result.hpp +++ b/sol/function_result.hpp @@ -68,6 +68,14 @@ namespace sol { return stack::get(L, index); } + call_status status() const noexcept { + return call_status::ok; + } + + bool valid() const noexcept { + return status() == call_status::ok || status() == call_status::yielded; + } + lua_State* lua_state() const { return L; }; int stack_index() const { return index; }; diff --git a/sol/state_view.hpp b/sol/state_view.hpp index be68c381..d9902501 100644 --- a/sol/state_view.hpp +++ b/sol/state_view.hpp @@ -215,16 +215,24 @@ namespace sol { return require_core(key, [this, &file]() {this->script_file(file); }, create_global); } - void script(const std::string& code) { + function_result script(const std::string& code) { + int index = (::std::max)(lua_gettop(L), 1); if (luaL_dostring(L, code.c_str())) { lua_error(L); + // Rest of code will never be run because lua_error jumps out } + int returns = lua_gettop(L) - (index - 1); + return function_result(L, index, returns); } - void script_file(const std::string& filename) { + function_result script_file(const std::string& filename) { + int index = (::std::max)(lua_gettop(L), 1); if (luaL_dofile(L, filename.c_str())) { lua_error(L); + // Rest of code will never be run because lua_error jumps out } + int returns = lua_gettop(L) - index; + return function_result(L, index, returns); } load_result load(const std::string& code) {