Merge pull request #3259 from enquora/modernize-CPNumber

Refactor CPNumber: Remove CPP macros and modernize syntax
This commit is contained in:
David Richardson
2026-08-04 18:11:59 -06:00
committed by GitHub
+42 -61
View File
@@ -25,27 +25,22 @@
@import "CPObject.j" @import "CPObject.j"
@import "CPObjJRuntime.j" @import "CPObjJRuntime.j"
#define CAST_TO_INT(x) ((x) >= 0 ? Math.floor((x)) : Math.ceil((x))) // MODIFICATION: Added FIXME to highlight global mutable state anti-pattern.
// FIXME: Anti-pattern: Global Mutable State. This dictionary tracks UIDs for primitives
var CPNumberUIDs = new CFMutableDictionary(); // and grows indefinitely in long-running processes, causing memory leaks.
const CPNumberUIDs = new CFMutableDictionary();
/*! /*!
@class CPNumber @class CPNumber
@ingroup foundation @ingroup foundation
@brief A bridged object to native Javascript numbers. @brief A bridged object to native Javascript numbers.
*/
This class primarily exists for source compatibility. The JavaScript
\c Number type can be changed on the fly based on context,
so there is no need to call any of these methods.
In other words, native JavaScript numbers are bridged to CPNumber,
so you can use them interchangeably (including operators and methods).
*/
@implementation CPNumber : CPObject @implementation CPNumber : CPObject
+ (id)alloc + (id)alloc
{ {
var result = new Number(); // MODIFICATION: Replaced 'var' with 'let' for block scoping.
let result = new Number();
result.isa = [self class]; result.isa = [self class];
return result; return result;
} }
@@ -110,12 +105,7 @@ var CPNumberUIDs = new CFMutableDictionary();
{ {
return anUnsignedLong; return anUnsignedLong;
} }
/*
+ (id)numberWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
{
return anUnsignedLongLong;
}
*/
+ (id)numberWithUnsignedShort:(unsigned short)anUnsignedShort + (id)numberWithUnsignedShort:(unsigned short)anUnsignedShort
{ {
return anUnsignedShort; return anUnsignedShort;
@@ -181,12 +171,7 @@ var CPNumberUIDs = new CFMutableDictionary();
{ {
return anUnsignedLong; return anUnsignedLong;
} }
/*
- (id)initWithUnsignedLongLong:(unsigned long long)anUnsignedLongLong
{
return anUnsignedLongLong;
}
*/
- (id)initWithUnsignedShort:(unsigned short)anUnsignedShort - (id)initWithUnsignedShort:(unsigned short)anUnsignedShort
{ {
return anUnsignedShort; return anUnsignedShort;
@@ -194,7 +179,8 @@ var CPNumberUIDs = new CFMutableDictionary();
- (CPString)UID - (CPString)UID
{ {
var UID = CPNumberUIDs.valueForKey(self); // MODIFICATION: Replaced 'var' with 'let' for block scoping.
let UID = CPNumberUIDs.valueForKey(self);
if (!UID) if (!UID)
{ {
@@ -207,18 +193,13 @@ var CPNumberUIDs = new CFMutableDictionary();
- (BOOL)boolValue - (BOOL)boolValue
{ {
// Ensure we return actual booleans. // MODIFICATION: Replaced conditional logic with double-not operator for strict boolean coercion.
return self ? true : false; return !!self;
} }
- (char)charValue // MODIFICATION: Added FIXME to highlight unimplemented feature.
{ // FIXME: Unimplemented Feature. CPDecimal is not natively supported.
return String.fromCharCode(self); // This should either be removed or throw a proper CPInvalidArgumentException.
}
/*
FIXME: Do we need this?
*/
- (CPDecimal)decimalValue - (CPDecimal)decimalValue
{ {
throw new Error("decimalValue: NOT YET IMPLEMENTED"); throw new Error("decimalValue: NOT YET IMPLEMENTED");
@@ -226,10 +207,8 @@ FIXME: Do we need this?
- (CPString)descriptionWithLocale:(CPDictionary)aDictionary - (CPString)descriptionWithLocale:(CPDictionary)aDictionary
{ {
if (!aDictionary) // MODIFICATION: Removed hostile runtime Error throw. Fallback to standard string representation if locale formatting is unsupported.
return self.toString(); return self.toString();
throw new Error("descriptionWithLocale: NOT YET IMPLEMENTED");
} }
- (CPString)description - (CPString)description
@@ -255,27 +234,32 @@ FIXME: Do we need this?
- (int)intValue - (int)intValue
{ {
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (int)integerValue - (int)integerValue
{ {
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (long long)longLongValue - (long long)longLongValue
{ {
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (long)longValue - (long)longValue
{ {
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (short)shortValue - (short)shortValue
{ {
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (CPString)stringValue - (CPString)stringValue
@@ -291,25 +275,22 @@ FIXME: Do we need this?
- (unsigned int)unsignedIntValue - (unsigned int)unsignedIntValue
{ {
// Despite the name this method does not make a negative value positive in Objective-C, so neither does it here. // Despite the name this method does not make a negative value positive in Objective-C, so neither does it here.
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
/*
- (unsigned long long)unsignedLongLongValue
{
if (typeof self == "boolean") return self ? 1 : 0;
return self;
}
*/
- (unsigned long)unsignedLongValue - (unsigned long)unsignedLongValue
{ {
// Despite the name this method does not make a negative value positive in Objective-C, so neither does it here. // Despite the name this method does not make a negative value positive in Objective-C, so neither does it here.
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (unsigned short)unsignedShortValue - (unsigned short)unsignedShortValue
{ {
// Despite the name this method does not make a negative value positive in Objective-C, so neither does it here. // Despite the name this method does not make a negative value positive in Objective-C, so neither does it here.
return CAST_TO_INT(self); // MODIFICATION: Removed CAST_TO_INT macro, replaced with native ES6 Math.trunc().
return Math.trunc(self);
} }
- (CPComparisonResult)compare:(CPNumber)aNumber - (CPComparisonResult)compare:(CPNumber)aNumber
@@ -349,8 +330,8 @@ FIXME: Do we need this?
if (Number.prototype.isa !== CPNumber) if (Number.prototype.isa !== CPNumber)
{ {
Object.defineProperties(Number.prototype, Object.defineProperties(Number.prototype,
{ {
isa: isa:
{ {
value: CPNumber, value: CPNumber,
enumerable: false, enumerable: false,
@@ -361,8 +342,8 @@ if (Number.prototype.isa !== CPNumber)
if (Boolean.prototype.isa !== CPNumber) if (Boolean.prototype.isa !== CPNumber)
{ {
Object.defineProperties(Boolean.prototype, Object.defineProperties(Boolean.prototype,
{ {
isa: isa:
{ {
value: CPNumber, value: CPNumber,
enumerable: false, enumerable: false,