Both interpreted execution and direct execution binaries use a stack structure. So why use a stack structure to manage function calls?

1. Why stack management function calls

In general functions have two main properties:

  • Functions can be called. When a function call occurs, control of the executing code is transferred from the parent function to the child function, and when the child function completes, control of the code is returned to the parent function.
  • Functions have a scope mechanism. When a function is executed, variables defined inside the function can be isolated from the external environment. Variables defined inside the function cannot be accessed from the external environment, and internal variables are destroyed after the function is executed.

The life cycle of a function caller (parent function) is always longer than that of the called (child function), and the life cycle of the called (child function) always ends before that of the caller (parent function). From the perspective of function resource allocation and reclamation, resource allocation of the called function (child function) is always later than that of the calling function (parent function), and resource release is also earlier than that of the calling function (parent function). LIFO is a LIFO LIFO policy, and the stack structure is the same, so the stack is used to manage the function call relationship.

2. How does the stack manage function calls

  • X =5, the variable x is pushed on the stack for the first time.
  • Y =6, the variable y is pushed on the stack for the first time.
  • If x=100, replace the value of x that was pushed to the stack. The value of x is 100 instead of 5.
  • Compute the value of x+y, assign it to z, and push it onto the stack.

During the execution of a function, its internal variables are pushed onto the stack in the order in which they are executed. When the parent function inserts a child function, the child function call ends, the function execution authority will be returned to the parent function, this recovery process is called recovery scene.

The recovery field method is to store a pointer in the register that always points to the top of the stack. The top of the stack pointer tells you where to add new elements. This pointer is usually stored in the ESP register. At the same time, another EBP register is added to hold the starting position of the current function (parent function). This position is called the stack frame pointer.

Each stack frame corresponds to an unfinished function, and the stack frame holds the return address and local variables of that function. In JS, function execution is similar. When a new function is called, V8 creates a stack frame for the function and destroys the stack frame when the function is finished. The capacity of stack structure is fixed, if not destroyed, it is easy to lead to stack overflow.

3. The role of the heap

The disadvantage of the stack is that it cannot allocate a large contiguous space in memory, so the stack space is limited. Now you have heap space to store some big data.

Data in the heap space does not require continuous storage, and there is no fixed pattern for allocating memory from the heap. It can be allocated and released at any time. When big data is encountered, a chunk of space is allocated in the heap and the allocated memory address is returned, which is stored on the stack. In pp, for example, the address in the stack refers to the spatial address allocated in the heap.

When data in the heap is no longer needed, it needs to be destroyed. If it is not destroyed in time, memory leaks may occur.

4. To summarize

  • A stack structure is used to manage the call process of functions, called the call stack.
  • The maximum stack capacity is limited, which may cause stack overflow. So use the heap to access big data, and then store the reference address of the heap on the stack.
  • To solve stack overflow, synchronous functions can also be separated into asynchronous functions.

Write in the last

A summary of V8 learning comes from geek Time teacher Li Bing’s course, Illustrated Ole V8. For more details, check out the course.