IReferenceResolver.cs 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Jint.Native;
  2. namespace Jint.Runtime.Interop;
  3. /// <summary>
  4. /// Reference resolver allows customizing behavior for reference resolving. This can be useful in cases where
  5. /// you want to ignore long chain of property accesses that might throw if anything is null or undefined.
  6. /// An example of such is <code>var a = obj.field.subField.value</code>. Custom resolver could accept chain to return
  7. /// null/undefined on first occurrence.
  8. /// </summary>
  9. public interface IReferenceResolver
  10. {
  11. /// <summary>
  12. /// When unresolvable reference occurs, check if another value can be provided instead of it.
  13. /// </summary>
  14. /// <remarks>
  15. /// A reference error will be thrown if this method return false.
  16. /// </remarks>
  17. /// <param name="engine">The current engine instance.</param>
  18. /// <param name="reference">The reference that is being processed.</param>
  19. /// <param name="value">Value that should be used instead of undefined.</param>
  20. /// <returns>Whether to use <paramref name="value" /> instead of undefined.</returns>
  21. bool TryUnresolvableReference(Engine engine, Reference reference, out JsValue value);
  22. /// <summary>
  23. /// When property reference is being processed, resolve to other value if needed.
  24. /// </summary>
  25. /// <param name="engine">The current engine instance.</param>
  26. /// <param name="reference">The reference that is being processed.</param>
  27. /// <param name="value">Value that should be used instead of reference target.</param>
  28. /// <returns>Whether to use <paramref name="value" /> instead of reference's value.</returns>
  29. bool TryPropertyReference(Engine engine, Reference reference, ref JsValue value);
  30. /// <summary>
  31. /// When evaluating a function call and a target that is not an object is encountered,
  32. /// custom implementation can return a value to call.
  33. /// </summary>
  34. /// <remarks>
  35. /// A reference error will be thrown if this method return false.
  36. /// </remarks>
  37. /// <param name="engine">The current engine instance.</param>
  38. /// <param name="callee">The callee.</param>
  39. /// <param name="value">Value that should be used when this method return true. Should be <see cref="ICallable"/>.</param>
  40. /// <returns>Whether to use <paramref name="value" /> instead of undefined.</returns>
  41. bool TryGetCallable(Engine engine, object callee, out JsValue value);
  42. /// <summary>
  43. /// Check whether objects property value is valid.
  44. /// </summary>
  45. /// <param name="value">The value to check</param>
  46. /// <returns>Whether to accept the value.</returns>
  47. bool CheckCoercible(JsValue value);
  48. }