brozula icon indicating copy to clipboard operation
brozula copied to clipboard

Script Fails (A Coroutine example)

Open daurnimator opened this issue 13 years ago • 1 comments

$ cat ~/luatute/tute-coro.lua

local read = function ( )
    return coroutine.yield ( )
end

local get_blah = function ( )
    print ( "PRE 1" )
    print ( read ( ) )
    print ( "PRE 2" )
    print ( read ( ) )
    print ( "PRE 3" )
    print ( read ( ) )
end

-- Create coroutine
local get_blah_co = coroutine.create ( get_blah )

-- Basic dispatcher
while coroutine.status ( get_blah_co ) == "suspended" do
    local ok , err_msg = coroutine.resume ( get_blah_co )
    if not ok then
        print("AN ERROR!",err_msg)
        break
    end
end

$ ./cli.js -pb ~/luatute/tute-coro.lua

[ { index: 0,
    flags: 0,
    numparams: 0,
    framesize: 1,
    bcins: 
     [ { op: 'GGET',
         args: [ 0, 'coroutine', 'index' ] },
       { op: 'TGETS',
         args: [ 0, 0, 'yield', 'index' ] },
       { op: 'CALLT',
         args: [ 0, 1, 'call' ] } ],
    uvdata: [] },
  { index: 1,
    flags: 0,
    numparams: 0,
    framesize: 2,
    bcins: 
     [ { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'KSTR',
         args: [ 1, 'PRE 1' ] },
       { op: 'CALL',
         args: [ 0, 1, 2, 'call' ] },
       { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'UGET', args: [ 1, 0 ] },
       { op: 'CALL',
         args: [ 1, 0, 1, 'call' ] },
       { op: 'CALLM',
         args: [ 0, 1, 0, 'call' ] },
       { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'KSTR',
         args: [ 1, 'PRE 2' ] },
       { op: 'CALL',
         args: [ 0, 1, 2, 'call' ] },
       { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'UGET', args: [ 1, 0 ] },
       { op: 'CALL',
         args: [ 1, 0, 1, 'call' ] },
       { op: 'CALLM',
         args: [ 0, 1, 0, 'call' ] },
       { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'KSTR',
         args: [ 1, 'PRE 3' ] },
       { op: 'CALL',
         args: [ 0, 1, 2, 'call' ] },
       { op: 'GGET',
         args: [ 0, 'print', 'index' ] },
       { op: 'UGET', args: [ 1, 0 ] },
       { op: 'CALL',
         args: [ 1, 0, 1, 'call' ] },
       { op: 'CALLM',
         args: [ 0, 1, 0, 'call' ] },
       { op: 'RET0', args: [ 0, 1 ] } ],
    uvdata: 
     [ { local: true,
         immutable: true,
         uv: 0 } ] },
  { index: 2,
    flags: 3,
    numparams: 0,
    framesize: 8,
    bcins: 
     [ { op: 'FNEW',
         args: 
          [ 0,
            { index: 0,
              flags: 0,
              numparams: 0,
              framesize: 1,
              bcins: [Object],
              uvdata: [] },
            'gc' ] },
       { op: 'FNEW',
         args: 
          [ 1,
            { index: 1,
              flags: 0,
              numparams: 0,
              framesize: 2,
              bcins: [Object],
              uvdata: [Object] },
            'gc' ] },
       { op: 'GGET',
         args: [ 2, 'coroutine', 'index' ] },
       { op: 'TGETS',
         args: 
          [ 2,
            2,
            'create',
            'index' ] },
       { op: 'MOV', args: [ 3, 1 ] },
       { op: 'CALL',
         args: [ 2, 2, 2, 'call' ] },
       { op: 'GGET',
         args: [ 3, 'coroutine', 'index' ] },
       { op: 'TGETS',
         args: 
          [ 3,
            3,
            'status',
            'index' ] },
       { op: 'MOV', args: [ 4, 2 ] },
       { op: 'CALL',
         args: [ 3, 2, 2, 'call' ] },
       { op: 'ISNES',
         args: [ 3, 'suspended', 'eq' ] },
       { op: 'JMP', args: [ 3, 13 ] },
       { op: 'LOOP', args: [ 3, 12 ] },
       { op: 'GGET',
         args: [ 3, 'coroutine', 'index' ] },
       { op: 'TGETS',
         args: 
          [ 3,
            3,
            'resume',
            'index' ] },
       { op: 'MOV', args: [ 4, 2 ] },
       { op: 'CALL',
         args: [ 3, 3, 2, 'call' ] },
       { op: 'IST', args: [ 3 ] },
       { op: 'JMP', args: [ 5, -13 ] },
       { op: 'GGET',
         args: [ 5, 'print', 'index' ] },
       { op: 'KSTR',
         args: [ 6, 'AN ERROR!' ] },
       { op: 'MOV', args: [ 7, 4 ] },
       { op: 'CALL',
         args: [ 5, 1, 3, 'call' ] },
       { op: 'JMP', args: [ 3, 1 ] },
       { op: 'JMP', args: [ 3, -19 ] },
       { op: 'UCLO', args: [ 0, 0 ] },
       { op: 'RET0', args: [ 0, 1 ] } ],
    uvdata: [] } ]

$ ./cli.js ~/luatute/tute-coro.lua

/home/daurnimator/src/brozula/interpreter.js:37
    throw new Error("Lua error at " + this.protoIndex + "-" + (this.pc-1) + "\
          ^
Error: Lua error at 2-5
attempt to call a nil value
    at Closure.execute (/home/daurnimator/src/brozula/interpreter.js:37:11)
    at Closure.toFunction.fn (/home/daurnimator/src/brozula/interpreter.js:45:25)
    at /home/daurnimator/src/brozula/cli.js:191:67
    at generate (/home/daurnimator/src/brozula/cli.js:73:5)
    at /home/daurnimator/src/brozula/cli.js:62:7
    at fs.readFile (fs.js:176:14)
    at Object.oncomplete (fs.js:297:15)

daurnimator avatar Dec 12 '12 23:12 daurnimator

I added your example to the test folder (it fails currently since I haven't implemented coroutines yet), but I did add placeholders to give a much better error message in 1f2fc87d38d5255c208352f2357f415e0b300b28.

tim@touchsmart:~/Code/brozula/tests$ brozula tute-coro.lua 

/home/tim/Code/brozula/globals.js:31
    create: function () { throw new Error("TODO: Implement coroutine.create");
                                ^
Error: TODO: Implement coroutine.create
    at _G.coroutine.create (/home/tim/Code/brozula/globals.js:31:33)
    at Object.call (/home/tim/Code/brozula/runtime.js:398:17)
    at Closure.call (/home/tim/Code/brozula/interpreter.js:218:26)
    at Closure.CALL (/home/tim/Code/brozula/interpreter.js:233:8)
    at Closure.execute (/home/tim/Code/brozula/interpreter.js:33:25)
    at Closure.toFunction.fn (/home/tim/Code/brozula/interpreter.js:45:25)
    at /home/tim/Code/brozula/cli.js:191:67
    at generate (/home/tim/Code/brozula/cli.js:73:5)
    at /home/tim/Code/brozula/cli.js:62:7
    at fs.readFile (fs.js:176:14)

creationix avatar Dec 13 '12 21:12 creationix