Sol2: moving data off the lua stack entirely and into C++?

Created on 2 Feb 2021  路  6Comments  路  Source: ThePhD/sol2

I only see references, objects, and the instruction to used shared pointer types in the docs so this question might be quite niche.

Essentially, I want to use scripting to return a UDT from lua and release ownership from the data created there. In my codebase, I have a very specific pipeline that ends with a pointer being de allocated a specific way. (think of a conveyor belt with no return!)

so something like

AbstractDevice* ScriptedDevice::OnExecute() {
    std::object obj = luaState["on_execute_impl"]();
    return obj->release(); // give me the pointer! (NOTE: program WILL delete when done!
}

and in the lua file

local function on_execute_impl
   return Engine.Devices.ExampleDevice.new() -- assume these namespaces and UDT are properly binded in my code
end

I'd rather have the ability to do this than refactor my code but I understand if that's what I'll have to do if this isn't possible.

Bug.Derp Help.Desk

All 6 comments

I see that factories might be able to produce what I want. If I return a unique_ptr from the factory method, I could release() it later.
But how can I grab the unique ptr out of the lua state?

This fails for obvious reasons:

AbstractDevice* OnExecute() {
  sol::object obj = luaState["on_execute_impl"]();

  if(obj.valid()) {
    auto ptr = obj.as<std::unique_ptr<CardAction>>(); // no copies allowed...
    AbstractDevice* device = ptr->release(); // what I want!
    return device;
  }

  return nullptr;
}
sol::object obj = luaState["on_execute_impl"]();
if (obj.valid()) {
     // notice the reference
     auto& ptr = obj.as<std::unique_ptr<CardAction>>();
     return ptr->release();
}
return nullptr;

// sol's optional, not the standard's
sol::optional<
     std::unique_ptr<AbstractDevice>& // Note the reference marker here
> maybe_device = luaState["on_execute_impl"]();
if (maybe_device.has_value()) {
     return maybe_device->release();
}
return nullptr;
auto& ptr = obj.as<std::unique_ptr<CardAction>();

still fails

attempting to reference a deleted function sol.hpp line 11800.

The second one:

U must be an lvalue
a value of type "sol::protected_function_result*" cannot be used to initialize an entity of type "std::unique_ptr<CardAction, std::default_delete<CardAction>>*"

'sol::optional<std::unique_ptr<CardAction, std::default_delete<CardAction>>&>::m_value' member could not be initialized
'initializing': cannot convert from '_Ty*' to 'T*'

in sol.hpp lines 5528-5529

Ah. Some funky interactions are stopping the 2nd one.

2 things:

  • Pull from the latest, I fixed a bug
  • Use the 1st example

Then you should be good to go.

are you the maintainer of the vcpkg of sol? That's what I was using.

Oh, no. I guess I'd have to send an update over there...

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Rochet2 picture Rochet2  路  7Comments

spin-scenario picture spin-scenario  路  8Comments

shohnwal picture shohnwal  路  4Comments

morsisko picture morsisko  路  3Comments

squeek502 picture squeek502  路  4Comments