1
1
forked from 0ad/0ad

Fix off-by-one line number in JS error reporting.

Post SM60 upgrade, CompileFunction started actually adding a line in
front of the source buffer, shifting the error reporting in the
simulation and other callers of LoadScript.
The GUI isn't affected as it uses LoadGlobalScript, which uses Evaluate
directly.

Refs #5859
Fixes #5895

Differential Revision: https://code.wildfiregames.com/D3257
This was SVN commit r24455.
This commit is contained in:
wraitii 2020-12-27 07:51:30 +00:00
parent 8a77efd849
commit 1e8299dcdb
2 changed files with 6 additions and 3 deletions

View File

@ -807,7 +807,10 @@ bool ScriptInterface::LoadScript(const VfsPath& filename, const std::string& cod
std::string filenameStr = filename.string8();
JS::CompileOptions options(rq.cx);
options.setFileAndLine(filenameStr.c_str(), 1);
// Set the line to 0 because CompileFunction silently adds a `(function() {` as the first line,
// and errors get misreported.
// TODO: it would probably be better to not implicitly introduce JS scopes.
options.setFileAndLine(filenameStr.c_str(), 0);
options.setIsRunOnce(false);
JS::SourceText<mozilla::Utf8Unit> src;

View File

@ -40,7 +40,7 @@ public:
ScriptInterface script("Test", "Test", g_ScriptContext);
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", "1+"));
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 3\nexpected expression, got \'}\'");
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 2\nexpected expression, got \'}\'");
}
void test_loadscript_strict_warning()
@ -57,7 +57,7 @@ public:
ScriptInterface script("Test", "Test", g_ScriptContext);
TestLogger logger;
TS_ASSERT(!script.LoadScript(L"test.js", "with(1){}"));
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 2\nstrict mode code may not contain \'with\' statements");
TS_ASSERT_STR_CONTAINS(logger.GetOutput(), "ERROR: JavaScript error: test.js line 1\nstrict mode code may not contain \'with\' statements");
}
void test_clone_basic()