using Microsoft.ClearScript.V8; using Newtonsoft.Json; foreach (var disableNarrowing in new[] { true, false }) { using var engine = new V8ScriptEngine(V8ScriptEngineFlags.EnableDateTimeConversion) { DisableFloatNarrowing = disableNarrowing }; engine.AddHostObject("host", new ProbeHost()); engine.AddHostType(typeof(JsonConvert)); void Show(string name, string code) => Console.WriteLine(name + ": " + engine.Evaluate(code)); Console.WriteLine("DisableFloatNarrowing=" + disableNarrowing); Show("numbers", "JSON.stringify([host.Kind(1), host.Kind(1.25), host.Kind(1.23456789)])"); Show("array", "JSON.stringify([Array.isArray(host.Values), host.Values.Length, typeof host.Values.map, Array.from(host.Values).join(',')])"); Show("list", "JSON.stringify([Array.isArray(host.List), host.List.Count, Array.from(host.List).join(',')])"); Show("date", "JSON.stringify([host.UtcDate instanceof Date, host.UtcDate.toISOString(), host.DateKind(new Date('2026-10-12T00:00:00Z'))])"); Show("Int64", "JSON.stringify([typeof host.Big, host.Big === 9007199254740992, typeof 1n])"); Show("decimal", "JSON.stringify([typeof host.Amount, host.Amount + 0.2])"); Show("JSON.stringify", "JSON.stringify(host)"); Show("JsonConvert", "JsonConvert.SerializeObject(host)"); Show("globals", "JSON.stringify([typeof window, typeof document, typeof fetch, typeof setTimeout, typeof require, typeof Promise])"); Show("case", "JSON.stringify([typeof host.Kind, typeof host.kind])"); Show("task", "JSON.stringify([host.TaskValue instanceof Promise, typeof host.TaskValue.then])"); Show("exception", "(()=>{try{host.Fail(); return false;}catch(e){return !!e.hostException;}})()"); Show("null", "JSON.stringify([host.Nothing === null, typeof host.Unknown])"); } public class ProbeHost { public int[] Values => new[] { 10, 20 }; public List List => new() { 10, 20 }; public DateTime UtcDate => new(2026, 10, 12, 0, 0, 0, DateTimeKind.Utc); public long Big => 9007199254740993L; public decimal Amount => 0.1M; public object Nothing => null; public Task TaskValue => Task.FromResult(42); public string Kind(object value) => value.GetType().Name; public string DateKind(DateTime value) => value.Kind.ToString(); public void Fail() => throw new InvalidOperationException("probe"); }