-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #68 from anatawa12/crashes-when-click-search-light
crash when click search light
- Loading branch information
Showing
3 changed files
with
107 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/anatawa12/fixRtm/scripting/PrimitiveJavaHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.anatawa12.fixRtm.scripting | ||
|
||
import org.mozilla.javascript.* | ||
|
||
@Suppress("FunctionName") | ||
object PrimitiveJavaHelper { | ||
const val internalClassName = "com/anatawa12/fixRtm/scripting/PrimitiveJavaHelper" | ||
|
||
const val NativeString_name = "org.mozilla.javascript.NativeString" | ||
const val NativeString_internal = "org/mozilla/javascript/NativeString" | ||
const val get_name = "get" | ||
const val get_desc = "(Lj${"ava/lang/String"};L${"org/mozilla/javascript/Scriptable"};)L${"java/lang/Object"};" | ||
const val NativeString_get_hook_name = "NativeString_get" | ||
const val NativeString_get_hook_desc = "(L${"java/lang/String"};L${"org/mozilla/javascript/Scriptable"};)L${"java/lang/Object"};" | ||
const val NativeString_string_name = "string" | ||
const val NativeString_string_desc = "L${"java/lang/CharSequence"};" | ||
|
||
@JvmStatic | ||
fun NativeString_get(name: String?, start: Scriptable?): Any? { | ||
if (name in StringMethod_names) return Scriptable.NOT_FOUND | ||
return wrapFunctionForString(stringObject.get(name, start)) | ||
} | ||
|
||
private val stringObject = NativeJavaObject(null, "", null) | ||
|
||
private fun wrapFunctionForString(scriptable: Any?): Any? { | ||
if (scriptable is NativeJavaMethod) | ||
return FixRTMWrappedFunction(scriptable, ::getStringInstance) | ||
else | ||
return scriptable | ||
} | ||
|
||
@JvmStatic | ||
fun getStringInstance(scriptable: Scriptable) = FixRTMRhinoAccessor.getStringOfNativeString(scriptable) | ||
|
||
val StringMethod_names = setOf( | ||
"charAt", "charCodeAt", "codePointAt", "concat", "includes", "endsWith", "indexOf", "lastIndexOf", | ||
"localeCompare", "match", "normalize", "padEnd", "padStart", "quote", "repeat", "replace", "search", | ||
"slice", "split", "startsWith", "substr", "substring", "toLocaleLowerCase", "toLocaleUpperCase", | ||
"toLowerCase", "toSource", "toString", "toUpperCase", "trim", "trimStart", "trimLeft", "trimEnd", | ||
"trimRight", "valueOf", "anchor", "big", "blink", "bold", "fixed", "fontcolor", "fontsize", "italics", | ||
"link", "small", "strike", "sub", "sup" | ||
) | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/java/com/anatawa12/fixRtm/scripting/inRhinoPackage.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
@file:Suppress("PackageDirectoryMismatch") | ||
|
||
package org.mozilla.javascript | ||
|
||
class FixRTMWrappedFunction(private val javaFunction: NativeJavaMethod, private val getInstance: (Scriptable) -> Any?) : BaseFunction() { | ||
override fun getFunctionName(): String = javaFunction.functionName | ||
override fun decompile(indent: Int, flags: Int): String = javaFunction.decompile(indent, flags) | ||
override fun toString(): String = javaFunction.toString() | ||
|
||
override fun call(cx: Context, scope: Scriptable, thisObj: Scriptable?, args: Array<out Any>?): Any { | ||
return javaFunction.call(cx, scope, thisObj?.let { wrap(cx, scope, thisObj) }, args) | ||
} | ||
|
||
private fun wrap(cx: Context, scope: Scriptable, thisObj: Scriptable?): Scriptable? { | ||
if (thisObj == null) return null | ||
val topLevel = ScriptableObject.getTopLevelScope(scope) | ||
return cx.wrapFactory.wrapNewObject(cx, topLevel, getInstance(thisObj)) | ||
} | ||
} | ||
|
||
object FixRTMRhinoAccessor { | ||
fun getStringOfNativeString(scriptable: Scriptable): String? | ||
= (scriptable as? NativeString)?.toCharSequence()?.toString() | ||
|
||
} |