WebAssembly / design

WebAssembly Design Documents
http://webassembly.org
Apache License 2.0
11.4k stars 694 forks source link

can webassembly open and read a file from the browser local storage, like javascript can do? #1128

Closed ghost closed 7 years ago

ghost commented 7 years ago

I have a problem, i need to speed up some sort of decoding file process, which is not video, and i would like to generate a dom from that file, but to be able to empower the encoding process, or even use it, webassembly needs to open and read the file from the local storage (i'm not sure that local storage has been implemented in firefox or safary), generate that chunk of xml, and send it back to javascript.

It's possible with the technology?

I have seen that ios WKWebview might not support webassembly. Is it right assumption, and i need to jump to crosswalk? My main goal is to be able to run my code on every platform using ionic.

Becavalier commented 7 years ago

@raxim1 No, wasm can not access localstorage directly, what you can do is to allocate a new memory buffer for wasm from the js side, and store the file content into this memory buffer with corresponding binary format. Then call the processing function which was written in wasm side. The wasm module can process the data in the same memory buffer which you created form js side. Finally, retrieve data from the same memory address from js side and convert those binary data to any encoding format you want.

jfbastien commented 7 years ago

FWIW there's a similar question on StackOverflow: https://stackoverflow.com/questions/45535301/can-i-read-files-from-the-disk-by-using-webassembly

ghost commented 7 years ago

The question might be misleading:

Local storage meant for me - (chrome has a sandbox local storage - i'm not sure that other browsers has) So to be able to read from there. "Becavalier"

I have read similar questions on stack overflow, but it's not the same how i'm thinking, i'm just processing the technology, but it's viable instead of processing memory buffers processing a stream of data. (to avoid frequent callback)

# I might have a design suggestion without knowing too much about WebAssembly:

There is a way how to capture frames from a video camera in html5 asynchronously: https://developer.mozilla.org/en/docs/Web/Guide/Events/Media_events

look at "timeupdate" - what it does, when the time has been changed on the video it's triggering an event, so you can capture a frame by sec

I think to read a file or stream is a valid query anyway, instead of using some dirty mechanism, what Becavalier explained, although i think she might attach some example on it, because i just slightly understand at the moment.

How i imagine WebAssebly, that it's running in a new thread, not in the same thread as javascript. (so it's a webworker a little bit)

there is a video file which has a link. <video src="">, or <file src="">

The browser is checking the permissions - cross origin etc. - so it's save to read.

Pass the filehandler (filedescriptor), as you pass in C++ to the function from that src tags, and call back asynchronously like "timeupdate" in video frame capture. Is this mechanism exist in webassembly? (that asynchronous callback)

You know you are specifying an API, which is fairly different (mostly based on the Electron way - which is nice for hybrid desktop applications), from what other HTML5 component is doing. What if we can standardize in a way, not to manipulate memory sections, just do whatever you would do it anyway. I have an Webworker implementation, which maps large file sections to the memory with FileApi. On my filereader i had to make a Blocking Queue implementation also.

I can imagine to map the file to a memory buffer, and read from there, but i'm not sure that whether it's passed by value to the webassembly which would be an expensive task in practice (even webassembly is nice and handly), or pass by reference, which would be quite nice option?

There was a promotional video by google on youtube https://youtu.be/6v4E6oksar0?t=1692 which has got thausands of viewers, but looking at the dsp site https://d2jta7o2zej4pf.cloudfront.net/, i had an annoying problem, on my i7 quad core system, with 16GB ram (ubuntu)

On Chrome the speed of the video processing of webassembly was far more slower, than the javascript itself, and the results was divided on firefox. WKWebview which is iphone at the moment does not support webassembly, even crosswalk does not. Ionic framework is the most popular one to use. That's the reason why i personally even think about webassembly, as several project showed that even optimized video player can get significant juce from javascript.

## So looking at the datas, i'm not convinient to ever use this technology, and who would like to promote it, need to speed up this experimental thing a little bit more.

I think i have found a way for further discussion, if somebody open-minded, can tell me what is the problem with my simplified approach, and what is the security risk behind. (what i don't see at the moment)

You can pass streaming data from javascript, instead of creating memory buffer. So you would have a smooth api. (anyway lot's of guy would rasterize the data to webgl, make the computation and get back the result - you can do some trick the emulate what WebCl does - you can specify file formats which is decoded in images - so you won't have any conversion at the input, and you can simplify the output)