create account

EOS Development For Beginners: WebAssembly by skenan

View this thread on: hive.blogpeakd.comecency.com
· @skenan ·
$3.40
EOS Development For Beginners: WebAssembly
Many guys are trying to learn how to develop smart contracts on EOS. However, those smart contracts are written by C++ and compiled into WebAssembly which seemed strange to most non-c++ programmers.  So before diving deep into the EOS, it’s better to learn some basic stuff about WebAssembly.

<b> What’s WebAssembly </b>

I don’t want to copy the definition from its [official website](http://webassembly.org/). You can take a look at it by yourself. Here you can think it as a file that can be loaded and run in the web browser.  It’s similar to the Javascript,  but it’s much faster, tinier and safer than JS.

<b> How to write  Webassembly </b>

Here is a work flow:
![](https://steemitimages.com/DQmRat5n75CXtGdEeiJDDazRmnMTjTPgEV282YQLnnj8Gbw/image.png)

Basically there are four steps, I will go though it by a very simple example.

* <b>1. Write the C/C++ code</b>
 
The following code is used to calculate the square root of a number.
```
#include <math.h>
float getSqrt (float num) {
  return sqrt(num);
}

```

* <b>2. Compile C/C++code into wasm (the format of Webassembly)</b>

There are [many ways](https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm) to compile the code. To simplify the process, I found a very [easy way](https://wasdk.github.io/WasmFiddle/?cvrmt) to do that.  
![](![](https://steemitimages.com/DQmcmZr9fa6Yi55ckaL7f7GsCpB3mT2Sh4oDVUYpZzEMxhS/image.png))

Copy your code to that website and click the build button, you can find complied file in the below.

![](https://steemitimages.com/DQmcz9SmQh9oR43dd2r2Ar56jMXSXZmPMCga5xbtKkyNVuC/image.png)

Then download the program.wasm file to your local box.

* <b>3. Load the wasm into browser with JavaScript </b>

Create a test.html using the following code, put it in the same fold as program.wasm
```
<!doctype html>
  <title>WASM Test</title>
  <script>
    fetch('./program.wasm')
    .then(res => {
      if (res.ok)
        return res.arrayBuffer();
      throw new Error(`Unable to fetch WASM.`);
    })
    .then(bytes => {
      return WebAssembly.compile(bytes);
    })
    .then(module => {
      return WebAssembly.instantiate(module);
    })
    .then(instance => {
      window.wasmSqrt = instance.exports.getSqrt;
    });
  </script>
```  

* <b>4. Run the method in Web browser </b>

To work around the website cross-origin issues, we need to run a local web server in that folder. With Linux or Mac, let's do
```
python -m SimpleHTTPServer
```
Then open our Chrome browser, go to this page: http://localhost:8000/test.html. In the console, try the following method.

![](https://steemitimages.com/DQmfEDJkxCTgEKTmEcdqjZ1tq3NkD7PgoViCL2y23ZuybBq/image.png)


## Done

Through this tutorial, you get familiar with WebAssemply, but for the advanced study, please check [eos-example-exchange-contract-and-benefits-of-c](https://steemit.com/eos/@dan/eos-example-exchange-contract-and-benefits-of-c
) written by @dan

If you are interested in how to build EOS on mac, please check my [previous tutorial](https://steemit.com/eosdev/@skenan/eos-build-guide-on-mac-os).
👍  , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorskenan
permlinkeos-development-for-beginners-webassembly
categoryeos
json_metadata{"tags":["eos","eosdev","webassembly"],"users":["dan"],"image":["https://steemitimages.com/DQmRat5n75CXtGdEeiJDDazRmnMTjTPgEV282YQLnnj8Gbw/image.png","![](https://steemitimages.com/DQmcmZr9fa6Yi55ckaL7f7GsCpB3mT2Sh4oDVUYpZzEMxhS/image.png)","https://steemitimages.com/DQmcz9SmQh9oR43dd2r2Ar56jMXSXZmPMCga5xbtKkyNVuC/image.png","https://steemitimages.com/DQmfEDJkxCTgEKTmEcdqjZ1tq3NkD7PgoViCL2y23ZuybBq/image.png"],"links":["http://webassembly.org/","https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm","https://wasdk.github.io/WasmFiddle/?cvrmt","http://localhost:8000/test.html","https://steemit.com/eos/@dan/eos-example-exchange-contract-and-benefits-of-c","https://steemit.com/eosdev/@skenan/eos-build-guide-on-mac-os"],"app":"steemit/0.1","format":"markdown"}
created2017-08-04 00:59:09
last_update2017-08-04 00:59:09
depth0
children6
last_payout2017-08-11 00:59:09
cashout_time1969-12-31 23:59:59
total_payout_value2.839 HBD
curator_payout_value0.556 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length3,068
author_reputation8,219,510,746,132
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,699,983
net_rshares860,488,873,417
author_curate_reward""
vote details (27)
@fureweb ·
Thanks for your post.
properties (22)
authorfureweb
permlinkre-skenan-eos-development-for-beginners-webassembly-20180124t094852631z
categoryeos
json_metadata{"tags":["eos"],"app":"steemit/0.1"}
created2018-01-24 09:48:54
last_update2018-01-24 09:48:54
depth1
children0
last_payout2018-01-31 09:48:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length21
author_reputation79,277,691,714
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id31,880,405
net_rshares0
@hkjn ·
Thanks for the post! I like the focus on concrete steps and keeping it simple,  would love to see more EOS-specific examples intermediate in difficulty between this and @dan's posts.. :)
👍  
properties (23)
authorhkjn
permlinkre-skenan-eos-development-for-beginners-webassembly-20170804t010634874z
categoryeos
json_metadata{"tags":["eos"],"users":["dan"],"app":"steemit/0.1"}
created2017-08-04 01:06:33
last_update2017-08-04 01:06:33
depth1
children1
last_payout2017-08-11 01:06:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length186
author_reputation1,420,329,207
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,700,428
net_rshares1,857,631,186
author_curate_reward""
vote details (1)
@skenan ·
Yeah. I am learning step by step. Hope I can write more.
properties (22)
authorskenan
permlinkre-hkjn-re-skenan-eos-development-for-beginners-webassembly-20170804t011128857z
categoryeos
json_metadata{"tags":["eos"],"app":"steemit/0.1"}
created2017-08-04 01:11:27
last_update2017-08-04 01:11:27
depth2
children0
last_payout2017-08-11 01:11:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation8,219,510,746,132
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,700,739
net_rshares0
@pandorasbox ·
This seems like content that is actually useful to people and deserves sharing. Upvoted.
properties (22)
authorpandorasbox
permlinkre-skenan-eos-development-for-beginners-webassembly-20170804t014653530z
categoryeos
json_metadata{"tags":["eos"],"app":"steemit/0.1"}
created2017-08-04 01:46:51
last_update2017-08-04 01:46:51
depth1
children0
last_payout2017-08-11 01:46:51
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length88
author_reputation6,724,880,351,404
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,702,969
net_rshares0
@trogdor ·
Hey, thanks for the post. Keep them coming!
properties (22)
authortrogdor
permlinkre-skenan-eos-development-for-beginners-webassembly-20170804t012231264z
categoryeos
json_metadata{"tags":["eos"],"app":"steemit/0.1"}
created2017-08-04 01:22:30
last_update2017-08-04 01:22:30
depth1
children0
last_payout2017-08-11 01:22:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length43
author_reputation22,905,182,177,434
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id10,701,365
net_rshares0
@xiaoliang ·
大神,有没有通俗点的讲解eos功能的文章?比eth强在哪里?
properties (22)
authorxiaoliang
permlinkre-skenan-eos-development-for-beginners-webassembly-20180301t045221215z
categoryeos
json_metadata{"tags":["eos"],"app":"steemit/0.1"}
created2018-03-01 04:52:24
last_update2018-03-01 04:52:24
depth1
children0
last_payout2018-03-08 04:52:24
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length30
author_reputation280,200,246,080
root_title"EOS Development For Beginners: WebAssembly"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id41,290,578
net_rshares0