aboutsummaryrefslogtreecommitdiff
path: root/src/lcorolib.js
blob: 58d061cbd2df0e59d1d1aa3b4abd68d8c55b9420 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
"use strict";

const assert  = require('assert');

const lua     = require('./lua.js');
const lapi    = require('./lapi.js');
const lauxlib = require('./lauxlib.js');
const lstate  = require('./lstate.js');
const ldo     = require('./ldo.js');
const ldebug  = require('./ldebug.js');
const lobject = require('./lobject.js');
const CT      = lua.constant_types;
const TS      = lua.thread_status;

const getco = function(L) {
    let co = lapi.lua_tothread(L, 1);
    lauxlib.luaL_argcheck(L, co, 1, lua.to_luastring("thread expected"));
    return co;
};

const auxresume = function(L, co, narg) {
    if (!lapi.lua_checkstack(co, narg)) {
        lapi.lua_pushliteral(L, "too many arguments to resume");
        return -1;  /* error flag */
    }

    if (lapi.lua_status(co) === TS.LUA_OK && lapi.lua_gettop(co) === 0) {
        lapi.lua_pushliteral(L, "cannot resume dead coroutine");
        return -1;  /* error flag */
    }

    lapi.lua_xmove(L, co, narg);
    let status = ldo.lua_resume(co, L, narg);
    if (status === TS.LUA_OK || status === TS.LUA_YIELD) {
        let nres = lapi.lua_gettop(co);
        if (!lapi.lua_checkstack(L, nres + 1)) {
            lapi.lua_pop(co, nres);  /* remove results anyway */
            lapi.lua_pushliteral(L, "too many results to resume");
            return -1;  /* error flag */
        }

        lapi.lua_xmove(co,  L, nres);  /* move yielded values */
        return nres;
    } else {
        lapi.lua_xmove(co, L, 1);  /* move error message */
        return -1;  /* error flag */
    }
};

const luaB_coresume = function(L) {
    let co = getco(L);
    let r = auxresume(L, co, lapi.lua_gettop(L) - 1);
    if (r < 0) {
        lapi.lua_pushboolean(L, 0);
        lapi.lua_insert(L, -2);
        return 2;  /* return false + error message */
    } else {
        lapi.lua_pushboolean(L, 1);
        lapi.lua_insert(L, -(r + 1));
        return r + 1;  /* return true + 'resume' returns */
    }
};

const luaB_auxwrap = function(L) {
    let co = lapi.lua_tothread(L, lua.lua_upvalueindex(1));
    let r = auxresume(L, co, lapi.lua_gettop(L));
    if (r < 0) {
        if (lapi.lua_type(L, -1) === CT.LUA_TSTRING) {  /* error object is a string? */
            lauxlib.luaL_where(L, 1);  /* add extra info */
            lapi.lua_insert(L, -2);
            lapi.lua_concat(L, 2);
        }

        return lapi.lua_error(L);  /* propagate error */
    }

    return r;
};

const luaB_cocreate = function(L) {
    lauxlib.luaL_checktype(L, 1, CT.LUA_TFUNCTION);
    let NL = lstate.lua_newthread(L);
    lapi.lua_pushvalue(L, 1);  /* move function to top */
    lapi.lua_xmove(L, NL, 1);  /* move function from L to NL */
    return 1;
};

const luaB_cowrap = function(L) {
    luaB_cocreate(L);
    lapi.lua_pushcclosure(L, luaB_auxwrap, 1);
    return 1;
};

const luaB_yield = function(L) {
    return ldo.lua_yield(L, lapi.lua_gettop(L));
};

const luaB_costatus = function(L) {
    let co = getco(L);
    if (L === co) lapi.lua_pushliteral(L, "running");
    else {
        switch (lapi.lua_status(co)) {
            case TS.LUA_YIELD:
                lapi.lua_pushliteral(L, "suspended");
                break;
            case TS.LUA_OK: {
                let ar = new lua.lua_Debug();
                if (ldebug.lua_getstack(co, 0, ar) > 0)  /* does it have frames? */
                    lapi.lua_pushliteral(L, "normal");  /* it is running */
                else if (lapi.lua_gettop(co) === 0)
                    lapi.lua_pushliteral(L, "dead");
                else
                    lapi.lua_pushliteral(L, "suspended");  /* initial state */
                break;
            }
            default:  /* some error occurred */
                lapi.lua_pushliteral(L, "dead");
                break;
        }
    }

    return 1;
};

const luaB_yieldable = function(L) {
    lapi.lua_pushboolean(L, ldo.lua_isyieldable(L));
    return 1;
};

const luaB_corunning = function(L) {
    lapi.lua_pushboolean(L, lapi.lua_pushthread(L));
    return 2;
};

const co_funcs = {
    "create":      luaB_cocreate,
    "isyieldable": luaB_yieldable,
    "resume":      luaB_coresume,
    "running":     luaB_corunning,
    "status":      luaB_costatus,
    "wrap":        luaB_cowrap,
    "yield":       luaB_yield
};

const luaopen_coroutine = function(L) {
    lauxlib.luaL_newlib(L, co_funcs);
    return 1;
};

module.exports.luaopen_coroutine = luaopen_coroutine;