1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
25 * Alternatively, the contents of this file may be used under the terms of
26 * either the GNU General Public License Version 2 or later (the "GPL"), or
27 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
41 File Name: 11.6.1-3.js
42 ECMA Section: 11.6.1 The addition operator ( + )
45 The addition operator either performs string concatenation or numeric
48 The production AdditiveExpression : AdditiveExpression + MultiplicativeExpression
49 is evaluated as follows:
51 1. Evaluate AdditiveExpression.
52 2. Call GetValue(Result(1)).
53 3. Evaluate MultiplicativeExpression.
54 4. Call GetValue(Result(3)).
55 5. Call ToPrimitive(Result(2)).
56 6. Call ToPrimitive(Result(4)).
57 7. If Type(Result(5)) is String or Type(Result(6)) is String, go to step 12.
58 (Note that this step differs from step 3 in the algorithm for comparison
59 for the relational operators in using or instead of and.)
60 8. Call ToNumber(Result(5)).
61 9. Call ToNumber(Result(6)).
62 10. Apply the addition operation to Result(8) and Result(9). See the discussion below (11.6.3).
63 11. Return Result(10).
64 12. Call ToString(Result(5)).
65 13. Call ToString(Result(6)).
66 14. Concatenate Result(12) followed by Result(13).
67 15. Return Result(14).
69 Note that no hint is provided in the calls to ToPrimitive in steps 5 and 6.
70 All native ECMAScript objects except Date objects handle the absence of a
71 hint as if the hint Number were given; Date objects handle the absence of a
72 hint as if the hint String were given. Host objects may handle the absence
73 of a hint in some other manner.
75 This test does only covers cases where the Additive or Mulplicative expression
78 Author: christine@netscape.com
79 Date: 12 november 1997
81 var SECTION = "11.6.1-3";
82 var VERSION = "ECMA_1";
85 writeHeaderToLog( SECTION + " The Addition operator ( + )");
87 // tests for a boolean primitive and a boolean object, and
88 // "MyValuelessObject", where the value is set in the object's
89 // prototype, not the object itself.
91 var DATE1 = new Date();
93 var MYOB1 = new MyObject( DATE1 );
94 var MYOB2 = new MyValuelessObject( DATE1 );
95 var MYOB3 = new MyProtolessObject( DATE1 );
96 var MYOB4 = new MyProtoValuelessObject( DATE1 );
98 new TestCase( SECTION,
99 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + 'string'",
100 DATE1.toString() + "string",
103 new TestCase( SECTION,
104 "MYOB2 = new MyValuelessObject(DATE1); MYOB3 + new String('string')",
105 DATE1.toString() + "string",
106 MYOB2 + new String('string') );
108 new TestCase( SECTION,
109 "MYOB3 = new MyProtolessObject(DATE1); MYOB3 + new Boolean(true)",
110 DATE1.toString() + "true",
111 MYOB3 + new Boolean(true) );
116 function MyProtoValuelessObject() {
117 this.valueOf = new Function ( "" );
118 this.__proto__ = null;
120 function MyProtolessObject( value ) {
121 this.valueOf = new Function( "return this.value" );
122 this.__proto__ = null;
125 function MyValuelessObject(value) {
126 this.__proto__ = new MyPrototypeObject(value);
128 function MyPrototypeObject(value) {
129 this.valueOf = new Function( "return this.value;" );
130 this.toString = new Function( "return (this.value + '');" );
133 function MyObject( value ) {
134 this.valueOf = new Function( "return this.value" );