-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Passing (very) complex objects into the Javascript Context #277
Comments
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:
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):
An easier script, like this one, works fine:
A more complex one, instead, does not (again, stripped down for brevity):
If I inspect (via
console.log
) the values of the arguments ofcompareEntries
, for example, bothentry
anditem
areundefined
. 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
JSObject
s.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.
The text was updated successfully, but these errors were encountered: