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-builtins.h"
17 #include "ecma-exceptions.h"
18 #include "ecma-gc.h"
19 #include "ecma-globals.h"
20 #include "ecma-helpers.h"
21 #include "ecma-proxy-object.h"
22 #include "jrt.h"
23
24 #if ENABLED (JERRY_ES2015_BUILTIN_PROXY)
25
26 #define ECMA_BUILTINS_INTERNAL
27 #include "ecma-builtins-internal.h"
28
29 #define BUILTIN_INC_HEADER_NAME "ecma-builtin-proxy.inc.h"
30 #define BUILTIN_UNDERSCORED_ID proxy
31 #include "ecma-builtin-internal-routines-template.inc.h"
32
33 /** \addtogroup ecma ECMA
34 * @{
35 *
36 * \addtogroup ecmabuiltins
37 * @{
38 *
39 * \addtogroup proxy ECMA Proxy object built-in
40 * @{
41 */
42
43 /**
44 * The Proxy object's 'revocable' routine
45 *
46 * See also:
47 * ES2015 26.2.2.1
48 *
49 * @return ecma value
50 * Returned value must be freed with ecma_free_value.
51 */
52 static ecma_value_t
ecma_builtin_proxy_object_revocable(ecma_value_t this_arg,ecma_value_t target,ecma_value_t handler)53 ecma_builtin_proxy_object_revocable (ecma_value_t this_arg, /**< 'this' argument */
54 ecma_value_t target, /**< target argument */
55 ecma_value_t handler) /**< handler argument */
56 {
57 JERRY_UNUSED (this_arg);
58
59 ecma_object_t *rev_proxy_p = ecma_proxy_create_revocable (target, handler);
60
61 if (JERRY_UNLIKELY (rev_proxy_p == NULL))
62 {
63 return ECMA_VALUE_ERROR;
64 }
65
66 return ecma_make_object_value (rev_proxy_p);
67 } /* ecma_builtin_proxy_object_revocable */
68
69 /**
70 * Handle calling [[Call]] of built-in Proxy object
71 *
72 * See also:
73 * ES2015 26.2.2
74 *
75 * @return raised error
76 */
77 ecma_value_t
ecma_builtin_proxy_dispatch_call(const ecma_value_t * arguments_list_p,ecma_length_t arguments_list_len)78 ecma_builtin_proxy_dispatch_call (const ecma_value_t *arguments_list_p, /**< arguments list */
79 ecma_length_t arguments_list_len) /**< number of arguments */
80 {
81 JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
82
83 /* 1. */
84 return ecma_raise_type_error (ECMA_ERR_MSG ("Constructor Proxy requires 'new'"));
85 } /* ecma_builtin_proxy_dispatch_call */
86
87 /**
88 * Handle calling [[Construct]] of built-in proxy object
89 *
90 * See also:
91 * ES2015 26.2.2
92 *
93 * @return ECMA_VALUE_ERROR - if the operation fails
94 * new proxy object - otherwise
95 */
96 ecma_value_t
ecma_builtin_proxy_dispatch_construct(const ecma_value_t * arguments_list_p,ecma_length_t arguments_list_len)97 ecma_builtin_proxy_dispatch_construct (const ecma_value_t *arguments_list_p, /**< arguments list */
98 ecma_length_t arguments_list_len) /**< number of arguments */
99 {
100 JERRY_ASSERT (arguments_list_len == 0 || arguments_list_p != NULL);
101
102 /* 2. */
103 ecma_object_t *proxy_p = ecma_proxy_create (arguments_list_len > 0 ? arguments_list_p[0] : ECMA_VALUE_UNDEFINED,
104 arguments_list_len > 1 ? arguments_list_p[1] : ECMA_VALUE_UNDEFINED);
105
106 if (JERRY_UNLIKELY (proxy_p == NULL))
107 {
108 return ECMA_VALUE_ERROR;
109 }
110
111 return ecma_make_object_value (proxy_p);
112 } /* ecma_builtin_proxy_dispatch_construct */
113
114 /**
115 * @}
116 * @}
117 * @}
118 */
119
120 #endif /* ENABLED (JERRY_ES2015_BUILTIN_PROXY) */
121