123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- using System.Runtime.CompilerServices;
- using Jint.Native;
- using Jint.Native.Promise;
- using Jint.Runtime;
- namespace Jint
- {
- public static class JsValueExtensions
- {
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static bool AsBoolean(this JsValue value)
- {
- if (value._type != InternalTypes.Boolean)
- {
- ThrowWrongTypeException(value, "boolean");
- }
- return ((JsBoolean) value)._value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static double AsNumber(this JsValue value)
- {
- if (!value.IsNumber())
- {
- ThrowWrongTypeException(value, "number");
- }
- return ((JsNumber) value)._value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- internal static int AsInteger(this JsValue value)
- {
- return (int) ((JsNumber) value)._value;
- }
- [MethodImpl(MethodImplOptions.AggressiveInlining)]
- public static string AsString(this JsValue value)
- {
- if (!value.IsString())
- {
- ThrowWrongTypeException(value, "string");
- }
- return value.ToString();
- }
-
- /// <summary>
- /// If the value is a Promise
- /// 1. If "Fulfilled" returns the value it was fulfilled with
- /// 2. If "Rejected" throws "PromiseRejectedException" with the rejection reason
- /// 3. If "Pending" throws "InvalidOperationException". Should be called only in "Settled" state
- /// Else
- /// returns the value intact
- /// </summary>
- /// <param name="value">value to unwrap</param>
- /// <returns>inner value if Promise the value itself otherwise</returns>
- public static JsValue UnwrapIfPromise(this JsValue value)
- {
- if (value is PromiseInstance promise)
- {
- return promise.State switch
- {
- PromiseState.Pending => ExceptionHelper.ThrowInvalidOperationException<JsValue>(
- "'UnwrapIfPromise' called before Promise was settled"),
- PromiseState.Fulfilled => promise.Value,
- PromiseState.Rejected => ExceptionHelper.ThrowPromiseRejectedException<JsValue>(promise.Value),
- _ => ExceptionHelper.ThrowArgumentOutOfRangeException<JsValue>()
- };
- }
- return value;
- }
- private static void ThrowWrongTypeException(JsValue value, string expectedType)
- {
- ExceptionHelper.ThrowArgumentException($"Expected {expectedType} but got {value._type}");
- }
- }
- }
|