Summarized using AI

What you can do with Ruby on WebAssembly

Yuta Saito • September 12, 2024 • Sarajevo, Bosnia and Herzegovina • Talk

Overview of Ruby on WebAssembly

Yuta Saito, a master's student from Tokyo, Japan, discusses the integration of Ruby with WebAssembly at the EuRuKo 2024 event. He highlights how the combination opens new possibilities for running Ruby applications across various platforms, including web browsers and edge computing devices.

Key Points Discussed

  • Introduction to Ruby on WebAssembly:
    • Ruby on WebAssembly aims to enable Ruby applications to run not only in web browsers but also in non-browser environments.
  • Understanding WebAssembly:
    • WebAssembly (wasm) is an efficient, portable instruction format that allows programs to be executed across different platforms.
    • It is particularly valuable for safely executing untrusted code.
  • Architecture of Ruby wasm:
    • The project consists of three main components: a C interpreter compiled to WebAssembly, a packaging system for Ruby files, and an interpretation library for JavaScript.
  • Showcase and Examples:
    • Demonstrations include running an Interactive Ruby (IRB) shell in a browser and executing a Ruby on Rails application (Mastodon) entirely within a web browser.
    • These examples showcase the handling of Ruby gems, including those with C extensions, highlighting the potential applications of Ruby wasm.
  • Technical Challenges:
    • Transforming Ruby C extensions to work with WebAssembly is complex, focusing on cross-compilation and packaging.
    • New strategies, including static and dynamic linking, are discussed, particularly the upcoming component model for WebAssembly which will enhance dynamic linking capabilities.
  • Future Directions:
    • Saito emphasizes improving binary sizes, runtime performance, and establishing Ruby as a standard language within the WebAssembly landscape.
    • He mentions ongoing exploration of new WebAssembly features to maintain Ruby's compatibility and efficiency on the platform.

Main Takeaways

  • Ruby wasm is a groundbreaking project that broadens the applicability of Ruby, allowing it to run seamlessly across diverse environments.
  • It utilizes the state-of-the-art capabilities of WebAssembly while targeting challenges such as runtime performance and binary size to ensure Ruby's competitiveness in the ever-evolving tech landscape.
  • Saito is also in search of job opportunities in compiler and toolchain development, hoping to leverage his expertise in this innovative area of programming.

What you can do with Ruby on WebAssembly
Yuta Saito • Sarajevo, Bosnia and Herzegovina • Talk

Date: September 12, 2024
Published: January 13, 2025
Announced: unknown

EuRuKo 2024

00:00:10.160 hi everyone um I'm excited to be here to talk about ruby and W assembly uh which
00:00:17.080 is my uh my main focus for past few years
00:00:22.760 so I will explain uh explore how this combination opens up the new possibility
00:00:30.160 for running Ruby on a range of platforms from web browser to Edge
00:00:37.760 Computing so today I will show you uh what you can do with a ruby on web
00:00:46.239 assembly so uh let me briefly introduce myself uh I'm UT Sao our Master student
00:00:53.120 from Tokyo Japan and I'm committed to uh Cobi and
00:01:00.039 Swift and working on web assembly support for both langages and working
00:01:06.600 mainly working on the back end stuff in the compiler and to
00:01:11.840 chain I'm also uh obviously the creator of the Ruby was Project have you ever
00:01:20.079 heard the project raise your hands oh
00:01:28.920 nice so uh today I will start with a brief introduction of Ruby Wen and then
00:01:37.119 uh I will show you some Showcases of running Ruby on web
00:01:42.720 assembly after that I will explain a bit about the technical details of the
00:01:49.200 project finally I will share the Future Vision and road
00:01:56.799 map so uh web assembly is a game changer when it comes to the program dist
00:02:03.759 distribution format some of you may only uh have the
00:02:09.800 name but uh uh first of all web assembly is a
00:02:15.080 specification for an instruction format designed for stock stock based machine
00:02:22.599 uh you know the stock based machine uh thanks to the last Co presentations of
00:02:29.200 course but as the name suggest uh it was primary designed for the use on the
00:02:37.200 web the main design goals are to be independent of architecture the
00:02:43.120 underlying architecture and the platforms and making it and it makes it
00:02:49.120 possible uh to Port Port the program into the every platform and also to be
00:02:57.280 efficient in terms of the size and Road time and to be able to safely execute
00:03:04.480 untrusted programs provided by provided by Third
00:03:11.400 parties so even though it has web in its name uh there are several runtime
00:03:18.480 implementations outside of the web uh the most well-known one is a wasm
00:03:25.599 time uh which is written in Rust language and is very fast
00:03:31.239 runtime uh and there also was a micro run time
00:03:36.840 by Intel which is made to run web assembly in resource limited environments like iot or embeded
00:03:45.519 systems and actually there are some products that shi the run time in a very
00:03:52.879 small uh embedded camera system uh to control the uh the your runtime Behavior
00:04:02.560 by uh writing a small configuration script so uh there and there are many
00:04:10.680 other run times as well since the wasm specification does
00:04:17.400 not uh demand the demand a rot of a rot from the system and all these R times
00:04:24.199 are easily uh to be embedded in applications and and in addition to its
00:04:32.759 inviability uh wasm also remits access to the host system so it's useful for
00:04:38.840 systems like uh that needs to take uh anet programs as as an input from users
00:04:47.120 and exe execute them in a safe way example of such application includes
00:04:53.880 are some pring systems uh s platforms and uh and so
00:05:02.160 so uh wasm has a lot of potential as a format for sharing
00:05:07.800 programs not just for web browsers so for example from the perspective of
00:05:14.160 programming language if you can compare to a compile a langage to a
00:05:20.840 was uh your program can run in all kinds of environments uh whe whether
00:05:28.280 it's uh whether it's on arm or x64 processors or even on
00:05:35.319 a b metal system on the other hand from the platform perspective uh if it compares
00:05:43.000 to the uh if it supports wasn't uh it can
00:05:49.919 indirectly support many different R gauges without uh without uh any
00:05:56.160 expressed support so that's why I think just like
00:06:01.520 Jason has become a defa standard for exchanging data uh wasm could become the
00:06:08.000 defa standard for sharing program so to take this idea further I
00:06:15.440 think we are seeing a trend uh where developer can focus on more on their
00:06:22.039 applications without infrastructure uh for example uh before
00:06:27.240 the container error uh when you really need to package up
00:06:34.520 the uh or uh you you need to package up
00:06:40.160 uh the entire environment into a vir machine image but in many case as long as the
00:06:47.919 file system and processes are isolated uh using something like Docker container
00:06:53.840 is enough and now there are cases where we don't even need full file system access
00:07:01.400 and want to minimize the attack surface you might need to handle basic
00:07:07.039 tasks like computational uh computation or reading just reading files and
00:07:13.360 writing files in those situations instead of container image a portable
00:07:20.080 application binary like a was makes more sense of course uh this does not mean
00:07:27.440 replacing everything so so it just gives an option to do it uh to do a simple
00:07:33.960 task in a simple way so web assembly is a good a cool
00:07:40.759 technology uh then what is uh Ruby wasm first of all uh Ruby wasm is a
00:07:47.720 project that enables running Ruby on Everywhere by uh web
00:07:53.639 Assembly not that uh everywhere is not limited to web browser but also in
00:07:59.479 includes uh non browser environment as I mentioned earlier and this project
00:08:05.240 consist of three main components C interpreter compiled to web assembly a
00:08:12.199 packaging system for Ruby files and an inter interpretation library for
00:08:20.159 JavaScript so let's see what it looks
00:08:25.479 like uh this is an example of running Ruby on we uh browser using ruby.
00:08:34.000 was you can run Ruby Ruby code in a script tag in an HTML
00:08:41.719 file you can call JavaScript functions through the interrup
00:08:48.440 library you can await JavaScript promises magically using await method so
00:08:55.320 you can write a synchronous code in a in a way in uh in a synchronous
00:09:01.880 way in and this is an example running uh
00:09:08.720 very simple Ruby on our standard R RI runtime we are Distributing our
00:09:15.480 precompile Ruby interpreter as a single web web assembly binary so you can just
00:09:21.519 download the binary and run it in with a wasy run time in any platform
00:09:32.640 okay so you saw the basic usage of Ruby was now let me show you some Showcases
00:09:41.000 of the advanced capability of Ruby
00:09:48.560 was so here is uh IRB is working on
00:09:55.040 browser you can access the website and try it yourself
00:10:00.640 and recently we integrated xjs to offer full featured terminal
00:10:07.040 emulations so it uh it can uh aut
00:10:21.040 Easter egg hidden in the IRB
00:10:31.120 and another example uh is a uh is a masteron running on
00:10:39.920 browser uh do you mean uh do you uh uh it might be crazy for you but uh all
00:10:48.240 things running in browser first
00:10:54.200 uh boot the boot the ra rails up
00:11:00.959 so as you know uh Mason is a popular open source application that is built on
00:11:06.399 Ruby on Rails it uh uses a quite large uh it's a quite large R app and includes
00:11:14.440 many gems including C extensions so it's good exam it's a good
00:11:21.519 example to show the quality of our gem support so it works uh
00:11:30.639 it support the device Jam of course and you you can see some of the
00:11:39.519 uh ra logs in the browser console and you can post the tooth on
00:11:48.440 browser so it works uh active record of course that's it
00:12:06.519 so this is uh the architecture of this demo uh the maon app is built on top of
00:12:13.440 many gems including noiri HL active record of course uh that work and
00:12:21.320 uh uh and many others uh and some of them like noiri includes C extensions
00:12:28.920 but as you may know uh the extensions brings many uh
00:12:35.600 difficulties so but uh we can now run them on web
00:12:40.760 assembly so the ra app runs on service worker and it intercept the net Network
00:12:48.880 request from the web pages then the app respond uh responds
00:12:54.560 the uh those requests through the rock interface
00:13:00.920 and uh the app runs uh postgress inside the browser too and it communicate with
00:13:08.040 active records so all things running inside
00:13:14.920 browser also you can find more example uh given by the
00:13:20.959 community uh the first two are good example for as a playground of the uh uh
00:13:29.440 Ruby on browser uh the first one is quite simple
00:13:34.920 and the one uh intro uh shortly introduced in the
00:13:41.519 last uh CO's presentation to show the is sec in the browser and the second one is
00:13:48.639 a very good uh integration has a good very good integration with Ruby jams so
00:13:55.360 you can try your Ruby jams on the website with without installing
00:14:00.720 anything and the r two are good example to build interactive playground to
00:14:07.120 demonstrate your gems so please check uh their good uh B post later they
00:14:15.800 describes how they buil the their playground step by step so it might be
00:14:23.519 interesting for you you can you can take photo
00:14:29.600 yeah okay so now let me explain the technical detail of uh Ruby on web
00:14:35.959 assembly but today I don't uh dig dig into too much but uh let me briefly
00:14:43.000 introduce the overview so uh there are a few topics I
00:14:48.880 will cover today uh proin Ruby C Ruby to web assembly packet gems uh cross
00:14:56.399 compile C extensions uh read link uh see extensions share
00:15:05.839 Ries the first challenge is obviously pting C itself to web
00:15:13.440 assembly you might think it's easy because we already have a C2 web
00:15:20.120 assembly compiler and C is written in
00:15:25.560 C actually I have been working on uh uh putting the Swift compiler to web
00:15:31.880 assembly in our last few years too and uh so i s it was easier than uh Swift
00:15:40.920 because I have we already have a compiler so we don't need to fix the
00:15:46.399 compiler itself so just compile C into the web assembly so i s that but I was wrong it
00:15:55.920 was not so easy and some of the feature including exceptions fiber and zc and
00:16:02.560 make and then uh and they make it really challenging to put the uh to put to web
00:16:15.279 assembly so we basically uh the pting work is uh
00:16:22.720 basically uh utilizing the as f instrumentations
00:16:30.399 which is uh uh post uh post rink optimization pass uh in provided by the
00:16:38.680 binari and tools and it enables some of the uh hcky way to enable the uh
00:16:48.519 conservative DC exception fiber with a minimal platform specific
00:16:54.360 hacks however uh we trade basically trade of the performance and cod size
00:17:00.880 for portability for now so yeah it works it works greatly for as a initial stage
00:17:09.600 but uh we need to improve it by using the recent web assembly features uh and
00:17:16.079 rage The Leverage them to uh uh trade back the performance and C
00:17:24.600 size you can see the details of this uh inter uh implementation techniques in
00:17:31.440 The ruik Kite the two years two years ago and I share the implementation
00:17:38.840 details in the uh presentation so check
00:17:45.000 it so uh the next step is a package gems
00:17:51.880 and Ruby files into a web assembly binary it might not be uh obvious to you
00:18:01.200 because uh in web assembly we need to package everything into a single binary
00:18:08.120 to uh uh to sep uh binary as a single binary uh
00:18:14.919 so so we made a a virtual fire system in on the top of the web assembly
00:18:22.760 technology so uh we now have a way to uh package the
00:18:29.760 ruby gems listed in the gem file uh by using rbwm command which is uh provided
00:18:39.200 by the ruby. wasm project and just uh just build them
00:18:45.679 using the build sub command then you can get the all package uh web assembly
00:18:55.840 binary and the package gems are store uh in the
00:19:02.159 BFS and you can set up the road pass by Band setup then you can you uh require
00:19:09.679 gems as usual so nothing special
00:19:16.280 here the next challenge is uh cross compil and C extensions to web
00:19:22.240 assembly if you want to build C extensions you need to talk about uh I
00:19:27.760 need to talk about about the mkmf uh as you know mkmf is a core of
00:19:35.840 the C extension build system in the Ruby EOS system mkmf is a Ab abbreviations of
00:19:43.960 make make file and it literally makes make file from XC com.
00:19:49.520 RV now you can uh you have three major users of
00:19:54.600 mkmf C build systems rate compiler uh and the last one is the Ruby jams Ruby
00:20:02.720 jams uses it to build to build z uh gems with C extensions when you use uh gem
00:20:14.640 install however uh Ruby James didn't have a uh cross comparation support at
00:20:22.200 the moment and uh and also mkmf itself didn't offer
00:20:28.720 good cross compilation support and it it forces the users
00:20:35.320 to add some extra hack so but I don't want to introduce h a new trick to Ruby
00:20:43.080 James so I introduced the native cross completion support in the mkmf
00:20:48.799 itself so yeah it's it will be sipped as a part of the Ruby
00:20:57.440 3.4 and and the and also we added the support for in
00:21:04.400 the ruby gem itself so and it's rounded in the main branch so it should be
00:21:13.159 available in the 3.42 so uh we can cross compile three
00:21:22.240 extension to web assembly then the next step is to rink the compiled rival
00:21:29.080 into the CB interpreter so we have two options for
00:21:35.320 ranking rinking C extension to The Interpreter the first one is a static
00:21:41.000 rinking and the second one is a dynamic rinking and usually uh on platforms you
00:21:48.000 use uh we use Dynamic rinking but
00:21:53.320 the uh however uh supporting uh dynamic rinking in web assembly is not uh
00:22:01.720 obvious way so uh we already support static rinking in the for the builtin
00:22:10.159 extensions like Json or something like that but we decided to go with di
00:22:16.320 rinking to support R Ruby jams why uh the main reason is that uh the build
00:22:23.440 model is not suitable for the Ruby J X system so static linking takes a long
00:22:31.080 time to link step and uh especially asy which is the
00:22:37.000 code transformation I introduced before is quite slow and it had had to be
00:22:44.720 applied after the rink static rink so yeah that was the bottom neck for the
00:22:50.400 development flow and also uh there are couple couple
00:22:58.799 of the issue in the static rinking build model and the main problem here is that
00:23:06.880 the uh we had to pause and resume the build system and which is not not a
00:23:14.480 common weight uh in the build system so but the in the dynamic rinking we can
00:23:22.320 uh we can utilize the existing workflow uh without any pain development
00:23:29.360 workfl pain so yeah that's why we tend to be uh tend to employ the dynamic
00:23:38.039 rinking so so uh but why we didn't didn't do it
00:23:44.880 so far the main reason was the web assembly does not have standard Dynamic
00:23:51.080 rinking Ai and if we do our bspoke Dynamic rinking
00:23:57.279 uh uh uh the d uh the binary will be incompatible with the standard web
00:24:03.240 assembly run times so we lose the portability of the web assembly binary so we didn't do uh we didn't uh do
00:24:12.880 that however the situation has changed recently the web assembly Community has
00:24:18.720 been working on a new layer of web assembly standards that the component
00:24:25.399 model the this model is built on top of the existing web assembly
00:24:30.720 standards and uh we expect it to be the future of web
00:24:36.039 assembly and the component model exists several feature including IDL modu
00:24:43.679 rinking and so and so on and but the most important feature to achieve Dynam
00:24:49.679 Dynamic rinking is module rinking modu rinking is a New Concept to
00:24:56.760 build a program by composing multiple programs uh at build uh at the build
00:25:06.200 time the new binary format allows embedding multiple web assembly modules
00:25:12.320 into a single binary and you uh it arrows describing how the module link
00:25:19.200 how the modules modules link to each
00:25:24.360 other so based on the module rinking we can build dynamic rinking mechanism on
00:25:29.880 top of that the key idea is that uh given the modu has its metadata for Dy
00:25:37.000 Dynamic rinking like symbol table and relocation informations and which
00:25:43.159 modules it depends on then we can encapsulate how to rink modules into a
00:25:49.919 new binary format uh we which is called by called as a
00:25:55.640 component so we can encapsulate the link Step at the build build time but we can
00:26:02.799 utilize the dynamic linking API in the existing exist in the implemented in the
00:26:09.000 existing tool chain so uh in this case we have uh four web assembly modules and
00:26:17.080 we can B bundle them into a single component ahead of
00:26:23.120 time so the ahead of time Dy rinking is a bit different from the traditional
00:26:28.399 Dynamic ranking and the actual ranking is done at the bill time so not at the
00:26:34.399 run time the binary is a single component and uh it consist of the all
00:26:41.559 uh dependencies and we made a a Linker uh
00:26:47.559 to link them up into a single binary uh with a python
00:26:54.120 uh uh with a guy uh who is working on the
00:26:59.159 uh porting uh C python to web assembly and we are uh working on together to the
00:27:08.159 to make the wrinkle so the the main difference from
00:27:14.720 the traditional dyamic rinking is that the D the final binary is a component uh
00:27:21.039 the the final binary is fully self-contained and the rinking is done
00:27:27.360 at the build time so there's no file based Library search at one time
00:27:33.640 therefore DL open is available for only libraries known at build time and it's
00:27:39.720 not possible to inject new libraries at run
00:27:45.679 time so yeah uh that's uh uh how we
00:27:51.480 implemented the Ruby wasn't so okay so finally let me introduce the future
00:27:58.360 vision and road map this year uh i d d the road map for
00:28:05.440 my three gos the first one is binary size and
00:28:11.320 the and also the runtime performance and normal
00:28:16.399 platforms because uh web assembly is uh very uh working in working in progress
00:28:24.279 technology so it's not a normal platforms yet so we we need to make it
00:28:30.080 as a normal as much as possible and digging into the binary
00:28:36.679 size uh uh there are several optimization opportunities uh in terms of the
00:28:43.600 interpret design and uh uh and the H we H we apply in the to
00:28:52.640 achieve the some of the uh feature like exception fiber and uh also we can apply
00:29:00.000 some of the uh compare optimizations uh to make it binary size
00:29:10.880 friendly so uh in summary Ruby runs on web assembly and it it now support Ruby
00:29:17.760 James and uh Ruby is re leveraging the State ofth art web assembly technology
00:29:25.279 including component model and we are in investing on the evolution of web
00:29:30.480 assembly to make Ruby more web assembly friendly
00:29:38.080 so that's it from me but uh by the way I'm going to graduate from uh the
00:29:44.080 university soon and I'm looking for job opportunities so uh I'm good I'm good at
00:29:50.600 compiler build system and the web assembly and looking for position
00:29:55.840 related to program rage tool chain development so if you have any interest please contact me thank you
Explore all talks recorded at EuRuKo 2024
+39