diff --git a/docs/examples.html b/docs/examples.html index 8cd98e2..c2322a5 100644 --- a/docs/examples.html +++ b/docs/examples.html @@ -19,7 +19,7 @@ int main(int argc, char **argv) char line[256]; js_State *J = js_newstate(NULL, NULL, JS_STRICT); while (fgets(line, sizeof line, stdin)) - js_dostring(J, line, 1); + js_dostring(J, line); js_freestate(J); } @@ -44,7 +44,7 @@ int main(int argc, char **argv) js_newcfunction(J, hello, 1); js_setglobal(J, "hello"); - js_dostring(J, "hello('world');", 0); + js_dostring(J, "hello('world');"); js_freestate(J); } diff --git a/docs/reference.html b/docs/reference.html index 1a0f02e..6fda048 100644 --- a/docs/reference.html +++ b/docs/reference.html @@ -244,13 +244,12 @@ In case of failure, return 1 with the error object on the stack. There are two convenience functions for loading and executing code.
-int js_dostring(js_State *J, const char *source, int report);
+int js_dostring(js_State *J, const char *source);
 

Compile and execute the script in the zero-terminated string in source argument. If any errors occur, print the error message to stderr and return 1. -If the report argument is non-zero, print the result of executing the script to stdout. Return 0 on success.

diff --git a/jsstate.c b/jsstate.c
index 2d9b6b9..d1a1406 100644
--- a/jsstate.c
+++ b/jsstate.c
@@ -125,7 +125,7 @@ void js_loadfile(js_State *J, const char *filename)
 	js_endtry(J);
 }
 
-int js_dostring(js_State *J, const char *source, int report)
+int js_dostring(js_State *J, const char *source)
 {
 	if (js_try(J)) {
 		fprintf(stderr, "%s\n", js_tostring(J, -1));
@@ -135,9 +135,6 @@ int js_dostring(js_State *J, const char *source, int report)
 	js_loadstring(J, "[string]", source);
 	js_pushglobal(J);
 	js_call(J, 0);
-	if (report)
-		if (js_isdefined(J, -1))
-			printf("%s\n", js_tostring(J, -1));
 	js_pop(J, 1);
 	js_endtry(J);
 	return 0;
diff --git a/main.c b/main.c
index 5c2abe6..41aff84 100644
--- a/main.c
+++ b/main.c
@@ -119,6 +119,23 @@ static const char *require_js =
 	"require.cache = Object.create(null);\n"
 ;
 
+int eval_print(js_State *J, const char *source)
+{
+	if (js_ploadstring(J, "[string]", source)) {
+		fprintf(stderr, "%s\n", js_tostring(J, -1));
+		return 1;
+	}
+	js_pushglobal(J);
+	if (js_pcall(J, 0)) {
+		fprintf(stderr, "%s\n", js_tostring(J, -1));
+		return 1;
+	}
+	if (js_isdefined(J, -1))
+		printf("%s\n", js_tostring(J, -1));
+	js_pop(J, 1);
+	return 0;
+}
+
 int
 main(int argc, char **argv)
 {
@@ -149,7 +166,7 @@ main(int argc, char **argv)
 	js_newcfunction(J, jsB_quit, "quit", 1);
 	js_setglobal(J, "quit");
 
-	js_dostring(J, require_js, 0);
+	js_dostring(J, require_js);
 
 	if (argc > 1) {
 		for (i = 1; i < argc; ++i) {
@@ -160,7 +177,7 @@ main(int argc, char **argv)
 	} else {
 		fputs(PS1, stdout);
 		while (fgets(line, sizeof line, stdin)) {
-			js_dostring(J, line, 1);
+			eval_print(J, line);
 			fputs(PS1, stdout);
 		}
 		putchar('\n');
diff --git a/mujs.h b/mujs.h
index dd3da50..d005c8f 100644
--- a/mujs.h
+++ b/mujs.h
@@ -39,7 +39,7 @@ js_Panic js_atpanic(js_State *J, js_Panic panic);
 void js_freestate(js_State *J);
 void js_gc(js_State *J, int report);
 
-int js_dostring(js_State *J, const char *source, int report);
+int js_dostring(js_State *J, const char *source);
 int js_dofile(js_State *J, const char *filename);
 int js_ploadstring(js_State *J, const char *filename, const char *source);
 int js_ploadfile(js_State *J, const char *filename);