@@ -72,16 +72,6 @@ class Crc {
7272 return JS::GetMaybePtrFromReservedSlot<Crc>(obj, CrcSlot);
7373 }
7474
75- static bool isPrototype (JSObject* obj) { return getPriv (obj) == nullptr ; }
76-
77- static bool checkIsInstance (JSContext* cx, JSObject* obj, const char * what) {
78- if (isPrototype (obj)) {
79- JS_ReportErrorASCII (cx, " can't %s on Crc.prototype" , what);
80- return false ;
81- }
82- return true ;
83- }
84-
8575 static bool constructor (JSContext* cx, unsigned argc, JS::Value* vp) {
8676 JS::CallArgs args = JS::CallArgsFromVp (argc, vp);
8777
@@ -106,26 +96,21 @@ class Crc {
10696 JS::CallArgs args = JS::CallArgsFromVp (argc, vp);
10797 JS::RootedObject thisObj (cx);
10898 if (!args.computeThis (cx, &thisObj)) return false ;
109- if (!checkIsInstance (cx, thisObj, " call update() " )) return false ;
99+ if (!JS_InstanceOf (cx, thisObj, &Crc::klass, &args )) return false ;
110100 return getPriv (thisObj)->updateImpl (cx, args);
111101 }
112102
113103 static bool getChecksum (JSContext* cx, unsigned argc, JS::Value* vp) {
114104 JS::CallArgs args = JS::CallArgsFromVp (argc, vp);
115105 JS::RootedObject thisObj (cx);
116106 if (!args.computeThis (cx, &thisObj)) return false ;
117- if (!checkIsInstance (cx, thisObj, " read checksum " )) return false ;
107+ if (!JS_InstanceOf (cx, thisObj, &Crc::klass, &args )) return false ;
118108 return getPriv (thisObj)->getChecksumImpl (cx, args);
119109 }
120110
121111 static bool newEnumerate (JSContext* cx, JS::HandleObject obj,
122112 JS::MutableHandleIdVector properties,
123113 bool enumerableOnly) {
124- // We only want to enumerate if obj is the prototype. For instances, we
125- // should return immediately, and this will be called again on the
126- // prototype.
127- if (!isPrototype (obj)) return true ;
128-
129114 jsid idUpdate =
130115 JS::PropertyKey::fromPinnedString (JS_AtomizeAndPinString (cx, " update" ));
131116 if (!properties.append (idUpdate)) return false ;
@@ -139,13 +124,6 @@ class Crc {
139124
140125 static bool resolve (JSContext* cx, JS::HandleObject obj, JS::HandleId id,
141126 bool * resolved) {
142- // We only want to resolve if obj is the prototype. For instances, we should
143- // return immediately, and this will be called again on the prototype.
144- if (!isPrototype (obj)) {
145- *resolved = false ;
146- return true ;
147- }
148-
149127 if (!id.isString ()) {
150128 *resolved = false ;
151129 return true ;
@@ -214,24 +192,21 @@ class Crc {
214192 public:
215193 static bool DefinePrototype (JSContext* cx) {
216194 JS::RootedObject global (cx, JS::CurrentGlobalOrNull (cx));
217- JS::RootedObject proto (
218- cx, JS_InitClass (cx,
219- global, // the object in which to define the class
220- nullptr , // the prototype of the parent class
221- // (in our case, no parent class)
222- &Crc::klass, // the JSClass defined above
223- &Crc::constructor,
224- 0 , // constructor and num. args
225- // The four nullptrs below are for arrays where you
226- // would list predefined (not lazy) methods and
227- // properties, static and non-static
228- nullptr , nullptr , nullptr , nullptr ));
229- if (!proto) return false ;
230-
231- // Here's how we tell the prototype apart from instances. The private
232- // pointer will be null.
233- JS::SetReservedSlot (proto, CrcSlot, JS::UndefinedValue ());
234- return true ;
195+ return JS_InitClass (cx,
196+ global, // the object in which to define the class
197+ nullptr , // the prototype object, Crc.prototype
198+ // (in our case, a plain JS object because
199+ // calling "Crc.prototype.update" does not
200+ // make sense)
201+ nullptr , // the prototype of the parent class (in our
202+ // case, Object.prototype)
203+ Crc::klass.name , // "Crc", the constructor name
204+ &Crc::constructor,
205+ 0 , // constructor and num. args
206+ // The four nullptrs below are for arrays where you
207+ // would list predefined (not lazy) methods and
208+ // properties, static and non-static
209+ nullptr , nullptr , nullptr , nullptr );
235210 }
236211};
237212constexpr JSClassOps Crc::classOps;
0 commit comments