26 메모리 관리

26.1 WeakRef 객체

WeakRef는 garbage collection으로부터 target object 또는 symbol을 preserving하지 않고 그것을 refer하는 데 사용되는 object입니다. target이 garbage collection에 의해 reclaimed되지 않았다면, target value에 대한 access를 allow하기 위해 WeakRefs는 dereferenced될 수 있습니다.

26.1.1 WeakRef 생성자

WeakRef constructor는:

  • %WeakRef%입니다.
  • global object"WeakRef" property의 initial value입니다.
  • constructor로 called될 때 new WeakRef를 create하고 initialize합니다.
  • function으로 called되도록 intended되지 않았으며, 그러한 방식으로 called되면 exception을 throw합니다.
  • 클래스 정의의 extends 절의 값으로 사용될 수 있다. 지정된 WeakRef 동작을 상속하려는 서브클래스 생성자는 WeakRef.prototype의 내장 메서드를 지원하는 데 필요한 내부 상태로 서브클래스 인스턴스를 생성하고 초기화하기 위해 WeakRef 생성자에 대한 super 호출을 포함해야 한다.

26.1.1.1 WeakRef ( target )

이 function은 called될 때 다음 steps를 수행합니다:

  1. NewTarget이 undefined이면, TypeError exception을 throw한다.
  2. CanBeHeldWeakly(target)가 false이면, TypeError exception을 throw한다.
  3. weakRef를 ? OrdinaryCreateFromConstructor(NewTarget, "%WeakRef.prototype%", « [[WeakRefTarget]] »)로 둔다.
  4. AddToKeptObjects(target)를 수행한다.
  5. weakRef.[[WeakRefTarget]]target으로 설정한다.
  6. weakRef를 반환한다.

26.1.2 WeakRef 생성자의 속성

WeakRef constructor는:

  • value가 %Function.prototype%[[Prototype]] internal slot을 가집니다.
  • 다음 properties를 가집니다:

26.1.2.1 WeakRef.prototype

WeakRef.prototype의 initial value는 WeakRef prototype object입니다.

이 property는 attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }를 가집니다.

26.1.3 WeakRef 프로토타입 객체의 속성

WeakRef prototype object는:

  • %WeakRef.prototype%입니다.
  • value가 %Object.prototype%[[Prototype]] internal slot을 가집니다.
  • ordinary object입니다.
  • [[WeakRefTarget]] internal slot을 가지지 않습니다.

26.1.3.1 WeakRef.prototype.constructor

WeakRef.prototype.constructor의 initial value는 %WeakRef%입니다.

26.1.3.2 WeakRef.prototype.deref ( )

이 method는 called될 때 다음 steps를 수행합니다:

  1. weakRefthis value로 둔다.
  2. RequireInternalSlot(weakRef, [[WeakRefTarget]])를 수행한다.
  3. WeakRefDeref(weakRef)를 반환한다.
Note

WeakRefundefined가 아닌 target value를 반환하면, 이 target value는 ECMAScript code의 current execution이 completed될 때까지 garbage collected되어서는 안 됩니다. AddToKeptObjects operation은 read consistency가 maintained되도록 보장합니다.

let target = { foo() {} };
let weakRef = new WeakRef(target);

// ... later ...

if (weakRef.deref()) {
  weakRef.deref().foo();
}

위 example에서 first deref가 undefined로 evaluate되지 않으면 second deref도 그럴 수 없습니다.

26.1.3.3 WeakRef.prototype [ %Symbol.toStringTag% ]

%Symbol.toStringTag% property의 initial value는 String value "WeakRef"입니다.

이 property는 attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }를 가집니다.

26.1.4 WeakRef Abstract Operations

26.1.4.1 WeakRefDeref ( weakRef )

The abstract operation WeakRefDeref takes argument weakRef (a WeakRef) and returns an ECMAScript language value. It performs the following steps when called:

  1. targetweakRef.[[WeakRefTarget]]으로 둔다.
  2. targetempty가 아니면, 다음을 수행한다.
    1. AddToKeptObjects(target)를 수행한다.
    2. target을 반환한다.
  3. undefined를 반환한다.
Note

이 abstract operation은 liveness를 succinctly define할 수 있도록 strictly WeakRef.prototype.deref와 separately defined됩니다.

26.1.5 WeakRef 인스턴스의 속성

WeakRef instances는 WeakRef prototype object로부터 properties를 inherit하는 ordinary objects입니다. WeakRef instances는 또한 [[WeakRefTarget]] internal slot을 가집니다.

26.2 FinalizationRegistry 객체

FinalizationRegistry는 target objects와 symbols가 garbage collected될 때 performed되는 cleanup operations의 registration과 unregistration을 manages하는 object입니다.

26.2.1 FinalizationRegistry 생성자

FinalizationRegistry constructor는:

  • %FinalizationRegistry%입니다.
  • global object"FinalizationRegistry" property의 initial value입니다.
  • constructor로 called될 때 new FinalizationRegistry를 create하고 initialize합니다.
  • function으로 called되도록 intended되지 않았으며, 그러한 방식으로 called되면 exception을 throw합니다.
  • 클래스 정의의 extends 절의 값으로 사용될 수 있다. 지정된 FinalizationRegistry 동작을 상속하려는 서브클래스 생성자는 FinalizationRegistry.prototype의 내장 메서드를 지원하는 데 필요한 내부 상태로 서브클래스 인스턴스를 생성하고 초기화하기 위해 FinalizationRegistry 생성자에 대한 super 호출을 포함해야 한다.

26.2.1.1 FinalizationRegistry ( cleanupCallback )

이 function은 called될 때 다음 steps를 수행합니다:

  1. NewTarget이 undefined이면, TypeError 예외를 던진다.
  2. IsCallable(cleanupCallback)이 false이면, TypeError 예외를 던진다.
  3. finalizationRegistry를 ? OrdinaryCreateFromConstructor(NewTarget, "%FinalizationRegistry.prototype%", « [[Realm]], [[CleanupCallback]], [[Cells]] »)라고 하자.
  4. activeFunc활성 함수 객체라고 하자.
  5. finalizationRegistry.[[Realm]]activeFunc.[[Realm]]으로 설정한다.
  6. finalizationRegistry.[[CleanupCallback]]HostMakeJobCallback(cleanupCallback)으로 설정한다.
  7. finalizationRegistry.[[Cells]]를 새로운 빈 List로 설정한다.
  8. finalizationRegistry를 반환한다.

26.2.2 FinalizationRegistry 생성자의 속성

FinalizationRegistry constructor는:

  • value가 %Function.prototype%[[Prototype]] internal slot을 가집니다.
  • 다음 properties를 가집니다:

26.2.2.1 FinalizationRegistry.prototype

FinalizationRegistry.prototype의 initial value는 FinalizationRegistry prototype object입니다.

이 property는 attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: false }를 가집니다.

26.2.3 FinalizationRegistry 프로토타입 객체의 속성

FinalizationRegistry prototype object는:

  • %FinalizationRegistry.prototype%입니다.
  • value가 %Object.prototype%[[Prototype]] internal slot을 가집니다.
  • ordinary object입니다.
  • [[Cells]][[CleanupCallback]] internal slots를 가지지 않습니다.

26.2.3.1 FinalizationRegistry.prototype.constructor

FinalizationRegistry.prototype.constructor의 initial value는 %FinalizationRegistry%입니다.

26.2.3.2 FinalizationRegistry.prototype.register ( target, heldValue [ , unregisterToken ] )

이 method는 called될 때 다음 steps를 수행합니다:

  1. finalizationRegistrythis value로 둔다.
  2. RequireInternalSlot(finalizationRegistry, [[Cells]])를 수행한다.
  3. CanBeHeldWeakly(target)가 false이면, TypeError exception을 throw한다.
  4. SameValue(target, heldValue)가 true이면, TypeError exception을 throw한다.
  5. CanBeHeldWeakly(unregisterToken)가 false이면, 다음을 수행한다.
    1. unregisterTokenundefined가 아니면, TypeError exception을 throw한다.
    2. unregisterTokenempty로 설정한다.
  6. cellRecord { [[WeakRefTarget]]: target, [[HeldValue]]: heldValue, [[UnregisterToken]]: unregisterToken }로 둔다.
  7. cellfinalizationRegistry.[[Cells]]에 append한다.
  8. undefined를 반환한다.
Note

이 specification의 algorithms와 definitions에 기반하면, finalizationRegistry.[[Cells]]cell을 contains할 때 cell.[[HeldValue]]live입니다; 그러나 이는 반드시 cell.[[UnregisterToken]] 또는 cell.[[Target]]live임을 의미하지는 않습니다. 예를 들어, object를 itself를 unregister token으로 하여 registering해도 그 object가 forever alive로 유지되지는 않습니다.

26.2.3.3 FinalizationRegistry.prototype.unregister ( unregisterToken )

이 method는 called될 때 다음 steps를 수행합니다:

  1. finalizationRegistrythis value로 둔다.
  2. RequireInternalSlot(finalizationRegistry, [[Cells]])를 수행한다.
  3. CanBeHeldWeakly(unregisterToken)가 false이면, TypeError exception을 throw한다.
  4. removedfalse로 둔다.
  5. finalizationRegistry.[[Cells]]의 each Record { [[WeakRefTarget]], [[HeldValue]], [[UnregisterToken]] } cell에 대해, 다음을 수행한다.
    1. cell.[[UnregisterToken]]empty가 아니고 SameValue(cell.[[UnregisterToken]], unregisterToken)가 true이면, 다음을 수행한다.
      1. cellfinalizationRegistry.[[Cells]]에서 remove한다.
      2. removedtrue로 설정한다.
  6. removed를 반환한다.

26.2.3.4 FinalizationRegistry.prototype [ %Symbol.toStringTag% ]

%Symbol.toStringTag% property의 initial value는 String value "FinalizationRegistry"입니다.

이 property는 attributes { [[Writable]]: false, [[Enumerable]]: false, [[Configurable]]: true }를 가집니다.

26.2.4 FinalizationRegistry 인스턴스의 속성

FinalizationRegistry instances는 FinalizationRegistry prototype object로부터 properties를 inherit하는 ordinary objects입니다. FinalizationRegistry instances는 또한 [[Cells]][[CleanupCallback]] internal slots를 가집니다.