static void Ap_join(js_State *J) { char * volatile out = NULL; const char *sep; const char *r; unsigned int seplen; unsigned int k, n, len; len = js_getlength(J, 0); if (js_isdefined(J, 1)) { sep = js_tostring(J, 1); seplen = strlen(sep); } else { sep = ","; seplen = 1; } if (len == 0) { js_pushliteral(J, ""); return; } if (js_try(J)) { js_free(J, out); js_throw(J); } n = 1; for (k = 0; k < len; ++k) { js_getindex(J, 0, k); if (js_isundefined(J, -1) || js_isnull(J, -1)) r = ""; else r = js_tostring(J, -1); n += strlen(r); if (k == 0) { out = js_malloc(J, n); strcpy(out, r); } else { n += seplen; out = realloc(out, n); strcat(out, sep); strcat(out, r); } js_pop(J, 1); } js_pushstring(J, out); js_endtry(J); js_free(J, out); }
static void Ap_pop(js_State *J) { unsigned int n; n = js_getlength(J, 0); if (n > 0) { js_getindex(J, 0, n - 1); js_delindex(J, 0, n - 1); js_setlength(J, 0, n - 1); } else { js_setlength(J, 0, 0); js_pushundefined(J); } }
static void Fp_apply(js_State *J) { int i, n; if (!js_iscallable(J, 0)) js_typeerror(J, "not a function"); js_copy(J, 0); js_copy(J, 1); n = js_getlength(J, 2); for (i = 0; i < n; ++i) js_getindex(J, 2, i); js_call(J, n); }
static void constructbound(js_State *J) { unsigned int top = js_gettop(J); unsigned int i, fun, args, n; fun = js_gettop(J); js_currentfunction(J); js_getproperty(J, fun, "__TargetFunction__"); args = js_gettop(J); js_getproperty(J, fun, "__BoundArguments__"); n = js_getlength(J, args); for (i = 0; i < n; ++i) js_getindex(J, args, i); js_remove(J, args); for (i = 1; i < top; ++i) js_copy(J, i); js_construct(J, n + top - 1); }
static void Ap_shift(js_State *J) { unsigned int k, len; len = js_getlength(J, 0); if (len == 0) { js_setlength(J, 0, 0); js_pushundefined(J); return; } js_getindex(J, 0, 0); for (k = 1; k < len; ++k) { if (js_hasindex(J, 0, k)) js_setindex(J, 0, k - 1); else js_delindex(J, 0, k - 1); } js_delindex(J, 0, len - 1); js_setlength(J, 0, len - 1); }
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; }
pdf_jsimp_obj *pdf_jsimp_array_item(pdf_jsimp *imp, pdf_jsimp_obj *obj, int i) { js_State *J = imp->J; js_getindex(J, IDX(obj), i); return NEWOBJ(J, -1); }