diff --git a/include/cubescript/cubescript.hh b/include/cubescript/cubescript.hh index aa80f015..691b83dc 100644 --- a/include/cubescript/cubescript.hh +++ b/include/cubescript/cubescript.hh @@ -424,7 +424,7 @@ struct OSTD_EXPORT CsState { template bool pcall( - F func, ostd::String *error = nullptr, + F func, ostd::ConstCharRange *error = nullptr, CsStackState *stack = nullptr ) { return ipcall([](void *data) { @@ -436,9 +436,15 @@ struct OSTD_EXPORT CsState { template void error(ostd::ConstCharRange msg, A &&...args) { - auto app = ostd::appender(); - ostd::format(app, msg, ostd::forward(args)...); - error(app.get()); + char fbuf[512]; + auto ret = ostd::format( + ostd::CharRange(fbuf, sizeof(fbuf)), msg, ostd::forward(args)... + ); + if ((ret < 0) || (ostd::Size(ret) > sizeof(fbuf))) { + error(msg); + } else { + error(ostd::CharRange(fbuf, ret)); + } } void clear_override(CsIdent &id); @@ -579,10 +585,11 @@ struct OSTD_EXPORT CsState { private: CsIdent *add_ident(CsIdent *id); bool ipcall( - void (*f)(void *data), ostd::String *error, + void (*f)(void *data), ostd::ConstCharRange *error, CsStackState *stack, void *data ); + char p_errbuf[512]; CsHookCb p_callhook; CsPanicCb p_panicfunc; CsStream *p_out, *p_err; diff --git a/src/cs_vm.hh b/src/cs_vm.hh index 80bd9c33..bda03000 100644 --- a/src/cs_vm.hh +++ b/src/cs_vm.hh @@ -99,15 +99,15 @@ constexpr ostd::Size CsTypeStorageSize = (sizeof(T) - 1) / sizeof(ostd::Uint32) + 1; struct CsErrorException { - CsString errmsg; + ostd::ConstCharRange errmsg; CsStackState stack; CsErrorException() = delete; CsErrorException(CsErrorException const &) = delete; CsErrorException(CsErrorException &&v): - errmsg(ostd::move(v.errmsg)), stack(ostd::move(v.stack)) + errmsg(v.errmsg), stack(ostd::move(v.stack)) {} - CsErrorException(CsString &&v, CsStackState &&st): - errmsg(ostd::move(v)), stack(ostd::move(st)) + CsErrorException(ostd::ConstCharRange v, CsStackState &&st): + errmsg(v), stack(ostd::move(st)) {} }; diff --git a/src/cubescript.cc b/src/cubescript.cc index 78fc522d..4bed52fe 100644 --- a/src/cubescript.cc +++ b/src/cubescript.cc @@ -430,7 +430,7 @@ CsPanicCb &CsState::get_panic_func() { } bool CsState::ipcall( - void (*f)(void *data), ostd::String *error, + void (*f)(void *data), ostd::ConstCharRange *error, CsStackState *stack, void *data ) { ++protect; @@ -439,7 +439,7 @@ bool CsState::ipcall( } catch (CsErrorException &v) { --protect; if (error) { - *error = ostd::move(v.errmsg); + *error = v.errmsg; } if (stack) { *stack = ostd::move(v.stack); @@ -454,11 +454,16 @@ bool CsState::ipcall( } void CsState::error(ostd::ConstCharRange msg) { + if (msg.size() > sizeof(p_errbuf)) { + msg = msg.slice(0, sizeof(p_errbuf)); + } + memcpy(p_errbuf, msg.data(), msg.size()); + auto err = ostd::ConstCharRange(p_errbuf, msg.size()); if (protect) { - throw CsErrorException(msg, cs_save_stack(*this)); + throw CsErrorException(err, cs_save_stack(*this)); } else { if (p_panicfunc) { - p_panicfunc(*this, msg, cs_save_stack(*this)); + p_panicfunc(*this, err, cs_save_stack(*this)); } exit(EXIT_FAILURE); } @@ -1140,7 +1145,7 @@ void cs_init_lib_base(CsState &gcs) { ret.set_int(0); return; } - CsString errmsg; + ostd::ConstCharRange errmsg; CsValue result, tback; CsStackState stack; bool rc = cs.pcall([&cs, &args, &result]() { @@ -1148,7 +1153,7 @@ void cs_init_lib_base(CsState &gcs) { }, &errmsg, &stack); ret.set_int(rc); if (!rc) { - result.set_str(ostd::move(errmsg)); + result.set_str(errmsg); if (stack.get()) { auto app = ostd::appender(); cscript::util::print_stack(app, stack); diff --git a/tools/repl.cc b/tools/repl.cc index 0887d864..ca09c26e 100644 --- a/tools/repl.cc +++ b/tools/repl.cc @@ -194,7 +194,7 @@ static bool do_call(CsState &cs, ostd::ConstCharRange line, bool file = false) { CsValue ret; scs = &cs; signal(SIGINT, do_sigint); - ostd::String err; + ostd::ConstCharRange err; cscript::CsStackState st; auto tocall = [&]() { if (file) {