Arguments.cs 1.5 KB

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