7.2. Inject a key into a value

The key paths used in this section are always strings and never sequences, since it is not possible to create a object store which has a key generator and also has a key path that is a sequence.

The steps to check that a key could be injected into a value are as follows. The algorithm takes a value and a keyPath, and outputs true or false.

  1. Let identifiers be the result of strictly splitting keyPath on U+002E FULL STOP characters (.).

  2. Assert: identifiers is not empty.

  3. Remove the last member of identifiers.

  4. For each remaining identifier in identifiers, if any:

    1. If value is not an Object or an Array, return false.

    2. Let hop be ! HasOwnProperty(value, identifier).

    3. If hop is false, return true.

    4. Let value be ! Get(value, identifier).

  5. Return true if value is an Object or an Array, or false otherwise.

Assertions can be made in the above steps because this algorithm is only applied to values that are the output of StructuredDeserialize.

The steps to inject a key into a value using a key path are as follows. The algorithm takes a value, a key and a keyPath.

  1. Let identifiers be the result of strictly splitting keyPath on U+002E FULL STOP characters (.).

  2. Assert: identifiers is not empty.

  3. Let last be the last member of identifiers and remove it from the list.

  4. For each remaining identifier in identifiers:

    1. Assert: value is an Object or an Array.

    2. Let hop be ! HasOwnProperty(value, identifier).

    3. If hop is false, then:

      1. Let o be a new Object created as if by the expression ({}).

      2. Let status be CreateDataProperty(value, identifier, o).

      3. Assert: status is true.

    4. Let value be ! Get(value, identifier).

  5. Assert: value is an Object or an Array.

  6. Let keyValue be the result of running the steps to convert a key to a value with key.

  7. Let status be CreateDataProperty(value, last, keyValue).

  8. Assert: status is true.

Assertions can be made in the above steps because this algorithm is only applied to values that are the output of StructuredDeserialize, and the steps to check that a key could be injected into a value have been run.