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

@@ -263,11 +263,11 @@ lsha1(lua_State *L) {
#define BLOCKSIZE 64 #define BLOCKSIZE 64
static inline void static inline void
xor_key(uint8_t key[BLOCKSIZE], uint32_t xor) { xor_key(uint8_t key[BLOCKSIZE], uint32_t xor_) {
int i; int i;
for (i=0;i<BLOCKSIZE;i+=sizeof(uint32_t)) { for (i=0;i<BLOCKSIZE;i+=sizeof(uint32_t)) {
uint32_t * k = (uint32_t *)&key[i]; uint32_t * k = (uint32_t *)&key[i];
*k ^= xor; *k ^= xor_;
} }
} }

View File

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

View File

@@ -455,7 +455,7 @@ des_key(lua_State *L, uint32_t SK[32]) {
if (keysz != 8) { if (keysz != 8) {
luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz); luaL_error(L, "Invalid key size %d, need 8 bytes", (int)keysz);
} }
des_main_ks(SK, key); des_main_ks(SK, (const uint8_t*)key);
} }
static int static int
@@ -470,7 +470,7 @@ ldesencode(lua_State *L) {
uint8_t tmp[SMALL_CHUNK]; uint8_t tmp[SMALL_CHUNK];
uint8_t *buffer = tmp; uint8_t *buffer = tmp;
if (chunksz > SMALL_CHUNK) { if (chunksz > SMALL_CHUNK) {
buffer = lua_newuserdatauv(L, chunksz, 0); buffer = (uint8_t*)lua_newuserdatauv(L, chunksz, 0);
} }
int i; int i;
for (i=0;i<(int)textsz-7;i+=8) { for (i=0;i<(int)textsz-7;i+=8) {
@@ -503,7 +503,7 @@ ldesdecode(lua_State *L) {
uint8_t tmp[SMALL_CHUNK]; uint8_t tmp[SMALL_CHUNK];
uint8_t *buffer = tmp; uint8_t *buffer = tmp;
if (textsz > SMALL_CHUNK) { if (textsz > SMALL_CHUNK) {
buffer = lua_newuserdatauv(L, textsz, 0); buffer = (uint8_t*)lua_newuserdatauv(L, textsz, 0);
} }
for (i=0;i<textsz;i+=8) { for (i=0;i<textsz;i+=8) {
des_crypt(SK, text+i, buffer+i); des_crypt(SK, text+i, buffer+i);
@@ -558,7 +558,7 @@ ltohex(lua_State *L) {
char tmp[SMALL_CHUNK]; char tmp[SMALL_CHUNK];
char *buffer = tmp; char *buffer = tmp;
if (sz > SMALL_CHUNK/2) { if (sz > SMALL_CHUNK/2) {
buffer = lua_newuserdatauv(L, sz * 2, 0); buffer = (char*)lua_newuserdatauv(L, sz * 2, 0);
} }
int i; int i;
for (i=0;i<sz;i++) { for (i=0;i<sz;i++) {
@@ -581,7 +581,7 @@ lfromhex(lua_State *L) {
char tmp[SMALL_CHUNK]; char tmp[SMALL_CHUNK];
char *buffer = tmp; char *buffer = tmp;
if (sz > SMALL_CHUNK*2) { if (sz > SMALL_CHUNK*2) {
buffer = lua_newuserdatauv(L, sz / 2, 0); buffer = (char*)lua_newuserdatauv(L, sz / 2, 0);
} }
int i; int i;
for (i=0;i<sz;i+=2) { for (i=0;i<sz;i+=2) {
@@ -898,7 +898,7 @@ lb64encode(lua_State *L) {
char tmp[SMALL_CHUNK]; char tmp[SMALL_CHUNK];
char *buffer = tmp; char *buffer = tmp;
if (encode_sz > SMALL_CHUNK) { if (encode_sz > SMALL_CHUNK) {
buffer = lua_newuserdatauv(L, encode_sz, 0); buffer = (char*)lua_newuserdatauv(L, encode_sz, 0);
} }
int i,j; int i,j;
j=0; j=0;
@@ -953,7 +953,7 @@ lb64decode(lua_State *L) {
char tmp[SMALL_CHUNK]; char tmp[SMALL_CHUNK];
char *buffer = tmp; char *buffer = tmp;
if (decode_sz > SMALL_CHUNK) { if (decode_sz > SMALL_CHUNK) {
buffer = lua_newuserdatauv(L, decode_sz, 0); buffer = (char*)lua_newuserdatauv(L, decode_sz, 0);
} }
int i,j; int i,j;
int output = 0; int output = 0;

View File

@@ -89,10 +89,10 @@ buffer_reserve(struct buffer *b, int sz) {
} while (b->cap <= b->size + sz); } while (b->cap <= b->size + sz);
if (b->ptr == b->buffer) { if (b->ptr == b->buffer) {
b->ptr = skynet_malloc(b->cap); b->ptr = (uint8_t*)malloc(b->cap);
memcpy(b->ptr, b->buffer, b->size); memcpy(b->ptr, b->buffer, b->size);
} else { } else {
b->ptr = skynet_realloc(b->ptr, b->cap); b->ptr = (uint8_t*)realloc(b->ptr, b->cap);
} }
} }
@@ -208,7 +208,7 @@ static int
op_reply(lua_State *L) { op_reply(lua_State *L) {
size_t data_len = 0; size_t data_len = 0;
const char * data = luaL_checklstring(L,1,&data_len); const char * data = luaL_checklstring(L,1,&data_len);
struct { struct reply_type {
// int32_t length; // total message size, including this // int32_t length; // total message size, including this
int32_t request_id; // identifier for this message int32_t request_id; // identifier for this message
int32_t response_id; // requestID from the original request int32_t response_id; // requestID from the original request
@@ -218,7 +218,8 @@ op_reply(lua_State *L) {
int32_t cursor_id[2]; int32_t cursor_id[2];
int32_t starting; int32_t starting;
int32_t number; int32_t number;
} const *reply = (const void *)data; };
const struct reply_type* reply = (const struct reply_type*)data;
if (data_len < sizeof(*reply)) { if (data_len < sizeof(*reply)) {
lua_pushboolean(L, 0); lua_pushboolean(L, 0);

View File

@@ -24,41 +24,49 @@
#else #else
#if defined (__cplusplus)
#include <atomic>
#define STD_ std::
#define atomic_value_type_(p, v) decltype((p)->load())(v)
#else
#include <stdatomic.h> #include <stdatomic.h>
#define STD_
#define atomic_value_type_(p, v) v
#endif
#define ATOM_INT atomic_int #define ATOM_INT STD_ atomic_int
#define ATOM_POINTER atomic_uintptr_t #define ATOM_POINTER STD_ atomic_uintptr_t
#define ATOM_SIZET atomic_size_t #define ATOM_SIZET STD_ atomic_size_t
#define ATOM_ULONG atomic_ulong #define ATOM_ULONG STD_ atomic_ulong
#define ATOM_INIT(ref, v) atomic_init(ref, v) #define ATOM_INIT(ref, v) STD_ atomic_init(ref, v)
#define ATOM_LOAD(ptr) atomic_load(ptr) #define ATOM_LOAD(ptr) STD_ atomic_load(ptr)
#define ATOM_STORE(ptr, v) atomic_store(ptr, v) #define ATOM_STORE(ptr, v) STD_ atomic_store(ptr, v)
static inline int static inline int
ATOM_CAS(atomic_int *ptr, int oval, int nval) { ATOM_CAS(STD_ atomic_int *ptr, int oval, int nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval); return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
} }
static inline int static inline int
ATOM_CAS_SIZET(atomic_size_t *ptr, size_t oval, size_t nval) { ATOM_CAS_SIZET(STD_ atomic_size_t *ptr, size_t oval, size_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval); return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
} }
static inline int static inline int
ATOM_CAS_ULONG(atomic_ulong *ptr, unsigned long oval, unsigned long nval) { ATOM_CAS_ULONG(STD_ atomic_ulong *ptr, unsigned long oval, unsigned long nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval); return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
} }
static inline int static inline int
ATOM_CAS_POINTER(atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) { ATOM_CAS_POINTER(STD_ atomic_uintptr_t *ptr, uintptr_t oval, uintptr_t nval) {
return atomic_compare_exchange_weak(ptr, &(oval), nval); return STD_ atomic_compare_exchange_weak(ptr, &(oval), nval);
} }
#define ATOM_FINC(ptr) atomic_fetch_add(ptr, 1) #define ATOM_FINC(ptr) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr,1))
#define ATOM_FDEC(ptr) atomic_fetch_sub(ptr, 1) #define ATOM_FDEC(ptr) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, 1))
#define ATOM_FADD(ptr,n) atomic_fetch_add(ptr, n) #define ATOM_FADD(ptr,n) STD_ atomic_fetch_add(ptr, atomic_value_type_(ptr, n))
#define ATOM_FSUB(ptr,n) atomic_fetch_sub(ptr, n) #define ATOM_FSUB(ptr,n) STD_ atomic_fetch_sub(ptr, atomic_value_type_(ptr, n))
#define ATOM_FAND(ptr,n) atomic_fetch_and(ptr, n) #define ATOM_FAND(ptr,n) STD_ atomic_fetch_and(ptr, atomic_value_type_(ptr, n))
#endif #endif

View File

@@ -47,10 +47,11 @@ spinlock_destroy(struct spinlock *lock) {
#else // __STDC_NO_ATOMICS__ #else // __STDC_NO_ATOMICS__
#include <stdatomic.h> #include "atomic.h"
#define atomic_test_and_set_(ptr) atomic_exchange_explicit(ptr, 1, memory_order_acquire)
#define atomic_clear_(ptr) atomic_store_explicit(ptr, 0, memory_order_release); #define atomic_test_and_set_(ptr) STD_ atomic_exchange_explicit(ptr, 1, STD_ memory_order_acquire)
#define atomic_load_relaxed_(ptr) atomic_load_explicit(ptr, memory_order_relaxed) #define atomic_clear_(ptr) STD_ atomic_store_explicit(ptr, 0, STD_ memory_order_release);
#define atomic_load_relaxed_(ptr) STD_ atomic_load_explicit(ptr, STD_ memory_order_relaxed)
#if defined(__x86_64__) #if defined(__x86_64__)
#include <immintrin.h> // For _mm_pause #include <immintrin.h> // For _mm_pause
@@ -60,12 +61,12 @@ spinlock_destroy(struct spinlock *lock) {
#endif #endif
struct spinlock { struct spinlock {
atomic_int lock; STD_ atomic_int lock;
}; };
static inline void static inline void
spinlock_init(struct spinlock *lock) { spinlock_init(struct spinlock *lock) {
atomic_init(&lock->lock, 0); STD_ atomic_init(&lock->lock, 0);
} }
static inline void static inline void