eval: Move get_float_arg to typval.h

Assuming `inline` is there for a reason, so it is kept and function was moved to
typval.h and not to typval.c which does not have problems with #including
message.h.
This commit is contained in:
ZyX
2016-08-21 00:20:01 +03:00
parent 2dcfc439b2
commit 5cdf7177ec
4 changed files with 72 additions and 53 deletions

View File

@@ -6473,24 +6473,6 @@ static int non_zero_arg(typval_T *argvars)
*/
/*
* Get the float value of "argvars[0]" into "f".
* Returns FAIL when the argument is not a Number or Float.
*/
static inline int get_float_arg(typval_T *argvars, float_T *f)
{
if (argvars[0].v_type == VAR_FLOAT) {
*f = argvars[0].vval.v_float;
return OK;
}
if (argvars[0].v_type == VAR_NUMBER) {
*f = (float_T)argvars[0].vval.v_number;
return OK;
}
EMSG(_("E808: Number or Float required"));
return FAIL;
}
// Apply a floating point C function on a typval with one float_T.
//
// Some versions of glibc on i386 have an optimization that makes it harder to
@@ -6502,7 +6484,7 @@ static void float_op_wrapper(typval_T *argvars, typval_T *rettv, FunPtr fptr)
float_T (*function)(float_T) = (float_T (*)(float_T))fptr;
rettv->v_type = VAR_FLOAT;
if (get_float_arg(argvars, &f) == OK) {
if (tv_get_float(argvars, &f)) {
rettv->vval.v_float = function(f);
} else {
rettv->vval.v_float = 0.0;
@@ -6957,14 +6939,15 @@ static void f_assert_true(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_atan2(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
float_T fx, fy;
float_T fx;
float_T fy;
rettv->v_type = VAR_FLOAT;
if (get_float_arg(argvars, &fx) == OK
&& get_float_arg(&argvars[1], &fy) == OK)
if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = atan2(fx, fy);
else
} else {
rettv->vval.v_float = 0.0;
}
}
/*
@@ -8557,13 +8540,14 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
float_T f;
if (get_float_arg(argvars, &f) == OK) {
if (f < -0x7fffffff)
rettv->vval.v_number = -0x7fffffff;
else if (f > 0x7fffffff)
rettv->vval.v_number = 0x7fffffff;
else
if (tv_get_float(argvars, &f)) {
if (f < VARNUMBER_MIN) {
rettv->vval.v_number = VARNUMBER_MIN;
} else if (f > VARNUMBER_MAX) {
rettv->vval.v_number = VARNUMBER_MAX;
} else {
rettv->vval.v_number = (varnumber_T)f;
}
}
}
@@ -8572,14 +8556,15 @@ static void f_float2nr(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_fmod(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
float_T fx, fy;
float_T fx;
float_T fy;
rettv->v_type = VAR_FLOAT;
if (get_float_arg(argvars, &fx) == OK
&& get_float_arg(&argvars[1], &fy) == OK)
if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = fmod(fx, fy);
else
} else {
rettv->vval.v_float = 0.0;
}
}
/*
@@ -12790,14 +12775,15 @@ static void f_pathshorten(typval_T *argvars, typval_T *rettv, FunPtr fptr)
*/
static void f_pow(typval_T *argvars, typval_T *rettv, FunPtr fptr)
{
float_T fx, fy;
float_T fx;
float_T fy;
rettv->v_type = VAR_FLOAT;
if (get_float_arg(argvars, &fx) == OK
&& get_float_arg(&argvars[1], &fy) == OK)
if (tv_get_float(argvars, &fx) && tv_get_float(&argvars[1], &fy)) {
rettv->vval.v_float = pow(fx, fy);
else
} else {
rettv->vval.v_float = 0.0;
}
}
/*

View File

@@ -13,6 +13,7 @@
#include "nvim/lib/queue.h"
#include "nvim/profile.h" // for proftime_T
#include "nvim/pos.h" // for linenr_T
#include "nvim/gettext.h"
/// Type used for VimL VAR_NUMBER values
typedef int varnumber_T;
@@ -370,6 +371,32 @@ extern bool tv_in_free_unref_items;
} \
})
static inline bool tv_get_float(const typval_T *const tv, float_T *const ret_f)
REAL_FATTR_NONNULL_ALL REAL_FATTR_WARN_UNUSED_RESULT;
// FIXME circular dependency, cannot import message.h.
bool emsgf(const char *const fmt, ...);
/// Get the float value
///
/// @param[in] tv VimL object to get value from.
/// @param[out] ret_f Location where resulting float is stored.
///
/// @return true in case of success, false if tv is not a number or float.
static inline bool tv_get_float(const typval_T *const tv, float_T *const ret_f)
{
if (tv->v_type == VAR_FLOAT) {
*ret_f = tv->vval.v_float;
return true;
}
if (tv->v_type == VAR_NUMBER) {
*ret_f = (float_T)tv->vval.v_number;
return true;
}
emsgf(_("E808: Number or Float required"));
return false;
}
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "eval/typval.h.generated.h"
#endif

21
src/nvim/gettext.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef NVIM_GETTEXT_H
#define NVIM_GETTEXT_H
#ifdef HAVE_WORKING_LIBINTL
# include <libintl.h>
# define _(x) gettext((char *)(x))
// XXX do we actually need this?
# ifdef gettext_noop
# define N_(x) gettext_noop(x)
# else
# define N_(x) x
# endif
#else
# define _(x) ((char *)(x))
# define N_(x) x
# define bindtextdomain(x, y) // empty
# define bind_textdomain_codeset(x, y) // empty
# define textdomain(x) // empty
#endif
#endif // NVIM_GETTEXT_H

View File

@@ -51,22 +51,7 @@ Error: configure did not run properly.Check auto/config.log.
/* ================ end of the header file puzzle =============== */
#ifdef HAVE_WORKING_LIBINTL
# include <libintl.h>
# define _(x) gettext((char *)(x))
// XXX do we actually need this?
# ifdef gettext_noop
# define N_(x) gettext_noop(x)
# else
# define N_(x) x
# endif
#else
# define _(x) ((char *)(x))
# define N_(x) x
# define bindtextdomain(x, y) /* empty */
# define bind_textdomain_codeset(x, y) /* empty */
# define textdomain(x) /* empty */
#endif
#include "nvim/gettext.h"
/* special attribute addition: Put message in history */
#define MSG_HIST 0x1000