summaryrefslogtreecommitdiff
path: root/src/LuaLoad.cc
blob: a2f12eae0deaa180098e41eb1116871f9aea5cec (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
#include <memory>

#include "LuaLoad.h"
#include "LuaTask.h"
#include "Buffer.h"
#include "Input.h"

using namespace Balau;

namespace {

class LuaLoad { };

enum LuaLoad_functions_t {
    LUALOAD
};

struct lua_functypes_t LuaLoad_functions[] = {
    { LUALOAD, "load", 1, 1, { BLUA_OBJECT | BLUA_STRING } },
    { -1, 0, 0, 0, 0 }
};

struct sLua_LuaLoad {
    static int LuaLoad_proceed_static(Lua & L, int n, int caller) {
        IO<Handle> h;
        enum {
            OPENING,
            CREATETASK,
            WAITTASK,
        } status;
        if (L.isobject()) {
            h = L.recast<Handle>();
            if (h.isA<Buffer>()) {
                L.load(h);
                return 0;
            }
            status = CREATETASK;
        } else {
            h = new Input(L.tostring());
            status = OPENING;
        }
        std::shared_ptr<LuaExecFile> execFile(new LuaExecFile(h));
        return L.yield(Future<int>([L, status, h, execFile]() mutable {
            switch (status) {
            case OPENING:
                h.asA<Input>()->open();
                status = CREATETASK;
            case CREATETASK:
                status = WAITTASK;
                execFile->exec(L);
            case WAITTASK:
                execFile->throwError();
            }
            return 0;
        }));
    }
};

};

void registerLuaLoad(Lua & L) {
    CHECK_FUNCTIONS(LuaLoad);
    PUSH_FUNCTION(LuaLoad, LUALOAD);
}