29 import javax.script.Invocable; 30 import javax.script.ScriptContext; 31 import javax.script.ScriptEngine; 32 import javax.script.ScriptEngineManager; 33 import javax.script.ScriptException; 34 import sun.jvm.hotspot.debugger.*; 35 import sun.jvm.hotspot.oops.*; 36 import sun.jvm.hotspot.runtime.*; 37 import sun.jvm.hotspot.utilities.*; 38 import sun.jvm.hotspot.tools.*; 39 import sun.jvm.hotspot.tools.jcore.*; 40 import java.lang.reflect.Method; 41 import java.lang.reflect.Modifier; 42 43 /** 44 * Simple wrapper around jsr-223 JavaScript script engine. 45 * In addition to wrapping useful functionality of jsr-223 engine, 46 * this class exposed certain "global" functions to the script. 47 */ 48 public abstract class JSJavaScriptEngine extends MapScriptObject { 49 /** 50 * Start a read-eval-print loop with this engine. 51 */ 52 public void startConsole() { 53 start(true); 54 } 55 56 /** 57 * Initialize the engine so that we can "eval" strings 58 * and files later. 59 */ 60 public void start() { 61 start(false); 62 } 63 64 /** 65 * Define a global function that invokes given Method. 66 */ 67 public void defineFunction(Object target, Method method) { 68 putFunction(target, method, false); | 29 import javax.script.Invocable; 30 import javax.script.ScriptContext; 31 import javax.script.ScriptEngine; 32 import javax.script.ScriptEngineManager; 33 import javax.script.ScriptException; 34 import sun.jvm.hotspot.debugger.*; 35 import sun.jvm.hotspot.oops.*; 36 import sun.jvm.hotspot.runtime.*; 37 import sun.jvm.hotspot.utilities.*; 38 import sun.jvm.hotspot.tools.*; 39 import sun.jvm.hotspot.tools.jcore.*; 40 import java.lang.reflect.Method; 41 import java.lang.reflect.Modifier; 42 43 /** 44 * Simple wrapper around jsr-223 JavaScript script engine. 45 * In addition to wrapping useful functionality of jsr-223 engine, 46 * this class exposed certain "global" functions to the script. 47 */ 48 public abstract class JSJavaScriptEngine extends MapScriptObject { 49 50 private static native Module getEveryoneModule(); 51 52 // Open all packages in jdk.hotspot.agent to all modules. 53 static{ 54 var everyoneModule = getEveryoneModule(); 55 var saModule = JSJavaScriptEngine.class.getModule(); 56 saModule.getPackages() 57 .forEach(p -> saModule.addOpens(p, everyoneModule)); 58 } 59 60 /** 61 * Start a read-eval-print loop with this engine. 62 */ 63 public void startConsole() { 64 start(true); 65 } 66 67 /** 68 * Initialize the engine so that we can "eval" strings 69 * and files later. 70 */ 71 public void start() { 72 start(false); 73 } 74 75 /** 76 * Define a global function that invokes given Method. 77 */ 78 public void defineFunction(Object target, Method method) { 79 putFunction(target, method, false); |