### Question:
NashornExecutor implements ScriptExecutor { @Override public ExecutionResult executeScript(String source, RequestContainer request, ResponseContainer response, RequestExecutionContext context) { ByteArrayOutputStream logStream = new ByteArrayOutputStream(); initGlobalBindings(); Bindings bindings = engine.createBindings(); engine.setContext(new SimpleScriptContext()); engine.getContext().setBindings(bindings, ScriptContext.GLOBAL_SCOPE); engine.getContext().setBindings(globalBindings, ScriptContext.ENGINE_SCOPE); engine.getContext().setErrorWriter(new OutputStreamWriter(logStream)); engine.getContext().setWriter(new OutputStreamWriter(logStream)); var facade = new MilkmanNashornFacade(request, response, context, toaster); bindings.put("milkman", facade); bindings.put("mm", facade); try { Object eval = engine.eval(source); return new ExecutionResult(logStream.toString(), Optional.ofNullable(eval), Optional.empty()); } catch (Exception e) { log.error("failed to execute script", e); return new ExecutionResult(logStream.toString(), Optional.empty(), Optional.of(e)); } }  @Override ExecutionResult executeScript(String source, RequestContainer request, ResponseContainer response, RequestExecutionContext context); void initGlobalBindings();  }### Answer:
@Test() void shouldUseChai(){ ScriptOptionsProvider.options().setPreloadScripts(List.of("https: Toaster mock = mock(Toaster.class); NashornExecutor executor = new NashornExecutor(mock); executor.executeScript("chai.should(); ''.should.be.a('string')", null, null, new RequestExecutionContext(Optional.empty(), List.of())); verify(mock, never()).showToast(anyString()); }
@Test() void shouldCarryOverChangesInGlobal() throws URISyntaxException { ScriptOptionsProvider.options().setPreloadScripts(List.of(getClass().getResource("/object-prototype-test.js").toURI().toString())); Toaster mock = mock(Toaster.class); NashornExecutor executor = new NashornExecutor(mock); String out = executor.executeScript("var x = {}; x.test('hello');", null, null, new RequestExecutionContext(Optional.empty(), List.of())).getConsoleOutput(); verify(mock, never()).showToast(anyString()); assertThat(out.trim()).isEqualTo("hello"); }
@Test @Disabled("no solution yet") void shouldUseGlobalScope(){ NashornExecutor executor = new NashornExecutor(mock(Toaster.class)); String out1 = executor.executeScript("console.log(testVar); var testVar = 'testValue';", null, null, new RequestExecutionContext(Optional.empty(), List.of())).getConsoleOutput(); String out2 = executor.executeScript("console.log(testVar); var testVar = 'testValue';", null, null, new RequestExecutionContext(Optional.empty(), List.of())).getConsoleOutput(); assertThat(out1.trim()).isEqualTo("undefined"); assertThat(out2.trim()).isEqualTo("undefined"); }