 Hello everybody, thank you. It's okay. We are here for to talk about the web assembly if you have been here in the law in the previous talks We talk about pie script about fire died web assembly is a technology which powers all the all these projects I'm I start to become interested in web assembly when I start to work in anaconda more than one year ago because I'm the pie script team and That that's what I'm doing for My daily job In the private community. I'm also known for my work on hpi and pi pi Now I start to work on spy, which is something that I will briefly talk about later But I announced it also yesterday at the lighting talks It's okay. What assembly? As I said one year ago in April more bit more than one year ago I start to look into tears because it was my job and I became excited. I I Feel that it's an incredibly cool technology and I will try to explain why I Feel that naming is a bit weird because web assembly is not really about the web only and it's not really an assembly So what what is it? I did what everybody does nowadays. I ask it to just shut GPT So what assembly is a binary instruction format for a stack-based virtual machine The important part here is virtual machine so The the correct way of thinking of web assembly is as a virtual machine or a CPU like it's an instruction set It's a virtual CPU which runs many places including in the browser a distinctive feature of a web assembly is that it's designed such a way to be Completely sandboxed and safe to execute so you can execute untrusted code in inside for example a browser and be sure that It can do anything malicious And and it's a virtual CPU it can run everywhere. There is a support for it Browser is the main example, but you can run it outside the browsers and this that's why the web thing is not completely true For example, you can run it inside node. Yes, which is the same virtual machine that powers JavaScript in the browser in node in Chrome sorry, but there are also independent run times for web assembly One is wasm time, which is just like you can run from seeing from the command line once time blah blah dot wasp It is was there are many many other also because what assembly is pretty small as a Instruction set so it's kind of easy to write a virtual machine for it But since it runs in the browser We can think of it as the most ubiquitous vehicle machine ever because if you compile something to web assembly it runs everywhere in desktop in mobile in Way devices So that that's one of the reason why it's becoming so popular It's a standard. It's developed by an organization because bytecode Alliance Where companies and the thing calls individuals can join and and and the development So you need to think of wasm as a CPU and it means that Unless you're doing very very very specialized that you are never going to write web assembly by yourself by hand It's mostly used as a compilation target. So you can start from high-level languages CC++ or etc and compile them using a compiler to web assembly and then run it It's designed such a way to be very very very fast Because it is a low-level virtual machine, but we can call it LLVM because LLVM is something different It's a compiler and yes naming is out It's a low-level machine and it's designed such a way that Runtimes for it can translate very easily to the base metal CPU which which runs it You can compile once to your program wants to web assembly run everywhere There is a virtual machine which we saw it's a lot of places From some of the view is the same concept of Java JVM or net compile once run everywhere But the big difference is that it's low-level. So a web assembly can You can represent pointers and structs and all these things which exist in the C and C++ world So it's it comes it's very easy to port legacy Code to web assembly and run it in the browser. That's for example, how you run a photoshop in the browser nowadays and Auto card But this is just like a very very high-level introduction and now I'm I'm going to be a bit more low-level to try to Show what it means in in more concretely This is like kind of the aloe world in web assembly a very simple function written in C which computes the factorial adjust the for loop and And we can compile it for example using slang the C compiler coming with LLVM to Web assembly is the command line which I used it's not important the important part is that it generates a was file which is binary and we can exit it later But I'm not going to you should show the binary because it's impossible to read I'm going to show you the equivalent in in what it's called a web assembly text format, which is a Well human readable way of representing wasp, which is abbreviated as what? And this is one of the reason why I wanted to do this talk was to show this picture So this is how the Factorial looks like in what I'm not going to Explain everything because it doesn't make any sense, but it's a kind of list like representation so you see that inside the what you have a function which is called factorial and You you take One parameter, which is an integer there's two beats it returns an integer and you have local variables and And it is the loop and you can see the instructions are very low level So a plus being becomes this and the compiler knows that easy This is the type of a and b and this is the instruction to get a local variable So if you are a web assembly The virtual machine is very easy to compile these two native code. That's why web assembly gets very very High speed How do we run it as I said we can run it everywhere. We have a Runtime for it This presentation is inside a browser so we can run it from JavaScript. This is how you run Instantiated web assembly module from JavaScript. You have this API, which I don't know much about because I'm not really a JavaScript developer But you can instantiate a module and then you can Get the exports so you can this is the factorial function implemented in web assembly and then I can run it and I for example, I can This is live demo and we will see if it works What's happening here? I it's jumping to somewhere in my Okay So the demo may be towards the slide show doesn't So I can compute a yes, it works. So this is how you run it from JavaScript Nothing spectacular, but you can see that basically you can get a function which looks like a JavaScript function And you can call it from it and that's it, but That's not that's not the only thing that you can do you can also run it from Python So hint is in this example. We are outside the browser. We are Linux command line We've write a Python program, which we run with CPython but we're using was time as a Python module to load and execute the web assembly and The concrete API is a bit different, but it's not too much different than what we did it from JavaScript So we instantiate a web assembly module using this module API from was time and then we can get a pointer to the to the function inside and we can call it So this already shows you how Web assembly is useful to run stuff in in multiple and very different environments So this is this was the yellow ward of web assembly. You can you can Write a factorial you can write things like this. So what you can do in general with web assembly You can write computational intensive programs very easily and That's it But then really like by default that does the only thing that web assembly lets you to know to do I say this that I said earlier that It's some box that and save Which means that by default you cannot do anything you cannot interact with the external system You don't have any access to the file to the file system. You cannot do any network Connection you can do any IO you cannot even print to the terminal You don't have any access to system call. You don't even have libc. So you cannot do malloc and free That's how you you become very safe, but you say it's like it is not very useful. So you say what are you talking about? The way we escape from this unboxing is the disconsert Concept of web assembly imports So whenever I write a web assembly module, I can declare that I expect some imports from the outside Which are function I can say that I expect a function which takes an integer and do something and returns a float Something it is but and the imports must be provided by the host and the whole the host in the example Before is the JavaScript program which calls my well assembly or my Python program which called my web assembly this is the host and the host can decide How to provide a functionality that the web assembly expects? And and and it seems a taskful control over it it can it can make sure that you're not reading the wrong files, for example Let's try to become again more concrete. This is the second example It's again something very simple, but it's a function written in C and here you can see that I have two external functions Which one from printing a string and one from printing a single integer and I am using it from from this very simple C function and C lang Compiles these external function references to web assembly imports So I can compile this to have assembly I get the what file Also, it was my of course, and you can see that instead of what I have these imports So when I instantiate it from JavaScript or from Python, I need to provide this to function From the outside. This is how you do it from JavaScript so I implement print int in JavaScript and I Implemented by simply calling console log, of course We will see later print str and then I define this Object in which I put my imports and then when I instantiate my module I pass the import objects and then the web assembly run times as the magic to put all the wires together and And so my counter function will call when from C I call From here in C. I call print str then web assembly will call This function implemented in JavaScript So as I said print int is very easy to implement print string is much harder why because web assembly speaks only numbers and From the web ascending point of view the memory is a big giant JavaScript array so If I have a string in C It's a pointer to the memory a pointer of bytes to a string of bytes and it's basically an index inside the JavaScript array and When I I call a I pass a pointer from C to to JavaScript What what JavaScript receives is a is a number. It's the point It's the offset in the in the memory inside this array So if I want to extract a string from memory, I need to read it one by one for example This is how I can get a reference to the web assembly memory from JavaScript. It's an array There's some JavaScript thing which I don't really understand copied and pasted from Stack Overflow probably this is Code that you should not show to children I hope that Roberto who has his kid is not here because I don't want him to to cities It's a very very wrong code, but it you you get the idea You read the byte by byte until you find a zero which is the end of the string from the C point of view and I'm building the JavaScript string which is equivalent to what it's in my web assembly memory and that's how I find can finally print it and I can do the same from Python. I can I can write my I can load my web assembly module using once time and then I need to Pass to imports again and print num and print a steer and print num is very easy to do and print a steer is basically the same This is how you access web assembly module from Python using one time and you see it's basically the same the same kind of stuff You iterate until you find a zero and you read character by character and building a string. It is also Not very good Python code. I don't have the excuse that I don't know how to run Python I mean, I know how to write better Python code, but I think this is easier to read This is how web assembly and their outside world communicates but To be not to be useful you need to provide a lot of functionality and I think that a Good example is how do you implement a file system and this is where you can really understand understand the power of a family and That the way you can use it as a kind of lightweight container and virtualization system Imagine that you want to access the file system You Under who do you need access to syscalls? You need access to a nice is called to open a file to read and write from files and you can use them as imports So your web assembly module will expect this kind of imports and it's up to the host to decide what do you do with them and One way one depending on the context But you could you could say that whenever you try to open ATC slash pass Boody then you open the real file Or you can say that you are opening a fake file in a completely virtual file system So that's the web assembly module see something completely virtual and safe Or you can say I want to expose only a part of the file system and block everything else It depends on on the host and again if the host that has control on it We are starting from the From the ground I showed you something very simple with no imports Then I start to write a couple of you bought my myself then I'm talking about the file system It's not I'm not telling that you should reimplement an operating system every time you want to use web assembly fortunately, there are Ways to well, there are products which did it for us The most well-known project in this area is mscripten and it's basically something which try to simulate a posits environment inside web assembly so from from the point of view of you as a programmer you use mscripten and and You can write your program using the normal C schools most of them and M-scripten will provide them in a simulated environment in JavaScript It's mscripten is really written in such a way to To be as much compatible as possible with legacy code so It is really trying to simulate the post equal it has a lot of Existing libraries Reimplemented in in the context of the browser So for example, you can use lib sdl for drawing on the screen and mscripten will take care of translating this call into a canvas So your sdl pi game program can run in the browser using this technology But since all the runtime is implemented in a large part in JavaScript It means that it works only If you have a web assembly runtime, which supports also just script, which is the browser or not. Yes If you want to compile something with mscripten and run it with other runtimes, it's not going to work There is another way of accessing the external world, which is called wazzy and you can think of wazzy, which is a Web assembly system interface. I think I why I didn't write it in this light Wazzy you can think of it as a standard library of imports It's a standard developed by the code alliance in which say oh if you want to open a file You need to have this import and then the host knows how to deal with it If you want to write to the network, you can have this import and then the host will see that you want to write a network and provide a good a good import for it and Again, it's up to you when you start the program to decide how to configure wazzy to give access to the real-life system FAQ1 and etc. From some point of view it's kind of the same problem space of my script and but a Rule of thumb is that if you want to run your program in the browser use mscripten If you want to run your wasm program on the server using these runtimes use wazzy It's not completely true, but it's a good starting point There are there are people who run wazzy in the browser because you can implement this import also in JavaScript As I did before so but it's it's it's a good starting point to understand the difference wazzy is divided into multiple Subposses so for example, you can decide I want to expose only the IO part to my program So the program can use the file system, but not the network or only the the clock But not the file system and etc. So it's it's still in progress, but It's it's what will happen in the future that you will have fine-grained control over what you expose to your web assembly functionality What we talked so far was the how to access the External word from was but web assembly the virtual machine itself is also gaining a lot of functionalities and This is also what it's getting me excited a lot in In in in this context space because The the first iteration of web assembly was a very low level machine It was a virtual machine designed to run C programs with the C semantics So you have pointers you have strikes you have memory Which is the giant be just guitar a and not much But now of assembly is slowly gaining one by one feature which are useful for higher level programming languages You have exception handling which means that you can have two web assembly modules one written in C++ and the other written in Well, not Python by imagine Python and I can raise an exception from C++ and catch it from Python and visa versa it's not there yet, but it can be and There is another proposal for garbage collector. So you can have a web assembly Language language company web assembly which use the garbage collector provided by the host and the browser are very very good garbage collectors better than what we have in Python word, so Basically web assembly is becoming a platform for multi-language interoperability and I Think that this is something that we will see more and more in the future Another exciting thing is the component model You will be able to write in component in one language expose some interface with high-level methods and Consume it from another language so I can imagine a future in which I write components in web assembly in Rust and consuming free Python And then visa versa and and etc. So I think we are going towards this model of multi-language Interoperability, we will see if I'm right We are at Euro Python and I've needn't speak much about Python yet. What's about web assembly and Python? If you want to run by Python in the browser you can use The iodide that Roman just talked about you can use by dying inside the browsing by script which is like a layer on top of it and In this case you are basically executing CPython compile to web assembly So it's an interpreter of Python running inside the browser exactly as on my x86 computer I have an interpreter running on them on my CPU So you have iodide which is basically CPython plus a lot of pre-built scientific libraries and not only scientific libraries Already compiled by the pyrite team plus an incredibly cool piece of technology Which allows you to interpret with JavaScript now? We have micro Python, which is also getting support for web assembly and JavaScript integration then of course we have pie script that it's it's what pays my salary actually and There is Jupiter light which is As same as Jupiter, but with the notebook the kernel of the notebooks running in the browser and not on the server again using pyrite and this fiscule.com which is this service offered by an a conda to To make it very very easy to write and deploy and share Python apps And what I talked yesterday lighting talks in case anybody was here I'm starting work a few weeks ago on spy which will be a compiler from Python to web assembly So not only an inter not not an interpreter in which you have to download a big runtime, but you will be able to Compile your pattern program directly to web assembly, but it's well very very early stage and a lot of work in progress So I think I'm done with my presentation. This is the link to the to the github where you can find the Examples and a link to the slides and yeah, that's it. Thank you so much Antonio for the talk Now you mentioned some things some directions in the future There is something I'd like to ask what doesn't exist today in terms of The ecosystem or applications that could be built using these technologies doesn't exist, but you think you should Something that you wish this community builder Okay, that's a very very broad question and hard question So what exists in the web assembly? What doesn't exist in the web assembly which I would like I think that it's basically what I described I I look forward for when web assembly will get enough features to become the standard for multi-language interoperability and It's not Python specific but I would like Python to to be able to play a significant role in this and I don't know who asked the question. I hope I answered in case please shout Thank you. We have another question here. It's not working. Yes. Hello Hi, thanks for talking. I just wanted to ask can you give me like some list of Successful projects using web assembly on web or elsewhere? repeat the question like some examples of Project that is successful using web assembly. Oh As far as I know for example Photoshop runs in the browser using web assembly and AutoCAD as I said Pyscript I Will say I will call it successful I think I don't know I'm I'm talking about what I have a red rumored But I think that web assembly started Mozilla as a way to have a fast fast way to get Decoders for videos in the browser and I think that's what Netflix is using for for DRM and in the browser, but I should double check this Thanks. Hi Thanks for that talk was really interesting I have a question with regards to mscriptum versus was he if I understand you correctly Was he is more like an interface specification while mscriptum is an implementation Does mscriptum also implement was he Not completely also for historical reason mscriptum started when was he didn't exist So it it it created some API which then was different was created a bit different in was he as far as I know Whenever they can they try to converge but was he is specified in such a way that Is not completely compatible with posix so sometimes it just cannot be was he compatible right But yes from some of you you are right mscriptum is both a specification and the Implementation and was he is just the specification and the actual implementation is provided by the runtimes Yeah, so someone still needs to implement was he for me. Okay. Thanks Another question up front, please If you had a CPython extension Could you compile it to wasm and have CPython imported as like as if it was a Native extension and what would the start you mean that's the CPython compared to WebAssembly or a CPython running in my desktop On your desktop. Yeah. Yes In theory it could be possible I fear that in practice you will have a lot of Lot of the small and big problems specifically because the C API is not really a good piece of Abstract it's not really a good layer of abstraction over the details of the interpreter so to start with or assembly is 32 bit and Your Python is 64. So all your pointer have different Sides and The CPython C API expect a structure which T is filled at his offset So if you manage to solve all the details and WebAssembly, maybe yes but What is interesting is to try to do what you suggested with HPie, which is another project which I work on in which try to have a Better or more obstructed API C API for Python and this is something which eventually I would like to try But it's not it's not around the corner Do we have one final question in the back? It's more like a request if you kindly could share your slides on this court once you're done. Thank you I will I think it is actually I think that is this link is a bit broken in the sense that are the slide of That I presented to Python Italy a few months ago So it's they are exactly the same but they contain Python Italy at the beginning. So if they're good enough, I can share this link Okay, this is the Q&A. Thank you so much. I tell you