projects/aports·blob master · refs

128 lines · 4.0 KiB

1From f50a2225b137700b7454c7b8da1266bb2329ddf1 Mon Sep 17 00:00:00 2001
2From: Cloud Wu <[email protected]>
3Date: Thu, 5 Mar 2015 17:42:42 +0800
4Subject: [PATCH 1/3] Support lua 5.3 integer representation
5
6---
7 lua_cjson.c | 23 +++++++++++++++++------
8 1 file changed, 17 insertions(+), 6 deletions(-)
9
10diff --git a/lua_cjson.c b/lua_cjson.c
11index c14a1c5..40b4bdf 100644
12--- a/lua_cjson.c
13+++ b/lua_cjson.c
14@@ -81,6 +81,7 @@ typedef enum {
15 T_ARR_END,
16 T_STRING,
17 T_NUMBER,
18+ T_INTEGER,
19 T_BOOLEAN,
20 T_NULL,
21 T_COLON,
22@@ -98,6 +99,7 @@ static const char *json_token_type_name[] = {
23 "T_ARR_END",
24 "T_STRING",
25 "T_NUMBER",
26+ "T_INTEGER",
27 "T_BOOLEAN",
28 "T_NULL",
29 "T_COLON",
30@@ -143,6 +145,7 @@ typedef struct {
31 union {
32 const char *string;
33 double number;
34+ lua_Integer integer;
35 int boolean;
36 } value;
37 int string_len;
38@@ -1002,13 +1005,18 @@ static int json_is_invalid_number(json_parse_t *json)
39 static void json_next_number_token(json_parse_t *json, json_token_t *token)
40 {
41 char *endptr;
42-
43- token->type = T_NUMBER;
44- token->value.number = fpconv_strtod(json->ptr, &endptr);
45- if (json->ptr == endptr)
46+ token->value.integer = strtoll(json->ptr, &endptr, 0);
47+ if (json->ptr == endptr) {
48 json_set_token_error(token, json, "invalid number");
49- else
50- json->ptr = endptr; /* Skip the processed number */
51+ return;
52+ }
53+ if (*endptr == '.' || *endptr == 'e' || *endptr == 'E') {
54+ token->type = T_NUMBER;
55+ token->value.number = fpconv_strtod(json->ptr, &endptr);
56+ } else {
57+ token->type = T_INTEGER;
58+ }
59+ json->ptr = endptr; /* Skip the processed number */
60
61 return;
62 }
63@@ -1237,6 +1245,9 @@ static void json_process_value(lua_State *l, json_parse_t *json,
64 case T_NUMBER:
65 lua_pushnumber(l, token->value.number);
66 break;;
67+ case T_INTEGER:
68+ lua_pushinteger(l, token->value.integer);
69+ break;;
70 case T_BOOLEAN:
71 lua_pushboolean(l, token->value.boolean);
72 break;;
73
74From e02072ce28a5621725cbce23e32bbe3e38f0dcb3 Mon Sep 17 00:00:00 2001
75From: Cloud Wu <[email protected]>
76Date: Tue, 1 Sep 2015 23:17:21 +0800
77Subject: [PATCH 2/3] encode integer for lua 5.3
78
79---
80 lua_cjson.c | 11 ++++++++++-
81 1 file changed, 10 insertions(+), 1 deletion(-)
82
83diff --git a/lua_cjson.c b/lua_cjson.c
84index 40b4bdf..da80f0d 100644
85--- a/lua_cjson.c
86+++ b/lua_cjson.c
87@@ -589,8 +589,17 @@ static void json_append_array(lua_State *l, json_config_t *cfg, int current_dept
88 static void json_append_number(lua_State *l, json_config_t *cfg,
89 strbuf_t *json, int lindex)
90 {
91- double num = lua_tonumber(l, lindex);
92 int len;
93+#if LUA_VERSION_NUM >= 503
94+ if (lua_isinteger(l, lindex)) {
95+ lua_Integer num = lua_tointeger(l, lindex);
96+ strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */
97+ len = lua_integer2str(strbuf_empty_ptr(json), num);
98+ strbuf_extend_length(json, len);
99+ return;
100+ }
101+#endif
102+ double num = lua_tonumber(l, lindex);
103
104 if (cfg->encode_invalid_numbers == 0) {
105 /* Prevent encoding invalid numbers */
106
107From f5ee43d284d24c100bb11ecf286a04999bca6514 Mon Sep 17 00:00:00 2001
108From: Cloud Wu <[email protected]>
109Date: Tue, 1 Dec 2015 22:21:19 +0800
110Subject: [PATCH 3/3] Compatible with lua 5.3.2
111
112---
113 lua_cjson.c | 2 +-
114 1 file changed, 1 insertion(+), 1 deletion(-)
115
116diff --git a/lua_cjson.c b/lua_cjson.c
117index da80f0d..2e918d7 100644
118--- a/lua_cjson.c
119+++ b/lua_cjson.c
120@@ -594,7 +594,7 @@ static void json_append_number(lua_State *l, json_config_t *cfg,
121 if (lua_isinteger(l, lindex)) {
122 lua_Integer num = lua_tointeger(l, lindex);
123 strbuf_ensure_empty_length(json, FPCONV_G_FMT_BUFSIZE); /* max length of int64 is 19 */
124- len = lua_integer2str(strbuf_empty_ptr(json), num);
125+ len = sprintf(strbuf_empty_ptr(json), LUA_INTEGER_FMT, num);
126 strbuf_extend_length(json, len);
127 return;
128 }