refactor(eval): use arena when converting typvals to Object

Note: this contains two _temporary_ changes which can be reverted
once the Arena vs no-Arena distinction in API wrappers has been removed.
Both nlua_push_Object and object_to_vim_take_luaref() has been changed
to take the object argument as a pointer. This is not going to be
necessary once these are only used with arena (or not at all) allocated
Objects.

The object_to_vim() variant which leaves luaref untouched might need to
stay for a little longer.
This commit is contained in:
bfredl
2024-02-12 20:40:27 +01:00
parent 0a51e7626a
commit d60412b18e
23 changed files with 227 additions and 192 deletions

View File

@@ -449,7 +449,7 @@ static void request_event(void **argv)
e->type,
e->request_id,
&error,
result,
&result,
&out_buffer));
}
if (!handler.arena_return) {
@@ -538,7 +538,7 @@ static void send_error(Channel *chan, MsgpackRpcRequestHandler handler, MessageT
type,
id,
&e,
NIL,
&NIL,
&out_buffer));
api_clear_error(&e);
}
@@ -669,7 +669,7 @@ static WBuffer *serialize_request(uint64_t channel_id, uint32_t request_id, cons
}
static WBuffer *serialize_response(uint64_t channel_id, MsgpackRpcRequestHandler handler,
MessageType type, uint32_t response_id, Error *err, Object arg,
MessageType type, uint32_t response_id, Error *err, Object *arg,
msgpack_sbuffer *sbuffer)
{
msgpack_packer pac;

View File

@@ -10,6 +10,7 @@
#include "msgpack/pack.h"
#include "nvim/api/private/helpers.h"
#include "nvim/assert_defs.h"
#include "nvim/lua/executor.h"
#include "nvim/memory.h"
#include "nvim/msgpack_rpc/helpers.h"
#include "nvim/types_defs.h"
@@ -309,34 +310,40 @@ static void msgpack_rpc_from_handle(ObjectType type, Integer o, msgpack_packer *
}
typedef struct {
const Object *aobj;
Object *aobj;
bool container;
size_t idx;
} APIToMPObjectStackItem;
/// Convert type used by Nvim API to msgpack type.
///
/// consumes (frees) any luaref inside `result`, even though they are not used
/// (just represented as NIL)
///
/// @param[in] result Object to convert.
/// @param[out] res Structure that defines where conversion results are saved.
///
/// @return true in case of success, false otherwise.
void msgpack_rpc_from_object(const Object result, msgpack_packer *const res)
void msgpack_rpc_from_object(Object *result, msgpack_packer *const res)
FUNC_ATTR_NONNULL_ARG(2)
{
kvec_withinit_t(APIToMPObjectStackItem, 2) stack = KV_INITIAL_VALUE;
kvi_init(stack);
kvi_push(stack, ((APIToMPObjectStackItem) { &result, false, 0 }));
kvi_push(stack, ((APIToMPObjectStackItem) { result, false, 0 }));
while (kv_size(stack)) {
APIToMPObjectStackItem cur = kv_last(stack);
STATIC_ASSERT(kObjectTypeWindow == kObjectTypeBuffer + 1
&& kObjectTypeTabpage == kObjectTypeWindow + 1,
"Buffer, window and tabpage enum items are in order");
switch (cur.aobj->type) {
case kObjectTypeNil:
case kObjectTypeLuaRef:
// TODO(bfredl): could also be an error. Though kObjectTypeLuaRef
// should only appear when the caller has opted in to handle references,
// see nlua_pop_Object.
api_free_luaref(cur.aobj->data.luaref);
cur.aobj->data.luaref = LUA_NOREF;
FALLTHROUGH;
case kObjectTypeNil:
msgpack_pack_nil(res);
break;
case kObjectTypeBoolean:
@@ -415,7 +422,7 @@ void msgpack_rpc_from_array(Array result, msgpack_packer *res)
msgpack_pack_array(res, result.size);
for (size_t i = 0; i < result.size; i++) {
msgpack_rpc_from_object(result.items[i], res);
msgpack_rpc_from_object(&result.items[i], res);
}
}
@@ -426,7 +433,7 @@ void msgpack_rpc_from_dictionary(Dictionary result, msgpack_packer *res)
for (size_t i = 0; i < result.size; i++) {
msgpack_rpc_from_string(result.items[i].key, res);
msgpack_rpc_from_object(result.items[i].value, res);
msgpack_rpc_from_object(&result.items[i].value, res);
}
}
@@ -447,9 +454,9 @@ void msgpack_rpc_serialize_request(uint32_t request_id, const String method, Arr
}
/// Serializes a msgpack-rpc response
void msgpack_rpc_serialize_response(uint32_t response_id, Error *err, Object arg,
void msgpack_rpc_serialize_response(uint32_t response_id, Error *err, Object *arg,
msgpack_packer *pac)
FUNC_ATTR_NONNULL_ARG(2, 4)
FUNC_ATTR_NONNULL_ALL
{
msgpack_pack_array(pac, 4);
msgpack_pack_int(pac, 1);