ScriptsLab
WikiDownloadsSourcesSupport
ScriptsLab
DocumentationDownloadsGitHubDiscord

© 2026 ScriptsLab

Back to src/main/java/com/scriptslab/core/script
J

GraalScriptEngine.java

Java · 553 lines · 20.1 KB

src/main/java/com/scriptslab/core/script/GraalScriptEngine.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
package com.scriptslab.core.script;

import com.scriptslab.api.script.ScriptEngine;
import org.graalvm.polyglot.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Stream;

/**
 * GraalVM-based JavaScript engine implementation.
 * Thread-safe with sandboxing and reload resilience.
 */
public final class GraalScriptEngine implements ScriptEngine {
    
    private static final Logger STATIC_LOGGER = Logger.getLogger("ScriptEngine.Static");
    private static volatile Engine sharedEngine;
    private static final AtomicBoolean engineLoaded = new AtomicBoolean(false);
    private static final Object ENGINE_LOCK = new Object();
    
    private final Logger logger;
    private final Path scriptsDirectory;
    private final Object scriptAPI;
    private final Map<String, LoadedScriptImpl> scripts;
    private final ExecutorService executor;
    private final AtomicBoolean initialized;
    private final AtomicBoolean wasInitialized;
    
    private Context jsContext;
    
    public GraalScriptEngine(Path scriptsDirectory, Object scriptAPI) {
        this.logger = Logger.getLogger("ScriptEngine");
        this.scriptsDirectory = scriptsDirectory;
        this.scriptAPI = scriptAPI;
        this.scripts = new ConcurrentHashMap<>();
        this.executor = Executors.newFixedThreadPool(4);
        this.initialized = new AtomicBoolean(false);
        this.wasInitialized = new AtomicBoolean(false);
    }
    
    @Override
    public CompletableFuture<Void> initialize() {
        return CompletableFuture.runAsync(() -> initializeCore(), executor);
    }
    
    public void initializeSync() {
        initializeCore();
    }
    
    private void initializeCore() {
            if (initialized.get()) {
                logger.warning("Script engine already initialized");
                return;
            }
            
            if (wasInitialized.get()) {
                logger.warning("Script engine was previously loaded - full reload required to reset");
                logger.info("Attempting to reset GraalVM state...");
                resetGraalVMState();
            }
            
            try {
                logger.info("Initializing GraalVM JavaScript engine...");
                
                Context context = null;
                Engine engine = null;
                
                synchronized (ENGINE_LOCK) {
                    if (sharedEngine == null || !engineLoaded.get()) {
                        logger.info("Creating new GraalVM engine...");
                        
                        engine = Engine.newBuilder()
                                .option("js.ecmascript-version", "2022")
                                .option("engine.WarnInterpreterOnly", "false") // Disable interpreter warning at engine level
                                .build();
                        
                        sharedEngine = engine;
                        engineLoaded.set(true);
                        
                        STATIC_LOGGER.info("Created new shared GraalVM engine");
                    } else {
                        engine = sharedEngine;
                        logger.info("Reusing existing GraalVM engine");
                    }
                }
                
                // Read sandbox setting from config
                boolean sandboxEnabled = false;
                try {
                    // scriptAPI is ScriptAPIImpl which holds the plugin reference
                    if (scriptAPI instanceof com.scriptslab.core.script.ScriptAPIImpl apiImpl) {
                        sandboxEnabled = apiImpl.getPlugin().getConfig()
                                .getBoolean("security.sandbox-enabled", false);
                    }
                } catch (Exception ignored) {}
                
                Context.Builder contextBuilder = Context.newBuilder("js")
                        .engine(engine)
                        .allowExperimentalOptions(true);
                
                if (sandboxEnabled) {
                    // Restricted mode - safe for untrusted scripts
                    contextBuilder
                            .allowHostAccess(org.graalvm.polyglot.HostAccess.EXPLICIT)
                            .allowHostClassLookup(className -> {
                                // Allow only safe Bukkit/Paper classes
                                return className.startsWith("org.bukkit.") ||
                                       className.startsWith("net.kyori.") ||
                                       className.startsWith("java.util.") ||
                                       className.startsWith("java.lang.String") ||
                                       className.startsWith("java.lang.Integer") ||
                                       className.startsWith("java.lang.Double") ||
                                       className.startsWith("java.lang.Boolean");
                            })
                            .allowIO(org.graalvm.polyglot.io.IOAccess.NONE); // No file access
                    
                    logger.info("Script engine initialized in SANDBOX mode (restricted)");
} else {
                    // Unrestricted mode - full access (use with caution!)
                    contextBuilder
                            .allowAllAccess(true)
                            .allowHostAccess(org.graalvm.polyglot.HostAccess.ALL)
                            .allowHostClassLookup(className -> true)
                            .allowIO(org.graalvm.polyglot.io.IOAccess.ALL)
                            .allowCreateThread(true)
                            .allowCreateProcess(true)
                            .allowEnvironmentAccess(org.graalvm.polyglot.EnvironmentAccess.INHERIT)
                            .allowPolyglotAccess(org.graalvm.polyglot.PolyglotAccess.ALL);

                    logger.warning("Script engine initialized in UNRESTRICTED mode - scripts have full access!");
                }
                
                context = contextBuilder.build();
                
                jsContext = context;
                
                ConsoleAPI consoleAPI = new ConsoleAPI(logger);
                
                // Expose main API objects
                jsContext.getBindings("js").putMember("API", scriptAPI);
                jsContext.getBindings("js").putMember("plugin", scriptAPI);
                
                // Expose Console/Logger
                jsContext.getBindings("js").putMember("console", consoleAPI);
                jsContext.getBindings("js").putMember("Console", consoleAPI);
                jsContext.getBindings("js").putMember("Logger", consoleAPI);
                
                // Expose individual API components as global objects
                jsContext.getBindings("js").putMember("Commands", scriptAPI);
                jsContext.getBindings("js").putMember("Events", scriptAPI);
                jsContext.getBindings("js").putMember("Players", scriptAPI);
                jsContext.getBindings("js").putMember("Server", scriptAPI);
                jsContext.getBindings("js").putMember("World", scriptAPI);
                jsContext.getBindings("js").putMember("Items", scriptAPI);

                // Expose subsystem APIs
                if (scriptAPI instanceof com.scriptslab.core.script.ScriptAPIImpl apiImpl) {
                    jsContext.getBindings("js").putMember("Scheduler", apiImpl.getScheduler());
                    jsContext.getBindings("js").putMember("Storage", apiImpl.getStorage());
                    // GUI is exposed after initGUI() is called
                    apiImpl.initGUI();
                    jsContext.getBindings("js").putMember("GUI", apiImpl.getGUI());
                } else {
                    jsContext.getBindings("js").putMember("Scheduler", scriptAPI);
                    jsContext.getBindings("js").putMember("Storage", scriptAPI);
                }
                
                scriptsDirectory.toFile().mkdirs();
                
                initialized.set(true);
                wasInitialized.set(true);
                logger.info("Script engine initialized successfully");
                
            } catch (Exception e) {
                logger.log(Level.SEVERE, "Failed to initialize script engine", e);
                throw new RuntimeException(e);
            }
    }
    
    private void resetGraalVMState() {
        logger.info("Resetting GraalVM static state...");
        
        synchronized (ENGINE_LOCK) {
            try {
                if (jsContext != null) {
                    jsContext.close();
                    jsContext = null;
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Error closing context", e);
            }
            
            try {
                if (sharedEngine != null) {
                    logger.info("Closing old shared engine...");
                    try {
                        sharedEngine.close();
                    } catch (Exception ignored) {}
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Error closing shared engine", e);
            }
            
            sharedEngine = null;
            engineLoaded.set(false);
            initialized.set(false);
            
            logger.info("GraalVM state reset complete");
        }
    }
    
    @Override
    public CompletableFuture<Void> shutdown() {
        return CompletableFuture.runAsync(() -> {
            if (!initialized.get()) {
                return;
            }
            
            logger.info("Shutting down script engine...");
            
            scripts.clear();
            
            try {
                if (jsContext != null) {
                    jsContext.close();
                    jsContext = null;
                }
            } catch (Exception e) {
                logger.log(Level.WARNING, "Error closing context", e);
            }
            
            initialized.set(false);
            
            logger.info("Script engine shut down (shared engine preserved for reload)");
        });
    }
    
    @Override
    public CompletableFuture<LoadedScript> loadScript(Path scriptPath) {
        return CompletableFuture.supplyAsync(() -> {
            ensureInitialized();
            
            try {
                String scriptId = scriptPath.getFileName().toString()
                        .replace(".js", "");
                
                String code = Files.readString(scriptPath);
                Source source = Source.newBuilder("js", code, scriptId).build();
                
                Value result = jsContext.eval(source);
                
                LoadedScriptImpl script = new LoadedScriptImpl(
                        scriptId, scriptPath, System.currentTimeMillis());
                
                scripts.put(scriptId, script);
                script.incrementExecutionCount();
                
                logger.info("Loaded script: " + scriptId);
                return script;
                
            } catch (IOException e) {
                logger.log(Level.SEVERE, "Failed to read script: " + scriptPath, e);
                throw new RuntimeException(e);
            } catch (PolyglotException e) {
                logger.log(Level.SEVERE, "Script error in " + scriptPath, e);
                
                LoadedScriptImpl script = new LoadedScriptImpl(
                        scriptPath.getFileName().toString().replace(".js", ""),
                        scriptPath,
                        System.currentTimeMillis());
                script.setError(e);
                scripts.put(script.getId(), script);
                
                return script;
            }
        }, executor);
    }
    
    @Override
    public CompletableFuture<Integer> loadScriptsFromDirectory(Path directory) {
        return CompletableFuture.supplyAsync(() -> {
            ensureInitialized();
            
            if (!Files.exists(directory)) {
                logger.warning("Scripts directory does not exist: " + directory);
                return 0;
            }
            
            try (Stream<Path> paths = Files.walk(directory)) {
                List<Path> scriptFiles = paths
                        .filter(Files::isRegularFile)
                        .filter(p -> p.toString().endsWith(".js"))
                        .toList();
                
                StringBuilder allCode = new StringBuilder();
                
                for (Path scriptFile : scriptFiles) {
                    try {
                        String code = Files.readString(scriptFile);
                        allCode.append(code).append("\n");
                    } catch (Exception e) {
                        logger.log(Level.SEVERE, "Failed to read script: " + scriptFile, e);
                    }
                }
                
                if (allCode.length() > 0) {
                    Source source = Source.newBuilder("js", allCode.toString(), "all_scripts").build();
                    jsContext.eval(source);
                    logger.info("Loaded " + scriptFiles.size() + " scripts from " + directory);
                    return scriptFiles.size();
                }
                
                return 0;
                
            } catch (IOException e) {
                logger.log(Level.SEVERE, "Failed to scan scripts directory", e);
                return 0;
            }
        }, executor);
    }
    
    @Override
    public CompletableFuture<Object> execute(String code) {
        return CompletableFuture.supplyAsync(() -> {
            ensureInitialized();
            
            // Синхронизируем доступ к Context для многопоточности
            synchronized (jsContext) {
                try {
                    Value result = jsContext.eval("js", code);
                    return result.isNull() ? null : result.as(Object.class);
                } catch (PolyglotException e) {
                    logger.log(Level.WARNING, "Script execution error", e);
                    throw new RuntimeException(e);
                }
            }
        }, executor);
    }
    
    @Override
    public CompletableFuture<Object> executeScript(String scriptId) {
        return CompletableFuture.supplyAsync(() -> {
            LoadedScriptImpl script = scripts.get(scriptId);
            if (script == null) {
                throw new IllegalArgumentException("Script not found: " + scriptId);
            }
            
            try {
                String code = Files.readString(script.getPath());
                Value result = jsContext.eval("js", code);
                
                script.incrementExecutionCount();
                script.updateLastExecutionTime();
                
                return result.isNull() ? null : result.as(Object.class);
                
            } catch (Exception e) {
                script.setError(e);
                logger.log(Level.SEVERE, "Failed to execute script: " + scriptId, e);
                throw new RuntimeException(e);
            }
        }, executor);
    }
    
    @Override
    public CompletableFuture<Void> reloadScript(String scriptId) {
        return CompletableFuture.runAsync(() -> {
            LoadedScriptImpl script = scripts.get(scriptId);
            if (script == null) {
                throw new IllegalArgumentException("Script not found: " + scriptId);
            }
            
            loadScript(script.getPath()).join();
            logger.info("Reloaded script: " + scriptId);
        }, executor);
    }
    
    @Override
    public CompletableFuture<Integer> reloadAllScripts() {
        return CompletableFuture.supplyAsync(() -> {
            List<Path> scriptPaths = scripts.values().stream()
                    .map(LoadedScriptImpl::getPath)
                    .toList();
            
            scripts.clear();
            
            int reloaded = 0;
            for (Path path : scriptPaths) {
                try {
                    loadScript(path).join();
                    reloaded++;
                } catch (Exception e) {
                    logger.log(Level.SEVERE, "Failed to reload script: " + path, e);
                }
            }
            
            logger.info("Reloaded " + reloaded + " scripts");
            return reloaded;
        }, executor);
    }
    
    @Override
    public CompletableFuture<Void> unloadScript(String scriptId) {
        return CompletableFuture.runAsync(() -> {
            scripts.remove(scriptId);
            logger.info("Unloaded script: " + scriptId);
        }, executor);
    }
    
    @Override
    public Optional<LoadedScript> getScript(String scriptId) {
        return Optional.ofNullable(scripts.get(scriptId));
    }
    
    @Override
    public Collection<LoadedScript> getAllScripts() {
        return Collections.unmodifiableCollection(scripts.values());
    }
    
    @Override
    public boolean isInitialized() {
        return initialized.get();
    }
    
    @Override
    public Object getScriptAPI() {
        return scriptAPI;
    }
    
    private void ensureInitialized() {
        if (!initialized.get()) {
            throw new IllegalStateException("Script engine not initialized");
        }
    }
    
    public boolean wasPreviouslyLoaded() {
        return wasInitialized.get();
    }
    
    /**
     * Console API for scripts.
     * Provides logging functionality.
     */
    public static class ConsoleAPI {
        private final Logger logger;
        
        public ConsoleAPI(Logger logger) {
            this.logger = logger;
        }
        
        public void log(String message) {
            logger.info("[Script] " + message);
        }
        
        public void log(Object obj) {
            logger.info("[Script] " + String.valueOf(obj));
        }
        
        public void warn(String message) {
            logger.warning("[Script] " + message);
        }
        
        public void warn(Object obj) {
            logger.warning("[Script] " + String.valueOf(obj));
        }
        
        public void error(String message) {
            logger.severe("[Script] " + message);
        }
        
        public void error(Object obj) {
            logger.severe("[Script] " + String.valueOf(obj));
        }
        
        public void info(String message) {
            log(message);
        }
        
        public void debug(String message) {
            logger.fine("[Script] " + message);
        }
    }
    
    private static class LoadedScriptImpl implements LoadedScript {
        private final String id;
        private final Path path;
        private final long loadTime;
        private long lastExecutionTime;
        private int executionCount;
        private Throwable lastError;
        
        LoadedScriptImpl(String id, Path path, long loadTime) {
            this.id = id;
            this.path = path;
            this.loadTime = loadTime;
            this.lastExecutionTime = 0;
            this.executionCount = 0;
        }
        
        @Override
        public String getId() {
            return id;
        }
        
        @Override
        public Path getPath() {
            return path;
        }
        
        @Override
        public long getLoadTime() {
            return loadTime;
        }
        
        @Override
        public long getLastExecutionTime() {
            return lastExecutionTime;
        }
        
        @Override
        public int getExecutionCount() {
            return executionCount;
        }
        
        @Override
        public boolean hasErrors() {
            return lastError != null;
        }
        
        @Override
        public Optional<Throwable> getLastError() {
            return Optional.ofNullable(lastError);
        }
        
        void updateLastExecutionTime() {
            this.lastExecutionTime = System.currentTimeMillis();
        }
        
        void incrementExecutionCount() {
            this.executionCount++;
        }
        
        void setError(Throwable error) {
            this.lastError = error;
        }
    }
}