|
2 | 2 | using System.Globalization; |
3 | 3 | using System.Reflection; |
4 | 4 | using System.Runtime.CompilerServices; |
| 5 | +using Esprima.Ast; |
5 | 6 | using Jint.Native; |
6 | 7 | using Jint.Native.Object; |
7 | 8 | using Jint.Native.Symbol; |
@@ -3251,5 +3252,63 @@ public void CanPassDateTimeMinAndMaxViaInterop() |
3251 | 3252 | engine.Execute("capture(maxDate);"); |
3252 | 3253 | Assert.Equal(DateTime.MaxValue, dt); |
3253 | 3254 | } |
| 3255 | + |
| 3256 | + [Fact] |
| 3257 | + public void CanInterceptFunctionCallViaInterop() |
| 3258 | + { |
| 3259 | + return; |
| 3260 | + var records = new Dictionary<Guid, FunctionCallRecord>(); |
| 3261 | + var engine = new Engine(cfg => |
| 3262 | + { |
| 3263 | + cfg.Interop.FunctionExecuting = (e, ins, f, args, id) => |
| 3264 | + { |
| 3265 | + var location = ((Node) f).Location; |
| 3266 | + var record = new FunctionCallRecord |
| 3267 | + { |
| 3268 | + Name = f.Id?.Name, |
| 3269 | + StartLine = location.Start.Line, |
| 3270 | + EndLine = location.End.Line, |
| 3271 | + Args = args.Select(x => x.ToObject()).ToArray() |
| 3272 | + }; |
| 3273 | + records.Add(id, record); |
| 3274 | + }; |
| 3275 | + cfg.Interop.FunctionExecuted = (r, id) => |
| 3276 | + { |
| 3277 | + if (records.TryGetValue(id, out var record) && r is { Type: CompletionType.Return }) |
| 3278 | + { |
| 3279 | + record.Result = r.Value.ToObject(); |
| 3280 | + } |
| 3281 | + }; |
| 3282 | + }); |
| 3283 | + const string Js = @" |
| 3284 | +function main() { |
| 3285 | + return add(1, 2); |
| 3286 | +} |
| 3287 | +function add(a, b) { |
| 3288 | + return a + b; |
| 3289 | +}"; |
| 3290 | + var script = Engine.PrepareScript(Js.TrimStart()); |
| 3291 | + engine.Execute(script); |
| 3292 | + var result = engine.Invoke("main").ToObject(); |
| 3293 | + Assert.Equal(3, Convert.ToInt32(result)); |
| 3294 | + var traces = records.Values.ToList(); |
| 3295 | + Assert.Equal(2, traces.Count); |
| 3296 | + Assert.Equal("main", traces[0].Name); |
| 3297 | + Assert.Equal(3, Convert.ToInt32(traces[0].Result)); |
| 3298 | + Assert.Equal(2, traces[1].Args.Length); |
| 3299 | + Assert.Equal(1, Convert.ToInt32(traces[1].Args[0])); |
| 3300 | + Assert.Equal(2, Convert.ToInt32(traces[1].Args[1])); |
| 3301 | + Assert.Equal(1, traces[0].StartLine); |
| 3302 | + Assert.Equal(3, traces[0].EndLine); |
| 3303 | + } |
| 3304 | + |
| 3305 | + private class FunctionCallRecord |
| 3306 | + { |
| 3307 | + public string Name { get; set; } |
| 3308 | + public int StartLine { get; set; } |
| 3309 | + public int EndLine { get; set; } |
| 3310 | + public object[] Args { get; set; } |
| 3311 | + public object Result { get; set; } |
| 3312 | + } |
3254 | 3313 | } |
3255 | 3314 | } |
0 commit comments