Skip to content

Commit 6247919

Browse files
committed
A little bit of magic and we have strings
1 parent 49fa6d2 commit 6247919

3 files changed

Lines changed: 53 additions & 3 deletions

File tree

usvm-ts/src/main/kotlin/org/usvm/machine/expr/TsExprResolver.kt

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ import org.usvm.util.throwExceptionWithoutStackFrameDrop
106106

107107
private val logger = KotlinLogging.logger {}
108108

109+
const val ADHOC_STRING = 777777.0 // arbitrary string
110+
const val ADHOC_STRING__NUMBER = 55555.0 // 'number'
111+
const val ADHOC_STRING__STRING = 2222.0 // 'string'
112+
109113
class TsExprResolver(
110114
private val ctx: TsContext,
111115
private val scope: TsStepScope,
@@ -252,7 +256,16 @@ class TsExprResolver(
252256
error("Not supported $expr")
253257
}
254258

255-
override fun visit(expr: EtsTypeOfExpr): UExpr<out USort>? {
259+
override fun visit(expr: EtsTypeOfExpr): UExpr<out USort>? = with(ctx) {
260+
val arg = resolve(expr.arg) ?: return null
261+
262+
if (arg.sort == fp64Sort) {
263+
if (arg == mkFp64(ADHOC_STRING)) {
264+
return mkFp64(ADHOC_STRING__STRING)
265+
}
266+
return mkFp64(ADHOC_STRING__NUMBER) // 'number'
267+
}
268+
256269
logger.warn { "visit(${expr::class.simpleName}) is not implemented yet" }
257270
error("Not supported $expr")
258271
}
@@ -834,8 +847,11 @@ class TsSimpleValueResolver(
834847
}
835848

836849
override fun visit(value: EtsStringConstant): UExpr<out USort> = with(ctx) {
837-
logger.warn { "visit(${value::class.simpleName}) is not implemented yet" }
838-
error("Not supported $value")
850+
return when (value.value) {
851+
"number" -> mkFp64(ADHOC_STRING__NUMBER)
852+
"string" -> mkFp64(ADHOC_STRING__STRING)
853+
else -> mkFp64(ADHOC_STRING)
854+
}
839855
}
840856

841857
override fun visit(value: EtsBooleanConstant): UExpr<out USort> = with(ctx) {

usvm-ts/src/test/kotlin/org/usvm/samples/Call.kt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,24 @@ class Call : TsMethodTestRunner() {
135135
{ r -> r.number == 2.0 },
136136
)
137137
}
138+
139+
@Test
140+
fun `test overloading number`() {
141+
val method = getMethod(className, "callOverloadedNumber")
142+
discoverProperties<TsValue.TsNumber>(
143+
method = method,
144+
{ r -> r.number == 1.0 }
145+
)
146+
}
147+
148+
@Test
149+
fun `test overloading string`() {
150+
val method = getMethod(className, "callOverloadedString")
151+
discoverProperties<TsValue.TsNumber>(
152+
method = method,
153+
{ r -> r.number == 2.0 }
154+
)
155+
}
138156
}
139157

140158
fun fib(n: Double): Double {

usvm-ts/src/test/resources/samples/Call.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ class Call {
7575
callExtra(): number {
7676
return this.g(5, 10, 20) // 2
7777
}
78+
79+
overloaded(a: number): number;
80+
overloaded(a: string): number;
81+
overloaded(a: any): number {
82+
if (typeof a === 'number') return 1
83+
if (typeof a === 'string') return 2
84+
return -1
85+
}
86+
87+
callOverloadedNumber(): number {
88+
return this.overloaded(5);
89+
}
90+
91+
callOverloadedString(): number {
92+
return this.overloaded("test");
93+
}
7894
}
7995

8096
class A {

0 commit comments

Comments
 (0)