From b3dc7a728e2bfbbd4ce31298be772f4735b7f1d5 Mon Sep 17 00:00:00 2001 From: Jeff Walden Date: Wed, 15 Jun 2011 10:15:37 -0700 Subject: [PATCH] Bug 671091 - Add DefinePropertiesAndBrand to abstract out most explicit branding. NOT REVIEWED YET --HG-- extra : rebase_source : 9a48fc83a876c95e90c807eb0b007d35211eb41c --- js/src/jsobj.cpp | 17 +++-------------- js/src/jsregexp.cpp | 7 +------ js/src/jsstr.cpp | 9 ++------- js/src/vm/GlobalObject.cpp | 9 +++++++++ js/src/vm/GlobalObject.h | 7 +++++++ 5 files changed, 22 insertions(+), 27 deletions(-) diff --git a/js/src/jsobj.cpp b/js/src/jsobj.cpp index f4f20cc7cb1..cb55f3df719 100644 --- a/js/src/jsobj.cpp +++ b/js/src/jsobj.cpp @@ -3838,23 +3838,12 @@ DefineConstructorAndPrototype(JSContext *cx, JSObject *obj, JSProtoKey key, JSAt ctor->setProto(proto); } - /* Add properties and methods to the prototype and the constructor. */ - if ((ps && !JS_DefineProperties(cx, proto, ps)) || - (fs && !JS_DefineFunctions(cx, proto, fs)) || - (static_ps && !JS_DefineProperties(cx, ctor, static_ps)) || - (static_fs && !JS_DefineFunctions(cx, ctor, static_fs))) { + if (!DefinePropertiesAndBrand(cx, proto, ps, fs) || + (ctor != proto && !DefinePropertiesAndBrand(cx, ctor, static_ps, static_fs))) + { goto bad; } - /* - * Pre-brand the prototype and constructor if they have built-in methods. - * This avoids extra shape guard branch exits in the tracejitted code. - */ - if (fs) - proto->brand(cx); - if (ctor != proto && static_fs) - ctor->brand(cx); - /* * Make sure proto's emptyShape is available to be shared by objects of * this class. JSObject::emptyShape is a one-slot cache. If we omit this, diff --git a/js/src/jsregexp.cpp b/js/src/jsregexp.cpp index 2adb41bdb75..f9e3ef99df1 100644 --- a/js/src/jsregexp.cpp +++ b/js/src/jsregexp.cpp @@ -841,13 +841,8 @@ js_InitRegExpClass(JSContext *cx, JSObject *obj) if (!proto->initRegExp(cx, re.get())) return NULL; - /* - * Now add the standard methods to RegExp.prototype, and pre-brand for - * better shape-guarding code. - */ - if (!JS_DefineFunctions(cx, proto, regexp_methods)) + if (!DefinePropertiesAndBrand(cx, proto, NULL, regexp_methods)) return NULL; - proto->brand(cx); JSFunction *ctor = global->createConstructor(cx, regexp_construct, &js_RegExpClass, CLASS_ATOM(cx, RegExp), 2); diff --git a/js/src/jsstr.cpp b/js/src/jsstr.cpp index e773894331a..0077acb5219 100644 --- a/js/src/jsstr.cpp +++ b/js/src/jsstr.cpp @@ -3185,17 +3185,12 @@ js_InitStringClass(JSContext *cx, JSObject *obj) if (!LinkConstructorAndPrototype(cx, ctor, proto)) return NULL; - /* Add properties and methods to the prototype and the constructor. */ - if (!JS_DefineFunctions(cx, proto, string_methods) || - !JS_DefineFunctions(cx, ctor, string_static_methods)) + if (!DefinePropertiesAndBrand(cx, proto, NULL, string_methods) || + !DefinePropertiesAndBrand(cx, ctor, NULL, string_static_methods)) { return NULL; } - /* Pre-brand String and String.prototype for trace-jitted code. */ - proto->brand(cx); - ctor->brand(cx); - if (!DefineConstructorAndPrototype(cx, global, JSProto_String, ctor, proto)) return NULL; diff --git a/js/src/vm/GlobalObject.cpp b/js/src/vm/GlobalObject.cpp index eaa34c2ace6..ff5f90a4890 100644 --- a/js/src/vm/GlobalObject.cpp +++ b/js/src/vm/GlobalObject.cpp @@ -252,4 +252,13 @@ LinkConstructorAndPrototype(JSContext *cx, JSObject *ctor, JSObject *proto) ObjectValue(*ctor), PropertyStub, StrictPropertyStub, 0); } +bool +DefinePropertiesAndBrand(JSContext *cx, JSObject *obj, JSPropertySpec *ps, JSFunctionSpec *fs) +{ + if ((ps && !JS_DefineProperties(cx, obj, ps)) || (fs && !JS_DefineFunctions(cx, obj, fs))) + return false; + obj->brand(cx); + return true; +} + } // namespace js diff --git a/js/src/vm/GlobalObject.h b/js/src/vm/GlobalObject.h index f80db0dd3b2..67dd5bd8dcd 100644 --- a/js/src/vm/GlobalObject.h +++ b/js/src/vm/GlobalObject.h @@ -177,6 +177,13 @@ class GlobalObject : public ::JSObject { extern bool LinkConstructorAndPrototype(JSContext *cx, JSObject *ctor, JSObject *proto); +/* + * Define properties, then functions, on the object, then brand for tracing + * benefits. + */ +extern bool +DefinePropertiesAndBrand(JSContext *cx, JSObject *obj, JSPropertySpec *ps, JSFunctionSpec *fs); + } // namespace js js::GlobalObject *