int js_ploadstring(js_State *J, const char *filename, const char *source) { if (js_try(J)) return 1; js_loadstring(J, filename, source); js_endtry(J); return 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); }
void js_loadfile(js_State *J, const char *filename) { FILE *f; char *s; int n, t; f = fopen(filename, "rb"); if (!f) { js_error(J, "cannot open file: '%s'", filename); } if (fseek(f, 0, SEEK_END) < 0) { fclose(f); js_error(J, "cannot seek in file: '%s'", filename); } n = ftell(f); if (n < 0) { fclose(f); js_error(J, "cannot tell in file: '%s'", filename); } if (fseek(f, 0, SEEK_SET) < 0) { fclose(f); js_error(J, "cannot seek in file: '%s'", filename); } s = js_malloc(J, n + 1); /* add space for string terminator */ if (!s) { fclose(f); js_error(J, "cannot allocate storage for file contents: '%s'", filename); } t = fread(s, 1, n, f); if (t != n) { js_free(J, s); fclose(f); js_error(J, "cannot read data from file: '%s'", filename); } s[n] = 0; /* zero-terminate string containing file data */ if (js_try(J)) { js_free(J, s); fclose(f); js_throw(J); } js_loadstring(J, filename, s); js_free(J, s); fclose(f); js_endtry(J); }
int js_dostring(js_State *J, const char *source) { if (js_try(J)) { fprintf(stderr, "%s\n", js_tostring(J, -1)); js_pop(J, 1); return 1; } js_loadstring(J, "[string]", source); js_pushglobal(J); js_call(J, 0); js_pop(J, 1); js_endtry(J); return 0; }
int js_dostring(js_State *J, const char *source, int report) { if (js_try(J)) { fprintf(stderr, "libjs: %s\n", js_tostring(J, -1)); js_pop(J, 1); return 1; } js_loadstring(J, "(string)", source); js_pushglobal(J); js_call(J, 0); if (report) printf("%s\n", js_tostring(J, -1)); js_pop(J, 1); js_endtry(J); return 0; }
static void jsB_compile(js_State *J) { const char *source = js_tostring(J, 1); const char *filename = js_isdefined(J, 2) ? js_tostring(J, 2) : "[string]"; js_loadstring(J, filename, source); }