Giter VIP home page Giter VIP logo

Comments (5)

yuin avatar yuin commented on July 30, 2024

GopherLua is a Lua5.1 implementation.
So codes that does not work in the original C-Lua 5.1 implementation does not work in the GopherLua too.

It seems that your code does not work in the original C-Lua.

#include <stdio.h>
#include "lua.h"

int main(int argc, char **argv) {
  lua_State *L;
  lua_State *co;
  int       i;
  int       status;

  L = (lua_State*)luaL_newstate();
  luaopen_base(L);

  co = lua_newthread(L);
  if (luaL_loadfile(co, "test.lua")) {
      printf("%s\n", lua_tostring(co, lua_gettop(co)));
      lua_close(co);
      return;
  }

  for(i = 0; i < 20; i++) {
    lua_pushstring(co, "tick");
    lua_pushinteger(co, i);
    status = lua_resume(co, 2);
    if(status == LUA_ERRRUN && lua_isstring(co, -1)) {
      printf("Error: %s",  lua_tostring(co, -1));
      lua_pop(co, 1);
      break;
    }
  }

  lua_close(L);
  return 0;
}

This code generates an error: "Got Error: attempt to yield across metamethod/C-call boundary".

from gopher-lua.

AmandaCameron avatar AmandaCameron commented on July 30, 2024

That is a seperate issue in C-Lua that can be worked around with the following Lua code, however it's because the coroutine yield is trying to pass through the pcall stack, which can't be serialised in the C Lua stack as a sane value without a patch to the VM. I would note that this work-around doesn't work with Gopher-Lua.

xpcall = function( _fn, _fnErrorHandler )
    local typeT = type( _fn )
    assert( typeT == "function", "bad argument #1 to xpcall (function expected, got "..typeT..")" )
    local co = coroutine.create( _fn )
    local tResults = { coroutine.resume( co ) }
    while coroutine.status( co ) ~= "dead" do
        tResults = { coroutine.resume( co, coroutine.yield() ) }
    end
    if tResults[1] == true then
        return true, unpack( tResults, 2 )
    else
        return false, _fnErrorHandler( tResults[2] )
    end
end

pcall = function( _fn, ... )
    local typeT = type( _fn )
    assert( typeT == "function", "bad argument #1 to pcall (function expected, got "..typeT..")" )
    local tArgs = { ... }
    return xpcall(
        function()
            return _fn( unpack( tArgs ) )
        end,
        function( _error )
            return _error
        end
    )
end

from gopher-lua.

yuin avatar yuin commented on July 30, 2024

Hmmm, your work-around works for me( it seems the same result with C-Lua5.1.4).

test.go

package main

import (
    "fmt"

    "github.com/yuin/gopher-lua"
)

func main() {
    state := lua.NewState()

    main, err := state.LoadFile("test.lua")
    if err != nil {
        panic(err)
    }

    thread := state.NewThread()
    s := lua.ResumeOK
    for i := 0; i < 20; i++ {
        if s != lua.ResumeError {
            s, err, _ = state.Resume(thread, main, lua.LString("tick"), lua.LNumber(i))
            if err != nil {
                fmt.Println("Error: ", err)

                break
            }
        }
    }
}

test.lua

require("workaround")

local ok, err = pcall(
  function()
    local evt, n = coroutine.yield()

        print("Tick: " .. n)

    if n > 10 then
      error("N is > 10")
    end
  end)

if not ok then
  print("Got Error: " .. tostring(err))
  error("Dying.")
end

workaround.lua

xpcall = function( _fn, _fnErrorHandler )
    local typeT = type( _fn )
    assert( typeT == "function", "bad argument #1 to xpcall (function expected, got "..typeT..")" )
    local co = coroutine.create( _fn )
    local tResults = { coroutine.resume( co ) }
    while coroutine.status( co ) ~= "dead" do
        tResults = { coroutine.resume( co, coroutine.yield() ) }
    end
    if tResults[1] == true then
        return true, unpack( tResults, 2 )
    else
        return false, _fnErrorHandler( tResults[2] )
    end
end

pcall = function( _fn, ... )
    local typeT = type( _fn )
    assert( typeT == "function", "bad argument #1 to pcall (function expected, got "..typeT..")" )
    local tArgs = { ... }
    return xpcall(
        function()
            return _fn( unpack( tArgs ) )
        end,
        function( _error )
            return _error
        end
    )
end

test.c

#include <stdio.h>
#include "lua.h"

int main(int argc, char **argv) {
  lua_State *L;
  lua_State *co;
  int       i;
  int       status;


  L = (lua_State*)luaL_newstate();
  luaL_openlibs(L);

  co = lua_newthread(L);
  if (luaL_loadfile(co, "test.lua")) {
      printf("%s\n", lua_tostring(co, lua_gettop(co)));
      lua_close(co);
      return;
  }

  for(i = 0; i < 20; i++) {
    lua_pushstring(co, "tick");
    lua_pushinteger(co, i);
    status = lua_resume(co, 2);
    if(status == LUA_ERRRUN && lua_isstring(co, -1)) {
      printf("Error: %s",  lua_tostring(co, -1));
      lua_pop(co, 1);
      break;
    }
  }

  lua_close(L);
  return 0;
}

GopherLua(master) prints

Tick: 1
Error:  can not resume a dead thread

C-Lua(5.1.4) prints

Tick: 1
Error: attempt to call a nil value

from gopher-lua.

AmandaCameron avatar AmandaCameron commented on July 30, 2024

Hrm, it would seem I'm misunderstanding something in the stack then. I'll do some more digging tomorrow and comment if I can find anything.

from gopher-lua.

yuin avatar yuin commented on July 30, 2024

Due to lack of your response I am closing this issue for now.
Please feel free to reopen this issue if you have any questions.

from gopher-lua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.