首页 > 编程语言 >JavaScript works behind the scenes -- Engine and Runtime

JavaScript works behind the scenes -- Engine and Runtime

时间:2022-10-12 00:33:15浏览次数:52  
标签:Engine 执行 code works -- JavaScript call Runtime stack

what is a JavaScript engine?

program that executes JavaScript code. JavaScript引擎是运行JavaScript代码的程序。

how engine works?

JavaScript contains a call stack and a heap. (调用栈和堆)

the call stack is where our code is actually executed using something called execution contexts.

the heap is an unstructured memory pool which store all the objects that our application needs.

image-20221010222823131

how the code is compiled to machine code?(编译)

汇编语言:翻译成机器码然后执行

解释型语言:获取源代码和执行程序是同时进行的,但是相对于汇编语言,解释型语言编译非常慢

即时编译:这里没有可移植文件,源代码被编译成机器码之后立即执行(JavaScript是即时编译)

image-20221010222844461

how the code is executed in engine? (运行)

  1. 解析:源代码会变成AST,abstract tree,抽象语法树

    during the parsing process, the code is parsed into a data structure called the abstract tree. This works by first splitting up each line of code into pieces that are meaningful the language like the const or function keywords, and then saving all these pieces into the tree in a structured way. This step also checks if there are any syntax errors(语法错误) and the resulting tree will later be used to generate the machine code.

  2. 将ast编译成机器码

  3. 机器码将被立即执行(这一步发生在call stack中)

  4. 在程序运行的过程中,同时进行着一个优化的步骤,在已经运行的程序执行期间重新编译。优化之后,未被优化的代码被简单扫过,对于已优化的代码重新执行。

    这些步骤都是在引擎的特殊线程中执行,不会在主线程中执行

image-20221010222901644

runtime(运行机制)

web api:这些api都不属于JavaScript的范围,如果没有浏览器提供这些api机制,JavaScript也没有办法去控制比如说DOM。

callback query:在addEventListener这个函数里面,第一个参数我们定义的是监听的事件,比如说‘click’,我们监听的就是点击事件。第二个参数是回调函数,如监听到点击事件之后执行的函数,这个函数执行的机制是什么?当监听到点击事件的时候,我们会将回调函数放入callback query中等待执行,当call stack中没有方法的时候,callback query中的方法就会进去call stack中执行(事件循环)

image-20221010222912213

image-20221010222929639

标签:Engine,执行,code,works,--,JavaScript,call,Runtime,stack
From: https://www.cnblogs.com/kihyunBlog/p/16783107.html

相关文章

  • 02 RabbitMQ 3.8 Feature Focus - Quorum Queues
    标题:RabbitMQ3.8FeatureFocus-QuorumQueues原文:https://www.cloudamqp.com/blog/rabbitmq-quorum-queues.html时间:2019-03-28RabbitMQ3.8将于今年推出,它将带来四......
  • Ceph使用---Crush Map进阶
    一、CephCrushMap介绍ceph集群中由mon服务器维护的的五种运行图:Monitormap#监视器运行图OSDmap#OSD运行图PGmap#PG运行图Crushmap#(Controllersrepl......
  • 实验5:开源控制器实践——POX
    一、实验目的1.能够理解POX控制器的工作原理;2.通过验证POX的forwarding.hub和forwarding.l2_learning模块,初步掌握POX控制器的使用方法;3.能够运用POX控制器编写自定义网......
  • 实验5:开源控制器实践——POX
    实验5:开源控制器实践——POX(一)基本要求1、使用tcpdump验证Hub模块h1pingh2h1pingh32、阅读L2_learning模块代码,画出程序流程图3、使用tcpdump验......
  • CentOS 7 安全基线检查
    注意:操作时建议做好记录或备份1.设置密码失效时间|身份鉴别描述:设置密码失效时间,强制定期修改密码,减少密码被泄漏和猜测风险,使用非密码登陆方式(如密钥对)请忽略此项......
  • unity界面介绍及导入模型
    unity界面介绍排版​ 一般刚打开unity默认是如下界面,可以自己拖拽为自己喜欢的布局,也可以使用右上角的Layout中来选择布局。目前显示出来的只是最常用的界......
  • LeetCode算法笔记 566. 重塑矩阵
    importjunit.framework.TestCase;importjava.util.Arrays;publicclassLeetCode04_1extendsTestCase{/***566.重塑矩阵*在MATLAB中,有......
  • 实验5:开源控制器实践——POX
    (一)基础要求搭建下图所示SDN拓扑,协议使用OpenFlow1.0,控制器使用部署于本地的POX(默认监听6633端口)阅读Hub模块代码,使用tcpdump验证Hub模块;h1pingh2时,h2和h3都能接......
  • Java中equals和==区别
    “==”和equals的区别......
  • Flink Table API 的开发步骤
    1.创建TableEnvironmentFlink的TableAPI/SQL的执行入口功能如下:1.1注册Catalog【数据系统实例】1.2在Catalog中注册库和表1.3加载插件模块1.4执行SQL的查询1.......