|
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-3.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 only covers cases where the Additive or Mulplicative expression |
|
43 is a Date. |
|
44 |
|
45 Author: christine@netscape.com |
|
46 Date: 12 november 1997 |
|
47 */ |
|
48 var SECTION = "11.6.1-3"; |
|
49 var VERSION = "ECMA_1"; |
|
50 startTest(); |
|
51 |
|
52 writeHeaderToLog( SECTION + " The Addition operator ( + )"); |
|
53 |
|
54 // tests for a boolean primitive and a boolean object, and |
|
55 // "MyValuelessObject", where the value is set in the object's |
|
56 // prototype, not the object itself. |
|
57 |
|
58 var DATE1 = new Date(); |
|
59 |
|
60 var MYOB1 = new MyObject( DATE1 ); |
|
61 var MYOB2 = new MyValuelessObject( DATE1 ); |
|
62 var MYOB3 = new MyProtolessObject( DATE1 ); |
|
63 var MYOB4 = new MyProtoValuelessObject( DATE1 ); |
|
64 |
|
65 new TestCase( SECTION, |
|
66 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + 'string'", |
|
67 DATE1.toString() + "string", |
|
68 MYOB2 + 'string' ); |
|
69 |
|
70 new TestCase( SECTION, |
|
71 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + new String('string')", |
|
72 DATE1.toString() + "string", |
|
73 MYOB2 + new String('string') ); |
|
74 /* |
|
75 new TestCase( SECTION, |
|
76 "MYOB3 = new MyProtolessObject(DATE1); MYOB3 + new Boolean(true)", |
|
77 DATE1.toString() + "true", |
|
78 MYOB3 + new Boolean(true) ); |
|
79 */ |
|
80 |
|
81 test(); |
|
82 |
|
83 function MyProtoValuelessObject() { |
|
84 this.valueOf = new Function ( "" ); |
|
85 this.__proto__ = null; |
|
86 } |
|
87 function MyProtolessObject( value ) { |
|
88 this.valueOf = new Function( "return this.value" ); |
|
89 this.__proto__ = null; |
|
90 this.value = value; |
|
91 } |
|
92 function MyValuelessObject(value) { |
|
93 this.__proto__ = new MyPrototypeObject(value); |
|
94 } |
|
95 function MyPrototypeObject(value) { |
|
96 this.valueOf = new Function( "return this.value;" ); |
|
97 this.toString = new Function( "return (this.value + '');" ); |
|
98 this.value = value; |
|
99 } |
|
100 function MyObject( value ) { |
|
101 this.valueOf = new Function( "return this.value" ); |
|
102 this.value = value; |
|
103 } |