Exemple #1
0
JSString *
js_ObjectToString(JSContext *cx, JSObject *obj)
{
    jsval v, *mark, *argv;
    JSString *str;

    if (!obj)
	return ATOM_TO_STRING(cx->runtime->atomState.nullAtom);
    v = JSVAL_VOID;
    if (!obj->map->clasp->convert(cx, obj, JSTYPE_STRING, &v))
	return NULL;
    if (JSVAL_IS_STRING(v))
	return JSVAL_TO_STRING(v);

    /* Try the toString method if it's defined. */
    js_TryMethod(cx, obj, cx->runtime->atomState.toStringAtom, 0, NULL, &v);
    if (JSVAL_IS_STRING(v))
	return JSVAL_TO_STRING(v);
#if JS_BUG_EAGER_TOSTRING
    js_TryValueOf(cx, obj, JSTYPE_STRING, &v);
    if (JSVAL_IS_STRING(v))
	return JSVAL_TO_STRING(v);
#endif
    mark = PR_ARENA_MARK(&cx->stackPool);
    PR_ARENA_ALLOCATE(argv, &cx->stackPool, OBJ_TOSTRING_NARGS * sizeof(jsval));
    if (!argv || !js_obj_toString(cx, obj, OBJ_TOSTRING_NARGS, argv, &v))
	str = NULL;
    else
	str = JSVAL_TO_STRING(v);
    PR_ARENA_RELEASE(&cx->stackPool, mark);
    return str;
}
Exemple #2
0
static JSBool
array_convert(JSContext *cx, JSObject *obj, JSType type, jsval *vp)
{
    jsuint length;

    if (cx->version == JSVERSION_1_2) {
        if (!js_GetLengthProperty(cx, obj, &length))
            return JS_FALSE;
        switch (type) {
          case JSTYPE_NUMBER:
            return IndexToValue(cx, length, vp);
          case JSTYPE_BOOLEAN:
            *vp = BOOLEAN_TO_JSVAL(length > 0);
            return JS_TRUE;
          default:
            return JS_TRUE;
        }
    }
    return js_TryValueOf(cx, obj, type, vp);
}