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); } }
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; } }
void js_eval(js_State *J) { if (!js_isstring(J, -1)) return; js_loadeval(J, "(eval)", js_tostring(J, -1)); js_rot2pop1(J); js_copy(J, 0); /* copy 'this' */ js_call(J, 0); }
static void jsB_eval(js_State *J) { if (!js_isstring(J, -1)) { js_copy(J, 1); return; } js_loadstring(J, "(eval)", js_tostring(J, -1)); js_pushglobal(J); js_call(J, 0); }
int pdf_jsimp_to_type(pdf_jsimp *imp, pdf_jsimp_obj *obj) { js_State *J = imp->J; if (js_isnull(J, IDX(obj))) return JS_TYPE_NULL; if (js_isboolean(J, IDX(obj))) return JS_TYPE_BOOLEAN; if (js_isnumber(J, IDX(obj))) return JS_TYPE_NUMBER; if (js_isstring(J, IDX(obj))) return JS_TYPE_STRING; if (js_isarray(J, IDX(obj))) return JS_TYPE_ARRAY; return JS_TYPE_UNKNOWN; }
JsonNode * js_util_tojsonnode(js_State *state, int idx) { const char *s; JsonNode *node; JsonNode *tmp; JsonObject *object; JsonArray *array; unsigned int i, length; node = json_node_alloc(); if (js_isstring(state, idx)) { json_node_init_string(node, js_tostring(state, idx)); } else if (js_isnumber(state, idx)) { json_node_init_int(node, js_tointeger(state, idx)); } else if (js_isboolean(state, idx)) { json_node_init_boolean(node, js_toboolean(state, idx)); } else if (js_isarray(state, idx)) { length = js_getlength(state, idx); array = json_array_new(); json_node_init_array(node, array); for (i = 0; i < length; i++) { js_getindex(state, idx, i); tmp = js_util_tojsonnode(state, -1); if (tmp) json_array_add_element(array, tmp); js_pop(state, 1); } json_array_unref(array); } else if (js_isobject(state, idx)) { object = json_object_new(); json_node_init_object(node, object); js_pushiterator(state, idx, 1); while((s = js_nextiterator(state, -1)) != NULL) { if (idx > 0) js_getproperty(state, idx, s); else js_getproperty(state, idx - 1, s); tmp = js_util_tojsonnode(state, -1); if (tmp) json_object_set_member(object, s, tmp); js_pop(state, 1); } js_pop(state, 1); json_object_unref(object); } else { json_node_free(node); return NULL; } return node; }