{"id":1495,"date":"2020-06-27T13:06:14","date_gmt":"2020-06-27T13:06:14","guid":{"rendered":"https:\/\/sp2hari.com\/?p=1495"},"modified":"2020-06-27T13:06:16","modified_gmt":"2020-06-27T13:06:16","slug":"webassembly-101","status":"publish","type":"post","link":"https:\/\/sp2hari.com\/index.php\/2020\/06\/27\/webassembly-101\/","title":{"rendered":"WebAssembly 101"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">WebAssembly has been there for quite some time. While I understand the overall idea, I never had a chance to build anything production-ready so far. I had a chance to play around with WebAssembly during our company hackathon and one of the engineers explained in detail how WebAssembly works.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">I wanted to build something with WebAssembly and wanted that to be a real-world program instead of a simple hello world. While doing something with image processing might have been useful, I wanted something small to start with. Two-player games with computer AI seemed like a good idea and tic tac toe is felt easier to build.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A quick search in Github for tic tac toe gave this result &#8211; <a href=\"https:\/\/github.com\/ryancesiel\/tictactoe-minimax\/\">https:\/\/github.com\/ryancesiel\/tictactoe-minimax\/<\/a>. While the code wasn&#8217;t the best (especially the way char board[3][3] is handled across all the functions), it was short, easy to understand &amp; modify and didn&#8217;t give me any nightmares about compiling the code. As you can see, I have had <a href=\"https:\/\/sp2hari.com\/tinderbox\/\">nightmares<\/a> with simple code compilation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In the first version, I thought I had to export Game::minimax function and I had no clue how to expose a function under a class. So, ended up creating a simple function which called this function and had a simple name &#8211; get_next_move()<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The whole thing works like a charm. You can try to play it here <a href=\"https:\/\/sp2hari.com\/work\/tic-tac-toe\/\">https:\/\/sp2hari.com\/work\/tic-tac-toe\/<\/a>. As always, the entire code is published in my Github here at <a href=\"https:\/\/github.com\/sp2hari\/tic-tac-toe\">https:\/\/github.com\/sp2hari\/tic-tac-toe<\/a><\/p>\n\n\n\n<figure class=\"wp-block-video\"><video height=\"761\" style=\"aspect-ratio: 1428 \/ 761;\" width=\"1428\" controls src=\"https:\/\/sp2hari.com\/wp-content\/uploads\/2020\/06\/2020-06-27_02-20-22.mp4\"><\/video><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">In short, this was a fun experiment to work on instead of the usual hello world or the Fibonacci. The next step is to build something to do with image processing or code auto-completion using WebAssembly. Super excited about both the ideas.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Useful Links<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/webassembly.org\/\">https:\/\/webassembly.org\/<\/a><br><a href=\"https:\/\/marcoselvatici.github.io\/WASM_tutorial\/\">https:\/\/marcoselvatici.github.io\/WASM_tutorial\/<\/a><br><a href=\"https:\/\/programmer.ink\/think\/webassembly-array-passing-input.html\">https:\/\/programmer.ink\/think\/webassembly-array-passing-input.html<\/a><br><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Hacks\/Pending issues with the script<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Issue #1 &#8211; Passing the integer array and using it in C++<\/strong> <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For some reason, the code to send the data from javascript and reading in C++ is ugly. For example, I&#8217;m passing values in the array like 88 (ASCII value of &#8216;X&#8217;) and 79(ASCII value of &#8216;O&#8217;), but in the C++ code, I&#8217;m receiving these values as 1118830592 and 1117650944. I&#8217;m guessing it is something to do with this line <\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Module.HEAPF32.set(typed_gameboard, buffer >> 2)<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">But for some reason, I wasn&#8217;t able to get it right for no value. With most of the example on the internet for float arrays, I didn&#8217;t have too much time to mess with this. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Issue #2 &#8211; Passing a 2D array of N rows and M columns, instead of a 1D array of N*M size<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, I didn&#8217;t want to fight with this. So many examples from the internet were built mainly to see pass 1d array and 1 didn&#8217;t have too much time to figure this out. Especially, copy pasting a working code is more enticing instead of wrestling with unknown C++ and Javascript code. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Issue #3 &#8211; Calling functions from a class. <\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Again, for the lack of documentation, I didn&#8217;t know how to call the functions of a class directly from Javascript. For example, if we have a function called &#8220;get_move&#8221;, we can use that in the EXPORTED_FUNCTIONS=[&#8220;_get_move&#8221;] and things worked. But if I have a function inside a class like &#8220;Game::get_move&#8221;, I had no clue how to use this function in the exported functions. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If you have any solutions for any one of the above issues, please respond in the comments below. I&#8217;ll be forever thankful!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>WebAssembly has been there for quite some time. While I understand the overall idea, I never had a chance to build anything production-ready so far. I had a chance to play around with WebAssembly during our company hackathon and one of the engineers explained in detail how WebAssembly works. I &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-1495","post","type-post","status-publish","format-standard","hentry","category-code"],"_links":{"self":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/1495","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/comments?post=1495"}],"version-history":[{"count":17,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/1495\/revisions"}],"predecessor-version":[{"id":1520,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/posts\/1495\/revisions\/1520"}],"wp:attachment":[{"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/media?parent=1495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/categories?post=1495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sp2hari.com\/index.php\/wp-json\/wp\/v2\/tags?post=1495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}