API: Move helper functions to another module

This commit is contained in:
Thiago de Arruda
2014-05-08 16:20:24 -03:00
parent e07099cb78
commit 7b04674174
4 changed files with 85 additions and 56 deletions

View File

@@ -6,6 +6,17 @@ file(GLOB API_HEADERS api/*.h)
set(MSGPACK_RPC_HEADER ${PROJECT_SOURCE_DIR}/src/os/msgpack_rpc.h)
set(MSGPACK_DISPATCH ${GENERATED_DIR}/msgpack_dispatch.c)
# Remove helpers.h from API_HEADERS since it doesn't contain public API
# functions
foreach(sfile ${API_HEADERS})
get_filename_component(f ${sfile} NAME)
if(${f} MATCHES "^(helpers.h)$")
list(APPEND to_remove ${sfile})
endif()
endforeach()
list(REMOVE_ITEM API_HEADERS ${to_remove})
set(to_remove)
file(MAKE_DIRECTORY ${GENERATED_DIR})
add_custom_command(OUTPUT ${MSGPACK_DISPATCH}

54
src/api/helpers.c Normal file
View File

@@ -0,0 +1,54 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "api/helpers.h"
#include "api/defs.h"
#include "../vim.h"
void try_start()
{
++trylevel;
}
bool try_end(Error *err)
{
--trylevel;
// Without this it stops processing all subsequent VimL commands and
// generates strange error messages if I e.g. try calling Test() in a
// cycle
did_emsg = false;
if (got_int) {
const char msg[] = "Keyboard interrupt";
if (did_throw) {
// If we got an interrupt, discard the current exception
discard_current_exception();
}
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
got_int = false;
} else if (msg_list != NULL && *msg_list != NULL) {
int should_free;
char *msg = (char *)get_exception_string(*msg_list,
ET_ERROR,
NULL,
&should_free);
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
free_global_msglist();
if (should_free) {
free(msg);
}
} else if (did_throw) {
strncpy(err->msg, (char *)current_exception->value, sizeof(err->msg));
err->set = true;
}
return err->set;
}

19
src/api/helpers.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef NEOVIM_API_HELPERS_H
#define NEOVIM_API_HELPERS_H
#include <stdbool.h>
#include "api/defs.h"
/// Start block that may cause vimscript exceptions
void try_start(void);
/// End try block, set the error message if any and return true if an error
/// occurred.
///
/// @param err Pointer to the stack-allocated error object
/// @return true if an error occurred
bool try_end(Error *err);
#endif /* NEOVIM_API_HELPERS_H */

View File

@@ -5,6 +5,7 @@
#include "api/vim.h"
#include "api/defs.h"
#include "api/helpers.h"
#include "../vim.h"
#include "types.h"
#include "ascii.h"
@@ -18,16 +19,6 @@
KHASH_SET_INIT_INT64(Lookup)
/// Start block that may cause vimscript exceptions
static void try_start(void);
/// End try block, set the error message if any and return true if an error
/// occurred.
///
/// @param err Pointer to the stack-allocated error object
/// @return true if an error occurred
static bool try_end(Error *err);
/// Convert a vim object to an `Object` instance, recursively expanding
/// Arrays/Dictionaries.
///
@@ -251,52 +242,6 @@ void vim_set_current_tabpage(Tabpage tabpage)
abort();
}
static void try_start()
{
++trylevel;
}
static bool try_end(Error *err)
{
--trylevel;
// Without this it stops processing all subsequent VimL commands and
// generates strange error messages if I e.g. try calling Test() in a
// cycle
did_emsg = false;
if (got_int) {
const char msg[] = "Keyboard interrupt";
if (did_throw) {
// If we got an interrupt, discard the current exception
discard_current_exception();
}
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
got_int = false;
} else if (msg_list != NULL && *msg_list != NULL) {
int should_free;
char *msg = (char *)get_exception_string(*msg_list,
ET_ERROR,
NULL,
&should_free);
strncpy(err->msg, msg, sizeof(err->msg));
err->set = true;
free_global_msglist();
if (should_free) {
free(msg);
}
} else if (did_throw) {
strncpy(err->msg, (char *)current_exception->value, sizeof(err->msg));
err->set = true;
}
return err->set;
}
static Object vim_to_object(typval_T *obj)
{
Object rv;