Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

3.7 KB
Raw Blame
use super::*;
use crate::webidl;

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "IDBCursor.advance")]
struct IdbCursorAdvanceArgs {
    #[webidl(required, converter = "enforce_range_unsigned_long")]
    count: u32,
}

#[derive(webidl::WebIdlArgs)]
#[webidl(prefix = "IDBCursor.continue")]
struct IdbCursorContinueArgs<'s> {
    #[webidl(converter = "raw")]
    key: Option<v8::Local<'s, v8::Value>>,
}

pub(in crate::context_bootstrap::indexed_db) fn idb_cursor_continue_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'s, v8::Value>,
) {
    let Some(parsed) = webidl::parse_args::<IdbCursorContinueArgs<'s>>(scope, &args) else {
        return;
    };
    let cursor = args.this();
    let current = cursor_position(scope, cursor);
    if current < 0 {
        let error = dom_exception_value(scope, "The cursor is exhausted.", "InvalidStateError");
        scope.throw_exception(error);
        return;
    }
    let key = parsed.key.unwrap_or_else(|| v8::undefined(scope).into());
    let target = match parse_idb_key(scope, key) {
        Ok(key) => key,
        Err(_) => {
            let error = dom_exception_value(
                scope,
                "Failed to execute 'continue': the key is not valid.",
                "DataError",
            );
            scope.throw_exception(error);
            return;
        }
    };
    let direction = cursor_direction_from_cursor(scope, cursor);
    if let Some(target) = &target
        && let Some(current_key) = cursor_key_at(scope, cursor, current as usize)
        && compare::cursor_direction_cmp(direction, target, &current_key)
            != std::cmp::Ordering::Greater
    {
        let error = dom_exception_value(
            scope,
            "Failed to execute 'continue': the key is not greater than the cursor position.",
            "DataError",
        );
        scope.throw_exception(error);
        return;
    }
    let next = next_cursor_position(scope, cursor, current as usize, target.as_ref(), &direction);
    let _ = result::enqueue_cursor_result(scope, cursor, next);
    rv.set_undefined();
}

pub(in crate::context_bootstrap::indexed_db) fn idb_cursor_advance_callback<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    args: v8::FunctionCallbackArguments<'s>,
    mut rv: v8::ReturnValue<'s, v8::Value>,
) {
    let Some(parsed) = webidl::parse_args::<IdbCursorAdvanceArgs>(scope, &args) else {
        return;
    };
    let cursor = args.this();
    let current = cursor_position(scope, cursor);
    if current < 0 {
        let error = dom_exception_value(scope, "The cursor is exhausted.", "InvalidStateError");
        scope.throw_exception(error);
        return;
    }
    let count = parsed.count;
    if count == 0 {
        throw_type_error(
            scope,
            "Failed to execute 'advance': count must be greater than zero.",
        );
        return;
    }
    let next = current as usize + count as usize;
    let next = (next < cursor_entries_len(scope, cursor)).then_some(next);
    let _ = result::enqueue_cursor_result(scope, cursor, next);
    rv.set_undefined();
}

fn next_cursor_position<'s>(
    scope: &mut v8::PinScope<'s, '_>,
    cursor: v8::Local<'s, v8::Object>,
    current: usize,
    target: Option<&Key>,
    direction: &CursorDirection,
) -> Option<usize> {
    for index in (current + 1)..cursor_entries_len(scope, cursor) {
        let Some(candidate_key) = cursor_key_at(scope, cursor, index) else {
            continue;
        };
        if let Some(target) = target {
            let cmp = compare::cursor_direction_cmp(*direction, &candidate_key, target);
            if cmp == std::cmp::Ordering::Less {
                continue;
            }
        }
        return Some(index);
    }
    None
}