Repository: jerryscript-project/jerryscript
File: tests/unit-core/test-symbol.c (C)

Code before:

    jerry_release_value (prop_result_global);
    jerry_release_value (prop_result_wn);
    jerry_release_value (current_global_symbol);
    jerry_release_value (well_known_symbol);
  }

  jerry_release_value (builtin_symbol);

  const jerry_char_t deleter_src[] = "Symbol = {}";

  jerry_value_t deleter = jerry_eval (deleter_src, sizeof (deleter_src) - 1, JERRY_PARSE_NO_OPTS);
  TEST_ASSERT (!jerry_value_is_error (deleter));
  jerry_release_value (deleter);

  builtin_symbol = jerry_get_property (global_obj, symbol_str);
  TEST_ASSERT (jerry_value_is_object (builtin_symbol));

  expected = 1.0;
  prop_index = 0;

  for (jerry_well_known_symbol_t id = JERRY_SYMBOL_HAS_INSTANCE;
       id < JERRY_SYMBOL__NUMBER;
       id++, expected++, prop_index++)
  {
    jerry_value_t well_known_symbol = jerry_get_well_known_symbol (id);
    jerry_value_t prop_str = jerry_create_string ((const jerry_char_t *) symbols[prop_index]);
    jerry_value_t current_global_symbol = jerry_get_property (builtin_symbol, prop_str);
    jerry_release_value (prop_str);

    TEST_ASSERT (jerry_value_is_undefined (current_global_symbol));

    jerry_value_t prop_result_wn = jerry_get_property (obj, well_known_symbol);

    TEST_ASSERT (jerry_value_is_number (prop_result_wn));
    double number_wn = jerry_get_number_value (prop_result_wn);
    TEST_ASSERT (number_wn == expected);

    jerry_release_value (prop_result_wn);
    jerry_release_value (current_global_symbol);
    jerry_release_value (well_known_symbol);
  }
  jerry_release_value (builtin_symbol);

  jerry_value_t invalid_global_symbol = jerry_get_well_known_symbol (JERRY_SYMBOL__NUMBER);
  TEST_ASSERT (jerry_value_is_undefined (invalid_global_symbol));
  jerry_release_value (invalid_global_symbol);

  jerry_release_value (symbol_str);
  jerry_release_value (global_obj);
  jerry_release_value (obj);

Review comment (suggestion):
This test deserves a comment on why it is here (after the "deletion" of the well-known Symbols) otherwise it almost looks the same as the one `for` loop above.

Code after:

    jerry_release_value (prop_result_global);
    jerry_release_value (prop_result_wn);
    jerry_release_value (current_global_symbol);
    jerry_release_value (well_known_symbol);
  }

  jerry_release_value (builtin_symbol);

  /* Deletion of the 'Symbol' builtin makes the well-known symbols unaccessible from JS context
     but the symbols still can be obtained via 'jerry_get_well_known_symbol' */
  const jerry_char_t deleter_src[] = "delete Symbol";

  jerry_value_t deleter = jerry_eval (deleter_src, sizeof (deleter_src) - 1, JERRY_PARSE_NO_OPTS);
  TEST_ASSERT (jerry_value_is_boolean (deleter)
               && jerry_get_boolean_value (deleter));
  jerry_release_value (deleter);

  builtin_symbol = jerry_get_property (global_obj, symbol_str);
  TEST_ASSERT (jerry_value_is_undefined (builtin_symbol));
  jerry_release_value (builtin_symbol);

  expected = 1.0;
  prop_index = 0;

  for (jerry_well_known_symbol_t id = JERRY_SYMBOL_HAS_INSTANCE;
       id <= JERRY_SYMBOL_UNSCOPABLES;
       id++, expected++, prop_index++)
  {
    jerry_value_t well_known_symbol = jerry_get_well_known_symbol (id);
    jerry_value_t prop_result_wn = jerry_get_property (obj, well_known_symbol);

    TEST_ASSERT (jerry_value_is_number (prop_result_wn));
    double number_wn = jerry_get_number_value (prop_result_wn);
    TEST_ASSERT (number_wn == expected);

    jerry_release_value (prop_result_wn);
    jerry_release_value (well_known_symbol);
  }

  jerry_well_known_symbol_t invalid_symbol = (jerry_well_known_symbol_t) (JERRY_SYMBOL_UNSCOPABLES + 1);
  jerry_value_t invalid_well_known_symbol = jerry_get_well_known_symbol (invalid_symbol);
  TEST_ASSERT (jerry_value_is_undefined (invalid_well_known_symbol));
  jerry_release_value (invalid_well_known_symbol);

  invalid_symbol = (jerry_well_known_symbol_t) (JERRY_SYMBOL_HAS_INSTANCE - 1);
  invalid_well_known_symbol = jerry_get_well_known_symbol (invalid_symbol);
  TEST_ASSERT (jerry_value_is_undefined (invalid_well_known_symbol));
  jerry_release_value (invalid_well_known_symbol);

  jerry_release_value (symbol_str);