Merge 'feat(wasm): add get and iterate func' from Jean Arhancet

Add `get` and `iterate` functions to the wasm module

Closes #421
This commit is contained in:
Pekka Enberg 2024-12-09 17:28:43 +02:00
commit 36f9565910
2 changed files with 61 additions and 0 deletions

View file

@ -61,6 +61,21 @@ impl Statement {
self
}
pub fn get(&self) -> JsValue {
match self.inner.borrow_mut().step() {
Ok(limbo_core::RowResult::Row(row)) => {
let row_array = js_sys::Array::new();
for value in row.values {
let value = to_js_value(value);
row_array.push(&value);
}
JsValue::from(row_array)
}
Ok(limbo_core::RowResult::IO) | Ok(limbo_core::RowResult::Done) => JsValue::UNDEFINED,
Err(e) => panic!("Error: {:?}", e),
}
}
pub fn all(&self) -> js_sys::Array {
let array = js_sys::Array::new();
loop {
@ -80,6 +95,18 @@ impl Statement {
}
array
}
pub fn iterate(&self) -> JsValue {
let all = self.all();
let iterator_fn = js_sys::Reflect::get(&all, &js_sys::Symbol::iterator())
.expect("Failed to get iterator function")
.dyn_into::<js_sys::Function>()
.expect("Symbol.iterator is not a function");
iterator_fn
.call0(&all)
.expect("Failed to call iterator function")
}
}
fn to_js_value(value: limbo_core::Value) -> JsValue {