diff --git a/README.md b/README.md
index 5bc0ee77c4..7d28fed8a0 100644
--- a/README.md
+++ b/README.md
@@ -1,7 +1,15 @@
-# Lua
+# Lua Embedded
-This is the repository of Lua development code, as seen by the Lua team. It contains the full history of all commits but is mirrored irregularly. For complete information about Lua, visit [Lua.org](https://www.lua.org/).
+> **NOTE:**
+> This project is actively developed and still working in progress.
-Please **do not** send pull requests. To report issues, post a message to the [Lua mailing list](https://www.lua.org/lua-l.html).
+**Lua Embedded** is a Lua fork that is specially optimized for embedded devices. Most of the original Lua features are preserved to ensure the maximum compatiblity. For complete information about Lua, visit [Lua.org](https://www.lua.org/).
-Download official Lua releases from [Lua.org](https://www.lua.org/download.html).
+## Major Modifications
+* Disabled some uncommonly used built-in libraries.
+* Extracted some system functions to the platform adaptor layer.
+* Removed the dependencies of some system functions (eg. `time`), while maintaining the correctness of related functionalities.
+
+## Build
+
+TBD
diff --git a/lauxlib.c b/lauxlib.c
index 4ca6c65488..1da919d4ff 100644
--- a/lauxlib.c
+++ b/lauxlib.c
@@ -13,7 +13,6 @@
#include
#include
#include
-#include
#include
@@ -1017,11 +1016,11 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s,
static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) {
(void)ud; (void)osize; /* not used */
if (nsize == 0) {
- free(ptr);
+ luaEm_free(ptr);
return NULL;
}
else
- return realloc(ptr, nsize);
+ return luaEm_realloc(ptr, nsize);
}
diff --git a/lauxlib.h b/lauxlib.h
index 5b977e2a39..cd1be6c59a 100644
--- a/lauxlib.h
+++ b/lauxlib.h
@@ -12,6 +12,7 @@
#include
#include
+#include "lembed.h"
#include "luaconf.h"
#include "lua.h"
@@ -257,18 +258,25 @@ typedef struct luaL_Stream {
/* print a string */
#if !defined(lua_writestring)
-#define lua_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
+#define lua_writestring(s,l) (luaEm_writestring((s), sizeof(char) * (l)))
#endif
/* print a newline and flush the output */
#if !defined(lua_writeline)
-#define lua_writeline() (lua_writestring("\n", 1), fflush(stdout))
+#define lua_writeline() (lua_writestring("\n", 1))
#endif
/* print an error message */
#if !defined(lua_writestringerror)
+l_sinline void lua_writestringerror_fwd(const char *fmt, ...) {
+ va_list ap;
+ va_start(ap, fmt);
+ luaEm_vwritestringerror(fmt, ap);
+ va_end(ap);
+}
+
#define lua_writestringerror(s,p) \
- (fprintf(stderr, (s), (p)), fflush(stderr))
+ (lua_writestringerror_fwd((s), (p)))
#endif
/* }================================================================== */
diff --git a/lbaselib.c b/lbaselib.c
index 1d60c9dede..614f03c516 100644
--- a/lbaselib.c
+++ b/lbaselib.c
@@ -111,6 +111,7 @@ static int luaB_tonumber (lua_State *L) {
}
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_error (lua_State *L) {
int level = (int)luaL_optinteger(L, 2, 1);
lua_settop(L, 1);
@@ -121,6 +122,7 @@ static int luaB_error (lua_State *L) {
}
return lua_error(L);
}
+#endif
static int luaB_getmetatable (lua_State *L) {
@@ -337,6 +339,7 @@ static int load_aux (lua_State *L, int status, int envidx) {
}
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_loadfile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
const char *mode = luaL_optstring(L, 2, NULL);
@@ -344,6 +347,7 @@ static int luaB_loadfile (lua_State *L) {
int status = luaL_loadfilex(L, fname, mode);
return load_aux(L, status, env);
}
+#endif
/*
@@ -384,6 +388,7 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
}
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_load (lua_State *L) {
int status;
size_t l;
@@ -402,6 +407,7 @@ static int luaB_load (lua_State *L) {
}
return load_aux(L, status, env);
}
+#endif
/* }====================================================== */
@@ -412,6 +418,7 @@ static int dofilecont (lua_State *L, int d1, lua_KContext d2) {
}
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_dofile (lua_State *L) {
const char *fname = luaL_optstring(L, 1, NULL);
lua_settop(L, 1);
@@ -420,8 +427,10 @@ static int luaB_dofile (lua_State *L) {
lua_callk(L, 0, LUA_MULTRET, 0, dofilecont);
return dofilecont(L, 0, 0);
}
+#endif
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_assert (lua_State *L) {
if (l_likely(lua_toboolean(L, 1))) /* condition is true? */
return lua_gettop(L); /* return all arguments */
@@ -433,6 +442,7 @@ static int luaB_assert (lua_State *L) {
return luaB_error(L); /* call 'error' */
}
}
+#endif
static int luaB_select (lua_State *L) {
@@ -469,6 +479,7 @@ static int finishpcall (lua_State *L, int status, lua_KContext extra) {
}
+#ifdef LUAEM_HAS_BASELIB_EX
static int luaB_pcall (lua_State *L) {
int status;
luaL_checkany(L, 1);
@@ -477,6 +488,7 @@ static int luaB_pcall (lua_State *L) {
status = lua_pcallk(L, lua_gettop(L) - 2, LUA_MULTRET, 0, 0, finishpcall);
return finishpcall(L, status, 0);
}
+#endif
/*
@@ -504,17 +516,11 @@ static int luaB_tostring (lua_State *L) {
static const luaL_Reg base_funcs[] = {
- {"assert", luaB_assert},
{"collectgarbage", luaB_collectgarbage},
- {"dofile", luaB_dofile},
- {"error", luaB_error},
{"getmetatable", luaB_getmetatable},
{"ipairs", luaB_ipairs},
- {"loadfile", luaB_loadfile},
- {"load", luaB_load},
{"next", luaB_next},
{"pairs", luaB_pairs},
- {"pcall", luaB_pcall},
{"print", luaB_print},
{"warn", luaB_warn},
{"rawequal", luaB_rawequal},
@@ -527,6 +533,15 @@ static const luaL_Reg base_funcs[] = {
{"tostring", luaB_tostring},
{"type", luaB_type},
{"xpcall", luaB_xpcall},
+ /* extra functions */
+#ifdef LUAEM_HAS_BASELIB_EX
+ {"pcall", luaB_pcall},
+ {"load", luaB_load},
+ {"loadfile", luaB_loadfile},
+ {"assert", luaB_assert},
+ {"dofile", luaB_dofile},
+ {"error", luaB_error},
+#endif
/* placeholders */
{LUA_GNAME, NULL},
{"_VERSION", NULL},
diff --git a/lcode.c b/lcode.c
index 911dbd5f1e..6797a0869b 100644
--- a/lcode.c
+++ b/lcode.c
@@ -607,7 +607,7 @@ static int luaK_numberK (FuncState *fs, lua_Number r) {
return addk(fs, &o, &o); /* use number itself as key */
else { /* must build an alternative key */
const int nbm = l_floatatt(MANT_DIG);
- const lua_Number q = l_mathop(ldexp)(l_mathop(1.0), -nbm + 1);
+ const lua_Number q = l_ldexp(l_mathop(1.0), -nbm + 1);
const lua_Number k = (ik == 0) ? q : r + r*q; /* new key */
TValue kv;
setfltvalue(&kv, k);
diff --git a/lcorolib.c b/lcorolib.c
index 40b880b14d..246d5de202 100644
--- a/lcorolib.c
+++ b/lcorolib.c
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifdef LUAEM_HAS_COROLIB
+
#define lcorolib_c
#define LUA_LIB
@@ -208,3 +210,4 @@ LUAMOD_API int luaopen_coroutine (lua_State *L) {
return 1;
}
+#endif
diff --git a/ldblib.c b/ldblib.c
index 6dcbaa9824..77473ea575 100644
--- a/ldblib.c
+++ b/ldblib.c
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifdef LUAEM_HAS_DBLIB
+
#define ldblib_c
#define LUA_LIB
@@ -481,3 +483,4 @@ LUAMOD_API int luaopen_debug (lua_State *L) {
return 1;
}
+#endif
diff --git a/ldo.c b/ldo.c
index c30cde76f5..40db92229e 100644
--- a/ldo.c
+++ b/ldo.c
@@ -10,12 +10,12 @@
#include "lprefix.h"
-#include
#include
#include
#include "lua.h"
+#include "lembed.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
@@ -70,9 +70,9 @@
#else /* }{ */
/* ISO C handling with long jumps */
-#define LUAI_THROW(L,c) longjmp((c)->b, 1)
-#define LUAI_TRY(L,c,a) if (setjmp((c)->b) == 0) { a }
-#define luai_jmpbuf jmp_buf
+#define LUAI_THROW(L,c) luaEm_restore_context(&(c)->b, 1)
+#define LUAI_TRY(L,c,a) if (luaEm_APIImpl->save_context(&(c)->b) == 0) { a }
+#define luai_jmpbuf luaEm_jmp_buf
#endif /* } */
@@ -129,7 +129,7 @@ l_noret luaD_throw (lua_State *L, int errcode) {
lua_unlock(L);
g->panic(L); /* call panic function (last chance to jump out) */
}
- abort();
+ luaEm_abort();
}
}
}
diff --git a/lembed.h b/lembed.h
new file mode 100644
index 0000000000..d6dd497002
--- /dev/null
+++ b/lembed.h
@@ -0,0 +1,130 @@
+/*
+** $Id: lembed.h $
+** Embedded platform adaptor layer of Lua
+** See Copyright Notice in lua.h
+*/
+
+#ifndef lembed_h
+#define lembed_h
+
+#include
+#include
+
+#include "luaconf.h"
+
+
+#ifndef HUGE_VAL
+#if defined(__GNUC__)
+#define HUGE_VAL __builtin_huge_val()
+#endif
+#endif
+
+#ifndef LUAEM_JMP_BUF_SIZE
+#if __has_include()
+#include
+#define LUAEM_JMP_BUF_SIZE (sizeof(jmp_buf))
+#else
+#define LUAEM_JMP_BUF_SIZE (sizeof(void *) * 32)
+#endif
+#endif
+
+typedef union {
+ char padding[LUAEM_JMP_BUF_SIZE];
+#ifdef LUAEM_JMP_BUF_TYPE
+ LUAEM_JMP_BUF_TYPE buf;
+#endif
+} luaEm_jmp_buf;
+
+#if !defined(__clock_t_defined) && !defined(_CLOCK_T_DECLARED)
+typedef unsigned long clock_t;
+#define __clock_t_defined
+#define _CLOCK_T_DECLARED
+#endif
+
+#define LUA_MATH_FLOOR l_mathop(floor)
+#define LUA_MATH_CEIL l_mathop(ceil)
+#define LUA_MATH_LOG l_mathop(log)
+#define LUA_MATH_LOG2 l_mathop(log2)
+#define LUA_MATH_LOG10 l_mathop(log10)
+#define LUA_MATH_SIN l_mathop(sin)
+#define LUA_MATH_COS l_mathop(cos)
+#define LUA_MATH_TAN l_mathop(tan)
+#define LUA_MATH_ASIN l_mathop(asin)
+#define LUA_MATH_ACOS l_mathop(acos)
+#define LUA_MATH_ATAN2 l_mathop(atan2)
+#define LUA_MATH_POW l_mathop(pow)
+#define LUA_MATH_FMOD l_mathop(fmod)
+#define LUA_MATH_EXP l_mathop(exp)
+#define LUA_MATH_LDEXP l_mathop(ldexp)
+#define LUA_MATH_FREXP l_mathop(frexp)
+
+#define LUAEM_API_DEFS \
+ LUAEM_API_DEF_EMIT(char, getlocaledecpoint, (void), ()) \
+ LUAEM_API_DEF_EMIT(clock_t, clock, (void), ()) \
+ LUAEM_API_DEF_EMIT(time_t, time, (time_t *tloc), (tloc)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_FLOOR, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_CEIL, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_LOG, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_LOG2, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_LOG10, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_SIN, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_COS, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_TAN, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_ASIN, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_ACOS, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_ATAN2, (double x, double y), (x, y)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_POW, (double a, double b), (a, b)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_FMOD, (double a, double b), (a, b)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_EXP, (double x), (x)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_LDEXP, (double a, int b), (a, b)) \
+ LUAEM_API_DEF_EMIT(double, LUA_MATH_FREXP, (double a, int *b), (a, b)) \
+ LUAEM_API_DEF_EMIT(LUA_NUMBER, str2number, (const char *s, char *endptr), (s, endptr)) \
+ LUAEM_API_DEF_EMIT(char *, strpbrk, (const char *s, const char *c), (s, c)) \
+ LUAEM_API_DEF_EMIT(int, save_context, (luaEm_jmp_buf *env), (env)) \
+ LUAEM_API_DEF_EMIT(void, restore_context, (luaEm_jmp_buf *env, int status), (env, status)) \
+ LUAEM_API_DEF_EMIT(void *, realloc, (void *ptr, size_t size), (ptr, size)) \
+ LUAEM_API_DEF_EMIT(void, free, (void *ptr), (ptr)) \
+ LUAEM_API_DEF_EMIT(void, vwritestringerror, (const char *fmt, va_list ap), (fmt, ap)) \
+ LUAEM_API_DEF_EMIT(void, writestring, (const char *s, size_t size), (s, size)) \
+ LUAEM_API_DEF_EMIT(void, abort, (void), ())
+
+typedef struct luaEm_API {
+#define LUAEM_API_DEF_EMIT(tret, name, args, cargs) \
+ tret (*name) args;
+ LUAEM_API_DEFS
+#undef LUAEM_API_DEF_EMIT
+} luaEm_API;
+
+extern luaEm_API *luaEm_APIImpl;
+
+#if !defined(LUA_USE_C89)
+#define l_inline inline
+#elif defined(__GNUC__)
+#define l_inline __inline__
+#else
+#define l_inline /* empty */
+#endif
+
+#define l_sinline static l_inline
+
+#define LUAEM_API_DEF_EMIT(tret, name, args, cargs) \
+ l_sinline tret luaEm_##name args { \
+ return luaEm_APIImpl->name cargs; \
+ }
+LUAEM_API_DEFS
+#undef LUAEM_API_DEF_EMIT
+
+#ifndef BUFSIZ
+#define BUFSIZ 1024
+#endif
+
+/*
+@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point).
+** Change that if you do not want to use C locales. (Code using this
+** macro must include the header 'locale.h'.)
+*/
+#if !defined(lua_getlocaledecpoint)
+#define lua_getlocaledecpoint() (luaEm_getlocaledecpoint())
+#endif
+
+#endif
diff --git a/linit.c b/linit.c
index 69808f84f4..76486c220c 100644
--- a/linit.c
+++ b/linit.c
@@ -41,15 +41,29 @@
*/
static const luaL_Reg loadedlibs[] = {
{LUA_GNAME, luaopen_base},
+#ifdef LUAEM_HAS_LOADLIB
{LUA_LOADLIBNAME, luaopen_package},
+#endif
+#ifdef LUAEM_HAS_COROLIB
{LUA_COLIBNAME, luaopen_coroutine},
+#endif
{LUA_TABLIBNAME, luaopen_table},
+#ifdef LUAEM_HAS_IOLIB
{LUA_IOLIBNAME, luaopen_io},
+#endif
+#ifdef LUAEM_HAS_OSLIB
{LUA_OSLIBNAME, luaopen_os},
+#endif
+#ifdef LUAEM_HAS_STRLIB
{LUA_STRLIBNAME, luaopen_string},
+#endif
{LUA_MATHLIBNAME, luaopen_math},
+#ifdef LUAEM_HAS_UTF8LIB
{LUA_UTF8LIBNAME, luaopen_utf8},
+#endif
+#ifdef LUAEM_HAS_DBLIB
{LUA_DBLIBNAME, luaopen_debug},
+#endif
{NULL, NULL}
};
diff --git a/liolib.c b/liolib.c
index b08397da45..d0fee6c2e1 100644
--- a/liolib.c
+++ b/liolib.c
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifdef LUAEM_HAS_IOLIB
+
#define liolib_c
#define LUA_LIB
@@ -19,6 +21,7 @@
#include "lua.h"
+#include "lembed.h"
#include "lauxlib.h"
#include "lualib.h"
@@ -826,3 +829,4 @@ LUAMOD_API int luaopen_io (lua_State *L) {
return 1;
}
+#endif
diff --git a/llex.c b/llex.c
index b0dc0acc24..1854c2349c 100644
--- a/llex.c
+++ b/llex.c
@@ -10,7 +10,6 @@
#include "lprefix.h"
-#include
#include
#include "lua.h"
diff --git a/llimits.h b/llimits.h
index 52a32f92e3..aa349d4027 100644
--- a/llimits.h
+++ b/llimits.h
@@ -13,6 +13,7 @@
#include "lua.h"
+#include "lembed.h"
/*
@@ -318,14 +319,14 @@ typedef l_uint32 Instruction;
*/
#if !defined(luai_nummod)
#define luai_nummod(L,a,b,m) \
- { (void)L; (m) = l_mathop(fmod)(a,b); \
+ { (void)L; (m) = l_fmod(a,b); \
if (((m) > 0) ? (b) < 0 : ((m) < 0 && (b) > 0)) (m) += (b); }
#endif
/* exponentiation */
#if !defined(luai_numpow)
#define luai_numpow(L,a,b) \
- ((void)L, (b == 2) ? (a)*(a) : l_mathop(pow)(a,b))
+ ((void)L, (b == 2) ? (a)*(a) : l_pow(a,b))
#endif
/* the others are quite standard operations */
diff --git a/lmathlib.c b/lmathlib.c
index e0c61a168d..3ee0916ee6 100644
--- a/lmathlib.c
+++ b/lmathlib.c
@@ -18,6 +18,8 @@
#include "lua.h"
+#include "lembed.h"
+#include "llimits.h"
#include "lauxlib.h"
#include "lualib.h"
@@ -32,40 +34,43 @@ static int math_abs (lua_State *L) {
if (n < 0) n = (lua_Integer)(0u - (lua_Unsigned)n);
lua_pushinteger(L, n);
}
- else
- lua_pushnumber(L, l_mathop(fabs)(luaL_checknumber(L, 1)));
+ else {
+ LUA_NUMBER n = luaL_checknumber(L, 1);
+ if (n < 0) n = -n;
+ lua_pushnumber(L, n);
+ }
return 1;
}
static int math_sin (lua_State *L) {
- lua_pushnumber(L, l_mathop(sin)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(sin))(luaL_checknumber(L, 1)));
return 1;
}
static int math_cos (lua_State *L) {
- lua_pushnumber(L, l_mathop(cos)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(cos))(luaL_checknumber(L, 1)));
return 1;
}
static int math_tan (lua_State *L) {
- lua_pushnumber(L, l_mathop(tan)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(tan))(luaL_checknumber(L, 1)));
return 1;
}
static int math_asin (lua_State *L) {
- lua_pushnumber(L, l_mathop(asin)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(asin))(luaL_checknumber(L, 1)));
return 1;
}
static int math_acos (lua_State *L) {
- lua_pushnumber(L, l_mathop(acos)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(acos))(luaL_checknumber(L, 1)));
return 1;
}
static int math_atan (lua_State *L) {
lua_Number y = luaL_checknumber(L, 1);
lua_Number x = luaL_optnumber(L, 2, 1);
- lua_pushnumber(L, l_mathop(atan2)(y, x));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(atan2))(y, x));
return 1;
}
@@ -96,7 +101,7 @@ static int math_floor (lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own floor */
else {
- lua_Number d = l_mathop(floor)(luaL_checknumber(L, 1));
+ lua_Number d = l_floor(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
@@ -107,7 +112,7 @@ static int math_ceil (lua_State *L) {
if (lua_isinteger(L, 1))
lua_settop(L, 1); /* integer is its own ceil */
else {
- lua_Number d = l_mathop(ceil)(luaL_checknumber(L, 1));
+ lua_Number d = l_ceil(luaL_checknumber(L, 1));
pushnumint(L, d);
}
return 1;
@@ -125,7 +130,7 @@ static int math_fmod (lua_State *L) {
lua_pushinteger(L, lua_tointeger(L, 1) % d);
}
else
- lua_pushnumber(L, l_mathop(fmod)(luaL_checknumber(L, 1),
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(fmod))(luaL_checknumber(L, 1),
luaL_checknumber(L, 2)));
return 1;
}
@@ -144,7 +149,7 @@ static int math_modf (lua_State *L) {
else {
lua_Number n = luaL_checknumber(L, 1);
/* integer part (rounds toward zero) */
- lua_Number ip = (n < 0) ? l_mathop(ceil)(n) : l_mathop(floor)(n);
+ lua_Number ip = (n < 0) ? l_ceil(n) : l_floor(n);
pushnumint(L, ip);
/* fractional part (test needed for inf/-inf) */
lua_pushnumber(L, (n == ip) ? l_mathop(0.0) : (n - ip));
@@ -170,25 +175,25 @@ static int math_log (lua_State *L) {
lua_Number x = luaL_checknumber(L, 1);
lua_Number res;
if (lua_isnoneornil(L, 2))
- res = l_mathop(log)(x);
+ res = (luaEm_APIImpl->l_mathop(log))(x);
else {
lua_Number base = luaL_checknumber(L, 2);
#if !defined(LUA_USE_C89)
if (base == l_mathop(2.0))
- res = l_mathop(log2)(x);
+ res = (luaEm_APIImpl->l_mathop(log2))(x);
else
#endif
if (base == l_mathop(10.0))
- res = l_mathop(log10)(x);
+ res = (luaEm_APIImpl->l_mathop(log10))(x);
else
- res = l_mathop(log)(x)/l_mathop(log)(base);
+ res = (luaEm_APIImpl->l_mathop(log))(x)/(luaEm_APIImpl->l_mathop(log))(base);
}
lua_pushnumber(L, res);
return 1;
}
static int math_exp (lua_State *L) {
- lua_pushnumber(L, l_mathop(exp)(luaL_checknumber(L, 1)));
+ lua_pushnumber(L, (luaEm_APIImpl->l_mathop(exp))(luaL_checknumber(L, 1)));
return 1;
}
@@ -609,7 +614,7 @@ static void setseed (lua_State *L, Rand64 *state,
** randomization).
*/
static void randseed (lua_State *L, RanState *state) {
- lua_Unsigned seed1 = (lua_Unsigned)time(NULL);
+ lua_Unsigned seed1 = (lua_Unsigned)luaEm_clock();
lua_Unsigned seed2 = (lua_Unsigned)(size_t)L;
setseed(L, state->s, seed1, seed2);
}
diff --git a/loadlib.c b/loadlib.c
index d792dffaa0..c58145e06d 100644
--- a/loadlib.c
+++ b/loadlib.c
@@ -8,6 +8,8 @@
** systems.
*/
+#ifdef LUAEM_HAS_LOADLIB
+
#define loadlib_c
#define LUA_LIB
@@ -765,3 +767,4 @@ LUAMOD_API int luaopen_package (lua_State *L) {
return 1; /* return 'package' table */
}
+#endif
diff --git a/lobject.c b/lobject.c
index f73ffc6d92..6becaa1032 100644
--- a/lobject.c
+++ b/lobject.c
@@ -10,7 +10,6 @@
#include "lprefix.h"
-#include
#include
#include
#include
@@ -19,6 +18,7 @@
#include "lua.h"
+#include "lembed.h"
#include "lctype.h"
#include "ldebug.h"
#include "ldo.h"
@@ -227,8 +227,9 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
*/
static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
char *endptr;
- *result = (mode == 'x') ? lua_strx2number(s, &endptr) /* try to convert */
- : lua_str2number(s, &endptr);
+ // TODO: in most case, strtod does the hexadecimal conversion, so we will
+ // not use 'lua_strx2number' anymore.
+ *result = luaEm_str2number(s, endptr); /* try to convert */
if (endptr == s) return NULL; /* nothing recognized? */
while (lisspace(cast_uchar(*endptr))) endptr++; /* skip trailing spaces */
return (*endptr == '\0') ? endptr : NULL; /* OK iff no trailing chars */
@@ -250,7 +251,7 @@ static const char *l_str2dloc (const char *s, lua_Number *result, int mode) {
*/
static const char *l_str2d (const char *s, lua_Number *result) {
const char *endptr;
- const char *pmode = strpbrk(s, ".xXnN"); /* look for special chars */
+ const char *pmode = luaEm_strpbrk(s, ".xXnN"); /* look for special chars */
int mode = pmode ? ltolower(cast_uchar(*pmode)) : 0;
if (mode == 'n') /* reject 'inf' and 'nan' */
return NULL;
diff --git a/loslib.c b/loslib.c
index 854dcf691e..a2c0cd282a 100644
--- a/loslib.c
+++ b/loslib.c
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifdef LUAEM_HAS_OSLIB
+
#define loslib_c
#define LUA_LIB
@@ -426,3 +428,4 @@ LUAMOD_API int luaopen_os (lua_State *L) {
return 1;
}
+#endif
diff --git a/lstate.c b/lstate.c
index 1fbefb4b14..60874b60e8 100644
--- a/lstate.c
+++ b/lstate.c
@@ -15,6 +15,7 @@
#include "lua.h"
+#include "lembed.h"
#include "lapi.h"
#include "ldebug.h"
#include "ldo.h"
@@ -70,7 +71,7 @@ typedef struct LG {
static unsigned int luai_makeseed (lua_State *L) {
char buff[3 * sizeof(size_t)];
- unsigned int h = cast_uint(time(NULL));
+ unsigned int h = cast_uint(luaEm_clock());
int p = 0;
addbuff(buff, p, L); /* heap variable */
addbuff(buff, p, &h); /* local variable */
diff --git a/lstate.h b/lstate.h
index 2e90781872..b474460c25 100644
--- a/lstate.h
+++ b/lstate.h
@@ -122,7 +122,11 @@ struct lua_longjmp; /* defined in ldo.c */
** is thread safe
*/
#if !defined(l_signalT)
+#if __has_include()
#include
+#else
+typedef int sig_atomic_t;
+#endif
#define l_signalT sig_atomic_t
#endif
diff --git a/lstrlib.c b/lstrlib.c
index 0b4fdbb7b5..9e74c0068b 100644
--- a/lstrlib.c
+++ b/lstrlib.c
@@ -7,13 +7,14 @@
#define lstrlib_c
#define LUA_LIB
+#ifdef LUAEM_HAS_STRLIB
+
#include "lprefix.h"
#include
#include
#include
-#include
#include
#include
#include
@@ -22,6 +23,8 @@
#include "lua.h"
+#include "lembed.h"
+#include "llimits.h"
#include "lauxlib.h"
#include "lualib.h"
@@ -746,7 +749,7 @@ static int push_captures (MatchState *ms, const char *s, const char *e) {
static int nospecials (const char *p, size_t l) {
size_t upto = 0;
do {
- if (strpbrk(p + upto, SPECIALS))
+ if (luaEm_strpbrk(p + upto, SPECIALS))
return 0; /* pattern has a special character */
upto += strlen(p + upto) + 1; /* may have more after \0 */
} while (upto <= l);
@@ -1014,7 +1017,7 @@ static int str_gsub (lua_State *L) {
** Add integer part of 'x' to buffer and return new 'x'
*/
static lua_Number adddigit (char *buff, int n, lua_Number x) {
- lua_Number dd = l_mathop(floor)(x); /* get integer part from 'x' */
+ lua_Number dd = l_floor(x); /* get integer part from 'x' */
int d = (int)dd;
buff[n] = (d < 10 ? d + '0' : d - 10 + 'a'); /* add to buffer */
return x - dd; /* return what is left */
@@ -1872,3 +1875,4 @@ LUAMOD_API int luaopen_string (lua_State *L) {
return 1;
}
+#endif
diff --git a/ltable.c b/ltable.c
index cc7993e083..0e203fefc9 100644
--- a/ltable.c
+++ b/ltable.c
@@ -28,6 +28,7 @@
#include "lua.h"
+#include "lembed.h"
#include "ldebug.h"
#include "ldo.h"
#include "lgc.h"
@@ -131,7 +132,7 @@ static Node *hashint (const Table *t, lua_Integer i) {
static int l_hashfloat (lua_Number n) {
int i;
lua_Integer ni;
- n = l_mathop(frexp)(n, &i) * -cast_num(INT_MIN);
+ n = l_frexp(n, &i) * -cast_num(INT_MIN);
if (!lua_numbertointeger(n, &ni)) { /* is 'n' inf/-inf/NaN? */
lua_assert(luai_numisnan(n) || l_mathop(fabs)(n) == cast_num(HUGE_VAL));
return 0;
diff --git a/ltablib.c b/ltablib.c
index e6bc4d04af..b5b6a7cb68 100644
--- a/ltablib.c
+++ b/ltablib.c
@@ -16,6 +16,7 @@
#include "lua.h"
+#include "lembed.h"
#include "lauxlib.h"
#include "lualib.h"
@@ -238,18 +239,16 @@ typedef unsigned int IdxT;
#define sof(e) (sizeof(e) / sizeof(unsigned int))
/*
-** Use 'time' and 'clock' as sources of "randomness". Because we don't
-** know the types 'clock_t' and 'time_t', we cannot cast them to
-** anything without risking overflows. A safe way to use their values
-** is to copy them to an array of a known type and use the array values.
+** Use 'clock' as sources of "randomness". Because we don't know the
+** types 'clock_t', we cannot cast them to anything without risking
+** overflows. A safe way to use their values is to copy them to an
+** array of a known type and use the array values.
*/
static unsigned int l_randomizePivot (void) {
- clock_t c = clock();
- time_t t = time(NULL);
- unsigned int buff[sof(c) + sof(t)];
+ clock_t c = luaEm_clock();
+ unsigned int buff[sof(c)];
unsigned int i, rnd = 0;
memcpy(buff, &c, sof(c) * sizeof(unsigned int));
- memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int));
for (i = 0; i < sof(buff); i++)
rnd += buff[i];
return rnd;
diff --git a/lua.h b/lua.h
index bfba4d1e1b..7f4f8a9aac 100644
--- a/lua.h
+++ b/lua.h
@@ -2,6 +2,7 @@
** $Id: lua.h $
** Lua - A Scripting Language
** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
+** Modified by Cyandev (unixzii@gmail.com)
** See Copyright Notice at the end of this file
*/
@@ -493,6 +494,7 @@ struct lua_Debug {
/******************************************************************************
* Copyright (C) 1994-2022 Lua.org, PUC-Rio.
+* Copyright (C) 2022-2023 Cyandev .
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
diff --git a/luaconf.h b/luaconf.h
index e4650fbce8..344db8807d 100644
--- a/luaconf.h
+++ b/luaconf.h
@@ -400,7 +400,12 @@
/* The following definitions are good for most cases here */
-#define l_floor(x) (l_mathop(floor)(x))
+#define l_floor(x) ((luaEm_APIImpl->l_mathop(floor))(x))
+#define l_ceil(x) ((luaEm_APIImpl->l_mathop(ceil))(x))
+#define l_pow(a, b) ((luaEm_APIImpl->l_mathop(pow))(a, b))
+#define l_fmod(a, b) ((luaEm_APIImpl->l_mathop(fmod))(a, b))
+#define l_ldexp(a, b) ((luaEm_APIImpl->l_mathop(ldexp))(a, b))
+#define l_frexp(x, i) ((luaEm_APIImpl->l_mathop(frexp))(x, i))
#define lua_number2str(s,sz,n) \
l_sprintf((s), sz, LUA_NUMBER_FMT, (LUAI_UACNUMBER)(n))
@@ -647,16 +652,6 @@
#endif
-/*
-@@ lua_getlocaledecpoint gets the locale "radix character" (decimal point).
-** Change that if you do not want to use C locales. (Code using this
-** macro must include the header 'locale.h'.)
-*/
-#if !defined(lua_getlocaledecpoint)
-#define lua_getlocaledecpoint() (localeconv()->decimal_point[0])
-#endif
-
-
/*
** macros to improve jump prediction, used mostly for error handling
** and debug facilities. (Some macros in the Lua API use these macros.
diff --git a/lutf8lib.c b/lutf8lib.c
index 3a5b9bc38a..9b048171fb 100644
--- a/lutf8lib.c
+++ b/lutf8lib.c
@@ -4,6 +4,8 @@
** See Copyright Notice in lua.h
*/
+#ifdef LUAEM_HAS_UTF8LIB
+
#define lutf8lib_c
#define LUA_LIB
@@ -289,3 +291,4 @@ LUAMOD_API int luaopen_utf8 (lua_State *L) {
return 1;
}
+#endif
diff --git a/lvm.c b/lvm.c
index 2e84dc63c1..760d6a7a23 100644
--- a/lvm.c
+++ b/lvm.c
@@ -369,8 +369,8 @@ void luaV_finishset (lua_State *L, const TValue *t, TValue *key,
** Compare two strings 'ls' x 'rs', returning an integer less-equal-
** -greater than zero if 'ls' is less-equal-greater than 'rs'.
** The code is a little tricky because it allows '\0' in the strings
-** and it uses 'strcoll' (to respect locales) for each segments
-** of the strings.
+** and it uses 'strcmp' instead of 'strcoll' (for better portability)
+** for each segments of the strings.
*/
static int l_strcmp (const TString *ls, const TString *rs) {
const char *l = getstr(ls);
@@ -378,7 +378,7 @@ static int l_strcmp (const TString *ls, const TString *rs) {
const char *r = getstr(rs);
size_t lr = tsslen(rs);
for (;;) { /* for each segment */
- int temp = strcoll(l, r);
+ int temp = strcmp(l, r);
if (temp != 0) /* not equal? */
return temp; /* done */
else { /* strings are equal up to a '\0' */
diff --git a/onelua.c b/onelua.c
index 3c605981f0..89a7e041d9 100644
--- a/onelua.c
+++ b/onelua.c
@@ -2,15 +2,6 @@
* one.c -- Lua core, libraries, and interpreter in a single file
*/
-/* default is to build the full interpreter */
-#ifndef MAKE_LIB
-#ifndef MAKE_LUAC
-#ifndef MAKE_LUA
-#define MAKE_LUA
-#endif
-#endif
-#endif
-
/* choose suitable platform-specific features */
/* some of these may need extra libraries such as -ldl -lreadline -lncurses */
#if 0
@@ -24,22 +15,6 @@
#include "lprefix.h"
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-#include
-
/* setup for luaconf.h */
#define LUA_CORE
diff --git a/platforms/posix_x86_64.c b/platforms/posix_x86_64.c
new file mode 100644
index 0000000000..428d5cfc4b
--- /dev/null
+++ b/platforms/posix_x86_64.c
@@ -0,0 +1,156 @@
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include "lembed.h"
+
+static char getlocaledecpoint_imp() {
+ return localeconv()->decimal_point[0];
+}
+
+clock_t clock_imp() {
+ return clock();
+}
+
+time_t time_imp(time_t *t) {
+ return time(t);
+}
+
+double floor_imp(double x) {
+ return floor(x);
+}
+
+double ceil_imp(double x) {
+ return ceil(x);
+}
+
+double log_imp(double x) {
+ return log(x);
+}
+
+double log2_imp(double x) {
+ return log2(x);
+}
+
+double log10_imp(double x) {
+ return log10(x);
+}
+
+double sin_imp(double x) {
+ return sin(x);
+}
+
+double cos_imp(double x) {
+ return cos(x);
+}
+
+double tan_imp(double x) {
+ return tan(x);
+}
+
+double asin_imp(double x) {
+ return asin(x);
+}
+
+double acos_imp(double x) {
+ return acos(x);
+}
+
+double atan2_imp(double x, double y) {
+ return atan2(x, y);
+}
+
+double pow_imp(double a, double b) {
+ return pow(a, b);
+}
+
+double fmod_imp(double a, double b) {
+ return fmod(a, b);
+}
+
+double exp_imp(double x) {
+ return exp(x);
+}
+
+double ldexp_imp(double a, int b) {
+ return ldexp(a, b);
+}
+
+double frexp_imp(double a, int *b) {
+ return frexp(a, b);
+}
+
+LUA_NUMBER str2number_imp(const char *s, const char *endptr) {
+ return strtod(s, &endptr);
+}
+
+char *strpbrk_imp(const char *s, const char *c) {
+ return strpbrk(s, c);
+}
+
+__attribute__((naked)) int save_context_imp(luaEm_jmp_buf *env) {
+ asm ("jmpq *%0"
+ :
+ : "rax"(&setjmp));
+}
+
+void restore_context_imp(luaEm_jmp_buf *env, int status) {
+ longjmp((void *) env, status);
+}
+
+void *realloc_imp(void *ptr, size_t size) {
+ return realloc(ptr, size);
+}
+
+void free_imp(void *ptr) {
+ free(ptr);
+}
+
+void vwritestringerror_imp(const char *fmt, va_list ap) {
+ vfprintf(stderr, fmt, ap);
+}
+
+void writestring_imp(const char *s, size_t size) {
+ fwrite(s, sizeof(char), size, stdout);
+}
+
+void abort_imp() {
+ abort();
+}
+
+static luaEm_API table = {
+ .getlocaledecpoint = &getlocaledecpoint_imp,
+ .clock = &clock_imp,
+ .time = &time_imp,
+ .floor = &floor_imp,
+ .ceil = &ceil_imp,
+ .log = &log_imp,
+ .log2 = &log2_imp,
+ .log10 = &log10_imp,
+ .sin = &sin_imp,
+ .cos = &cos_imp,
+ .tan = &tan_imp,
+ .asin = &asin_imp,
+ .acos = &acos_imp,
+ .atan2 = &atan2_imp,
+ .pow = &pow_imp,
+ .fmod = &fmod_imp,
+ .exp = &exp_imp,
+ .ldexp = &ldexp_imp,
+ .frexp = &frexp_imp,
+ .str2number = &str2number_imp,
+ .strpbrk = &strpbrk_imp,
+ .save_context = &save_context_imp,
+ .restore_context = &restore_context_imp,
+ .realloc = &realloc_imp,
+ .free = &free_imp,
+ .vwritestringerror = &vwritestringerror_imp,
+ .writestring = &writestring_imp,
+ .abort = &abort_imp
+};
+
+luaEm_API *luaEm_APIImpl = &table;