Passing (very) complex objects into the Javascript Context
Is there a limit on the complexity of the C# object I can pass into a JSContext? I tried an object like this (stripped down for brevity):
public class Event
{
public List<EventSession> Sessions { get; set; } = new List<EventSession>();
}
public class EventSession
{
public int Type { get; set; }
public List<SessionResultRow> FinalResults { get; set; } = new List<SessionResultRow>();
}
public class SessionResultRow
{
public int Position { get; set; }
public EventEntry Entry { get; set; } = new EventEntry();
}
public class EventEntry
{
public List<Driver> Drivers { get; set; } = new List<Driver>();
public Car Car { get; set; } = new Car();
}
// more classes down the way
An easier script, like this one, works fine:
const session = currentEvent.Sessions.find(s => s.Type === 2)
var output = {
EventFinalResults: [
{
Name: 'Race Results',
Results: session
? session.FinalResults
.map(fr => ({
Position: fr.Position,
Entry: fr.Entry
}))
: []
}
]
}
A more complex one, instead, does not (again, stripped down for brevity):
function compareEntries(i_entry1, i_entry2) {
if (i_entry1 && !i_entry2) return false
if (!i_entry1 && i_entry2) return false
if (!i_entry1 && !i_entry2) return true
return i_entry1.Drivers.length === i_entry2.Drivers.length
&& i_entry1.Drivers.reduce((all, d, i) => all && compareDrivers(d, i_entry2.Drivers[i]), true)
&& compareCars(i_entry1.Car, i_entry2.Car)
&& compareTeams(i_entry1.Team, i_entry2.Team)
}
function flattenArray(i_array) {
return i_array.reduce((acc, val) => acc.concat(val), [])
}
const sessions = currentEvent.Sessions.filter(s => s.Type === 2)
const points = [ /* ... */ ]
const pointsResults = sessions.map(s => s.FinalResults.map(fr => ({ Entry: fr.Entry, Points: points[fr.Position - 1], Result: fr.Position })))
const entries = flattenArray(sessions.map(s => s.FinalResults.map(fr => fr.Entry)))
.reduce((list, entry) => {
if (!list.some(item => compareEntries(entry, item))) {
list.push(entry)
}
return list
}, [])
If I inspect (via console.log) the values of the arguments of compareEntries, for example, both entry and item are undefined. Which is weird, since they are "as deep" as in the first script.
If I look at those variables in Visual Studio's debugger, after going down a couple of levels I start getting C# objects instead of JSObjects.
Workaround
If I serialize the object into a JSON string, then I pass the string into the JSContext and parse it in my Javascript code, then everything works fine.
I run this code:
public class Event
{
public List<EventSession> Sessions { get; set; } = new List<EventSession> { new EventSession { Type = 2 }, new EventSession() };
}
public class EventSession
{
public int Type { get; set; }
public List<SessionResultRow> FinalResults { get; set; } = new List<SessionResultRow> { new SessionResultRow(), new SessionResultRow() };
}
public class SessionResultRow
{
public int Position { get; set; }
public EventEntry Entry { get; set; } = new EventEntry();
}
public class EventEntry
{
public List<Driver> Drivers { get; set; } = new List<Driver>();
public Car Car { get; set; } = new Car();
}
public class Driver { }
public class Car { }
private static void testEx()
{
var context = new Context();
context.DefineVariable("currentEvent").Assign(new Event());
context.Eval(@"
function compareCars(car0, car1) {
console.log('compareCars: ' + car0 + ', ' + car1);
return true;
}
function compareTeams(team0, team1) {
console.log('compareTeams: ' + team0 + ', ' + team1);
}
function compareEntries(i_entry1, i_entry2) {
if (i_entry1 && !i_entry2) return false
if (!i_entry1 && i_entry2) return false
if (!i_entry1 && !i_entry2) return true
return i_entry1.Drivers.length === i_entry2.Drivers.length
&& i_entry1.Drivers.reduce((all, d, i) => all && compareDrivers(d, i_entry2.Drivers[i]), true)
&& compareCars(i_entry1.Car, i_entry2.Car)
&& compareTeams(i_entry1.Team, i_entry2.Team)
}
function flattenArray(i_array) {
return i_array.reduce((acc, val) => acc.concat(val), [])
}
const sessions = currentEvent.Sessions.filter(s => s.Type === 2)
const points = [ /* ... */ ]
const pointsResults = sessions.map(s => s.FinalResults.map(fr => ({ Entry: fr.Entry, Points: points[fr.Position - 1], Result: fr.Position })))
const entries = flattenArray(sessions.map(s => s.FinalResults.map(fr => fr.Entry)))
.reduce((list, entry) => {
console.log('entry: ' + entry);
if (!list.some(item => compareEntries(entry, item))) {
list.push(entry)
}
return list
}, []);
console.log(entries)");
}
and got:
entry: [object EventEntry]
entry: [object EventEntry]
compareCars: [object Car], [object Car]
compareTeams: undefined, undefined
Array (2) [ EventEntry, EventEntry ]
Looks like valid result
Looks like valid result
The types are correct, but the values are not. For example, entries were not deduplicated as they should. I'll try to post a more detailed example.