---
# System prepended metadata

title: ECMAScript
tags: [pl]

---

---
title: ECMAScript
author: bynx
tags: pl
---

# ECMAScript
## Structural Type System
### **tl;dr**
- Objects are hashtables
- Functions are **call-able** Objects (Objects w/ `[[Call]]`)
- Arrays are Objects whose _integer index_ is a numeric value $i$ is **in the range** $+0𝔽 ≤ i < 𝔽$

---

A _structural_ (or _property-based_) _type system_ is a major class of type system in which **type compatibility and equivalence are determined by the type’s actual structure** or definition and not by other characteristics such as its name or place of declaration. 

Structural systems are used to determine if types are equivalent and whether a type is a subtype of another. It contrasts with _nominative_ type systems, where comparisons are based on the names of the types or explicit declarations, and duck typing, in which only the part of the structure accessed at runtime is checked for compatibility.


## Graphical View

Graphical view of ECMAScript's _structual_ type system. Internal `[[Data]]` and `Method()` slots are included for completness: 
```mermaid
classDiagram
  %% Primitives
  class Undefined
  class Boolean
  class String
  class Symbol
  class Number
  class BigInt
  class Object
  class Null
  
  class Primitive_Types {
    Data_Types
    Structural_Types
  
  }
  Primitive_Types --> Data_Type
  Data_Type ..> Undefined
  Data_Type ..> Boolean
  Data_Type ..> String
  Data_Type ..> Number
  Data_Type ..> BigInt
  Data_Type ..> Symbol
  
  Primitive_Types --> Structural_Type
  Structural_Type ..> Object
  Structural_Type ..> Null
  Structural_Type ..> Indexed_Collections
  Structural_Type ..> Keyed_Collections
```

### Ordinary and Exotic Objects

```mermaid
classDiagram
  class Object {
    <<%Object.prototype%>>
  	[[Prototype]] : null
    [[Extensible]] : true
    GetPrototypeOf()
  	SetPrototypeOf()
  	IsExtensible()
  	PreventExtensions()
  	GetOwnProperty()
  	DefineOwnProperty()
  	OwnPropertyKeys()
  	HasProperty()
  	Get()
  	Set()
  	Delete()
  }
  class Function {
    <<Ordinary Object>>
    <<%Function.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[Extensible]]
    [[SourceText]]
    [[InitialName]]

    [[Environment]]
    [[FormalParameters]]
    [[ECMAScriptCode]]
    [[Realm]]
    [[ScriptOrModule]]
    [[ThisMode]]
    [[Strict]]
    [[HomeObject]]
    [[IsClassConstructor]]
    Call()
  }
  class Boolean {
    <<Ordinary Object>>
    <<%Boolean.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[BooleanData]]: false
    thisBooleanValue(value)
  }
  class Symbol {
    <<Ordinary Object>>
    <<%Symbol.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[SymbolData]]: false
  }
 
  %% https://262.ecma-international.org/12.0/#sec-bound-function-exotic-objects
  class Bound_Function {
    <<Exotic Object>>
    Call()
    Construct()
  }
  %% https://262.ecma-international.org/12.0/#sec-array-exotic-objects
  class Array {
    <<Exotic Object>>
    DefineOwnProperty( P, Desc )
  }
  %% https://262.ecma-international.org/12.0/#sec-string-exotic-objects
  class String {
    <<Exotic Object>>
   	[[StringData]]
    GetOwnProperty( P )
  	DefineOwnProperty( P, Desc )
  	OwnPropertyKeys( )
  }
  %% https://262.ecma-international.org/12.0/#sec-integer-indexed-exotic-objects
  class Integer_Indexed {
    <<Exotic Object>>
    [[ViewedArrayBuffer]]
    [[ArrayLength]]
    [[ByteOffset]]
    [[ContentType]]
    [[TypedArrayName]]
    GetOwnProperty( P )
    HasProperty( P )
    DefineOwnProperty( P, Desc )
    Get ( P, Receiver )
    Set( P, V, Receiver)
    Delete( P )
    OwnPropertyKeys( )
  }
  
  %%class Arguments {
  %%  <<Exotic Object>>
  %%}
  %%class Module_Namespace {
  %%  <<Exotic Object>>
  %%}
  %%class Immutable_Prototype {
  %%  <<Exotic Object>>
  %%}
  class Proxy_Object {
    <<Exotic Object>>
    [[ProxyHandler]]
    [[ProxyTarget]]
  }

  %% Type system view
  Object ..> Function            : Ordinary
  Object ..> Boolean             : Ordinary
  Object ..> Symbol              : Ordinary

  Object ..> Bound_Function      : Exotic
  Object ..> Array               : Exotic
  Object ..> String              : Exotic
  Object ..> Integer_Indexed     : Exotic
  %%Object ..> Arguments           : Exotic
  %%Object ..> Module_Namespace    : Exotic
  %%Object ..> Immutable_Prototype : Exotic
  Object ..> Proxy_Object        : Exotic 
```

### Ordinary

```mermaid
classDiagram
  class Object {
    <<%Object.prototype%>>
  	[[Prototype]] : null
    [[Extensible]] : true
    GetPrototypeOf()
  	SetPrototypeOf()
  	IsExtensible()
  	PreventExtensions()
  	GetOwnProperty()
  	DefineOwnProperty()
  	OwnPropertyKeys()
  	HasProperty()
  	Get()
  	Set()
  	Delete()
  }
  class Function {
    <<Ordinary Object>>
    <<%Function.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[Extensible]]
    [[SourceText]]
    [[InitialName]]

    [[Environment]]
    [[FormalParameters]]
    [[ECMAScriptCode]]
    [[Realm]]
    [[ScriptOrModule]]
    [[ThisMode]]
    [[Strict]]
    [[HomeObject]]
    [[IsClassConstructor]]
    Call()
  }
  class Boolean {
    <<Ordinary Object>>
    <<%Boolean.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[BooleanData]]: false
    thisBooleanValue(value)
  }
  class Symbol {
    <<Ordinary Object>>
    <<%Symbol.prototype%>>
    [[Prototype]] : %Object.prototype%
    [[SymbolData]]: false
  }

  %% Type system view
  Object ..> Function            : Ordinary
  Object ..> Boolean             : Ordinary
  Object ..> Symbol              : Ordinary
```


### Exotic

Exotic Objects are Ordinary Objects that have a special implementation of some
internal (method) slots.

```mermaid
classDiagram
  class Object {
    <<%Object.prototype%>>
  	[[Prototype]] : null
    [[Extensible]] : true
    GetPrototypeOf()
  	SetPrototypeOf()
  	IsExtensible()
  	PreventExtensions()
  	GetOwnProperty()
  	DefineOwnProperty()
  	OwnPropertyKeys()
  	HasProperty()
  	Get()
  	Set()
  	Delete()
  }
  %% https://262.ecma-international.org/12.0/#sec-bound-function-exotic-objects
  class Bound_Function {
    <<Exotic Object>>
    Call()
    Construct()
  }
  %% https://262.ecma-international.org/12.0/#sec-array-exotic-objects
  class Array {
    <<Exotic Object>>
    DefineOwnProperty( P, Desc )
  }
  %% https://262.ecma-international.org/12.0/#sec-string-exotic-objects
  class String {
    <<Exotic Object>>
   	[[StringData]]
    GetOwnProperty( P )
  	DefineOwnProperty( P, Desc )
  	OwnPropertyKeys( )
  }
  %% https://262.ecma-international.org/12.0/#sec-integer-indexed-exotic-objects
  class Integer_Indexed {
    <<Exotic Object>>
    [[ViewedArrayBuffer]]
    [[ArrayLength]]
    [[ByteOffset]]
    [[ContentType]]
    [[TypedArrayName]]
    GetOwnProperty( P )
    HasProperty( P )
    DefineOwnProperty( P, Desc )
    Get ( P, Receiver )
    Set( P, V, Receiver)
    Delete( P )
    OwnPropertyKeys( )
  }
  class Proxy_Object {
    <<Exotic Object>>
    [[ProxyHandler]]
    [[ProxyTarget]]
  }

  %% Type system view
  Object ..> Bound_Function      : Exotic
  Object ..> Array               : Exotic
  Object ..> String              : Exotic
  Object ..> Integer_Indexed     : Exotic
  Object ..> Proxy_Object        : Exotic 
```



