|
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
|
2 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
3 * License, v. 2.0. If a copy of the MPL was not distributed with this |
|
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
5 |
|
6 |
|
7 /** |
|
8 File Name: 11.6.1-1.js |
|
9 ECMA Section: 11.6.1 The addition operator ( + ) |
|
10 Description: |
|
11 |
|
12 The addition operator either performs string concatenation or numeric |
|
13 addition. |
|
14 |
|
15 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression |
|
16 is evaluated as follows: |
|
17 |
|
18 1. Evaluate AdditiveExpression. |
|
19 2. Call GetValue(Result(1)). |
|
20 3. Evaluate MultiplicativeExpression. |
|
21 4. Call GetValue(Result(3)). |
|
22 5. Call ToPrimitive(Result(2)). |
|
23 6. Call ToPrimitive(Result(4)). |
|
24 7. If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12. |
|
25 (Note that this step differs from step 3 in the algorithm for comparison |
|
26 for the relational operators in using or instead of and.) |
|
27 8. Call ToNumber(Result(5)). |
|
28 9. Call ToNumber(Result(6)). |
|
29 10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3). |
|
30 11. Return Result(10). |
|
31 12. Call ToString(Result(5)). |
|
32 13. Call ToString(Result(6)). |
|
33 14. Concatenate Result(12) followed by Result(13). |
|
34 15. Return Result(14). |
|
35 |
|
36 Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6. |
|
37 All native ECMAScript objects except Date objects handle the absence of a |
|
38 hint as if the hint Number were given; Date objects handle the absence of a |
|
39 hint as if the hint String were given. Host objects may handle the absence |
|
40 of a hint in some other manner. |
|
41 |
|
42 This test does not cover cases where the Additive or Mulplicative expression |
|
43 ToPrimitive is string. |
|
44 |
|
45 Author: christine@netscape.com |
|
46 Date: 12 november 1997 |
|
47 */ |
|
48 var SECTION = "11.6.1-1"; |
|
49 var VERSION = "ECMA_1"; |
|
50 startTest(); |
|
51 |
|
52 writeHeaderToLog( SECTION + " The Addition operator ( + )"); |
|
53 |
|
54 // tests for "MyValuelessObject", where the value is |
|
55 // set in the object's prototype, not the object itself. |
|
56 |
|
57 new TestCase( SECTION, |
|
58 "var EXP_1 = new MyValuelessObject(true); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2", |
|
59 1, |
|
60 eval("var EXP_1 = new MyValuelessObject(true); var EXP_2 = new MyValuelessObject(false); EXP_1 + EXP_2") ); |
|
61 |
|
62 new TestCase( SECTION, |
|
63 "var EXP_1 = new MyValuelessObject(new Boolean(true)); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2", |
|
64 "truefalse", |
|
65 eval("var EXP_1 = new MyValuelessObject(new Boolean(true)); var EXP_2 = new MyValuelessObject(new Boolean(false)); EXP_1 + EXP_2") ); |
|
66 |
|
67 // tests for "MyValuelessObject", where the value is |
|
68 // set in the object's prototype, not the object itself. |
|
69 |
|
70 |
|
71 new TestCase( SECTION, |
|
72 "var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject(-1); EXP_1 + EXP_2", |
|
73 99, |
|
74 eval("var EXP_1 = new MyValuelessObject(100); var EXP_2 = new MyValuelessObject(-1); EXP_1 + EXP_2") ); |
|
75 |
|
76 new TestCase( SECTION, |
|
77 "var EXP_1 = new MyValuelessObject(new Number(100)); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2", |
|
78 "100-1", |
|
79 eval("var EXP_1 = new MyValuelessObject(new Number(100)); var EXP_2 = new MyValuelessObject(new Number(-1)); EXP_1 + EXP_2") ); |
|
80 |
|
81 new TestCase( SECTION, |
|
82 "var EXP_1 = new MyValuelessObject( new MyValuelessObject( new Boolean(true) ) ); EXP_1 + EXP_1", |
|
83 "truetrue", |
|
84 eval("var EXP_1 = new MyValuelessObject( new MyValuelessObject( new Boolean(true) ) ); EXP_1 + EXP_1") ); |
|
85 |
|
86 test(); |
|
87 |
|
88 function MyProtoValuelessObject() { |
|
89 this.valueOf = new Function ( "" ); |
|
90 this.__proto__ = null; |
|
91 } |
|
92 |
|
93 function MyProtolessObject( value ) { |
|
94 this.valueOf = new Function( "return this.value" ); |
|
95 this.__proto__ = null; |
|
96 this.value = value; |
|
97 } |
|
98 |
|
99 function MyValuelessObject(value) { |
|
100 this.__proto__ = new MyPrototypeObject(value); |
|
101 } |
|
102 function MyPrototypeObject(value) { |
|
103 this.valueOf = new Function( "return this.value;" ); |
|
104 this.toString = new Function( "return (this.value + '');" ); |
|
105 this.value = value; |
|
106 } |
|
107 |
|
108 function MyObject( value ) { |
|
109 this.valueOf = new Function( "return this.value" ); |
|
110 this.value = value; |
|
111 } |