1
2 /*
3 * Copyright (C) Xiaozhe Wang (chaoslawful)
4 * Copyright (C) Yichun Zhang (agentzh)
5 */
6
7
8 #ifndef _NGX_HTTP_LUA_OUTPUT_H_INCLUDED_
9 #define _NGX_HTTP_LUA_OUTPUT_H_INCLUDED_
10
11
12 #include "ngx_http_lua_common.h"
13
14
15 void ngx_http_lua_inject_output_api(lua_State *L);
16
17 size_t ngx_http_lua_calc_strlen_in_table(lua_State *L, int index, int arg_i,
18 unsigned strict);
19
20 u_char *ngx_http_lua_copy_str_in_table(lua_State *L, int index, u_char *dst);
21
22 ngx_int_t ngx_http_lua_flush_resume_helper(ngx_http_request_t *r,
23 ngx_http_lua_ctx_t *ctx);
24
25
26 /* Get the maximum possible length, not the actual length */
27 static ngx_inline size_t
ngx_http_lua_get_num_len(lua_State * L,int idx)28 ngx_http_lua_get_num_len(lua_State *L, int idx)
29 {
30 double num;
31
32 num = (double) lua_tonumber(L, idx);
33 if (num == (double) (int32_t) num) {
34 return NGX_INT32_LEN;
35 }
36
37 return NGX_DOUBLE_LEN;
38 }
39
40
41 static ngx_inline u_char *
ngx_http_lua_write_num(lua_State * L,int idx,u_char * dst)42 ngx_http_lua_write_num(lua_State *L, int idx, u_char *dst)
43 {
44 double num;
45 int n;
46
47 num = (double) lua_tonumber(L, idx);
48 /*
49 * luajit format number with only 14 significant digits.
50 * To be consistent with lujit, don't use (double) (long) below
51 * or integer greater than 99,999,999,999,999 will different from luajit.
52 */
53 if (num == (double) (int32_t) num) {
54 dst = ngx_snprintf(dst, NGX_INT64_LEN, "%D", (int32_t) num);
55
56 } else {
57 /*
58 * The maximum number of significant digits is 14 in lua.
59 * Please refer to lj_strfmt.c for more details.
60 */
61 n = snprintf((char *) dst, NGX_DOUBLE_LEN, "%.14g", num);
62 if (n < 0) {
63 ngx_log_error(NGX_LOG_ERR, ngx_cycle->log, ngx_errno,
64 "snprintf(\"%f\") failed");
65
66 } else {
67 dst += n;
68 }
69 }
70
71 return dst;
72 }
73
74
75 #endif /* _NGX_HTTP_LUA_OUTPUT_H_INCLUDED_ */
76
77 /* vi:set ft=c ts=4 sw=4 et fdm=marker: */
78