Skip to content

Commit 2005e4f

Browse files
committed
fix null cast expection
1 parent 86c2359 commit 2005e4f

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

substrata-kotlin/src/main/java/com/segment/analytics/substrata/kotlin/Conversions.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,29 @@ internal inline fun <reified T> JSConvertible.isTypeOf() = when(T::class) {
2525
JSArray::class -> context.isArray(ref)
2626
JSObject::class -> context.isObject(ref)
2727
JSFunction::class -> context.isFunction(ref)
28+
JSException::class -> true
2829
else -> false
2930
}
3031

3132
internal inline fun <reified T> JSConvertible.cast() =
3233
if (isTypeOf<T>()) wrap<T>()
33-
else null
34+
else throw Exception("Failed to cast JSConvertible. Unknown type is passed")
3435

35-
fun JSConvertible.asString(): String? = cast()
36+
fun JSConvertible.asString(): String = cast()
3637

37-
fun JSConvertible.asBoolean(): Boolean? = cast()
38+
fun JSConvertible.asBoolean(): Boolean = cast()
3839

39-
fun JSConvertible.asInt(): Int? = cast()
40+
fun JSConvertible.asInt(): Int = cast()
4041

41-
fun JSConvertible.asDouble(): Double? = cast()
42+
fun JSConvertible.asDouble(): Double = cast()
4243

43-
fun JSConvertible.asJSArray(): JSArray? = cast()
44+
fun JSConvertible.asJSArray(): JSArray= cast()
4445

45-
fun JSConvertible.asJSObject(): JSObject? = cast()
46+
fun JSConvertible.asJSObject(): JSObject = cast()
4647

47-
fun JSConvertible.asJSFunction(): JSFunction? = cast()
48+
fun JSConvertible.asJSFunction(): JSFunction = cast()
4849

49-
fun JSConvertible.asJSException(): JSException? = wrap()
50+
fun JSConvertible.asJSException(): JSException = wrap()
5051

5152
fun String.toJSValue(context: JSContext) = context.newJSValue(this)
5253

substrata-kotlin/src/main/java/com/segment/analytics/substrata/kotlin/JSContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ class JSContext(
8484

8585
fun getAny(ref: Long) = getAny(JSValue(ref, this))
8686

87-
fun getAny(value: JSValue): Any? {
87+
fun getAny(value: JSValue): Any {
8888
val type = QuickJS.getType(value.ref)
8989
return when (type) {
9090
QuickJS.TYPE_STRING -> value.asString()
@@ -103,7 +103,7 @@ class JSContext(
103103
}
104104
}
105105
QuickJS.TYPE_EXCEPTION -> value.asJSException()
106-
QuickJS.TYPE_NULL -> null
106+
QuickJS.TYPE_NULL -> JSNull
107107
QuickJS.TYPE_UNDEFINED -> JSUndefined
108108
// QuickJS.TYPE_EXCEPTION -> getExecption()
109109
else -> throw Exception("Property type is undefined")

0 commit comments

Comments
 (0)