/// <summary> /// Creates a new ScriptVersion with the given parameters. /// </summary> /// <param name="spec">The specification to use, or 0 (zero) to use ECMA standard.</param> /// <param name="specVersion">The specification version. Pass 0 if using ECMA standard.</param> /// <param name="ecmaVersion"></param> /// <param name="allowDeprecated"></param> /// <param name="allowProposals"></param> /// <returns></returns> public static ScriptVersion Create(ScriptVersion spec, int specVersion, int ecmaVersion, bool allowDeprecated, bool allowProposals) { if (spec.GetSpecification() != spec) { throw new ArgumentException("Pure specification expected in parameter.", nameof(spec)); } if (spec == 0 && specVersion != 0) { throw new ArgumentException("Specification version must be zero when no specification is passed.", nameof(specVersion)); } var specVerFld = specVersion * Consts.SVerFld; if (specVerFld >= Consts.SVerLim) { throw new ArgumentException("Specification version overflow.", nameof(specVersion)); } var ecmaVerFld = ecmaVersion * Consts.EcmaFld; if (ecmaVerFld >= Consts.EcmaLim) { throw new ArgumentException("ECMAScript version overflow.", nameof(ecmaVersion)); } var flags = (allowProposals ? 1 : 0) + (allowDeprecated ? 2 : 0); return(spec + specVerFld + ecmaVerFld + flags * Consts.FlagFld); }
/// <summary> /// Changes the script version to accept proposed features. /// </summary> /// <param name="scriptVersion"></param> /// <returns></returns> public static ScriptVersion Proposals(this ScriptVersion scriptVersion, bool value) { return(Create( scriptVersion.GetSpecification(), scriptVersion.GetSpecificationVersion(), scriptVersion.GetStandardVersion(), scriptVersion.IsDeprecated(), value)); }
/// <summary> /// Changes the script version to accept deprecated features. /// </summary> /// <param name="scriptVersion"></param> /// <returns></returns> public static ScriptVersion Deprecated(this ScriptVersion scriptVersion) { return(Create( scriptVersion.GetSpecification(), scriptVersion.GetSpecificationVersion(), scriptVersion.GetStandardVersion(), true, scriptVersion.IsProposals())); }
/// <summary> /// Changes the script version to accept Javascript (Mozilla) features. /// </summary> /// <param name="scriptVersion"></param> /// <returns></returns> public static ScriptVersion Javascript(this ScriptVersion scriptVersion, int specVersion) { if (scriptVersion.GetSpecification() != 0) { throw new ArgumentException("Script version must have no assigned specification.", nameof(scriptVersion)); } return(Create( ScriptVersion.Js, specVersion, scriptVersion.GetStandardVersion(), scriptVersion.IsDeprecated(), scriptVersion.IsProposals())); }
/// <summary> /// Whether a specific version of the script is a superset of another version. /// This can be used as a rather general feature indicator, but there may be exceptions, such as deprecated features. /// </summary> /// <param name="scriptVersion"></param> /// <param name="baseScriptVersion"></param> /// <returns></returns> public static bool IsSupersetOf(this ScriptVersion scriptVersion, ScriptVersion baseScriptVersion) { var specOther = baseScriptVersion.GetSpecification(); if (specOther == 0) { return(scriptVersion.GetStandardVersion() >= baseScriptVersion.GetStandardVersion()); } var specThis = scriptVersion.GetSpecification(); if (specThis == ScriptVersion.NonStandard) { return(scriptVersion.GetStandardVersion() >= baseScriptVersion.GetStandardVersion()); } if (specThis == specOther) { return(scriptVersion.GetSpecificationVersion() >= baseScriptVersion.GetSpecificationVersion()); } return(false); }