1 // Copyright 2011 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
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.
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.
28 // Flags: --allow-natives-syntax
30 var RUN_WITH_ALL_ARGUMENT_ENTRIES = false;
31 var kOnManyArgumentsRemove = 5;
33 function makeArguments() {
37 result.push(new Array(100));
38 result.push(new Array(100003));
39 result.push(Number.MIN_VALUE);
40 result.push("whoops");
42 result.push({"x": 1, "y": 2});
43 var slowCaseObj = {"a": 3, "b": 4, "c": 5};
45 result.push(slowCaseObj);
46 result.push(function () { return 8; });
50 var kArgObjects = makeArguments().length;
52 function makeFunction(name, argc) {
54 for (var i = 0; i < argc; i++)
56 var argsStr = args.join(", ");
57 return new Function(args.join(", "), "return %" + name + "(" + argsStr + ");");
60 function testArgumentCount(name, argc) {
61 for (var i = 0; i < 10; i++) {
64 func = makeFunction(name, i);
66 if (e != "SyntaxError: Illegal access") throw e;
68 if (func === null && i == argc) {
69 throw "unexpected exception";
72 for (var j = 0; j < i; j++)
75 func.apply(void 0, args);
77 // we don't care what happens as long as we don't crash
82 function testArgumentTypes(name, argc) {
85 var func = makeFunction(name, argc);
87 var argPool = makeArguments();
88 // 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);
101 for (var i = 0; i < argc; i++) {
102 var index = current % numArguments;
103 current = (current / numArguments) << 0;
104 if (index != (numArguments - 1))
106 argList.push(argPool[index]);
109 func.apply(void 0, argList);
111 // we don't care what happens as long as we don't crash
117 var knownProblems = {
120 // Avoid calling the concat operation, because weird lengths
121 // may lead to out-of-memory. Ditto for StringBuilderJoin.
122 "StringBuilderConcat": true,
123 "StringBuilderJoin": true,
125 // These functions use pseudo-stack-pointers and are not robust
126 // to unexpected integer values.
127 "DebugEvaluate": true,
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,
136 "DebugReferencedBy": true,
138 // Calling disable/enable access checks may interfere with the
139 // the rest of the tests.
140 "DisableAccessChecks": true,
141 "EnableAccessChecks": true,
143 // IS_VAR is special.
146 // Vararg with minimum number > 0.
148 "SetAllocationTimeout": true,
150 // Requires integer arguments to be non-negative.
153 // That can only be invoked on Array.prototype.
154 "FinishArrayPrototypeSetup": true,
156 "_SwapElements": true,
158 // Performance critical functions which cannot afford type checks.
159 "_IsNativeOrStrictMode": true,
160 "_CallFunction": true,
162 // Tries to allocate based on argument, and (correctly) throws
163 // out-of-memory if the request is too large. In practice, the
164 // size will be the number of captures of a RegExp.
165 "RegExpConstructResult": true,
166 "_RegExpConstructResult": true,
168 // This functions perform some checks compile time (they require one of their
169 // arguments to be a compile time smi).
171 "_GetFromCache": true,
173 // This function expects its first argument to be a non-smi.
174 "_IsStringWrapperSafeForDefaultValueOf" : true,
176 // Only applicable to strings.
177 "_HasCachedArrayIndex": true,
178 "_GetCachedArrayIndex": true,
179 "_OneByteSeqStringSetChar": true,
180 "_TwoByteSeqStringSetChar": true,
182 // Only applicable to TypedArrays.
183 "_TypedArrayInitialize": true,
185 // Only applicable to loading mutable doubles.
186 "LoadMutableDouble": true,
188 // Only applicable to generators.
189 "_GeneratorNext": true,
190 "_GeneratorThrow": true,
192 // Only applicable to DataViews.
193 "_DataViewInitialize": true,
196 var currentlyUncallable = {
197 // We need to find a way to test this without breaking the system.
199 // Inserts an int3/stop instruction when run with --always-opt.
200 "_DebugBreakInOptimizedCode": true
203 function testNatives() {
204 var allNatives = %ListNatives();
206 var stop = (allNatives.length >> 2);
207 for (var i = start; i < stop; i++) {
208 var nativeInfo = allNatives[i];
209 var name = nativeInfo[0];
210 if (name in knownProblems || name in currentlyUncallable)
213 var argc = nativeInfo[1];
214 testArgumentCount(name, argc);
215 testArgumentTypes(name, argc);