diff options
Diffstat (limited to 'README.md')
-rw-r--r-- | README.md | 229 |
1 files changed, 229 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..08614a1 --- /dev/null +++ b/README.md | |||
@@ -0,0 +1,229 @@ | |||
1 | # lua-compat-5.3 | ||
2 | |||
3 | Lua-5.3-style APIs for Lua 5.2 and 5.1. | ||
4 | |||
5 | ## What is it | ||
6 | |||
7 | This is a small module that aims to make it easier to write code | ||
8 | in a Lua-5.3-style that is compatible with Lua 5.1, Lua 5.2, and Lua | ||
9 | 5.3. This does *not* make Lua 5.2 (or even Lua 5.1) entirely | ||
10 | compatible with Lua 5.3, but it brings the API closer to that of Lua | ||
11 | 5.3. | ||
12 | |||
13 | It includes: | ||
14 | |||
15 | * _For writing Lua_: The Lua module `compat53`, which can be require'd | ||
16 | from Lua scripts and run in Lua 5.1, 5.2, and 5.3, including a | ||
17 | backport of the `utf8` module, the 5.3 `table` module, and the | ||
18 | string packing functions straight from the Lua 5.3 sources. | ||
19 | * _For writing C_: A C header and file which can be linked to your | ||
20 | Lua module written in C, providing some functions from the C API | ||
21 | of Lua 5.3 that do not exist in Lua 5.2 or 5.1, making it easier to | ||
22 | write C code that compiles with all three versions of liblua. | ||
23 | |||
24 | ## How to use it | ||
25 | |||
26 | ### Lua module | ||
27 | |||
28 | ```lua | ||
29 | require("compat53") | ||
30 | ``` | ||
31 | |||
32 | `compat53` makes changes to your global environment and does not return | ||
33 | a meaningful return value, so the usual idiom of storing the return of | ||
34 | `require` in a local variable makes no sense. | ||
35 | |||
36 | When run under Lua 5.3, this module does nothing. | ||
37 | |||
38 | When run under Lua 5.2 or 5.1, it replaces some of your standard | ||
39 | functions and adds new ones to bring your environment closer to that | ||
40 | of Lua 5.3. It also tries to load the backported `utf8`, `table`, and | ||
41 | string packing modules automatically. If unsuccessful, pure Lua | ||
42 | versions of the new `table` functions are used as a fallback, and | ||
43 | [Roberto's struct library][1] is tried for string packing. | ||
44 | |||
45 | #### Lua submodules | ||
46 | |||
47 | ```lua | ||
48 | local _ENV = require("compat53.module") | ||
49 | if setfenv then setfenv(1, _ENV) end | ||
50 | ``` | ||
51 | |||
52 | The `compat53.module` module does not modify the global environment, | ||
53 | and so it is safe to use in modules without affecting other Lua files. | ||
54 | It is supposed to be set as the current environment (see above), i.e. | ||
55 | cherry picking individual functions from this module is expressly | ||
56 | *not* supported!). Not all features are available when using this | ||
57 | module (e.g. yieldable (x)pcall support, string/file methods, etc.), | ||
58 | so it is recommended to use plain `require("compat53")` whenever | ||
59 | possible. | ||
60 | |||
61 | ### C code | ||
62 | |||
63 | There are two ways of adding the C API compatibility functions/macros to | ||
64 | your project: | ||
65 | * If `COMPAT53_PREFIX` is *not* `#define`d, `compat-5.3.h` `#include`s | ||
66 | `compat-5.3.c`, and all functions are made `static`. You don't have to | ||
67 | compile/link/add `compat-5.3.c` yourself. This is useful for one-file | ||
68 | projects. | ||
69 | * If `COMPAT53_PREFIX` is `#define`d, all exported functions are renamed | ||
70 | behind the scenes using this prefix to avoid linker conflicts with other | ||
71 | code using this package. This doesn't change the way you call the | ||
72 | compatibility functions in your code. You have to compile and link | ||
73 | `compat-5.3.c` to your project yourself. You can change the way the | ||
74 | functions are exported using the `COMPAT53_API` macro (e.g. if you need | ||
75 | some `__declspec` magic). While it is technically possible to use | ||
76 | the "lua" prefix (and it looks better in the debugger), this is | ||
77 | discouraged because LuaJIT has started to implement its own Lua 5.2+ | ||
78 | C API functions, and with the "lua" prefix you'd violate the | ||
79 | one-definition rule with recent LuaJIT versions. | ||
80 | |||
81 | ## What's implemented | ||
82 | |||
83 | ### Lua | ||
84 | |||
85 | * the `utf8` module backported from the Lua 5.3 sources | ||
86 | * `string.pack`, `string.packsize`, and `string.unpack` from the Lua | ||
87 | 5.3 sources or from the `struct` module. (`struct` is not 100% | ||
88 | compatible to Lua 5.3's string packing!) (See [here][4]) | ||
89 | * `math.maxinteger` and `math.mininteger`, `math.tointeger`, `math.type`, | ||
90 | and `math.ult` (see [here][5]) | ||
91 | * `assert` accepts non-string error messages | ||
92 | * `ipairs` respects `__index` metamethod | ||
93 | * `table.move` | ||
94 | * `table` library respects metamethods | ||
95 | |||
96 | For Lua 5.1 additionally: | ||
97 | * `load` and `loadfile` accept `mode` and `env` parameters | ||
98 | * `table.pack` and `table.unpack` | ||
99 | * string patterns may contain embedded zeros (but see [here][6]) | ||
100 | * `string.rep` accepts `sep` argument | ||
101 | * `string.format` calls `tostring` on arguments for `%s` | ||
102 | * `math.log` accepts base argument | ||
103 | * `xpcall` takes additional arguments | ||
104 | * `pcall` and `xpcall` can execute functions that yield (see | ||
105 | [here][22] for a possible problem with `coroutine.running`) | ||
106 | * `pairs` respects `__pairs` metamethod (see [here][7]) | ||
107 | * `rawlen` (but `#` still doesn't respect `__len` for tables) | ||
108 | * `package.searchers` as alias for `package.loaders` | ||
109 | * `package.searchpath` (see [here][8]) | ||
110 | * `coroutine` functions dealing with the main coroutine (see | ||
111 | [here][22] for a possible problem with `coroutine.running`) | ||
112 | * `coroutine.create` accepts functions written in C | ||
113 | * return code of `os.execute` (see [here][9]) | ||
114 | * `io.write` and `file:write` return file handle | ||
115 | * `io.lines` and `file:lines` accept format arguments (like `io.read`) | ||
116 | (see [here][10] and [here][11]) | ||
117 | * `debug.setmetatable` returns object | ||
118 | * `debug.getuservalue` (see [here][12]) | ||
119 | * `debug.setuservalue` (see [here][13]) | ||
120 | |||
121 | ### C | ||
122 | |||
123 | * `lua_KContext` (see [here][14]) | ||
124 | * `lua_KFunction` (see [here][14]) | ||
125 | * `lua_dump` (extra `strip` parameter, ignored, see [here][15]) | ||
126 | * `lua_getfield` (return value) | ||
127 | * `lua_geti` and `lua_seti` | ||
128 | * `lua_getglobal` (return value) | ||
129 | * `lua_getmetafield` (return value) | ||
130 | * `lua_gettable` (return value) | ||
131 | * `lua_getuservalue` (limited compatibility, see [here][16]) | ||
132 | * `lua_setuservalue` (limited compatibility, see [here][17]) | ||
133 | * `lua_isinteger` | ||
134 | * `lua_numbertointeger` | ||
135 | * `lua_callk` and `lua_pcallk` (limited compatibility, see [here][14]) | ||
136 | * `lua_rawget` and `lua_rawgeti` (return values) | ||
137 | * `lua_rawgetp` and `lua_rawsetp` | ||
138 | * `luaL_requiref` (now checks `package.loaded` first) | ||
139 | * `lua_rotate` | ||
140 | * `lua_stringtonumber` (see [here][18]) | ||
141 | |||
142 | For Lua 5.1 additionally: | ||
143 | * `LUA_OK` | ||
144 | * `LUA_OP*` macros for `lua_arith` and `lua_compare` | ||
145 | * `lua_Unsigned` | ||
146 | * `lua_absindex` | ||
147 | * `lua_arith` (see [here][19]) | ||
148 | * `lua_compare` | ||
149 | * `lua_len`, `lua_rawlen`, and `luaL_len` | ||
150 | * `lua_pushstring`, `lua_pushlstring` (return value) | ||
151 | * `lua_copy` | ||
152 | * `lua_pushglobaltable` | ||
153 | * `luaL_testudata` | ||
154 | * `luaL_setfuncs`, `luaL_newlibtable`, and `luaL_newlib` | ||
155 | * `luaL_setmetatable` | ||
156 | * `luaL_getsubtable` | ||
157 | * `luaL_traceback` | ||
158 | * `luaL_execresult` | ||
159 | * `luaL_fileresult` | ||
160 | * `luaL_checkversion` (with empty body, only to avoid compile errors, | ||
161 | see [here][20]) | ||
162 | * `luaL_tolstring` | ||
163 | * `luaL_buffinitsize`, `luaL_prepbuffsize`, and `luaL_pushresultsize` | ||
164 | (see [here][21]) | ||
165 | * `lua_pushunsigned`, `lua_tounsignedx`, `lua_tounsigned`, | ||
166 | `luaL_checkunsigned`, `luaL_optunsigned`, if | ||
167 | `LUA_COMPAT_APIINTCASTS` is defined. | ||
168 | |||
169 | ## What's not implemented | ||
170 | |||
171 | * bit operators | ||
172 | * integer division operator | ||
173 | * utf8 escape sequences | ||
174 | * 64 bit integers | ||
175 | * `coroutine.isyieldable` | ||
176 | * Lua 5.1: `_ENV`, `goto`, labels, ephemeron tables, etc. See | ||
177 | [`lua-compat-5.2`][2] for a detailed list. | ||
178 | * the following C API functions/macros: | ||
179 | * `lua_isyieldable` | ||
180 | * `lua_getextraspace` | ||
181 | * `lua_arith` (new operators missing) | ||
182 | * `lua_push(v)fstring` (new formats missing) | ||
183 | * `lua_upvalueid` (5.1) | ||
184 | * `lua_upvaluejoin` (5.1) | ||
185 | * `lua_version` (5.1) | ||
186 | * `lua_yieldk` (5.1) | ||
187 | * `luaL_loadbufferx` (5.1) | ||
188 | * `luaL_loadfilex` (5.1) | ||
189 | |||
190 | ## See also | ||
191 | |||
192 | * For Lua-5.2-style APIs under Lua 5.1, see [lua-compat-5.2][2], | ||
193 | which also is the basis for most of the code in this project. | ||
194 | * For Lua-5.1-style APIs under Lua 5.0, see [Compat-5.1][3] | ||
195 | |||
196 | ## Credits | ||
197 | |||
198 | This package contains code written by: | ||
199 | |||
200 | * [The Lua Team](http://www.lua.org) | ||
201 | * Philipp Janda ([@siffiejoe](http://github.com/siffiejoe)) | ||
202 | * Tomás Guisasola Gorham ([@tomasguisasola](http://github.com/tomasguisasola)) | ||
203 | * Hisham Muhammad ([@hishamhm](http://github.com/hishamhm)) | ||
204 | * Renato Maia ([@renatomaia](http://github.com/renatomaia)) | ||
205 | |||
206 | |||
207 | [1]: http://www.inf.puc-rio.br/~roberto/struct/ | ||
208 | [2]: http://github.com/keplerproject/lua-compat-5.2/ | ||
209 | [3]: http://keplerproject.org/compat/ | ||
210 | [4]: https://github.com/keplerproject/lua-compat-5.3/wiki/string_packing | ||
211 | [5]: https://github.com/keplerproject/lua-compat-5.3/wiki/math.type | ||
212 | [6]: https://github.com/keplerproject/lua-compat-5.3/wiki/pattern_matching | ||
213 | [7]: https://github.com/keplerproject/lua-compat-5.3/wiki/pairs | ||
214 | [8]: https://github.com/keplerproject/lua-compat-5.3/wiki/package.searchpath | ||
215 | [9]: https://github.com/keplerproject/lua-compat-5.3/wiki/os.execute | ||
216 | [10]: https://github.com/keplerproject/lua-compat-5.3/wiki/io.lines | ||
217 | [11]: https://github.com/keplerproject/lua-compat-5.3/wiki/file.lines | ||
218 | [12]: https://github.com/keplerproject/lua-compat-5.3/wiki/debug.getuservalue | ||
219 | [13]: https://github.com/keplerproject/lua-compat-5.3/wiki/debug.setuservalue | ||
220 | [14]: https://github.com/keplerproject/lua-compat-5.3/wiki/yieldable_c_functions | ||
221 | [15]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_dump | ||
222 | [16]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_getuservalue | ||
223 | [17]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_setuservalue | ||
224 | [18]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_stringtonumber | ||
225 | [19]: https://github.com/keplerproject/lua-compat-5.3/wiki/lua_arith | ||
226 | [20]: https://github.com/keplerproject/lua-compat-5.3/wiki/luaL_checkversion | ||
227 | [21]: https://github.com/keplerproject/lua-compat-5.3/wiki/luaL_Buffer | ||
228 | [22]: https://github.com/keplerproject/lua-compat-5.3/wiki/coroutine.running | ||
229 | |||