so, i want that my native functions that recieve boolean parameters, will
also be able to recieve boolean objects as well as primitives (without
forcing the user to invoke valueOf() directly). i can't use
JS_ValueToBoolean as i use other JS_ValueTo* to have the same behaviour,
what should i do?
thanks in advance
o = {valueOf:function(t){return (t == "boolean") ? o.myBoolValue
: o},
myBoolValue:true};
return (someExpression && o) || someOtherExpression;
in a function. The return expression evaluates someExpression and, if it converts to true, then evaluates o by calling o.valueOf("boolean"), which returns true, in order to evaluate the first && expression, whose result is o, an object reference. It then converts o to boolean again in order to decide whether the || result is o or the value of someOtherExpression.
At least one member of the ECMA TC39 working group felt strongly that requiring object-to-boolean conversion to be idempotent and cheap was too much to require of all object classes, and anyway was too different from all other conversion cases, where there is no possibility of an expression converting any of its operands more than once. I agreed, and this conversion case was dropped from our proposal for the ECMA-262 standard. It survives in JavaScript1.2, which you can enable in HTML and XUL via (with html: namespace and lower-case names for XUL) <SCRIPT LANGUAGE="JavaScript1.2">, but note well that the 1.2 version of JS has == and != operators that act as === and !== do in ECMA and modern JS.
/be
Brendan Eich <bre...@meer.net> wrote in message news:38D67B33...@meer.net...
What you can do from C or C++, rather than add JS_CallFunctionName with "valueOf" as the name, that's cheaper:
// assume the value to convert is in jsval v
JSBool b;
JSVersion oldVersion = JS_SetVersion(cx, JSVERSION_1_1);
ok = JS_ValueToBoolean(cx, v, &b);
JS_SetVersion(cx, oldVersion);
JSVERSION_1_1 is cheaper here than JSVERSION_1_2, and just as effective for enabling object-to-boolean conversions, because it doesn't change the meaning of the equality operators. This sequence will be cheaper still if you apply the attached patch.
/be
/be