Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(file-api): use the shared illegal FileList constructor

ldm06e82924b023f
diff --git a/moli-core/tests/fixtures/runtime/secondary_webapis.html b/moli-core/tests/fixtures/runtime/secondary_webapis.html
index 64107346b..76db4ad78 100644
--- a/moli-core/tests/fixtures/runtime/secondary_webapis.html
+++ b/moli-core/tests/fixtures/runtime/secondary_webapis.html
@@ -35,7 +35,9 @@ document.body.setAttribute("data-file-type", file.type);
document.body.setAttribute("data-file-last-modified", String(file.lastModified));
document.body.setAttribute("data-file-size", String(file.size));
-const list = new FileList([file]);
+const transfer = new DataTransfer();
+transfer.items.add(file);
+const list = transfer.files;
document.body.setAttribute("data-file-list-tag", Object.prototype.toString.call(list));
document.body.setAttribute("data-file-list-length", String(list.length));
document.body.setAttribute("data-file-list-item-same", String(list.item(0) === file));
diff --git a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
index 770ade205..96e60d7ae 100644
--- a/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
+++ b/moli-renderer-v8/src/context_bootstrap/assets/constructor_templates.rs
@@ -15,8 +15,7 @@ use super::super::{
exposed_interfaces::install_interface_template_metadata,
file_api::{
data_transfer_constructor_callback, file_constructor_callback,
- file_list_constructor_callback, file_reader_constructor_callback,
- file_reader_sync_constructor_callback,
+ file_reader_constructor_callback, file_reader_sync_constructor_callback,
},
form_data_runtime::build_form_data_constructor_template,
geometry_runtime::{dom_matrix_constructor_callback, dom_point_constructor_callback},
@@ -451,14 +450,6 @@ pub(in crate::context_bootstrap) fn build_constructor_template<'s>(
.length(2)
.build(scope)
}
- ConstructorKind::FileList => {
- v8::FunctionTemplate::builder(moli_webapi_declare::web_api_constructor!(
- web_api_interfaces::FileList,
- file_list_constructor_callback
- ))
- .length(0)
- .build(scope)
- }
ConstructorKind::FileReader => {
v8::FunctionTemplate::builder(moli_webapi_declare::web_api_constructor!(
web_api_interfaces::FileReader,
diff --git a/moli-renderer-v8/src/context_bootstrap/file_api.rs b/moli-renderer-v8/src/context_bootstrap/file_api.rs
index 1bb0442f3..219e22432 100644
--- a/moli-renderer-v8/src/context_bootstrap/file_api.rs
+++ b/moli-renderer-v8/src/context_bootstrap/file_api.rs
@@ -37,11 +37,11 @@ pub(crate) use file_entry_file::{
FileEntryFileCallbackTask, FileEntryFileCallbackTaskEffect,
file_system_file_entry_file_callback,
};
+pub(super) use file_list::file_list_item_callback;
pub(crate) use file_list::{
build_file_list_object, file_list_files_from_object, is_file_list_object,
sync_file_list_contents,
};
-pub(super) use file_list::{file_list_constructor_callback, file_list_item_callback};
pub(crate) use file_reader::flush_one_pending_file_reader;
pub(super) use file_reader::{
file_reader_abort_callback, file_reader_add_event_listener_callback,
diff --git a/moli-renderer-v8/src/context_bootstrap/file_api/file_list.rs b/moli-renderer-v8/src/context_bootstrap/file_api/file_list.rs
index 8d65a4f00..58ac251d9 100644
--- a/moli-renderer-v8/src/context_bootstrap/file_api/file_list.rs
+++ b/moli-renderer-v8/src/context_bootstrap/file_api/file_list.rs
@@ -1,4 +1,3 @@
-use super::*;
use crate::util::{get_private_value, set_private_value};
use crate::web_api_interfaces;
use crate::webidl;
@@ -42,22 +41,6 @@ pub(super) fn install_file_list_template_bindings<'s>(
);
}
-pub(in crate::context_bootstrap) fn file_list_constructor_callback<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- args: v8::FunctionCallbackArguments<'s>,
- mut rv: v8::ReturnValue<'_, v8::Value>,
-) {
- if !args.is_construct_call() {
- throw_type_error(
- scope,
- "Failed to construct 'FileList': Please use the 'new' operator.",
- );
- return;
- }
- initialize_file_list_object(scope, args.this(), args.get(0));
- rv.set(args.this().into());
-}
-
pub(crate) fn build_file_list_object<'s>(
scope: &mut v8::PinScope<'s, '_>,
files: &[v8::Local<'s, v8::Object>],
@@ -185,34 +168,3 @@ fn file_list_length_from_object<'s>(
get_private_value(scope, object, FILE_LIST_LENGTH_SLOT)
.and_then(|value| value.number_value(scope))
}
-
-fn initialize_file_list_object<'s>(
- scope: &mut v8::PinScope<'s, '_>,
- object: v8::Local<'s, v8::Object>,
- files_value: v8::Local<'s, v8::Value>,
-) {
- let files = if files_value.is_null_or_undefined() {
- None
- } else {
- files_value.to_object(scope)
- };
- let length = files
- .and_then(|value| value.get(scope, v8str(scope, "length").into()))
- .and_then(|value| value.number_value(scope))
- .filter(|value| value.is_finite() && *value >= 0.0)
- .map(|value| value as u32)
- .unwrap_or(0);
- let mut contents = Vec::new();
- for index in 0..length {
- let file = files.and_then(|files| files.get_index(scope, index));
- contents.push(file.unwrap_or_else(|| v8::undefined(scope).into()));
- if let Some(file) = file {
- let _ = object.set_index(scope, index, file);
- }
- }
- let contents = v8::Array::new_with_elements(scope, &contents);
- set_private_value(scope, object, FILE_LIST_FILES_SLOT, contents.into());
- FileListObjectDeclaration::new(length as f64)
- .initialize(scope, object)
- .expect("FileList declaration should initialize object");
-}
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
index dc0f583b5..2827b7628 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/registry.rs
@@ -937,7 +937,7 @@ const CONSTRUCTOR_SPECS_AFTER_STREAMS: &[ConstructorSpec] = &[
},
ConstructorSpec {
interface: web_api_interfaces::FileList::DESCRIPTOR,
- kind: ConstructorKind::FileList,
+ kind: ConstructorKind::Illegal,
},
ConstructorSpec {
interface: web_api_interfaces::FileSystem::DESCRIPTOR,
diff --git a/moli-renderer-v8/src/context_bootstrap/specs/types.rs b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
index faa534fbd..1d32bb52d 100644
--- a/moli-renderer-v8/src/context_bootstrap/specs/types.rs
+++ b/moli-renderer-v8/src/context_bootstrap/specs/types.rs
@@ -71,7 +71,6 @@ pub(in crate::context_bootstrap) enum ConstructorKind {
WebGlDebugRendererInfo,
WebGlLoseContext,
File,
- FileList,
FileReader,
FileReaderSync,
XmlSerializer,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/files_and_transfer.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/files_and_transfer.rs
index b2adff83a..7a899c1e9 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/files_and_transfer.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/files_and_transfer.rs
@@ -964,3 +964,27 @@ fn mouse_dragstart_bubbles_to_window_once() {
.expect("dragstart log should evaluate");
assert_eq!(result, "1|dragstart:true:true");
}
+
+#[test]
+fn file_list_uses_illegal_constructor_and_native_file_sources() {
+ let mut vm = new_storage_test_vm("https://file-list-construction.test/");
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ let reads = 0;
+ const input = {get length() { reads++; return 0; }};
+ let rejected = false;
+ try { new FileList(input); } catch (error) { rejected = error instanceof TypeError; }
+ const transfer = new DataTransfer();
+ const file = new File(['payload'], 'example.txt');
+ transfer.items.add(file);
+ const native = transfer.files;
+ return JSON.stringify([rejected, reads, native instanceof FileList,
+ native.length, native.item(0) === file, native.item(1) === null]);
+ })()
+ "#,
+ )
+ .unwrap();
+ assert_eq!(result, "[true,0,true,1,true,true]");
+}
diff --git a/moli-renderer-v8/src/worker/thread/tests/network/file_and_xhr.rs b/moli-renderer-v8/src/worker/thread/tests/network/file_and_xhr.rs
index 6050747e6..46a2b5a9b 100644
--- a/moli-renderer-v8/src/worker/thread/tests/network/file_and_xhr.rs
+++ b/moli-renderer-v8/src/worker/thread/tests/network/file_and_xhr.rs
@@ -6,19 +6,23 @@ async fn worker_filelist_interface_object_is_available() {
ensure_v8();
let mut handle = spawn_worker(
r#"
- const file = new File(["hello"], "note.txt", { type: "text/plain" });
- const list = new FileList([file]);
+ let constructError = null;
+ try {
+ new FileList();
+ } catch (error) {
+ constructError = error && error.name;
+ }
postMessage({
ctorOwn: Object.prototype.hasOwnProperty.call(self, "FileList"),
ctorType: typeof FileList,
- ctorName: list.constructor && list.constructor.name,
- tag: Object.prototype.toString.call(list),
- instanceofFileList: list instanceof FileList,
- length: list.length,
- firstName: list.item(0) && list.item(0).name,
- indexName: list[0] && list[0].name,
- iterType: typeof list[Symbol.iterator],
- iterName: Array.from(list).map(file => file.name).join(","),
+ ctorName: FileList.name,
+ constructError,
+ itemType: typeof FileList.prototype.item,
+ lengthGetterType: typeof Object.getOwnPropertyDescriptor(
+ FileList.prototype,
+ "length",
+ ).get,
+ iterType: typeof FileList.prototype[Symbol.iterator],
});
close();
"#
@@ -32,7 +36,7 @@ async fn worker_filelist_interface_object_is_available() {
.expect("channel closed");
assert_eq!(
expect_post_json(msg),
- r#"{"ctorOwn":true,"ctorType":"function","ctorName":"FileList","tag":"[object FileList]","instanceofFileList":true,"length":1,"firstName":"note.txt","indexName":"note.txt","iterType":"function","iterName":"note.txt"}"#
+ r#"{"ctorOwn":true,"ctorType":"function","ctorName":"FileList","constructError":"TypeError","itemType":"function","lengthGetterType":"function","iterType":"function"}"#
);
}
diff --git a/moli-wpt-compat/fixtures/wpt/ported/fileapi/file-basic.html b/moli-wpt-compat/fixtures/wpt/ported/fileapi/file-basic.html
index 101c8cafb..c84af9640 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/fileapi/file-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/fileapi/file-basic.html
@@ -14,6 +14,14 @@ function assert_throws_name(expectedName, callback, description) {
throw new Error(description || "expected exception");
}
+function file_list(files) {
+ const transfer = new DataTransfer();
+ for (const file of files) {
+ transfer.items.add(file);
+ }
+ return transfer.files;
+}
+
promise_test(async function () {
const file = new File(["hello ", new Uint8Array([0xe2, 0x99, 0xa5])], "note.txt", {
type: "TEXT/PLAIN",
@@ -33,7 +41,8 @@ test(function () {
const file = new File(["hello"], "note.txt");
const second = new File(["second"], "second.txt");
const third = new File(["third"], "third.txt");
- const list = new FileList([file, second, third]);
+ assert_throws_name("TypeError", () => new FileList(), "FileList is not constructible");
+ const list = file_list([file, second, third]);
assert_true(list instanceof FileList, "FileList instance");
assert_equals(list.length, 3, "FileList length");
diff --git a/moli-wpt-compat/fixtures/wpt/ported/forms/form-submission-file-multipart-basic.html b/moli-wpt-compat/fixtures/wpt/ported/forms/form-submission-file-multipart-basic.html
index 6c12a0ddb..d3abdf843 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/forms/form-submission-file-multipart-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/forms/form-submission-file-multipart-basic.html
@@ -54,6 +54,14 @@ function append_target_frame(name) {
return frame;
}
+function file_list(files) {
+ const transfer = new DataTransfer();
+ for (const file of files) {
+ transfer.items.add(file);
+ }
+ return transfer.files;
+}
+
test(function () {
const text = document.createElement("input");
assert_equals(text.files, null, "files should not apply to non-file inputs");
@@ -66,7 +74,7 @@ test(function () {
assert_equals(input.files, empty, "files getter should return the same object until replaced");
const file = new File(["file-body"], "note.txt", { type: "text/plain" });
- const files = new FileList([file]);
+ const files = file_list([file]);
input.files = files;
assert_equals(input.files, files, "files setter should install the selected FileList");
}, "HTMLInputElement.files exposes a replaceable FileList for file inputs");
@@ -80,7 +88,7 @@ test(function () {
upload.type = "file";
upload.name = "upload";
upload.multiple = true;
- upload.files = new FileList([
+ upload.files = file_list([
new File(["alpha-body"], "alpha.txt", { type: "text/plain" }),
new File(["beta-body"], "beta.bin"),
]);
@@ -115,7 +123,7 @@ promise_test(async function () {
const upload = document.createElement("input");
upload.type = "file";
upload.name = "upload";
- upload.files = new FileList([
+ upload.files = file_list([
new File(["file-body"], 'report"name.txt', { type: "text/custom" }),
]);
const emptyInput = document.createElement("input");
@@ -180,7 +188,7 @@ promise_test(async function () {
externalUpload.type = "file";
externalUpload.name = "upload";
externalUpload.multiple = true;
- externalUpload.files = new FileList([
+ externalUpload.files = file_list([
new File(["external-alpha-body"], "external-alpha.txt", { type: "text/external-alpha" }),
new File(["external-beta-body"], "external-beta.bin"),
]);
diff --git a/moli-wpt-compat/fixtures/wpt/ported/worker/resources/worker-fileapi-worker.js b/moli-wpt-compat/fixtures/wpt/ported/worker/resources/worker-fileapi-worker.js
index 3701fff14..af7108130 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/worker/resources/worker-fileapi-worker.js
+++ b/moli-wpt-compat/fixtures/wpt/ported/worker/resources/worker-fileapi-worker.js
@@ -2,17 +2,23 @@ self.onmessage = async function (event) {
const mode = event.data && event.data.mode;
if (mode === "filelist") {
- const file = new File(["hello"], "note.txt", { type: "text/plain" });
- const list = new FileList([file]);
+ let constructError = null;
+ try {
+ new FileList();
+ } catch (error) {
+ constructError = error && error.name;
+ }
postMessage({
ctorOwn: Object.prototype.hasOwnProperty.call(self, "FileList"),
ctorType: typeof FileList,
- ctorName: list.constructor && list.constructor.name,
- tag: Object.prototype.toString.call(list),
- instanceofFileList: list instanceof FileList,
- length: list.length,
- firstName: list.item(0) && list.item(0).name,
- indexName: list[0] && list[0].name,
+ ctorName: FileList.name,
+ constructError: constructError,
+ itemType: typeof FileList.prototype.item,
+ lengthGetterType: typeof Object.getOwnPropertyDescriptor(
+ FileList.prototype,
+ "length",
+ ).get,
+ iterType: typeof FileList.prototype[Symbol.iterator],
});
close();
return;
diff --git a/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fileapi-basic.html b/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fileapi-basic.html
index 5c64b1356..8292e2667 100644
--- a/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fileapi-basic.html
+++ b/moli-wpt-compat/fixtures/wpt/ported/worker/worker-fileapi-basic.html
@@ -42,11 +42,10 @@ promise_test(async function () {
assert_equals(message.data.ctorOwn, true, "worker should expose FileList");
assert_equals(message.data.ctorType, "function", "FileList constructor type");
assert_equals(message.data.ctorName, "FileList", "FileList constructor name");
- assert_equals(message.data.tag, "[object FileList]", "FileList object brand");
- assert_equals(message.data.instanceofFileList, true, "FileList instanceof");
- assert_equals(message.data.length, 1, "FileList length");
- assert_equals(message.data.firstName, "note.txt", "FileList.item(0)");
- assert_equals(message.data.indexName, "note.txt", "FileList index getter");
+ assert_equals(message.data.constructError, "TypeError", "FileList is not constructible");
+ assert_equals(message.data.itemType, "function", "FileList.item is exposed");
+ assert_equals(message.data.lengthGetterType, "function", "FileList.length is exposed");
+ assert_equals(message.data.iterType, "function", "FileList iterator is exposed");
} finally {
worker.terminate();
}