Skip to content

Commit 7799ed3

Browse files
authored
Fix null exception when null returned from js function (#13)
* fix null cast expection * make it backward compatible
1 parent 86c2359 commit 7799ed3

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ class JSContext(
3434

3535
fun evaluate(script: String): Any? {
3636
val ret = QuickJS.evaluate(contextRef, script, QuickJS.EVALUATOR, QuickJS.EVAL_TYPE_GLOBAL)
37-
return getAny(ret)
37+
val result = getAny(ret)
38+
return if (result == JSNull) null else result
3839
}
3940

4041
// fun executeFunction(function: String, params: JSArray?): Any {
@@ -84,7 +85,7 @@ class JSContext(
8485

8586
fun getAny(ref: Long) = getAny(JSValue(ref, this))
8687

87-
fun getAny(value: JSValue): Any? {
88+
fun getAny(value: JSValue): Any {
8889
val type = QuickJS.getType(value.ref)
8990
return when (type) {
9091
QuickJS.TYPE_STRING -> value.asString()
@@ -103,7 +104,7 @@ class JSContext(
103104
}
104105
}
105106
QuickJS.TYPE_EXCEPTION -> value.asJSException()
106-
QuickJS.TYPE_NULL -> null
107+
QuickJS.TYPE_NULL -> JSNull
107108
QuickJS.TYPE_UNDEFINED -> JSUndefined
108109
// QuickJS.TYPE_EXCEPTION -> getExecption()
109110
else -> throw Exception("Property type is undefined")

0 commit comments

Comments
 (0)