Skip to main content

to_primitive

Function to_primitive 

Source
pub fn to_primitive(v: &Value, hint: &str) -> Result<Value, String>
Expand description

ToPrimitive(v, hint) — ECMA-262 7.1.1. hint is "default", "number" or "string".

An object carrying a Symbol.toPrimitive method (internal key @@toPrimitive) has it called with the hint and must return a primitive. Otherwise OrdinaryToPrimitive (7.1.1.1) tries valueOf then toString — the order reversed for the string hint — and takes the FIRST call whose result is a primitive. An object that yields no primitive (a null-prototype object has neither method) throws V8’s TypeError: Cannot convert object to primitive value.

This is the conversion behind +, -/*///%/**, the relational operators, == against a primitive, and ToPropertyKey — all of which used to read str_of directly and so never invoked a user valueOf.