2

Is there a way to convert cdata to userdata? How can I use the C api to push/retrieve cdata to/from the stack?

Edit: The original title mentioned luajit but not Torch. In that context the other answers beside the one I finally had submitted myself (e.g wolfgang's) made perfect sense.

5
  • 1
    Show concrete code and error messages, otherwise it didn't happen. Commented May 26, 2015 at 19:53
  • 2
    I don't have error messages, I just want to know if there is a way to do it. Commented May 26, 2015 at 19:54
  • Well thank you both. You have been extremely helpful. Commented May 26, 2015 at 20:26
  • @Ash So was your question. Commented May 26, 2015 at 20:28
  • To the people who have put this on hold: at the time the question was posted, the Torch7 c api was VERY poorly documented. It is still not very well documented today (Torch7 itself is, but the C api, not so much). I still think that the answer I posted can be helpful to a small number of people (I have redirected collegues to that post). Closing such a question as off-topic for a very well documented library such as openCV or Eigen would of course be appropriate, but I think that you should consider the state of the library in question before voting to close. Commented Apr 13, 2017 at 7:12

3 Answers 3

3

There is no extension to the Lua C API to access cdata objects as created by LuaJIT's FFI library.

So, the simple and recommended way is to do your marshalling from the Lua side if you're dealing with cdata. So, call a C function from Lua and pass that cdata as a parameter.

Sign up to request clarification or add additional context in comments.

Comments

1

The cdata I was trying to access was the data of a tensor object from torch7, I finally found a way by using the torch7 C API, I will post it here just in case someone finds it useful:

#include <TH/TH.HW>
#include <TH/THStorage.h>
#include <TH/THTensor.h>

And then to get a tensor called "an_image" in the torch code:

lua_getglobal(L,"an_image");//assuming it goes on top of the stack
THDoubleTensor*data=(THDoubleTensor*)luaT_toudata(L,-1,"torch.DoubleTensor");

And finally given a buffer dest of doubles,

memcpy(dest,data->storage->data,n*sizeof(double));

Comments

1

Maybe you'll find useful information here: http://luajit.org/ext_ffi_semantics.html and more particularly here: http://luajit.org/ext_ffi_semantics.html#convert_fromlua

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.