void js_concat(js_State *J) { js_toprimitive(J, -2, JS_HNONE); js_toprimitive(J, -1, JS_HNONE); if (js_isstring(J, -2) || js_isstring(J, -1)) { const char *sa = js_tostring(J, -2); const char *sb = js_tostring(J, -1); /* TODO: create js_String directly */ char *sab = js_malloc(J, strlen(sa) + strlen(sb) + 1); strcpy(sab, sa); strcat(sab, sb); if (js_try(J)) { js_free(J, sab); js_throw(J); } js_pop(J, 2); js_pushstring(J, sab); js_endtry(J); js_free(J, sab); } else { double x = js_tonumber(J, -2); double y = js_tonumber(J, -1); js_pop(J, 2); js_pushnumber(J, x + y); } }
void js_concat(js_State *J) { js_Value va = js_toprimitive(J, -2, JS_HNONE); js_Value vb = js_toprimitive(J, -1, JS_HNONE); if (va.type == JS_TSTRING || vb.type == JS_TSTRING) { const char *sa = jsV_tostring(J, &va); const char *sb = jsV_tostring(J, &vb); char *sab = js_malloc(J, strlen(sa) + strlen(sb) + 1); strcpy(sab, sa); strcat(sab, sb); if (js_try(J)) { js_free(J, sab); js_throw(J); } js_pop(J, 2); js_pushstring(J, sab); js_endtry(J); js_free(J, sab); } else { double x = jsV_tonumber(J, &va); double y = jsV_tonumber(J, &vb); js_pop(J, 2); js_pushnumber(J, x + y); } }
int js_compare(js_State *J, int *okay) { js_toprimitive(J, -2, JS_HNUMBER); js_toprimitive(J, -1, JS_HNUMBER); *okay = 1; if (js_isstring(J, -2) && js_isstring(J, -1)) { return strcmp(js_tostring(J, -2), js_tostring(J, -1)); } else { double x = js_tonumber(J, -2); double y = js_tonumber(J, -1); if (isnan(x) || isnan(y)) *okay = 0; return x < y ? -1 : x > y ? 1 : 0; } }
int js_compare(js_State *J, int *okay) { js_Value va = js_toprimitive(J, -2, JS_HNUMBER); js_Value vb = js_toprimitive(J, -1, JS_HNUMBER); *okay = 1; if (va.type == JS_TSTRING && vb.type == JS_TSTRING) { return strcmp(va.u.string, vb.u.string); } else { double x = jsV_tonumber(J, &va); double y = jsV_tonumber(J, &vb); if (isnan(x) || isnan(y)) *okay = 0; return x < y ? -1 : x > y ? 1 : 0; } }