My favorites | Sign in
Project Logo
Project hosting will be READ-ONLY Wednesday, 7AM PST due to brief network maintenance
                
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
package com.netgents.script.scala

import com.netgents.script.scala.util._
import com.netgents.script.scala.util.JclUtil._
import java.io._
import javax.script._
import _root_.scala.tools.nsc._

/**
* written by Walter Chang (weihsiu@gmail.com)
* patch submitted by Mateusz Fiolka (mateusz.fiolka@gmail.com)
*/
class ScalaScriptEngine(factory: ScriptEngineFactory) extends AbstractScriptEngine {

private val interpreter = new Interpreter(new Settings)
interpreter.beQuiet

def createBindings = new SimpleBindings

@throws(classOf[ScriptException])
def eval(reader: Reader, context: ScriptContext): Object = {
val bufferedReader = new BufferedReader(reader)
val script = new StringBuilder
var ch: Int = -1
while ({ch = bufferedReader.read; ch != -1})
script.append(ch.asInstanceOf[Char])
eval(script.toString, context)
}

@throws(classOf[ScriptException])
def eval(script: String, context: ScriptContext): Object = {
bindContext(context)
val result = Array("".asInstanceOf[Object])
interpreter.bind("$JSR223result", "Array[Any]", result)
val newScript: String =
"var scriptResult: Any = \"\" ; " +
script +
" ; $JSR223result(0) = scriptResult"

if (interpreter.interpret(newScript) != InterpreterResults.Success)
throw new ScriptException("something went wrong")
result(0).asInstanceOf[Object]
}

def getFactory = factory

private def bindContext(context: ScriptContext) {
bindScope(ScriptContext.GLOBAL_SCOPE, context)
bindScope(ScriptContext.ENGINE_SCOPE, context)
}

private def bindScope(scope: Int, context: ScriptContext) {
val bindings = context.getBindings(scope)
if (!(bindings eq null)) {
asMap(bindings) foreach { e =>
val parts = e._1.split(":")
parts.length match {
case 1 => {
val valValue = e._2
val valName = parts(0).trim
val valType = valValue.getClass.getName
val newType =
basicTypesMapping.get(valType).getOrElse(valType)

interpreter.bind(valName, newType, valValue)
}
case 2 => {
val valValue = e._2
val valName = parts(0).trim
val valType = parts(1).trim
val realType = valValue.getClass.getName
val newType =
basicTypesMapping.get(realType).getOrElse(valType)

interpreter.bind(valName, newType, valValue)
}
case _ => error("binding key has to be " +
"\"<variable name>:<variable type>\"" +
"or \"<variable name>\"")
}
}
}
}

private val basicTypesMapping = Map(
"java.lang.Boolean" -> "Boolean",
"boolean" -> "Boolean",
"java.lang.Character" -> "Char",
"char" -> "Char",
"java.lang.Byte" -> "Byte",
"byte" -> "Byte",
"java.lang.Short" -> "Short",
"short" -> "Short",
"java.lang.Integer" -> "Int",
"int" -> "Int",
"java.lang.Long" -> "Long",
"long" -> "Long",
"java.lang.Float" -> "Float",
"float" -> "Float",
"java.lang.Double" -> "Double",
"double" -> "Double"
)

}
Show details Hide details

Change log

r30 by weihsiu on Feb 01, 2009   Diff
patch submitted by Mateusz Fiolka
(mateusz.fiolka@gmail.com)
Go to: 
Project members, sign in to write a code review

Older revisions

r25 by weihsiu on May 13, 2008   Diff
Initial import.
All revisions of this file

File info

Size: 3181 bytes, 104 lines
Hosted by Google Code