1 // Copyright 2018 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 #ifndef V8_OBJECTS_JS_PROXY_H_ 6 #define V8_OBJECTS_JS_PROXY_H_ 7 8 #include "src/objects/js-objects.h" 9 #include "torque-generated/builtin-definitions.h" 10 11 // Has to be the last include (doesn't have include guards): 12 #include "src/objects/object-macros.h" 13 14 namespace v8 { 15 namespace internal { 16 17 #include "torque-generated/src/objects/js-proxy-tq.inc" 18 19 // The JSProxy describes EcmaScript Harmony proxies 20 class JSProxy : public TorqueGeneratedJSProxy<JSProxy, JSReceiver> { 21 public: 22 V8_WARN_UNUSED_RESULT static MaybeHandle<JSProxy> New(Isolate* isolate, 23 Handle<Object>, 24 Handle<Object>); 25 26 static MaybeHandle<NativeContext> GetFunctionRealm(Handle<JSProxy> proxy); 27 28 V8_INLINE bool IsRevoked() const; 29 static void Revoke(Handle<JSProxy> proxy); 30 31 // ES6 9.5.1 32 static MaybeHandle<HeapObject> GetPrototype(Handle<JSProxy> receiver); 33 34 // ES6 9.5.2 35 V8_WARN_UNUSED_RESULT static Maybe<bool> SetPrototype( 36 Handle<JSProxy> proxy, Handle<Object> value, bool from_javascript, 37 ShouldThrow should_throw); 38 // ES6 9.5.3 39 V8_WARN_UNUSED_RESULT static Maybe<bool> IsExtensible(Handle<JSProxy> proxy); 40 41 // ES6, #sec-isarray. NOT to be confused with %_IsArray. 42 V8_WARN_UNUSED_RESULT static Maybe<bool> IsArray(Handle<JSProxy> proxy); 43 44 // ES6 9.5.4 (when passed kDontThrow) 45 V8_WARN_UNUSED_RESULT static Maybe<bool> PreventExtensions( 46 Handle<JSProxy> proxy, ShouldThrow should_throw); 47 48 // ES6 9.5.5 49 V8_WARN_UNUSED_RESULT static Maybe<bool> GetOwnPropertyDescriptor( 50 Isolate* isolate, Handle<JSProxy> proxy, Handle<Name> name, 51 PropertyDescriptor* desc); 52 53 // ES6 9.5.6 54 V8_WARN_UNUSED_RESULT static Maybe<bool> DefineOwnProperty( 55 Isolate* isolate, Handle<JSProxy> object, Handle<Object> key, 56 PropertyDescriptor* desc, Maybe<ShouldThrow> should_throw); 57 58 // ES6 9.5.7 59 V8_WARN_UNUSED_RESULT static Maybe<bool> HasProperty(Isolate* isolate, 60 Handle<JSProxy> proxy, 61 Handle<Name> name); 62 63 // This function never returns false. 64 // It returns either true or throws. 65 V8_WARN_UNUSED_RESULT static Maybe<bool> CheckHasTrap( 66 Isolate* isolate, Handle<Name> name, Handle<JSReceiver> target); 67 68 // ES6 9.5.10 69 V8_WARN_UNUSED_RESULT static Maybe<bool> CheckDeleteTrap( 70 Isolate* isolate, Handle<Name> name, Handle<JSReceiver> target); 71 72 // ES6 9.5.8 73 V8_WARN_UNUSED_RESULT static MaybeHandle<Object> GetProperty( 74 Isolate* isolate, Handle<JSProxy> proxy, Handle<Name> name, 75 Handle<Object> receiver, bool* was_found); 76 77 enum AccessKind { kGet, kSet }; 78 79 static MaybeHandle<Object> CheckGetSetTrapResult(Isolate* isolate, 80 Handle<Name> name, 81 Handle<JSReceiver> target, 82 Handle<Object> trap_result, 83 AccessKind access_kind); 84 85 // ES6 9.5.9 86 V8_WARN_UNUSED_RESULT static Maybe<bool> SetProperty( 87 Handle<JSProxy> proxy, Handle<Name> name, Handle<Object> value, 88 Handle<Object> receiver, Maybe<ShouldThrow> should_throw); 89 90 // ES6 9.5.10 (when passed LanguageMode::kSloppy) 91 V8_WARN_UNUSED_RESULT static Maybe<bool> DeletePropertyOrElement( 92 Handle<JSProxy> proxy, Handle<Name> name, LanguageMode language_mode); 93 94 // ES6 9.5.12 95 V8_WARN_UNUSED_RESULT static Maybe<bool> OwnPropertyKeys( 96 Isolate* isolate, Handle<JSReceiver> receiver, Handle<JSProxy> proxy, 97 PropertyFilter filter, KeyAccumulator* accumulator); 98 99 V8_WARN_UNUSED_RESULT static Maybe<PropertyAttributes> GetPropertyAttributes( 100 LookupIterator* it); 101 102 // Dispatched behavior. 103 DECL_PRINTER(JSProxy) 104 DECL_VERIFIER(JSProxy) 105 106 static const int kMaxIterationLimit = 100 * 1024; 107 108 // kTargetOffset aliases with the elements of JSObject. The fact that 109 // JSProxy::target is a Javascript value which cannot be confused with an 110 // elements backing store is exploited by loading from this offset from an 111 // unknown JSReceiver. 112 STATIC_ASSERT(static_cast<int>(JSObject::kElementsOffset) == 113 static_cast<int>(JSProxy::kTargetOffset)); 114 115 using BodyDescriptor = 116 FixedBodyDescriptor<JSReceiver::kPropertiesOrHashOffset, kSize, kSize>; 117 118 static Maybe<bool> SetPrivateSymbol(Isolate* isolate, Handle<JSProxy> proxy, 119 Handle<Symbol> private_name, 120 PropertyDescriptor* desc, 121 Maybe<ShouldThrow> should_throw); 122 123 TQ_OBJECT_CONSTRUCTORS(JSProxy) 124 }; 125 126 // JSProxyRevocableResult is just a JSObject with a specific initial map. 127 // This initial map adds in-object properties for "proxy" and "revoke". 128 // See https://tc39.github.io/ecma262/#sec-proxy.revocable 129 class JSProxyRevocableResult : public JSObject { 130 public: 131 // Layout description. 132 DEFINE_FIELD_OFFSET_CONSTANTS( 133 JSObject::kHeaderSize, TORQUE_GENERATED_JS_PROXY_REVOCABLE_RESULT_FIELDS) 134 135 // Indices of in-object properties. 136 static const int kProxyIndex = 0; 137 static const int kRevokeIndex = 1; 138 139 private: 140 DISALLOW_IMPLICIT_CONSTRUCTORS(JSProxyRevocableResult); 141 }; 142 143 } // namespace internal 144 } // namespace v8 145 146 #include "src/objects/object-macros-undef.h" 147 148 #endif // V8_OBJECTS_JS_PROXY_H_ 149