windows fixes and other correctness/portability stuff

master
Daniel Kolesa 2021-03-31 01:10:58 +02:00
parent eddbf64c87
commit 13d46881e8
9 changed files with 50 additions and 29 deletions

View File

@ -8,6 +8,7 @@
#include <functional>
#include <type_traits>
#include <algorithm>
#include <stdexcept>
#include <memory>
#include <utility>
#include <span>
@ -305,7 +306,7 @@ protected:
alias() = default;
};
struct command: ident {
struct LIBCUBESCRIPT_EXPORT command: ident {
std::string_view get_args() const;
int get_num_args() const;
@ -354,18 +355,18 @@ struct LIBCUBESCRIPT_EXPORT state {
template<typename F>
hook_func set_call_hook(F &&f) {
return std::move(set_call_hook(
return set_call_hook(
hook_func{std::forward<F>(f), callable_alloc, this}
));
);
}
hook_func const &get_call_hook() const;
hook_func &get_call_hook();
template<typename F>
var_print_func set_var_printer(F &&f) {
return std::move(set_var_printer(
return set_var_printer(
var_print_func{std::forward<F>(f), callable_alloc, this}
));
);
}
var_print_func const &get_var_printer() const;
@ -532,7 +533,7 @@ private:
thread_state *p_tstate = nullptr;
};
struct stack_state {
struct LIBCUBESCRIPT_EXPORT stack_state {
struct node {
node const *next;
ident const *id;

View File

@ -706,7 +706,7 @@ static bool compilearg(
}
case VAL_WORD:
if (word) {
*word = std::move(gs.get_str_dup());
*word = gs.get_str_dup();
}
break;
case VAL_ANY:

View File

@ -2,6 +2,7 @@
#include <cmath>
#include <cctype>
#include <iterator>
#include "cs_std.hh"
#include "cs_thread.hh"
@ -18,8 +19,8 @@ LIBCUBESCRIPT_EXPORT char const *parse_string(
if (str.empty() || (str.front() != '\"')) {
return str.data();
}
char const *beg = str.begin();
char const *end = str.end();
char const *beg = &str[0];
char const *end = &str[str.size()];
char const *orig = beg++;
++nl;
while (beg != end) {
@ -66,8 +67,8 @@ end:
LIBCUBESCRIPT_EXPORT char const *parse_word(
state &cs, std::string_view str
) {
char const *it = str.begin();
char const *end = str.end();
char const *it = &str[0];
char const *end = &str[str.size()];
for (; it != end; ++it) {
std::string_view chrs{"\"/;()[] \t\r\n"};
it = std::find_first_of(it, end, chrs.begin(), chrs.end());
@ -148,8 +149,8 @@ static inline bool p_check_neg(char const *&input) {
}
integer_type parse_int(std::string_view input, std::string_view *endstr) {
char const *beg = input.begin();
char const *end = input.end();
char const *beg = &input[0];
char const *end = &input[input.size()];
char const *orig = beg;
beg = p_skip_white(beg, end);
if (beg == end) {
@ -256,8 +257,8 @@ static inline bool parse_gen_float(
}
float_type parse_float(std::string_view input, std::string_view *endstr) {
char const *beg = input.begin();
char const *end = input.end();
char const *beg = &input[0];
char const *end = &input[input.size()];
char const *orig = beg;
beg = p_skip_white(beg, end);
if (beg == end) {

View File

@ -5,6 +5,7 @@
#include "cs_state.hh"
#include <cmath>
#include <iterator>
namespace cubescript {
@ -191,7 +192,7 @@ float_type any_value::force_float() {
float_type rf = 0.0f;
switch (get_type()) {
case value_type::INT:
rf = csv_get<integer_type>(&p_stor);
rf = float_type(csv_get<integer_type>(&p_stor));
break;
case value_type::STRING:
rf = parse_float(
@ -211,7 +212,7 @@ integer_type any_value::force_int() {
integer_type ri = 0;
switch (get_type()) {
case value_type::FLOAT:
ri = csv_get<float_type>(&p_stor);
ri = integer_type(std::floor(csv_get<float_type>(&p_stor)));
break;
case value_type::STRING:
ri = parse_int(

View File

@ -1,5 +1,7 @@
#include <cubescript/cubescript.hh>
#include <iterator>
#include "cs_std.hh"
#include "cs_ident.hh"

View File

@ -8,8 +8,9 @@
namespace cubescript {
static constexpr float_type PI = 3.14159265358979f;
static constexpr float_type RAD = PI / 180.0f;
static constexpr float_type PI = float_type(3.14159265358979323846);
static constexpr float_type LN2 = float_type(0.693147180559945309417);
static constexpr float_type RAD = PI / float_type(180.0);
template<typename T>
struct math_val;
@ -103,7 +104,7 @@ void init_lib_math(state &cs) {
res.set_float(std::log(args[0].get_float()));
});
cs.new_command("log2", "f", [](auto &, auto args, auto &res) {
res.set_float(std::log(args[0].get_float()) / M_LN2);
res.set_float(std::log(args[0].get_float()) / LN2);
});
cs.new_command("log10", "f", [](auto &, auto args, auto &res) {
res.set_float(std::log10(args[0].get_float()));

View File

@ -198,7 +198,6 @@ void init_lib_string(state &cs) {
}
charbuf buf{ccs};
for (size_t i = 0;; ++i) {
std::string_view found;
auto p = s.find(oldval);
if (p == s.npos) {
buf.append(s);

View File

@ -21,10 +21,20 @@ libcubescript_src = [
'lib_str.cc'
]
lib_cxxflags = []
host_system = host_machine.system()
if host_system == 'windows' or host_system == 'cygwin'
lib_cxxflags += '-DLIBCUBESCRIPT_BUILD'
if get_option('default_library') != 'static'
lib_cxxflags += '-DLIBCUBESCRIPT_DLL'
endif
endif
libcubescript_target = library('cubescript',
libcubescript_src,
include_directories: libcubescript_includes + [include_directories('.')],
cpp_args: extra_cxxflags,
cpp_args: extra_cxxflags + lib_cxxflags,
install: true,
pic: true,
version: meson.project_version()

View File

@ -1,6 +1,12 @@
/* avoid silly complaints about fopen */
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#include <signal.h>
#include <cmath>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <optional>
@ -183,9 +189,9 @@ static cs::state *scs = nullptr;
static void do_sigint(int n) {
/* in case another SIGINT happens, terminate normally */
signal(n, SIG_DFL);
scs->set_call_hook([](cs::state &cs) {
cs.set_call_hook(nullptr);
throw cs::error{cs, "<execution interrupted>"};
scs->set_call_hook([](cs::state &css) {
css.set_call_hook(nullptr);
throw cs::error{css, "<execution interrupted>"};
});
}
@ -355,13 +361,13 @@ int main(int argc, char **argv) {
gcs.set_var_printer(repl_print_var);
gcs.init_libs();
gcs.new_command("exec", "s", [](auto &cs, auto args, auto &) {
gcs.new_command("exec", "s", [](auto &css, auto args, auto &) {
auto file = args[0].get_str();
cs::any_value val{cs};
bool ret = do_run_file(cs, file, val);
cs::any_value val{css};
bool ret = do_run_file(css, file, val);
if (!ret) {
throw cs::error(
cs, "could not run file \"%s\"", file
css, "could not run file \"%s\"", file.data()
);
}
});