What is a declarative environment record and how does it differ from an activation object?

Okay, that's why I recently read about the lexical environment of ES-5, and I'm not sure if I really understand what is happening with how the variables are stored in EcmaScript. I did some research, but he did not clarify my information, but only brought me to two questions. So here they are:

  • The first is ES-3 activations objects / variable objects . After reading the ES-3 specification and some sources on the Internet, I can assume that they are a regular object, such as those created by new Object , but none of the sources say β€œyes, it's just a simple object” directly. Moreover, Dmitry Soshnikov wrote on his blog (my emphasis):

    Schematically and for examples, you can represent a variable object as a regular ECMAScript object

    and this quote does not allow me to be sure what an activation object is. So this is the first question: is an ordinary EcmaScript object an activation object? If not, then what else?

  • In ES-5, we now have object environment records , which seem to be more or less the same as ES-3 activation objects, and declarative environment records , which replaced activation objects in functions and the try-catch operator. So, assuming that the object environment records are just EcmaScript objects, then what is a declarative environment record? The specification does not clarify this, moreover, from what I read there, I cannot imagine that this is not implemented as an object. So, if declarative environment entries are not ES objects, then what are they and how are they implemented and presented at runtime?

Thank you very much for covering this topic for me.

EDIT . I think I need to clarify what this question is about. The main thing I want to know is the exact difference between the records of objects of objects and the object environment and the declarative data of the environment. This is what interests me the most.

+6
source share
1 answer

First of all, you should know that all these terms simply describe concepts. They do not dictate any implementation. But, since it can be difficult to imagine / visualize, it can be useful to think of these concepts as how you know, for example, maps or tables.

Declarative Environment Records (DER) and Object Environment Records (OER) have one thing in common: they are environment (ER) records that are defined in the as specification :

Identifier bindings created within the scope of the associated Lexical environment are recorded in the environment record.

This basically means that ER keeps track of variable names and function names and their associated values.

Consider the following example:

 var foo = 42; function bar() { }; 

The corresponding ER will have two entries, one for foo and one for bar . If you imagine that ER is a table, then it will look like

  name value ---------------------- foo 42 bar <function object> 

Now about the differences between DER and OER. DER may be the easiest to understand.

Declarative Environment Record

The declarative term should sound familiar, as we often talk about variable declarations and function declarations. The specification states:

Each declarative environment entry is associated with an area of ​​the ECMAScript program that contains declarations of variables and / or functions. A declarative environment record associates a set of identifiers defined by declarations contained in its scope.

So when you see

 var foo = 42; 

or

 function bar() { } 

then you can assume that their names and values ​​are stored in DER.

Object Environment Record

OERs are less common, but at least one OER exists in every JS application. The specification describes it as

Each object environment record is associated with an object called its binding object. An object environment record associates a set of identifier names that directly correspond to the property names of the binding object.

Have you ever wondered why the properties of the window object are global variables? This is because the global scope ER is OER: window is a binding object, and a corresponding record is created in OER for each of its properties. This is also indicated in the specification:

Global environment An environment record is an object environment record whose binding object is a global object.

Here is an example: Let's assume that the binding object

 var obj = { answer: 42 }; 

then oer will be

  name value ------------------------ answer 42 

Note that in this case, the binding object ( obj ) is actually a JavaScript object. You are in the same situation when using the with statement:

 var obj = { foo: 42; }; with (obj) { foo = foo / 2; } console.log(obj); 

with creates OER and populates it with the names and values ​​of the properties from the passed object. This is why you can access them without explicitly accessing them through obj.* . OER will also necessarily update the binding object with a new value if one of them is assigned to one of the identifiers.


Activation object

This is similar to ES3, activation objects (AO), which are automatically created when the function is executed and contain a link to a special arguments object. This seems to be related to DER, but should still be something independent.
Apparently, the concept of ES does not exist in ES5, and I assume that it is not needed, since arguments can be added directly to the DER execution context.

Execution context

A new execution context (EC) is set up whenever the function is executed and used to track the execution status:

The execution context contains any state necessary to track the execution of the code associated with it.

This means that the engine can add any information needed to track progress. But the specification also defines the components that the EC should have, one of which is VariableEnvironment, which is ER (probably always DER, but I don't know for sure). This means that ER is part of the EU.

+12
source

Source: https://habr.com/ru/post/958624/


All Articles