Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upImplement Blob reading APIs (arrayBuffer/text) #24287
Comments
|
In-progress steps:
|
|
I'd love to work on it. Can you assign it to me? |
|
@mvashishth Go ahead :) |
|
Can I get more information about "get stream" algorithm? Can't find it anywhere. |
|
@mvashishth Since Servo does not support streams yet, my incomplete steps from above were designed to implement a stream-like file reading operation. |
|
The easiest first step is to implement the new methods synchronously and resolve the Promise value with the blob contents. Once that is done, it will be easier to focus on making the actual operation asynchronous. |
|
The documentation says Blob has an associated get stream algorithm. I didn't find it anywhere or am I missing something? |
|
You are not missing anything. Servo doesn't support streams yet, so we don't implement that algorithm yet. |
|
Blob.text() |
|
Yes, you are correct. These APIs are used to extract the contents of a Blob into a value that JS code that interact with. Websites could create a Blob and store it in some persistent storage (like IndexedDB), and then later get the blob out of the storage and use these APIs to read the blob's contents. You can learn more about Blobs at https://www.javascripture.com/Blob. |
|
The scope of this issue only covers arrays as blobs right? |
|
As opposed to what? The scope of this issue is the unimplemented |
|
I tried using the blob.text() implemented in Chromium. It returns a string made of all the elements of the blob array. Any reason why we need to convert it to byte sequence first using the "get stream" algorithm? |
|
@mvashishth All blob contents are stored as a |
|
Servo takes an hour to build on my laptop. Is there a way I can only build specific components? Also, can I ask Rust and WebIDL syntax related questions here? |
|
You can run |
|
Is there a simpler way of making the get_bytes function? At the minimum, it should be able to just read the contents of the blob object and run the get stream of algorithm right? Can I do a python implementation of the function first and get that reviewed? I am having a hard time understanding the syntax of Rust. It'd be much better if I can understand the program structure in a programming language I am familiar with. |
|
If writing pseudocode in another language will help you understand what needs to be done in Rust, that's fine. |
|
What I meant by #24287 (comment) is that for the first step, calling get_bytes() directly to retrieve the contents of the Blob will give us the bytes that we can use to create a blob or an arraybuffer, and we can then create a promise value and resolve the promise with the blob or arraybuffer. From there it will be easier to work on making it asynchronous instead of synchronous. |
|
Can you explain how exactly get_bytes() works? |
|
Hey @mvashishth, are you still working on this issue? If not, I'd like to try it. |
|
To answer the previous question (sorry for missing it), get_bytes has three different cases:
|
|
I see two methods in promises to create a new promise- one creates a new compartment and the other does the work in the current compartment. I am having a bit of trouble in understanding compartments and when to use which method. I read a little about compartments in the josephine crate, which helped a bit, but the usage of both the methods is still unclear. Can you explain that? |
|
Some of the compartment terminology is outdated, unfortunately, and we should be talking about entering realms instead. However, the short version is that promise objects have to be allocated in a particular realm. The API exists to ensure that the caller has either verified that we've already entered a realm (by passing the InCompartment argument) or asserts if there a realm has actually not been entered. In the short term you can use the API that creates a promise in the current compartment, and we can sort out whether that's appropriate during review. |
|
@jdm You can assign this issue to @kunalmohan. Apologies for not being able to contribute to it. |
|
I just wanted to run through my understanding of the readasArrayBuffer method. servo/components/script/dom/filereadersync.rs Lines 92 to 105 in e6b271d line 93: get blob contents as Vec<u8>97: create a mutable null pointer (I am not sure what null_mut::<JSObject>() does) in the JSContext accepted as the parameter.98: create a new arraybuffer object and assert its success. 105: return Nonnull type pointer to newly created arraybuffer(JSObject). please explain the line 97 and correct any other mistakes I have made in the understanding of the method Also how should the method of processing blob contents in ArrayBuffer be different from the readasArrayBuffer method? |
|
line 97 is calling ptr::null_mut() and using an explicit type annotation to ensure that we're getting a I'm not sure I understand your second question, however. In the initial synchronous implementation, I believe we will be creating an ArrayBuffer the exact same way. |
|
Yes, that's what I was asking for now, the synchronous implementation. Thanks! |
Can you explain how the
Also, shouldn't the second function here accept |
|
read_fike sends a message to the file handler thread (https://github.com/servo/servo/blob/master/components/net/filemanager_thread.rs#L424) containing a sender, and it reads from the corresponding receiver to get the content of the blob in sequential messages. And I believe you are right about your second question. |
|
I believe I have successfully implemented the I have also implemented the Also the |
|
I think it's going to be hard to provide any feedback about the previous comment without seeing the code. Please push it to a branch on your fork and link it if you would like any particular feedback at this point :) I don't think read_file_async needs to return the TrustedPromise. The caller already has a |
|
I have pushed the changes to my fork (branch Please check the |
|
@kunalmohan Ok, here's the design we need to follow for the async read:
This removes the need to wait for the thread to finish running, and passes the promise object along through the thread until it can queue a task for the original thread to run that resolves the promise object. Does that make sense? |
|
Yes, I think I should be able to implement it now. |
|
Ah, no. I missed that; good catch! |
|
How do I ensure thread safety for
(26 similar errors are encountered right now) |
|
That happens when you reference |
Implement Blob methods (text/arraybuffer) <!-- Please describe your changes on the following line: --> #24287 (comment) #24287 (comment) r?@jdm --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #24287 (GitHub issue number if applicable) <!-- Either: --> - [X] There are tests for these changes <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Implement Blob methods (text/arraybuffer) <!-- Please describe your changes on the following line: --> #24287 (comment) #24287 (comment) r?@jdm --- <!-- Thank you for contributing to Servo! Please replace each `[ ]` by `[X]` when the step is complete, and replace `___` with appropriate data: --> - [X] `./mach build -d` does not report any errors - [X] `./mach test-tidy` does not report any errors - [X] These changes fix #24287 (GitHub issue number if applicable) <!-- Either: --> - [X] There are tests for these changes <!-- Also, please make sure that "Allow edits from maintainers" checkbox is checked, so that we can help you if you get stuck somewhere along the way.--> <!-- Pull requests that do not address these steps are welcome, but they will require additional verification as part of the review process. -->
Spec:
Code:
components/script/dom/webidls/Blob.webidlcompoentns/script/dom/blob.rsTests:
./mach test-wpt tests/wpt/web-platform-tests/FileAPI/blob/Blob-array-buffer.any.js./mach test-wpt tests/wpt/web-platform-tests/FileAPI/blob/Blob-text.any.js