Skip to content

Commit

Permalink
luau: refactor to convert table.pairs() to faster table.for_each() …
Browse files Browse the repository at this point in the history
…introduced in mlua 0.9.2
  • Loading branch information
jqnatividad committed Nov 22, 2023
1 parent 83066a6 commit 444d91b
Showing 1 changed file with 18 additions and 27 deletions.
45 changes: 18 additions & 27 deletions src/cmd/luau.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1274,39 +1274,30 @@ fn map_computedvalue(
record.clear();
}
let mut columns_inserted = 0_u8;
for pair in table.clone().pairs::<mlua::Value, mlua::Value>() {
// we don't care about the key, just the value
let (_k, v) = pair?;
let mut ibuffer = itoa::Buffer::new();
let mut nbuffer = ryu::Buffer::new();
table.for_each::<String, Value>(|_k, v| {
match v {
Value::Integer(intval) => {
let mut buffer = itoa::Buffer::new();
record.push_field(buffer.format(intval));
},
Value::String(strval) => {
record.push_field(&strval.to_string_lossy());
},
Value::Number(number) => {
let mut buffer = ryu::Buffer::new();
record.push_field(buffer.format(number));
},
Value::Boolean(boolean) => {
record.push_field(if boolean { "true" } else { "false" });
},
Value::Nil => {
record.push_field("");
},
_ => {
return fail_clierror!(
"Unexpected value type returned by provided Luau expression. {v:?}"
);
},
Value::Integer(intval) =>
record.push_field(ibuffer.format(intval)),
Value::String(strval) =>
record.push_field(&strval.to_string_lossy()),
Value::Number(number) =>
record.push_field(nbuffer.format(number)),
Value::Boolean(boolean) =>
record.push_field(if boolean { "true" } else { "false" }),
Value::Nil =>
record.push_field(""),
_ => unreachable!("unsupported value type"),
}
columns_inserted += 1;
if new_column_count > 0 && columns_inserted >= new_column_count {
// we ignore table values more than the number of new columns defined
break;
return Ok(());
}
}
Ok(())
})?;

// on the other hand, if there are less table values than expected
// we fill it up with empty fields
while new_column_count > 0 && columns_inserted < new_column_count {
Expand Down

0 comments on commit 444d91b

Please sign in to comment.