This week is pretty challenging, I was assigned on task of putting a CPU into web application. The first thing to be done is to analyze the code of the CPU, it was written in pure JavaScript.
The Best Way to Learn JavaScript
I found out that the previous learning on JavaScript is too shallow, the knowledge of JavaScript learning from website is always not sufficient to be applied in project. However, the best way to learn would be hands-on on certain project, or reading other professional’s code. You would simply don’t understand what they are writing and trying to figure out why. The most interesting feature I found in JavaScript would be the web worker of JavaScript, an asynchronous worker that works behind the background, which perfectly fit the job for CPU. All we need to do is just setup a worker function <code> var worker_name = new Worker(‘JavaScrip_filename.js’) </code> , and store all the asynchronous code in the filename.js to run the asynchronous application that we wanted to. The debugging can be done by using the output processed to the console.
The Interesting Part of JavaScript
The second interesting part about JavaScript is to learn more about Scoping and Hoisting. Scoping is the core of learning JavaScript, as it is very different from C/C++ programming. Ever wonder the output of picture below?
It is familiar to you if you are used to learn about C programming, the output is always 1 as the if loop never satisfy.
However, this seems to be a little different, the output of foo is 10 in the end. This came across the concept of hoisting and scoping. The Scoping of JavaScript is the idea of an existing variable that has been redefined in a function would be completely new, which is undefined if not assigned with definition values. However, the output of the existing variable would be unaffected outside the function. The illustration below shows the details looking on hoisting and scoping of JavaScript. The term Hoisting, meaning that the declaration is actually before the if loop.
The Implementation of Witty
After analyzing the JavaScript codes, I decided to implement the code into Witty, where I have to start learning the implementation of JavaScript in Witty. And finally, the code is working well in Witty. However, the working code is with the binary file that has been provided. I tried to implement the new Hello World program into binary file, to be read by Witty, but I was getting error of undefined instruction of 0 that has been returned by the Hello World program. I am still finding the way to get rid of the error of undefined 0. Anyway, I always have a thought that this is a good start for implementation of CPU in Witty, at least it has been proven that the concept of embedding a CPU using Witty is applicable and it definitely require more effort to fully understand the code implementation of Angel.
0 Comments