API issues within coroutines

Hi all,

I’ve come across an issue when attempting to call into the Codea API from within a coroutine.

The example below shows the issue clearly and simply calls exactly the same function both in and out of a coroutine:

-- CoroutineTest
--
-- Call the same function from a coroutine & the main thread.

function setup()
    -- Coroutine
    local co = coroutine.create(function(fn)
        return fn()
    end)
    
    -- Main Thread
    local fn = layout.___getters.safeArea
    print("fn=" .. tostring(fn))
    local fn_res = fn()
    local _,cofn_res = coroutine.resume(co, fn)
    
    -- Analyse results
    print("mn ret=" .. tostring(fn_res))
    print("co ret=" .. tostring(cofn_res))
    
    -- As we're calling exactly the same function we would expect the
    -- return types to be identical (not the same object but same type).
    --
    -- It looks like when called from a coroutine, the function returns itself!?
    if type(fn_res) ~= type(cofn_res) then
        objc.warning("Return types do not match!")
    end
end

This then produces the following in the output pane:

fn=function: 0x2994ae3d0
mn ret=table: 0x288009ac0
co ret=function: 0x2994ae3d0
Return types do not match!

I’ve attempted to figure out what’s going wrong myself and I’ve used the ___getters value as it appears to be as far as debugging from Lua can take me.

Could anyone shed some light on what’s going on here please?

Cheers,
Steppers

@sim

1 Like

@sim @John Any ideas what could be going on here? I have a new preview feature (executing a project from within WebRepo) in the works but this is throwing a bit of a spanner in the works at the moment! :sweat_smile:

It’s not just this function in my example either. There are many that appear to have the same issue when calling into native functions.

Cheers!

That is weird, I don’t have much time right now but I’ll give the other guys a nudge and see if they have any ideas

1 Like