Wire up jsB_load, jsB_read to FileRead in mujs

This commit is contained in:
Alec Murphy
2025-06-10 10:39:34 -04:00
parent 9ad75fff5c
commit d8108823b3

View File

@@ -94,6 +94,53 @@ char *readline(const char *prompt)
#define PS1 "> "
static int eval_file(js_State *J, const char* filename)
{
char* source = (char*)os_call_ext_str_3("FileRead", (uint64_t)filename, 0, 0);
if (!source) {
js_pushundefined(J);
return 1;
}
int res = js_ploadstring(J, filename, source);
free(source);
if (res) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
js_pushundefined(J);
if (js_pcall(J, 0)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
if (js_isdefined(J, -1)) {
printf("%s\n", js_tryrepr(J, -1, "can't convert to string"));
}
js_pop(J, 1);
return 0;
}
static int eval_print(js_State *J, const char *source)
{
if (js_ploadstring(J, "[stdin]", source)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
js_pushundefined(J);
if (js_pcall(J, 0)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
if (js_isdefined(J, -1)) {
printf("%s\n", js_tryrepr(J, -1, "can't convert to string"));
}
js_pop(J, 1);
return 0;
}
static void jsB_gc(js_State *J)
{
int report = js_toboolean(J, 1);
@@ -103,14 +150,7 @@ static void jsB_gc(js_State *J)
static void jsB_load(js_State *J)
{
int i, n = js_gettop(J);
for (i = 1; i < n; ++i) {
js_loadfile(J, js_tostring(J, i));
js_pushundefined(J);
js_call(J, 0);
js_pop(J, 1);
}
js_pushundefined(J);
eval_file(J, js_tostring(J, 1));
}
static void jsB_compile(js_State *J)
@@ -164,49 +204,13 @@ static void jsB_write(js_State *J)
static void jsB_read(js_State *J)
{
const char *filename = js_tostring(J, 1);
FILE *f;
char *s;
int n, t;
f = fopen(filename, "rb");
if (!f) {
js_error(J, "cannot open file '%s': %s", filename, strerror(errno));
}
if (fseek(f, 0, SEEK_END) < 0) {
fclose(f);
js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
}
n = ftell(f);
if (n < 0) {
fclose(f);
js_error(J, "cannot tell in file '%s': %s", filename, strerror(errno));
}
if (fseek(f, 0, SEEK_SET) < 0) {
fclose(f);
js_error(J, "cannot seek in file '%s': %s", filename, strerror(errno));
}
s = malloc(n + 1);
char *s = (char*)os_call_ext_str_3("FileRead", (uint64_t)js_tostring(J, 1), 0, 0);
if (!s) {
fclose(f);
js_error(J, "out of memory");
js_pushundefined(J);
return;
}
t = fread(s, 1, n, f);
if (t != n) {
free(s);
fclose(f);
js_error(J, "cannot read data from file '%s': %s", filename, strerror(errno));
}
s[n] = 0;
js_pushstring(J, s);
free(s);
fclose(f);
}
static void jsB_readline(js_State *J)
@@ -258,49 +262,6 @@ static const char *console_js =
"var console = { log: print, debug: print, warn: print, error: print };"
;
static int eval_file(js_State *J, const char* filename)
{
char* source = (char*)os_call_ext_str_3("FileRead", (uint64_t)filename, 0, 0);
int res = js_ploadstring(J, filename, source);
free(source);
if (res) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
js_pushundefined(J);
if (js_pcall(J, 0)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
if (js_isdefined(J, -1)) {
printf("%s\n", js_tryrepr(J, -1, "can't convert to string"));
}
js_pop(J, 1);
return 0;
}
static int eval_print(js_State *J, const char *source)
{
if (js_ploadstring(J, "[stdin]", source)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
js_pushundefined(J);
if (js_pcall(J, 0)) {
fprintf(stderr, "%s\n", js_trystring(J, -1, "Error"));
js_pop(J, 1);
return 1;
}
if (js_isdefined(J, -1)) {
printf("%s\n", js_tryrepr(J, -1, "can't convert to string"));
}
js_pop(J, 1);
return 0;
}
static void usage(void)
{
fprintf(stderr, "Usage: mujs [options] [script [scriptArgs*]]\n");