1 | 'use strict';
|
---|
2 |
|
---|
3 | var $TypeError = require('es-errors/type');
|
---|
4 |
|
---|
5 | var HasOwnProperty = require('./HasOwnProperty');
|
---|
6 | var ToNumeric = require('./ToNumeric');
|
---|
7 | var ToPrimitive = require('./ToPrimitive');
|
---|
8 | var ToString = require('./ToString');
|
---|
9 | var Type = require('./Type');
|
---|
10 |
|
---|
11 | var NumberAdd = require('./Number/add');
|
---|
12 | var NumberBitwiseAND = require('./Number/bitwiseAND');
|
---|
13 | var NumberBitwiseOR = require('./Number/bitwiseOR');
|
---|
14 | var NumberBitwiseXOR = require('./Number/bitwiseXOR');
|
---|
15 | var NumberDivide = require('./Number/divide');
|
---|
16 | var NumberExponentiate = require('./Number/exponentiate');
|
---|
17 | var NumberLeftShift = require('./Number/leftShift');
|
---|
18 | var NumberMultiply = require('./Number/multiply');
|
---|
19 | var NumberRemainder = require('./Number/remainder');
|
---|
20 | var NumberSignedRightShift = require('./Number/signedRightShift');
|
---|
21 | var NumberSubtract = require('./Number/subtract');
|
---|
22 | var NumberUnsignedRightShift = require('./Number/unsignedRightShift');
|
---|
23 | var BigIntAdd = require('./BigInt/add');
|
---|
24 | var BigIntBitwiseAND = require('./BigInt/bitwiseAND');
|
---|
25 | var BigIntBitwiseOR = require('./BigInt/bitwiseOR');
|
---|
26 | var BigIntBitwiseXOR = require('./BigInt/bitwiseXOR');
|
---|
27 | var BigIntDivide = require('./BigInt/divide');
|
---|
28 | var BigIntExponentiate = require('./BigInt/exponentiate');
|
---|
29 | var BigIntLeftShift = require('./BigInt/leftShift');
|
---|
30 | var BigIntMultiply = require('./BigInt/multiply');
|
---|
31 | var BigIntRemainder = require('./BigInt/remainder');
|
---|
32 | var BigIntSignedRightShift = require('./BigInt/signedRightShift');
|
---|
33 | var BigIntSubtract = require('./BigInt/subtract');
|
---|
34 | var 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
|
---|
39 | var 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 |
|
---|
54 | module.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 | };
|
---|