88import com .oracle .truffle .js .runtime .objects .JSAttributes ;
99import com .oracle .truffle .js .runtime .objects .JSObject ;
1010import com .oracle .truffle .js .runtime .objects .JSObjectUtil ;
11- import com .oracle .truffle .js .scriptengine .GraalJSScriptEngine ;
1211import io .github .thebusybiscuit .slimefun4 .api .items .SlimefunItem ;
1312import io .github .thebusybiscuit .slimefun4 .api .player .PlayerProfile ;
1413import io .github .thebusybiscuit .slimefun4 .implementation .Slimefun ;
1514import io .github .thebusybiscuit .slimefun4 .implementation .SlimefunItems ;
1615import io .github .thebusybiscuit .slimefun4 .utils .SlimefunUtils ;
1716import java .io .File ;
17+ import java .io .IOException ;
1818import java .util .HashSet ;
1919import java .util .Objects ;
2020import java .util .Set ;
21- import javax .script .ScriptException ;
2221import me .mrCookieSlime .Slimefun .api .inventory .BlockMenu ;
23- import org .graalvm .polyglot .Context ;
24- import org .graalvm .polyglot .Engine ;
25- import org .graalvm .polyglot .PolyglotAccess ;
22+ import org .graalvm .polyglot .*;
2623import org .graalvm .polyglot .io .IOAccess ;
2724import org .jetbrains .annotations .NotNull ;
2825import org .jetbrains .annotations .Nullable ;
@@ -37,8 +34,7 @@ public class JavaScriptEval extends ScriptEval {
3734 RykenSlimefunCustomizer .INSTANCE .getDataFolder ().getParentFile ();
3835 private final Set <String > failed_functions = new HashSet <>();
3936
40- private final ProjectAddon addon ;
41- private GraalJSScriptEngine jsEngine ;
37+ private Context jsEngine ;
4238
4339 public JavaScriptEval (@ NotNull File js , ProjectAddon addon ) {
4440 super (js , addon );
@@ -54,7 +50,7 @@ public JavaScriptEval(@NotNull File js, ProjectAddon addon) {
5450 }
5551
5652 private void advancedSetup () {
57- JSRealm realm = JavaScriptLanguage .getJSRealm (jsEngine . getPolyglotContext () );
53+ JSRealm realm = JavaScriptLanguage .getJSRealm (jsEngine );
5854 TruffleLanguage .Env env = realm .getEnv ();
5955 addThing ("SlimefunItems" , env .asHostSymbol (SlimefunItems .class ));
6056 addThing ("SlimefunItem" , env .asHostSymbol (SlimefunItem .class ));
@@ -75,19 +71,22 @@ private void advancedSetup() {
7571 JSObjectUtil .putToStringTag (java , JSRealm .JAVA_CLASS_NAME );
7672
7773 JSObjectUtil .putDataProperty (realm .getGlobalObject (), "Java" , java , JSAttributes .getDefaultNotEnumerable ());
74+
75+ jsEngine .enter ();
7876 }
7977
8078 @ Override
8179 public void close () {
8280 try {
81+ jsEngine .leave ();
8382 jsEngine .close ();
8483 } catch (IllegalStateException ignored ) {
8584 }
8685 }
8786
8887 @ Override
8988 public void addThing (String name , Object value ) {
90- jsEngine .put (name , value );
89+ jsEngine .getBindings ( "js" ). putMember (name , value );
9190 }
9291
9392 @ Override
@@ -99,8 +98,8 @@ protected final void contextInit() {
9998 super .contextInit ();
10099 if (jsEngine != null ) {
101100 try {
102- jsEngine .eval (getFileContext ());
103- } catch (ScriptException e ) {
101+ jsEngine .eval (Source . newBuilder ( "js" , getFileContext (), "JavaScript" ). build ());
102+ } catch (IOException e ) {
104103 e .printStackTrace ();
105104 }
106105 }
@@ -118,20 +117,21 @@ public Object evalFunction(String funName, Object... args) {
118117 return null ;
119118 }
120119
120+ Value member = jsEngine .getBindings ("js" ).getMember (funName );
121+ if (member == null ) {
122+ failed_functions .add (funName );
123+ return null ;
124+ }
125+
121126 try {
122- Object result = jsEngine . invokeFunction ( funName , args );
127+ Object result = member . execute ( args );
123128 ExceptionHandler .debugLog ("Run function " + funName + " in file " + getFile ().getName () + " of addon " + getAddon ().getAddonName ());
124129 return result ;
125130 } catch (IllegalStateException e ) {
126131 String message = e .getMessage ();
127132 if (!message .contains ("Multi threaded access" )) {
128133 ExceptionHandler .handleError ("An error occcured while executing script file " + getFile ().getName () + "of addon" + addon .getAddonName (), e );
129134 }
130- } catch (ScriptException e ) {
131- ExceptionHandler .handleError ("An error occurred while executing script file " + getFile ().getName () + "of addonn" + addon .getAddonName (), e );
132- } catch (NoSuchMethodException ignored ) {
133- // won't log it, because listeners always send a lot of functions
134- failed_functions .add (funName );
135135 } catch (Throwable e ) {
136136 ExceptionHandler .handleError ("An error occcured while executing script file " + getFile ().getName () + "of addon" + addon .getAddonName (), e );
137137 }
@@ -140,11 +140,7 @@ public Object evalFunction(String funName, Object... args) {
140140 }
141141
142142 private void reSetup () {
143- jsEngine = GraalJSScriptEngine .create (
144- Engine .newBuilder ("js" )
145- .allowExperimentalOptions (true )
146- .build (),
147- Context .newBuilder ("js" )
143+ jsEngine = Context .newBuilder ("js" )
148144 .hostClassLoader (RykenSlimefunCustomizer .class .getClassLoader ())
149145 .hostClassLoader (ClassLoader .getSystemClassLoader ())
150146 .allowAllAccess (true )
@@ -157,7 +153,11 @@ private void reSetup() {
157153 .allowIO (IOAccess .ALL )
158154 .allowHostClassLookup (s -> true )
159155 .allowHostClassLoading (true )
160- .hostClassLoader (ClassLoader .getSystemClassLoader ()));
156+ .engine (Engine .newBuilder ("js" )
157+ .allowExperimentalOptions (true )
158+ .build ())
159+ .currentWorkingDirectory (getAddon ().getScriptsFolder ().toPath ().toAbsolutePath ())
160+ .build ();
161161
162162 advancedSetup ();
163163 }
0 commit comments