aboutsummaryrefslogtreecommitdiff
path: root/jerry-core/ecma/builtin-objects/ecma-builtin-object-prototype.c
blob: e57af2ec3572e77568ff137c66020e133c25e4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
/* Copyright JS Foundation and other contributors, http://js.foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "ecma-alloc.h"
#include "ecma-builtin-helpers.h"
#include "ecma-builtin-object.h"
#include "ecma-builtins.h"
#include "ecma-conversion.h"
#include "ecma-exceptions.h"
#include "ecma-function-object.h"
#include "ecma-gc.h"
#include "ecma-globals.h"
#include "ecma-helpers.h"
#include "ecma-objects.h"
#include "ecma-proxy-object.h"
#include "ecma-string-object.h"

#include "jrt.h"

#define ECMA_BUILTINS_INTERNAL
#include "ecma-builtins-internal.h"

/**
 * This object has a custom dispatch function.
 */
#define BUILTIN_CUSTOM_DISPATCH

/**
 * List of built-in routine identifiers.
 */
enum
{
  /* Note: these 6 routines must be in this order */
  ECMA_OBJECT_PROTOTYPE_ROUTINE_START = 0,
  ECMA_OBJECT_PROTOTYPE_TO_STRING,
  ECMA_OBJECT_PROTOTYPE_VALUE_OF,
  ECMA_OBJECT_PROTOTYPE_TO_LOCALE_STRING,
  ECMA_OBJECT_PROTOTYPE_GET_PROTO,
  ECMA_OBJECT_PROTOTYPE_IS_PROTOTYPE_OF,
  ECMA_OBJECT_PROTOTYPE_HAS_OWN_PROPERTY,
  ECMA_OBJECT_PROTOTYPE_PROPERTY_IS_ENUMERABLE,
  ECMA_OBJECT_PROTOTYPE_SET_PROTO,
#if JERRY_ESNEXT && JERRY_BUILTIN_ANNEXB
  ECMA_OBJECT_PROTOTYPE_DEFINE_GETTER,
  ECMA_OBJECT_PROTOTYPE_DEFINE_SETTER,
  ECMA_OBJECT_PROTOTYPE_LOOKUP_GETTER,
  ECMA_OBJECT_PROTOTYPE_LOOKUP_SETTER,
#endif /* JERRY_ESNEXT && JERRY_BUILTIN_ANNEXB */
};

#define BUILTIN_INC_HEADER_NAME "ecma-builtin-object-prototype.inc.h"
#define BUILTIN_UNDERSCORED_ID  object_prototype
#include "ecma-builtin-internal-routines-template.inc.h"

/** \addtogroup ecma ECMA
 * @{
 *
 * \addtogroup ecmabuiltins
 * @{
 *
 * \addtogroup objectprototype ECMA Object.prototype object built-in
 * @{
 */

/**
 * The Object.prototype object's 'toString' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.2
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_to_string (ecma_value_t this_arg) /**< this argument */
{
  return ecma_builtin_helper_object_to_string (this_arg);
} /* ecma_builtin_object_prototype_object_to_string */

/**
 * The Object.prototype object's 'valueOf' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.4
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_value_of (ecma_value_t this_arg) /**< this argument */
{
  return ecma_op_to_object (this_arg);
} /* ecma_builtin_object_prototype_object_value_of */

/**
 * The Object.prototype object's 'toLocaleString' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.3
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_to_locale_string (ecma_value_t this_arg) /**< this argument */
{
  return ecma_op_invoke_by_magic_id (this_arg, LIT_MAGIC_STRING_TO_STRING_UL, &this_arg, 1);
} /* ecma_builtin_object_prototype_object_to_locale_string */

/**
 * The Object.prototype object's 'hasOwnProperty' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.5
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_has_own_property (ecma_object_t *obj_p, /**< this argument */
                                                       ecma_string_t *prop_name_p) /**< first argument */
{
  return ecma_op_object_has_own_property (obj_p, prop_name_p);
} /* ecma_builtin_object_prototype_object_has_own_property */

/**
 * The Object.prototype object's 'isPrototypeOf' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.6
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_is_prototype_of (ecma_object_t *obj_p, /**< this argument */
                                                      ecma_value_t arg) /**< routine's first argument */
{
  /* 3. Compare prototype to object */
  ecma_value_t v_obj_value = ecma_op_to_object (arg);

  if (ECMA_IS_VALUE_ERROR (v_obj_value))
  {
    return v_obj_value;
  }

  ecma_object_t *v_obj_p = ecma_get_object_from_value (v_obj_value);

  ecma_value_t ret_value = ecma_op_object_is_prototype_of (obj_p, v_obj_p);

  ecma_deref_object (v_obj_p);

  return ret_value;
} /* ecma_builtin_object_prototype_object_is_prototype_of */

/**
 * The Object.prototype object's 'propertyIsEnumerable' routine
 *
 * See also:
 *          ECMA-262 v5, 15.2.4.7
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
static ecma_value_t
ecma_builtin_object_prototype_object_property_is_enumerable (ecma_object_t *obj_p, /**< this argument */
                                                             ecma_string_t *prop_name_p) /**< first argument */
{
  ecma_property_descriptor_t prop_desc;
  ecma_value_t status = ecma_op_object_get_own_property_descriptor (obj_p, prop_name_p, &prop_desc);

  if (!ecma_is_value_true (status))
  {
    return status;
  }

  bool is_enumerable = (prop_desc.flags & JERRY_PROP_IS_ENUMERABLE);

  ecma_free_property_descriptor (&prop_desc);

  return ecma_make_boolean_value (is_enumerable);
} /* ecma_builtin_object_prototype_object_property_is_enumerable */

#if JERRY_ESNEXT && JERRY_BUILTIN_ANNEXB
/**
 * The Object.prototype object's '__defineGetter__' and '__defineSetter__' routine
 *
 * See also:
 *          ECMA-262 v11, B.2.2.2
 *          ECMA-262 v11, B.2.2.3
 *
 * @return ECMA_VALUE_ERROR - if the operation fails,
 *         ECMA_VALUE_UNDEFINED - otherwise
 */
static ecma_value_t
ecma_builtin_object_prototype_define_getter_setter (ecma_value_t this_arg, /**< this argument */
                                                    ecma_value_t prop, /**< property */
                                                    ecma_value_t accessor, /**< getter/setter function */
                                                    bool define_getter) /**< true - defineGetter method
                                                                             false - defineSetter method */
{
  /* 1. */
  ecma_value_t to_obj = ecma_op_to_object (this_arg);

  if (ECMA_IS_VALUE_ERROR (to_obj))
  {
    return to_obj;
  }

  ecma_object_t *obj_p = ecma_get_object_from_value (to_obj);

  /* 2. */
  if (!ecma_op_is_callable (accessor))
  {
    ecma_deref_object (obj_p);
    return ecma_raise_type_error (ECMA_ERR_GETTER_IS_NOT_CALLABLE);
  }

  ecma_object_t *accessor_obj_p = ecma_get_object_from_value (accessor);

  /* 3. */
  ecma_property_descriptor_t desc = ecma_make_empty_property_descriptor ();
  desc.flags |= (JERRY_PROP_IS_ENUMERABLE | JERRY_PROP_IS_CONFIGURABLE | JERRY_PROP_IS_ENUMERABLE_DEFINED
                 | JERRY_PROP_IS_CONFIGURABLE_DEFINED | JERRY_PROP_SHOULD_THROW);

  if (define_getter)
  {
    desc.get_p = accessor_obj_p;
    desc.flags |= JERRY_PROP_IS_GET_DEFINED;
  }
  else
  {
    desc.set_p = accessor_obj_p;
    desc.flags |= JERRY_PROP_IS_SET_DEFINED;
  }

  /* 4. */
  ecma_string_t *prop_name_p = ecma_op_to_property_key (prop);

  if (JERRY_UNLIKELY (prop_name_p == NULL))
  {
    ecma_deref_object (obj_p);
    return ECMA_VALUE_ERROR;
  }

  /* 5. */
  ecma_value_t define_prop = ecma_op_object_define_own_property (obj_p, prop_name_p, &desc);

  ecma_deref_object (obj_p);
  ecma_deref_ecma_string (prop_name_p);

  if (ECMA_IS_VALUE_ERROR (define_prop))
  {
    return define_prop;
  }

  /* 6. */
  return ECMA_VALUE_UNDEFINED;
} /* ecma_builtin_object_prototype_define_getter_setter */

/**
 * The Object.prototype object's '__lookupGetter__' and '__lookupSetter__' routine
 *
 * See also:
 *          ECMA-262 v11, B.2.2.4
 *          ECMA-262 v11, B.2.2.5
 *
 * @return ECMA_VALUE_ERROR - if the operation fails,
 *         ECMA_VALUE_UNDEFINED - if the property was not found
 *         Accessor property - otherwise
 */
static ecma_value_t
ecma_builtin_object_prototype_lookup_getter_setter (ecma_value_t this_arg, /**< this argument */
                                                    ecma_value_t prop, /**< property */
                                                    bool lookup_getter) /**< true - lookupGetter method
                                                                             false - lookupSetter method */
{
  /* 1. */
  ecma_value_t to_obj = ecma_op_to_object (this_arg);

  if (ECMA_IS_VALUE_ERROR (to_obj))
  {
    return to_obj;
  }

  ecma_object_t *obj_p = ecma_get_object_from_value (to_obj);

  /* 2. */
  ecma_string_t *prop_name_p = ecma_op_to_property_key (prop);

  if (JERRY_UNLIKELY (prop_name_p == NULL))
  {
    ecma_deref_object (obj_p);
    return ECMA_VALUE_ERROR;
  }

  ecma_value_t ret_value = ECMA_VALUE_UNDEFINED;

  ecma_ref_object (obj_p);

  /* 3. */
  while (true)
  {
    /* 3.a */
    ecma_property_descriptor_t desc;
    ecma_value_t get_desc = ecma_op_object_get_own_property_descriptor (obj_p, prop_name_p, &desc);

    if (ECMA_IS_VALUE_ERROR (get_desc))
    {
      ret_value = get_desc;
      ecma_deref_object (obj_p);
      break;
    }

    /* 3.b */
    if (ecma_is_value_true (get_desc))
    {
      if ((desc.flags & JERRY_PROP_IS_SET_DEFINED) || (desc.flags & JERRY_PROP_IS_GET_DEFINED))
      {
        if (lookup_getter && desc.get_p != NULL)
        {
          ecma_ref_object (desc.get_p);
          ret_value = ecma_make_object_value (desc.get_p);
        }
        else if (!lookup_getter && desc.set_p != NULL)
        {
          ecma_ref_object (desc.set_p);
          ret_value = ecma_make_object_value (desc.set_p);
        }
      }

      ecma_free_property_descriptor (&desc);
      ecma_deref_object (obj_p);
      break;
    }

    /* 3.c */
    ecma_object_t *proto_p = ecma_op_object_get_prototype_of (obj_p);
    ecma_deref_object (obj_p);

    if (proto_p == NULL)
    {
      break;
    }
    else if (JERRY_UNLIKELY (proto_p == ECMA_OBJECT_POINTER_ERROR))
    {
      ret_value = ECMA_VALUE_ERROR;
      break;
    }

    /* Advance up on prototype chain. */
    obj_p = proto_p;
  }

  ecma_free_value (to_obj);
  ecma_deref_ecma_string (prop_name_p);

  return ret_value;
} /* ecma_builtin_object_prototype_lookup_getter_setter */
#endif /* JERRY_ESNEXT && JERRY_BUILTIN_ANNEXB */

/**
 * Dispatcher of the built-in's routines
 *
 * @return ecma value
 *         Returned value must be freed with ecma_free_value.
 */
ecma_value_t
ecma_builtin_object_prototype_dispatch_routine (uint8_t builtin_routine_id, /**< built-in wide routine
                                                                             *   identifier */
                                                ecma_value_t this_arg, /**< 'this' argument value */
                                                const ecma_value_t arguments_list_p[], /**< list of arguments
                                                                                        *   passed to routine */
                                                uint32_t arguments_number) /**< length of arguments' list */
{
  JERRY_UNUSED (arguments_number);

  /* no specialization */
  if (builtin_routine_id <= ECMA_OBJECT_PROTOTYPE_VALUE_OF)
  {
    if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_TO_STRING)
    {
      return ecma_builtin_object_prototype_object_to_string (this_arg);
    }

    JERRY_ASSERT (builtin_routine_id <= ECMA_OBJECT_PROTOTYPE_VALUE_OF);

    return ecma_builtin_object_prototype_object_value_of (this_arg);
  }

  if (builtin_routine_id <= ECMA_OBJECT_PROTOTYPE_IS_PROTOTYPE_OF)
  {
    if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_IS_PROTOTYPE_OF)
    {
      /* 15.2.4.6.1. */
      if (!ecma_is_value_object (arguments_list_p[0]))
      {
        return ECMA_VALUE_FALSE;
      }
    }

    if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_TO_LOCALE_STRING)
    {
      return ecma_builtin_object_prototype_object_to_locale_string (this_arg);
    }

    ecma_value_t to_object = ecma_op_to_object (this_arg);

    if (ECMA_IS_VALUE_ERROR (to_object))
    {
      return to_object;
    }

    ecma_object_t *obj_p = ecma_get_object_from_value (to_object);

    ecma_value_t ret_value;

#if JERRY_ESNEXT
    if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_GET_PROTO)
    {
      ret_value = ecma_builtin_object_object_get_prototype_of (obj_p);
    }
    else
#endif /* JERRY_ESNEXT */
    {
      ret_value = ecma_builtin_object_prototype_object_is_prototype_of (obj_p, arguments_list_p[0]);
    }

    ecma_deref_object (obj_p);

    return ret_value;
  }

  JERRY_ASSERT (builtin_routine_id >= ECMA_OBJECT_PROTOTYPE_HAS_OWN_PROPERTY);

#if JERRY_ESNEXT
  if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_SET_PROTO)
  {
    return ecma_builtin_object_object_set_proto (this_arg, arguments_list_p[0]);
  }
#if JERRY_BUILTIN_ANNEXB
  else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_LOOKUP_GETTER)
  {
    return ecma_builtin_object_prototype_lookup_getter_setter (this_arg, arguments_list_p[0], true);
  }
  else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_LOOKUP_SETTER)
  {
    return ecma_builtin_object_prototype_lookup_getter_setter (this_arg, arguments_list_p[0], false);
  }
  else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_DEFINE_GETTER)
  {
    return ecma_builtin_object_prototype_define_getter_setter (this_arg,
                                                               arguments_list_p[0],
                                                               arguments_list_p[1],
                                                               true);
  }
  else if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_DEFINE_SETTER)
  {
    return ecma_builtin_object_prototype_define_getter_setter (this_arg,
                                                               arguments_list_p[0],
                                                               arguments_list_p[1],
                                                               false);
  }
#endif /* JERRY_BUILTIN_ANNEXB */
#endif /* JERRY_ESNEXT*/

  ecma_string_t *prop_name_p = ecma_op_to_property_key (arguments_list_p[0]);

  if (prop_name_p == NULL)
  {
    return ECMA_VALUE_ERROR;
  }

  ecma_value_t to_object = ecma_op_to_object (this_arg);

  if (ECMA_IS_VALUE_ERROR (to_object))
  {
    ecma_deref_ecma_string (prop_name_p);
    return to_object;
  }

  ecma_object_t *obj_p = ecma_get_object_from_value (to_object);

  ecma_value_t ret_value;

  if (builtin_routine_id == ECMA_OBJECT_PROTOTYPE_HAS_OWN_PROPERTY)
  {
    ret_value = ecma_builtin_object_prototype_object_has_own_property (obj_p, prop_name_p);
  }
  else
  {
    ret_value = ecma_builtin_object_prototype_object_property_is_enumerable (obj_p, prop_name_p);
  }

  ecma_deref_ecma_string (prop_name_p);
  ecma_deref_object (obj_p);

  return ret_value;
} /* ecma_builtin_object_prototype_dispatch_routine */

/**
 * @}
 * @}
 * @}
 */