Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(3)

Side by Side Diff: src/runtime/runtime-classes.cc

Issue 1235883002: [strong] Strong classes can't extend null (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: cl feedback Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/runtime/runtime.h ('k') | test/mjsunit/strong/class-extend-null.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 the V8 project authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdlib.h> 5 #include <stdlib.h>
6 #include <limits> 6 #include <limits>
7 7
8 #include "src/v8.h" 8 #include "src/v8.h"
9 9
10 #include "src/arguments.h" 10 #include "src/arguments.h"
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 return *clone; 86 return *clone;
87 } 87 }
88 88
89 89
90 RUNTIME_FUNCTION(Runtime_HomeObjectSymbol) { 90 RUNTIME_FUNCTION(Runtime_HomeObjectSymbol) {
91 DCHECK(args.length() == 0); 91 DCHECK(args.length() == 0);
92 return isolate->heap()->home_object_symbol(); 92 return isolate->heap()->home_object_symbol();
93 } 93 }
94 94
95 95
96 RUNTIME_FUNCTION(Runtime_DefineClass) { 96 static MaybeHandle<Object> DefineClass(Isolate* isolate, Handle<Object> name,
97 HandleScope scope(isolate); 97 Handle<Object> super_class,
98 DCHECK(args.length() == 6); 98 Handle<JSFunction> constructor,
99 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0); 99 Handle<Script> script,
100 CONVERT_ARG_HANDLE_CHECKED(Object, super_class, 1); 100 int start_position, int end_position) {
101 CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, 2);
102 CONVERT_ARG_HANDLE_CHECKED(Script, script, 3);
103 CONVERT_SMI_ARG_CHECKED(start_position, 4);
104 CONVERT_SMI_ARG_CHECKED(end_position, 5);
105
106 Handle<Object> prototype_parent; 101 Handle<Object> prototype_parent;
107 Handle<Object> constructor_parent; 102 Handle<Object> constructor_parent;
108 103
109 if (super_class->IsTheHole()) { 104 if (super_class->IsTheHole()) {
110 prototype_parent = isolate->initial_object_prototype(); 105 prototype_parent = isolate->initial_object_prototype();
111 } else { 106 } else {
112 if (super_class->IsNull()) { 107 if (super_class->IsNull()) {
113 prototype_parent = isolate->factory()->null_value(); 108 prototype_parent = isolate->factory()->null_value();
114 } else if (super_class->IsSpecFunction()) { 109 } else if (super_class->IsSpecFunction()) {
115 if (Handle<JSFunction>::cast(super_class)->shared()->is_generator()) { 110 if (Handle<JSFunction>::cast(super_class)->shared()->is_generator()) {
116 THROW_NEW_ERROR_RETURN_FAILURE( 111 THROW_NEW_ERROR(
117 isolate, 112 isolate,
118 NewTypeError(MessageTemplate::kExtendsValueGenerator, super_class)); 113 NewTypeError(MessageTemplate::kExtendsValueGenerator, super_class),
114 Object);
119 } 115 }
120 ASSIGN_RETURN_FAILURE_ON_EXCEPTION( 116 ASSIGN_RETURN_ON_EXCEPTION(
121 isolate, prototype_parent, 117 isolate, prototype_parent,
122 Runtime::GetObjectProperty(isolate, super_class, 118 Runtime::GetObjectProperty(isolate, super_class,
123 isolate->factory()->prototype_string(), 119 isolate->factory()->prototype_string(),
124 SLOPPY)); 120 SLOPPY),
121 Object);
125 if (!prototype_parent->IsNull() && !prototype_parent->IsSpecObject()) { 122 if (!prototype_parent->IsNull() && !prototype_parent->IsSpecObject()) {
126 THROW_NEW_ERROR_RETURN_FAILURE( 123 THROW_NEW_ERROR(
127 isolate, NewTypeError(MessageTemplate::kPrototypeParentNotAnObject, 124 isolate, NewTypeError(MessageTemplate::kPrototypeParentNotAnObject,
128 prototype_parent)); 125 prototype_parent),
126 Object);
129 } 127 }
130 constructor_parent = super_class; 128 constructor_parent = super_class;
131 } else { 129 } else {
132 // TODO(arv): Should be IsConstructor. 130 // TODO(arv): Should be IsConstructor.
133 THROW_NEW_ERROR_RETURN_FAILURE( 131 THROW_NEW_ERROR(
134 isolate, 132 isolate,
135 NewTypeError(MessageTemplate::kExtendsValueNotFunction, super_class)); 133 NewTypeError(MessageTemplate::kExtendsValueNotFunction, super_class),
134 Object);
136 } 135 }
137 } 136 }
138 137
139 Handle<Map> map = 138 Handle<Map> map =
140 isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize); 139 isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
141 Map::SetPrototype(map, prototype_parent); 140 Map::SetPrototype(map, prototype_parent);
142 map->SetConstructor(*constructor); 141 map->SetConstructor(*constructor);
143 Handle<JSObject> prototype = isolate->factory()->NewJSObjectFromMap(map); 142 Handle<JSObject> prototype = isolate->factory()->NewJSObjectFromMap(map);
144 143
145 Handle<String> name_string = name->IsString() 144 Handle<String> name_string = name->IsString()
146 ? Handle<String>::cast(name) 145 ? Handle<String>::cast(name)
147 : isolate->factory()->empty_string(); 146 : isolate->factory()->empty_string();
148 constructor->shared()->set_name(*name_string); 147 constructor->shared()->set_name(*name_string);
149 148
150 if (!super_class->IsTheHole()) { 149 if (!super_class->IsTheHole()) {
151 Handle<Code> stub(isolate->builtins()->JSConstructStubForDerived()); 150 Handle<Code> stub(isolate->builtins()->JSConstructStubForDerived());
152 constructor->shared()->set_construct_stub(*stub); 151 constructor->shared()->set_construct_stub(*stub);
153 } 152 }
154 153
155 JSFunction::SetPrototype(constructor, prototype); 154 JSFunction::SetPrototype(constructor, prototype);
156 PropertyAttributes attribs = 155 PropertyAttributes attribs =
157 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY); 156 static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
158 RETURN_FAILURE_ON_EXCEPTION( 157 RETURN_ON_EXCEPTION(isolate,
159 isolate, JSObject::SetOwnPropertyIgnoreAttributes( 158 JSObject::SetOwnPropertyIgnoreAttributes(
160 constructor, isolate->factory()->prototype_string(), 159 constructor, isolate->factory()->prototype_string(),
161 prototype, attribs)); 160 prototype, attribs),
161 Object);
162 162
163 // TODO(arv): Only do this conditionally. 163 // TODO(arv): Only do this conditionally.
164 Handle<Symbol> home_object_symbol(isolate->heap()->home_object_symbol()); 164 Handle<Symbol> home_object_symbol(isolate->heap()->home_object_symbol());
165 RETURN_FAILURE_ON_EXCEPTION( 165 RETURN_ON_EXCEPTION(
166 isolate, JSObject::SetOwnPropertyIgnoreAttributes( 166 isolate, JSObject::SetOwnPropertyIgnoreAttributes(
167 constructor, home_object_symbol, prototype, DONT_ENUM)); 167 constructor, home_object_symbol, prototype, DONT_ENUM),
168 Object);
168 169
169 if (!constructor_parent.is_null()) { 170 if (!constructor_parent.is_null()) {
170 RETURN_FAILURE_ON_EXCEPTION( 171 RETURN_ON_EXCEPTION(
171 isolate, 172 isolate, JSObject::SetPrototype(constructor, constructor_parent, false),
172 JSObject::SetPrototype(constructor, constructor_parent, false)); 173 Object);
173 } 174 }
174 175
175 JSObject::AddProperty(prototype, isolate->factory()->constructor_string(), 176 JSObject::AddProperty(prototype, isolate->factory()->constructor_string(),
176 constructor, DONT_ENUM); 177 constructor, DONT_ENUM);
177 178
178 // Install private properties that are used to construct the FunctionToString. 179 // Install private properties that are used to construct the FunctionToString.
179 RETURN_FAILURE_ON_EXCEPTION( 180 RETURN_ON_EXCEPTION(
180 isolate, Object::SetProperty(constructor, 181 isolate, Object::SetProperty(constructor,
181 isolate->factory()->class_script_symbol(), 182 isolate->factory()->class_script_symbol(),
182 script, STRICT)); 183 script, STRICT),
183 RETURN_FAILURE_ON_EXCEPTION( 184 Object);
185 RETURN_ON_EXCEPTION(
184 isolate, 186 isolate,
185 Object::SetProperty( 187 Object::SetProperty(
186 constructor, isolate->factory()->class_start_position_symbol(), 188 constructor, isolate->factory()->class_start_position_symbol(),
187 handle(Smi::FromInt(start_position), isolate), STRICT)); 189 handle(Smi::FromInt(start_position), isolate), STRICT),
188 RETURN_FAILURE_ON_EXCEPTION( 190 Object);
191 RETURN_ON_EXCEPTION(
189 isolate, Object::SetProperty( 192 isolate, Object::SetProperty(
190 constructor, isolate->factory()->class_end_position_symbol(), 193 constructor, isolate->factory()->class_end_position_symbol(),
191 handle(Smi::FromInt(end_position), isolate), STRICT)); 194 handle(Smi::FromInt(end_position), isolate), STRICT),
195 Object);
192 196
193 return *constructor; 197 return constructor;
194 } 198 }
195 199
196 200
201 RUNTIME_FUNCTION(Runtime_DefineClass) {
202 HandleScope scope(isolate);
203 DCHECK(args.length() == 6);
204 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
205 CONVERT_ARG_HANDLE_CHECKED(Object, super_class, 1);
206 CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, 2);
207 CONVERT_ARG_HANDLE_CHECKED(Script, script, 3);
208 CONVERT_SMI_ARG_CHECKED(start_position, 4);
209 CONVERT_SMI_ARG_CHECKED(end_position, 5);
210
211 Handle<Object> result;
212 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
213 isolate, result, DefineClass(isolate, name, super_class, constructor,
214 script, start_position, end_position));
215 return *result;
216 }
217
218
219 RUNTIME_FUNCTION(Runtime_DefineClassStrong) {
220 HandleScope scope(isolate);
221 DCHECK(args.length() == 6);
222 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
223 CONVERT_ARG_HANDLE_CHECKED(Object, super_class, 1);
224 CONVERT_ARG_HANDLE_CHECKED(JSFunction, constructor, 2);
225 CONVERT_ARG_HANDLE_CHECKED(Script, script, 3);
226 CONVERT_SMI_ARG_CHECKED(start_position, 4);
227 CONVERT_SMI_ARG_CHECKED(end_position, 5);
228
229 if (super_class->IsNull()) {
230 THROW_NEW_ERROR_RETURN_FAILURE(
231 isolate, NewTypeError(MessageTemplate::kStrongExtendNull));
232 }
233
234 Handle<Object> result;
235 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
236 isolate, result, DefineClass(isolate, name, super_class, constructor,
237 script, start_position, end_position));
238 return *result;
239 }
240
241
197 RUNTIME_FUNCTION(Runtime_DefineClassMethod) { 242 RUNTIME_FUNCTION(Runtime_DefineClassMethod) {
198 HandleScope scope(isolate); 243 HandleScope scope(isolate);
199 DCHECK(args.length() == 3); 244 DCHECK(args.length() == 3);
200 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0); 245 CONVERT_ARG_HANDLE_CHECKED(JSObject, object, 0);
201 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); 246 CONVERT_ARG_HANDLE_CHECKED(Name, name, 1);
202 CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 2); 247 CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 2);
203 248
204 RETURN_FAILURE_ON_EXCEPTION(isolate, 249 RETURN_FAILURE_ON_EXCEPTION(isolate,
205 JSObject::DefinePropertyOrElementIgnoreAttributes( 250 JSObject::DefinePropertyOrElementIgnoreAttributes(
206 object, name, function, DONT_ENUM)); 251 object, name, function, DONT_ENUM));
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
480 return nullptr; 525 return nullptr;
481 } 526 }
482 527
483 528
484 RUNTIME_FUNCTION(Runtime_CallSuperWithSpread) { 529 RUNTIME_FUNCTION(Runtime_CallSuperWithSpread) {
485 UNIMPLEMENTED(); 530 UNIMPLEMENTED();
486 return nullptr; 531 return nullptr;
487 } 532 }
488 } // namespace internal 533 } // namespace internal
489 } // namespace v8 534 } // namespace v8
OLDNEW
« no previous file with comments | « src/runtime/runtime.h ('k') | test/mjsunit/strong/class-extend-null.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698