4 ms·
What type of strings though? Exposing Javascript string objects in WASM doesn't make much sense if the code is expecting C strings for instance. Same for other
by flohofwoe 5y ago
What type of strings though? Exposing Javascript string objects in WASM doesn't make much sense if the code is expecting C strings for instance. Same for other languages, those all have their own incompatible internal representations for strings. The only somewhat interop-friendly string type is a zero-terminated bag of bytes, usually UTF-8 encoded (aka C strings), but that's a different string representation than Javascript uses.
The Emscripten SDK offers helper functions to marshal high level data types like Javascript strings to UTF-8 encoded C strings on the WASM heap and back to JS, so it's not that bad.
DOM access can be achieved with helper libraries which call out into JS. And since any sort of DOM manipulation is extremely slow anyway there's not much of a performance difference even with the overhead of calling out from WASM into JS (which actually is quite fast nowadays).
- mamcx 5y ago>What type of strings though? The good one! UTf-8, NOT null terminated, pascal like. ie: what rust have: https://doc.rust-lang.org/std/string/struct.String.html https://doc.rust-lang.org/std/string/struct.String.html REPEAT the mistakes of C (and considering the security angle! in a browser!) must be a big no.
- masklinn 5y ago> UTf-8, NOT null terminated, pascal like. > > ie: what rust have: > > https://doc.rust-lang.org/std/string/struct.String.html https://doc.rust-lang.org/std/string/struct.String.html Rust (or C++) strings are not pascal strings. In pascal strings, the "string buffer" also contains the length information, and historically it was all bytes with a length byte at the start, which was why your strings started at index 1 and limited to 254 bytes. It's possible to modernise this style of strings to be less crummy (that is essentially what sds does), but C++/Rust string are a third take where the length (and capacity) are stored separately from the string buffer, and that buffer is always on the other side of a pointer (ignoring SSO, which Rust sadly doesn't have due to the original interface definition).
- pjmlp 5y agoUnfortunely that is not what WASM designers decided when they went without memory tagging for linear memory segments. So you get all the fun to corrupt linear memory C style.
- flohofwoe 5y agoI think if you're afraid of memory corruption inside the WASM heap, it's better to use Rust instead of C or C++. WASM's job is to prevent code inside the sandbox from escaping the sandbox, not to prevent memory corruption inside the sandbox.
- pjmlp 5y ago> I think if you're afraid of memory corruption inside the WASM heap, it's better to use Rust instead of C or C++. Agreed, but as consumer from WASM modules that isn't your option to make. > WASM's job is to prevent code inside the sandbox from escaping the sandbox, not to prevent memory corruption inside the sandbox. That is not better than a typical OS process, just it happens to be randomly downloaded into my computer.
- afiori 5y agoIt is a lot better: there is a sandbox with minimal surface area compared to no sandbox at all (except for memory isolation)
- pjmlp 5y agoWhen they are finished with the WebAssembly roadmap there will be the same sandbox as a typical OS process, no different of running a ART executable on Android, bitcode on watchOS, MSIL on Windows, or TIMI on IBM i.
- afiori 5y agoOf course, that is the plan, but even then it will still be possible to run WebAssebly modules with no permissions or limited permissions, as the sandbox was always there. On the other hand I need to admit that I would have not forseen some of the more recent use cases for WebAssembly https://bytecodealliance.org/articles/making-javascript-run-fast-on-webassembly https://bytecodealliance.org/articles/making-javascript-run-... which can be reminescent of the "docker daemon running as root" issue.
- flohofwoe 5y agoThis would still require conversion from and to Javascript strings, and doesn't help with any language compiled to WASM that isn't Rust. And it probably wouldn't even help Rust because such a native WASM string type would presumably live outside the WASM heap (because if the string data would be on the WASM heap, there's no need for a native string type).
- mamcx 5y agoAny complex structure past int/floats requiere conversion. Heck, even floats and ints (for example: oCalm and anybody with more/less bits than JS). So, given this is a fact, the best course of action is chosen the most safe alternative. And for everyone else? Well an array of bits ant let the host/callers that are the only that know their own stuff deal with it. INCLUDING Js.
- brabel 5y agoI don't know what you're arguing about. The interface types proposal defines a string like this: string ≡ (list char) char is defined to be a Unicode scalar value (i.e., a non-surrogate code point). Basically, this is "the most safe alternative"??!
- mamcx 5y agoI was arguing against the idea of "string are implemented differently by different languages, so WHICH one choose?". A safe one. Your sample is that - except I think is better if is a utf-8 string, but this one works for me too-. What will be worrisome is if is made to be like in C.
- miohtama 5y agoDOM manipulation is slow is pretty much busted misconception today: https://svelte.dev/blog/virtual-dom-is-pure-overhead https://svelte.dev/blog/virtual-dom-is-pure-overhead
- flohofwoe 5y agoIt is "slow" relative to the overhead of calling from WASM into JS to manipulate the DOM from JS. To be fair, I don't know how many clock cycles creating, destroying or modifying a DOM node costs on average, but most likely "a lot" compared to the overhead of a WASM to JS call because a lot more machinery is involved.
- miohtama 5y agoYou are correct, today. But the overhead from WASM to JS is disappering with host binding, also known as WebAssembly Interface Types proposal. https://www.chromestatus.com/feature/6219189974990848 https://www.chromestatus.com/feature/6219189974990848