|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 unknowIfGuestInDream. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * Redistribution and use in source and binary forms, with or without |
| 6 | + * modification, are permitted provided that the following conditions are met: |
| 7 | + * * Redistributions of source code must retain the above copyright |
| 8 | + * notice, this list of conditions and the following disclaimer. |
| 9 | + * * Redistributions in binary form must reproduce the above copyright |
| 10 | + * notice, this list of conditions and the following disclaimer in the |
| 11 | + * documentation and/or other materials provided with the distribution. |
| 12 | + * * Neither the name of unknowIfGuestInDream, any associated website, nor the |
| 13 | + * names of its contributors may be used to endorse or promote products |
| 14 | + * derived from this software without specific prior written permission. |
| 15 | + * |
| 16 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | + * DISCLAIMED. IN NO EVENT SHALL UNKNOWIFGUESTINDREAM BE LIABLE FOR ANY |
| 20 | + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 24 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 25 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + */ |
| 27 | + |
| 28 | +package com.tlcsdm.core.util.nashorn; |
| 29 | + |
| 30 | +import cn.hutool.core.io.resource.ResourceUtil; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | + |
| 33 | +import javax.script.Compilable; |
| 34 | +import javax.script.CompiledScript; |
| 35 | +import javax.script.Invocable; |
| 36 | +import javax.script.ScriptEngine; |
| 37 | +import javax.script.ScriptEngineFactory; |
| 38 | +import javax.script.ScriptEngineManager; |
| 39 | +import javax.script.ScriptException; |
| 40 | +import java.io.IOException; |
| 41 | +import java.io.InputStreamReader; |
| 42 | + |
| 43 | +/** |
| 44 | + * @author unknowIfGuestInDream |
| 45 | + */ |
| 46 | +public class NashornDemoTest { |
| 47 | + |
| 48 | + @Test |
| 49 | + public void bind() throws ScriptException { |
| 50 | + ScriptEngineManager engineManager = new ScriptEngineManager(); |
| 51 | + ScriptEngine engine = engineManager.getEngineByName("Nashorn"); |
| 52 | + |
| 53 | + engine.put("name", "Rahman"); |
| 54 | + engine.put("surname", "Usta"); |
| 55 | + |
| 56 | + String fullName = engine.get("name") + " " + engine.get("surname"); |
| 57 | + |
| 58 | + engine.put("fullName", fullName); |
| 59 | + |
| 60 | + engine.eval("var person={};"); |
| 61 | + engine.eval("person.name=name;"); |
| 62 | + engine.eval("person.surname=surname;"); |
| 63 | + |
| 64 | + engine.eval("print(JSON.stringify(person));"); |
| 65 | + |
| 66 | + engine.put("age", 26); |
| 67 | + engine.eval("person.age=age; person.fullName=fullName"); |
| 68 | + |
| 69 | + engine.eval("print(JSON.stringify(person));"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void compilable() throws ScriptException { |
| 74 | + ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn"); |
| 75 | + |
| 76 | + Compilable compilable = (Compilable) engine; |
| 77 | + |
| 78 | + CompiledScript compiled = |
| 79 | + compilable.compile("var calculate=function(one,two){ return (one*two); }"); |
| 80 | + |
| 81 | + Object mul = null; |
| 82 | + try { |
| 83 | + mul = engine.eval("calculate(20,30)"); |
| 84 | + } catch (ScriptException se) { |
| 85 | + System.out.println(se.getMessage()); |
| 86 | + compiled.eval(); |
| 87 | + |
| 88 | + mul = engine.eval("calculate(5,10)"); |
| 89 | + } |
| 90 | + |
| 91 | + System.out.println(mul); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void engines() throws ScriptException { |
| 96 | + ScriptEngineManager mgr = new ScriptEngineManager(); |
| 97 | + |
| 98 | + for (ScriptEngineFactory factory : mgr.getEngineFactories()) { |
| 99 | + System.out.println("ScriptEngineFactory Info"); |
| 100 | + System.out.printf("\tScript Engine: %s (%s)\n", factory.getEngineName(), factory.getEngineVersion()); |
| 101 | + System.out.printf("\tLanguage: %s (%s)\n", factory.getLanguageName(), factory.getLanguageVersion()); |
| 102 | + for (String name : factory.getNames()) { |
| 103 | + System.out.printf("\tEngine Alias: %s\n", name); |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void eval() throws ScriptException { |
| 110 | + ScriptEngineManager engineManager = new ScriptEngineManager(); |
| 111 | + ScriptEngine engine = engineManager.getEngineByName("Nashorn"); |
| 112 | + |
| 113 | + engine.eval("var person= new Object();"); |
| 114 | + engine.eval("person.name='Rahman';"); |
| 115 | + engine.eval("person.surname='Usta';"); |
| 116 | + |
| 117 | + engine.eval("print(JSON.stringify(person));"); |
| 118 | + |
| 119 | + engine.eval("person.age=26;"); |
| 120 | + |
| 121 | + engine.eval("print(JSON.stringify(person));"); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void func() throws ScriptException { |
| 126 | + ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn"); |
| 127 | + |
| 128 | + engine.eval("var person={};"); |
| 129 | + engine.eval("person.name='Rahman';"); |
| 130 | + engine.eval("person.surname='Usta';"); |
| 131 | + engine.eval("person.fullName=function(){return this.name+' '+this.surname};"); |
| 132 | + |
| 133 | + engine.eval("print('FullName: '+person.fullName());"); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void geo() throws ScriptException, IOException { |
| 138 | + ScriptEngineManager engineManager = new ScriptEngineManager(); |
| 139 | + ScriptEngine engine = engineManager.getEngineByName("js"); |
| 140 | + engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/geo.js").openStream())); |
| 141 | + } |
| 142 | + |
| 143 | + @Test |
| 144 | + public void invokable() throws ScriptException, NoSuchMethodException { |
| 145 | + ScriptEngine engine = new ScriptEngineManager().getEngineByName("Nashorn"); |
| 146 | + |
| 147 | + engine.eval("var person={};"); |
| 148 | + engine.eval("person.name='Rahman';"); |
| 149 | + engine.eval("person.surname='Usta';"); |
| 150 | + |
| 151 | + engine.eval("person.calculate=function(age){return this.name+':'+this.surname+':'+age};"); |
| 152 | + engine.eval("calculate=function(one,two){ return (one*two); }"); |
| 153 | + |
| 154 | + Invocable inv = (Invocable) engine; |
| 155 | + |
| 156 | + Object person = engine.get("person"); |
| 157 | + |
| 158 | + Object calculate = inv.invokeMethod(person, "calculate", 26); |
| 159 | + |
| 160 | + System.out.println(calculate); |
| 161 | + |
| 162 | + System.out.println(inv.invokeFunction("calculate", 5, 4)); |
| 163 | + } |
| 164 | + |
| 165 | + @Test |
| 166 | + public void javatype() throws ScriptException, IOException { |
| 167 | + ScriptEngine engine = new ScriptEngineManager().getEngineByName("js"); |
| 168 | + engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/javatype.js").openStream())); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void jruby() throws ScriptException, IOException { |
| 173 | + ScriptEngineManager engineManager = new ScriptEngineManager(); |
| 174 | + ScriptEngine engine = engineManager.getEngineByName("ruby"); |
| 175 | + engine.eval(new InputStreamReader(ResourceUtil.getResource("nashorn/jruby.rb").openStream())); |
| 176 | + } |
| 177 | + |
| 178 | +} |
0 commit comments