diff --git a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/JsInterpreter.kt b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/JsInterpreter.kt index 3d85f87a22a..8b2b96584b4 100644 --- a/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/JsInterpreter.kt +++ b/library/src/commonMain/kotlin/com/lagradost/cloudstream3/utils/JsInterpreter.kt @@ -29,13 +29,16 @@ import kotlin.math.* * - typeof * * Usage: - * val result = JsInterpreter().eval("var x = 1+2; x") + * val result = evalJs("var x = 1+2; x") * // result == 3.0 (numbers are always Double internally) * * // Drop-in replacement for the old Rhino-based evaluateString pattern: * val ctx = JsContext() * ctx.eval("var url = 'https:' + computeSuffix()") * val url = ctx["url"] // retrieves the variable as a String + * + * // Or simpler: + * val url = evalJs("var url = 'https:' + computeSuffix()", "url") */ /** @@ -695,8 +698,7 @@ private class Scope(val parent: Scope? = null) { if (vars.containsKey(name)) this else parent?.findOwner(name) } -@Prerelease -class JsInterpreter { +private class JsInterpreter { private val globalScope = Scope() init { installGlobals() } diff --git a/library/src/commonTest/kotlin/com/lagradost/cloudstream3/utils/JsInterpreterTest.kt b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/utils/JsInterpreterTest.kt index 20b46acb93d..3e90feccd39 100644 --- a/library/src/commonTest/kotlin/com/lagradost/cloudstream3/utils/JsInterpreterTest.kt +++ b/library/src/commonTest/kotlin/com/lagradost/cloudstream3/utils/JsInterpreterTest.kt @@ -9,9 +9,9 @@ import kotlin.test.assertFalse class JsInterpreterTest { - private fun num(code: String, variable: String? = null) = (evalJs(code, variable) as? Double) ?: Double.NaN - private fun str(code: String, variable: String? = null) = jsValueToString(evalJs(code, variable)) private fun bool(code: String, variable: String? = null) = evalJs(code, variable) as? Boolean ?: false + private fun num(code: String, variable: String? = null) = evalJs(code, variable) as? Double ?: Double.NaN + private fun str(code: String, variable: String? = null) = jsValueToString(evalJs(code, variable)) private fun assertApprox(expected: Double, actual: Double, tol: Double = 1e-9) { assertTrue(abs(actual - expected) <= tol, "Expected $expected ± $tol but was $actual")