From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: From: Serge Petrenko Subject: [PATCH v2 4/8] lua: rework luaL_field types to support msgpack extensions Date: Thu, 8 Aug 2019 14:55:55 +0300 Message-Id: <8732ff5bc9cfe6622a14e773a76053ee1a20fcf8.1565263272.git.sergepetrenko@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit To: vdavydov.dev@gmail.com Cc: tarantool-patches@freelists.org, kostja@tarantool.org, Serge Petrenko List-ID: We are planning to add new types, such as decimal, which will all share a single msgpack type - MP_EXT. MP_EXT is currently treated as uninterpretable in encoders. So, in order to distinguish such new types introduce MP_FIELD_* instead of MP_* types for use in luaL_field structure and msgpack encoders. New MP_FIELD_* types will correspond to MP_* types, MP_FIELD_UNKNOWN will serve the same purpose, as MP_EXT does now, and various MP_FIELD_* types will correspond to various MP_EXT subtypes, such as MP_DECIMAL in future. Prerequisite #4333 --- src/box/lua/call.c | 16 +++--- src/box/lua/execute.c | 24 ++++---- src/box/lua/tuple.c | 12 ++-- src/lib/core/mp_user_types.h | 87 +++++++++++++++++++++++++++++ src/lua/msgpack.c | 93 ++++++++++++++++--------------- src/lua/msgpack.h | 6 +- src/lua/pickle.c | 14 ++--- src/lua/utils.c | 66 +++++++++++----------- src/lua/utils.h | 5 +- third_party/lua-cjson/lua_cjson.c | 28 +++++----- third_party/lua-yaml/lyaml.cc | 22 ++++---- 11 files changed, 231 insertions(+), 142 deletions(-) create mode 100644 src/lib/core/mp_user_types.h diff --git a/src/box/lua/call.c b/src/box/lua/call.c index 0ac2eb7a6..189da0d1b 100644 --- a/src/box/lua/call.c +++ b/src/box/lua/call.c @@ -178,11 +178,11 @@ luamp_encode_call_16(lua_State *L, struct luaL_serializer *cfg, if (luaL_tofield(L, cfg, i, &field) < 0) return luaT_error(L); struct tuple *tuple; - if (field.type == MP_EXT && + if (field.type == MP_FIELD_UNKNOWN && (tuple = luaT_istuple(L, i)) != NULL) { /* `return ..., box.tuple.new(...), ...` */ tuple_to_mpstream(tuple, stream); - } else if (field.type != MP_ARRAY) { + } else if (field.type != MP_FIELD_ARRAY) { /* * `return ..., scalar, ... => * ..., { scalar }, ...` @@ -207,11 +207,11 @@ luamp_encode_call_16(lua_State *L, struct luaL_serializer *cfg, if (luaL_tofield(L, cfg, 1, &root) < 0) return luaT_error(L); struct tuple *tuple; - if (root.type == MP_EXT && (tuple = luaT_istuple(L, 1)) != NULL) { + if (root.type == MP_FIELD_UNKNOWN && (tuple = luaT_istuple(L, 1)) != NULL) { /* `return box.tuple()` */ tuple_to_mpstream(tuple, stream); return 1; - } else if (root.type != MP_ARRAY) { + } else if (root.type != MP_FIELD_ARRAY) { /* * `return scalar` * `return map` @@ -222,7 +222,7 @@ luamp_encode_call_16(lua_State *L, struct luaL_serializer *cfg, return 1; } - assert(root.type == MP_ARRAY); + assert(root.type == MP_FIELD_ARRAY); if (root.size == 0) { /* `return {}` => `{ box.tuple() }` */ mpstream_encode_array(stream, 0); @@ -230,15 +230,15 @@ luamp_encode_call_16(lua_State *L, struct luaL_serializer *cfg, } /* `return { tuple, scalar, tuple }` */ - assert(root.type == MP_ARRAY && root.size > 0); + assert(root.type == MP_FIELD_ARRAY && root.size > 0); for (uint32_t t = 1; t <= root.size; t++) { lua_rawgeti(L, 1, t); struct luaL_field field; if (luaL_tofield(L, cfg, -1, &field) < 0) return luaT_error(L); - if (field.type == MP_EXT && (tuple = luaT_istuple(L, -1))) { + if (field.type == MP_FIELD_UNKNOWN && (tuple = luaT_istuple(L, -1))) { tuple_to_mpstream(tuple, stream); - } else if (field.type != MP_ARRAY) { + } else if (field.type != MP_FIELD_ARRAY) { /* The first member of root table is not tuple/array */ if (t == 1) { /* diff --git a/src/box/lua/execute.c b/src/box/lua/execute.c index 76ecdd541..fa234f6d2 100644 --- a/src/box/lua/execute.c +++ b/src/box/lua/execute.c @@ -141,15 +141,15 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) if (luaL_tofield(L, luaL_msgpack_default, -1, &field) < 0) return -1; switch (field.type) { - case MP_UINT: + case MP_FIELD_UINT: bind->u64 = field.ival; bind->bytes = sizeof(bind->u64); break; - case MP_INT: + case MP_FIELD_INT: bind->i64 = field.ival; bind->bytes = sizeof(bind->i64); break; - case MP_STR: + case MP_FIELD_STR: /* * Data should be saved in allocated memory as it * will be poped from Lua stack. @@ -164,38 +164,38 @@ lua_sql_bind_decode(struct lua_State *L, struct sql_bind *bind, int idx, int i) bind->s = buf; bind->bytes = field.sval.len; break; - case MP_DOUBLE: - case MP_FLOAT: + case MP_FIELD_DOUBLE: + case MP_FIELD_FLOAT: bind->d = field.dval; bind->bytes = sizeof(bind->d); break; - case MP_NIL: + case MP_FIELD_NIL: bind->bytes = 1; break; - case MP_BOOL: + case MP_FIELD_BOOL: /* SQLite doesn't support boolean. Use int instead. */ bind->i64 = field.bval ? 1 : 0; bind->bytes = sizeof(bind->i64); break; - case MP_BIN: + case MP_FIELD_BIN: bind->s = mp_decode_bin(&field.sval.data, &bind->bytes); break; - case MP_EXT: + case MP_FIELD_UNKNOWN: diag_set(ClientError, ER_SQL_BIND_TYPE, "USERDATA", sql_bind_name(bind)); return -1; - case MP_ARRAY: + case MP_FIELD_ARRAY: diag_set(ClientError, ER_SQL_BIND_TYPE, "ARRAY", sql_bind_name(bind)); return -1; - case MP_MAP: + case MP_FIELD_MAP: diag_set(ClientError, ER_SQL_BIND_TYPE, "MAP", sql_bind_name(bind)); return -1; default: unreachable(); } - bind->type = field.type; + bind->type = field_type_to_msgpack(field.type); lua_pop(L, lua_gettop(L) - idx); return 0; } diff --git a/src/box/lua/tuple.c b/src/box/lua/tuple.c index 183c3901d..23bc73208 100644 --- a/src/box/lua/tuple.c +++ b/src/box/lua/tuple.c @@ -249,11 +249,11 @@ luamp_convert_key(struct lua_State *L, struct luaL_serializer *cfg, struct luaL_field field; if (luaL_tofield(L, cfg, index, &field) < 0) luaT_error(L); - if (field.type == MP_ARRAY) { + if (field.type == MP_FIELD_ARRAY) { lua_pushvalue(L, index); luamp_encode_r(L, cfg, stream, &field, 0); lua_pop(L, 1); - } else if (field.type == MP_NIL) { + } else if (field.type == MP_FIELD_NIL) { mpstream_encode_array(stream, 0); } else { mpstream_encode_array(stream, 1); @@ -270,7 +270,7 @@ luamp_encode_tuple(struct lua_State *L, struct luaL_serializer *cfg, struct tuple *tuple = luaT_istuple(L, index); if (tuple != NULL) { return tuple_to_mpstream(tuple, stream); - } else if (luamp_encode(L, cfg, stream, index) != MP_ARRAY) { + } else if (luamp_encode(L, cfg, stream, index) != MP_FIELD_ARRAY) { diag_set(ClientError, ER_TUPLE_NOT_ARRAY); luaT_error(L); } @@ -286,17 +286,17 @@ tuple_to_mpstream(struct tuple *tuple, struct mpstream *stream) } /* A MsgPack extensions handler that supports tuples */ -static enum mp_type +static enum mp_field_type luamp_encode_extension_box(struct lua_State *L, int idx, struct mpstream *stream) { struct tuple *tuple = luaT_istuple(L, idx); if (tuple != NULL) { tuple_to_mpstream(tuple, stream); - return MP_ARRAY; + return MP_FIELD_ARRAY; } - return MP_EXT; + return MP_FIELD_UNKNOWN; } /** diff --git a/src/lib/core/mp_user_types.h b/src/lib/core/mp_user_types.h new file mode 100644 index 000000000..d84748b71 --- /dev/null +++ b/src/lib/core/mp_user_types.h @@ -0,0 +1,87 @@ +#ifndef TARANTOOL_LIB_CORE_MP_USER_TYPES_H_INCLUDED +#define TARANTOOL_LIB_CORE_MP_USER_TYPES_H_INCLUDED +/* + * Copyright 2019, 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 ``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 + * 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. + */ + +enum mp_field_type { + MP_FIELD_UNKNOWN, + MP_FIELD_NIL, + MP_FIELD_INT, + MP_FIELD_UINT, + MP_FIELD_STR, + MP_FIELD_BIN, + MP_FIELD_ARRAY, + MP_FIELD_MAP, + MP_FIELD_BOOL, + MP_FIELD_FLOAT, + MP_FIELD_DOUBLE, +}; + +static inline enum mp_type +field_type_to_msgpack(enum mp_field_type type) +{ + switch (type) { + case MP_FIELD_NIL: return MP_NIL; + case MP_FIELD_INT: return MP_INT; + case MP_FIELD_UINT: return MP_UINT; + case MP_FIELD_STR: return MP_STR; + case MP_FIELD_BIN: return MP_BIN; + case MP_FIELD_ARRAY: return MP_ARRAY; + case MP_FIELD_MAP: return MP_MAP; + case MP_FIELD_BOOL: return MP_BOOL; + case MP_FIELD_FLOAT: return MP_FLOAT; + case MP_FIELD_DOUBLE: return MP_DOUBLE; + default: return MP_EXT; + } +} + +static inline enum mp_field_type +msgpack_to_field_type(const char *data) +{ + switch(mp_typeof(*data)) + { + case MP_NIL: return MP_FIELD_NIL; + case MP_UINT: return MP_FIELD_UINT; + case MP_INT: return MP_FIELD_INT; + case MP_STR: return MP_FIELD_STR; + case MP_BIN: return MP_FIELD_BIN; + case MP_ARRAY: return MP_FIELD_ARRAY; + case MP_MAP: return MP_FIELD_MAP; + case MP_BOOL: return MP_FIELD_BOOL; + case MP_FLOAT: return MP_FIELD_FLOAT; + case MP_DOUBLE: return MP_FIELD_DOUBLE; + case MP_EXT: + default: + return MP_FIELD_UNKNOWN; + } +} + +#endif diff --git a/src/lua/msgpack.c b/src/lua/msgpack.c index 2126988eb..6fc2b8278 100644 --- a/src/lua/msgpack.c +++ b/src/lua/msgpack.c @@ -52,7 +52,7 @@ luamp_error(void *error_ctx) struct luaL_serializer *luaL_msgpack_default = NULL; -static enum mp_type +static enum mp_field_type luamp_encode_extension_default(struct lua_State *L, int idx, struct mpstream *stream); @@ -64,14 +64,14 @@ static luamp_encode_extension_f luamp_encode_extension = static luamp_decode_extension_f luamp_decode_extension = luamp_decode_extension_default; -static enum mp_type +static enum mp_field_type luamp_encode_extension_default(struct lua_State *L, int idx, struct mpstream *stream) { (void) L; (void) idx; (void) stream; - return MP_EXT; + return MP_FIELD_UNKNOWN; } void @@ -102,45 +102,45 @@ luamp_set_decode_extension(luamp_decode_extension_f handler) } } -enum mp_type +enum mp_field_type luamp_encode_r(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, struct luaL_field *field, int level) { int top = lua_gettop(L); - enum mp_type type; + enum mp_field_type type; -restart: /* used by MP_EXT */ +restart: /* used by MP_FIELD_UNKNOWN */ switch (field->type) { - case MP_UINT: + case MP_FIELD_UINT: mpstream_encode_uint(stream, field->ival); - return MP_UINT; - case MP_STR: + return MP_FIELD_UINT; + case MP_FIELD_STR: mpstream_encode_strn(stream, field->sval.data, field->sval.len); - return MP_STR; - case MP_BIN: + return MP_FIELD_STR; + case MP_FIELD_BIN: mpstream_encode_strn(stream, field->sval.data, field->sval.len); - return MP_BIN; - case MP_INT: + return MP_FIELD_BIN; + case MP_FIELD_INT: mpstream_encode_int(stream, field->ival); - return MP_INT; - case MP_FLOAT: + return MP_FIELD_INT; + case MP_FIELD_FLOAT: mpstream_encode_float(stream, field->fval); - return MP_FLOAT; - case MP_DOUBLE: + return MP_FIELD_FLOAT; + case MP_FIELD_DOUBLE: mpstream_encode_double(stream, field->dval); - return MP_DOUBLE; - case MP_BOOL: + return MP_FIELD_DOUBLE; + case MP_FIELD_BOOL: mpstream_encode_bool(stream, field->bval); - return MP_BOOL; - case MP_NIL: + return MP_FIELD_BOOL; + case MP_FIELD_NIL: mpstream_encode_nil(stream); - return MP_NIL; - case MP_MAP: + return MP_FIELD_NIL; + case MP_FIELD_MAP: /* Map */ if (level >= cfg->encode_max_depth) { mpstream_encode_nil(stream); /* Limit nested maps */ - return MP_NIL; + return MP_FIELD_NIL; } mpstream_encode_map(stream, field->size); lua_pushnil(L); /* first key */ @@ -156,12 +156,12 @@ restart: /* used by MP_EXT */ lua_pop(L, 1); /* pop value */ } assert(lua_gettop(L) == top); - return MP_MAP; - case MP_ARRAY: + return MP_FIELD_MAP; + case MP_FIELD_ARRAY: /* Array */ if (level >= cfg->encode_max_depth) { mpstream_encode_nil(stream); /* Limit nested arrays */ - return MP_NIL; + return MP_FIELD_NIL; } uint32_t size = field->size; mpstream_encode_array(stream, size); @@ -173,23 +173,23 @@ restart: /* used by MP_EXT */ lua_pop(L, 1); } assert(lua_gettop(L) == top); - return MP_ARRAY; - case MP_EXT: + return MP_FIELD_ARRAY; + case MP_FIELD_UNKNOWN: /* Run trigger if type can't be encoded */ type = luamp_encode_extension(L, top, stream); - if (type != MP_EXT) + if (type != MP_FIELD_UNKNOWN) return type; /* Value has been packed by the trigger */ /* Try to convert value to serializable type */ luaL_convertfield(L, cfg, top, field); /* handled by luaL_convertfield */ - assert(field->type != MP_EXT); + assert(field->type != MP_FIELD_UNKNOWN); assert(lua_gettop(L) == top); goto restart; } - return MP_EXT; + return MP_FIELD_UNKNOWN; } -enum mp_type +enum mp_field_type luamp_encode(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, int index) { @@ -205,7 +205,7 @@ luamp_encode(struct lua_State *L, struct luaL_serializer *cfg, struct luaL_field field; if (luaL_tofield(L, cfg, lua_gettop(L), &field) < 0) return luaT_error(L); - enum mp_type top_type = luamp_encode_r(L, cfg, stream, &field, 0); + enum mp_field_type top_type = luamp_encode_r(L, cfg, stream, &field, 0); if (!on_top) { lua_remove(L, top + 1); /* remove a value copy */ @@ -219,45 +219,46 @@ luamp_decode(struct lua_State *L, struct luaL_serializer *cfg, const char **data) { double d; - switch (mp_typeof(**data)) { - case MP_UINT: + enum mp_field_type type = msgpack_to_field_type(*data); + switch (type) { + case MP_FIELD_UINT: luaL_pushuint64(L, mp_decode_uint(data)); break; - case MP_INT: + case MP_FIELD_INT: luaL_pushint64(L, mp_decode_int(data)); break; - case MP_FLOAT: + case MP_FIELD_FLOAT: d = mp_decode_float(data); luaL_checkfinite(L, cfg, d); lua_pushnumber(L, d); return; - case MP_DOUBLE: + case MP_FIELD_DOUBLE: d = mp_decode_double(data); luaL_checkfinite(L, cfg, d); lua_pushnumber(L, d); return; - case MP_STR: + case MP_FIELD_STR: { uint32_t len = 0; const char *str = mp_decode_str(data, &len); lua_pushlstring(L, str, len); return; } - case MP_BIN: + case MP_FIELD_BIN: { uint32_t len = 0; const char *str = mp_decode_bin(data, &len); lua_pushlstring(L, str, len); return; } - case MP_BOOL: + case MP_FIELD_BOOL: lua_pushboolean(L, mp_decode_bool(data)); return; - case MP_NIL: + case MP_FIELD_NIL: mp_decode_nil(data); luaL_pushnull(L); return; - case MP_ARRAY: + case MP_FIELD_ARRAY: { uint32_t size = mp_decode_array(data); lua_createtable(L, size, 0); @@ -269,7 +270,7 @@ luamp_decode(struct lua_State *L, struct luaL_serializer *cfg, luaL_setarrayhint(L, -1); return; } - case MP_MAP: + case MP_FIELD_MAP: { uint32_t size = mp_decode_map(data); lua_createtable(L, 0, size); @@ -282,7 +283,7 @@ luamp_decode(struct lua_State *L, struct luaL_serializer *cfg, luaL_setmaphint(L, -1); return; } - case MP_EXT: + case MP_FIELD_UNKNOWN: luamp_decode_extension(L, data); break; } diff --git a/src/lua/msgpack.h b/src/lua/msgpack.h index d0ade303f..08b8d5baf 100644 --- a/src/lua/msgpack.h +++ b/src/lua/msgpack.h @@ -61,12 +61,12 @@ luamp_error(void *); enum { LUAMP_ALLOC_FACTOR = 256 }; /* low-level function needed for execute_lua_call() */ -enum mp_type +enum mp_field_type luamp_encode_r(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, struct luaL_field *field, int level); -enum mp_type +enum mp_field_type luamp_encode(struct lua_State *L, struct luaL_serializer *cfg, struct mpstream *stream, int index); @@ -74,7 +74,7 @@ void luamp_decode(struct lua_State *L, struct luaL_serializer *cfg, const char **data); -typedef enum mp_type +typedef enum mp_field_type (*luamp_encode_extension_f)(struct lua_State *, int, struct mpstream *); /** diff --git a/src/lua/pickle.c b/src/lua/pickle.c index 65208b5b3..046fbba2f 100644 --- a/src/lua/pickle.c +++ b/src/lua/pickle.c @@ -85,7 +85,7 @@ lbox_pack(struct lua_State *L) case 'B': case 'b': /* signed and unsigned 8-bit integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 8-bit int"); luaL_region_dup(L, buf, &field.ival, sizeof(uint8_t)); @@ -93,14 +93,14 @@ lbox_pack(struct lua_State *L) case 'S': case 's': /* signed and unsigned 16-bit integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 16-bit int"); luaL_region_dup(L, buf, &field.ival, sizeof(uint16_t)); break; case 'n': /* signed and unsigned 16-bit big endian integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 16-bit int"); field.ival = (uint16_t) htons((uint16_t) field.ival); @@ -109,14 +109,14 @@ lbox_pack(struct lua_State *L) case 'I': case 'i': /* signed and unsigned 32-bit integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 32-bit int"); luaL_region_dup(L, buf, &field.ival, sizeof(uint32_t)); break; case 'N': /* signed and unsigned 32-bit big endian integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 32-bit int"); field.ival = htonl(field.ival); @@ -125,7 +125,7 @@ lbox_pack(struct lua_State *L) case 'L': case 'l': /* signed and unsigned 64-bit integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 64-bit int"); luaL_region_dup(L, buf, &field.ival, sizeof(uint64_t)); @@ -133,7 +133,7 @@ lbox_pack(struct lua_State *L) case 'Q': case 'q': /* signed and unsigned 64-bit integers */ - if (field.type != MP_UINT && field.type != MP_INT) + if (field.type != MP_FIELD_UINT && field.type != MP_FIELD_INT) luaL_error(L, "pickle.pack: expected 64-bit int"); field.ival = bswap_u64(field.ival); diff --git a/src/lua/utils.c b/src/lua/utils.c index 0a4bcf517..9b2f4fd6c 100644 --- a/src/lua/utils.c +++ b/src/lua/utils.c @@ -501,13 +501,13 @@ lua_field_try_serialize(struct lua_State *L) const char *type = lua_tostring(L, -1); if (strcmp(type, "array") == 0 || strcmp(type, "seq") == 0 || strcmp(type, "sequence") == 0) { - field->type = MP_ARRAY; /* Override type */ + field->type = MP_FIELD_ARRAY; /* Override type */ field->size = luaL_arrlen(L, 1); /* YAML: use flow mode if __serialize == 'seq' */ if (cfg->has_compact && type[3] == '\0') field->compact = true; } else if (strcmp(type, "map") == 0 || strcmp(type, "mapping") == 0) { - field->type = MP_MAP; /* Override type */ + field->type = MP_FIELD_MAP; /* Override type */ field->size = luaL_maplen(L, 1); /* YAML: use flow mode if __serialize == 'map' */ if (cfg->has_compact && type[3] == '\0') @@ -562,7 +562,7 @@ lua_field_inspect_table(struct lua_State *L, struct luaL_serializer *cfg, lua_pop(L, 1); } - field->type = MP_ARRAY; + field->type = MP_FIELD_ARRAY; /* Calculate size and check that table can represent an array */ lua_pushnil(L); @@ -578,7 +578,7 @@ lua_field_inspect_table(struct lua_State *L, struct luaL_serializer *cfg, size++; lua_pop(L, 1); /* pop the value */ } - field->type = MP_MAP; + field->type = MP_FIELD_MAP; field->size = size; return 0; } @@ -594,12 +594,12 @@ lua_field_inspect_table(struct lua_State *L, struct luaL_serializer *cfg, diag_set(LuajitError, "excessively sparse array"); return -1; } - field->type = MP_MAP; + field->type = MP_FIELD_MAP; field->size = size; return 0; } - assert(field->type == MP_ARRAY); + assert(field->type == MP_FIELD_ARRAY); field->size = max; return 0; } @@ -635,23 +635,23 @@ luaL_tofield(struct lua_State *L, struct luaL_serializer *cfg, int index, diag_set(LuajitError, "number must not be NaN or Inf"); \ return -1; \ } \ - field->type = MP_NIL; \ + field->type = MP_FIELD_NIL; \ }}) switch (lua_type(L, index)) { case LUA_TNUMBER: num = lua_tonumber(L, index); if (isfinite(num) && modf(num, &intpart) != 0.0) { - field->type = MP_DOUBLE; + field->type = MP_FIELD_DOUBLE; field->dval = num; } else if (num >= 0 && num < exp2(64)) { - field->type = MP_UINT; + field->type = MP_FIELD_UINT; field->ival = (uint64_t) num; } else if (num > -exp2(63) && num < exp2(63)) { - field->type = MP_INT; + field->type = MP_FIELD_INT; field->ival = (int64_t) num; } else { - field->type = MP_DOUBLE; + field->type = MP_FIELD_DOUBLE; field->dval = num; CHECK_NUMBER(num); } @@ -664,79 +664,79 @@ luaL_tofield(struct lua_State *L, struct luaL_serializer *cfg, int index, int64_t ival; switch (cd->ctypeid) { case CTID_BOOL: - field->type = MP_BOOL; + field->type = MP_FIELD_BOOL; field->bval = *(bool*) cdata; return 0; case CTID_CCHAR: case CTID_INT8: ival = *(int8_t *) cdata; - field->type = (ival >= 0) ? MP_UINT : MP_INT; + field->type = (ival >= 0) ? MP_FIELD_UINT : MP_FIELD_INT; field->ival = ival; return 0; case CTID_INT16: ival = *(int16_t *) cdata; - field->type = (ival >= 0) ? MP_UINT : MP_INT; + field->type = (ival >= 0) ? MP_FIELD_UINT : MP_FIELD_INT; field->ival = ival; return 0; case CTID_INT32: ival = *(int32_t *) cdata; - field->type = (ival >= 0) ? MP_UINT : MP_INT; + field->type = (ival >= 0) ? MP_FIELD_UINT : MP_FIELD_INT; field->ival = ival; return 0; case CTID_INT64: ival = *(int64_t *) cdata; - field->type = (ival >= 0) ? MP_UINT : MP_INT; + field->type = (ival >= 0) ? MP_FIELD_UINT : MP_FIELD_INT; field->ival = ival; return 0; case CTID_UINT8: - field->type = MP_UINT; + field->type = MP_FIELD_UINT; field->ival = *(uint8_t *) cdata; return 0; case CTID_UINT16: - field->type = MP_UINT; + field->type = MP_FIELD_UINT; field->ival = *(uint16_t *) cdata; return 0; case CTID_UINT32: - field->type = MP_UINT; + field->type = MP_FIELD_UINT; field->ival = *(uint32_t *) cdata; return 0; case CTID_UINT64: - field->type = MP_UINT; + field->type = MP_FIELD_UINT; field->ival = *(uint64_t *) cdata; return 0; case CTID_FLOAT: - field->type = MP_FLOAT; + field->type = MP_FIELD_FLOAT; field->fval = *(float *) cdata; CHECK_NUMBER(field->fval); return 0; case CTID_DOUBLE: - field->type = MP_DOUBLE; + field->type = MP_FIELD_DOUBLE; field->dval = *(double *) cdata; CHECK_NUMBER(field->dval); return 0; case CTID_P_CVOID: case CTID_P_VOID: if (*(void **) cdata == NULL) { - field->type = MP_NIL; + field->type = MP_FIELD_NIL; return 0; } /* Fall through */ default: - field->type = MP_EXT; + field->type = MP_FIELD_UNKNOWN; } return 0; } case LUA_TBOOLEAN: - field->type = MP_BOOL; + field->type = MP_FIELD_BOOL; field->bval = lua_toboolean(L, index); return 0; case LUA_TNIL: - field->type = MP_NIL; + field->type = MP_FIELD_NIL; return 0; case LUA_TSTRING: field->sval.data = lua_tolstring(L, index, &size); field->sval.len = (uint32_t) size; - field->type = MP_STR; + field->type = MP_FIELD_STR; return 0; case LUA_TTABLE: { @@ -748,12 +748,12 @@ luaL_tofield(struct lua_State *L, struct luaL_serializer *cfg, int index, field->sval.data = NULL; field->sval.len = 0; if (lua_touserdata(L, index) == NULL) { - field->type = MP_NIL; + field->type = MP_FIELD_NIL; return 0; } /* Fall through */ default: - field->type = MP_EXT; + field->type = MP_FIELD_UNKNOWN; } #undef CHECK_NUMBER return 0; @@ -765,7 +765,7 @@ luaL_convertfield(struct lua_State *L, struct luaL_serializer *cfg, int idx, { if (idx < 0) idx = lua_gettop(L) + idx + 1; - assert(field->type == MP_EXT); /* must be called after tofield() */ + assert(field->type == MP_FIELD_UNKNOWN); /* must be called after tofield() */ if (cfg->encode_load_metatables) { int type = lua_type(L, idx); @@ -782,14 +782,14 @@ luaL_convertfield(struct lua_State *L, struct luaL_serializer *cfg, int idx, } } - if (field->type == MP_EXT && cfg->encode_use_tostring) + if (field->type == MP_FIELD_UNKNOWN && cfg->encode_use_tostring) lua_field_tostring(L, cfg, idx, field); - if (field->type != MP_EXT) + if (field->type != MP_FIELD_UNKNOWN) return; if (cfg->encode_invalid_as_nil) { - field->type = MP_NIL; + field->type = MP_FIELD_NIL; return; } diff --git a/src/lua/utils.h b/src/lua/utils.h index 7e7cdc0c6..4a314ab32 100644 --- a/src/lua/utils.h +++ b/src/lua/utils.h @@ -54,6 +54,7 @@ extern "C" { #include #include "lua/error.h" +#include "lib/core/mp_user_types.h" struct lua_State; struct ibuf; @@ -287,7 +288,7 @@ struct luaL_field { /* Array or map. */ uint32_t size; }; - enum mp_type type; + enum mp_field_type type; bool compact; /* a flag used by YAML serializer */ }; @@ -373,7 +374,7 @@ luaL_checkfield(struct lua_State *L, struct luaL_serializer *cfg, int idx, { if (luaL_tofield(L, cfg, idx, field) < 0) luaT_error(L); - if (field->type != MP_EXT) + if (field->type != MP_FIELD_UNKNOWN) return; luaL_convertfield(L, cfg, idx, field); } diff --git a/third_party/lua-cjson/lua_cjson.c b/third_party/lua-cjson/lua_cjson.c index 631c53e3f..d8244054c 100644 --- a/third_party/lua-cjson/lua_cjson.c +++ b/third_party/lua-cjson/lua_cjson.c @@ -348,15 +348,15 @@ static void json_append_object(lua_State *l, struct luaL_serializer *cfg, struct luaL_field field; luaL_checkfield(l, cfg, -2, &field); - if (field.type == MP_UINT) { + if (field.type == MP_FIELD_UINT) { strbuf_append_char(json, '"'); json_append_uint(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); - } else if (field.type == MP_INT) { + } else if (field.type == MP_FIELD_INT) { strbuf_append_char(json, '"'); json_append_int(cfg, json, field.ival); strbuf_append_mem(json, "\":", 2); - } else if (field.type == MP_STR) { + } else if (field.type == MP_FIELD_STR) { json_append_string(cfg, json, field.sval.data, field.sval.len); strbuf_append_char(json, ':'); } else { @@ -379,38 +379,38 @@ static void json_append_data(lua_State *l, struct luaL_serializer *cfg, struct luaL_field field; luaL_checkfield(l, cfg, -1, &field); switch (field.type) { - case MP_UINT: + case MP_FIELD_UINT: return json_append_uint(cfg, json, field.ival); - case MP_STR: - case MP_BIN: + case MP_FIELD_STR: + case MP_FIELD_BIN: return json_append_string(cfg, json, field.sval.data, field.sval.len); - case MP_INT: + case MP_FIELD_INT: return json_append_int(cfg, json, field.ival); - case MP_FLOAT: + case MP_FIELD_FLOAT: return json_append_number(cfg, json, field.fval); - case MP_DOUBLE: + case MP_FIELD_DOUBLE: return json_append_number(cfg, json, field.dval); - case MP_BOOL: + case MP_FIELD_BOOL: if (field.bval) strbuf_append_mem(json, "true", 4); else strbuf_append_mem(json, "false", 5); break; - case MP_NIL: + case MP_FIELD_NIL: json_append_nil(cfg, json); break; - case MP_MAP: + case MP_FIELD_MAP: if (current_depth >= cfg->encode_max_depth) return json_append_nil(cfg, json); /* Limit nested maps */ json_append_object(l, cfg, current_depth, json); return; - case MP_ARRAY: + case MP_FIELD_ARRAY: /* Array */ if (current_depth >= cfg->encode_max_depth) return json_append_nil(cfg, json); /* Limit nested arrays */ json_append_array(l, cfg, current_depth, json, field.size); return; - case MP_EXT: + case MP_FIELD_UNKNOWN: /* handled by luaL_convertfield */ assert(false); return; diff --git a/third_party/lua-yaml/lyaml.cc b/third_party/lua-yaml/lyaml.cc index 46c98bde1..23d10335b 100644 --- a/third_party/lua-yaml/lyaml.cc +++ b/third_party/lua-yaml/lyaml.cc @@ -620,33 +620,33 @@ static int dump_node(struct lua_yaml_dumper *dumper) int top = lua_gettop(dumper->L); luaL_checkfield(dumper->L, dumper->cfg, top, &field); switch(field.type) { - case MP_UINT: + case MP_FIELD_UINT: snprintf(buf, sizeof(buf) - 1, "%" PRIu64, field.ival); buf[sizeof(buf) - 1] = 0; str = buf; len = strlen(buf); break; - case MP_INT: + case MP_FIELD_INT: snprintf(buf, sizeof(buf) - 1, "%" PRIi64, field.ival); buf[sizeof(buf) - 1] = 0; str = buf; len = strlen(buf); break; - case MP_FLOAT: + case MP_FIELD_FLOAT: fpconv_g_fmt(buf, field.fval, dumper->cfg->encode_number_precision); str = buf; len = strlen(buf); break; - case MP_DOUBLE: + case MP_FIELD_DOUBLE: fpconv_g_fmt(buf, field.dval, dumper->cfg->encode_number_precision); str = buf; len = strlen(buf); break; - case MP_ARRAY: + case MP_FIELD_ARRAY: return dump_array(dumper, &field); - case MP_MAP: + case MP_FIELD_MAP: return dump_table(dumper, &field); - case MP_STR: + case MP_FIELD_STR: str = lua_tolstring(dumper->L, -1, &len); if (yaml_is_null(str, len) || yaml_is_bool(str, len, &unused) || lua_isnumber(dumper->L, -1)) { @@ -672,13 +672,13 @@ static int dump_node(struct lua_yaml_dumper *dumper) break; } /* Fall through */ - case MP_BIN: + case MP_FIELD_BIN: is_binary = 1; tobase64(dumper->L, -1); str = lua_tolstring(dumper->L, -1, &len); tag = (yaml_char_t *) LUAYAML_TAG_PREFIX "binary"; break; - case MP_BOOL: + case MP_FIELD_BOOL: if (field.bval) { str = "true"; len = 4; @@ -687,12 +687,12 @@ static int dump_node(struct lua_yaml_dumper *dumper) len = 5; } break; - case MP_NIL: + case MP_FIELD_NIL: style = YAML_PLAIN_SCALAR_STYLE; str = "null"; len = 4; break; - case MP_EXT: + case MP_FIELD_UNKNOWN: assert(0); /* checked by luaL_checkfield() */ break; } -- 2.20.1 (Apple Git-117)