mirror of https://github.com/OpenTTD/OpenTTD
Codechange: use std::string_view for sq_pushstring
parent
3020e615a9
commit
ec79ceb2be
|
@ -341,7 +341,7 @@ foreach(LINE IN LISTS SOURCE_LINES)
|
|||
endif()
|
||||
|
||||
string(APPEND SQUIRREL_EXPORT "\n")
|
||||
string(APPEND SQUIRREL_EXPORT "\ntemplate <> SQInteger PushClassName<${CLS}, ScriptType::${APIUC}>(HSQUIRRELVM vm) { sq_pushstring(vm, \"${API_CLS}\", -1); return 1; }")
|
||||
string(APPEND SQUIRREL_EXPORT "\ntemplate <> SQInteger PushClassName<${CLS}, ScriptType::${APIUC}>(HSQUIRRELVM vm) { sq_pushstring(vm, \"${API_CLS}\"); return 1; }")
|
||||
string(APPEND SQUIRREL_EXPORT "\n")
|
||||
|
||||
# Then do the registration functions of the class.
|
||||
|
|
|
@ -237,8 +237,7 @@ void sq_newarray(HSQUIRRELVM v,SQInteger size);
|
|||
void sq_newclosure(HSQUIRRELVM v,SQFUNCTION func,SQUnsignedInteger nfreevars);
|
||||
SQRESULT sq_setparamscheck(HSQUIRRELVM v,SQInteger nparamscheck,const SQChar *typemask);
|
||||
SQRESULT sq_bindenv(HSQUIRRELVM v,SQInteger idx);
|
||||
void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len);
|
||||
inline void sq_pushstring(HSQUIRRELVM v, std::string_view str, SQInteger len = -1) { sq_pushstring(v, str.data(), len == -1 ? str.size() : len); }
|
||||
void sq_pushstring(HSQUIRRELVM v, std::string_view str);
|
||||
void sq_pushfloat(HSQUIRRELVM v,SQFloat f);
|
||||
void sq_pushinteger(HSQUIRRELVM v,SQInteger n);
|
||||
void sq_pushbool(HSQUIRRELVM v,SQBool b);
|
||||
|
|
|
@ -99,7 +99,7 @@ SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
|
|||
{
|
||||
SQInteger i=0;
|
||||
while(mathlib_funcs[i].name!=nullptr) {
|
||||
sq_pushstring(v,mathlib_funcs[i].name,-1);
|
||||
sq_pushstring(v,mathlib_funcs[i].name);
|
||||
sq_newclosure(v,mathlib_funcs[i].f,0);
|
||||
sq_setparamscheck(v,mathlib_funcs[i].nparamscheck,mathlib_funcs[i].typemask);
|
||||
sq_setnativeclosurename(v,-1,mathlib_funcs[i].name);
|
||||
|
@ -107,11 +107,11 @@ SQRESULT sqstd_register_mathlib(HSQUIRRELVM v)
|
|||
i++;
|
||||
}
|
||||
#ifdef EXPORT_DEFAULT_SQUIRREL_FUNCTIONS
|
||||
sq_pushstring(v,"RAND_MAX",-1);
|
||||
sq_pushstring(v,"RAND_MAX");
|
||||
sq_pushinteger(v,RAND_MAX);
|
||||
sq_createslot(v,-3);
|
||||
#endif /* EXPORT_DEFAULT_SQUIRREL_FUNCTIONS */
|
||||
sq_pushstring(v,"PI",-1);
|
||||
sq_pushstring(v,"PI");
|
||||
sq_pushfloat(v,(SQFloat)M_PI);
|
||||
sq_createslot(v,-3);
|
||||
return SQ_OK;
|
||||
|
|
|
@ -210,11 +210,9 @@ void sq_pushnull(HSQUIRRELVM v)
|
|||
v->Push(_null_);
|
||||
}
|
||||
|
||||
void sq_pushstring(HSQUIRRELVM v,const SQChar *s,SQInteger len)
|
||||
void sq_pushstring(HSQUIRRELVM v,std::string_view s)
|
||||
{
|
||||
if(s)
|
||||
v->Push(SQObjectPtr(SQString::Create(_ss(v), s, len)));
|
||||
else v->Push(_null_);
|
||||
v->Push(SQObjectPtr(SQString::Create(_ss(v), s)));
|
||||
}
|
||||
|
||||
void sq_pushinteger(HSQUIRRELVM v,SQInteger n)
|
||||
|
|
|
@ -111,20 +111,20 @@ static SQInteger base_getstackinfos(HSQUIRRELVM v)
|
|||
if(si.funcname)fn = si.funcname;
|
||||
if(si.source)src = si.source;
|
||||
sq_newtable(v);
|
||||
sq_pushstring(v, "func", -1);
|
||||
sq_pushstring(v, fn, -1);
|
||||
sq_pushstring(v, "func");
|
||||
sq_pushstring(v, fn);
|
||||
sq_createslot(v, -3);
|
||||
sq_pushstring(v, "src", -1);
|
||||
sq_pushstring(v, src, -1);
|
||||
sq_pushstring(v, "src");
|
||||
sq_pushstring(v, src);
|
||||
sq_createslot(v, -3);
|
||||
sq_pushstring(v, "line", -1);
|
||||
sq_pushstring(v, "line");
|
||||
sq_pushinteger(v, si.line);
|
||||
sq_createslot(v, -3);
|
||||
sq_pushstring(v, "locals", -1);
|
||||
sq_pushstring(v, "locals");
|
||||
sq_newtable(v);
|
||||
seq=0;
|
||||
while ((name = sq_getlocal(v, level, seq))) {
|
||||
sq_pushstring(v, name, -1);
|
||||
sq_pushstring(v, name);
|
||||
sq_push(v, -2);
|
||||
sq_createslot(v, -4);
|
||||
sq_pop(v, 1);
|
||||
|
@ -272,23 +272,23 @@ void sq_base_register(HSQUIRRELVM v)
|
|||
SQInteger i=0;
|
||||
sq_pushroottable(v);
|
||||
while(base_funcs[i].name!=nullptr) {
|
||||
sq_pushstring(v,base_funcs[i].name,-1);
|
||||
sq_pushstring(v,base_funcs[i].name);
|
||||
sq_newclosure(v,base_funcs[i].f,0);
|
||||
sq_setnativeclosurename(v,-1,base_funcs[i].name);
|
||||
sq_setparamscheck(v,base_funcs[i].nparamscheck,base_funcs[i].typemask);
|
||||
sq_createslot(v,-3);
|
||||
i++;
|
||||
}
|
||||
sq_pushstring(v,"_version_",-1);
|
||||
sq_pushstring(v,SQUIRREL_VERSION,-1);
|
||||
sq_pushstring(v,"_version_");
|
||||
sq_pushstring(v,SQUIRREL_VERSION);
|
||||
sq_createslot(v,-3);
|
||||
sq_pushstring(v,"_charsize_",-1);
|
||||
sq_pushstring(v,"_charsize_");
|
||||
sq_pushinteger(v,sizeof(SQChar));
|
||||
sq_createslot(v,-3);
|
||||
sq_pushstring(v,"_intsize_",-1);
|
||||
sq_pushstring(v,"_intsize_");
|
||||
sq_pushinteger(v,sizeof(SQInteger));
|
||||
sq_createslot(v,-3);
|
||||
sq_pushstring(v,"_floatsize_",-1);
|
||||
sq_pushstring(v,"_floatsize_");
|
||||
sq_pushinteger(v,sizeof(SQFloat));
|
||||
sq_createslot(v,-3);
|
||||
sq_pop(v,1);
|
||||
|
@ -872,13 +872,13 @@ static SQInteger thread_getstatus(HSQUIRRELVM v)
|
|||
SQObjectPtr &o = stack_get(v,1);
|
||||
switch(sq_getvmstate(_thread(o))) {
|
||||
case SQ_VMSTATE_IDLE:
|
||||
sq_pushstring(v,"idle",-1);
|
||||
sq_pushstring(v,"idle");
|
||||
break;
|
||||
case SQ_VMSTATE_RUNNING:
|
||||
sq_pushstring(v,"running",-1);
|
||||
sq_pushstring(v,"running");
|
||||
break;
|
||||
case SQ_VMSTATE_SUSPENDED:
|
||||
sq_pushstring(v,"suspended",-1);
|
||||
sq_pushstring(v,"suspended");
|
||||
break;
|
||||
default:
|
||||
return sq_throwerror(v,"internal VM error");
|
||||
|
|
|
@ -27,7 +27,7 @@ static bool CheckAPIVersion(const std::string &api_version)
|
|||
return std::ranges::find(AIInfo::ApiVersions, api_version) != std::end(AIInfo::ApiVersions);
|
||||
}
|
||||
|
||||
template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq_pushstring(vm, "AIInfo", -1); return 1; }
|
||||
template <> SQInteger PushClassName<AIInfo, ScriptType::AI>(HSQUIRRELVM vm) { sq_pushstring(vm, "AIInfo"); return 1; }
|
||||
|
||||
/* static */ void AIInfo::RegisterAPI(Squirrel &engine)
|
||||
{
|
||||
|
|
|
@ -25,7 +25,7 @@ static bool CheckAPIVersion(const std::string &api_version)
|
|||
return std::ranges::find(GameInfo::ApiVersions, api_version) != std::end(GameInfo::ApiVersions);
|
||||
}
|
||||
|
||||
template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) { sq_pushstring(vm, "GSInfo", -1); return 1; }
|
||||
template <> SQInteger PushClassName<GameInfo, ScriptType::GS>(HSQUIRRELVM vm) { sq_pushstring(vm, "GSInfo"); return 1; }
|
||||
|
||||
/* static */ void GameInfo::RegisterAPI(Squirrel &engine)
|
||||
{
|
||||
|
|
|
@ -357,12 +357,12 @@ void RegisterGameTranslation(Squirrel &engine)
|
|||
|
||||
HSQUIRRELVM vm = engine.GetVM();
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, "GSText", -1);
|
||||
sq_pushstring(vm, "GSText");
|
||||
if (SQ_FAILED(sq_get(vm, -2))) return;
|
||||
|
||||
int idx = 0;
|
||||
for (const auto &p : _current_gamestrings_data->string_names) {
|
||||
sq_pushstring(vm, p, -1);
|
||||
sq_pushstring(vm, p);
|
||||
sq_pushinteger(vm, idx);
|
||||
sq_rawset(vm, -3);
|
||||
idx++;
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "../script_controller.hpp"
|
||||
|
||||
template <> SQInteger PushClassName<ScriptController, ScriptType::AI>(HSQUIRRELVM vm) { sq_pushstring(vm, "AIController", -1); return 1; }
|
||||
template <> SQInteger PushClassName<ScriptController, ScriptType::AI>(HSQUIRRELVM vm) { sq_pushstring(vm, "AIController"); return 1; }
|
||||
|
||||
void SQAIController_Register(Squirrel &engine)
|
||||
{
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
|
||||
#include "../script_controller.hpp"
|
||||
|
||||
template <> SQInteger PushClassName<ScriptController, ScriptType::GS>(HSQUIRRELVM vm) { sq_pushstring(vm, "GSController", -1); return 1; }
|
||||
template <> SQInteger PushClassName<ScriptController, ScriptType::GS>(HSQUIRRELVM vm) { sq_pushstring(vm, "GSController"); return 1; }
|
||||
|
||||
void SQGSController_Register(Squirrel &engine)
|
||||
{
|
||||
|
|
|
@ -125,7 +125,7 @@ ScriptController::ScriptController(::CompanyID company) :
|
|||
|
||||
/* Load the library in a 'fake' namespace, so we can link it to the name the user requested */
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, fake_class, -1);
|
||||
sq_pushstring(vm, fake_class);
|
||||
sq_newclass(vm, SQFalse);
|
||||
/* Load the library */
|
||||
if (!engine->LoadScript(vm, lib->GetMainScript(), false)) {
|
||||
|
@ -140,11 +140,11 @@ ScriptController::ScriptController(::CompanyID company) :
|
|||
|
||||
/* Find the real class inside the fake class (like 'sets.Vector') */
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, fake_class, -1);
|
||||
sq_pushstring(vm, fake_class);
|
||||
if (SQ_FAILED(sq_get(vm, -2))) {
|
||||
throw sq_throwerror(vm, "internal error assigning library class");
|
||||
}
|
||||
sq_pushstring(vm, lib->GetInstanceName(), -1);
|
||||
sq_pushstring(vm, lib->GetInstanceName());
|
||||
if (SQ_FAILED(sq_get(vm, -2))) {
|
||||
throw sq_throwerror(vm, fmt::format("unable to find class '{}' in the library '{}' version {}", lib->GetInstanceName(), library, version));
|
||||
}
|
||||
|
@ -156,7 +156,7 @@ ScriptController::ScriptController(::CompanyID company) :
|
|||
|
||||
/* Now link the name the user wanted to our 'fake' class */
|
||||
sq_pushobject(vm, parent);
|
||||
sq_pushstring(vm, class_name, -1);
|
||||
sq_pushstring(vm, class_name);
|
||||
sq_pushobject(vm, obj);
|
||||
sq_newclass(vm, SQTrue);
|
||||
sq_newslot(vm, -3, SQFalse);
|
||||
|
|
|
@ -139,7 +139,7 @@ static bool ScriptEventAdminPortReadValue(HSQUIRRELVM vm, nlohmann::json &json)
|
|||
|
||||
case nlohmann::json::value_t::string: {
|
||||
auto value = json.get<std::string>();
|
||||
sq_pushstring(vm, value.data(), value.size());
|
||||
sq_pushstring(vm, value);
|
||||
break;
|
||||
}
|
||||
|
||||
|
@ -152,7 +152,7 @@ static bool ScriptEventAdminPortReadValue(HSQUIRRELVM vm, nlohmann::json &json)
|
|||
sq_newtable(vm);
|
||||
|
||||
for (auto &[key, value] : json.items()) {
|
||||
sq_pushstring(vm, key.data(), key.size());
|
||||
sq_pushstring(vm, key);
|
||||
|
||||
if (!ScriptEventAdminPortReadValue(vm, value)) {
|
||||
return false;
|
||||
|
|
|
@ -84,7 +84,7 @@ SQInteger ScriptText::_SetParam(int parameter, HSQUIRRELVM vm)
|
|||
|
||||
/* Validate if it is a GSText instance */
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, "GSText", -1);
|
||||
sq_pushstring(vm, "GSText");
|
||||
sq_get(vm, -2);
|
||||
sq_pushobject(vm, instance);
|
||||
if (sq_instanceof(vm) != SQTrue) return SQ_ERROR;
|
||||
|
@ -171,9 +171,9 @@ void ScriptText::SetPadParameterCount(HSQUIRRELVM vm)
|
|||
|
||||
SQInteger top = sq_gettop(vm);
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, "GSText", -1);
|
||||
sq_pushstring(vm, "GSText");
|
||||
if (!SQ_FAILED(sq_get(vm, -2))) {
|
||||
sq_pushstring(vm, "SCRIPT_TEXT_MAX_PARAMETERS", -1);
|
||||
sq_pushstring(vm, "SCRIPT_TEXT_MAX_PARAMETERS");
|
||||
if (!SQ_FAILED(sq_get(vm, -2))) {
|
||||
SQInteger value;
|
||||
if (!SQ_FAILED(sq_getinteger(vm, -1, &value))) {
|
||||
|
|
|
@ -649,7 +649,7 @@ bool ScriptInstance::IsPaused()
|
|||
ScriptData *data;
|
||||
|
||||
bool operator()(const SQInteger &value) { sq_pushinteger(this->vm, value); return true; }
|
||||
bool operator()(const std::string &value) { sq_pushstring(this->vm, value, -1); return true; }
|
||||
bool operator()(const std::string &value) { sq_pushstring(this->vm, value); return true; }
|
||||
bool operator()(const SQBool &value) { sq_pushbool(this->vm, value); return true; }
|
||||
bool operator()(const SQSaveLoadType &type)
|
||||
{
|
||||
|
@ -778,7 +778,7 @@ bool ScriptInstance::CallLoad()
|
|||
/* Go to the instance-root */
|
||||
sq_pushobject(vm, *this->instance);
|
||||
/* Find the function-name inside the script */
|
||||
sq_pushstring(vm, "Load", -1);
|
||||
sq_pushstring(vm, "Load");
|
||||
/* Change the "Load" string in a function pointer */
|
||||
sq_get(vm, -2);
|
||||
/* Push the main instance as "this" object */
|
||||
|
|
|
@ -257,7 +257,7 @@ void Squirrel::AddMethod(std::string_view method_name, SQFUNCTION proc, std::str
|
|||
{
|
||||
ScriptAllocatorScope alloc_scope(this);
|
||||
|
||||
sq_pushstring(this->vm, method_name, -1);
|
||||
sq_pushstring(this->vm, method_name);
|
||||
|
||||
if (size != 0) {
|
||||
void *ptr = sq_newuserdata(vm, size);
|
||||
|
@ -274,7 +274,7 @@ void Squirrel::AddConst(std::string_view var_name, int value)
|
|||
{
|
||||
ScriptAllocatorScope alloc_scope(this);
|
||||
|
||||
sq_pushstring(this->vm, var_name, -1);
|
||||
sq_pushstring(this->vm, var_name);
|
||||
sq_pushinteger(this->vm, value);
|
||||
sq_newslot(this->vm, -3, SQTrue);
|
||||
}
|
||||
|
@ -283,7 +283,7 @@ void Squirrel::AddConst(std::string_view var_name, bool value)
|
|||
{
|
||||
ScriptAllocatorScope alloc_scope(this);
|
||||
|
||||
sq_pushstring(this->vm, var_name, -1);
|
||||
sq_pushstring(this->vm, var_name);
|
||||
sq_pushbool(this->vm, value);
|
||||
sq_newslot(this->vm, -3, SQTrue);
|
||||
}
|
||||
|
@ -293,7 +293,7 @@ void Squirrel::AddClassBegin(std::string_view class_name)
|
|||
ScriptAllocatorScope alloc_scope(this);
|
||||
|
||||
sq_pushroottable(this->vm);
|
||||
sq_pushstring(this->vm, class_name, -1);
|
||||
sq_pushstring(this->vm, class_name);
|
||||
sq_newclass(this->vm, SQFalse);
|
||||
}
|
||||
|
||||
|
@ -302,8 +302,8 @@ void Squirrel::AddClassBegin(std::string_view class_name, std::string_view paren
|
|||
ScriptAllocatorScope alloc_scope(this);
|
||||
|
||||
sq_pushroottable(this->vm);
|
||||
sq_pushstring(this->vm, class_name, -1);
|
||||
sq_pushstring(this->vm, parent_class, -1);
|
||||
sq_pushstring(this->vm, class_name);
|
||||
sq_pushstring(this->vm, parent_class);
|
||||
if (SQ_FAILED(sq_get(this->vm, -3))) {
|
||||
Debug(misc, 0, "[squirrel] Failed to initialize class '{}' based on parent class '{}'", class_name, parent_class);
|
||||
Debug(misc, 0, "[squirrel] Make sure that '{}' exists before trying to define '{}'", parent_class, class_name);
|
||||
|
@ -329,7 +329,7 @@ bool Squirrel::MethodExists(HSQOBJECT instance, std::string_view method_name)
|
|||
/* Go to the instance-root */
|
||||
sq_pushobject(this->vm, instance);
|
||||
/* Find the function-name inside the script */
|
||||
sq_pushstring(this->vm, method_name, -1);
|
||||
sq_pushstring(this->vm, method_name);
|
||||
if (SQ_FAILED(sq_get(this->vm, -2))) {
|
||||
sq_settop(this->vm, top);
|
||||
return false;
|
||||
|
@ -388,7 +388,7 @@ bool Squirrel::CallMethod(HSQOBJECT instance, std::string_view method_name, HSQO
|
|||
/* Go to the instance-root */
|
||||
sq_pushobject(this->vm, instance);
|
||||
/* Find the function-name inside the script */
|
||||
sq_pushstring(this->vm, method_name, -1);
|
||||
sq_pushstring(this->vm, method_name);
|
||||
if (SQ_FAILED(sq_get(this->vm, -2))) {
|
||||
Debug(misc, 0, "[squirrel] Could not find '{}' in the class", method_name);
|
||||
sq_settop(this->vm, top);
|
||||
|
@ -445,9 +445,9 @@ bool Squirrel::CallBoolMethod(HSQOBJECT instance, std::string_view method_name,
|
|||
|
||||
if (prepend_API_name) {
|
||||
std::string prepended_class_name = fmt::format("{}{}", engine->GetAPIName(), class_name);
|
||||
sq_pushstring(vm, prepended_class_name, -1);
|
||||
sq_pushstring(vm, prepended_class_name);
|
||||
} else {
|
||||
sq_pushstring(vm, class_name, -1);
|
||||
sq_pushstring(vm, class_name);
|
||||
}
|
||||
|
||||
if (SQ_FAILED(sq_get(vm, -2))) {
|
||||
|
|
|
@ -61,7 +61,7 @@ namespace SQConvert {
|
|||
static inline int Set(HSQUIRRELVM vm, std::optional<std::string> res)
|
||||
{
|
||||
if (res.has_value()) {
|
||||
sq_pushstring(vm, res.value(), -1);
|
||||
sq_pushstring(vm, res.value());
|
||||
} else {
|
||||
sq_pushnull(vm);
|
||||
}
|
||||
|
|
|
@ -58,7 +58,7 @@ static std::optional<std::string> TestScriptAdminMakeJSON(std::string_view squir
|
|||
|
||||
/* Insert an (empty) class for testing. */
|
||||
sq_pushroottable(vm);
|
||||
sq_pushstring(vm, "DummyClass", -1);
|
||||
sq_pushstring(vm, "DummyClass");
|
||||
sq_newclass(vm, SQFalse);
|
||||
sq_newslot(vm, -3, SQFalse);
|
||||
sq_pop(vm, 1);
|
||||
|
|
Loading…
Reference in New Issue