示例#1
0
/* ToString() on a value */
const char *jsV_tostring(js_State *J, js_Value *v)
{
	char buf[32];
	const char *p;
	switch (v->type) {
	default:
	case JS_TSHRSTR: return v->u.shrstr;
	case JS_TUNDEFINED: return "undefined";
	case JS_TNULL: return "null";
	case JS_TBOOLEAN: return v->u.boolean ? "true" : "false";
	case JS_TLITSTR: return v->u.litstr;
	case JS_TMEMSTR: return v->u.memstr->p;
	case JS_TNUMBER:
		p = jsV_numbertostring(J, buf, v->u.number);
		if (p == buf) {
			unsigned int n = strlen(p);
			if (n <= offsetof(js_Value, type)) {
				char *s = v->u.shrstr;
				while (n--) *s++ = *p++;
				*s = 0;
				v->type = JS_TSHRSTR;
				return v->u.shrstr;
			} else {
				v->type = JS_TMEMSTR;
				v->u.memstr = jsV_newmemstring(J, p, n);
				return v->u.memstr->p;
			}
		}
		return p;
	case JS_TOBJECT:
		jsV_toprimitive(J, v, JS_HSTRING);
		return jsV_tostring(J, v);
	}
}
示例#2
0
文件: jsrun.c 项目: v09-software/mujs
void js_pushlstring(js_State *J, const char *v, unsigned int n)
{
	CHECKSTACK(1);
	if (n <= offsetof(js_Value, type)) {
		char *s = STACK[TOP].u.shrstr;
		while (n--) *s++ = *v++;
		*s = 0;
		STACK[TOP].type = JS_TSHRSTR;
	} else {
		STACK[TOP].type = JS_TMEMSTR;
		STACK[TOP].u.memstr = jsV_newmemstring(J, v, n);
	}
	++TOP;
}