Jurassic.Library.ObjectInstance.GetPrimitiveValuePreES6 C# (CSharp) Method

GetPrimitiveValuePreES6() private method

Returns a primitive value that represents the current object, without using the @@toPrimitive symbol.
private GetPrimitiveValuePreES6 ( PrimitiveTypeHint typeHint ) : object
typeHint PrimitiveTypeHint Indicates the preferred type of the result.
return object
        internal object GetPrimitiveValuePreES6(PrimitiveTypeHint typeHint)
        {
            if (typeHint == PrimitiveTypeHint.None || typeHint == PrimitiveTypeHint.Number)
            {

                // Try calling valueOf().
                object valueOfResult;
                if (TryCallMemberFunction(out valueOfResult, "valueOf") == true)
                {
                    // Return value must be primitive.
                    if (valueOfResult is double || TypeUtilities.IsPrimitive(valueOfResult) == true)
                        return valueOfResult;
                }

                // Try calling toString().
                object toStringResult;
                if (TryCallMemberFunction(out toStringResult, "toString") == true)
                {
                    // Return value must be primitive.
                    if (toStringResult is string || TypeUtilities.IsPrimitive(toStringResult) == true)
                        return toStringResult;
                }

            }
            else
            {

                // Try calling toString().
                object toStringResult;
                if (TryCallMemberFunction(out toStringResult, "toString") == true)
                {
                    // Return value must be primitive.
                    if (toStringResult is string || TypeUtilities.IsPrimitive(toStringResult) == true)
                        return toStringResult;
                }

                // Try calling valueOf().
                object valueOfResult;
                if (TryCallMemberFunction(out valueOfResult, "valueOf") == true)
                {
                    // Return value must be primitive.
                    if (valueOfResult is double || TypeUtilities.IsPrimitive(valueOfResult) == true)
                        return valueOfResult;
                }

            }

            throw new JavaScriptException(this.Engine, ErrorType.TypeError, "Attempted conversion of the object to a primitive value failed.  Check the toString() and valueOf() functions.");
        }

Usage Example

示例#1
0
 private static object ToPrimitive(ScriptEngine engine, ObjectInstance thisObj, string hint)
 {
     // This behaviour differs from the standard behaviour only in that the "default" hint
     // results in a conversion to a string, not a number.
     if (hint == "default" || hint == "string")
         return thisObj.GetPrimitiveValuePreES6(PrimitiveTypeHint.String);
     if (hint == "number")
         return thisObj.GetPrimitiveValuePreES6(PrimitiveTypeHint.Number);
     throw new JavaScriptException(engine, ErrorType.TypeError, "Invalid type hint.");
 }