mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-16 14:34:20 +00:00
Uses std::string_view for Script::Interface::Eval
This commit is contained in:
@@ -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<mozilla::Utf8Unit> 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<mozilla::Utf8Unit> 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
|
||||
} // namespace Script
|
||||
|
||||
@@ -34,6 +34,7 @@
|
||||
#include <js/ValueArray.h>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include <utility>
|
||||
@@ -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<typename T> 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<typename T> 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<typename T>
|
||||
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);
|
||||
|
||||
@@ -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");
|
||||
|
||||
@@ -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}");
|
||||
|
||||
Reference in New Issue
Block a user