blob: cefef0a4b352802be2325323701b43cb04f99e45 [file] [log] [blame]
Steve Blocka7e24c12009-10-30 11:49:00 +00001// Copyright 2008 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6// * Redistributions of source code must retain the above copyright
7// notice, this list of conditions and the following disclaimer.
8// * Redistributions in binary form must reproduce the above
9// copyright notice, this list of conditions and the following
10// disclaimer in the documentation and/or other materials provided
11// with the distribution.
12// * Neither the name of Google Inc. nor the names of its
13// contributors may be used to endorse or promote products derived
14// from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29
Andrei Popescu402d9372010-02-26 13:31:12 +000030var RUN_WITH_ALL_ARGUMENT_ENTRIES = false;
31var kOnManyArgumentsRemove = 5;
32
Steve Blocka7e24c12009-10-30 11:49:00 +000033function makeArguments() {
34 var result = [ ];
35 result.push(17);
36 result.push(-31);
37 result.push(new Array(100));
38 result.push(new Array(100003));
39 result.push(Number.MIN_VALUE);
40 result.push("whoops");
41 result.push("x");
42 result.push({"x": 1, "y": 2});
43 var slowCaseObj = {"a": 3, "b": 4, "c": 5};
44 delete slowCaseObj.c;
45 result.push(slowCaseObj);
46 result.push(function () { return 8; });
47 return result;
48}
49
50var kArgObjects = makeArguments().length;
51
52function makeFunction(name, argc) {
53 var args = [];
54 for (var i = 0; i < argc; i++)
55 args.push("x" + i);
56 var argsStr = args.join(", ");
57 return new Function(args.join(", "), "return %" + name + "(" + argsStr + ");");
58}
59
Steve Block6ded16b2010-05-10 14:33:55 +010060function testArgumentCount(name, argc) {
Steve Blocka7e24c12009-10-30 11:49:00 +000061 for (var i = 0; i < 10; i++) {
Steve Block6ded16b2010-05-10 14:33:55 +010062 var func = null;
63 try {
64 func = makeFunction(name, i);
65 } catch (e) {
Leon Clarkeac952652010-07-15 11:15:24 +010066 if (e != "SyntaxError: Illegal access") throw e;
Steve Block6ded16b2010-05-10 14:33:55 +010067 }
68 if (func === null && i == argc) {
69 throw "unexpected exception";
70 }
Steve Blocka7e24c12009-10-30 11:49:00 +000071 var args = [ ];
72 for (var j = 0; j < i; j++)
73 args.push(0);
74 try {
75 func.apply(void 0, args);
76 } catch (e) {
77 // we don't care what happens as long as we don't crash
78 }
79 }
80}
81
82function testArgumentTypes(name, argc) {
83 var type = 0;
84 var hasMore = true;
85 var func = makeFunction(name, argc);
86 while (hasMore) {
87 var argPool = makeArguments();
Andrei Popescu402d9372010-02-26 13:31:12 +000088 // When we have 5 or more arguments we lower the amount of tests cases
89 // by randomly removing kOnManyArgumentsRemove entries
90 var numArguments = RUN_WITH_ALL_ARGUMENT_ENTRIES ?
91 kArgObjects : kArgObjects-kOnManyArgumentsRemove;
92 if (argc >= 5 && !RUN_WITH_ALL_ARGUMENT_ENTRIES) {
93 for (var i = 0; i < kOnManyArgumentsRemove; i++) {
94 var rand = Math.floor(Math.random() * (kArgObjects - i));
95 argPool.splice(rand,1);
96 }
97 }
Steve Blocka7e24c12009-10-30 11:49:00 +000098 var current = type;
99 var hasMore = false;
100 var argList = [ ];
101 for (var i = 0; i < argc; i++) {
Andrei Popescu402d9372010-02-26 13:31:12 +0000102 var index = current % numArguments;
103 current = (current / numArguments) << 0;
104 if (index != (numArguments - 1))
Steve Blocka7e24c12009-10-30 11:49:00 +0000105 hasMore = true;
106 argList.push(argPool[index]);
107 }
108 try {
109 func.apply(void 0, argList);
110 } catch (e) {
111 // we don't care what happens as long as we don't crash
112 }
113 type++;
114 }
115}
116
117var knownProblems = {
118 "Abort": true,
Leon Clarkee46be812010-01-19 14:06:41 +0000119
120 // Avoid calling the concat operation, because weird lengths
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100121 // may lead to out-of-memory. Ditto for StringBuilderJoin.
Leon Clarkee46be812010-01-19 14:06:41 +0000122 "StringBuilderConcat": true,
Ben Murdoche0cee9b2011-05-25 10:26:03 +0100123 "StringBuilderJoin": true,
Leon Clarkee46be812010-01-19 14:06:41 +0000124
Steve Blocka7e24c12009-10-30 11:49:00 +0000125 // These functions use pseudo-stack-pointers and are not robust
126 // to unexpected integer values.
127 "DebugEvaluate": true,
128
129 // These functions do nontrivial error checking in recursive calls,
130 // which means that we have to propagate errors back.
131 "SetFunctionBreakPoint": true,
132 "SetScriptBreakPoint": true,
Steve Blocka7e24c12009-10-30 11:49:00 +0000133 "PrepareStep": true,
134
135 // Too slow.
136 "DebugReferencedBy": true,
137
138 // Calling disable/enable access checks may interfere with the
139 // the rest of the tests.
140 "DisableAccessChecks": true,
141 "EnableAccessChecks": true,
Leon Clarkee46be812010-01-19 14:06:41 +0000142
Steve Blocka7e24c12009-10-30 11:49:00 +0000143 // These functions should not be callable as runtime functions.
144 "NewContext": true,
145 "NewArgumentsFast": true,
146 "PushContext": true,
147 "LazyCompile": true,
Ben Murdochb0fe1622011-05-05 13:52:32 +0100148 "LazyRecompile": true,
149 "NotifyDeoptimized": true,
150 "NotifyOSR": true,
Steve Blocka7e24c12009-10-30 11:49:00 +0000151 "CreateObjectLiteralBoilerplate": true,
152 "CloneLiteralBoilerplate": true,
153 "CloneShallowLiteralBoilerplate": true,
154 "CreateArrayLiteralBoilerplate": true,
155 "IS_VAR": true,
156 "ResolvePossiblyDirectEval": true,
157 "Log": true,
Steve Block3ce2e202009-11-05 08:53:23 +0000158 "DeclareGlobals": true,
Steve Blocka7e24c12009-10-30 11:49:00 +0000159
Steve Blockd0582a62009-12-15 09:54:21 +0000160 "PromoteScheduledException": true,
Steve Block6ded16b2010-05-10 14:33:55 +0100161 "DeleteHandleScopeExtensions": true,
162
163 // That can only be invoked on Array.prototype.
164 "FinishArrayPrototypeSetup": true,
165
166 "_SwapElements": true,
167
168 // Performance critical function which cannot afford type checks.
169 "_CallFunction": true,
170
171 // Tries to allocate based on argument, and (correctly) throws
172 // out-of-memory if the request is too large. In practice, the
173 // size will be the number of captures of a RegExp.
174 "RegExpConstructResult": true,
175 "_RegExpConstructResult": true,
176
177 // This function performs some checks compile time (it requires its first
178 // argument to be a compile time smi).
179 "_GetFromCache": true,
Iain Merrick75681382010-08-19 15:07:18 +0100180
181 // This function expects its first argument to be a non-smi.
Kristian Monsen80d68ea2010-09-08 11:05:35 +0100182 "_IsStringWrapperSafeForDefaultValueOf" : true,
183
184 // Only applicable to strings.
185 "_HasCachedArrayIndex": true,
186 "_GetCachedArrayIndex": true
Steve Blocka7e24c12009-10-30 11:49:00 +0000187};
188
189var currentlyUncallable = {
190 // We need to find a way to test this without breaking the system.
191 "SystemBreak": true
192};
193
194function testNatives() {
195 var allNatives = %ListNatives();
196 for (var i = 0; i < allNatives.length; i++) {
197 var nativeInfo = allNatives[i];
198 var name = nativeInfo[0];
199 if (name in knownProblems || name in currentlyUncallable)
200 continue;
201 print(name);
202 var argc = nativeInfo[1];
Steve Block6ded16b2010-05-10 14:33:55 +0100203 testArgumentCount(name, argc);
Steve Blocka7e24c12009-10-30 11:49:00 +0000204 testArgumentTypes(name, argc);
205 }
206}
207
208testNatives();