Skip to content

Commit

Permalink
FEATURE: lua-cjson: Support lua 5.3 integer representation
Browse files Browse the repository at this point in the history
Integrate pull #59 request by cloudwu:

Support lua 5.3 integer representation
mpx/lua-cjson#59
  • Loading branch information
julthomas committed Jun 26, 2022
1 parent e22ef65 commit fc65831
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 0 deletions.
132 changes: 132 additions & 0 deletions SOURCES/lua-cjson-integer-support.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
Pull request by cloudwu:
Support lua 5.3 integer representation
https://github.com/mpx/lua-cjson/pull/59

From f13d18e4dd0ff3ec79c712452a57730fc1bbacc8 Mon Sep 17 00:00:00 2001
From: Cloud Wu <[email protected]>
Date: Thu, 5 Mar 2015 17:42:42 +0800
Subject: [PATCH 1/3] Support lua 5.3 integer representation

---
lua_cjson.c | 23 +++++++++++++++++------
1 file changed, 17 insertions(+), 6 deletions(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index 22f33f1..a601f02 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -87,6 +87,7 @@ typedef enum {
T_ARR_END,
T_STRING,
T_NUMBER,
+ T_INTEGER,
T_BOOLEAN,
T_NULL,
T_COLON,
@@ -104,6 +105,7 @@ static const char *json_token_type_name[] = {
"T_ARR_END",
"T_STRING",
"T_NUMBER",
+ "T_INTEGER",
"T_BOOLEAN",
"T_NULL",
"T_COLON",
@@ -149,6 +151,7 @@ typedef struct {
union {
const char *string;
double number;
+ lua_Integer integer;
int boolean;
} value;
int string_len;
@@ -1008,13 +1011,18 @@ static int json_is_invalid_number(json_parse_t *json)
static void json_next_number_token(json_parse_t *json, json_token_t *token)
{
char *endptr;
-
- token->type = T_NUMBER;
- token->value.number = fpconv_strtod(json->ptr, &endptr);
- if (json->ptr == endptr)
+ token->value.integer = strtoll(json->ptr, &endptr, 0);
+ if (json->ptr == endptr) {
json_set_token_error(token, json, "invalid number");
- else
- json->ptr = endptr; /* Skip the processed number */
+ return;
+ }
+ if (*endptr == '.' || *endptr == 'e' || *endptr == 'E') {
+ token->type = T_NUMBER;
+ token->value.number = fpconv_strtod(json->ptr, &endptr);
+ } else {
+ token->type = T_INTEGER;
+ }
+ json->ptr = endptr; /* Skip the processed number */

return;
}
@@ -1243,6 +1251,9 @@ static void json_process_value(lua_State *l, json_parse_t *json,
case T_NUMBER:
lua_pushnumber(l, token->value.number);
break;;
+ case T_INTEGER:
+ lua_pushinteger(l, token->value.integer);
+ break;;
case T_BOOLEAN:
lua_pushboolean(l, token->value.boolean);
break;;

From a82560481c51b2719aa41ee541023f45d098042e Mon Sep 17 00:00:00 2001
From: Cloud Wu <[email protected]>
Date: Tue, 1 Sep 2015 23:17:21 +0800
Subject: [PATCH 2/3] encode integer for lua 5.3

---
lua_cjson.c | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index a601f02..d8ed0e9 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -595,8 +595,17 @@ static void json_append_array(lua_State *l, json_config_t *cfg, int current_dept
static void json_append_number(lua_State *l, json_config_t *cfg,
strbuf_t *json, int lindex)
{
- double num = lua_tonumber(l, lindex);
int len;
+#if LUA_VERSION_NUM >= 503
+ if (lua_isinteger(l, lindex)) {
+ lua_Integer num = lua_tointeger(l, lindex);
+ strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */
+ len = lua_integer2str(strbuf_empty_ptr(json), num);
+ strbuf_extend_length(json, len);
+ return;
+ }
+#endif
+ double num = lua_tonumber(l, lindex);

if (cfg->encode_invalid_numbers == 0) {
/* Prevent encoding invalid numbers */

From c3f001fc5b3f629b4b9872b50ed672e310c264ee Mon Sep 17 00:00:00 2001
From: Cloud Wu <[email protected]>
Date: Tue, 1 Dec 2015 22:21:19 +0800
Subject: [PATCH 3/3] Compatible with lua 5.3.2

---
lua_cjson.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/lua_cjson.c b/lua_cjson.c
index d8ed0e9..2cfaac3 100644
--- a/lua_cjson.c
+++ b/lua_cjson.c
@@ -600,7 +600,7 @@ static void json_append_number(lua_State *l, json_config_t *cfg,
if (lua_isinteger(l, lindex)) {
lua_Integer num = lua_tointeger(l, lindex);
strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */
- len = lua_integer2str(strbuf_empty_ptr(json), num);
+ len = sprintf(strbuf_empty_ptr(json), LUA_INTEGER_FMT, num);
strbuf_extend_length(json, len);
return;
}
4 changes: 4 additions & 0 deletions SPECS/lua.spec
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Source1200: https://github.com/keplerproject/luafilesystem/archive/refs/tags/v%{
Source1300: https://github.com/lunarmodules/luasocket/archive/refs/tags/v%{lua_socket_version}.tar.gz#/%{lua_socket_xprefix}.tar.gz

Patch0: lua-5.3.6-lua-path.patch
Patch1000: lua-cjson-integer-support.patch

BuildRequires: libcurl-devel
BuildRequires: ncurses-devel
Expand Down Expand Up @@ -71,6 +72,9 @@ This package contains development files for %{name}.

# lua-cjson
%setup -n lua-%{version} -T -D -a 1000
cd %{lua_cjson_xprefix}
%patch1000 -p1
cd ..

# lua-curl
%setup -n lua-%{version} -T -D -a 1100
Expand Down

0 comments on commit fc65831

Please sign in to comment.