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); }
int main(int argc, char **argv) { char line[256]; js_State *J; int i; J = js_newstate(NULL, NULL, JS_STRICT); js_newcfunction(J, jsB_gc, "gc", 0); js_setglobal(J, "gc"); js_newcfunction(J, jsB_load, "load", 1); js_setglobal(J, "load"); js_newcfunction(J, jsB_print, "print", 0); js_setglobal(J, "print"); js_newcfunction(J, jsB_write, "write", 0); js_setglobal(J, "write"); js_newcfunction(J, jsB_read, "read", 1); js_setglobal(J, "read"); js_newcfunction(J, jsB_readline, "readline", 0); js_setglobal(J, "readline"); js_newcfunction(J, jsB_quit, "quit", 1); js_setglobal(J, "quit"); js_dostring(J, require_js); if (argc > 1) { for (i = 1; i < argc; ++i) { if (js_dofile(J, argv[i])) return 1; js_gc(J, 0); } } else { fputs(PS1, stdout); while (fgets(line, sizeof line, stdin)) { eval_print(J, line); fputs(PS1, stdout); } putchar('\n'); js_gc(J, 1); } js_freestate(J); return 0; }
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); }
int main(int argc, char **argv) { char *input; js_State *J; int status = 0; int strict = 0; int interactive = 0; int i, c; while ((c = xgetopt(argc, argv, "is")) != -1) { switch (c) { default: usage(); break; case 'i': interactive = 1; break; case 's': strict = 1; break; } } J = js_newstate(NULL, NULL, strict ? JS_STRICT : 0); js_newcfunction(J, jsB_gc, "gc", 0); js_setglobal(J, "gc"); js_newcfunction(J, jsB_load, "load", 1); js_setglobal(J, "load"); js_newcfunction(J, jsB_compile, "compile", 2); js_setglobal(J, "compile"); js_newcfunction(J, jsB_print, "print", 0); js_setglobal(J, "print"); js_newcfunction(J, jsB_write, "write", 0); js_setglobal(J, "write"); js_newcfunction(J, jsB_read, "read", 1); js_setglobal(J, "read"); js_newcfunction(J, jsB_readline, "readline", 0); js_setglobal(J, "readline"); js_newcfunction(J, jsB_repr, "repr", 0); js_setglobal(J, "repr"); js_newcfunction(J, jsB_quit, "quit", 1); js_setglobal(J, "quit"); js_dostring(J, require_js); js_dostring(J, stacktrace_js); if (xoptind == argc) { interactive = 1; } else { c = xoptind++; js_newarray(J); i = 0; while (xoptind < argc) { js_pushstring(J, argv[xoptind++]); js_setindex(J, -2, i++); } js_setglobal(J, "scriptArgs"); if (js_dofile(J, argv[c])) status = 1; } if (interactive) { if (isatty(0)) { using_history(); rl_bind_key('\t', rl_insert); input = readline(PS1); while (input) { eval_print(J, input); if (*input) add_history(input); free(input); input = readline(PS1); } putchar('\n'); } else { input = read_stdin(); if (!input || !js_dostring(J, input)) status = 1; free(input); } } js_gc(J, 0); js_freestate(J); return status; }
static void jsB_globalf(js_State *J, const char *name, js_CFunction cfun, int n) { js_newcfunction(J, cfun, n); js_defglobal(J, name, JS_DONTENUM); }
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); }