From c3b47aca928c693687eefcaa659ee801fc7839bd Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on Windows 11)" <rob@synchro.net>
Date: Fri, 5 Apr 2024 14:01:18 -0700
Subject: [PATCH] Save/restore js.scope property value in sbbs_t::js_execfile()

This solves the problem of exit() values (e.g. non-zero return codes) not
getting propagated to callers when nest-called (e.g. via bbs.exec()).

I think it was kk4qbn that pointed this out via IRC: an exit(1) call from
prextrn.js did not stop the external program from running (as it should, for
any non-zero exit code). This only happened when the prextrn.js called another
JS script (e.g. via bbs.exec() or as was the case here, indirectly via "EXEC"
@-code in the YesNoBar text.dat string (which executed yesnobar.js). This
nested JS script invocation via sbbs_t::js_execfile() would clobber the stored
js.scope property value (where the "exit_code" property is written).

Script invoked in their own context (e.g. via js.exec()) wouldn't have this
issue in the first place.
---
 src/sbbs3/exec.cpp | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/src/sbbs3/exec.cpp b/src/sbbs3/exec.cpp
index 1f28e7802d..e1b5c1297d 100644
--- a/src/sbbs3/exec.cpp
+++ b/src/sbbs3/exec.cpp
@@ -544,6 +544,7 @@ int sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* scop
 	jsval		old_js_exec_path = JSVAL_VOID;
 	jsval		old_js_exec_file = JSVAL_VOID;
 	jsval		old_js_exec_dir = JSVAL_VOID;
+	jsval		old_js_scope = JSVAL_VOID;
 	jsval		val;
 	jsval		rval;
 	int32		result=0;
@@ -676,6 +677,8 @@ int sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* scop
 			JS_AddValueRoot(js_cx, &old_js_exec_file);
 		if(JS_GetProperty(js_cx, js, "exec_dir", &old_js_exec_dir))
 			JS_AddValueRoot(js_cx, &old_js_exec_dir);
+		if(JS_GetProperty(js_cx, js, "scope", &old_js_scope))
+			JS_AddValueRoot(js_cx, &old_js_scope);
 	}
 
 	js_PrepareToExecute(js_cx, js_glob, path, startup_dir, js_scope);
@@ -728,6 +731,9 @@ int sbbs_t::js_execfile(const char *cmd, const char* startup_dir, JSObject* scop
 		JS_DefineProperty(js_cx, js, "exec_dir", old_js_exec_dir
 			,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY);
 		JS_RemoveValueRoot(js_cx, &old_js_exec_dir);
+		JS_DefineProperty(js_cx, js, "scope", old_js_scope
+			,NULL,NULL,JSPROP_ENUMERATE|JSPROP_READONLY);
+		JS_RemoveValueRoot(js_cx, &old_js_scope);
 	}
 
 	JS_GC(js_cx);
-- 
GitLab