-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsection.javax.script.xml
More file actions
417 lines (352 loc) · 12.2 KB
/
section.javax.script.xml
File metadata and controls
417 lines (352 loc) · 12.2 KB
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
<?xml version="1.0" encoding="UTF-8"?>
<section id="index"><?dbhtml dir="javax/script" ?>
<title>java 脚本引擎</title>
<para>什么是脚本引擎,脚本引擎是指在程序运行期间嵌入另一种脚本语言,并与其交互,产生最终运行结果</para>
<para>脚本引擎存在的意义是什么?脚本引擎可以改变编译语言的内部运行逻辑,弥补编译语言的不足,使编译语言具备动态语言的一部分特性。</para>
<para>是否有成功案例?最成功的案例就是基于C++和Lua语言开发的端游(网游一种,需要按照客户端),编译语言最大的缺点就是客户端升级需要重新安装并且安装之后重启应用程序才能生效。脚本引擎弥补了这项致命的缺点,用户只需升级剧情脚本,而不需要退出整个游戏然后重新进入。</para>
<section id="maven">
<title>Maven</title>
<programlisting>
<![CDATA[
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.netkiller</groupId>
<artifactId>script</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Java Script</name>
<description>Java Script Engine</description>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
</dependency>
</dependencies>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
]]>
</programlisting>
</section>
<section id="helloworld">
<title>Helloworld</title>
<para></para>
<programlisting>
<![CDATA[
package cn.netkiller.javascript;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineFactory;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class Helloworld {
public Helloworld() {
ScriptEngineManager manager = new ScriptEngineManager();
List<ScriptEngineFactory> factories = manager.getEngineFactories();
for (ScriptEngineFactory f : factories) {
System.out.println("egine name:" + f.getEngineName());
System.out.println("engine version:" + f.getEngineVersion());
System.out.println("language name:" + f.getLanguageName());
System.out.println("language version:" + f.getLanguageVersion());
System.out.println("names:" + f.getNames());
System.out.println("mime:" + f.getMimeTypes());
System.out.println("extension:" + f.getExtensions());
System.out.println("-----------------------------------------------");
}
}
public void hello() throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// ScriptEngine engine = manager.getEngineByExtension("js");
// ScriptEngine engine = manager.getEngineByMimeType("text/javascript");
engine.eval("print('Hello, World')");
}
public static void main(String[] args) {
try {
new Helloworld().hello();
} catch (ScriptException ex) {
Logger.getLogger(Helloworld.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
]]>
</programlisting>
</section>
<section id="file">
<title>运行脚本文件</title>
<para>将脚本放入外部文件</para>
<programlisting>
<![CDATA[
package javascript;
import java.io.FileNotFoundException;
import java.net.URL;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class EvalFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByExtension("js");
// ScriptEngine engine = manager.getEngineByMimeType("text/javascript");
try {
URL location = EvalFile.class.getProtectionDomain().getCodeSource().getLocation();
String file = location.getFile() + "test.js";
System.out.println(file);
engine.eval(new java.io.FileReader(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
]]>
</programlisting>
<para>test.js</para>
<programlisting>
print("This is hello from test.js");
</programlisting>
</section>
<section id="variable">
<title>变量传递</title>
<para></para>
<programlisting>
<![CDATA[
package javascript;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.script.Bindings;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
public class ScriptVars {
ScriptEngine engine = null;
public ScriptVars() {
ScriptEngineManager manager = new ScriptEngineManager();
engine = manager.getEngineByMimeType("text/javascript");
}
public void variable() throws ScriptException {
engine.put("name", "Neo");
engine.eval("var message = 'Hello, ' + name;");
engine.eval("print(message);");
Object obj = engine.get("message");
System.out.println(obj);
}
public void simpleBindings() throws ScriptException {
Bindings bindings = new SimpleBindings();
bindings.put("name", "Neo");
engine.eval("print('I am ' + name);", bindings);
}
public void function() throws ScriptException {
engine.put("a", 4);
engine.put("b", 6);
Object maxNum = engine.eval("function max_num(a,b){return (a>b)?a:b;}max_num(a,b);");
System.out.println("max_num:" + maxNum + ", (class = " + maxNum.getClass() + ")");
Map<String, Integer> m = new HashMap<String, Integer>();
m.put("c", 10);
engine.put("m", m);
engine.eval("var x= max_num(a,m.get('c'));");
System.out.println("max_num:" + engine.get("x"));
}
public void object() throws ScriptException {
File f = new File("test.txt");
// expose File object as variable to script
engine.put("file", f);
// evaluate a script string. The script accesses "file"
// variable and calls method on it
engine.eval("print(file.getAbsolutePath())");
}
public void outputToFile() throws IOException, ScriptException {
ScriptContext context = engine.getContext();
context.setWriter(new FileWriter("script.log"));
engine.eval("print('Hello World!');");
}
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
ScriptVars sv = new ScriptVars();
sv.variable();
sv.simpleBindings();
sv.outputToFile();
sv.function();
sv.object();
sv.outputToFile();
} catch (ScriptException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
]]>
</programlisting>
</section>
<section id="global">
<title>全局变量与局部变量定义</title>
<para>ScriptContext.GLOBAL_SCOPE 作用于 ScriptEngineManager, ScriptContext.ENGINE_SCOPE 作用于 ScriptEngine</para>
<programlisting>
<![CDATA[
package javascript;
import javax.script.ScriptContext;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class ContextVariable {
public static void main(String[] args) throws ScriptException {
// TODO Auto-generated method stub
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
ScriptContext context = engine.getContext();
context.setAttribute("name", "Netkiller", ScriptContext.GLOBAL_SCOPE);
context.setAttribute("name", "Neo", ScriptContext.ENGINE_SCOPE);
//context.getAttribute("name");
engine.eval("print('I am ' + name);", context);
// engine1,context1 并没有定义 name 但输出结果却显示 Netkiller,所以ScriptContext.GLOBAL_SCOPE定义的变量是全局的。
ScriptEngine engine1 = manager.getEngineByName("JavaScript");
ScriptContext context1 = engine1.getContext();
engine.eval("print('I am ' + name);", context1);
}
}
]]>
</programlisting>
<programlisting>
<![CDATA[
import javax.script.*;
public class MultiScopes {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
engine.put("x", "hello");
// print global variable "x"
engine.eval("print(x);");
// the above line prints "hello"
// Now, pass a different script context
ScriptContext newContext = new SimpleScriptContext();
Bindings engineScope = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
// add new variable "x" to the new engineScope
engineScope.put("x", "world");
// execute the same script - but this time pass a different script context
engine.eval("print(x);", newContext);
// the above line prints "world"
}
}
]]>
</programlisting>
</section>
<section id="fun">
<title>调用脚本中的函数或方法</title>
<para></para>
<programlisting>
<![CDATA[
package javascript;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
public class InvokeScriptFunction {
public static void main(String[] args) throws ScriptException, NoSuchMethodException {
// TODO Auto-generated method stub
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
// JavaScript code in a String
String script = "function hello(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);
// javax.script.Invocable is an optional interface.
// Check whether your script engine implements or not!
// Note that the JavaScript engine implements Invocable interface.
Invocable inv = (Invocable) engine;
// invoke the global function named "hello"
inv.invokeFunction("hello", "Scripting!!");
// JavaScript code in a String. This code defines a script object 'obj'
// with one method called 'hello'.
script = "var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }";
// evaluate script
engine.eval(script);
// get script object on which we want to call the method
Object obj = engine.get("obj");
// invoke the method named "hello" on the script object "obj"
inv.invokeMethod(obj, "hello", "Script Method !!");
}
}
]]>
</programlisting>
</section>
<section id="compile">
<title>脚本编译</title>
<para>只有重复执行脚本时才需要编译。只运行一次不建议编译运行。</para>
<programlisting>
<![CDATA[
package javascript;
import javax.script.*;
public class ScriptCompile {
public CompiledScript compile(String script) throws ScriptException {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
return ((Compilable) engine).compile(script);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
ScriptCompile sc = new ScriptCompile();
try {
CompiledScript script = sc.compile("print('Helloworld!!!');");
for (int i = 0; i < 100; i++) {
script.eval();
}
} catch (ScriptException ex) {
ex.printStackTrace();
}
}
}
]]>
</programlisting>
</section>
<section id="jjs">
<title>jjs - Invokes the Nashorn engine.</title>
<para>jjs 是一个在命令行运行脚本的命令</para>
<para>创建脚本文件</para>
<programlisting>
<![CDATA[
[neo@netkiller tmp]$ cat test.js
function f() {
return 1;
};
print( f() + 1 );
]]>
</programlisting>
<para>运行结果</para>
<programlisting>
<![CDATA[
[neo@netkiller tmp]$ jjs test.js
2
]]>
</programlisting>
<programlisting>
<![CDATA[]]>
</programlisting>
</section>
</section>