mirror of
https://github.com/lexmount/moli.git
synced 2026-11-23 08:02:14 +00:00
206 lines
6.8 KiB
Rust
206 lines
6.8 KiB
Rust
use crate::interfaces as web_api_interfaces;
|
|
use moli_v8_util::{get_private_value, set_private_value};
|
|
use moli_webapi_declare::{WebApiFunctionTemplate, WebApiObject};
|
|
use moli_webidl as webidl;
|
|
|
|
const FILE_LIST_LENGTH_SLOT: &str = "__lmFileListLength";
|
|
const FILE_LIST_FILES_SLOT: &str = "__lmFileListFiles";
|
|
|
|
#[derive(WebApiObject)]
|
|
#[webapi(interface = web_api_interfaces::FileList, require_prototype)]
|
|
struct FileListObjectDeclaration {
|
|
#[webapi(slot = FILE_LIST_LENGTH_SLOT)]
|
|
length: f64,
|
|
}
|
|
|
|
#[derive(WebApiFunctionTemplate)]
|
|
#[webapi(interface = web_api_interfaces::FileList)]
|
|
struct FileListPrototypeAccessorsDeclaration {
|
|
#[webapi(accessor_property, getter = file_list_length_getter_callback, enumerable)]
|
|
length: (),
|
|
}
|
|
|
|
#[derive(webidl::WebIdlArgs)]
|
|
#[webidl(prefix = "FileList.item")]
|
|
struct FileListItemArgs {
|
|
#[webidl(required)]
|
|
index: u32,
|
|
}
|
|
|
|
pub fn install_file_list_template_bindings<'s>(
|
|
scope: &mut v8::PinScope<'s, '_, ()>,
|
|
template: v8::Local<'s, v8::FunctionTemplate>,
|
|
interface_name: &str,
|
|
) {
|
|
if interface_name != "FileList" {
|
|
return;
|
|
}
|
|
FileListTemplateMethodsDeclaration::initialize_prototype_template(
|
|
scope,
|
|
template.prototype_template(scope),
|
|
);
|
|
FileListPrototypeAccessorsDeclaration::initialize_prototype_template(
|
|
scope,
|
|
template.prototype_template(scope),
|
|
);
|
|
}
|
|
|
|
pub fn build_file_list_object<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
files: &[v8::Local<'s, v8::Object>],
|
|
) -> Option<v8::Local<'s, v8::Object>> {
|
|
let length = u32::try_from(files.len()).ok()?;
|
|
let contents = build_file_list_contents_array(scope, files);
|
|
let object = FileListObjectDeclaration::new(f64::from(length))
|
|
.bind(scope)
|
|
.ok()?;
|
|
for (index, file) in files.iter().enumerate() {
|
|
let index = u32::try_from(index).ok()?;
|
|
if define_file_list_index(scope, object, index, *file) != Some(true) {
|
|
return None;
|
|
}
|
|
}
|
|
set_private_value(scope, object, FILE_LIST_FILES_SLOT, contents.into());
|
|
Some(object)
|
|
}
|
|
|
|
pub fn sync_file_list_contents<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
object: v8::Local<'s, v8::Object>,
|
|
files: &[v8::Local<'s, v8::Object>],
|
|
) {
|
|
let Ok(length) = u32::try_from(files.len()) else {
|
|
return;
|
|
};
|
|
let contents = build_file_list_contents_array(scope, files);
|
|
let previous_length = file_list_length_from_object(scope, object)
|
|
.filter(|value| {
|
|
value.is_finite()
|
|
&& *value >= 0.0
|
|
&& value.fract() == 0.0
|
|
&& *value <= f64::from(u32::MAX)
|
|
})
|
|
.map(|value| value as u32)
|
|
.unwrap_or(0);
|
|
for index in 0..previous_length {
|
|
let _ = object.delete_index(scope, index);
|
|
}
|
|
for (index, file) in files.iter().enumerate() {
|
|
let _ = define_file_list_index(scope, object, index as u32, *file);
|
|
}
|
|
set_private_value(scope, object, FILE_LIST_FILES_SLOT, contents.into());
|
|
let length = v8::Number::new(scope, f64::from(length));
|
|
set_private_value(scope, object, FILE_LIST_LENGTH_SLOT, length.into());
|
|
}
|
|
|
|
fn define_file_list_index<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
object: v8::Local<'s, v8::Object>,
|
|
index: u32,
|
|
file: v8::Local<'s, v8::Object>,
|
|
) -> Option<bool> {
|
|
// Native indexed properties must not invoke inherited author setters.
|
|
let key = v8::String::new(scope, &index.to_string())?;
|
|
object.create_data_property(scope, key.into(), file.into())
|
|
}
|
|
|
|
fn build_file_list_contents_array<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
files: &[v8::Local<'s, v8::Object>],
|
|
) -> v8::Local<'s, v8::Array> {
|
|
// Populate own elements without invoking author Array.prototype setters.
|
|
let values = files.iter().map(|file| (*file).into()).collect::<Vec<_>>();
|
|
v8::Array::new_with_elements(scope, &values)
|
|
}
|
|
|
|
pub fn is_file_list_object<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
object: v8::Local<'s, v8::Object>,
|
|
) -> bool {
|
|
web_api_interfaces::FileList::is_instance(scope, object)
|
|
}
|
|
|
|
pub fn file_list_files_from_object<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
object: v8::Local<'s, v8::Object>,
|
|
) -> Option<Vec<v8::Local<'s, v8::Object>>> {
|
|
if !is_file_list_object(scope, object) {
|
|
return None;
|
|
}
|
|
let length = file_list_length_from_object(scope, object)?;
|
|
if !length.is_finite() || length < 0.0 || length.fract() != 0.0 || length > f64::from(u32::MAX)
|
|
{
|
|
return None;
|
|
}
|
|
let contents = get_private_value(scope, object, FILE_LIST_FILES_SLOT)
|
|
.and_then(|value| v8::Local::<v8::Array>::try_from(value).ok())?;
|
|
let length = length as u32;
|
|
let mut files = Vec::new();
|
|
files.try_reserve_exact(length as usize).ok()?;
|
|
for index in 0..length {
|
|
let file = contents
|
|
.get_index(scope, index)
|
|
.and_then(|value| v8::Local::<v8::Object>::try_from(value).ok())?;
|
|
files.push(file);
|
|
}
|
|
Some(files)
|
|
}
|
|
|
|
pub fn file_list_item_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
let Some(parsed) = webidl::parse_args::<FileListItemArgs>(scope, &args) else {
|
|
return;
|
|
};
|
|
let receiver = moli_webapi_declare::web_api_object_target(scope, args.this())
|
|
.expect("FileList receiver validated by WebIDL");
|
|
let Some(value) = get_private_value(scope, receiver, FILE_LIST_FILES_SLOT)
|
|
.and_then(|value| v8::Local::<v8::Array>::try_from(value).ok())
|
|
.filter(|files| parsed.index < files.length())
|
|
.and_then(|files| files.get_index(scope, parsed.index))
|
|
else {
|
|
rv.set(v8::null(scope).into());
|
|
return;
|
|
};
|
|
if value.is_undefined() {
|
|
rv.set(v8::null(scope).into());
|
|
return;
|
|
}
|
|
rv.set(value);
|
|
}
|
|
|
|
fn file_list_length_getter_callback<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
args: v8::FunctionCallbackArguments<'s>,
|
|
mut rv: v8::ReturnValue<'_, v8::Value>,
|
|
) {
|
|
let length = file_list_length_from_object(scope, args.this())
|
|
.filter(|value| value.is_finite() && *value >= 0.0)
|
|
.unwrap_or(0.0);
|
|
rv.set(v8::Number::new(scope, length).into());
|
|
}
|
|
|
|
fn file_list_length_from_object<'s>(
|
|
scope: &mut v8::PinScope<'s, '_>,
|
|
object: v8::Local<'s, v8::Object>,
|
|
) -> Option<f64> {
|
|
let object = moli_webapi_declare::web_api_object_target(scope, object)?;
|
|
get_private_value(scope, object, FILE_LIST_LENGTH_SLOT)
|
|
.and_then(|value| value.number_value(scope))
|
|
}
|
|
|
|
#[derive(WebApiFunctionTemplate)]
|
|
#[webapi(interface = web_api_interfaces::FileList, enumerable)]
|
|
struct FileListTemplateMethodsDeclaration {
|
|
#[webapi(method, length = 1, callback = file_list_item_callback)]
|
|
item: (),
|
|
|
|
#[webapi(
|
|
intrinsic_data_property = v8::Intrinsic::ArrayProtoValues,
|
|
symbol = "iterator",
|
|
)]
|
|
iterator: (),
|
|
}
|