lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Sep 29, 2011 at 18:24, mitchell <code@caladbolg.net> wrote:
> Hi,
>
> I am running a Ruby interpreter and the Lua interpreter within a single
> application and I was wondering if I can call lua_yield() from within a Ruby
> function using LuaCoco. So far I'm getting the usual "attempt to yield
> across C-boundary" error, but my implementation might be erroneous. Here's a
> quick overview of the code:
>
> -- Lua code
> co = coroutine.create(function()
>  run_ruby() // calls the c function below
>  print('done')
> end)
> value = coroutine.resume(co)
>
> ----------
>
> // Lua C function
> static int run_ruby(lua_State *L) {
>  ruby_init_stuff();
>  rb_define_global_function("lua_yield", l_yield, 1); // l_yield is below
>  ruby_run(some_script) // where some_script calls "lua_yield"
>  return 0;
> }
>
> // Ruby function
> static VALUE l_yield(VALUE self, VALUE arg) {
>  lua_pushsomething(L, RUBY2LUA(arg));
>  lua_yield(L, 1); // coroutine.resume() should return the arg
>  printf("This should not be called right away due to lua_yield()\n");
>  ...
> }
>
> ----------
>
> Thanks for any pointers.
>
> mitchell
>
>

The message is correct; in Lua 5.1 a coroutine can't yield across a C
function call. 5.2 allows this.

-- 
Sent from my toaster.