ExternalScriptable.kt 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. val context: ScriptContext,
  39. private val indexedProps: MutableMap<Any, Any?> = HashMap()
  40. ) : Scriptable {
  41. private var prototype: Scriptable? = null
  42. private var parent: Scriptable? = null
  43. private fun isEmpty(name: String): Boolean {
  44. return name == ""
  45. }
  46. override fun getClassName(): String {
  47. return "Global"
  48. }
  49. @Synchronized
  50. override fun get(name: String, start: Scriptable): Any? {
  51. return if (this.isEmpty(name)) {
  52. indexedProps.getOrElse(name) { Scriptable.NOT_FOUND }
  53. } else {
  54. synchronized(context) {
  55. val scope = context.getAttributesScope(name)
  56. return if (scope != -1) {
  57. val value = context.getAttribute(name, scope)
  58. Context.javaToJS(value, this)
  59. } else {
  60. Scriptable.NOT_FOUND
  61. }
  62. }
  63. }
  64. }
  65. @Synchronized
  66. override fun get(index: Int, start: Scriptable): Any? {
  67. return indexedProps.getOrElse(index) { Scriptable.NOT_FOUND }
  68. }
  69. @Synchronized
  70. override fun has(name: String, start: Scriptable): Boolean {
  71. return if (this.isEmpty(name)) {
  72. indexedProps.containsKey(name)
  73. } else {
  74. synchronized(context) { return context.getAttributesScope(name) != -1 }
  75. }
  76. }
  77. @Synchronized
  78. override fun has(index: Int, start: Scriptable): Boolean {
  79. return indexedProps.containsKey(index)
  80. }
  81. override fun put(name: String, start: Scriptable, value: Any?) {
  82. if (start === this) {
  83. synchronized(this) {
  84. if (this.isEmpty(name)) {
  85. indexedProps.put(name, value)
  86. } else {
  87. synchronized(context) {
  88. var scope = context.getAttributesScope(name)
  89. if (scope == -1) {
  90. scope = 100
  91. }
  92. context.setAttribute(name, jsToJava(value), scope)
  93. }
  94. }
  95. }
  96. } else {
  97. start.put(name, start, value)
  98. }
  99. }
  100. override fun put(index: Int, start: Scriptable, value: Any?) {
  101. if (start === this) {
  102. synchronized(this) { indexedProps.put(index, value) }
  103. } else {
  104. start.put(index, start, value)
  105. }
  106. }
  107. @Synchronized
  108. override fun delete(name: String) {
  109. if (this.isEmpty(name)) {
  110. indexedProps.remove(name)
  111. } else {
  112. synchronized(context) {
  113. val scope = context.getAttributesScope(name)
  114. if (scope != -1) {
  115. context.removeAttribute(name, scope)
  116. }
  117. }
  118. }
  119. }
  120. override fun delete(index: Int) {
  121. indexedProps.remove(index)
  122. }
  123. override fun getPrototype(): Scriptable? {
  124. return prototype
  125. }
  126. override fun setPrototype(prototype: Scriptable?) {
  127. this.prototype = prototype
  128. }
  129. override fun getParentScope(): Scriptable? {
  130. return parent
  131. }
  132. override fun setParentScope(parent: Scriptable?) {
  133. this.parent = parent
  134. }
  135. @Synchronized
  136. override fun getIds(): Array<Any> {
  137. val keys = allKeys
  138. val size = keys.size + indexedProps.size
  139. val res = arrayOfNulls<Any>(size)
  140. System.arraycopy(keys, 0, res, 0, keys.size)
  141. var i = keys.size
  142. var index: Any
  143. val var5: Iterator<*> = indexedProps.keys.iterator()
  144. while (var5.hasNext()) {
  145. index = var5.next()!!
  146. res[i++] = index
  147. }
  148. @Suppress("UNCHECKED_CAST")
  149. return res as Array<Any>
  150. }
  151. override fun getDefaultValue(typeHint: Class<*>?): Any {
  152. for (i in 0..1) {
  153. val tryToString: Boolean =
  154. if (typeHint == ScriptRuntime.StringClass) {
  155. i == 0
  156. } else {
  157. i == 1
  158. }
  159. var methodName: String
  160. var args: Array<Any?>
  161. if (tryToString) {
  162. methodName = "toString"
  163. args = ScriptRuntime.emptyArgs
  164. } else {
  165. methodName = "valueOf"
  166. args = arrayOfNulls(1)
  167. val hint: String = when {
  168. typeHint == null -> {
  169. "undefined"
  170. }
  171. typeHint == ScriptRuntime.StringClass -> {
  172. "string"
  173. }
  174. typeHint == ScriptRuntime.ScriptableClass -> {
  175. "object"
  176. }
  177. typeHint == ScriptRuntime.FunctionClass -> {
  178. "function"
  179. }
  180. typeHint != ScriptRuntime.BooleanClass && typeHint != java.lang.Boolean.TYPE -> {
  181. if (typeHint != ScriptRuntime.NumberClass
  182. && typeHint != ScriptRuntime.ByteClass
  183. && typeHint != java.lang.Byte.TYPE
  184. && typeHint != ScriptRuntime.ShortClass
  185. && typeHint != java.lang.Short.TYPE
  186. && typeHint != ScriptRuntime.IntegerClass
  187. && typeHint != Integer.TYPE
  188. && typeHint != ScriptRuntime.FloatClass
  189. && typeHint != java.lang.Float.TYPE
  190. && typeHint != ScriptRuntime.DoubleClass
  191. && typeHint != java.lang.Double.TYPE
  192. ) {
  193. throw Context.reportRuntimeError("Invalid JavaScript value of type $typeHint")
  194. }
  195. "number"
  196. }
  197. else -> {
  198. "boolean"
  199. }
  200. }
  201. args[0] = hint
  202. }
  203. var v = ScriptableObject.getProperty(this, methodName)
  204. if (v is Function) {
  205. val function = v
  206. val cx = Context.enter()
  207. v = try {
  208. function.call(cx, function.parentScope, this, args)
  209. } finally {
  210. Context.exit()
  211. }
  212. if (v != null) {
  213. if (v !is Scriptable) {
  214. return v
  215. }
  216. if (typeHint == ScriptRuntime.ScriptableClass || typeHint == ScriptRuntime.FunctionClass) {
  217. return v
  218. }
  219. if (tryToString && v is Wrapper) {
  220. val u = (v as Wrapper).unwrap()
  221. if (u is String) {
  222. return u
  223. }
  224. }
  225. }
  226. }
  227. }
  228. val arg = if (typeHint == null) "undefined" else typeHint.name
  229. throw Context.reportRuntimeError("找不到对象的默认值 $arg")
  230. }
  231. override fun hasInstance(instance: Scriptable): Boolean {
  232. var proto = instance.prototype
  233. while (proto != null) {
  234. if (proto == this) {
  235. return true
  236. }
  237. proto = proto.prototype
  238. }
  239. return false
  240. }
  241. private val allKeys: Array<String>
  242. get() {
  243. val list = ArrayList<String>()
  244. synchronized(context) {
  245. val iterator: Iterator<*> = context.scopes.iterator()
  246. while (iterator.hasNext()) {
  247. val scope = iterator.next() as Int
  248. val bindings = context.getBindings(scope)
  249. if (bindings != null) {
  250. list.ensureCapacity(bindings.size)
  251. val iterator1: Iterator<*> = bindings.keys.iterator()
  252. while (iterator1.hasNext()) {
  253. val key = iterator1.next() as String
  254. list.add(key)
  255. }
  256. }
  257. }
  258. }
  259. return list.toTypedArray()
  260. }
  261. private fun jsToJava(jsObj: Any?): Any? {
  262. return if (jsObj is Wrapper) {
  263. if (jsObj is NativeJavaClass) {
  264. jsObj
  265. } else {
  266. val obj = jsObj.unwrap()
  267. if (obj !is Number && obj !is String && obj !is Boolean && obj !is Char) obj else jsObj
  268. }
  269. } else {
  270. jsObj
  271. }
  272. }
  273. }