From ef5088e5958d27016c4e7705685e1367988d8205 Mon Sep 17 00:00:00 2001 From: Vladislav Belov Date: Mon, 13 Jul 2026 23:53:24 +0200 Subject: [PATCH] Uses std::string_view for Script::Interface::Eval --- source/rlinterface/RLInterface.cpp | 2 +- source/scriptinterface/Interface.cpp | 10 +++++----- source/scriptinterface/Interface.h | 9 +++++---- source/scriptinterface/tests/test_FunctionWrapper.h | 2 +- source/scriptinterface/tests/test_ScriptInterface.h | 2 +- 5 files changed, 13 insertions(+), 12 deletions(-) diff --git a/source/rlinterface/RLInterface.cpp b/source/rlinterface/RLInterface.cpp index f134c655b1..a60d0e1ead 100644 --- a/source/rlinterface/RLInterface.cpp +++ b/source/rlinterface/RLInterface.cpp @@ -367,7 +367,7 @@ void Interface::ApplyMessage(const GameMessage& msg) const Script::Interface& scriptInterface = g_Game->GetSimulation2()->GetScriptInterface(); Script::Request rq(scriptInterface); JS::RootedValue ret(rq.cx); - scriptInterface.Eval(m_Code.c_str(), &ret); + scriptInterface.Eval(m_Code, &ret); m_ReturnValue = Script::StringifyJSON(rq, &ret, false); m_MsgApplied.notify_one(); m_MsgLock.unlock(); diff --git a/source/scriptinterface/Interface.cpp b/source/scriptinterface/Interface.cpp index e7db6a21f6..1f9f09745b 100644 --- a/source/scriptinterface/Interface.cpp +++ b/source/scriptinterface/Interface.cpp @@ -734,7 +734,7 @@ bool Interface::LoadGlobalScriptFile(const VfsPath& path) const return LoadGlobalScript(path, code); } -bool Interface::Eval(const char* code) const +bool Interface::Eval(const std::string_view code) const { Request rq(this); JS::RootedValue rval(rq.cx); @@ -743,7 +743,7 @@ bool Interface::Eval(const char* code) const opts.setFileAndLine("(eval)", 1); opts.setForceStrictMode(); JS::SourceText src; - ENSURE(src.init(rq.cx, code, strlen(code), JS::SourceOwnership::Borrowed)); + ENSURE(src.init(rq.cx, code.data(), code.size(), JS::SourceOwnership::Borrowed)); if (JS::Evaluate(rq.cx, opts, src, &rval)) return true; @@ -751,7 +751,7 @@ bool Interface::Eval(const char* code) const return false; } -bool Interface::Eval(const char* code, JS::MutableHandleValue rval) const +bool Interface::Eval(const std::string_view code, JS::MutableHandleValue rval) const { Request rq(this); @@ -759,7 +759,7 @@ bool Interface::Eval(const char* code, JS::MutableHandleValue rval) const opts.setFileAndLine("(eval)", 1); opts.setForceStrictMode(); JS::SourceText src; - ENSURE(src.init(rq.cx, code, strlen(code), JS::SourceOwnership::Borrowed)); + ENSURE(src.init(rq.cx, code.data(), code.size(), JS::SourceOwnership::Borrowed)); if (JS::Evaluate(rq.cx, opts, src, rval)) return true; @@ -767,4 +767,4 @@ bool Interface::Eval(const char* code, JS::MutableHandleValue rval) const return false; } -} // namespace Script \ No newline at end of file +} // namespace Script diff --git a/source/scriptinterface/Interface.h b/source/scriptinterface/Interface.h index c55cd51c5d..6320f10271 100644 --- a/source/scriptinterface/Interface.h +++ b/source/scriptinterface/Interface.h @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -242,9 +243,9 @@ public: * Evaluate some JS code in the global scope. * @return true on successful compilation and execution; false otherwise */ - bool Eval(const char* code) const; - bool Eval(const char* code, JS::MutableHandleValue out) const; - template bool Eval(const char* code, T& out) const; + bool Eval(const std::string_view code) const; + bool Eval(const std::string_view code, JS::MutableHandleValue out) const; + template bool Eval(const std::string_view code, T& out) const; /** * Calls the random number generator assigned to this Interface instance and returns the generated number. @@ -336,7 +337,7 @@ bool Script::Interface::SetGlobal(const char* name, const T& value, bool replace } template -bool Script::Interface::Eval(const char* code, T& ret) const +bool Script::Interface::Eval(const std::string_view code, T& ret) const { Script::Request rq(this); JS::RootedValue rval(rq.cx); diff --git a/source/scriptinterface/tests/test_FunctionWrapper.h b/source/scriptinterface/tests/test_FunctionWrapper.h index cab5934c96..07676fc024 100644 --- a/source/scriptinterface/tests/test_FunctionWrapper.h +++ b/source/scriptinterface/tests/test_FunctionWrapper.h @@ -106,7 +106,7 @@ public: { std::string input = "Test._1p_v(0);"; JS::RootedValue val(rq.cx); - TS_ASSERT(script.Eval(input.c_str(), &val)); + TS_ASSERT(script.Eval(input, &val)); } Script::Function::Register<&TestFunctionWrapper::_3p_r>(script, "_3p_r"); diff --git a/source/scriptinterface/tests/test_ScriptInterface.h b/source/scriptinterface/tests/test_ScriptInterface.h index 2623b2f279..a4d55de4d9 100644 --- a/source/scriptinterface/tests/test_ScriptInterface.h +++ b/source/scriptinterface/tests/test_ScriptInterface.h @@ -263,7 +263,7 @@ public: std::string input = "({'x':1,'z':[2,'3\\u263A\\ud800'],\"y\":true})"; JS::RootedValue val(rq.cx); - TS_ASSERT(script.Eval(input.c_str(), &val)); + TS_ASSERT(script.Eval(input, &val)); std::string stringified = Script::StringifyJSON(rq, &val); TS_ASSERT_STR_EQUALS(stringified, "{\n \"x\": 1,\n \"z\": [\n 2,\n \"3\u263A\\ud800\"\n ],\n \"y\": true\n}");