/
luastream.c
151 lines (133 loc) · 4.12 KB
/
luastream.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
* Copyright 2010-2018, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "lua/utils.h"
#include "box/execute.h"
#include "box/vstream.h"
struct luastream {
struct lua_State *L;
};
void
luastream_init(struct luastream *stream, struct lua_State *L)
{
struct luastream *luastream = (struct luastream *)stream;
luastream->L = L;
}
void
luastream_encode_array(struct luastream *stream, uint32_t size)
{
lua_createtable(stream->L, size, 0);
}
void
luastream_encode_map(struct luastream *stream, uint32_t size)
{
lua_createtable(stream->L, size, 0);
}
void
luastream_encode_uint(struct luastream *stream, uint64_t num)
{
luaL_pushuint64(stream->L, num);
}
void
luastream_encode_int(struct luastream *stream, int64_t num)
{
luaL_pushint64(stream->L, num);
}
void
luastream_encode_float(struct luastream *stream, float num)
{
lua_pushnumber(stream->L, num);
}
void
luastream_encode_double(struct luastream *stream, double num)
{
lua_pushnumber(stream->L, num);
}
void
luastream_encode_strn(struct luastream *stream, const char *str, uint32_t len)
{
lua_pushlstring(stream->L, str, len);
}
void
luastream_encode_nil(struct luastream *stream)
{
lua_pushnil(stream->L);
}
void
luastream_encode_bool(struct luastream *stream, bool val)
{
lua_pushboolean(stream->L, val);
}
int
lua_vstream_encode_port(struct vstream *stream, struct port *port)
{
port_dump_lua(port, ((struct luastream *)stream)->L);
return 0;
}
void
lua_vstream_encode_enum(struct vstream *stream, int64_t num, const char *str)
{
(void)num;
lua_pushlstring(((struct luastream *)stream)->L, str, strlen(str));
}
void
lua_vstream_encode_map_commit(struct vstream *stream)
{
size_t length;
const char *key = lua_tolstring(((struct luastream *)stream)->L, -2,
&length);
lua_setfield(((struct luastream *)stream)->L, -3, key);
lua_pop(((struct luastream *)stream)->L, 1);
}
void
lua_vstream_encode_array_commit(struct vstream *stream, uint32_t id)
{
lua_rawseti(((struct luastream *)stream)->L, -2, id + 1);
}
const struct vstream_vtab lua_vstream_vtab = {
/** encode_array = */ (encode_array_f)luastream_encode_array,
/** encode_map = */ (encode_map_f)luastream_encode_map,
/** encode_uint = */ (encode_uint_f)luastream_encode_uint,
/** encode_int = */ (encode_int_f)luastream_encode_int,
/** encode_float = */ (encode_float_f)luastream_encode_float,
/** encode_double = */ (encode_double_f)luastream_encode_double,
/** encode_strn = */ (encode_strn_f)luastream_encode_strn,
/** encode_nil = */ (encode_nil_f)luastream_encode_nil,
/** encode_bool = */ (encode_bool_f)luastream_encode_bool,
/** encode_enum = */ lua_vstream_encode_enum,
/** encode_port = */ lua_vstream_encode_port,
/** encode_array_commit = */ lua_vstream_encode_array_commit,
/** encode_map_commit = */ lua_vstream_encode_map_commit,
};
void
lua_vstream_init_vtab(struct vstream *stream)
{
stream->vtab = &lua_vstream_vtab;
}