public static string ToStringJS(ScriptEngine engine, object thisObject)
{
if (thisObject == null || thisObject == Undefined.Value)
return "[object Undefined]";
if (thisObject == Null.Value)
return "[object Null]";
var obj = TypeConverter.ToObject(engine, thisObject);
// ES6 - if the value of @@toStringTag is a string, use it to form the result.
object tag = obj.GetPropertyValue(engine.Symbol.ToStringTag);
if (tag is string)
return $"[object {tag}]";
// Fall back to previous behaviour.
if (obj is ArrayInstance)
return "[object Array]";
if (obj is StringInstance)
return "[object String]";
if (obj is ArgumentsInstance)
return "[object Arguments]";
if (obj is FunctionInstance)
return "[object Function]";
if (obj is ErrorInstance)
return "[object Error]";
if (obj is BooleanInstance)
return "[object Boolean]";
if (obj is NumberInstance)
return "[object Number]";
if (obj is DateInstance)
return "[object Date]";
if (obj is RegExpInstance)
return "[object RegExp]";
return "[object Object]";
}