void pdf_jsimp_addproperty(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_getter *get, pdf_jsimp_setter *set) { js_State *J = imp->J; js_getregistry(J, (const char *)type); { js_newcfunction(J, wrapgetter, 0); { js_pushnull(J); js_newuserdata(J, "getter", get); js_defproperty(J, -2, "__get", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_pushstring(J, (const char *)type); js_defproperty(J, -2, "__type", JS_READONLY | JS_DONTENUM | JS_DONTCONF); } js_newcfunction(J, wrapsetter, 0); { js_pushnull(J); js_newuserdata(J, "setter", set); js_defproperty(J, -2, "__set", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_pushstring(J, (const char *)type); js_defproperty(J, -2, "__type", JS_READONLY | JS_DONTENUM | JS_DONTCONF); } js_defaccessor(J, -3, name, JS_READONLY | JS_DONTENUM | JS_DONTCONF); } js_pop(J, 1); }
/* prototype -- constructor */ void js_newcconstructor(js_State *J, js_CFunction cfun, js_CFunction ccon, unsigned int length) { js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype); obj->u.c.function = cfun; obj->u.c.constructor = ccon; js_pushobject(J, obj); /* proto obj */ { js_pushnumber(J, length); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_rot2(J); /* obj proto */ js_copy(J, -2); /* obj proto obj */ js_defproperty(J, -2, "constructor", JS_DONTENUM); js_defproperty(J, -2, "prototype", JS_READONLY | JS_DONTENUM | JS_DONTCONF); } }
void pdf_jsimp_addmethod(pdf_jsimp *imp, pdf_jsimp_type *type, char *name, pdf_jsimp_method *meth) { js_State *J = imp->J; js_getregistry(J, (const char *)type); { js_newcfunction(J, wrapmethod, 0); { js_pushnull(J); js_newuserdata(J, "method", meth); js_defproperty(J, -2, "__call", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_pushstring(J, (const char *)type); js_defproperty(J, -2, "__type", JS_READONLY | JS_DONTENUM | JS_DONTCONF); } js_defproperty(J, -2, name, JS_READONLY | JS_DONTENUM | JS_DONTCONF); } js_pop(J, 1); }
void js_newfunction(js_State *J, js_Function *fun, js_Environment *scope) { js_Object *obj = jsV_newobject(J, JS_CFUNCTION, J->Function_prototype); obj->u.f.function = fun; obj->u.f.scope = scope; js_pushobject(J, obj); { js_pushnumber(J, fun->numparams); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_newobject(J); { js_copy(J, -2); js_defproperty(J, -2, "constructor", JS_DONTENUM); } js_defproperty(J, -2, "prototype", JS_DONTCONF); } }
static void jsR_callfunction(js_State *J, unsigned int n, js_Function *F, js_Environment *scope) { js_Value v; unsigned int i; scope = jsR_newenvironment(J, jsV_newobject(J, JS_COBJECT, NULL), scope); jsR_savescope(J, scope); if (F->arguments) { js_newobject(J); if (!J->strict) { js_currentfunction(J); js_defproperty(J, -2, "callee", JS_DONTENUM); } js_pushnumber(J, n); js_defproperty(J, -2, "length", JS_DONTENUM); for (i = 0; i < n; ++i) { js_copy(J, i + 1); js_setindex(J, -2, i); } js_initvar(J, "arguments", -1); js_pop(J, 1); } for (i = 0; i < F->numparams; ++i) { if (i < n) js_initvar(J, F->vartab[i], i + 1); else { js_pushundefined(J); js_initvar(J, F->vartab[i], -1); js_pop(J, 1); } } js_pop(J, n); jsR_run(J, F); v = *stackidx(J, -1); TOP = --BOT; /* clear stack */ js_pushvalue(J, v); jsR_restorescope(J); }
void js_newcfunction(js_State *J, js_CFunction cfun, const char *name, unsigned int length) { js_Object *obj = jsV_newobject(J, JS_CCFUNCTION, J->Function_prototype); obj->u.c.name = name; obj->u.c.function = cfun; obj->u.c.constructor = NULL; obj->u.c.length = length; js_pushobject(J, obj); { js_pushnumber(J, length); js_defproperty(J, -2, "length", JS_READONLY | JS_DONTENUM | JS_DONTCONF); js_newobject(J); { js_copy(J, -2); js_defproperty(J, -2, "constructor", JS_DONTENUM); } js_defproperty(J, -2, "prototype", JS_DONTCONF); } }
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); }
void jsB_props(js_State *J, const char *name, const char *string) { js_pushliteral(J, string); js_defproperty(J, -2, name, JS_DONTENUM); }
void jsB_propn(js_State *J, const char *name, double number) { js_pushnumber(J, number); js_defproperty(J, -2, name, JS_READONLY | JS_DONTENUM | JS_DONTCONF); }
void jsB_propf(js_State *J, const char *name, js_CFunction cfun, int n) { js_newcfunction(J, cfun, n); js_defproperty(J, -2, name, JS_DONTENUM); }