Permalink
Browse files

Replaced ->isa with object_getClass() to conform to requirements of t…

…he modern runtime. This will affect arithnetical efficiency but is necessary if a seg fault is to be avoided on tagged pointers.
  • Loading branch information...
1 parent 6bfb258 commit e09a74d57ce9d6b58a4a8fc942234cc6d7c216f9 @mugginsoft committed Nov 25, 2012
Showing with 25 additions and 20 deletions.
  1. +25 −20 FScriptFramework/FSNumber.m
@@ -13,6 +13,7 @@
#import <math.h>
#import "FSBlock.h"
#import "BlockPrivate.h"
+#import <objc/objc-runtime.h>
@class NSPortCoder;
@@ -96,7 +97,7 @@ - (NSDate *)asDate
- (FSBoolean *)between:(NSNumber *)a and:(NSNumber *)b
{
- if (a && ((id)a)->isa == FSNumberClass && b && ((id)b)->isa == FSNumberClass)
+ if (a && object_getClass((id)a) == FSNumberClass && b && object_getClass((id)b) == FSNumberClass)
{
if ( ((FSNumber *)a)->value <= ((FSNumber *)b)->value )
return value >= ((FSNumber *)a)->value && value <= ((FSNumber *)b)->value ? fsTrue : fsFalse;
@@ -108,23 +109,23 @@ - (FSBoolean *)between:(NSNumber *)a and:(NSNumber *)b
- (NSNumber *)bitAnd:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
+ if (operand && object_getClass(operand) == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
return [FSNumber numberWithDouble:(NSUInteger)value & (NSUInteger)(((FSNumber *)operand)->value)];
else
return [super bitAnd:operand];
}
- (NSNumber *)bitOr:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
+ if (operand && object_getClass(operand) == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
return [FSNumber numberWithDouble:(NSUInteger)value | (NSUInteger)(((FSNumber *)operand)->value)];
else
return [super bitOr:operand];
}
- (NSNumber *)bitXor:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
+ if (operand && object_getClass(operand) == FSNumberClass && value >=0 && ((FSNumber *)operand)->value >= 0 && value <= UINT_MAX && ((FSNumber *)operand)->value <= UINT_MAX && value == floor(value) && ((FSNumber *)operand)->value == floor(((FSNumber *)operand)->value))
return [FSNumber numberWithDouble:(NSUInteger)value ^ (NSUInteger)(((FSNumber *)operand)->value)];
else
return [super bitXor:operand];
@@ -194,13 +195,13 @@ - (NSNumber *)log
- (NSNumber *)max:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return value > ((FSNumber *)operand)->value ? (NSNumber *)self : operand;
+ if (operand && object_getClass(operand) == FSNumberClass) return value > ((FSNumber *)operand)->value ? (NSNumber *)self : operand;
else return [super max:operand];
}
- (NSNumber *)min:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return value < ((FSNumber *)operand)->value ? (NSNumber *)self : operand;
+ if (operand && object_getClass(operand) == FSNumberClass) return value < ((FSNumber *)operand)->value ? (NSNumber *)self : operand;
else return [super min:operand];
}
@@ -211,79 +212,79 @@ - (NSNumber *) negated
- (NSNumber *)operator_asterisk:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return numberWithDouble(value * ((FSNumber *)operand)->value);
+ if (operand && object_getClass(operand) == FSNumberClass) return numberWithDouble(value * ((FSNumber *)operand)->value);
else return [super operator_asterisk:operand];
}
- (NSNumber *)operator_hyphen:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return numberWithDouble(value - ((FSNumber *)operand)->value);
+ if (operand && object_getClass(operand)== FSNumberClass) return numberWithDouble(value - ((FSNumber *)operand)->value);
else return [super operator_hyphen:operand];
}
- (NSPoint)operator_less_greater:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && value >= -CGFLOAT_MAX && value <= CGFLOAT_MAX && ((FSNumber *)operand)->value >= -CGFLOAT_MAX && ((FSNumber *)operand)->value <= CGFLOAT_MAX)
+ if (operand && object_getClass(operand) == FSNumberClass && value >= -CGFLOAT_MAX && value <= CGFLOAT_MAX && ((FSNumber *)operand)->value >= -CGFLOAT_MAX && ((FSNumber *)operand)->value <= CGFLOAT_MAX)
return NSMakePoint(value,((FSNumber *)operand)->value);
else
return [super operator_less_greater:operand];
}
- (NSNumber *)operator_plus:(id)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return numberWithDouble(value + ((FSNumber *)operand)->value);
+ if (operand && object_getClass(operand) == FSNumberClass) return numberWithDouble(value + ((FSNumber *)operand)->value);
else if (operand == fsFalse) return self;
else if (operand == fsTrue) return numberWithDouble(value + 1);
else return [super operator_plus:operand];
}
- (NSNumber *)operator_slash:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && ((FSNumber *)operand)->value != 0.0)
+ if (operand && object_getClass(operand) == FSNumberClass && ((FSNumber *)operand)->value != 0.0)
return numberWithDouble(value / ((FSNumber *)operand)->value);
else
return [super operator_slash:operand];
}
- (FSBoolean *)operator_equal:(id)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value == ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value == ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_equal:operand];
}
- (FSBoolean *)operator_tilde_equal:(id)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value != ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value != ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_tilde_equal:operand];
}
- (FSBoolean *)operator_greater:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value > ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value > ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_greater:operand];
}
- (FSBoolean *)operator_greater_equal:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value >= ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value >= ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_greater_equal:operand];
}
- (FSBoolean *)operator_less:(id)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value < ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value < ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_less:operand];
}
- (FSBoolean *)operator_less_equal:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass) return (value <= ((FSNumber *)operand)->value ? fsTrue : fsFalse);
+ if (operand && object_getClass(operand) == FSNumberClass) return (value <= ((FSNumber *)operand)->value ? fsTrue : fsFalse);
else return [super operator_less_equal:operand];
}
- (NSNumber *)raisedTo:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass)
+ if (operand && object_getClass(operand) == FSNumberClass)
{
double operandValue = ((FSNumber *)operand)->value;
if ((value < 0.0 && [(FSNumber *)operand hasFrac_bool]) || (value == 0.0 && operandValue <= 0.0))
@@ -304,7 +305,7 @@ - (NSNumber *)random
- (NSNumber *)rem:(NSNumber *)operand
{
- if (operand && ((id)operand)->isa == FSNumberClass && ((FSNumber *)operand)->value != 0.0)
+ if (operand && object_getClass(operand) == FSNumberClass && ((FSNumber *)operand)->value != 0.0)
return numberWithDouble(fmod(value,((FSNumber *)operand)->value));
else
return [super rem:operand];
@@ -465,7 +466,11 @@ - (id)initWithUnsignedShort:(unsigned short)val {return [self initWithDouble:val
- (BOOL) isEqual:anObject
{
- if (anObject && anObject->isa == FSNumberClass) // direct isa access for better performance
+ // direct isa access was used for better performance.
+ // this is now deprecated in the modern runtime as features such as tagged pointers may break.
+ // also note that object_getClass returns the swizzled classname in the case of KVO activation.
+ // if this proves to be a problem then -class is the answer as it returns the unswizzled class.
+ if (anObject && object_getClass(anObject) == FSNumberClass)
return value == ((FSNumber *)anObject)->value;
else
return [super isEqual:anObject];

0 comments on commit e09a74d

Please sign in to comment.