JavascriptEngine
Regarding my JS issue I started posting about yesterday. I’ve expanded my tests a little it seems like quite a big change to what JS is accepted by the JavascriptEngine especially if one is using it to parse JSON-like data as Javascript code instead of JSON e.g.
{ message: 'hello', counters: [ 1, 2, 3 ] }
as opposed to:
{ "message": "hello", "counters": [ 1, 2, 3 ] }
The former was parsed correctly in JUCE 7 and returned to a var
, but in JUCE 8 it fails to parse. It can be rewritten as a function that returns that. object and is then immediately called, but that approach isn’t backwards compatible with JUCE 7 and fails to parse there.
Stranger is that this one: { message: 'hello' }
— this simply evaulates to the string "hello"
.
Integer values are all parsed in JUCE 8 to doubles too, although this perhaps should be an acceptable change given JS doesn’t really support a separate integer type.
Here is my expanded test case:
TEST_CASE ("JUCE JavascriptEngine evalations all pass on JUCE 7", "[JuceJSEngine]")
{
juce::String jsCode;
juce::String json;
SECTION ("Integer")
{
jsCode = "1"; // PASSES
json = "1";
}
SECTION ("Number")
{
jsCode = "2.0"; // PASSES
json = "2.0";
}
SECTION ("String")
{
jsCode = "'hello'"; // PASSES
json = R"("hello")";
}
SECTION ("Object")
{
jsCode = "{ message: 'hello' }"; // parses but incorrectly on JUCE 8 as just "hello"
json = R"({ "message": "hello" })";
}
SECTION ("Object with trailing commma")
{
jsCode = "{ message: 'hello', }"; // FAILS to parse on JUCE 8, passes in JUCE 7
json = R"({ "message": "hello" })";
}
SECTION ("Array")
{
jsCode = "[ 1, 2, 3 ]"; // FAILS to parse on JUCE 8, passes in JUCE 7
json = R"([ 1, 2, 3 ])";
}
SECTION ("Array with trailing comma")
{
jsCode = "[ 1, 2, 3, ]"; // FAILS to parse on JUCE 8, passes in JUCE 7
json = R"([ 1, 2, 3 ])";
}
SECTION ("Complex object")
{
jsCode = "{ message: 'hello', counters: [ 1, 2, 3 ] }"; // FAILS to parse on JUCE 8, passes in JUCE 7
json = R"({ "message": "hello", "counters": [ 1, 2, 3 ] })";
}
SECTION ("Complex object with inner trailing comma")
{
jsCode = "{ message: 'hello', counters: [ 1, 2, 3, ] }"; // FAILS to parse on JUCE 8, passes in JUCE 7
json = R"({ "message": "hello", "counters": [ 1, 2, 3 ] })";
}
SECTION ("Complex object returned from function")
{
jsCode = R"(
function fn()
{
return { message: "hello", counters: [ 1, 2, 3 ] };
}
fn();
)"; // PASSES on JUCE 8, fails to parse on JUCE 7
json = R"({ "message": "hello", "counters": [ 1, 2, 3 ] })";
}
juce::JavascriptEngine jsEngine;
auto errorMessage = juce::Result::ok();
const auto result = jsEngine.evaluate (jsCode, &errorMessage);
const auto errorString = errorMessage.getErrorMessage();
juce::String log;
constexpr auto divider = "------------------------------------------------\n";
const auto jsonFormat = juce::JSON::FormatOptions().withSpacing (juce::JSON::Spacing::singleLine);
log << divider;
log << "jsCode: " << jsCode << "\n";
log << divider;
log << "json: " << json << "\n";
log << divider;
log << "result: " << juce::JSON::toString (result, jsonFormat) << "\n";
log << divider;
juce::Logger::outputDebugString (log);
REQUIRE (errorString == "");
REQUIRE (JSONUtils::deepEqual (result, juce::JSON::fromString (json)));
}
Output from JUCE 7:
------------------------------------------------
jsCode: 1
------------------------------------------------
json: 1
------------------------------------------------
result: 1
------------------------------------------------
------------------------------------------------
jsCode: 2.0
------------------------------------------------
json: 2.0
------------------------------------------------
result: 2.0
------------------------------------------------
------------------------------------------------
jsCode: 'hello'
------------------------------------------------
json: "hello"
------------------------------------------------
result: "hello"
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello' }
------------------------------------------------
json: { "message": "hello" }
------------------------------------------------
result: {"message": "hello"}
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', }
------------------------------------------------
json: { "message": "hello" }
------------------------------------------------
result: {"message": "hello"}
------------------------------------------------
------------------------------------------------
jsCode: [ 1, 2, 3 ]
------------------------------------------------
json: [ 1, 2, 3 ]
------------------------------------------------
result: [1, 2, 3]
------------------------------------------------
------------------------------------------------
jsCode: [ 1, 2, 3, ]
------------------------------------------------
json: [ 1, 2, 3 ]
------------------------------------------------
result: [1, 2, 3]
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', counters: [ 1, 2, 3 ] }
------------------------------------------------
json: { "message": "hello", "counters": [ 1, 2, 3 ] }
------------------------------------------------
result: {"message": "hello", "counters": [1, 2, 3]}
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', counters: [ 1, 2, 3, ] }
------------------------------------------------
json: { "message": "hello", "counters": [ 1, 2, 3 ] }
------------------------------------------------
result: {"message": "hello", "counters": [1, 2, 3]}
------------------------------------------------
Output from JUCE 8:
------------------------------------------------
jsCode: 1
------------------------------------------------
json: 1
------------------------------------------------
result: 1.0
------------------------------------------------
------------------------------------------------
jsCode: 2.0
------------------------------------------------
json: 2.0
------------------------------------------------
result: 2.0
------------------------------------------------
------------------------------------------------
jsCode: 'hello'
------------------------------------------------
json: "hello"
------------------------------------------------
result: "hello"
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello' }
------------------------------------------------
json: { "message": "hello" }
------------------------------------------------
result: "hello"
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', }
------------------------------------------------
json: { "message": "hello" }
------------------------------------------------
result: undefined
------------------------------------------------
------------------------------------------------
jsCode: [ 1, 2, 3 ]
------------------------------------------------
json: [ 1, 2, 3 ]
------------------------------------------------
result: [1.0, 2.0, 3.0]
------------------------------------------------
------------------------------------------------
jsCode: [ 1, 2, 3, ]
------------------------------------------------
json: [ 1, 2, 3 ]
------------------------------------------------
result: [1.0, 2.0, 3.0]
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', counters: [ 1, 2, 3 ] }
------------------------------------------------
json: { "message": "hello", "counters": [ 1, 2, 3 ] }
------------------------------------------------
result: undefined
------------------------------------------------
------------------------------------------------
jsCode: { message: 'hello', counters: [ 1, 2, 3, ] }
------------------------------------------------
json: { "message": "hello", "counters": [ 1, 2, 3 ] }
------------------------------------------------
result: undefined
------------------------------------------------
------------------------------------------------
jsCode:
function fn()
{
return { message: "hello", counters: [ 1, 2, 3 ] };
}
fn();
------------------------------------------------
json: { "message": "hello", "counters": [ 1, 2, 3 ] }
------------------------------------------------
result: {"message": "hello", "counters": [1.0, 2.0, 3.0]}
------------------------------------------------