Copy out returned string (and free it Rust-internal)

This commit is contained in:
Jan-Erik Rediger 2019-05-15 17:13:17 +02:00
Родитель 88001cc3fc
Коммит 15913f906b
3 изменённых файлов: 32 добавлений и 30 удалений

Просмотреть файл

@ -9,6 +9,7 @@ import com.sun.jna.StringArray
import mozilla.telemetry.glean.Glean
import mozilla.telemetry.glean.rust.LibGleanFFI
import mozilla.telemetry.glean.rust.toByte
import mozilla.telemetry.glean.rust.getAndConsumeRustString
import mozilla.telemetry.glean.Dispatchers
// import mozilla.components.service.glean.storages.StringsStorageEngine
@ -84,11 +85,10 @@ class StringMetricType(
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun testHasValue(pingName: String = sendInPings.first()): Boolean {
/*@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()
Dispatchers.API.assertInTestingMode()*/
return StringsStorageEngine.getSnapshot(pingName, false)?.get(identifier) != null*/
assert(false, { "Testing API not implementated for StringMetricType" })
return false
val res = LibGleanFFI.INSTANCE.glean_string_test_has_value(Glean.handle, this.handle, pingName)
return res != 0.toByte()
}
/**
@ -104,10 +104,12 @@ class StringMetricType(
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
fun testGetValue(pingName: String = sendInPings.first()): String {
/*@Suppress("EXPERIMENTAL_API_USAGE")
Dispatchers.API.assertInTestingMode()
Dispatchers.API.assertInTestingMode()*/
return StringsStorageEngine.getSnapshot(pingName, false)!![identifier]!!*/
assert(false, { "Testing API not implementated for StringMetricType" })
return "asd"
if (!testHasValue(pingName)) {
throw NullPointerException()
}
val ptr = LibGleanFFI.INSTANCE.glean_string_test_get_value(Glean.handle, this.handle, pingName)!!
return ptr.getAndConsumeRustString()
}
}

Просмотреть файл

@ -15,6 +15,28 @@ import java.lang.reflect.Proxy
// Turn a boolean into its Byte (u8) representation
internal fun Boolean.toByte(): Byte = if (this) 1 else 0
/**
* Helper to read a null terminated String out of the Pointer and free it.
*
* Important: Do not use this pointer after this! For anything!
*/
internal fun Pointer.getAndConsumeRustString(): String {
try {
return this.getRustString()
} finally {
LibGleanFFI.INSTANCE.glean_str_free(this)
}
}
/**
* Helper to read a null terminated string out of the pointer.
*
* Important: doesn't free the pointer, use [getAndConsumeRustString] for that!
*/
internal fun Pointer.getRustString(): String {
return this.getString(0, "utf8")
}
@Suppress("TooManyFunctions")
internal interface LibGleanFFI : Library {
companion object {

Просмотреть файл

@ -67,25 +67,3 @@ internal open class RustError : Structure() {
return Arrays.asList("code", "message")
}
}
/**
* Helper to read a null terminated String out of the Pointer and free it.
*
* Important: Do not use this pointer after this! For anything!
*/
internal fun Pointer.getAndConsumeRustString(): String {
try {
return this.getRustString()
} finally {
LibGleanFFI.INSTANCE.glean_str_free(this)
}
}
/**
* Helper to read a null terminated string out of the pointer.
*
* Important: doesn't free the pointer, use [getAndConsumeRustString] for that!
*/
internal fun Pointer.getRustString(): String {
return this.getString(0, "utf8")
}