-
Notifications
You must be signed in to change notification settings - Fork 0
/
state.cpp
106 lines (95 loc) · 3.1 KB
/
state.cpp
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
// Copyright (C) 2019 Tomoyuki Fujimori <moyu@dromozoa.com>
//
// This file is part of dromozoa-multi.
//
// dromozoa-multi is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// dromozoa-multi is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with dromozoa-multi. If not, see <http://www.gnu.org/licenses/>.
#include "common.hpp"
extern "C" {
#include <lualib.h>
}
namespace dromozoa {
namespace {
void impl_gc(lua_State* L) {
check_state_handle(L, 1)->~state_handle();
}
void impl_call(lua_State* L) {
state_handle self(luaL_newstate());
if (self.get()) {
luaL_openlibs(self.get());
luaX_new<state_handle>(L, self.get());
self.release();
luaX_set_metatable(L, "dromozoa.multi.state");
} else {
luaX_throw_failure("cannot luaL_newstate");
}
}
void impl_load(lua_State* L) {
lua_State* self = check_state(L, 1);
if (!self) {
luaX_throw_failure("invalid state");
}
luaX_string_reference chunk = luaX_check_string(L, 2);
luaX_string_reference name;
if (!lua_isnoneornil(L, 3)) {
name = luaX_check_string(L, 3);
}
int result = luaL_loadbuffer(self, chunk.data(), chunk.size(), name.data());
if (result == 0) {
luaX_push_success(L);
} else {
luaX_throw_failure("cannot luaL_loadbuffer", result);
}
}
void impl_loadfile(lua_State* L) {
lua_State* self = check_state(L, 1);
if (!self) {
luaX_throw_failure("invalid state");
}
luaX_string_reference filename = luaX_check_string(L, 2);
int result = luaL_loadfile(self, filename.data());
if (result == 0) {
luaX_push_success(L);
} else {
luaX_throw_failure("cannot luaL_loadfile", result);
}
}
void impl_id(lua_State* L) {
lua_State* self = check_state(L, 1);
std::ostringstream out;
out << self;
luaX_push(L, out.str());
}
}
state_handle* check_state_handle(lua_State* L, int arg) {
return luaX_check_udata<state_handle>(L, arg, "dromozoa.multi.state");
}
lua_State* check_state(lua_State* L, int arg) {
return check_state_handle(L, arg)->get();
}
void initialize_state(lua_State* L) {
lua_newtable(L);
{
luaL_newmetatable(L, "dromozoa.multi.state");
lua_pushvalue(L, -2);
luaX_set_field(L, -2, "__index");
luaX_set_field(L, -1, "__gc", impl_gc);
lua_pop(L, 1);
luaX_set_metafield(L, -1, "__call", impl_call);
luaX_set_field(L, -1, "load", impl_load);
luaX_set_field(L, -1, "loadfile", impl_loadfile);
luaX_set_field(L, -1, "id", impl_id);
}
luaX_set_field(L, -2, "state");
}
}