|
1 | 1 | /*
|
2 |
| - * Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved. |
| 2 | + * Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved. |
3 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
4 | 4 | *
|
5 | 5 | * Licensed under the Universal Permissive License v 1.0 as shown at http://oss.oracle.com/licenses/upl.
|
@@ -155,6 +155,12 @@ const VectorOperators = Operators({
|
155 | 155 | }
|
156 | 156 | }
|
157 | 157 | return false;
|
| 158 | + }, |
| 159 | + "pos"(a) { |
| 160 | + return new Vector(a.contents.slice()); |
| 161 | + }, |
| 162 | + "neg"(a) { |
| 163 | + return new Vector(a.contents.map(e => -e)); |
158 | 164 | }
|
159 | 165 | }, {
|
160 | 166 | right: Number,
|
@@ -239,9 +245,10 @@ function STR(string) {
|
239 | 245 | }
|
240 | 246 |
|
241 | 247 |
|
242 |
| -// Basic tests on vectors: addition, subtraction, equality |
| 248 | +// Basic tests on vectors: addition, subtraction, unary negation, equality |
243 | 249 | assertEqual(V(1, 2, 3) + V(2, 3, 4), V(3, 5, 7));
|
244 | 250 | assertEqual(V(1, 2, 3) - V(2, 3, 4), V(-1, -1, -1));
|
| 251 | +assertTrue(-V(1, 2, 3) == V(-1, -2, -3)) |
245 | 252 |
|
246 | 253 | // Update operators should work too
|
247 | 254 | let A = V(1, 2, 3);
|
@@ -347,25 +354,31 @@ assertThrows(() => V(1, 2, 3) / V(2, 3, 4), TypeError);
|
347 | 354 | assertThrows(() => 1 + V(1, 2, 3), TypeError);
|
348 | 355 | assertThrows(() => S(1) + V(1, 2, 3), TypeError);
|
349 | 356 | assertThrows(() => V(1, 2, 3) + STR("a"), TypeError);
|
| 357 | +assertThrows(() => { let v = V(1, 2, 3); v++; }, TypeError); |
| 358 | +assertThrows(() => { let v = V(1, 2, 3); v--; }, TypeError); |
| 359 | + |
| 360 | + |
| 361 | +// Handle null, undefined and non-numeric primitives when dispatching operators that use ToOperand |
| 362 | +// (ToPrimitive) internally. |
| 363 | +// The cases below are not covered explicitly by the operator overloading proposal, but this |
| 364 | +// behavior implied by the proposal spec. |
| 365 | + |
| 366 | +function illegalValueThrowsTypeError(illegalValue) { |
| 367 | + assertThrows(() => S(1) + illegalValue, TypeError); |
| 368 | + // For equality checks, a missing operator is interpreted as a negative result. |
| 369 | + assertFalse(S(1) == illegalValue); |
| 370 | + assertTrue(S(1) != illegalValue); |
| 371 | + assertThrows(() => S(1) < illegalValue, TypeError); |
| 372 | + assertThrows(() => S(1) <= illegalValue, TypeError); |
| 373 | + assertThrows(() => S(1) > illegalValue, TypeError); |
| 374 | + assertThrows(() => S(1) >= illegalValue, TypeError); |
| 375 | +} |
350 | 376 |
|
351 |
| - |
352 |
| -// Handle null and undefined when dispatching operators that use ToOperand (ToPrimitive) internally |
353 |
| -// the cases below are not covered by the operator overloading proposal |
354 |
| -assertThrows(() => S(1) + undefined, TypeError); |
355 |
| -assertFalse(S(1) == undefined); |
356 |
| -assertTrue(S(1) != undefined); |
357 |
| -assertThrows(() => S(1) < undefined, TypeError); |
358 |
| -assertThrows(() => S(1) <= undefined, TypeError); |
359 |
| -assertThrows(() => S(1) > undefined, TypeError); |
360 |
| -assertThrows(() => S(1) >= undefined, TypeError); |
361 |
| - |
362 |
| -assertThrows(() => S(1) + null, TypeError); |
363 |
| -assertFalse(S(1) == null); |
364 |
| -assertTrue(S(1) != null); |
365 |
| -assertThrows(() => S(1) < null, TypeError); |
366 |
| -assertThrows(() => S(1) <= null, TypeError); |
367 |
| -assertThrows(() => S(1) > null, TypeError); |
368 |
| -assertThrows(() => S(1) >= null, TypeError); |
| 377 | +illegalValueThrowsTypeError(undefined); |
| 378 | +illegalValueThrowsTypeError(null); |
| 379 | +illegalValueThrowsTypeError(false); |
| 380 | +illegalValueThrowsTypeError(true); |
| 381 | +illegalValueThrowsTypeError(Symbol("foo")); |
369 | 382 |
|
370 | 383 |
|
371 | 384 | // The Operators function can reject junk input
|
|
0 commit comments