Arguments.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. using System.Runtime.CompilerServices;
  3. using Jint.Native;
  4. namespace Jint.Runtime
  5. {
  6. public static class Arguments
  7. {
  8. public static readonly JsValue[] Empty = Array.Empty<JsValue>();
  9. public static JsValue[] From(params JsValue[] o)
  10. {
  11. return o;
  12. }
  13. /// <summary>
  14. /// Returns the arguments at the provided position or Undefined if not present
  15. /// </summary>
  16. /// <param name="args"></param>
  17. /// <param name="index">The index of the parameter to return</param>
  18. /// <param name="undefinedValue">The value to return is the parameter is not provided</param>
  19. /// <returns></returns>
  20. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  21. public static JsValue At(this JsValue[] args, int index, JsValue undefinedValue)
  22. {
  23. return (uint) index < (uint) args.Length ? args[index] : undefinedValue;
  24. }
  25. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  26. public static JsValue At(this JsValue[] args, int index)
  27. {
  28. return At(args, index, Undefined.Instance);
  29. }
  30. [MethodImpl(MethodImplOptions.AggressiveInlining)]
  31. public static JsValue[] Skip(this JsValue[] args, int count)
  32. {
  33. var newLength = args.Length - count;
  34. if (newLength <= 0)
  35. {
  36. return Array.Empty<JsValue>();
  37. }
  38. var array = new JsValue[newLength];
  39. Array.Copy(args, count, array, 0, newLength);
  40. return array;
  41. }
  42. }
  43. }