Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions ffi/runtime/rt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,29 @@ class __Writer {
held.add(v)
}

// Whether each of [held] is one [moved] encoded, up to the last it did.
private var moves: BooleanArray? = null

/** An object's handle whose value the call takes, as [handle] encodes it, which [abandon] closes. */
fun moved(v: __Handle?) {
handle(v)
if (v == null) return
val at = held!!.size - 1
var moves = moves ?: BooleanArray(slots.size)
if (moves.size <= at) moves = moves.copyOf(slots.size)
moves[at] = true
this.moves = moves
}

/**
* Abandon each handle [moved] encoded, once the call took their values: those encoded,
* not those the value holds now, as a callback may have changed it.
*/
fun abandon() {
val moves = moves ?: return
held?.forEachIndexed { i, h -> if (i < moves.size && moves[i]) h.abandon() }
}

/**
* The encoding, then where [refs] names any, the place in it of each text, in order, and
* how many, which Rust reads first.
Expand Down
60 changes: 22 additions & 38 deletions ffi/src/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -899,6 +899,12 @@ impl<'a> Gen<'a> {

/// Kotlin statements writing the Kotlin value `e` of type `ty` into the writer `w`.
fn write_value(&mut self, ty: &Type, w: &str, e: &str) -> String {
self.encode(ty, w, e, false)
}

/// [`Self::write_value`], marking in `w` each handle whose value the call takes if `moves`,
/// for `{w}.abandon()` to close those once it returns, not whatever `e` holds by then.
fn encode(&mut self, ty: &Type, w: &str, e: &str, moves: bool) -> String {
let abi = self.abi;
match ty {
Type::Bool => format!("{w}.bool({e})"),
Expand All @@ -924,36 +930,40 @@ impl<'a> Gen<'a> {
Type::List(..) if let Some(inner) = ty.block() => format!("{w}.block({})", block(inner).signed(e)),
Type::List(_, inner) => {
let (l, v) = (self.fresh("l"), self.fresh("v"));
let item = self.write_value(inner, w, &v);
let item = self.encode(inner, w, &v, moves);
format!("{e}.let {{ {l} -> {w}.i32({l}.size); for ({v} in {l}) {{ {item} }} }}")
}
Type::Option(inner) => match inner.base() {
Type::Object(_) if moves && moved(abi, inner) => format!("{w}.moved({e}?.handle)"),
Type::Object(_) => format!("{w}.handle({e}?.handle)"),
base if Abi::is_buf(base) => self.write_value(base, w, e),
base if Abi::is_buf(base) => self.encode(base, w, e, moves),
// None is a length of -1.
Type::List(..) => {
let v = self.fresh("v");
let some = self.write_value(inner, w, &v);
let some = self.encode(inner, w, &v, moves);
format!("{e}.let {{ {v} -> if ({v} == null) {w}.i32(-1) else {some} }}")
}
_ => {
let v = self.fresh("v");
let some = self.write_value(inner, w, &v);
let some = self.encode(inner, w, &v, moves);
format!("{e}.let {{ {v} -> if ({v} == null) {w}.bool(false) else {{ {w}.bool(true); {some} }} }}")
}
},
Type::Box(inner) | Type::Ref(_, inner) => self.write_value(inner, w, e),
Type::Box(inner) => self.encode(inner, w, e, moves),
// Borrowed, nothing in it moves.
Type::Ref(_, inner) => self.write_value(inner, w, e),
Type::Record(i) => format!("w_{}({w}, {e})", abi.record(*i)),
Type::Enum(i) => match abi.api.enums[*i].plain() {
true => format!("{w}.i32({e}.value)"),
false => format!("w_{}({w}, {e})", abi.enumeration(*i)),
},
Type::Object(_) if moves && moved(abi, ty) => format!("{w}.moved({e}.handle)"),
Type::Object(_) => format!("{w}.handle({e}.handle)"),
Type::Range(_) | Type::Bound(_) | Type::Bounds(_) => format!("w_{}({w}, {e})", abi.range(ty)),
Type::Tuple(elements) => {
let t = self.fresh("t");
let writes: Vec<String> = (elements.iter().enumerate())
.map(|(k, ty)| self.write_value(ty, w, &format!("{t}.{}", ordinal(k))))
.map(|(k, ty)| self.encode(ty, w, &format!("{t}.{}", ordinal(k)), moves))
.collect();
format!("{e}.let {{ {t} -> {} }}", writes.join("; "))
}
Expand Down Expand Up @@ -1317,13 +1327,14 @@ impl<'a> Gen<'a> {
}
Ty::U128 | Ty::Duration | Ty::SocketAddr(_) | Ty::IpAddr(_) | Ty::Struct(_) | Ty::In(_) => {
let w = self.fresh("w");
// Taken by value, each object in a list moves out, as one passed alone does.
if moved(abi, ty) {
let close = self.close(ty, e);
arg.after.push(close);
// Taken by value, each object in a list moves out, as one passed alone does: the
// writer closes those it encoded, as a callback may change the list meanwhile.
let moves = moved(abi, ty);
if moves {
arg.after.push(format!("{w}.abandon()"));
}
arg.open = Some(format!("_Native.__Writer().use {{ {w} ->"));
let value = self.write_value(ty, &w, e);
let value = self.encode(ty, &w, e, moves);
arg.inside.push(value);
// Then the arrays and text it names, which Rust copies from once.
arg.jni.push("jobjectArray");
Expand Down Expand Up @@ -3548,33 +3559,6 @@ impl<'a> Gen<'a> {
Some(indent_by(&code, indent_level))
}

/// The Kotlin statement closing each handle the value `e` of type `ty` holds, once a
/// call moved their values out.
fn close(&mut self, ty: &Type, e: &str) -> String {
match ty {
Type::Option(inner) => {
let v = self.fresh("v");
let close = self.close(inner, &v);
format!("{e}?.let {{ {v} -> {close} }}")
}
Type::Box(inner) => self.close(inner, e),
Type::Tuple(elements) => {
let t = self.fresh("t");
let closes: Vec<String> = (elements.iter().enumerate())
.filter(|(_, ty)| moved(self.abi, ty))
.map(|(k, ty)| self.close(ty, &format!("{t}.{}", ordinal(k))))
.collect();
format!("{e}.let {{ {t} -> {} }}", closes.join("; "))
}
Type::List(_, inner) => {
let v = self.fresh("v");
let close = self.close(inner, &v);
format!("{e}.forEach {{ {v} -> {close} }}")
}
_ => format!("{e}.handle.abandon()"),
}
}

/// The glue's out values for `outs`, of a function returning `ok` or `err`, the
/// pointers the C call takes after its parameters, and the statements writing them,
/// `ok` saying whether it returned ok.
Expand Down
25 changes: 25 additions & 0 deletions ffi/tests/api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1014,6 +1014,31 @@ fun unwinds() {
Counter.new("zero", 0u).use { counter -> check(rejects { logCounts(failing, 0u, counter, more) }, "a callback's failed check unwinds a call that moved its arguments") }
val gone = runCatching { more[0].get() }.exceptionOrNull()
check(gone is IllegalStateException && gone.message == "Counter is closed", "what it moved is closed in Kotlin: $gone")
// A callback replacing what the call moved out of its list: the call closes what it took.
val taken = Counter.new("taken", 2u)
val placed = Counter.new("placed", 3u)
val changed = mutableListOf(taken)
val swapping = object : Log {
override fun log(level: UByte, line: String): Boolean {
changed[0] = placed
return true
}
}
Counter.new("zero", 0u).use { counter -> check(logCounts(swapping, 0u, counter, changed) == 2u, "a callback may change a list the call moved out of") }
check(runCatching { taken.get() }.exceptionOrNull()?.message == "Counter is closed", "what the call moved is closed")
check(placed.get().toUInt() == 3u, "what the callback put in its place stays open")
changed[0] = Counter.new("failing", 4u)
val replaced = changed[0]
val throwing = object : Log {
override fun log(level: UByte, line: String): Boolean {
changed[0] = placed
return lettered { 0xD800 } == 0
}
}
Counter.new("zero", 0u).use { counter -> check(rejects { logCounts(throwing, 0u, counter, changed) }, "a failing callback unwinds a call that moved its list") }
check(runCatching { replaced.get() }.exceptionOrNull()?.message == "Counter is closed", "what an unwound call moved is closed")
check(placed.get().toUInt() == 3u, "what the callback put in its place stays open after it unwinds")
placed.close()
val tag = Tag.new()
check(tag.tagged(bumps) { 'a'.code } == "a" && rejects { tag.tagged(bumps) { 0xD800 } } && bumps.value == 4u, "a borrowed result's call writes back too: ${bumps.value}")
check(tag.tagged(bumps) { 'b'.code } == "ab", "the receiver is whole after")
Expand Down
27 changes: 0 additions & 27 deletions quest/m1/ffi-export/kotlin-moved-snapshot.md

This file was deleted.