Skip to content

Commit cc64068

Browse files
committed
Implemented Math min and max as JsBuiltIns
1 parent eeba684 commit cc64068

13 files changed

+7853
-7050
lines changed

lib/Runtime/Base/JnDirectFields.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,8 @@ ENTRY(Array_every)
659659
ENTRY(Array_includes)
660660
ENTRY(Array_reduce)
661661
ENTRY(Object_fromEntries)
662+
ENTRY(Math_min)
663+
ENTRY(Math_max)
662664
ENTRY(FunctionKind)
663665

664666
// EngineInterfaceObject built-ins

lib/Runtime/Library/EngineInterfaceObjectBuiltIns.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ GlobalBuiltInConstructor(Symbol)
5757
GlobalMathBuiltIn(Abs)
5858
GlobalMathBuiltIn(Floor)
5959
GlobalMathBuiltIn(Max)
60+
GlobalMathBuiltIn(Min)
6061
GlobalMathBuiltIn(Pow)
6162

6263
GlobalBuiltIn(JavascriptObject, DefineProperty)
@@ -90,6 +91,8 @@ GlobalBuiltIn(JavascriptString, Substring)
9091
GlobalBuiltIn(JavascriptString, Repeat)
9192
GlobalBuiltIn(JavascriptString, IndexOf)
9293

94+
GlobalBuiltIn(JavascriptNumber, IsNaN)
95+
9396
GlobalBuiltIn(GlobalObject, IsFinite) // TODO(jahorto): consider switching to Number.isFinite
9497
GlobalBuiltIn(GlobalObject, IsNaN) // TODO(jahorto): consider switching to Number.isNaN
9598
GlobalBuiltIn(GlobalObject, Eval) // TODO(jahorto): consider deleting (currently used by WinRT Promises)

lib/Runtime/Library/InJavascript/Intl.js.bc.32b.h

Lines changed: 922 additions & 927 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.bc.64b.h

Lines changed: 930 additions & 930 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.32b.h

Lines changed: 940 additions & 940 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/InJavascript/Intl.js.nojit.bc.64b.h

Lines changed: 909 additions & 927 deletions
Large diffs are not rendered by default.

lib/Runtime/Library/JavascriptLibrary.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1876,7 +1876,6 @@ namespace Js
18761876

18771877
/* No inlining Array_Fill */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::fill, &JavascriptArray::EntryInfo::Fill, 1);
18781878
/* No inlining Array_CopyWithin */ library->AddFunctionToLibraryObject(arrayPrototype, PropertyIds::copyWithin, &JavascriptArray::EntryInfo::CopyWithin, 2);
1879-
18801879
DebugOnly(CheckRegisteredBuiltIns(builtinFuncs, scriptContext));
18811880

18821881
arrayPrototype->SetHasNoEnumerableProperties(true);
@@ -2860,6 +2859,18 @@ namespace Js
28602859

28612860
Field(JavascriptFunction*)* builtinFuncs = library->GetBuiltinFunctions();
28622861

2862+
#ifdef ENABLE_JS_BUILTINS
2863+
if (scriptContext->IsJsBuiltInEnabled())
2864+
{
2865+
library->EnsureBuiltInEngineIsReady();
2866+
}
2867+
else
2868+
#endif
2869+
{
2870+
builtinFuncs[BuiltinFunction::Math_Max] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::max, &Math::EntryInfo::Max, 2);
2871+
builtinFuncs[BuiltinFunction::Math_Min] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::min, &Math::EntryInfo::Min, 2);
2872+
}
2873+
28632874
builtinFuncs[BuiltinFunction::Math_Abs] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::abs, &Math::EntryInfo::Abs, 1);
28642875
builtinFuncs[BuiltinFunction::Math_Acos] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::acos, &Math::EntryInfo::Acos, 1);
28652876
builtinFuncs[BuiltinFunction::Math_Asin] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::asin, &Math::EntryInfo::Asin, 1);
@@ -2870,8 +2881,6 @@ namespace Js
28702881
builtinFuncs[BuiltinFunction::Math_Exp] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::exp, &Math::EntryInfo::Exp, 1);
28712882
builtinFuncs[BuiltinFunction::Math_Floor] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::floor, &Math::EntryInfo::Floor, 1);
28722883
builtinFuncs[BuiltinFunction::Math_Log] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::log, &Math::EntryInfo::Log, 1);
2873-
builtinFuncs[BuiltinFunction::Math_Max] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::max, &Math::EntryInfo::Max, 2);
2874-
builtinFuncs[BuiltinFunction::Math_Min] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::min, &Math::EntryInfo::Min, 2);
28752884
builtinFuncs[BuiltinFunction::Math_Pow] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::pow, &Math::EntryInfo::Pow, 2);
28762885
builtinFuncs[BuiltinFunction::Math_Random] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::random, &Math::EntryInfo::Random, 0);
28772886
builtinFuncs[BuiltinFunction::Math_Round] = library->AddFunctionToLibraryObject(mathObject, PropertyIds::round, &Math::EntryInfo::Round, 1);

lib/Runtime/Library/JsBuiltIn/JsBuiltIn.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
__chakraLibrary.raiseFunctionArgument_NeedFunction = platform.raiseFunctionArgument_NeedFunction;
3535
__chakraLibrary.functionBind = platform.builtInJavascriptFunctionEntryBind;
3636
__chakraLibrary.objectDefineProperty = _objectDefineProperty;
37+
__chakraLibrary.isNaN = platform.builtInGlobalObjectEntryIsNaN;
38+
__chakraLibrary.positiveInfinity = platform.POSITIVE_INFINITY;
39+
__chakraLibrary.negativeInfinity = platform.NEGATIVE_INFINITY;
3740

3841
_objectDefineProperty(__chakraLibrary.ArrayIterator.prototype, 'next',
3942
// Object's getter and setter can get overriden on the prototype, in that case while setting the value attributes, we will end up with TypeError
@@ -595,4 +598,76 @@
595598
}
596599
return o;
597600
});
601+
602+
platform.registerFunction(platform.FunctionKind.Math_min, function (value1, value2, ...values) {
603+
// #sec-math.min
604+
605+
// If no arguments are given, the result is positive infinity
606+
// If any value is NaN, the result is NaN.
607+
// The comparison of values to determine the smallest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
608+
if (arguments.length === 0 ) {
609+
return __chakraLibrary.positiveInfinity;
610+
}
611+
612+
if (__chakraLibrary.isNaN(value1)) return NaN;
613+
if (value1 === null) value1 = 0;
614+
615+
if (arguments.length === 1) {
616+
return value1;
617+
}
618+
619+
if (__chakraLibrary.isNaN(value2)) return NaN;
620+
if (value2 === null) value2 = 0;
621+
622+
if (arguments.length === 2) {
623+
if ((value1 < value2) || (value1 === value2 && value1 === 0 && 1/value1 < 1/value2)) return value1; // checks for -0 and +0
624+
else return value2;
625+
}
626+
627+
let min = value1;
628+
629+
for (let i = 1; i < arguments.length; i++) {
630+
if (arguments[i] === null) arguments[i] = 0;
631+
if (__chakraLibrary.isNaN(arguments[i])) return NaN;
632+
else if ((min > arguments[i]) || (min === arguments[i] && min === 0 && 1/min > 1/arguments[i])) min = arguments[i];
633+
}
634+
635+
return min;
636+
});
637+
638+
platform.registerFunction(platform.FunctionKind.Math_max, function (value1, value2, ...values) {
639+
// #sec-math.max
640+
641+
// If no arguments are given, the result is negative infinity
642+
// If any value is NaN, the result is NaN.
643+
// The comparison of values to determine the largest value is done using the Abstract Relational Comparison algorithm except that +0 is considered to be larger than -0.
644+
if (arguments.length === 0) {
645+
return __chakraLibrary.negativeInfinity;
646+
}
647+
648+
if (__chakraLibrary.isNaN(value1)) return NaN;
649+
if (value1 === null) value1 = 0;
650+
651+
if (arguments.length === 1) {
652+
return value1;
653+
}
654+
655+
if (__chakraLibrary.isNaN(value2)) return NaN;
656+
if (value2 === null) value2 = 0;
657+
658+
if (arguments.length === 2) {
659+
if ((value1 > value2) || (value1 === value2 && value1 === 0 && 1/value1 > 1/value2)) return value1;
660+
else return value2;
661+
}
662+
663+
let max = value1;
664+
665+
for (let i = 1; i < arguments.length; i++) {
666+
if (arguments[i] === null) arguments[i] = 0;
667+
if (__chakraLibrary.isNaN(arguments[i])) return NaN;
668+
else if ((max < arguments[i]) || (max === arguments[i] && max === 0 && 1/max < 1/arguments[i])) max = arguments[i];
669+
}
670+
671+
return max;
672+
});
598673
});

0 commit comments

Comments
 (0)