Skip to content

Commit

Permalink
Split JsBuiltins.js into 3 files by object type
Browse files Browse the repository at this point in the history
  • Loading branch information
rhuanjl committed Feb 1, 2021
1 parent 29bd881 commit a879856
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 142 deletions.
@@ -1,5 +1,6 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

Expand Down Expand Up @@ -27,15 +28,9 @@
__chakraLibrary.ArrayIterator.prototype = CreateObject(iteratorPrototype);
__chakraLibrary.raiseNeedObjectOfType = platform.raiseNeedObjectOfType;
__chakraLibrary.raiseThis_NullOrUndefined = platform.raiseThis_NullOrUndefined;
__chakraLibrary.raiseNeedObject = platform.raiseNeedObject;
__chakraLibrary.raiseNonObjectFromIterable = platform.raiseNonObjectFromIterable;
__chakraLibrary.raiseEmptyArrayAndInitValueNotPresent = platform.raiseEmptyArrayAndInitValueNotPresent;
__chakraLibrary.raiseLengthIsTooBig = platform.raiseLengthIsTooBig;
__chakraLibrary.raiseFunctionArgument_NeedFunction = platform.raiseFunctionArgument_NeedFunction;
__chakraLibrary.functionBind = platform.builtInJavascriptFunctionEntryBind;
__chakraLibrary.objectDefineProperty = _objectDefineProperty;
__chakraLibrary.positiveInfinity = platform.POSITIVE_INFINITY;
__chakraLibrary.negativeInfinity = platform.NEGATIVE_INFINITY;

_objectDefineProperty(__chakraLibrary.ArrayIterator.prototype, 'next',
// 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
Expand Down Expand Up @@ -92,31 +87,31 @@
return new __chakraLibrary.ArrayIterator(arrayObj, iterationKind);
});

platform.registerFunction(platform.FunctionKind.Array_keys, function () {
platform.registerFunction('keys', function () {
if (this === null || this === undefined) {
__chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.keys");
}
let o = __chakraLibrary.Object(this);
return __chakraLibrary.CreateArrayIterator(o, 0 /* ArrayIterationKind.Key*/);
});

platform.registerFunction(platform.FunctionKind.Array_values, function () {
platform.registerFunction('values', function () {
if (this === null || this === undefined) {
__chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.values");
}
let o = __chakraLibrary.Object(this);
return __chakraLibrary.CreateArrayIterator(o, 1 /* ArrayIterationKind.Value*/);
});

platform.registerFunction(platform.FunctionKind.Array_entries, function () {
platform.registerFunction('entries', function () {
if (this === null || this === undefined) {
__chakraLibrary.raiseThis_NullOrUndefined("Array.prototype.entries");
}
let o = __chakraLibrary.Object(this);
return __chakraLibrary.CreateArrayIterator(o, 2 /* ArrayIterationKind.KeyAndValue*/);
});

platform.registerFunction(platform.FunctionKind.Array_indexOf, function (searchElement, fromIndex = undefined) {
platform.registerFunction('indexOf', function (searchElement, fromIndex = undefined) {
// ECMAScript 2017 #sec-array.prototype.indexof

let {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.indexOf");
Expand Down Expand Up @@ -259,7 +254,7 @@
return valuesLength;
});

platform.registerFunction(platform.FunctionKind.Array_sort, function (compareFn) {
platform.registerFunction('sort', function (compareFn) {
//#sec-array.prototype.sort
if (compareFn !== undefined) {
if (typeof compareFn !== "function") {
Expand Down Expand Up @@ -319,7 +314,7 @@
return o;
});

platform.registerFunction(platform.FunctionKind.Array_filter, function (callbackfn, thisArg = undefined) {
platform.registerFunction('filter', function (callbackfn, thisArg = undefined) {
// ECMAScript 2017 #sec-array.prototype.filter

let {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.filter");
Expand Down Expand Up @@ -457,7 +452,7 @@
return targetIndex;
});

platform.registerFunction(platform.FunctionKind.Array_flat, function (depth = undefined) {
platform.registerFunction('flat', function (depth = undefined) {
//1. Let O be ? ToObject(this value).
//2. Let sourceLen be ? ToLength(? Get(O, "length")).
let {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.flat");
Expand All @@ -474,7 +469,7 @@
return A;
});

platform.registerFunction(platform.FunctionKind.Array_flatMap, function (mapperFunction, thisArg = undefined) {
platform.registerFunction('flatMap', function (mapperFunction, thisArg = undefined) {
//1. Let O be ? ToObject(this value).
//2. Let sourceLen be ? ToLength(? Get(O, "length")).
let {o, len} = __chakraLibrary.CheckArrayAndGetLen(this, "Array.prototype.flatMap");
Expand All @@ -492,7 +487,7 @@
return A;
});

platform.registerFunction(platform.FunctionKind.Array_forEach, function (callbackfn, thisArg = undefined) {
platform.registerFunction('forEach', function (callbackfn, thisArg = undefined) {
// ECMAScript 2017 #sec-array.prototype.foreach

//Let O be ? ToObject(this value).
Expand Down Expand Up @@ -526,7 +521,7 @@
return undefined;
});

platform.registerFunction(platform.FunctionKind.Array_some, function (callbackfn, thisArg = undefined) {
platform.registerFunction('some', function (callbackfn, thisArg = undefined) {
// ECMAScript 2017 #sec-array.prototype.some

//Let O be ? ToObject(this value).
Expand Down Expand Up @@ -563,7 +558,7 @@
return false;
});

platform.registerFunction(platform.FunctionKind.Array_every, function (callbackfn, thisArg = undefined) {
platform.registerFunction('every', function (callbackfn, thisArg = undefined) {
// ECMAScript 2017 #sec-array.prototype.every

//Let O be ? ToObject(this value).
Expand Down Expand Up @@ -600,7 +595,7 @@
return true;
});

platform.registerFunction(platform.FunctionKind.Array_includes, function (searchElement, fromIndex = undefined) {
platform.registerFunction('includes', function (searchElement, fromIndex = undefined) {
// ECMAScript 2017 #sec-array.prototype.includes

//Let O be ? ToObject(this value).
Expand Down Expand Up @@ -648,7 +643,7 @@
return false;
});

platform.registerFunction(platform.FunctionKind.Array_reduce, function (callbackfn, initialValue = undefined) {
platform.registerFunction('reduce', function (callbackfn, initialValue = undefined) {
// ECMAScript 2017 #sec-array.prototype.reduce

//Let O be ? ToObject(this value).
Expand Down Expand Up @@ -715,127 +710,4 @@
//Return accumulator.
return accumulator;
});

platform.registerFunction(platform.FunctionKind.Object_fromEntries, function (iterable) {
// #sec-object.fromentries
if (iterable === null || iterable === undefined) {
__chakraLibrary.raiseNeedObject("Object.fromEntries");
}

const o = {};
const propDescriptor = {
enumerable : true,
configurable : true,
writable : true,
value : undefined
};

let key;
for (const entry of iterable) {
if (typeof entry !== "object" || entry === null) {
__chakraLibrary.raiseNonObjectFromIterable("Object.fromEntries");
}

key = entry[0];
propDescriptor.value = entry[1];
__chakraLibrary.objectDefineProperty(o, key, propDescriptor);
}
return o;
});

platform.registerFunction(platform.FunctionKind.Math_min, function (value1, value2) {
// #sec-math.min

// If no arguments are given, the result is positive infinity
// If any value is NaN, the result is NaN.
// 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.
if (arguments.length === 0 ) {
return __chakraLibrary.positiveInfinity;
}

value1 = +value1;
if (value1 !== value1) {
return NaN;
}

if (arguments.length === 1) {
return value1;
}

if (arguments.length === 2) {
value2 = +value2;
if (value2 !== value2) {
return NaN;
}
if ((value1 < value2) || (value1 === value2 && value1 === 0 && 1/value1 < 1/value2)) { // checks for -0 and +0
return value1;
}
else {
return value2;
}
}

let min = value1;
let nextVal;

for (let i = 1; i < arguments.length; i++) {
nextVal = +arguments[i];
if (nextVal !== nextVal) {
return NaN;
}
else if ((min > nextVal) || (min === nextVal && min === 0 && 1/min > 1/nextVal)) { // checks for -0 and +0
min = nextVal;
}
}

return min;
});

platform.registerFunction(platform.FunctionKind.Math_max, function (value1, value2) {
// #sec-math.max

// If no arguments are given, the result is negative infinity
// If any value is NaN, the result is NaN.
// 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.
if (arguments.length === 0) {
return __chakraLibrary.negativeInfinity;
}

value1 = +value1;
if (value1 !== value1) {
return NaN;
}

if (arguments.length === 1) {
return value1;
}

if (arguments.length === 2) {
value2 = +value2;
if (value2 !== value2) {
return NaN;
}
if ((value1 > value2) || (value1 === value2 && value1 === 0 && 1/value1 > 1/value2)) { // checks for -0 and +0
return value1;
}
else {
return value2;
}
}

let max = value1;
let nextVal;

for (let i = 1; i < arguments.length; i++) {
nextVal = +arguments[i];
if (nextVal !== nextVal) {
return NaN;
}
else if ((max < nextVal) || (max === nextVal && max === 0 && 1/max < 1/nextVal)) { // checks for -0 and +0
max = nextVal;
}
}

return max;
});
});
110 changes: 110 additions & 0 deletions lib/Runtime/Library/InJavascript/Math_object.js
@@ -0,0 +1,110 @@
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Copyright (c) 2021 ChakraCore Project Contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------

"use strict";

(function (intrinsic) {
var platform = intrinsic.JsBuiltIn;

__chakraLibrary.positiveInfinity = platform.POSITIVE_INFINITY;
__chakraLibrary.negativeInfinity = platform.NEGATIVE_INFINITY;

platform.registerFunction('min', function (value1, value2) {
// #sec-math.min

// If no arguments are given, the result is positive infinity
// If any value is NaN, the result is NaN.
// 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.
if (arguments.length === 0 ) {
return __chakraLibrary.positiveInfinity;
}

value1 = +value1;
if (value1 !== value1) {
return NaN;
}

if (arguments.length === 1) {
return value1;
}

if (arguments.length === 2) {
value2 = +value2;
if (value2 !== value2) {
return NaN;
}
if ((value1 < value2) || (value1 === value2 && value1 === 0 && 1/value1 < 1/value2)) { // checks for -0 and +0
return value1;
}
else {
return value2;
}
}

let min = value1;
let nextVal;

for (let i = 1; i < arguments.length; i++) {
nextVal = +arguments[i];
if (nextVal !== nextVal) {
return NaN;
}
else if ((min > nextVal) || (min === nextVal && min === 0 && 1/min > 1/nextVal)) { // checks for -0 and +0
min = nextVal;
}
}

return min;
});

platform.registerFunction('max', function (value1, value2) {
// #sec-math.max

// If no arguments are given, the result is negative infinity
// If any value is NaN, the result is NaN.
// 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.
if (arguments.length === 0) {
return __chakraLibrary.negativeInfinity;
}

value1 = +value1;
if (value1 !== value1) {
return NaN;
}

if (arguments.length === 1) {
return value1;
}

if (arguments.length === 2) {
value2 = +value2;
if (value2 !== value2) {
return NaN;
}
if ((value1 > value2) || (value1 === value2 && value1 === 0 && 1/value1 > 1/value2)) { // checks for -0 and +0
return value1;
}
else {
return value2;
}
}

let max = value1;
let nextVal;

for (let i = 1; i < arguments.length; i++) {
nextVal = +arguments[i];
if (nextVal !== nextVal) {
return NaN;
}
else if ((max < nextVal) || (max === nextVal && max === 0 && 1/max < 1/nextVal)) { // checks for -0 and +0
max = nextVal;
}
}

return max;
});
});

0 comments on commit a879856

Please sign in to comment.