summaryrefslogtreecommitdiff
path: root/lprefix.h
diff options
context:
space:
mode:
Diffstat (limited to 'lprefix.h')
-rw-r--r--lprefix.h175
1 files changed, 175 insertions, 0 deletions
diff --git a/lprefix.h b/lprefix.h
new file mode 100644
index 0000000..0eb149f
--- /dev/null
+++ b/lprefix.h
@@ -0,0 +1,175 @@
1/*
2** $Id: lprefix.h,v 1.2 2014/12/29 16:54:13 roberto Exp $
3** Definitions for Lua code that must come before any other header file
4** See Copyright Notice in lua.h
5*/
6
7#ifndef lprefix_h
8#define lprefix_h
9
10
11/*
12** Allows POSIX/XSI stuff
13*/
14#if !defined(LUA_USE_C89) /* { */
15
16#if !defined(_XOPEN_SOURCE)
17#define _XOPEN_SOURCE 600
18#elif _XOPEN_SOURCE == 0
19#undef _XOPEN_SOURCE /* use -D_XOPEN_SOURCE=0 to undefine it */
20#endif
21
22/*
23** Allows manipulation of large files in gcc and some other compilers
24*/
25#if !defined(LUA_32BITS) && !defined(_FILE_OFFSET_BITS)
26#define _LARGEFILE_SOURCE 1
27#define _FILE_OFFSET_BITS 64
28#endif
29
30#endif /* } */
31
32
33/*
34** Windows stuff
35*/
36#if defined(_WIN32) /* { */
37
38#if !defined(_CRT_SECURE_NO_WARNINGS)
39#define _CRT_SECURE_NO_WARNINGS /* avoid warnings about ISO C functions */
40#endif
41
42#endif /* } */
43
44
45/* COMPAT53 adaptation */
46#include "c-api/compat-5.3.h"
47
48#undef LUAMOD_API
49#define LUAMOD_API extern
50
51
52#ifdef lutf8lib_c
53# define luaopen_utf8 luaopen_compat53_utf8
54/* we don't support the %U format string of lua_pushfstring!
55 * code below adapted from the Lua 5.3 sources:
56 */
57static const char *compat53_utf8_escape (lua_State* L, long x) {
58 if (x < 0x80) { /* ASCII */
59 char c = (char)x;
60 lua_pushlstring(L, &c, 1);
61 } else {
62 char buff[8] = { 0 };
63 unsigned int mfb = 0x3f;
64 int n = 1;
65 do {
66 buff[8 - (n++)] = (char)(0x80|(x & 0x3f));
67 x >>= 6;
68 mfb >>= 1;
69 } while (x > mfb);
70 buff[8-n] = (char)((~mfb << 1) | x);
71 lua_pushlstring(L, buff+8-n, n);
72 }
73 return lua_tostring(L, -1);
74}
75# define lua_pushfstring(L, fmt, l) \
76 compat53_utf8_escape(L, l)
77#endif
78
79
80#ifdef ltablib_c
81# define luaopen_table luaopen_compat53_table
82# ifndef LUA_MAXINTEGER
83/* conservative estimate: */
84# define LUA_MAXINTEGER INT_MAX
85# endif
86#endif /* ltablib_c */
87
88
89#ifdef lstrlib_c
90#include <locale.h>
91#include <lualib.h>
92/* move the string library open function out of the way (we only take
93 * the string packing functions)!
94 */
95# define luaopen_string luaopen_string_XXX
96/* used in string.format implementation, which we don't use: */
97# ifndef LUA_INTEGER_FRMLEN
98# define LUA_INTEGER_FRMLEN ""
99# define LUA_NUMBER_FRMLEN ""
100# endif
101# ifndef LUA_MININTEGER
102# define LUA_MININTEGER 0
103# endif
104# ifndef LUA_INTEGER_FMT
105# define LUA_INTEGER_FMT "%d"
106# endif
107# ifndef LUAI_UACINT
108# define LUAI_UACINT lua_Integer
109# endif
110/* different Lua 5.3 versions have conflicting variants of this macro
111 * in luaconf.h, there's a fallback implementation in lstrlib.c, and
112 * the macro isn't used for string (un)packing anyway!
113 * */
114# undef lua_number2strx
115# if LUA_VERSION_NUM < 503
116/* lstrlib assumes that lua_Integer and lua_Unsigned have the same
117 * size, so we use the unsigned equivalent of ptrdiff_t! */
118# define lua_Unsigned size_t
119# endif
120# ifndef l_mathlim
121# ifdef LUA_NUMBER_DOUBLE
122# define l_mathlim(n) (DBL_##n)
123# else
124# define l_mathlim(n) (FLT_##n)
125# endif
126# endif
127# ifndef l_mathop
128# ifdef LUA_NUMBER_DOUBLE
129# define l_mathop(op) op
130# else
131# define l_mathop(op) op##f
132# endif
133# endif
134# ifndef lua_getlocaledecpoint
135# define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
136# endif
137# ifndef l_sprintf
138# if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
139# define l_sprintf(s,sz,f,i) (snprintf(s, sz, f, i))
140# else
141# define l_sprintf(s,sz,f,i) ((void)(sz), sprintf(s, f, i))
142# endif
143# endif
144
145static int str_pack (lua_State *L);
146static int str_packsize (lua_State *L);
147static int str_unpack (lua_State *L);
148LUAMOD_API int luaopen_compat53_string (lua_State *L) {
149 luaL_Reg const funcs[] = {
150 { "pack", str_pack },
151 { "packsize", str_packsize },
152 { "unpack", str_unpack },
153 { NULL, NULL }
154 };
155 luaL_newlib(L, funcs);
156 return 1;
157}
158/* fake CLANG feature detection on other compilers */
159# ifndef __has_attribute
160# define __has_attribute(x) 0
161# endif
162/* make luaopen_string(_XXX) static, so it (and all other referenced
163 * string functions) won't be included in the resulting dll
164 * (hopefully).
165 */
166# undef LUAMOD_API
167# if defined(__GNUC__) || __has_attribute(__unused__)
168# define LUAMOD_API __attribute__((__unused__)) static
169# else
170# define LUAMOD_API static
171# endif
172#endif /* lstrlib.c */
173
174#endif
175