ExternalScriptable.kt 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * Copyright (c) 2005, 2006, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation. Oracle designates this
  8. * particular file as subject to the "Classpath" exception as provided
  9. * by Oracle in the LICENSE file that accompanied this code.
  10. *
  11. * This code is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  14. * version 2 for more details (a copy is included in the LICENSE file that
  15. * accompanied this code).
  16. *
  17. * You should have received a copy of the GNU General Public License version
  18. * 2 along with this work; if not, write to the Free Software Foundation,
  19. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22. * or visit www.oracle.com if you need additional information or have any
  23. * questions.
  24. */
  25. package com.script.rhino
  26. import com.script.ScriptContext
  27. import org.mozilla.javascript.*
  28. import org.mozilla.javascript.Function
  29. /**
  30. * ExternalScriptable is an implementation of Scriptable
  31. * backed by a JSR 223 ScriptContext instance.
  32. *
  33. * @author Mike Grogan
  34. * @author A. Sundararajan
  35. * @since 1.6
  36. */
  37. internal class ExternalScriptable @JvmOverloads constructor(
  38. context: ScriptContext?,
  39. indexedProps: MutableMap<Any, Any?> = HashMap()
  40. ) : Scriptable {
  41. val context: ScriptContext
  42. private val indexedProps: MutableMap<Any, Any?>
  43. private var prototype: Scriptable? = null
  44. private var parent: Scriptable? = null
  45. init {
  46. if (context == null) {
  47. throw NullPointerException("context is null")
  48. } else {
  49. this.context = context
  50. this.indexedProps = indexedProps
  51. }
  52. }
  53. private fun isEmpty(name: String): Boolean {
  54. return name == ""
  55. }
  56. override fun getClassName(): String {
  57. return "Global"
  58. }
  59. @Synchronized
  60. override fun get(name: String, start: Scriptable): Any? {
  61. return if (this.isEmpty(name)) {
  62. indexedProps.getOrElse(name) { Scriptable.NOT_FOUND }
  63. } else {
  64. synchronized(context) {
  65. val scope = context.getAttributesScope(name)
  66. return if (scope != -1) {
  67. val value = context.getAttribute(name, scope)
  68. Context.javaToJS(value, this)
  69. } else {
  70. Scriptable.NOT_FOUND
  71. }
  72. }
  73. }
  74. }
  75. @Synchronized
  76. override fun get(index: Int, start: Scriptable): Any? {
  77. return indexedProps.getOrElse(index) { Scriptable.NOT_FOUND }
  78. }
  79. @Synchronized
  80. override fun has(name: String, start: Scriptable): Boolean {
  81. return if (this.isEmpty(name)) {
  82. indexedProps.containsKey(name)
  83. } else {
  84. synchronized(context) { return context.getAttributesScope(name) != -1 }
  85. }
  86. }
  87. @Synchronized
  88. override fun has(index: Int, start: Scriptable): Boolean {
  89. return indexedProps.containsKey(index)
  90. }
  91. override fun put(name: String, start: Scriptable, value: Any?) {
  92. if (start === this) {
  93. synchronized(this) {
  94. if (this.isEmpty(name)) {
  95. indexedProps.put(name, value)
  96. } else {
  97. synchronized(context) {
  98. var scope = context.getAttributesScope(name)
  99. if (scope == -1) {
  100. scope = 100
  101. }
  102. context.setAttribute(name, jsToJava(value), scope)
  103. }
  104. }
  105. }
  106. } else {
  107. start.put(name, start, value)
  108. }
  109. }
  110. override fun put(index: Int, start: Scriptable, value: Any?) {
  111. if (start === this) {
  112. synchronized(this) { indexedProps.put(index, value) }
  113. } else {
  114. start.put(index, start, value)
  115. }
  116. }
  117. @Synchronized
  118. override fun delete(name: String) {
  119. if (this.isEmpty(name)) {
  120. indexedProps.remove(name)
  121. } else {
  122. synchronized(context) {
  123. val scope = context.getAttributesScope(name)
  124. if (scope != -1) {
  125. context.removeAttribute(name, scope)
  126. }
  127. }
  128. }
  129. }
  130. override fun delete(index: Int) {
  131. indexedProps.remove(index)
  132. }
  133. override fun getPrototype(): Scriptable? {
  134. return prototype
  135. }
  136. override fun setPrototype(prototype: Scriptable?) {
  137. this.prototype = prototype
  138. }
  139. override fun getParentScope(): Scriptable? {
  140. return parent
  141. }
  142. override fun setParentScope(parent: Scriptable?) {
  143. this.parent = parent
  144. }
  145. @Synchronized
  146. override fun getIds(): Array<Any> {
  147. val keys = allKeys
  148. val size = keys.size + indexedProps.size
  149. val res = arrayOfNulls<Any>(size)
  150. System.arraycopy(keys, 0, res, 0, keys.size)
  151. var i = keys.size
  152. var index: Any
  153. val var5: Iterator<*> = indexedProps.keys.iterator()
  154. while (var5.hasNext()) {
  155. index = var5.next()!!
  156. res[i++] = index
  157. }
  158. @Suppress("UNCHECKED_CAST")
  159. return res as Array<Any>
  160. }
  161. override fun getDefaultValue(typeHint: Class<*>?): Any {
  162. for (i in 0..1) {
  163. val tryToString: Boolean =
  164. if (typeHint == ScriptRuntime.StringClass) {
  165. i == 0
  166. } else {
  167. i == 1
  168. }
  169. var methodName: String
  170. var args: Array<Any?>
  171. if (tryToString) {
  172. methodName = "toString"
  173. args = ScriptRuntime.emptyArgs
  174. } else {
  175. methodName = "valueOf"
  176. args = arrayOfNulls(1)
  177. val hint: String = when {
  178. typeHint == null -> {
  179. "undefined"
  180. }
  181. typeHint == ScriptRuntime.StringClass -> {
  182. "string"
  183. }
  184. typeHint == ScriptRuntime.ScriptableClass -> {
  185. "object"
  186. }
  187. typeHint == ScriptRuntime.FunctionClass -> {
  188. "function"
  189. }
  190. typeHint != ScriptRuntime.BooleanClass && typeHint != java.lang.Boolean.TYPE -> {
  191. if (typeHint != ScriptRuntime.NumberClass
  192. && typeHint != ScriptRuntime.ByteClass
  193. && typeHint != java.lang.Byte.TYPE
  194. && typeHint != ScriptRuntime.ShortClass
  195. && typeHint != java.lang.Short.TYPE
  196. && typeHint != ScriptRuntime.IntegerClass
  197. && typeHint != Integer.TYPE
  198. && typeHint != ScriptRuntime.FloatClass
  199. && typeHint != java.lang.Float.TYPE
  200. && typeHint != ScriptRuntime.DoubleClass
  201. && typeHint != java.lang.Double.TYPE
  202. ) {
  203. throw Context.reportRuntimeError("Invalid JavaScript value of type $typeHint")
  204. }
  205. "number"
  206. }
  207. else -> {
  208. "boolean"
  209. }
  210. }
  211. args[0] = hint
  212. }
  213. var v = ScriptableObject.getProperty(this, methodName)
  214. if (v is Function) {
  215. val function = v
  216. val cx = Context.enter()
  217. v = try {
  218. function.call(cx, function.parentScope, this, args)
  219. } finally {
  220. Context.exit()
  221. }
  222. if (v != null) {
  223. if (v !is Scriptable) {
  224. return v
  225. }
  226. if (typeHint == ScriptRuntime.ScriptableClass || typeHint == ScriptRuntime.FunctionClass) {
  227. return v
  228. }
  229. if (tryToString && v is Wrapper) {
  230. val u = (v as Wrapper).unwrap()
  231. if (u is String) {
  232. return u
  233. }
  234. }
  235. }
  236. }
  237. }
  238. val arg = if (typeHint == null) "undefined" else typeHint.name
  239. throw Context.reportRuntimeError("找不到对象的默认值 $arg")
  240. }
  241. override fun hasInstance(instance: Scriptable): Boolean {
  242. var proto = instance.prototype
  243. while (proto != null) {
  244. if (proto == this) {
  245. return true
  246. }
  247. proto = proto.prototype
  248. }
  249. return false
  250. }
  251. private val allKeys: Array<String>
  252. get() {
  253. val list = ArrayList<String>()
  254. synchronized(context) {
  255. val iterator: Iterator<*> = context.scopes.iterator()
  256. while (iterator.hasNext()) {
  257. val scope = iterator.next() as Int
  258. val bindings = context.getBindings(scope)
  259. if (bindings != null) {
  260. list.ensureCapacity(bindings.size)
  261. val iterator1: Iterator<*> = bindings.keys.iterator()
  262. while (iterator1.hasNext()) {
  263. val key = iterator1.next() as String
  264. list.add(key)
  265. }
  266. }
  267. }
  268. }
  269. return list.toTypedArray()
  270. }
  271. private fun jsToJava(jsObj: Any?): Any? {
  272. return if (jsObj is Wrapper) {
  273. if (jsObj is NativeJavaClass) {
  274. jsObj
  275. } else {
  276. val obj = jsObj.unwrap()
  277. if (obj !is Number && obj !is String && obj !is Boolean && obj !is Char) obj else jsObj
  278. }
  279. } else {
  280. jsObj
  281. }
  282. }
  283. }