library: buffer_writebuffer api

This commit is contained in:
Adam
2024-11-05 02:21:43 +00:00
parent bb491bc024
commit 2f388495fe
2 changed files with 14 additions and 0 deletions

View File

@@ -1427,6 +1427,7 @@ uint8_t _bolt_plugin_add(const char* path, struct Plugin* plugin) {
API_ADD_SUB(plugin->state, writeinteger, buffer)
API_ADD_SUB(plugin->state, writenumber, buffer)
API_ADD_SUB(plugin->state, writestring, buffer)
API_ADD_SUB(plugin->state, writebuffer, buffer)
lua_settable(plugin->state, -3);
lua_pushliteral(plugin->state, "__gc");
lua_pushcfunction(plugin->state, buffer_gc);
@@ -2890,3 +2891,11 @@ static int api_buffer_writestring(lua_State* state) {
memcpy((uint8_t*)buffer->data + offset, data, size);
return 0;
}
static int api_buffer_writebuffer(lua_State* state) {
const struct FixedBuffer* buffer = require_self_userdata(state, "writebuffer");
const struct FixedBuffer* source = require_userdata(state, 2, "writebuffer");
const long offset = luaL_checklong(state, 3);
memcpy((uint8_t*)buffer->data + offset, source->data, source->size);
return 0;
}

View File

@@ -882,3 +882,8 @@ static int api_buffer_writenumber(lua_State*);
/// Writes a string into the buffer. The first parameter is the string and the second is the offset
/// into the buffer where the string should begin.
static int api_buffer_writestring(lua_State*);
/// [-3, +0, -]
/// Writes the contents of another buffer into this buffer. The first parameter is the buffer to be
/// copied from, and the second is the offset in this buffer where it should be copied to.
static int api_buffer_writebuffer(lua_State*);