diff --git a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/JSBuilder.kt b/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/JSBuilder.kt index b6b58179b..c77f0c9ef 100644 --- a/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/JSBuilder.kt +++ b/kmath-ast/src/jsMain/kotlin/kscience/kmath/estree/internal/JSBuilder.kt @@ -56,18 +56,25 @@ internal class JSBuilder(val bodyCallback: JSBuilder.() -> BaseExpression) } } - fun variable(name: String): BaseExpression { - return MemberExpression( - computed = true, - optional = false, - `object` = Identifier("arguments"), - property = SimpleLiteral(name), - ) - } + fun variable(name: String): BaseExpression = call(getOrFail, Identifier("arguments"), SimpleLiteral(name)) fun call(function: Function, vararg args: BaseExpression): BaseExpression = SimpleCallExpression( optional = false, callee = constant(function), *args, ) + + private companion object { + @Suppress("UNUSED_VARIABLE") + val getOrFail: (`object`: dynamic, key: String) -> dynamic = { `object`, key -> + val k = key + val o = `object` + + if (!(js("k in o") as Boolean)) { + throw NoSuchElementException() + } + + js("o[k]") + } + } }