1515import org .graalvm .polyglot .HostAccess ;
1616import org .graalvm .polyglot .Value ;
1717
18+ import java .util .ArrayList ;
19+ import java .util .List ;
20+
1821public class JsScript implements Script <Value > {
1922
2023 private final ScriptSecurityManager securityManager = new StandardSecurityManager ();
2124 private ScriptFunctionManager functionManager = new StandardFunctionManager ();
2225 private ScriptConstantManager constantManager = new StandardConstantManager ();
26+ private final List <String > extraScript = new ArrayList <>();
27+
28+ @ Override
29+ public ScriptConstantManager getConstantManager () {
30+ return constantManager ;
31+ }
2332
2433 @ Override
2534 public ScriptSecurityManager getSecurityManager () {
@@ -37,13 +46,13 @@ public void setFunctionManager(ScriptFunctionManager functionManager) {
3746 }
3847
3948 @ Override
40- public ScriptConstantManager getConstantManager ( ) {
41- return constantManager ;
49+ public void setConstantManager ( ScriptConstantManager constantManager ) {
50+ this . constantManager = constantManager ;
4251 }
4352
4453 @ Override
45- public void setConstantManager ( ScriptConstantManager constantManager ) {
46- this .constantManager = constantManager ;
54+ public void addExtraScript ( String script ) {
55+ this .extraScript . add ( script ) ;
4756 }
4857
4958 @ Override
@@ -84,8 +93,15 @@ public Value eval(String script) throws ScriptException {
8493 }
8594 }
8695
96+ // Building full script including extra script code
97+ StringBuilder fullScript = new StringBuilder ();
98+ for (String extra : extraScript ) {
99+ fullScript .append (extra ).append ("\n " );
100+ }
101+ fullScript .append (script );
102+
87103 try {
88- return context .eval ("js" , script );
104+ return context .eval ("js" , fullScript . toString () );
89105 } catch (Exception e ) {
90106 throw new ScriptException (e );
91107 } finally {
0 commit comments