Chromium Code Reviews

Unified Diff: src/runtime/runtime.cc

Issue 631433002: Classes runtime (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Create prototype object with initial prototype parent Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Index: src/runtime/runtime.cc
diff --git a/src/runtime/runtime.cc b/src/runtime/runtime.cc
index 815bc4592a65600086955620e69c10557621eb25..cb29d627c4457d923092b69adc2c1a94afa58e96 100644
--- a/src/runtime/runtime.cc
+++ b/src/runtime/runtime.cc
@@ -361,6 +361,87 @@ RUNTIME_FUNCTION(Runtime_StoreToSuper_Sloppy) {
}
+RUNTIME_FUNCTION(Runtime_DefineClass) {
+ HandleScope scope(isolate);
+ DCHECK(args.length() == 3);
+ CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
+ CONVERT_ARG_HANDLE_CHECKED(Object, super_class, 1);
+ CONVERT_ARG_HANDLE_CHECKED(Object, constructor, 2);
+
+ Handle<Object> prototype_parent;
+ Handle<Object> constructor_parent;
+ bool has_constructor_parent = false;
+
+ if (super_class->IsTheHole()) {
+ prototype_parent = isolate->initial_object_prototype();
+ } else {
+ if (super_class->IsNull()) {
+ prototype_parent = isolate->factory()->null_value();
+ } else if (super_class->IsSpecFunction()) {
+ ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
+ isolate, prototype_parent,
+ Runtime::GetObjectProperty(isolate, super_class,
+ isolate->factory()->prototype_string()));
+ if (!prototype_parent->IsNull() && !prototype_parent->IsSpecObject()) {
+ Handle<Object> args[1] = {prototype_parent};
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate, NewTypeError("prototype_parent_not_an_object",
+ HandleVector(args, 1)));
+ }
+ constructor_parent = super_class;
+ has_constructor_parent = true;
+ } else {
+ // TODO(arv): Should be IsConstructor.
+ Handle<Object> args[1] = {super_class};
+ THROW_NEW_ERROR_RETURN_FAILURE(
+ isolate,
+ NewTypeError("extends_value_not_a_function", HandleVector(args, 1)));
+ }
+ }
+
+ Handle<Map> map =
+ isolate->factory()->NewMap(JS_OBJECT_TYPE, JSObject::kHeaderSize);
+ map->set_prototype(*prototype_parent);
+ Handle<JSObject> prototype = isolate->factory()->NewJSObjectFromMap(map);
+
+ Handle<String> name_string = name->IsString()
+ ? Handle<String>::cast(name)
+ : isolate->factory()->empty_string();
+
+ Handle<JSFunction> ctor;
+ if (constructor->IsSpecFunction()) {
+ ctor = Handle<JSFunction>::cast(constructor);
+ JSFunction::SetPrototype(ctor, prototype);
+ PropertyAttributes attribs =
+ static_cast<PropertyAttributes>(DONT_ENUM | DONT_DELETE | READ_ONLY);
+ RETURN_FAILURE_ON_EXCEPTION(
+ isolate,
+ JSObject::SetOwnPropertyIgnoreAttributes(
+ ctor, isolate->factory()->prototype_string(), prototype, attribs));
+ } else {
+ // TODO(arv): This should not use an empty function but a function that
+ // calls super.
+ Handle<Code> code(isolate->builtins()->builtin(Builtins::kEmptyFunction));
+ ctor = isolate->factory()->NewFunction(name_string, code, prototype, true);
+ }
+
+ Handle<Symbol> home_object_symbol(isolate->heap()->home_object_symbol());
+ RETURN_FAILURE_ON_EXCEPTION(
+ isolate, JSObject::SetOwnPropertyIgnoreAttributes(
+ ctor, home_object_symbol, prototype, DONT_ENUM));
+
+ if (has_constructor_parent) {
Dmitry Lomov (no reviews) 2014/10/07 15:09:37 Replace with constructor_parent.is_null() and remo
arv (Not doing code reviews) 2014/10/07 15:16:52 Good idea. Done.
+ RETURN_FAILURE_ON_EXCEPTION(
+ isolate, JSObject::SetPrototype(ctor, constructor_parent, false));
+ }
+
+ JSObject::AddProperty(prototype, isolate->factory()->constructor_string(),
+ ctor, DONT_ENUM);
+
+ return *ctor;
+}
+
+
RUNTIME_FUNCTION(Runtime_IsExtensible) {
SealHandleScope shs(isolate);
DCHECK(args.length() == 1);

Powered by Google App Engine