source: imaps-frontend/node_modules/es-abstract/2024/ApplyStringOrNumericBinaryOperator.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 3.1 KB
RevLine 
[d565449]1'use strict';
2
3var $TypeError = require('es-errors/type');
4
5var HasOwnProperty = require('./HasOwnProperty');
6var ToNumeric = require('./ToNumeric');
7var ToPrimitive = require('./ToPrimitive');
8var ToString = require('./ToString');
9var Type = require('./Type');
10
11var NumberAdd = require('./Number/add');
12var NumberBitwiseAND = require('./Number/bitwiseAND');
13var NumberBitwiseOR = require('./Number/bitwiseOR');
14var NumberBitwiseXOR = require('./Number/bitwiseXOR');
15var NumberDivide = require('./Number/divide');
16var NumberExponentiate = require('./Number/exponentiate');
17var NumberLeftShift = require('./Number/leftShift');
18var NumberMultiply = require('./Number/multiply');
19var NumberRemainder = require('./Number/remainder');
20var NumberSignedRightShift = require('./Number/signedRightShift');
21var NumberSubtract = require('./Number/subtract');
22var NumberUnsignedRightShift = require('./Number/unsignedRightShift');
23var BigIntAdd = require('./BigInt/add');
24var BigIntBitwiseAND = require('./BigInt/bitwiseAND');
25var BigIntBitwiseOR = require('./BigInt/bitwiseOR');
26var BigIntBitwiseXOR = require('./BigInt/bitwiseXOR');
27var BigIntDivide = require('./BigInt/divide');
28var BigIntExponentiate = require('./BigInt/exponentiate');
29var BigIntLeftShift = require('./BigInt/leftShift');
30var BigIntMultiply = require('./BigInt/multiply');
31var BigIntRemainder = require('./BigInt/remainder');
32var BigIntSignedRightShift = require('./BigInt/signedRightShift');
33var BigIntSubtract = require('./BigInt/subtract');
34var BigIntUnsignedRightShift = require('./BigInt/unsignedRightShift');
35
36// https://262.ecma-international.org/12.0/#sec-applystringornumericbinaryoperator
37
38// https://262.ecma-international.org/12.0/#step-applystringornumericbinaryoperator-operations-table
39var table = {
40 '**': [NumberExponentiate, BigIntExponentiate],
41 '*': [NumberMultiply, BigIntMultiply],
42 '/': [NumberDivide, BigIntDivide],
43 '%': [NumberRemainder, BigIntRemainder],
44 '+': [NumberAdd, BigIntAdd],
45 '-': [NumberSubtract, BigIntSubtract],
46 '<<': [NumberLeftShift, BigIntLeftShift],
47 '>>': [NumberSignedRightShift, BigIntSignedRightShift],
48 '>>>': [NumberUnsignedRightShift, BigIntUnsignedRightShift],
49 '&': [NumberBitwiseAND, BigIntBitwiseAND],
50 '^': [NumberBitwiseXOR, BigIntBitwiseXOR],
51 '|': [NumberBitwiseOR, BigIntBitwiseOR]
52};
53
54module.exports = function ApplyStringOrNumericBinaryOperator(lval, opText, rval) {
55 if (typeof opText !== 'string' || !HasOwnProperty(table, opText)) {
56 throw new $TypeError('Assertion failed: `opText` must be a valid operation string');
57 }
58 if (opText === '+') {
59 var lprim = ToPrimitive(lval);
60 var rprim = ToPrimitive(rval);
61 if (typeof lprim === 'string' || typeof rprim === 'string') {
62 var lstr = ToString(lprim);
63 var rstr = ToString(rprim);
64 return lstr + rstr;
65 }
66 /* eslint no-param-reassign: 1 */
67 lval = lprim;
68 rval = rprim;
69 }
70 var lnum = ToNumeric(lval);
71 var rnum = ToNumeric(rval);
72 var T = Type(lnum);
73 if (T !== Type(rnum)) {
74 throw new $TypeError('types of ' + lnum + ' and ' + rnum + ' differ');
75 }
76 var Operation = table[opText][T === 'BigInt' ? 1 : 0];
77 return Operation(lnum, rnum);
78};
Note: See TracBrowser for help on using the repository browser.