restricting host access

This commit is contained in:
minjaesong
2021-10-27 19:37:23 +09:00
parent 886bea5ccb
commit 1c8261cc7e
5 changed files with 31 additions and 430 deletions

View File

@@ -47,7 +47,7 @@ object IME {
private val highLayers = HashMap<String, TerrarumInputMethod>()
private val context = org.graalvm.polyglot.Context.newBuilder("js")
.allowHostAccess(org.graalvm.polyglot.HostAccess.ALL)
.allowHostAccess(org.graalvm.polyglot.HostAccess.EXPLICIT)
// .allowHostClassLookup { it.equals("net.torvald.terrarum.gamecontroller.IMEProviderDelegate") }
.allowHostClassLookup { false }
.allowIO(false)

View File

@@ -2,6 +2,7 @@ package net.torvald.terrarum.gamecontroller
import net.torvald.terrarum.App.printdbg
import net.torvald.util.SortedArrayList
import org.graalvm.polyglot.HostAccess
import java.io.File
import java.io.FileReader
@@ -9,6 +10,7 @@ class IMEProviderDelegate(val ime: IME) {
private val dictionaries = HashMap<String, IMEDictionary>()
@HostAccess.Export
fun requestDictionary(filename: String): IMEDictionary {
return dictionaries.getOrPut(filename) { IMEDictionary(filename) }
}
@@ -18,7 +20,7 @@ class IMEProviderDelegate(val ime: IME) {
class IMEDictionary(private val filename: String) {
private val candidates = HashMap<String, String>(16384)
private val keys = SortedArrayList<String>(16384)
private val keys = SortedArrayList<String>(16384) // keys on the .han file are absofreakinlutely not sorted
private var dictLoaded = false
@@ -44,15 +46,36 @@ class IMEDictionary(private val filename: String) {
loadDict() // loading the dict doesn't take too long so no need to do it lazily
}
operator fun get(key: String): String {
@HostAccess.Export
fun getCangjie(key: String): String {
//if (!dictLoaded) loadDict()
val out = StringBuilder()
var outsize = 0
var index = keys.searchForInterval(key) { it }.second
while (outsize < 10) {
val keysym = keys[index]
val allRelevantKeys = ArrayList<String>() // oh, oha, ohag, ohbt, ohby, ...
for (i in 0 until 10) {
if (index + 1 >= keys.size) break
val keysym = keys[index + i]
if (!keysym.startsWith(key)) break
allRelevantKeys.add(keysym)
}
// printdbg(this, "lookup key: $key")
// sort allRelevantKeys so that short sequences come first
// e.g. oh, oha, ohg, ohj, ohn, ohq, ohag, ohbt, ...
allRelevantKeys.sortWith { it, other ->
if (it.length == other.length) it.compareTo(other)
else it.length.compareTo(other.length)
}
// printdbg(this, "predictions: (${allRelevantKeys.size}) ${allRelevantKeys.joinToString()}")
index = 0 // now this is an index for the allRelevantKeys
while (outsize < 10 && index < allRelevantKeys.size) {
val keysym = allRelevantKeys[index]
if (!keysym.startsWith(key)) break
val outstr = ",${candidates[keysym]}"
@@ -64,5 +87,5 @@ class IMEDictionary(private val filename: String) {
return if (out.isNotEmpty()) out.substring(1) else ""
}
}