-26

I don't see any way to use WebAssembly in snippets; just HTML, JavaScript and CSS. Allowing WebAssembly would let any compilable language be indirectly used for snippets. It's not complicated to let WebAssembly run.

Can we have WebAssembly support?

18
  • 4
    Is it "not complicated"? From a cursory glance at Wikipedia it seems like this technology has a bunch of unresolved security problems.
    – Laurel
    Dec 20, 2021 at 16:18
  • @Laurel It's good enough to be enabled in all major browsers.
    – Shambhav
    Dec 20, 2021 at 16:27
  • @Laurel It'd actually be cool if we could secretly crypto mine in our answers.
    – Shambhav
    Dec 20, 2021 at 16:35
  • 7
    So I'm just going to compile my Rust code to WASM and dump the compiled result into a stack snippet? Erm, no. Dec 20, 2021 at 16:39
  • 14
    "it would be cool if we could secretly steal the user's resources"
    – Kevin B
    Dec 20, 2021 at 16:56
  • 1
    @KevinB No no, that's not known to be possible as of now. There probably are vulnerabilities, but that's not a major concern in this setting. I just can't see why a person would spend time finding a vulnerability and then use that in a post to get data of literally random people. Someone could, right now, make a snippet that sends system info to their website. It should be possible, and if it's not where does the user's resources go? Nowhere. So, if people can't steal your data right now, they can't even by using WebAssembly.
    – Shambhav
    Dec 20, 2021 at 23:06
  • @MisterMiyagi It's still useful for WebAssembly questions. Actually, you could dump the result, it would be convenient... Maybe, there will be a bot to do this in the future. It's the right direction.
    – Shambhav
    Dec 20, 2021 at 23:08
  • 2
    @ShambhavGautam i was re-quoting what you said but with my interpretation of what you said: "It'd actually be cool if we could secretly crypto mine in our answers."
    – Kevin B
    Dec 20, 2021 at 23:08
  • @KevinB Yeaah, but it's inaccurate unless you're talking about CPU resources. No major technology like this can get release while having such severe flaws like that.
    – Shambhav
    Dec 20, 2021 at 23:11
  • 1
    That's precisely what i was referring to
    – Kevin B
    Dec 20, 2021 at 23:11
  • @KevinB Oh okay. That is also still cool. Secretly stealing one's resources is also cool, not good but cool for sure. If that's concern, maybe we could only allow WebAssembly to run after SO itself compiles the code. It might be feasible though. The user could download a WebAssembly compiler(running in WebAssembly itself) provided by SO. Then the compiler in the user's machine could compile that code and send it with the post to the server, where the server packs it into a final post. Not that it is good but it's worth researching about.
    – Shambhav
    Dec 20, 2021 at 23:20
  • 1
    I don't get it, we can already run WebAssembly in snippets. What makes you think we can't? You obviously need to compile and host the wasm file yourself, but apart from that it should just work™.
    – Kaiido
    Dec 21, 2021 at 1:59
  • 3
    @MisterMiyagi They are clearly jokes and if someone thinks they're more than that then.. idk. Anyway, you can already do that, so I can steal someone's resources to mine crypto right now, so it wouldn't change anything. It's a valid joke.
    – Shambhav
    Dec 21, 2021 at 8:45
  • 3
    Merits of the proposal aside, folks, I do not think the proposal is unclear or needs details, looks clear enough to me, I voted to leave open. Dec 21, 2021 at 9:16
  • 1
    How is a feature-request "Opinion based"?
    – Ryan M Mod
    Dec 30, 2021 at 4:35

1 Answer 1

4

Nobody keeps you from running WebAssembly in a snippet:

(module
  (import "env" "memory" (memory 1))
  (import "env" "log" (func $log (param i32 i32)))

  (data (i32.const 0) "Hello, World!")

  (func (export "hello")
    i32.const 0
    i32.const 13
    call $log
  )
)

(async function runner() {
  const memory = new WebAssembly.Memory({ initial: 1 });
  
  function dereferenceString(offset, length) {
    return new TextDecoder().decode(new Uint8Array(memory.buffer, offset, length));
  }
  
  function log(messageOffset, messageLength) {
    console.log(dereferenceString(messageOffset, messageLength));
  }
  
  const code = (new TextEncoder()).encode('\x00asm\x01\x00\x00\x00\x01\t\x02`\x02\x7F\x7F\x00`\x00\x00\x02\x19\x02\x03env\x06memory\x02\x00\x01\x03env\x03log\x00\x00\x03\x02\x01\x01\x07\t\x01\x05hello\x00\x01\n\n\x01\b\x00A\x00A\r\x10\x00\x0B\x0B\x13\x01\x00A\x00\x0B\rHello, World!\x00\x18\x04name\x01\x06\x01\x00\x03log\x02\t\x02\x00\x02\x00\x00\x01\x00\x01\x00');
  
  const { instance } = await WebAssembly.instantiate(code, { env: { memory, log } });
  instance.exports.hello();
})();

It just doesn't make sense (except the question is about WebAssembly). WebAssembly itself is pretty much useless without Modules being imported / exported. Now for sure one could create a "snippet library" with some utility function for logging and debugging, however this will only be useful for a very limited set of questions in the compiled languages as:

  • Most questions aren't actually including runnable code, as they're about a few lines of specific code. Creating a minimal, reproducible example is much harder than for scripting languages
  • A lot of questions are also about the behavior of specific compilers or the runtime performance, as such running the code in "yet another runtime" is not that useful
  • Reworking the code to use the "snippet library" might actually destroy the phenomenon in question

For questions about WebAssembly itself it might be quite useful to create the above in a more elegant fashion (e.g. directly compile and embed the WAT into the snippet), however as is currently a niche tag (there are 1000x more questions for JavaScript), I doubt that investing into this would be justified.

2
  • Not sure if this post is gonna be canonical in any way, but if you really want to embed the wasm in the post, wouldn't it be preferable to base64 encode it? It would be smaller than hex escaping (your 323B code becomes 200B), and allows the I believe more current fetch(url) pattern instead of using the TextEncoder.
    – Kaiido
    Dec 22, 2021 at 5:34
  • 1
    @Kaiido true, though this has the advantage that one can see the data sections and other strings in the WASM compilation :) (Actually the hex escaping was not deliberate, simply copied a console.log(require("fs").readFileSync("hello-world.wasm") into the snippet ...) Dec 22, 2021 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .