diff options
| author | Philipp Janda <siffiejoe@gmx.net> | 2017-09-16 15:11:29 +0200 |
|---|---|---|
| committer | Philipp Janda <siffiejoe@gmx.net> | 2017-09-16 15:11:29 +0200 |
| commit | 3b52d81dcf1b5d6d49e1837612e207b2cab6b74d (patch) | |
| tree | b01cca67ed685b6b3083ba6aae719eeb3cb54a15 /c-api | |
| parent | 8425f997b48b2e2bdef2b2a614472769d92da73f (diff) | |
| download | lua-compat-5.3-3b52d81dcf1b5d6d49e1837612e207b2cab6b74d.tar.gz lua-compat-5.3-3b52d81dcf1b5d6d49e1837612e207b2cab6b74d.tar.bz2 lua-compat-5.3-3b52d81dcf1b5d6d49e1837612e207b2cab6b74d.zip | |
Add Lua 5.3 version of `lua_load`.
Change `luaL_loadfilex` to use the modified `lua_load` function.
Add more credits to the README.md.
Diffstat (limited to 'c-api')
| -rw-r--r-- | c-api/compat-5.3.c | 104 | ||||
| -rw-r--r-- | c-api/compat-5.3.h | 3 |
2 files changed, 70 insertions, 37 deletions
diff --git a/c-api/compat-5.3.c b/c-api/compat-5.3.c index bd9e277..4bd984a 100644 --- a/c-api/compat-5.3.c +++ b/c-api/compat-5.3.c | |||
| @@ -28,9 +28,12 @@ | |||
| 28 | #endif /* VC++ _fsopen for share-allowed file read */ | 28 | #endif /* VC++ _fsopen for share-allowed file read */ |
| 29 | 29 | ||
| 30 | #ifndef COMPAT53_HAVE_STRERROR_R | 30 | #ifndef COMPAT53_HAVE_STRERROR_R |
| 31 | # if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) | 31 | # if defined(__GLIBC__) || defined(_POSIX_VERSION) || defined(__APPLE__) || \ |
| 32 | (!defined (__MINGW32__) && defined(__GNUC__) && (__GNUC__ < 6)) | ||
| 32 | # define COMPAT53_HAVE_STRERROR_R 1 | 33 | # define COMPAT53_HAVE_STRERROR_R 1 |
| 33 | # if ((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || (defined(_XOPEN_SOURCE) || _XOPEN_SOURCE >= 600)) && (!defined(_GNU_SOURCE) || !_GNU_SOURCE) | 34 | # if ((defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE >= 200112L) || \ |
| 35 | (defined(_XOPEN_SOURCE) || _XOPEN_SOURCE >= 600)) && \ | ||
| 36 | (!defined(_GNU_SOURCE) || !_GNU_SOURCE) | ||
| 34 | # ifndef COMPAT53_HAVE_STRERROR_R_XSI | 37 | # ifndef COMPAT53_HAVE_STRERROR_R_XSI |
| 35 | # define COMPAT53_HAVE_STRERROR_R_XSI 1 | 38 | # define COMPAT53_HAVE_STRERROR_R_XSI 1 |
| 36 | # endif /* XSI-Compliant strerror_r */ | 39 | # endif /* XSI-Compliant strerror_r */ |
| @@ -57,7 +60,8 @@ | |||
| 57 | #endif /* strerror_r */ | 60 | #endif /* strerror_r */ |
| 58 | 61 | ||
| 59 | #ifndef COMPAT53_HAVE_STRERROR_S | 62 | #ifndef COMPAT53_HAVE_STRERROR_S |
| 60 | # if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__) | 63 | # if defined(_MSC_VER) || (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L) || \ |
| 64 | (defined(__STDC_LIB_EXT1__) && __STDC_LIB_EXT1__) | ||
| 61 | # define COMPAT53_HAVE_STRERROR_S 1 | 65 | # define COMPAT53_HAVE_STRERROR_S 1 |
| 62 | # else /* not VC++ or C11 */ | 66 | # else /* not VC++ or C11 */ |
| 63 | # define COMPAT53_HAVE_STRERROR_S 0 | 67 | # define COMPAT53_HAVE_STRERROR_S 0 |
| @@ -68,7 +72,8 @@ | |||
| 68 | #define COMPAT53_LUA_FILE_BUFFER_SIZE 4096 | 72 | #define COMPAT53_LUA_FILE_BUFFER_SIZE 4096 |
| 69 | #endif /* Lua File Buffer Size */ | 73 | #endif /* Lua File Buffer Size */ |
| 70 | 74 | ||
| 71 | static char* compat53_strerror(int en, char* buff, size_t sz) { | 75 | |
| 76 | static char* compat53_strerror (int en, char* buff, size_t sz) { | ||
| 72 | #if COMPAT53_HAVE_STRERROR_R | 77 | #if COMPAT53_HAVE_STRERROR_R |
| 73 | /* use strerror_r here, because it's available on these specific platforms */ | 78 | /* use strerror_r here, because it's available on these specific platforms */ |
| 74 | #if COMPAT53_HAVE_STRERROR_R_XSI | 79 | #if COMPAT53_HAVE_STRERROR_R_XSI |
| @@ -98,6 +103,7 @@ static char* compat53_strerror(int en, char* buff, size_t sz) { | |||
| 98 | #endif | 103 | #endif |
| 99 | } | 104 | } |
| 100 | 105 | ||
| 106 | |||
| 101 | COMPAT53_API int lua_absindex (lua_State *L, int i) { | 107 | COMPAT53_API int lua_absindex (lua_State *L, int i) { |
| 102 | if (i < 0 && i > LUA_REGISTRYINDEX) | 108 | if (i < 0 && i > LUA_REGISTRYINDEX) |
| 103 | i += lua_gettop(L) + 1; | 109 | i += lua_gettop(L) + 1; |
| @@ -448,7 +454,46 @@ static int compat53_checkmode (lua_State *L, const char *mode, const char *moden | |||
| 448 | } | 454 | } |
| 449 | 455 | ||
| 450 | 456 | ||
| 451 | typedef struct compat53_LoadF { | 457 | typedef struct { |
| 458 | lua_Reader reader; | ||
| 459 | void *ud; | ||
| 460 | int has_peeked_data; | ||
| 461 | const char *peeked_data; | ||
| 462 | size_t peeked_data_size; | ||
| 463 | } compat53_reader_data; | ||
| 464 | |||
| 465 | |||
| 466 | static const char *compat53_reader (lua_State *L, void *ud, size_t *size) { | ||
| 467 | compat53_reader_data *data = (compat53_reader_data *)ud; | ||
| 468 | if (data->has_peeked_data) { | ||
| 469 | data->has_peeked_data = 0; | ||
| 470 | *size = data->peeked_data_size; | ||
| 471 | return data->peeked_data; | ||
| 472 | } else | ||
| 473 | return data->reader(L, data->ud, size); | ||
| 474 | } | ||
| 475 | |||
| 476 | |||
| 477 | COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char *source, const char *mode) { | ||
| 478 | compat53_reader_data compat53_data = { reader, data, 1, 0, 0 }; | ||
| 479 | compat53_data.peeked_data = reader(L, data, &(compat53_data.peeked_data_size)); | ||
| 480 | if (compat53_data.peeked_data && compat53_data.peeked_data_size) { | ||
| 481 | int status = LUA_OK; | ||
| 482 | if (compat53_data.peeked_data[0] == LUA_SIGNATURE[0]) /* binary file? */ | ||
| 483 | status = compat53_checkmode(L, mode, "binary", LUA_ERRFILE); | ||
| 484 | else | ||
| 485 | status = compat53_checkmode(L, mode, "text", LUA_ERRFILE); | ||
| 486 | if (status != LUA_OK) | ||
| 487 | return status; | ||
| 488 | } | ||
| 489 | /* we need to call the original 5.1 version of lua_load! */ | ||
| 490 | #undef lua_load | ||
| 491 | return lua_load(L, compat53_reader, &compat53_data, source); | ||
| 492 | #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) | ||
| 493 | } | ||
| 494 | |||
| 495 | |||
| 496 | typedef struct { | ||
| 452 | int n; /* number of pre-read characters */ | 497 | int n; /* number of pre-read characters */ |
| 453 | FILE *f; /* file being read */ | 498 | FILE *f; /* file being read */ |
| 454 | char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */ | 499 | char buff[COMPAT53_LUA_FILE_BUFFER_SIZE]; /* area for reading file */ |
| @@ -518,7 +563,6 @@ static int compat53_skipcomment (compat53_LoadF *lf, int *cp) { | |||
| 518 | 563 | ||
| 519 | 564 | ||
| 520 | COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { | 565 | COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode) { |
| 521 | static const char lua_signature[] = "\x1bLua"; | ||
| 522 | compat53_LoadF lf; | 566 | compat53_LoadF lf; |
| 523 | int status, readstatus; | 567 | int status, readstatus; |
| 524 | int c; | 568 | int c; |
| @@ -530,29 +574,25 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char | |||
| 530 | else { | 574 | else { |
| 531 | lua_pushfstring(L, "@%s", filename); | 575 | lua_pushfstring(L, "@%s", filename); |
| 532 | #if defined(_MSC_VER) | 576 | #if defined(_MSC_VER) |
| 533 | /* this code is here to stop a deprecation error that | 577 | /* This code is here to stop a deprecation error that stops builds |
| 534 | * stops builds if a certain macro is defined | 578 | * if a certain macro is defined. While normally not caring would |
| 535 | * while normally not caring would be best, some | 579 | * be best, some header-only libraries and builds can't afford to |
| 536 | * header-only libraries and builds can't afford | 580 | * dictate this to the user. A quick check shows that fopen_s this |
| 537 | * to dictate this to the user*/ | 581 | * goes back to VS 2005, and _fsopen goes back to VS 2003 .NET, |
| 538 | /* a quick check shows that fopen_s this goes back to VS 2005, | 582 | * possibly even before that so we don't need to do any version |
| 539 | * and _fsopen goes back to VS 2003 .NET, possibly even before that | 583 | * number checks, since this has been there since forever. |
| 540 | * so we don't need to do any version number checks, | ||
| 541 | * since this has been there since forever | ||
| 542 | */ | 584 | */ |
| 543 | 585 | ||
| 544 | /* TO USER: if you want the behavior of typical fopen_s/fopen, | 586 | /* TO USER: if you want the behavior of typical fopen_s/fopen, |
| 545 | * which does lock the file on VC++, define the macro used below to 0 | 587 | * which does lock the file on VC++, define the macro used below to 0 |
| 546 | */ | 588 | */ |
| 547 | #if COMPAT53_FOPEN_NO_LOCK | 589 | #if COMPAT53_FOPEN_NO_LOCK |
| 548 | lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */ | 590 | lf.f = _fsopen(filename, "r", _SH_DENYNO); /* do not lock the file in any way */ |
| 549 | if (lf.f == NULL) { | 591 | if (lf.f == NULL) |
| 550 | return compat53_errfile(L, "open", fnameindex); | 592 | return compat53_errfile(L, "open", fnameindex); |
| 551 | } | ||
| 552 | #else /* use default locking version */ | 593 | #else /* use default locking version */ |
| 553 | if (fopen_s(&lf.f, filename, "r") != 0) { | 594 | if (fopen_s(&lf.f, filename, "r") != 0) |
| 554 | return compat53_errfile(L, "open", fnameindex); | 595 | return compat53_errfile(L, "open", fnameindex); |
| 555 | } | ||
| 556 | #endif /* Locking vs. No-locking fopen variants */ | 596 | #endif /* Locking vs. No-locking fopen variants */ |
| 557 | #else | 597 | #else |
| 558 | lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */ | 598 | lf.f = fopen(filename, "r"); /* default stdlib doesn't forcefully lock files here */ |
| @@ -561,30 +601,19 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char | |||
| 561 | } | 601 | } |
| 562 | if (compat53_skipcomment(&lf, &c)) /* read initial portion */ | 602 | if (compat53_skipcomment(&lf, &c)) /* read initial portion */ |
| 563 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ | 603 | lf.buff[lf.n++] = '\n'; /* add line to correct line numbers */ |
| 564 | if (c == lua_signature[0]) { /* binary file? */ | 604 | if (c == LUA_SIGNATURE[0] && filename) { /* binary file? */ |
| 565 | status = compat53_checkmode(L, mode, "binary", LUA_ERRFILE); | ||
| 566 | if (status != LUA_OK) { | ||
| 567 | fclose(lf.f); | ||
| 568 | return compat53_errfile(L, "improper mode", fnameindex); | ||
| 569 | } | ||
| 570 | #if defined(_MSC_VER) | 605 | #if defined(_MSC_VER) |
| 571 | if (freopen_s(&lf.f, filename, "r", lf.f) != 0) return compat53_errfile(L, "open", fnameindex); | 606 | if (freopen_s(&lf.f, filename, "rb", lf.f) != 0) |
| 607 | return compat53_errfile(L, "reopen", fnameindex); | ||
| 572 | #else | 608 | #else |
| 573 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ | 609 | lf.f = freopen(filename, "rb", lf.f); /* reopen in binary mode */ |
| 574 | if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex); | 610 | if (lf.f == NULL) return compat53_errfile(L, "reopen", fnameindex); |
| 575 | #endif | 611 | #endif |
| 576 | compat53_skipcomment(&lf, &c); /* re-read initial portion */ | 612 | compat53_skipcomment(&lf, &c); /* re-read initial portion */ |
| 577 | } | 613 | } |
| 578 | else { /* text file */ | ||
| 579 | status = compat53_checkmode(L, mode, "text", LUA_ERRFILE); | ||
| 580 | if (status != LUA_OK) { | ||
| 581 | fclose(lf.f); | ||
| 582 | return compat53_errfile(L, "improper mode", fnameindex); | ||
| 583 | } | ||
| 584 | } | ||
| 585 | if (c != EOF) | 614 | if (c != EOF) |
| 586 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ | 615 | lf.buff[lf.n++] = c; /* 'c' is the first character of the stream */ |
| 587 | status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1)); | 616 | status = lua_load(L, &compat53_getF, &lf, lua_tostring(L, -1), mode); |
| 588 | readstatus = ferror(lf.f); | 617 | readstatus = ferror(lf.f); |
| 589 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ | 618 | if (filename) fclose(lf.f); /* close file (even in case of errors) */ |
| 590 | if (readstatus) { | 619 | if (readstatus) { |
| @@ -598,7 +627,7 @@ COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char | |||
| 598 | 627 | ||
| 599 | COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) { | 628 | COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, const char *name, const char *mode) { |
| 600 | int status = LUA_OK; | 629 | int status = LUA_OK; |
| 601 | if (sz > 0 && buff[0] == '\x1b') { | 630 | if (sz > 0 && buff[0] == LUA_SIGNATURE[0]) { |
| 602 | status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); | 631 | status = compat53_checkmode(L, mode, "binary", LUA_ERRSYNTAX); |
| 603 | } | 632 | } |
| 604 | else { | 633 | else { |
| @@ -609,6 +638,7 @@ COMPAT53_API int luaL_loadbufferx (lua_State *L, const char *buff, size_t sz, co | |||
| 609 | return luaL_loadbuffer(L, buff, sz, name); | 638 | return luaL_loadbuffer(L, buff, sz, name); |
| 610 | } | 639 | } |
| 611 | 640 | ||
| 641 | |||
| 612 | #if !defined(l_inspectstat) && \ | 642 | #if !defined(l_inspectstat) && \ |
| 613 | (defined(unix) || defined(__unix) || defined(__unix__) || \ | 643 | (defined(unix) || defined(__unix) || defined(__unix__) || \ |
| 614 | defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \ | 644 | defined(__TOS_AIX__) || defined(_SYSTYPE_BSD) || \ |
diff --git a/c-api/compat-5.3.h b/c-api/compat-5.3.h index c62d6bf..755c23e 100644 --- a/c-api/compat-5.3.h +++ b/c-api/compat-5.3.h | |||
| @@ -174,6 +174,9 @@ COMPAT53_API lua_Number lua_tonumberx (lua_State *L, int i, int *isnum); | |||
| 174 | #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) | 174 | #define luaL_checkversion COMPAT53_CONCAT(COMPAT53_PREFIX, L_checkversion) |
| 175 | COMPAT53_API void luaL_checkversion (lua_State *L); | 175 | COMPAT53_API void luaL_checkversion (lua_State *L); |
| 176 | 176 | ||
| 177 | #define lua_load COMPAT53_CONCAT(COMPAT53_PREFIX, _load_53) | ||
| 178 | COMPAT53_API int lua_load (lua_State *L, lua_Reader reader, void *data, const char* source, const char* mode); | ||
| 179 | |||
| 177 | #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex) | 180 | #define luaL_loadfilex COMPAT53_CONCAT(COMPAT53_PREFIX, L_loadfilex) |
| 178 | COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode); | 181 | COMPAT53_API int luaL_loadfilex (lua_State *L, const char *filename, const char *mode); |
| 179 | 182 | ||
