ObjFW  Check-in [fbee44d38e]

Overview
Comment:Cleaner -[setImplementation:...] for old GNU runtime.

Still work needed to correctly update the dtable.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: fbee44d38e160b7680c486aa403848a5facfddb5cd631a443b7c510171965778
User & Date: js on 2011-07-31 19:45:37
Other Links: manifest | tags
Context
2011-07-31
20:03
Correctly update the dtable for the old GNU runtime. check-in: df914ca51d user: js tags: trunk
19:45
Cleaner -[setImplementation:...] for old GNU runtime. check-in: fbee44d38e user: js tags: trunk
02:07
Implement adding methods for the GNU runtime. check-in: db5f8c2c1b user: js tags: trunk
Changes

Modified src/OFObject.h from [5c52be14f5] to [7e695cf1f5].

374
375
376
377
378
379
380
















381
382
383
384
385
386
387
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403







+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+







 * \param selector The selector of the instance method to replace
 * \param class_ The class from which the new instance method should be taken
 * \return The old implementation
 */
+ (IMP)replaceInstanceMethod: (SEL)selector
	 withMethodFromClass: (Class)class_;

/**
 * \brief Adds a class method to the class.
 *
 * If the method already exists, nothing is done and NO is returned. If you want
 * to change the implementation of a method, use
 * setImplementation:forClassMethod:.
 *
 * \param selector The selector for the new method
 * \param typeEncoding The type encoding for the new method
 * \param implementation The implementation for the new method
 * \return Whether the method has been added
 */
+ (BOOL)addClassMethod: (SEL)selector
      withTypeEncoding: (const char*)typeEncoding
	implementation: (IMP)implementation;

/**
 * \brief Adds an instance method to the class.
 *
 * If the method already exists, nothing is done and NO is returned. If you want
 * to change the implementation of a method, use
 * setImplementation:forInstanceMethod:.
 *

Modified src/OFObject.m from [e350530160] to [c563fc5512].

336
337
338
339
340
341
342
343

344
345
346
347



348
349
350

351
352
353







354

355

356
357
358
359
360
361











362
363

364
365
366
367
368
369
370
336
337
338
339
340
341
342

343
344



345
346
347
348
349
350
351



352
353
354
355
356
357
358
359
360

361
362





363
364
365
366
367
368
369
370
371
372
373
374

375
376
377
378
379
380
381
382







-
+

-
-
-
+
+
+



+
-
-
-
+
+
+
+
+
+
+

+
-
+

-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+

-
+







	if (newImp == (IMP)0 || !class_respondsToSelector(self->isa, selector))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	return objc_replace_class_method(self, selector, newImp);
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t method;
	IMP oldImp;
	MethodList_t iter;

	/* The class method is the instance method of the meta class */
	if ((method = class_get_instance_method(self->class_pointer,
	    selector)) == NULL)
	method = class_get_class_method(self->class_pointer, selector);

	if (newImp == (IMP)0 || method == METHOD_NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	for (iter = ((Class)self->class_pointer)->methods; iter != NULL;
	if ((oldImp = method_get_imp(method)) == (IMP)0 || newImp == (IMP)0)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
	    iter = iter->method_next) {
		int i;

		for (i = 0; i < iter->method_count; i++)
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
	method->method_imp = newImp;
				iter->method_list[i].method_imp = newImp;

	/* Update the dtable if necessary */
	if (sarray_get_safe(((Class)self->class_pointer)->dtable,
	    (sidx)method->method_name->sel_id))
		sarray_at_put_safe(((Class)self->class_pointer)->dtable,
		    (sidx)method->method_name->sel_id, method->method_imp);
				sarray_at_put_safe(
				    ((Class)self->class_pointer)->dtable,
				    (sidx)selector->sel_id, newImp);

				return oldImp;
			}
	}

	assert([self addClassMethod: selector
		   withTypeEncoding: method->method_types
		     implementation: newImp]);

	return oldImp;
	return (IMP)0;
#else
	Method method;

	if (newImp == (IMP)0 ||
	    (method = class_getClassMethod(self, selector)) == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
399
400
401
402
403
404
405



406

407
408
409

410
411
412

413
414
415







416

417

418
419
420
421
422
423


424
425









426
427
428
429
430
431
432
411
412
413
414
415
416
417
418
419
420

421

422

423
424
425
426
427



428
429
430
431
432
433
434
435
436

437
438





439
440
441

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457







+
+
+
-
+
-

-
+



+
-
-
-
+
+
+
+
+
+
+

+
-
+

-
-
-
-
-
+
+

-
+
+
+
+
+
+
+
+
+







#if defined(OF_OBJFW_RUNTIME)
	if (newImp == (IMP)0 || !class_respondsToSelector(self, selector))
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	return objc_replace_instance_method(self, selector, newImp);
#elif defined(OF_OLD_GNU_RUNTIME)
	Method_t method;
	MethodList_t iter;

	Method_t method = class_get_instance_method(self, selector);
	method = class_get_instance_method(self, selector);
	IMP oldImp;

	if (method == NULL)
	if (newImp == (IMP)0 || method == METHOD_NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];

	for (iter = ((Class)self)->methods; iter != NULL;
	if ((oldImp = method_get_imp(method)) == (IMP)0 || newImp == (IMP)0)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];
	    iter = iter->method_next) {
		int i;

		for (i = 0; i < iter->method_count; i++)
			if (sel_eq(iter->method_list[i].method_name,
			    selector)) {
				IMP oldImp;

				oldImp = iter->method_list[i].method_imp;
	method->method_imp = newImp;
				iter->method_list[i].method_imp = newImp;

	/* Update the dtable if necessary */
	if (sarray_get_safe(((Class)self)->dtable,
	    (sidx)method->method_name->sel_id))
		sarray_at_put_safe(((Class)self)->dtable,
		    (sidx)method->method_name->sel_id, method->method_imp);
				sarray_at_put_safe(((Class)self)->dtable,
				    (sidx)selector->sel_id, newImp);

	return oldImp;
				return oldImp;
			}
	}

	assert([self addInstanceMethod: selector
		      withTypeEncoding: method->method_types
			implementation: newImp]);

	return (IMP)0;
#else
	Method method;

	if (newImp == (IMP)0 ||
	    (method = class_getInstanceMethod(self, selector)) == NULL)
		@throw [OFInvalidArgumentException newWithClass: self
						       selector: _cmd];