1 // Copyright 2014 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "src/runtime/runtime-utils.h"
6 
7 #include "src/arguments.h"
8 #include "src/elements.h"
9 #include "src/heap/factory.h"
10 #include "src/isolate-inl.h"
11 #include "src/objects-inl.h"
12 
13 namespace v8 {
14 namespace internal {
15 
16 
RUNTIME_FUNCTION(Runtime_IsJSProxy)17 RUNTIME_FUNCTION(Runtime_IsJSProxy) {
18   SealHandleScope shs(isolate);
19   DCHECK_EQ(1, args.length());
20   CONVERT_ARG_CHECKED(Object, obj, 0);
21   return isolate->heap()->ToBoolean(obj->IsJSProxy());
22 }
23 
24 
RUNTIME_FUNCTION(Runtime_JSProxyGetHandler)25 RUNTIME_FUNCTION(Runtime_JSProxyGetHandler) {
26   SealHandleScope shs(isolate);
27   DCHECK_EQ(1, args.length());
28   CONVERT_ARG_CHECKED(JSProxy, proxy, 0);
29   return proxy->handler();
30 }
31 
32 
RUNTIME_FUNCTION(Runtime_JSProxyGetTarget)33 RUNTIME_FUNCTION(Runtime_JSProxyGetTarget) {
34   SealHandleScope shs(isolate);
35   DCHECK_EQ(1, args.length());
36   CONVERT_ARG_CHECKED(JSProxy, proxy, 0);
37   return proxy->target();
38 }
39 
40 
RUNTIME_FUNCTION(Runtime_GetPropertyWithReceiver)41 RUNTIME_FUNCTION(Runtime_GetPropertyWithReceiver) {
42   HandleScope scope(isolate);
43 
44   DCHECK_EQ(3, args.length());
45   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, holder, 0);
46   CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
47   CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 2);
48 
49   bool success = false;
50   LookupIterator it = LookupIterator::PropertyOrElement(isolate, receiver, key,
51                                                         &success, holder);
52   if (!success) {
53     DCHECK(isolate->has_pending_exception());
54     return isolate->heap()->exception();
55   }
56   RETURN_RESULT_OR_FAILURE(isolate, Object::GetProperty(&it));
57 }
58 
RUNTIME_FUNCTION(Runtime_SetPropertyWithReceiver)59 RUNTIME_FUNCTION(Runtime_SetPropertyWithReceiver) {
60   HandleScope scope(isolate);
61 
62   DCHECK_EQ(5, args.length());
63   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, holder, 0);
64   CONVERT_ARG_HANDLE_CHECKED(Object, key, 1);
65   CONVERT_ARG_HANDLE_CHECKED(Object, value, 2);
66   CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 3);
67   CONVERT_LANGUAGE_MODE_ARG_CHECKED(language_mode, 4);
68 
69   bool success = false;
70   LookupIterator it = LookupIterator::PropertyOrElement(isolate, receiver, key,
71                                                         &success, holder);
72   if (!success) {
73     DCHECK(isolate->has_pending_exception());
74     return isolate->heap()->exception();
75   }
76   Maybe<bool> result = Object::SetSuperProperty(
77       &it, value, language_mode, Object::MAY_BE_STORE_FROM_KEYED);
78   MAYBE_RETURN(result, isolate->heap()->exception());
79   return *isolate->factory()->ToBoolean(result.FromJust());
80 }
81 
RUNTIME_FUNCTION(Runtime_CheckProxyGetSetTrapResult)82 RUNTIME_FUNCTION(Runtime_CheckProxyGetSetTrapResult) {
83   HandleScope scope(isolate);
84 
85   DCHECK_EQ(4, args.length());
86   CONVERT_ARG_HANDLE_CHECKED(Name, name, 0);
87   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, target, 1);
88   CONVERT_ARG_HANDLE_CHECKED(Object, trap_result, 2);
89   CONVERT_NUMBER_CHECKED(int64_t, access_kind, Int64, args[3]);
90 
91   RETURN_RESULT_OR_FAILURE(isolate, JSProxy::CheckGetSetTrapResult(
92                                         isolate, name, target, trap_result,
93                                         JSProxy::AccessKind(access_kind)));
94 }
95 
RUNTIME_FUNCTION(Runtime_CheckProxyHasTrap)96 RUNTIME_FUNCTION(Runtime_CheckProxyHasTrap) {
97   HandleScope scope(isolate);
98 
99   DCHECK_EQ(2, args.length());
100   CONVERT_ARG_HANDLE_CHECKED(Name, name, 0);
101   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, target, 1);
102 
103   Maybe<bool> result = JSProxy::CheckHasTrap(isolate, name, target);
104   if (!result.IsJust()) return isolate->heap()->exception();
105   return isolate->heap()->ToBoolean(result.FromJust());
106 }
107 
108 }  // namespace internal
109 }  // namespace v8
110