static void jsB_print(js_State *J) { int i, top = js_gettop(J); for (i = 1; i < top; ++i) { const char *s = js_tostring(J, i); if (i > 1) putchar(' '); fputs(s, stdout); } putchar('\n'); js_pushundefined(J); }
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 Fp_call(js_State *J) { unsigned int i, top = js_gettop(J); if (!js_iscallable(J, 0)) js_typeerror(J, "not a function"); for (i = 0; i < top; ++i) js_copy(J, i); js_call(J, top - 2); }
static void Ap_splice(js_State *J) { unsigned int top = js_gettop(J); unsigned int len, start, del, add, k; double f; js_newarray(J); len = js_getlength(J, 0); f = js_tointeger(J, 1); if (f < 0) f = f + len; start = f < 0 ? 0 : f > len ? len : f; f = js_tointeger(J, 2); del = f < 0 ? 0 : f > len - start ? len - start : f; /* copy deleted items to return array */ for (k = 0; k < del; ++k) if (js_hasindex(J, 0, start + k)) js_setindex(J, -2, k); js_setlength(J, -1, del); /* shift the tail to resize the hole left by deleted items */ add = top - 3; if (add < del) { for (k = start; k < len - del; ++k) { if (js_hasindex(J, 0, k + del)) js_setindex(J, 0, k + add); else js_delindex(J, 0, k + add); } for (k = len; k > len - del + add; --k) js_delindex(J, 0, k - 1); } else if (add > del) { for (k = len - del; k > start; --k) { if (js_hasindex(J, 0, k + del - 1)) js_setindex(J, 0, k + add - 1); else js_delindex(J, 0, k + add - 1); } } /* copy new items into the hole */ for (k = 0; k < add; ++k) { js_copy(J, 3 + k); js_setindex(J, 0, start + k); } js_setlength(J, 0, len - del + add); }
static void Ap_push(js_State *J) { unsigned int i, top = js_gettop(J); unsigned int n; n = js_getlength(J, 0); for (i = 1; i < top; ++i, ++n) { js_copy(J, i); js_setindex(J, 0, n); } js_setlength(J, 0, n); js_pushnumber(J, n); }
static void Math_max(js_State *J) { unsigned int i, n = js_gettop(J); double x = -INFINITY; for (i = 1; i < n; ++i) { double y = js_tonumber(J, i); if (isnan(y)) { x = y; break; } if (signbit(x) == signbit(y)) x = x > y ? x : y; else if (signbit(x)) x = y; } js_pushnumber(J, x); }
static void wrapmethod(js_State *J) { pdf_jsimp_obj *args[MAXARGS]; pdf_jsimp_obj *ret; pdf_jsimp_method *meth; const char *type; void *jsctx; void *obj; int i; int argc = js_gettop(J) - 1; js_getregistry(J, "jsctx"); jsctx = js_touserdata(J, "jsctx", -1); js_pop(J, 1); js_currentfunction(J); { js_getproperty(J, -1, "__call"); meth = js_touserdata(J, "method", -1); js_pop(J, 1); js_getproperty(J, -1, "__type"); type = js_tostring(J, -1); js_pop(J, 1); } js_pop(J, 1); if (js_isuserdata(J, type, 0)) obj = js_touserdata(J, type, 0); else obj = NULL; if (argc > MAXARGS) js_rangeerror(J, "too many arguments"); for (i = 0; i < argc; ++i) args[i] = OBJ(i+1); ret = meth(jsctx, obj, argc, args); if (ret) js_copy(J, IDX(ret)); else js_pushundefined(J); }
static void Ap_concat(js_State *J) { unsigned int i, top = js_gettop(J); unsigned int n, k, len; js_newarray(J); n = 0; for (i = 0; i < top; ++i) { js_copy(J, i); if (js_isarray(J, -1)) { len = js_getlength(J, -1); for (k = 0; k < len; ++k) if (js_hasindex(J, -1, k)) js_setindex(J, -3, n++); js_pop(J, 1); } else { js_setindex(J, -2, n++); } } }
static void jsB_new_Array(js_State *J) { unsigned int i, top = js_gettop(J); js_newarray(J); if (top == 2) { if (js_isnumber(J, 1)) { js_copy(J, 1); js_setproperty(J, -2, "length"); } else { js_copy(J, 1); js_setindex(J, -2, 0); } } else { for (i = 1; i < top; ++i) { js_copy(J, i); js_setindex(J, -2, i - 1); } } }
static void Ap_reduceRight(js_State *J) { int hasinitial = js_gettop(J) >= 3; int k, len; if (!js_iscallable(J, 1)) js_typeerror(J, "callback is not a function"); len = js_getlength(J, 0); k = len - 1; if (len == 0 && !hasinitial) js_typeerror(J, "no initial value"); /* initial value of accumulator */ if (hasinitial) js_copy(J, 2); else { while (k >= 0) if (js_hasindex(J, 0, k--)) break; if (k < 0) js_typeerror(J, "no initial value"); } while (k >= 0) { if (js_hasindex(J, 0, k)) { js_copy(J, 1); js_pushundefined(J); js_rot(J, 4); /* accumulator on top */ js_rot(J, 4); /* property on top */ js_pushnumber(J, k); js_copy(J, 0); js_call(J, 4); /* calculate new accumulator */ } --k; } /* return accumulator */ }
static void S_fromCharCode(js_State *J) { unsigned int i, top = js_gettop(J); Rune c; char *s, *p; s = p = js_malloc(J, (top-1) * UTFmax + 1); if (js_try(J)) { js_free(J, s); js_throw(J); } for (i = 1; i < top; ++i) { c = js_touint16(J, i); p += runetochar(p, &c); } *p = 0; js_pushstring(J, s); js_endtry(J); js_free(J, s); }
static void jsB_Function(js_State *J) { unsigned int i, top = js_gettop(J); js_Buffer *sb = NULL; const char *body; js_Ast *parse; js_Function *fun; /* p1, p2, ..., pn */ if (top > 2) { for (i = 1; i < top - 1; ++i) { if (i > 1) js_putc(J, &sb, ','); js_puts(J, &sb, js_tostring(J, i)); } js_putc(J, &sb, ')'); } /* body */ body = js_isdefined(J, top - 1) ? js_tostring(J, top - 1) : ""; if (js_try(J)) { js_free(J, sb); jsP_freeparse(J); js_throw(J); } parse = jsP_parsefunction(J, "[string]", sb ? sb->s : NULL, body); fun = jsC_compilefunction(J, parse); js_endtry(J); js_free(J, sb); jsP_freeparse(J); js_newfunction(J, fun, J->GE); }
static void Fp_bind(js_State *J) { unsigned int i, top = js_gettop(J); unsigned int n; if (!js_iscallable(J, 0)) js_typeerror(J, "not a function"); n = js_getlength(J, 0); if (n > top - 2) n -= top - 2; else n = 0; js_newcconstructor(J, callbound, constructbound, "[bind]", n); /* Reuse target function's prototype for HasInstance check. */ js_getproperty(J, 0, "prototype"); js_defproperty(J, -2, "prototype", JS_READONLY | JS_DONTENUM | JS_DONTCONF); /* target function */ js_copy(J, 0); js_defproperty(J, -2, "__TargetFunction__", JS_READONLY | JS_DONTENUM | JS_DONTCONF); /* bound this */ js_copy(J, 1); js_defproperty(J, -2, "__BoundThis__", JS_READONLY | JS_DONTENUM | JS_DONTCONF); /* bound arguments */ js_newarray(J); for (i = 2; i < top; ++i) { js_copy(J, i); js_setindex(J, -2, i - 2); } js_defproperty(J, -2, "__BoundArguments__", JS_READONLY | JS_DONTENUM | JS_DONTCONF); }
static void jsB_String(js_State *J) { js_pushliteral(J, js_gettop(J) > 1 ? js_tostring(J, 1) : ""); }
static void jsB_new_String(js_State *J) { js_newstring(J, js_gettop(J) > 1 ? js_tostring(J, 1) : ""); }
static void Ap_toString(js_State *J) { unsigned int top = js_gettop(J); js_pop(J, top - 1); Ap_join(J); }