ldoc icon indicating copy to clipboard operation
ldoc copied to clipboard

Have a single Type defined across both C and Lua

Open dbaileychess opened this issue 7 years ago • 1 comments

I have 1 module and 1 class, separated in two files (.lua and .cpp). The module has a single factory function that creates an instance of that class. That class is defined in the .cpp layer, and extended in the Lua layer by adding to that class's metatable.

In the .cpp file, I set up the module and provide the factory function for my class (implementation is not important)

/// MyModule
// @module myModule

/// Create a new instance of myClass
// @function New
// @treturn myClass An instance of myClass
int l_newFunc(lua_State * L) 
{ 
    // push a new myClass as userdata to the Lua Stack
}

// @type myClass

/// Get a value from myClass
// @function  GetValue
// @treturn     float someValue
int getValue(lua_State * L)
{ 
    // operate on myClass as userdata on top of the stack
}

In the .lua file, I require this cpp file and want to extent the functionality on myClass in Lua.

--- MyModule
-- @type myClass

 -- here is where I load the .cpp version of the class
 local _mymodule = require("myModule.core")

 -- I grab the metatable for myClass so I can alter it in Lua
 local _mymodule_mt = _mymodule.GetMetaTable()

  --- Get some other value from myClass
  -- @function GetOtherValue
  -- @treturn float someOtherValue
  function _mymodule_mt:GetOtherValue()
         return self.someOtherValue
  end

But I get the following error: 'class' cannot have multiple values; {type,module}.

How would one define a single type/class that spans both C and Lua files. I have 'merge=true' in my config.ld file, but that didn't seem to help.

dbaileychess avatar Apr 01 '18 05:04 dbaileychess

I have tried your example and it works for me, if you change it a little bit.

First my config.ld:

project="bla"
title="bla"
description="bla bla."
add_language_extension("lc", "c")
merge=true

Then the two files. The cpp file:

/// MyModule
// @module myModule

/// Create a new instance of myClass
// @function New
// @treturn myClass An instance of myClass
int l_newFunc(lua_State * L)
{
    // push a new myClass as userdata to the Lua Stack
}

/// MyClass
// @type myClass

/// Get a value from myClass
// @function  GetValue
// @treturn     float someValue
int getValue(lua_State * L)
{
    // operate on myClass as userdata on top of the stack
}

The lua file:

--- MyModule
-- @module myModule

--- MyClass
-- @type myClass

 -- here is where I load the .cpp version of the class
 local _mymodule = require("myModule.core")

 -- I grab the metatable for myClass so I can alter it in Lua
 local _mymodule_mt = _mymodule.GetMetaTable()

  --- Get some other value from myClass
  -- @function GetOtherValue
  -- @treturn float someOtherValue
  function _mymodule_mt:GetOtherValue()
         return self.someOtherValue
  end

I hope this works for you.

joclement avatar Apr 03 '18 08:04 joclement