Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Reference to native objects
Started by ojnab Sep 10 2019 02:11 PM

2 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

ojnab

[GLOBAL: userInfoPane.html]
ojnab
  • Contributor

  • 574 posts
  • Corona SDK

I see in the corona core source that the reference for self on native objects (eg. a textbox) is found like this:

IPhoneTextBoxObject::doStuff( lua_State *L )
{
    PlatformDisplayObject *o = (PlatformDisplayObject*)LuaProxy::GetProxyableObject( L, 1 );
    if ( & o->ProxyVTable() == & PlatformDisplayObject::GetTextBoxObjectProxyVTable() )
    {
        UIView *v = ((IPhoneTextBoxObject*)o)->GetView();
        Rtt_UITextView *t = (Rtt_UITextView*)v
        // Do stuff with native text box (t)
    }
    return 0;
}

Is it possible to do something like this in plugins? I suspect the libraries used are not available on the plugin side.



[TOPIC: post.html]
#2

vlads

[GLOBAL: userInfoPane.html]
vlads
  • Contributor

  • 852 posts
  • Corona Staff

well... You could do this, but I would suggest to use lightuserdata or userdata to store pointers/datum in your C/C++ code.

This is how proxies work. They have a lot of optimizations not required by pretty much all plugins.

Corona intentionally does not expose this interfaces, and you should not rely on them. I would suggest reading Programming In Lua about interop with C. But basically you can push values to lua with this two functions. They are quite different despite similar names.

Light use data is created with lua_pushlightuserdata(L, pointer). It will push raw pointer on stack, and they it can be passed around in Lua as a value. You're responsible for handling it correctly. Usually, I don't refer to it directly, rather through the dictionary (hash map) to not to invoke dangling pointer.

When using full user data, you ask Lua to allocate you a chunk of memory. You can also add metatable to it. Metatable can have special __gc function called when before it will be deallocated.

Both pointers are retrieved with lua_touserdata, with key difference that light user data will always return you the pointer you put into it, while full user data would create Lua managed chunk of memory, subject to Lua garbage collection.

Think about who "owns" your data. If it's some element which exists outside Lua, like some native UI, then probably you want light user data. If you want object to live and die by Lua - it is user data. Those two are somehow interchangable, but it is up to you really.

Just make sure you can't dereference light user data after it was released on native side, well, same goes to stuff you store inside user data.

 

See:

Chapter about this stuff: https://www.lua.org/pil/contents.html#P4

light user data: https://www.lua.org/pil/28.5.html

user data: https://www.lua.org/pil/28.1.html



[TOPIC: post.html]
#3

ojnab

[GLOBAL: userInfoPane.html]
ojnab
  • Contributor

  • 574 posts
  • Corona SDK

Ok. No quick solution to this question :)

I will dive into your suggestions.

Thank you for taking time to explain.




[topic_controls]
[/topic_controls]