Skip to content

Passing (very) complex objects into the Javascript Context #277

@savissimo

Description

@savissimo

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions