1/* Copyright JS Foundation and other contributors, http://js.foundation
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include "ecma-alloc.h"
17#include "ecma-builtins.h"
18#include "ecma-exceptions.h"
19#include "ecma-gc.h"
20#include "ecma-globals.h"
21#include "ecma-helpers.h"
22#include "ecma-objects.h"
23#include "ecma-objects-general.h"
24#include "ecma-symbol-object.h"
25#include "lit-char-helpers.h"
26
27#if ENABLED (JERRY_ES2015)
28
29/** \addtogroup ecma ECMA
30 * @{
31 *
32 * \addtogroup ecmasymbolobject ECMA Symbol object related routines
33 * @{
34 */
35
36/**
37 * Symbol creation operation.
38 *
39 * See also: ECMA-262 v6, 6.1.5.1
40 *
41 * @return ecma value
42 *         Returned value must be freed with ecma_free_value
43 */
44ecma_value_t
45ecma_op_create_symbol (const ecma_value_t *arguments_list_p, /**< list of arguments */
46                       ecma_length_t arguments_list_len) /**< length of the arguments' list */
47{
48  JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
49
50  ecma_value_t string_desc;
51
52  /* 1-3. */
53  if (arguments_list_len == 0)
54  {
55    string_desc = ecma_make_magic_string_value (LIT_MAGIC_STRING__EMPTY);
56  }
57  else
58  {
59    ecma_string_t *str_p = ecma_op_to_string (arguments_list_p[0]);
60
61    /* 4. */
62    if (JERRY_UNLIKELY (str_p == NULL))
63    {
64      return ECMA_VALUE_ERROR;
65    }
66
67    string_desc = ecma_make_string_value (str_p);
68  }
69
70  /* 5. */
71  return ecma_make_symbol_value (ecma_new_symbol_from_descriptor_string (string_desc));
72} /* ecma_op_create_symbol */
73
74/**
75 * Symbol object creation operation.
76 *
77 * See also: ECMA-262 v6, 19.4.1
78 *
79 * @return ecma value
80 *         Returned value must be freed with ecma_free_value
81 */
82ecma_value_t
83ecma_op_create_symbol_object (const ecma_value_t value) /**< symbol value */
84{
85  JERRY_ASSERT (ecma_is_value_symbol (value));
86
87  ecma_object_t *prototype_obj_p = ecma_builtin_get (ECMA_BUILTIN_ID_SYMBOL_PROTOTYPE);
88  ecma_object_t *object_p = ecma_create_object (prototype_obj_p,
89                                                sizeof (ecma_extended_object_t),
90                                                ECMA_OBJECT_TYPE_CLASS);
91
92  ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
93  ext_object_p->u.class_prop.class_id = LIT_MAGIC_STRING_SYMBOL_UL;
94  ext_object_p->u.class_prop.u.value = ecma_copy_value (value);
95
96  return ecma_make_object_value (object_p);
97} /* ecma_op_create_symbol_object */
98
99/**
100 * Get the symbol descriptor ecma-string from an ecma-symbol
101 *
102 * @return pointer to ecma-string descriptor
103 */
104ecma_string_t *
105ecma_get_symbol_description (ecma_string_t *symbol_p) /**< ecma-symbol */
106{
107  JERRY_ASSERT (symbol_p != NULL);
108  JERRY_ASSERT (ecma_prop_name_is_symbol (symbol_p));
109
110  return ecma_get_string_from_value (((ecma_extended_string_t *) symbol_p)->u.symbol_descriptor);
111} /* ecma_get_symbol_description */
112
113/**
114 * Get the descriptive string of the Symbol.
115 *
116 * See also: ECMA-262 v6, 19.4.3.2.1
117 *
118 * @return ecma value
119 *         Returned value must be freed with ecma_free_value.
120 */
121ecma_value_t
122ecma_get_symbol_descriptive_string (ecma_value_t symbol_value) /**< symbol to stringify */
123{
124  /* 1. */
125  JERRY_ASSERT (ecma_is_value_symbol (symbol_value));
126
127  /* 2 - 3. */
128  ecma_string_t *symbol_p = ecma_get_symbol_from_value (symbol_value);
129  ecma_string_t *string_desc_p = ecma_get_symbol_description (symbol_p);
130
131  /* 5. */
132  ecma_stringbuilder_t builder = ecma_stringbuilder_create_raw ((lit_utf8_byte_t *) ("Symbol("), 7);
133  ecma_stringbuilder_append (&builder, string_desc_p);
134  ecma_stringbuilder_append_byte (&builder, LIT_CHAR_RIGHT_PAREN);
135
136  return ecma_make_string_value (ecma_stringbuilder_finalize (&builder));
137} /* ecma_get_symbol_descriptive_string */
138
139/**
140 * Helper for Symbol.prototype.{toString, valueOf} routines
141 *
142 * See also: 19.4.3.2, 19.4.3.3
143 *
144 * @return ecma value
145 *         Returned value must be freed with ecma_free_value.
146 */
147ecma_value_t
148ecma_symbol_to_string_helper (ecma_value_t this_arg, /**< this argument value */
149                              bool is_to_string) /**< true - perform the 'toString' routine steps
150                                                  *   false - perform the 'valueOf' routine steps */
151{
152  if (ecma_is_value_symbol (this_arg))
153  {
154    return is_to_string ? ecma_get_symbol_descriptive_string (this_arg) : ecma_copy_value (this_arg);
155  }
156
157  if (ecma_is_value_object (this_arg))
158  {
159    ecma_object_t *object_p = ecma_get_object_from_value (this_arg);
160
161    if (ecma_get_object_type (object_p) == ECMA_OBJECT_TYPE_CLASS)
162    {
163      ecma_extended_object_t *ext_object_p = (ecma_extended_object_t *) object_p;
164
165      if (ext_object_p->u.class_prop.class_id == LIT_MAGIC_STRING_SYMBOL_UL)
166      {
167        return (is_to_string ? ecma_get_symbol_descriptive_string (ext_object_p->u.class_prop.u.value)
168                             : ecma_copy_value (ext_object_p->u.class_prop.u.value));
169      }
170    }
171  }
172
173  return ecma_raise_type_error (ECMA_ERR_MSG ("Argument 'this' is must be a Symbol."));
174} /* ecma_symbol_to_string_helper */
175
176#endif /* ENABLED (JERRY_ES2015) */
177
178/**
179 * @}
180 * @}
181 */
182