"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.ClassPropertiesToIgnore = undefined;
exports.getSuggestedArrayLiteralLength = getSuggestedArrayLiteralLength;
exports.commonAncestorOf = commonAncestorOf;
exports.getOrDefault = getOrDefault;
exports.withDescriptorValue = withDescriptorValue;
exports.canIgnoreClassLengthProperty = canIgnoreClassLengthProperty;
exports.getObjectPrototypeMetadata = getObjectPrototypeMetadata;

var _index = require("../values/index.js");

var _errors = require("../errors.js");

var _invariant = require("../invariant.js");

var _invariant2 = _interopRequireDefault(_invariant);

var _index2 = require("../methods/index.js");

var _logger = require("../utils/logger.js");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

/**
 * Get index property list length by searching array properties list for the max index key value plus 1.
 */
function getSuggestedArrayLiteralLength(realm, val) {
  (0, _invariant2.default)((0, _index2.IsArray)(realm, val));

  let length = 0;
  for (const key of val.properties.keys()) {
    if ((0, _index2.IsArrayIndex)(realm, key) && Number(key) >= length) {
      length = Number(key) + 1;
    }
  }
  return length;
} /**
   * Copyright (c) 2017-present, Facebook, Inc.
   * All rights reserved.
   *
   * This source code is licensed under the BSD-style license found in the
   * LICENSE file in the root directory of this source tree. An additional grant
   * of patent rights can be found in the PATENTS file in the same directory.
   */

function commonAncestorOf(node1, node2, getParent) {
  if (node1 === node2) return node1;
  // First get the path length to the root node for both nodes while also checking if
  // either node is the parent of the other.
  let n1 = node1,
      n2 = node2,
      count1 = 0,
      count2 = 0;
  while (true) {
    let p1 = n1 && getParent(n1);
    let p2 = n2 && getParent(n2);
    if (p1 === node2) return node2;
    if (p2 === node1) return node1;
    if (p1) count1++;
    if (p2) count2++;
    if (!p1 && !p2) break;
    n1 = p1;
    n2 = p2;
  }
  // Now shorten the longest path to the same length as the shorter path
  n1 = node1;
  while (count1 > count2) {
    (0, _invariant2.default)(n1 !== undefined);
    n1 = getParent(n1);
    count1--;
  }
  n2 = node2;
  while (count1 < count2) {
    (0, _invariant2.default)(n2 !== undefined);
    n2 = getParent(n2);
    count2--;
  }
  // Now run up both paths in tandem, stopping at the first common entry
  while (n1 !== n2) {
    (0, _invariant2.default)(n1 !== undefined);
    n1 = getParent(n1);
    (0, _invariant2.default)(n2 !== undefined);
    n2 = getParent(n2);
  }
  return n1;
}

// Gets map[key] with default value provided by defaultFn
function getOrDefault(map, key, defaultFn) {
  let value = map.get(key);
  if (value === undefined) map.set(key, value = defaultFn());
  (0, _invariant2.default)(value !== undefined);
  return value;
}

function withDescriptorValue(propertyNameOrSymbol, descriptor, func) {
  if (descriptor !== undefined) {
    if (descriptor.value !== undefined) {
      func(propertyNameOrSymbol, descriptor.value, "value");
    } else {
      if (descriptor.get !== undefined) {
        func(propertyNameOrSymbol, descriptor.get, "get");
      }
      if (descriptor.set !== undefined) {
        func(propertyNameOrSymbol, descriptor.set, "set");
      }
    }
  }
}

const ClassPropertiesToIgnore = exports.ClassPropertiesToIgnore = new Set(["arguments", "name", "caller"]);

function canIgnoreClassLengthProperty(val, desc, logger) {
  if (desc && desc.value === undefined) {
    logger.logError(val, "Functions with length accessor properties are not supported in residual heap.");
  }
  return true;
}

function getObjectPrototypeMetadata(realm, obj) {
  let proto = obj.$Prototype;
  let skipPrototype = false;
  let constructor;

  if (obj.$IsClassPrototype) {
    skipPrototype = true;
  }
  if (proto && proto.$IsClassPrototype) {
    (0, _invariant2.default)(proto instanceof _index.ObjectValue);
    // we now need to check if the prototpe has a constructor
    if (proto.properties.has("constructor")) {
      let _constructor = proto.properties.get("constructor");
      (0, _invariant2.default)(_constructor !== undefined);
      // if the contructor has been deleted then we have no way
      // to serialize the original class AST as it won't have been
      // evluated and thus visited
      if (_constructor.descriptor === undefined) {
        throw new _errors.FatalError("TODO #1024: implement object prototype serialization with deleted constructor");
      }
      let classFunc = (0, _index2.Get)(realm, proto, "constructor");
      constructor = classFunc;
      (0, _invariant2.default)(constructor instanceof _index.ECMAScriptSourceFunctionValue);
      skipPrototype = true;
    }
  }

  return {
    skipPrototype,
    constructor
  };
}
//# sourceMappingURL=utils.js.map