Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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")
*/

/**
Expand Down Expand Up @@ -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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down