Skip to content

Commit

Permalink
改为c++
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Oct 11, 2024
1 parent d207940 commit 2bfa5a2
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 30 deletions.
28 changes: 16 additions & 12 deletions 3rd/lua-seri/lua-seri.c → 3rd/lua-seri/lua-seri.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,12 @@

#endif

#include <lua.h>
#include <lauxlib.h>
#ifdef __cplusplus
# include <lua.hpp>
#else
# include <lauxlib.h>
# include <lua.h>
#endif
#include <stdlib.h>
#include <stdint.h>
#include <assert.h>
Expand Down Expand Up @@ -92,14 +96,14 @@ struct read_block {

inline static struct block *
blk_alloc(void) {
struct block *b = malloc(sizeof(struct block));
struct block *b = (struct block *)malloc(sizeof(struct block));
b->next = NULL;
return b;
}

static inline void
wb_push(struct write_block *b, const void *buf, int sz) {
const char * buffer = buf;
const char * buffer = (const char *)buf;
if (b->ptr == BLOCK_SIZE) {
_again:
b->current = b->current->next = blk_alloc();
Expand Down Expand Up @@ -355,7 +359,7 @@ mark_table(lua_State *L, struct write_block *b, int index) {
}
if (id < MAX_REFERENCE) {
b->r[id].object = obj;
b->r[id].address = addr;
b->r[id].address = (uint8_t*)addr;
} else {
++id;
lua_pushinteger(L, id);
Expand Down Expand Up @@ -383,12 +387,12 @@ wb_table(lua_State *L, struct write_block *wb, int index) {
static int
ref_ancestor(lua_State *L, struct write_block *b, int index) {
struct stack *s = &b->s;
int n = s->depth;
if (n == 0 || n >= MAX_DEPTH)
int depth = s->depth;
if (depth == 0 || depth >= MAX_DEPTH)
return 0;
int i;
const void * obj = lua_topointer(L, index);
for (i=n-1;i>=0;i--) {
for (i=depth-1;i>=0;i--) {
const void * ancestor = lua_topointer(L, s->ancestor[i]);
if (ancestor == obj) {
uint8_t n = COMBINE_TYPE(TYPE_REF, i);
Expand Down Expand Up @@ -610,7 +614,7 @@ static void unpack_one(lua_State *L, struct read_block *rb);
static int
get_extend_integer(lua_State *L, struct read_block *rb) {
uint8_t type;
const uint8_t *t = rb_read(rb, sizeof(type));
const uint8_t *t = (const uint8_t *)rb_read(rb, sizeof(type));
if (t==NULL) {
invalid_stream(L,rb);
}
Expand Down Expand Up @@ -752,7 +756,7 @@ push_value(lua_State *L, struct read_block *rb, int type, int cookie) {
static void
unpack_one(lua_State *L, struct read_block *rb) {
uint8_t type;
const uint8_t *t = rb_read(rb, sizeof(type));
const uint8_t *t = (const uint8_t *)rb_read(rb, sizeof(type));
if (t==NULL) {
invalid_stream(L, rb);
}
Expand All @@ -762,7 +766,7 @@ unpack_one(lua_State *L, struct read_block *rb) {

static void *
seri(struct block *b, int len) {
uint8_t * buffer = malloc(len + 4);
uint8_t * buffer = (uint8_t *)malloc(len + 4);
memcpy(buffer, &len, 4); // write length
uint8_t * ptr = buffer + 4;
while(len>0) {
Expand Down Expand Up @@ -797,7 +801,7 @@ seri_unpack(lua_State *L, void *buffer) {
luaL_checkstack(L,LUA_MINSTACK,NULL);
}
uint8_t type = 0;
const uint8_t *t = rb_read(&rb, sizeof(type));
const uint8_t *t = (const uint8_t *)rb_read(&rb, sizeof(type));
if (t==NULL)
break;
type = *t;
Expand Down
7 changes: 2 additions & 5 deletions 3rd/lua-seri/lua-seri.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#ifndef LUA_SERIALIZE_H
#define LUA_SERIALIZE_H
#pragma once

#include <lua.h>
struct lua_State;

int seri_unpack(lua_State* L, void* buffer);
int seri_unpackptr(lua_State* L, void* buffer);
void * seri_pack(lua_State* L, int from, int* sz);
void * seri_packstring(const char* str, int sz);

#endif
5 changes: 1 addition & 4 deletions binding/lua_channel.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <3rd/lua-seri/lua-seri.h>
#include <bee/error.h>
#include <bee/lua/binding.h>
#include <bee/lua/module.h>
Expand All @@ -12,10 +13,6 @@
#include <queue>
#include <string>

extern "C" {
#include <3rd/lua-seri/lua-seri.h>
}

namespace bee::lua_channel {
class channel {
public:
Expand Down
5 changes: 1 addition & 4 deletions binding/lua_serialization.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
#include <3rd/lua-seri/lua-seri.h>
#include <bee/lua/binding.h>
#include <bee/lua/module.h>

extern "C" {
#include <3rd/lua-seri/lua-seri.h>
}

namespace bee::lua_serialization {
static int unpack(lua_State* L) {
switch (lua_type(L, 1)) {
Expand Down
5 changes: 1 addition & 4 deletions binding/lua_thread.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <3rd/lua-seri/lua-seri.h>
#include <bee/error.h>
#include <bee/lua/binding.h>
#include <bee/lua/module.h>
Expand All @@ -11,10 +12,6 @@
#include <queue>
#include <string>

extern "C" {
#include <3rd/lua-seri/lua-seri.h>
}

namespace bee::lua_thread {
static const char* errmsg(lua_State* L, int idx) {
const char* msg = lua_tostring(L, idx);
Expand Down
2 changes: 1 addition & 1 deletion compile/common.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ if lm.sanitize then
end

lm:lua_src "source_bee" {
sources = "3rd/lua-seri/lua-seri.c",
sources = "3rd/lua-seri/lua-seri.cpp",
msvc = {
flags = "/wd4244"
}
Expand Down

0 comments on commit 2bfa5a2

Please sign in to comment.