1

I would like to write a function head that return the first value of a an array with a type signature (arr: T[]) => T (Typescript pseudo code).

The idea is to compile the C++ function to WebAssembly using Emscripten and use this head function in my javascript app.

I know C++ template would provide the right tool for such an abstraction but I wonder if templates would work as they operate at compile time.

PS: I am a C++ beginner, any link to any ressource is welcome, I would like to learn.

JF Bastien
  • 6,673
  • 2
  • 26
  • 34
alex3165
  • 195
  • 1
  • 11
  • 1
    Generally you can't compile templates to assembler code. You can only compile template instantiations to assembler code which requires to know the `T` you will be using. At that point you may as well not use a template. – nwp Sep 14 '17 at 21:21
  • This whole idea to "compile C++ to WASM using emscripten to use it in javascript" sounds insane. What are you trying to achieve with it? – user7860670 Sep 14 '17 at 21:28
  • I actually found this github issue https://github.com/kripken/emscripten/issues/4887 on emscripten, seems like this type `emscripten::val` would solve my problem but I wonder what impact this `any` type would have on my C++ script – alex3165 Sep 14 '17 at 21:29
  • @VTT Nothing really, I just want to play with wasm and emscripten, and ended up stuck with this type abstraction problem – alex3165 Sep 14 '17 at 21:31
  • @VTT "insane" as it may sound, that's exactly what WebAssembly is designed to do. – JF Bastien Sep 14 '17 at 21:50

1 Answers1

2

WebAssembly doesn't support "generics" or "templates" per-se, it only has types i32, i64, f32, and f64.

In pure C++ that's fine because your compile will just instantiate all the template specializations you need, and then use them within WebAssembly. If you inter-operate across languages (say C++ in WebAssembly to JavaScript or TypeScript) then you can explicitly specialize your templates and export them from your .wasm file so that JavaScript / TypeScript can call that specialization. Of course that means you have to know what you'll need up front!

One thing you could do, but is totally impractical, is just-in-time generate the .wasm file at runtime when you figure out what template instantiation you actually need. That's impractical because tooling just isn't there right now, you'd need at least parts of a C++ compiler running in WebAssembly, and then you'd need to patch your WebAssembly.Table at runtime (which is totally doable... just not actively done these days).

For your specific usecase though (return the first element of an array) I'm not sure you can do much! Because WebAssembly's types are so limited you can only deal with things that fit in 32 or 64 bits if you must pass through as parameters. Even then, your array can't just generically expand to arguments because WebAssembly parameter counts are pre-determined at compilation time (binding them to JavaScript can drop / getValue on them, but you really don't want that). What you want is probably to pass things through the Memory, which is similar to dealing with strings (in that strings are an array of characters).

JF Bastien
  • 6,673
  • 2
  • 26
  • 34