forked from topameng/tolua_runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int64.c
455 lines (377 loc) · 10.3 KB
/
int64.c
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
The MIT License (MIT)
Copyright (c) 2015-2016 topameng(topameng@qq.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <string.h>
#include <stdbool.h>
#include <math.h>
#include <stdint.h>
#include <inttypes.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include "tolua.h"
static bool _isint64(lua_State* L, int pos)
{
if (lua_getmetatable(L, pos))
{
lua_getref(L, LUA_RIDX_INT64);
int equal = lua_rawequal(L, -1, -2);
lua_pop(L, 2);
return equal;
}
return false;
}
bool _str2long(const char *s, int64_t* result)
{
char *endptr;
*result = strtoll(s, &endptr, 10);
if (endptr == s)
{
return false;
}
if (*endptr == 'x' || *endptr == 'X')
{
*result = (int64_t)strtoull(s, &endptr, 16);
}
if (*endptr == '\0')
{
return true;
}
while (isspace((unsigned char)*endptr)) endptr++;
return *endptr == '\0';
}
LUALIB_API bool tolua_isint64(lua_State* L, int pos)
{
int64_t num;
if (lua_type(L, pos) == LUA_TNUMBER)
{
return true;
}
else if (lua_type(L, pos) == LUA_TSTRING && _str2long(lua_tostring(L, pos), &num))
{
return true;
}
return _isint64(L, pos);
}
LUALIB_API void tolua_pushint64(lua_State* L, int64_t n)
{
/*if (toluaflags & FLAG_INT64)
{
lua_pushinteger(L, (lua_Integer)n);
}
else
{*/
int64_t* p = (int64_t*)lua_newuserdata(L, sizeof(int64_t));
*p = n;
lua_getref(L, LUA_RIDX_INT64);
lua_setmetatable(L, -2);
//}
}
//转换一个字符串为 int64
static int64_t _long(lua_State* L, int pos)
{
int64_t n = 0;
int old = errno;
errno = 0;
const char* str = lua_tostring(L, pos);
if (!_str2long(str, &n))
{
luaL_typerror(L, pos, "long");
}
if (errno == ERANGE)
{
errno = old;
return luaL_error(L, "integral is too large: %s", str);
}
errno = old;
return n;
}
LUALIB_API int64_t tolua_toint64(lua_State* L, int pos)
{
int64_t n = 0;
int type = lua_type(L, pos);
switch(type)
{
case LUA_TNUMBER:
n = (int64_t)lua_tonumber(L, pos);
break;
case LUA_TSTRING:
if (!_str2long(lua_tostring(L, pos), &n))
{
n = 0;
}
break;
case LUA_TUSERDATA:
if (_isint64(L, pos))
{
n = *(int64_t*)lua_touserdata(L, pos);
}
break;
default:
break;
}
return n;
}
static int64_t tolua_checkint64(lua_State* L, int pos)
{
int64_t n = 0;
int type = lua_type(L, pos);
switch(type)
{
case LUA_TNUMBER:
n = (int64_t)lua_tonumber(L, pos);
break;
case LUA_TSTRING:
n = _long(L, pos);
break;
case LUA_TUSERDATA:
if (_isint64(L, pos))
{
n = *(int64_t*)lua_touserdata(L, pos);
}
break;
default:
return luaL_typerror(L, pos, "long");
}
return n;
}
static int _int64add(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
tolua_pushint64(L, lhs + rhs);
return 1;
}
static int _int64sub(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
tolua_pushint64(L, lhs - rhs);
return 1;
}
static int _int64mul(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
tolua_pushint64(L, lhs * rhs);
return 1;
}
static int _int64div(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
if (rhs == 0)
{
return luaL_error(L, "div by zero");
}
tolua_pushint64(L, lhs / rhs);
return 1;
}
static int _int64mod(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
if (rhs == 0)
{
return luaL_error(L, "mod by zero");
}
tolua_pushint64(L, lhs % rhs);
return 1;
}
static int _int64unm(lua_State* L)
{
int64_t lhs = *(int64_t*)lua_touserdata(L, 1);
tolua_pushint64(L, -lhs);
return 1;
}
static int _int64pow(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
int64_t ret;
if (rhs > 0)
{
ret = pow(lhs, rhs);
}
else if (rhs == 0)
{
ret = 1;
}
else
{
char temp[64];
sprintf(temp, "%" PRId64, rhs);
return luaL_error(L, "pow by nagtive number: %s", temp);
}
tolua_pushint64(L, ret);
return 1;
}
static int _int64eq(lua_State* L)
{
int64_t lhs = *(int64_t*)lua_touserdata(L, 1);
int64_t rhs = *(int64_t*)lua_touserdata(L, 2);
lua_pushboolean(L, lhs == rhs);
return 1;
}
static int _int64equals(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_toint64(L, 2);
lua_pushboolean(L, lhs == rhs);
return 1;
}
static int _int64lt(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
lua_pushboolean(L, lhs < rhs);
return 1;
}
static int _int64le(lua_State* L)
{
int64_t lhs = tolua_checkint64(L, 1);
int64_t rhs = tolua_checkint64(L, 2);
lua_pushboolean(L, lhs <= rhs);
return 1;
}
static int _int64tostring(lua_State* L)
{
if (!tolua_isint64(L, 1))
{
return luaL_typerror(L, 1, "long");
}
int64_t n = tolua_toint64(L, 1);
char temp[64];
sprintf(temp, "%" PRId64, n);
lua_pushstring(L, temp);
return 1;
}
static int _int64tonum2(lua_State* L)
{
if (!tolua_isint64(L, 1))
{
return luaL_typerror(L, 1, "long");
}
int64_t n = tolua_toint64(L, 1);
//作为无符号数拆分
if (n != 0)
{
uint32_t high = n >> 32;
uint32_t low = n & 0xFFFFFFFF;
lua_pushnumber(L, low);
lua_pushnumber(L, high);
}
else
{
lua_pushnumber(L, 0);
lua_pushnumber(L, 0);
}
return 2;
}
int tolua_newint64(lua_State* L)
{
int64_t n = 0;
int type = lua_type(L, 1);
if (type == LUA_TSTRING)
{
n = _long(L, 1);
}
else if (type == LUA_TNUMBER)
{
int64_t n1 = (int64_t)luaL_checknumber(L, 1);
int64_t n2 = (int64_t)lua_tonumber(L, 2);
if (n1 < 0 || n1 > UINT_MAX)
{
return luaL_error(L, "#1 out of range: %" PRId64, n1);
}
if (n2 < 0 || n2 > UINT_MAX)
{
return luaL_error(L, "#2 out of range: %" PRId64, n2);
}
n = n1 + (n2 << 32);
}
tolua_pushint64(L, n);
return 1;
}
void tolua_openint64(lua_State* L)
{
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setglobal(L, "int64");
lua_getref(L, LUA_RIDX_LOADED);
lua_pushstring(L, "int64");
lua_pushvalue(L, -3);
lua_rawset(L, -3);
lua_pop(L, 1);
lua_pushstring(L, "__add"),
lua_pushcfunction(L, _int64add);
lua_rawset(L, -3);
lua_pushstring(L, "__sub"),
lua_pushcfunction(L, _int64sub);
lua_rawset(L, -3);
lua_pushstring(L, "__mul"),
lua_pushcfunction(L, _int64mul);
lua_rawset(L, -3);
lua_pushstring(L, "__div"),
lua_pushcfunction(L, _int64div);
lua_rawset(L, -3);
lua_pushstring(L, "__mod"),
lua_pushcfunction(L, _int64mod);
lua_rawset(L, -3);
lua_pushstring(L, "__unm"),
lua_pushcfunction(L, _int64unm);
lua_rawset(L, -3);
lua_pushstring(L, "__pow"),
lua_pushcfunction(L, _int64pow);
lua_rawset(L, -3);
lua_pushstring(L, "__tostring");
lua_pushcfunction(L, _int64tostring);
lua_rawset(L, -3);
lua_pushstring(L, "tostring");
lua_pushcfunction(L, _int64tostring);
lua_rawset(L, -3);
lua_pushstring(L, "__eq");
lua_pushcfunction(L, _int64eq);
lua_rawset(L, -3);
lua_pushstring(L, "__lt");
lua_pushcfunction(L, _int64lt);
lua_rawset(L, -3);
lua_pushstring(L, "__le");
lua_pushcfunction(L, _int64le);
lua_rawset(L, -3);
lua_pushstring(L, ".name");
lua_pushstring(L, "int64");
lua_rawset(L, -3);
lua_pushstring(L, "new");
lua_pushcfunction(L, tolua_newint64);
lua_rawset(L, -3);
lua_pushstring(L, "equals");
lua_pushcfunction(L, _int64equals);
lua_rawset(L, -3);
lua_pushstring(L, "tonum2");
lua_pushcfunction(L, _int64tonum2);
lua_rawset(L, -3);
lua_pushstring(L, "__index");
lua_pushvalue(L, -2);
lua_rawset(L, -3);
lua_rawseti(L, LUA_REGISTRYINDEX, LUA_RIDX_INT64);
}