Ejemplo n.º 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;
}
Ejemplo n.º 2
0
static JSBool
bool_toString(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
	      jsval *rval)
{
    jsval v;
    JSAtom *atom;
    JSString *str;

    if (!JS_InstanceOf(cx, obj, &boolean_class, argv))
	return JS_FALSE;
    v = OBJ_GET_SLOT(cx, obj, JSSLOT_PRIVATE);
    if (!JSVAL_IS_BOOLEAN(v))
	return js_obj_toString(cx, obj, argc, argv, rval);
    atom = cx->runtime->atomState.booleanAtoms[JSVAL_TO_BOOLEAN(v) ? 1 : 0];
    str = ATOM_TO_STRING(atom);
    if (!str)
	return JS_FALSE;
    *rval = STRING_TO_JSVAL(str);
    return JS_TRUE;
}