From de9f3cc8df4ebb0c05ee81d6a77922174e8d532c Mon Sep 17 00:00:00 2001
From: Gleb Minaev <43728100+lounres@users.noreply.github.com>
Date: Fri, 10 Jun 2022 23:37:50 +0300
Subject: [PATCH 1/3] Inlined LaTeX formula. Now GitHub supports MathJax!
---
kmath-ast/README.md | 5 +----
kmath-ast/docs/README-TEMPLATE.md | 5 +----
2 files changed, 2 insertions(+), 8 deletions(-)
diff --git a/kmath-ast/README.md b/kmath-ast/README.md
index 5d5630f46..cc5f5b764 100644
--- a/kmath-ast/README.md
+++ b/kmath-ast/README.md
@@ -199,10 +199,7 @@ public fun main() {
Result LaTeX:
-
-
-![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
-
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\,x\right)}{2\times10^{10}%2Bx^{3}}}{12}+x^{2/3}$$
Result MathML (can be used with MathJax or other renderers):
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index e9e22f4d4..1f14deaba 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -170,10 +170,7 @@ public fun main() {
Result LaTeX:
-
-
-![](https://latex.codecogs.com/gif.latex?%5Coperatorname{exp}%5C,%5Cleft(%5Csqrt{x}%5Cright)-%5Cfrac{%5Cfrac{%5Coperatorname{arcsin}%5C,%5Cleft(2%5C,x%5Cright)}{2%5Ctimes10^{10}%2Bx^{3}}}{12}+x^{2/3})
-
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\,x\right)}{2\times10^{10}%2Bx^{3}}}{12}+x^{2/3}$$
Result MathML (can be used with MathJax or other renderers):
--
2.34.1
From b92ef23f5d3ec2c2578abc6f8734a12cd837bbf9 Mon Sep 17 00:00:00 2001
From: Gleb Minaev <43728100+lounres@users.noreply.github.com>
Date: Sat, 11 Jun 2022 00:06:45 +0300
Subject: [PATCH 2/3] Some fixes
---
kmath-ast/README.md | 2 +-
kmath-ast/docs/README-TEMPLATE.md | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/kmath-ast/README.md b/kmath-ast/README.md
index cc5f5b764..553c60bb3 100644
--- a/kmath-ast/README.md
+++ b/kmath-ast/README.md
@@ -199,7 +199,7 @@ public fun main() {
Result LaTeX:
-$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\,x\right)}{2\times10^{10}%2Bx^{3}}}{12}+x^{2/3}$$
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\\,x\right)}{2\times10^{10}+x^{3}}}{12}+x^{2/3}$$
Result MathML (can be used with MathJax or other renderers):
diff --git a/kmath-ast/docs/README-TEMPLATE.md b/kmath-ast/docs/README-TEMPLATE.md
index 1f14deaba..96bbfbf5a 100644
--- a/kmath-ast/docs/README-TEMPLATE.md
+++ b/kmath-ast/docs/README-TEMPLATE.md
@@ -170,7 +170,7 @@ public fun main() {
Result LaTeX:
-$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\,x\right)}{2\times10^{10}%2Bx^{3}}}{12}+x^{2/3}$$
+$$\operatorname{exp}\\,\left(\sqrt{x}\right)-\frac{\frac{\operatorname{arcsin}\\,\left(2\\,x\right)}{2\times10^{10}+x^{3}}}{12}+x^{2/3}$$
Result MathML (can be used with MathJax or other renderers):
--
2.34.1
From b09127f090c2b9df283eae2bbf689a38bae9b8f0 Mon Sep 17 00:00:00 2001
From: Iaroslav Postovalov
Date: Tue, 14 Jun 2022 22:52:47 +0700
Subject: [PATCH 3/3] Fix name clash in strict mode; replace eval with new
Function
---
.../space/kscience/kmath/estree/estree.kt | 2 +-
.../kmath/estree/internal/ESTreeBuilder.kt | 28 +++++++------------
.../internal/estree/estree.extensions.kt | 5 +++-
3 files changed, 15 insertions(+), 20 deletions(-)
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
index a8b1aa2e1..240acf1d7 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/estree.kt
@@ -36,7 +36,7 @@ public fun MST.compileToExpression(algebra: Algebra): Expression
)
}
- return ESTreeBuilder { visit(typed) }.instance
+ return ESTreeBuilder { visit(typed) }.instance
}
/**
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
index 40c446da0..16a59d273 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/estree/internal/ESTreeBuilder.kt
@@ -14,37 +14,28 @@ internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExp
private class GeneratedExpression(val executable: dynamic, val constants: Array) : Expression {
@Suppress("UNUSED_VARIABLE")
override fun invoke(arguments: Map): T {
- //val e = executable
- //val c = constants
+ val e = executable
+ val c = constants
val a = js("{}")
arguments.forEach { (key, value) -> a[key.identity] = value }
- return executable.call(constants, a).unsafeCast()
- //return js("e(c, a)").unsafeCast()
+ return js("e(c, a)").unsafeCast()
}
}
+ @Suppress("UNUSED_VARIABLE")
val instance: Expression by lazy {
val node = Program(
sourceType = "script",
- VariableDeclaration(
- kind = "var",
- VariableDeclarator(
- id = Identifier("executable"),
- init = FunctionExpression(
- params = arrayOf(Identifier("constants"), Identifier("arguments")),
- body = BlockStatement(ReturnStatement(bodyCallback())),
- ),
- ),
- ),
+ ReturnStatement(bodyCallback())
)
- eval(generate(node))
- GeneratedExpression(js("executable"), constants.toTypedArray())
+ val code = generate(node)
+ GeneratedExpression(js("new Function('constants', 'arguments_0', code)"), constants.toTypedArray())
}
private val constants = mutableListOf()
- fun constant(value: Any?) = when {
+ fun constant(value: Any?): BaseExpression = when {
value == null || jsTypeOf(value) == "number" || jsTypeOf(value) == "string" || jsTypeOf(value) == "boolean" ->
SimpleLiteral(value)
@@ -62,7 +53,8 @@ internal class ESTreeBuilder(val bodyCallback: ESTreeBuilder.() -> BaseExp
}
}
- fun variable(name: Symbol): BaseExpression = call(getOrFail, Identifier("arguments"), SimpleLiteral(name.identity))
+ fun variable(name: Symbol): BaseExpression =
+ call(getOrFail, Identifier("arguments_0"), SimpleLiteral(name.identity))
fun call(function: Function, vararg args: BaseExpression): BaseExpression = SimpleCallExpression(
optional = false,
diff --git a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
index 3aa31f921..5b1ce914e 100644
--- a/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
+++ b/kmath-ast/src/jsMain/kotlin/space/kscience/kmath/internal/estree/estree.extensions.kt
@@ -3,6 +3,8 @@
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
+@file:Suppress("unused")
+
package space.kscience.kmath.internal.estree
internal fun Program(sourceType: String, vararg body: dynamic) = object : Program {
@@ -28,9 +30,10 @@ internal fun Identifier(name: String) = object : Identifier {
override var name = name
}
-internal fun FunctionExpression(params: Array, body: BlockStatement) = object : FunctionExpression {
+internal fun FunctionExpression(id: Identifier?, params: Array, body: BlockStatement) = object : FunctionExpression {
override var params = params
override var type = "FunctionExpression"
+ override var id: Identifier? = id
override var body = body
}
--
2.34.1