<p>As we all know, the newly open source “Hongmeng 2.0” uses JavaScript as IoT Framework language for application development. This marks that JavaScript has once again become a hot topic at the news network level after SpaceX took off. It would be a pity to only use Yin and Yang for such a good opportunity. As a popular science, this article will not use a magnifying glass to find out the flaws in the code to find faults. Instead, I hope to explain in a popular way what the GUI it supports is all about. As long as you have a general understanding of computer basics, there should be no obstacles to reading this article.
<p>We already know that on "Hongmeng 2.0", developers only need to write JavaScript business logic in the form of Vue components, and they can be rendered into UI interfaces on embedded hardware such as smart watches. What core modules need to be involved in this process? Which of these modules are self-developed, and which ones use ready-made open source projects? It is divided into three abstract layers from top to bottom to introduce:
JS framework layer, which can be understood as a greatly simplified Vue-style JavaScript framework
JS engine and runtime layer, can be understood as a greatly simplified WebKit-style runtime
Graphics rendering layer, can be understood as a greatly simplified Skia-style graphics drawing library
<p>These three abstraction layers form a GUI technology stack for embedded hardware. Different from many public opinions that shout "unclear/unfathomable", I personally think that at least for the GUI part, any front-line developer in China who has been exposed to the current mainstream Hybrid cross-end solutions or JS runtime development can easily start from the source code. Set out to understand it. Let’s do some interpretation and analysis of it layer by layer.
JS Framework Layer
<p>From the top-level perspective, if you want to use "Hongmeng 2.0" to render a dynamic text, you only need to write the following HML (XML-like) format code:
<p> In this way, as long as the text is clicked, the boil method will be called to make PPT change Into nuclear weapons.
<p>What happened behind this? Students who are familiar with Vue 2.0 should immediately think of the following things:
requires a preprocessing mechanism for XML to convert it into a nested function structure in JS. In this way, you only need to do a simple eval at runtime, and you can use JS to generate a UI that conforms to the XML structure.
An event mechanism is required so that the corresponding callback can be executed when the onclick event is triggered.
Requires a data hijacking mechanism so that the corresponding callback can be executed when assigning a value to this.hello.
Need to be able to update UI object controls in callbacks.
<p>How are these things achieved? To put it simply, it is this:
XML preprocessing relies on the ready-made NPM open source package to convert the onclick attribute in XML into a JS object. Attribute field.
Event registration and triggering are implemented directly in C. The onclick attribute of the JS object obtained in the previous step will be checked and registered in C, which is equivalent to all components being native.
Data hijacking mechanism is implemented in JS and is a ViewModel (a few hundred lines in size) based on Object.defineProperty.
The update of the UI control will be implemented by calling the native method of C in the JS callback automatically executed by the ViewModel. This part is completely done implicitly and does not expose the document.createElement-style standardized API.
<p> Since a large number of capabilities in common JS frameworks are directly implemented into C, the entire GUI technology stack is implemented using pure JavaScript (mainly see ace_lite_jsfwk under the warehouse core/index.js, observer.js and subject.js), which is equivalent to having and only one function:
<p>A ViewModel that can be watched.
<p>As for the implementation complexity and quality of the pure JS framework part, objectively speaking, if it is a personal amateur work, it can be used as a good bonus in the school recruitment interview.
JS engine and runtime layer
<p>After understanding the JS framework layer, we can either think that "Hongmeng 2.0" has chosen to deeply customize the highly simplified Vue into C, or we can think that It implements a supporting front-end framework tightly around a highly simplified (and private) DOM. Therefore, if we want to continue to explore the principles of this GUI, we must enter its C part and understand the implementation of its JS engine and runtime layer.
<p>What are the differences and connections between JS engine and runtime? JS engines generally only need to comply with the ECMA-262 specification, which does not define any platform APIs with "side effects". From setTimeout to document.getElementById to console.log to fs.readFile, these functions that can perform actual IO operations are all It needs to be provided by the runtime of "glue the engine API and platform API together". The principle of the runtime itself is not complicated. For example, in my personal article "From JS Engine to JS Runtime", you can see how to build a runtime yourself with the help of the ready-made QuickJS engine.
<p>So how is the JS runtime built in "Hongmeng 2.0"? There are several key points:
The JS engine chose JerryScript, an embedded JS engine developed by Samsung.
Each XML tag component in the shape of <text> and <p> corresponds to a C Component class bound to JerryScript, such as TextComponent and pComponent etc.
In addition to UI native objects, there are also a series of built-in modules prefixed with @system in JS, which provide platform capabilities such as Router / Audio / File available in JS (See ohos_module_config.h).
<p> Router is particularly worth mentioning here. It is very different from the implementation principles of common Web platform routing such as vue-router. It is specially customized deeply during runtime (see router_module.cpp, js_router.cpp and js_page_state_machine.cpp). Simply put, this "routing" is implemented like this:
Call the router.replace native method of switching pages in JS and enter C. In
C, load the new page JS according to the new page URI path (such as pages/detail), create a new page state machine instance, and switch it to the Init state.
In the Init process of the new state machine, call the JS engine to eval the JS code of the new page and obtain the ViewModel of the new page.
Append route parameters to the ViewModel and destroy the old state machine and the JS objects on it.
<p>So we can find that the so-called "switch route" here is actually closer to the "refresh page" of the web browser. So can we think that the capabilities of this JS runtime can already benchmark the WebKit-level browser kernel?
<p>Of course it’s still far from it. Compared with WebKit, it does not support the parsing of HTML and CSS (both will be parsed and converted into JS with the same execution effect during the development phase), nor does it have the challenge of continuously dynamically loading, parsing and executing resources in the browser (small The program is nothing more than a few local static JS files). As for typesetting, layout and rendering, there is naturally a big gap, which will be mentioned in the last section.
<p>In addition, I believe many students will be curious about the JerryScript engine. This section concludes by sharing some personal information on this issue.
<p>JerryScript engine is a JS interpreter specially implemented for embedded hardware and only supports the ES5.1 standard. In QuickJS Benchmark, you can see their performance comparison results:
<p>You can see that in terms of performance, JerryScript is significantly weaker in engines without JIT For QuickJS and Hermes. If compared with V8 with JIT turned on, it will even be two orders of magnitude slower. Therefore, this is a very specific engine for low-end devices. If you need to support basic libraries that are standard in medium and large front-end projects such as React and Vue (or even their corresponding family buckets), you may still need to use a more powerful engine.
<p> Regarding the use of JerryScript, RT-Thread founder @midnightbear is undoubtedly the one who has heavy application experience in the same scenario. The smart watch they developed in cooperation with a domestic first-tier manufacturer used JerryScript to implement the UI. The current product is available now It's about to be launched. Some feedback from their team on the use of JerryScript is also consistent with the above evaluation. In summary, it is as follows:
Execute code like this.hello = 'PPT' in JS to trigger dependency tracking.
JS dependency tracking callback triggers the native function and updates the Component component state of C.
Component updates the state of its bound UIView subclass and triggers graphics library updates.
The graphics library updates the pixel status in memory and completes drawing.
<p>This is my personal interpretation of the GUI technology stack of "Hongmeng 2.0". Due to limited time, I did not delve further, and (civilized) criticisms and corrections are welcome.
Summary
<p>Special statement: This subjective review is only for the current GUI framework of “Hongmeng 2.0”, please do not misinterpret it at will.
<p> Regarding the highlights of "Hongmeng 2.0" in the GUI part, I can personally think of these:
It does have pragmatic code (but it is completely different from the PPT introduction back then).
It is not a WebView shell, the layout and drawing are done by myself.
You don’t need to have computer knowledge beyond the undergraduate level to read and understand smoothly.
<p>As for the obvious (not just some ugly lines of code) deficiencies or problems, it seems that there are currently the following:
JS Framework Layer
No basic inter-component communication (such as props/emit, etc.) capabilities
No basic custom component capabilities
No State management capabilities other than basic dependency tracking
JS engine and runtime layer
Standard support is too low to run next-generation front-ends that require Proxy such as Vue 3.0 The framework
The performance level is weak and it is difficult to support medium and large JS applications
There is no open DOM-style object model API, which is not conducive to the upper layer smoothing the differences
Graphics Rendering Layer
No substantial GPU acceleration available
No advanced rendering capabilities such as SVG and rich text
Canvas Complete Low degree, lack of state stack and many APIs
<p> It seems that there are many shortcomings, but would you accuse the car of not having a jet engine? For scenarios of different complexity, there are naturally different optimal architecture designs. At present, it seems that this design is indeed very suitable for embedded hardware and simple "small programs" scenarios. But if we look at it according to the so-called "distributed full-scenario cross-platform" requirements, the complexity of this architecture is completely incomparable to modern web browsers or iOS and Android GUIs. If you want to implement it on a mobile phone, you will almost certainly need to add a large number of complex modules and undergo substantial architectural evolution and redesign.
<p>Of course, car manufacturers will not say that they build airplanes, right?
<p>In short, this is indeed a plate of mapo tofu made by myself, but it is not the Manchu-Han banquet as some people say.
<p>Finally, a personal subjective comment:
<p>First of all, this GUI technology stack has reached the mainstream level that can be obtained when assembling and drawing on open source products. However, in terms of performance and expressiveness, there is still an order of magnitude generational gap between its core modules and industry-cutting-edge industry-university-research cutting-edge solutions such as Microsoft MakeCode.
<p>Secondly, don’t think of it as Rocket Science, which requires precision calculations by a large number of experts - I am not belittling independent research and development, but I sincerely hope that everyone can understand, "I can also actually participate in this matter !" The operating system and GUI are not so mysterious. There are many domestic mature open source products available for learning, use and contribution (by the way, RT-Thread, which is very easy to experience and also made in China, is recommended as an early adopter). After all, only if you truly understand what a product is technically like, will it be less likely to be manipulated by people with ulterior motives, right?
<p>Finally, for all front-end developers who are familiar with JavaScript, Why do you still laugh at Hongmeng in a weird way? Hongmeng is the wealth code of JavaScript in China! JavaScript is adopted by a "national weapon" like Hongmeng, which can greatly enhance the front-end's road confidence, theoretical confidence, cultural confidence and technology stack confidence. As long as we combine splicing and self-research in this way, we can gain a high reputation across the country in one fell swoop. This road is really fascinating (whispering)
<p>We must unite and vigorously promote and publicize JavaScript’s status as a nuclear deterrent in the competition between great powers must rise to a level where as long as you say you can write JavaScript, everyone will respect you - as long as you are a front-end programmer, you can jump in line when buying a ticket, give up your seat when taking a bus, and get a room. You can have sex for free...the good times are coming!
<p> Do you want to be a pillar of the country? Let’s write JavaScript!
<p>No more words, I am going to work hard to rejuvenate the country!
<p> If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of Let's take a look at Hongmeng JavaScript GUI technology stack. For more information, please follow other related articles on the PHP Chinese website!