How function parameters are compiled in JavaScript
I am trying to understand how functions — which could have dozens of parameters that could themselves be functions or complex objects (I’m thinking JavaScript) — get passed the arguments when compiled. I have seen before simple examples like this in C, which shows:
push $3 push $2 push $1 call _foo
That makes sense, but this is a simple case. I’m wondering for more complicated cases like in JavaScript where you might have:
foo(new Date, /asdf/i, < a: 1, b: 2 >, function(err)< >)
function a(a, b, c) < c.a = 1 c.b = 2 var d = [] foo(a, b, c, function(err, value)< d.push(value) >) return d > a(new Date, /asdf/i, <>)
I’m basically wondering in this last case, if foo(a, b, c, . ) gets a new scope object created, with these variables named and assigned to the parent scopes values, sort of like this
foo(parentScope.a, parentScope.b, parentScope.c)
In that way it wouldn’t have to construct all kinds of extra data. Basically wondering generally how arguments literally get passed to the functions when compiled in JavaScript.
Creates the Activation object or the variable object: Activation object is a special object in JS which contain all the variables, function arguments and inner functions declarations information. As activation object is a special object it does not have the dunder proto property.
Which version of V8? There have been (to my knowledge, and I don’t even follow its development) at least three different architectures. Originally, it was a single straight-to-native code compiler, no bytecode, no VM, no interpreter. Later, a it was two compilers, a fast one that produces non-optimized code, and a slow one that produces optimized code. And at some point, an interpreter was added that interprets an intermediate format. Also, AFAIK, there are no JavaScript implementations using LLVM, but I might be wrong.
1 Answer 1
You should keep three different kinds of things conceptually separate:
- How JavaScript is defined to work in the ECMA 262 standard
- How programming language functionality is commonly explained
- How a JavaScript implementation actually works
For example your stack operation example is a kind of white lie that is useful for explaining how things work, but not how a real-world implementation like V8 operates. Execution Contexts are part of the formal semantics of the ECMA 262 standard, but they might not actually be represented in an implementation – as long as the implementation works as if it implemented those semantics, everything is fine. ECMA 262 does not mention “activation objects” and only mentions the technical term “activation record” in a note.
This answer will only focus on the general concepts, not on the ECMA standard.
A variable is a name–value association, also called a binding. A scope or environment consists of bindings. So the scope is a bit like a JavaScript object, where each variable is a property of this scope object.
When a function is called, it receives values as parameters, not variables. This is why function arguments can be arbitrary expressions, for example like foo(x*2) : that calls foo with the value of x*2 . Therefore, a called function is not connected with the calling scope. It only gets some values, no bindings.
When a function is executed, it gets its own set of bindings with its local variables. This also includes any function parameters. If this helps, you can imagine the start of a function popping values of a call stack and placing the values into local variables:
For compiled code (and V8 compiles the code) this actually happens to be for free. The variables are not actually stored in a JavaScript object, but in a flat piece of memory that is accessed like a C struct, sometimes called the activation record or stack frame. During compilation the compiler doesn’t know where in memory that record will be, but it knows the offset of each variable’s storage relative to the start of the record. Then at run time, the record will be placed on the call stack, a memory region laid aside for function parameters, return values, and these records. The relative position of each function argument is already known, so it doesn’t have to be moved. The typical layout of a call stack might be:
| . | data of caller +---------+ | result | space for return value +---------+ | a | function arguments | b | | c | +---------+ | return | call metadata such as return location +---------+ | . | space for activation record
Things are more difficult with nested functions. In a way, a function is just an ordinary object that can be passed around as a value. However, the code in a function can access variables in surrounding scopes. Example:
Here, the inner() function accesses the outer scope’s x variable. We call such functions a closure.
In our “scopes are like JS objects” analogy, this would mean that our inner scope object has the outer scope object as prototype.
In the reality of call stacks, this causes a problem. Where bindings of an outer scope are captured by a closures, that part of the activation record might have to live longer than the stack frame. Then, a part of the activation record might be allocated outside of the stack (on the heap). This record will be garbage collected when no closures continue to reference these variables. The reference to the outer activation record is just a pointer that is passed as an implicit argument to the function. Since accesses to outer variables must go through a pointer, they are generally compiled differently to local variables.
| inner() function v +----------+----------+---------------+ | metadata | captures | compiled code | +----------+----------+---------------+ | v outer() activation record +-------+ | x = 2 | +-------+
There is a deep correspondence between closures and objects. When you call an object method, the object is passed as an implicit this parameter. Similarly, when you call a closure, the outer scope needs to be passed as an implicit parameter. A captured outer variable of a function is similar to a property of an object. But again: the scope is not actually implemented in terms of JavaScript objects.
Expression parsing and evaluation #
Expressions can be parsed and evaluated in various ways:
- Using the function math.evaluate(expr [,scope]) .
- Using the function math.compile(expr) .
- Using the function math.parse(expr) .
- By creating a parser, math.parser() , which contains a method evaluate and keeps a scope with assigned variables in memory.
Evaluate #
Math.js comes with a function math.evaluate to evaluate expressions. Syntax:
math.evaluate(expr) math.evaluate(expr, scope) math.evaluate([expr1, expr2, expr3, . ]) math.evaluate([expr1, expr2, expr3, . ], scope)
Function evaluate accepts a single expression or an array with expressions as the first argument and has an optional second argument containing a scope with variables and functions. The scope can be a regular JavaScript Object, or Map. The scope will be used to resolve symbols, and to write assigned variables or function.
The following code demonstrates how to evaluate expressions.
// evaluate expressions math.evaluate('sqrt(3^2 + 4^2)') // 5 math.evaluate('sqrt(-4)') // 2i math.evaluate('2 inch to cm') // 5.08 cm math.evaluate('cos(45 deg)') // 0.7071067811865476 // provide a scope let scope = a: 3, b: 4 > math.evaluate('a * b', scope) // 12 math.evaluate('c = 2.3 + 4.5', scope) // 6.8 scope.c // 6.8
Compile #
Math.js contains a function math.compile which compiles expressions into JavaScript code. This is a shortcut for first parsing and then compiling an expression. The syntax is:
math.compile(expr) math.compile([expr1, expr2, expr3, . ])
Function compile accepts a single expression or an array with expressions as the argument. Function compile returns an object with a function evaluate([scope]) , which can be executed to evaluate the expression against an (optional) scope:
const code = math.compile(expr) // compile an expression const result = code.evaluate([scope]) // evaluate the code with an optional scope
An expression needs to be compiled only once, after which the expression can be evaluated repeatedly and against different scopes. The optional scope is used to resolve symbols and to write assigned variables or functions. Parameter scope can be a regular Object, or Map.
// parse an expression into a node, and evaluate the node const code1 = math.compile('sqrt(3^2 + 4^2)') code1.evaluate() // 5
Parse #
Math.js contains a function math.parse to parse expressions into an expression tree. The syntax is:
math.parse(expr) math.parse([expr1, expr2, expr3, . ])
Function parse accepts a single expression or an array with expressions as the argument. Function parse returns the root node of the tree, which can be successively compiled and evaluated:
const node = math.parse(expr) // parse expression into a node tree const code = node.compile() // compile the node tree const result = code.evaluate([scope]) // evaluate the code with an optional scope
The API of nodes is described in detail on the page Expression trees.
An expression needs to be parsed and compiled only once, after which the expression can be evaluated repeatedly. On evaluation, an optional scope can be provided, which is used to resolve symbols and to write assigned variables or functions. Parameter scope is a regular Object or Map.
// parse an expression into a node, and evaluate the node const node1 = math.parse('sqrt(3^2 + 4^2)') const code1 = node1.compile() code1.evaluate() // 5 // provide a scope const node2 = math.parse('x^a') const code2 = node2.compile() let scope = x: 3, a: 2 > code2.evaluate(scope) // 9 // change a value in the scope and re-evaluate the node scope.a = 3 code2.evaluate(scope) // 27
Parsed expressions can be exported to text using node.toString() , and can be exported to LaTeX using node.toTex() . The LaTeX export can be used to pretty print an expression in the browser with a library like MathJax. Example usage:
// parse an expression const node = math.parse('sqrt(x/x+1)') node.toString() // returns 'sqrt((x / x) + 1)' node.toTex() // returns '\sqrt< >+ >'
Parser #
In addition to the static functions math.evaluate and math.parse , math.js contains a parser with functions evaluate and parse , which automatically keeps a scope with assigned variables in memory. The parser also contains some convenience functions to get, set, and remove variables from memory.
A parser can be created by:
The parser contains the following functions:
- clear() Completely clear the parser’s scope.
- evaluate(expr) Evaluate an expression. Returns the result of the expression.
- get(name) Retrieve a variable or function from the parser’s scope.
- getAll() Retrieve a map with all defined a variables from the parser’s scope.
- remove(name) Remove a variable or function from the parser’s scope.
- set(name, value) Set a variable or function in the parser’s scope.
The following code shows how to create and use a parser.
// create a parser const parser = math.parser() // evaluate expressions parser.evaluate('sqrt(3^2 + 4^2)') // 5 parser.evaluate('sqrt(-4)') // 2i parser.evaluate('2 inch to cm') // 5.08 cm parser.evaluate('cos(45 deg)') // 0.7071067811865476 // define variables and functions parser.evaluate('x = 7 / 2') // 3.5 parser.evaluate('x + 3') // 6.5 parser.evaluate('f(x, y) = x^y') // f(x, y) parser.evaluate('f(2, 3)') // 8 // get and set variables and functions const x = parser.get('x') // x = 3.5 const f = parser.get('f') // function const g = f(3, 3) // g = 27 parser.set('h', 500) parser.evaluate('h / 2') // 250 parser.set('hello', function (name) return 'hello, ' + name + '!' >) parser.evaluate('hello("user")') // "hello, user!" // clear defined functions and variables parser.clear()
Scope #
The scope is a data-structure used to store and lookup variables and functions defined and used by expressions.
It is passed to mathjs via calls to math.evaluate or simplify .
For ease of use, it can be a Plain Javascript Object; for safety it can be a plain Map and for flexibility, any object that has the methods get / set / has / keys , seen on Map .
Some care is taken to mutate the same object that is passed into mathjs, so they can collect the definitions from mathjs scripts and expressions.
evaluate will fail if the expression uses a blacklisted symbol, preventing mathjs expressions to escape into Javascript. This is enforced by access to the scope.
For less reliance on this blacklist, scope can also be a Map , which allows mathjs expressions to define variables and functions of any name.