Compat lua compile as cpp (#1568)

* Compat lua compile as cpp

* Compat lua compile as cpp
This commit is contained in:
Bruce
2022-04-07 17:22:32 +08:00
committed by GitHub
parent 2660b10442
commit 9fd7a51b34
6 changed files with 68 additions and 58 deletions

View File

@@ -85,10 +85,10 @@ bson_reserve(struct bson *b, int sz) {
} while (b->cap <= b->size + sz);
if (b->ptr == b->buffer) {
b->ptr = malloc(b->cap);
b->ptr = (uint8_t*)malloc(b->cap);
memcpy(b->ptr, b->buffer, b->size);
} else {
b->ptr = realloc(b->ptr, b->cap);
b->ptr = (uint8_t*)realloc(b->ptr, b->cap);
}
}
@@ -329,7 +329,7 @@ append_one(struct bson *bs, lua_State *L, const char *key, size_t sz, int depth)
break;
case LUA_TUSERDATA: {
append_key(bs, L, BSON_DOCUMENT, key, sz);
int32_t * doc = lua_touserdata(L,-1);
int32_t * doc = (int32_t*)lua_touserdata(L,-1);
int32_t sz = *doc;
bson_reserve(bs,sz);
memcpy(bs->ptr + bs->size, doc, sz);
@@ -574,7 +574,7 @@ static int
ltostring(lua_State *L) {
size_t sz = lua_rawlen(L, 1);
void * ud = lua_touserdata(L,1);
lua_pushlstring(L, ud, sz);
lua_pushlstring(L, (const char*)ud, sz);
return 1;
}
@@ -591,7 +591,7 @@ make_object(lua_State *L, int type, const void * ptr, size_t len) {
luaL_buffinit(L, &b);
luaL_addchar(&b, 0);
luaL_addchar(&b, type);
luaL_addlstring(&b, ptr, len);
luaL_addlstring(&b, (const char*)ptr, len);
luaL_pushresult(&b);
}
@@ -600,7 +600,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
luaL_checkstack(L, 16, NULL); // reserve enough stack space to unpack table
int sz = read_int32(L, br);
const void * bytes = read_bytes(L, br, sz-5);
struct bson_reader t = { bytes, sz-5 };
struct bson_reader t = { (const uint8_t*)bytes, sz-5 };
int end = read_byte(L, br);
if (end != '\0') {
luaL_error(L, "Invalid document end");
@@ -632,7 +632,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
if (sz <= 0) {
luaL_error(L, "Invalid bson string , length = %d", sz);
}
lua_pushlstring(L, read_bytes(L, &t, sz), sz-1);
lua_pushlstring(L, (const char*)read_bytes(L, &t, sz), sz-1);
break;
}
case BSON_DOCUMENT:
@@ -650,7 +650,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
luaL_addchar(&b, 0);
luaL_addchar(&b, BSON_BINARY);
luaL_addchar(&b, subtype);
luaL_addlstring(&b, read_bytes(L, &t, sz), sz);
luaL_addlstring(&b, (const char*)read_bytes(L, &t, sz), sz);
luaL_pushresult(&b);
break;
}
@@ -666,7 +666,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
case BSON_MINKEY:
case BSON_MAXKEY:
case BSON_NULL: {
char key[] = { 0, bt };
char key[] = { 0, (char)bt };
lua_pushlstring(L, key, sizeof(key));
break;
}
@@ -739,7 +739,7 @@ unpack_dict(lua_State *L, struct bson_reader *br, bool array) {
static int
lmakeindex(lua_State *L) {
int32_t *bson = luaL_checkudata(L,1,"bson");
int32_t *bson = (int32_t*)luaL_checkudata(L,1,"bson");
const uint8_t * start = (const uint8_t *)bson;
struct bson_reader br = { start+4, get_length(start) - 5 };
lua_newtable(L);
@@ -871,7 +871,7 @@ lreplace(lua_State *L) {
int id = lua_tointeger(L, -1);
int type = id & ((1<<(BSON_TYPE_SHIFT)) - 1);
int offset = id >> BSON_TYPE_SHIFT;
uint8_t * start = lua_touserdata(L,1);
uint8_t * start = (uint8_t*)lua_touserdata(L,1);
struct bson b = { 0,16, start + offset };
switch (type) {
case BSON_REAL:
@@ -910,7 +910,7 @@ lreplace(lua_State *L) {
static int
ldecode(lua_State *L) {
const int32_t * data = lua_touserdata(L,1);
const int32_t * data = (const int32_t*)lua_touserdata(L,1);
if (data == NULL) {
return 0;
}
@@ -945,7 +945,7 @@ bson_meta(lua_State *L) {
static int
encode_bson(lua_State *L) {
struct bson *b = lua_touserdata(L, 2);
struct bson *b = (struct bson*)lua_touserdata(L, 2);
lua_settop(L, 1);
if (luaL_getmetafield(L, -1, "__pairs") != LUA_TNIL) {
pack_meta_dict(L, b, 0);
@@ -978,7 +978,7 @@ lencode(lua_State *L) {
static int
encode_bson_byorder(lua_State *L) {
int n = lua_gettop(L);
struct bson *b = lua_touserdata(L, n);
struct bson *b = (struct bson*)lua_touserdata(L, n);
lua_settop(L, --n);
pack_ordered_dict(L, b, n, 0);
lua_settop(L,0);
@@ -1106,7 +1106,7 @@ lsubtype(lua_State *L, int subtype, const uint8_t * buf, size_t sz) {
char oid[24];
int i;
const uint8_t * id = buf;
static char *hex = "0123456789abcdef";
static const char *hex = "0123456789abcdef";
for (i=0;i<12;i++) {
oid[i*2] = hex[id[i] >> 4];
oid[i*2+1] = hex[id[i] & 0xf];
@@ -1219,7 +1219,7 @@ ltype(lua_State *L) {
static void
typeclosure(lua_State *L) {
static const char * typename[] = {
static const char * typename_[] = {
"number", // 1
"boolean", // 2
"table", // 3
@@ -1236,9 +1236,9 @@ typeclosure(lua_State *L) {
"unsupported", // 14
};
int i;
int n = sizeof(typename)/sizeof(typename[0]);
int n = sizeof(typename_)/sizeof(typename_[0]);
for (i=0;i<n;i++) {
lua_pushstring(L,typename[i]);
lua_pushstring(L, typename_[i]);
}
lua_pushcclosure(L, ltype, n);
}
@@ -1347,7 +1347,7 @@ luaopen_bson(lua_State *L) {
char null[] = { 0, BSON_NULL };
lua_pushlstring(L, null, sizeof(null));
lua_setfield(L,-2,"null");
char minkey[] = { 0, BSON_MINKEY };
char minkey[] = { 0, (char)BSON_MINKEY };
lua_pushlstring(L, minkey, sizeof(minkey));
lua_setfield(L,-2,"minkey");
char maxkey[] = { 0, BSON_MAXKEY };