Example #1
0
/**
 * Check if two literals are equal
 * Compare types of literals before performing detailed comparison.
 *
 * @return true if equal
 *         false otherwise
 */
bool
lit_literal_equal_type (lit_literal_t lit1, /**< first literal */
                        lit_literal_t lit2) /**< second literal */
{
  if (lit1->type != lit2->type)
  {
    return false;
  }

  return lit_literal_equal (lit1, lit2);
} /* lit_literal_equal_type */
Example #2
0
void
syntax_check_for_duplication_of_prop_names (bool is_strict, locus loc __attr_unused___)
{
  if (STACK_SIZE (props) - STACK_TOP (U8) < 2)
  {
    STACK_DROP (U8, 1);
    return;
  }

  for (uint8_t i = (uint8_t) (STACK_TOP (U8) + 1);
       i < STACK_SIZE (props);
       i++)
  {
    const prop_literal previous = STACK_ELEMENT (props, i);
    if (previous.type == VARG)
    {
      continue;
    }
    JERRY_ASSERT (previous.type == PROP_DATA
                  || previous.type == PROP_GET
                  || previous.type == PROP_SET);
    for (uint8_t j = STACK_TOP (U8); j < i; j = (uint8_t) (j + 1))
    {
      /*4*/
      const prop_literal current = STACK_ELEMENT (props, j);
      if (current.type == VARG)
      {
        continue;
      }
      JERRY_ASSERT (current.type == PROP_DATA
                    || current.type == PROP_GET
                    || current.type == PROP_SET);
      if (lit_literal_equal (previous.lit, current.lit))
      {
        /*a*/
        if (is_strict && previous.type == PROP_DATA && current.type == PROP_DATA)
        {
          PARSE_ERROR_VARG ("Duplication of parameter name '%s' in ObjectDeclaration is not allowed in strict mode",
                            loc, lit_literal_to_str_internal_buf (current.lit));
        }
        /*b*/
        if (previous.type == PROP_DATA
            && (current.type == PROP_SET || current.type == PROP_GET))
        {
          PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
                            loc, lit_literal_to_str_internal_buf (current.lit));
        }
        /*c*/
        if (current.type == PROP_DATA
            && (previous.type == PROP_SET || previous.type == PROP_GET))
        {
          PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be both data and accessor",
                            loc, lit_literal_to_str_internal_buf (current.lit));
        }
        /*d*/
        if ((previous.type == PROP_SET && current.type == PROP_SET)
            || (previous.type == PROP_GET && current.type == PROP_GET))
        {
          PARSE_ERROR_VARG ("Parameter name '%s' in ObjectDeclaration may not be accessor of same type",
                            loc, lit_literal_to_str_internal_buf (current.lit));
        }
      }
    }
  }

  STACK_DROP (props, (uint8_t) (STACK_SIZE (props) - STACK_TOP (U8)));
  STACK_DROP (U8, 1);
}
int
main (int __attr_unused___ argc,
      char __attr_unused___ **argv)
{
  const ecma_char_t *ptrs[test_sub_iters];
  ecma_number_t numbers[test_sub_iters];
  ecma_char_t strings[test_sub_iters][max_characters_in_string + 1];
  ecma_length_t lengths[test_sub_iters];

  mem_init ();
  lit_init ();

  srand ((unsigned int) time (NULL));
  int k = rand ();
  printf ("seed=%d\n", k);
  srand ((unsigned int) k);

  for (uint32_t i = 0; i < test_iters; i++)
  {
    memset (numbers, 0, sizeof (ecma_number_t) * test_sub_iters);
    memset (lengths, 0, sizeof (ecma_length_t) * test_sub_iters);
    memset (ptrs, 0, sizeof (ecma_char_t *) * test_sub_iters);

    for (uint32_t j = 0; j < test_sub_iters; j++)
    {
      int type = rand () % 3;
      if (type == 0)
      {
        lengths[j] = (ecma_length_t) (rand () % max_characters_in_string + 1);
        generate_string (strings[j], lengths[j]);
        lit_create_literal_from_charset (strings[j], lengths[j]);
        strings[j][lengths[j]] = '\0';
        ptrs[j] = strings[j];
        JERRY_ASSERT (ptrs[j]);
      }
      else if (type == 1)
      {
        ecma_magic_string_id_t msi = (ecma_magic_string_id_t) (rand () % ECMA_MAGIC_STRING__COUNT);
        ptrs[j] = ecma_get_magic_string_zt (msi);
        JERRY_ASSERT (ptrs[j]);
        lengths[j] = (ecma_length_t) ecma_zt_string_length (ptrs[j]);
        lit_create_literal_from_charset (ptrs[j], lengths[j]);
      }
      else
      {
        ecma_number_t num = generate_number ();
        lengths[j] = ecma_number_to_zt_string (num, strings[j], max_characters_in_string);
        lit_create_literal_from_num (num);
      }
    }

    // Add empty string
    lit_create_literal_from_charset (NULL, 0);

    for (uint32_t j = 0; j < test_sub_iters; j++)
    {
      literal_t lit1;
      literal_t lit2;
      if (ptrs[j])
      {
        lit1 = lit_find_or_create_literal_from_charset (ptrs[j], lengths[j]);
        lit2 = lit_find_literal_by_charset (ptrs[j], lengths[j]);
        JERRY_ASSERT (lit_literal_equal_zt (lit1, ptrs[j]));
        JERRY_ASSERT (lit_literal_equal_type_zt (lit2, ptrs[j]));
      }
      else
      {
        lit1 = lit_find_or_create_literal_from_num (numbers[j]);
        lit2 = lit_find_literal_by_num (numbers[j]);
        JERRY_ASSERT (lit_literal_equal_num (lit1, numbers[j]));
        JERRY_ASSERT (lit_literal_equal_type_num (lit2, numbers[j]));
      }
      JERRY_ASSERT (lit1);
      JERRY_ASSERT (lit2);
      JERRY_ASSERT (lit1 == lit2);
      JERRY_ASSERT (lit_literal_equal (lit1, lit2));
    }

    // Check empty string exists
    JERRY_ASSERT (lit_find_literal_by_charset (NULL, 0));

    lit_storage.cleanup ();
    JERRY_ASSERT (lit_storage.get_first () == NULL);
  }

  lit_finalize ();
  mem_finalize (true);
  return 0;
}