forked from pioneerspacesim/pioneer
/
LuaFixed.cpp
165 lines (142 loc) · 3.8 KB
/
LuaFixed.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// Copyright © 2008-2014 Pioneer Developers. See AUTHORS.txt for details
// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
#include "libs.h"
#include "LuaFixed.h"
#include "LuaUtils.h"
static int l_fixed_new(lua_State *L)
{
LUA_DEBUG_START(L);
int num = luaL_checkinteger(L, 1);
int denom = luaL_checkinteger(L, 2);
if (!denom)
return luaL_error(L, "cannot construct a fixed-point value with a zero denominator");
LuaFixed::PushToLua(L, fixed(num, denom));
LUA_DEBUG_END(L, 1);
return 1;
}
static int l_fixed_tostring(lua_State *L)
{
const fixed *v = LuaFixed::CheckFromLua(L, 1);
lua_pushfstring(L, "fixed(%f)", lua_Number(v->ToDouble()));
return 1;
}
static int l_fixed_add(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
LuaFixed::PushToLua(L, *a + *b);
return 1;
}
static int l_fixed_sub(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
LuaFixed::PushToLua(L, *a - *b);
return 1;
}
static int l_fixed_mul(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
LuaFixed::PushToLua(L, *a * *b);
return 1;
}
static int l_fixed_div(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
LuaFixed::PushToLua(L, *a / *b);
return 1;
}
static int l_fixed_unm(lua_State *L)
{
const fixed *v = LuaFixed::CheckFromLua(L, 1);
LuaFixed::PushToLua(L, -*v);
return 1;
}
static int l_fixed_eq(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
lua_pushboolean(L, *a == *b);
return 1;
}
static int l_fixed_lt(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
lua_pushboolean(L, *a < *b);
return 1;
}
static int l_fixed_le(lua_State *L)
{
const fixed *a = LuaFixed::CheckFromLua(L, 1);
const fixed *b = LuaFixed::CheckFromLua(L, 2);
lua_pushboolean(L, *a <= *b);
return 1;
}
static int l_fixed_tonumber(lua_State *L)
{
const fixed *v = LuaFixed::CheckFromLua(L, 1);
lua_pushnumber(L, v->ToDouble());
return 1;
}
static int l_fixed_deg2rad(lua_State *L)
{
const fixed *v = LuaFixed::CheckFromLua(L, 1);
LuaFixed::PushToLua(L, (*v) * fixed(31416,1800000));
return 1;
}
static luaL_Reg l_fixed_lib[] = {
{ "new", &l_fixed_new },
{ "deg2rad", &l_fixed_deg2rad },
{ 0, 0 }
};
static luaL_Reg l_fixed_meta[] = {
{ "__tostring", &l_fixed_tostring },
{ "__add", &l_fixed_add },
{ "__sub", &l_fixed_sub },
{ "__mul", &l_fixed_mul },
{ "__div", &l_fixed_div },
{ "__unm", &l_fixed_unm },
{ "__eq", &l_fixed_eq },
{ "__lt", &l_fixed_lt },
{ "__le", &l_fixed_le },
{ "tonumber", &l_fixed_tonumber },
{ 0, 0 }
};
const char LuaFixed::LibName[] = "fixed";
const char LuaFixed::TypeName[] = "fixed";
void LuaFixed::Register(lua_State *L)
{
LUA_DEBUG_START(L);
luaL_newlib(L, l_fixed_lib);
lua_setglobal(L, LuaFixed::LibName);
luaL_newmetatable(L, LuaFixed::TypeName);
luaL_setfuncs(L, l_fixed_meta, 0);
// hide the metatable to thwart crazy exploits
lua_pushboolean(L, 0);
lua_setfield(L, -2, "__metatable");
// map index back to the metatable
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
// pop the metatable
lua_pop(L, 1);
LUA_DEBUG_END(L, 0);
}
void LuaFixed::PushToLua(lua_State *L, const fixed &v)
{
LUA_DEBUG_START(L);
fixed *ptr = static_cast<fixed*>(lua_newuserdata(L, sizeof(fixed)));
*ptr = v;
luaL_setmetatable(L, LuaFixed::TypeName);
LUA_DEBUG_END(L, 1);
}
const fixed *LuaFixed::GetFromLua(lua_State *L, int idx)
{
return static_cast<fixed*>(luaL_testudata(L, idx, LuaFixed::TypeName));
}
const fixed *LuaFixed::CheckFromLua(lua_State *L, int idx)
{
return static_cast<fixed*>(luaL_checkudata(L, idx, LuaFixed::TypeName));
}