|
1 | 1 | using AngleSharp.Attributes;
|
| 2 | +using AngleSharp.Js.Attributes; |
| 3 | +using AngleSharp.Js.Proxies; |
2 | 4 | using Jint.Native.Object;
|
3 | 5 | using Jint.Runtime.Descriptors;
|
4 | 6 | using System;
|
@@ -40,6 +42,39 @@ public static Action<EngineInstance, ObjectInstance> GetConstructorAction(this T
|
40 | 42 | return action;
|
41 | 43 | }
|
42 | 44 |
|
| 45 | + private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _constructorFunctionActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>(); |
| 46 | + |
| 47 | + public static Action<EngineInstance, ObjectInstance> GetConstructorFunctionAction(this Type type) |
| 48 | + { |
| 49 | + if (!_constructorFunctionActions.TryGetValue(type, out var action)) |
| 50 | + { |
| 51 | + var constructorFunctions = type.GetTypeInfo().GetMethods().Where(m => m.GetCustomAttributes<DomConstructorFunctionAttribute>().Any()); |
| 52 | + |
| 53 | + if (constructorFunctions.Any()) |
| 54 | + { |
| 55 | + action = (engine, obj) => |
| 56 | + { |
| 57 | + foreach (var constructorFunction in constructorFunctions) |
| 58 | + { |
| 59 | + var attribute = constructorFunction.GetCustomAttribute<DomConstructorFunctionAttribute>(); |
| 60 | + |
| 61 | + var constructorFunctionInstance = new DomConstructorFunctionInstance(engine, constructorFunction, attribute.OfficialName); |
| 62 | + |
| 63 | + obj.FastSetProperty(attribute.OfficialName, new PropertyDescriptor(constructorFunctionInstance, false, true, false)); |
| 64 | + } |
| 65 | + }; |
| 66 | + } |
| 67 | + else |
| 68 | + { |
| 69 | + action = (e, o) => { }; |
| 70 | + } |
| 71 | + |
| 72 | + _constructorFunctionActions.Add(type, action); |
| 73 | + } |
| 74 | + |
| 75 | + return action; |
| 76 | + } |
| 77 | + |
43 | 78 | private static readonly Dictionary<Type, Action<EngineInstance, ObjectInstance>> _instanceActions = new Dictionary<Type, Action<EngineInstance, ObjectInstance>>();
|
44 | 79 |
|
45 | 80 | public static Action<EngineInstance, ObjectInstance> GetInstanceAction(this Type type)
|
|
0 commit comments