Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhirkevich Alexander Y authored and Zhirkevich Alexander Y committed Oct 17, 2024
1 parent d60feb2 commit 192b14a
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,24 @@ internal fun OpNot(



internal val OpGreaterComparator : (Comparable<*>, Comparable<*>, ScriptRuntime) -> Boolean = { a, b, _ ->
internal val OpGreaterComparator : (Comparable<*>, Comparable<*>, ScriptRuntime) -> Boolean = { a, b, r ->

if (a is Number && b is Number) {
a.toDouble() > b.toDouble()
val ka = r.toKotlin(a)
val kb = r.toKotlin(b)

if (ka is Number || kb is Number) {
r.toNumber(a).toDouble() > r.toNumber(b).toDouble()
} else {
a.toString() > b.toString()
}
}

internal val OpLessComparator : (Comparable<*>, Comparable<*>, ScriptRuntime) -> Boolean = { a, b, _ ->
if (a is Number && b is Number) {
a.toDouble() < b.toDouble()
internal val OpLessComparator : (Comparable<*>, Comparable<*>, ScriptRuntime) -> Boolean = { a, b, r ->
val ka = r.toKotlin(a)
val kb = r.toKotlin(b)

if (ka is Number || kb is Number) {
r.toNumber(a).toDouble() < r.toNumber(b).toDouble()
} else {
a.toString() < b.toString()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ internal class ESInterpreterImpl(
parseExpressionOp(
context = context,
blockContext = blockContext,
isExpressionStart = isExpressionStart
isExpressionStart = isExpressionStart,
factorOnly = unaryOnly
)
} else {
OpGetVariable(variableName, receiver = null)
Expand Down Expand Up @@ -317,7 +318,7 @@ internal class ESInterpreterImpl(

eatSequence("=>") -> OpConstant(parseArrowFunction(listOf(x), blockContext))

eat('=') -> {
eatAndExpectNot('=', '='::equals) -> {
checkAssignment()
parseAssignmentValue(x, null)
}
Expand Down Expand Up @@ -406,10 +407,14 @@ internal class ESInterpreterImpl(
context: Expression,
logicalContext: LogicalContext? = null,
blockContext: List<BlockContext>,
isExpressionStart: Boolean = false
isExpressionStart: Boolean = false,
factorOnly : Boolean = false
): Expression {
var x = parseOperator3(context, logicalContext, blockContext, isExpressionStart)

var x = if (factorOnly) {
parseFactorOp(context, blockContext, isExpressionStart)
} else {
parseOperator3(context, logicalContext, blockContext, isExpressionStart)
}
while (true){
prepareNextChar()
x = when {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,16 @@ private tailrec fun Any?.numberOrNull(withNaNs : Boolean = true) : Number? = whe
null -> 0L
true -> 1L
false -> 0L
is JsString -> if (withNaNs) value.numberOrNull(withNaNs) else null
is JsArray -> if (withNaNs) value.numberOrNull(withNaNs) else null
is JsWrapper<*> -> value.numberOrNull()


is CharSequence -> when {
isEmpty() -> 0L
withNaNs -> {
val s = trim().toString()
s.toLongOrNull() ?: s.toDoubleOrNull()
}
else -> null
}
is Byte -> toLong()
is UByte -> toLong()
is Short -> toLong()
Expand All @@ -234,17 +241,14 @@ private tailrec fun Any?.numberOrNull(withNaNs : Boolean = true) : Number? = whe
is Float -> toDouble()
is Long -> this
is Double -> this
is String -> if (withNaNs) {
val t = trim()
t.toLongOrNull() ?: t.toDoubleOrNull()
} else null
is List<*> -> {
if (withNaNs) {
singleOrNull()?.numberOrNull(withNaNs)
} else{
null
}
}
is JsWrapper<*> -> value.numberOrNull()
else -> null
}

Expand Down
22 changes: 22 additions & 0 deletions skriptie/src/commonTest/kotlin/js/ComparisonTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package js

import kotlin.test.Test

class ComparisonTest {

@Test
fun number_string(){
"'1' > 2".eval().assertEqualsTo(false)
"'1' == 1".eval().assertEqualsTo(true)
"'1' === 1".eval().assertEqualsTo(false)

"'test' == 0".eval().assertEqualsTo(false)
"'test' == NaN".eval().assertEqualsTo(false)

"'00100' < '1'".eval().assertEqualsTo(true)
"'00100' < 1".eval().assertEqualsTo(false)

" +'2' > +'10'".eval().assertEqualsTo(false)
"'2' > '10'".eval().assertEqualsTo(true)
}
}
4 changes: 3 additions & 1 deletion skriptie/src/commonTest/kotlin/js/SyntaxTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,10 @@ class SyntaxTest {
"typeof null".eval().assertEqualsTo("object")
"typeof undefined".eval().assertEqualsTo("undefined")

"typeof 1===1".eval().assertEqualsTo("boolean")
"typeof 1===1".eval().assertEqualsTo(false)
"typeof 1>2".eval().assertEqualsTo(false)

"let x = 1; typeof ++x".eval().assertEqualsTo("number")
"let x = 1; typeof x++".eval().assertEqualsTo("number")
assertFailsWith<SyntaxError> {
"let x = 1; typeof x = 2".eval()
Expand Down

0 comments on commit 192b14a

Please sign in to comment.