erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 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 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 28 | #include <climits> |
| 29 | #include <csignal> |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 30 | #include <string> |
| 31 | #include <map> |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 32 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 33 | #include "v8.h" |
| 34 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 35 | #if V8_OS_POSIX |
| 36 | #include <unistd.h> // NOLINT |
| 37 | #endif |
| 38 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 39 | #include "api.h" |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 40 | #include "arguments.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 41 | #include "cctest.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 42 | #include "compilation-cache.h" |
| 43 | #include "cpu-profiler.h" |
| 44 | #include "execution.h" |
| 45 | #include "isolate.h" |
| 46 | #include "objects.h" |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 47 | #include "parser.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 48 | #include "platform.h" |
| 49 | #include "snapshot.h" |
erik.corry@gmail.com | 4a6c327 | 2010-11-18 12:04:40 +0000 | [diff] [blame] | 50 | #include "unicode-inl.h" |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 51 | #include "utils.h" |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 52 | #include "vm-state.h" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 53 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 54 | static const bool kLogThreading = false; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 55 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 56 | using ::v8::Boolean; |
| 57 | using ::v8::BooleanObject; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 58 | using ::v8::Context; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 59 | using ::v8::Extension; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 60 | using ::v8::Function; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 61 | using ::v8::FunctionTemplate; |
| 62 | using ::v8::Handle; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 63 | using ::v8::HandleScope; |
| 64 | using ::v8::Local; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 65 | using ::v8::Message; |
| 66 | using ::v8::MessageCallback; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 67 | using ::v8::Object; |
| 68 | using ::v8::ObjectTemplate; |
| 69 | using ::v8::Persistent; |
| 70 | using ::v8::Script; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 71 | using ::v8::StackTrace; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 72 | using ::v8::String; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 73 | using ::v8::TryCatch; |
| 74 | using ::v8::Undefined; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 75 | using ::v8::UniqueId; |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 76 | using ::v8::V8; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 77 | using ::v8::Value; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 78 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 79 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 80 | #define THREADED_PROFILED_TEST(Name) \ |
| 81 | static void Test##Name(); \ |
| 82 | TEST(Name##WithProfiler) { \ |
| 83 | RunWithProfiler(&Test##Name); \ |
| 84 | } \ |
| 85 | THREADED_TEST(Name) |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 86 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 87 | |
| 88 | void RunWithProfiler(void (*test)()) { |
| 89 | LocalContext env; |
| 90 | v8::HandleScope scope(env->GetIsolate()); |
| 91 | v8::Local<v8::String> profile_name = v8::String::New("my_profile1"); |
| 92 | v8::CpuProfiler* cpu_profiler = env->GetIsolate()->GetCpuProfiler(); |
| 93 | |
| 94 | cpu_profiler->StartCpuProfiling(profile_name); |
| 95 | (*test)(); |
| 96 | cpu_profiler->DeleteAllCpuProfiles(); |
| 97 | } |
| 98 | |
| 99 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 100 | static void ExpectString(const char* code, const char* expected) { |
| 101 | Local<Value> result = CompileRun(code); |
| 102 | CHECK(result->IsString()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 103 | String::Utf8Value utf8(result); |
| 104 | CHECK_EQ(expected, *utf8); |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 105 | } |
| 106 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 107 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 108 | static void ExpectInt32(const char* code, int expected) { |
| 109 | Local<Value> result = CompileRun(code); |
| 110 | CHECK(result->IsInt32()); |
| 111 | CHECK_EQ(expected, result->Int32Value()); |
| 112 | } |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 113 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 114 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 115 | static void ExpectBoolean(const char* code, bool expected) { |
| 116 | Local<Value> result = CompileRun(code); |
| 117 | CHECK(result->IsBoolean()); |
| 118 | CHECK_EQ(expected, result->BooleanValue()); |
| 119 | } |
| 120 | |
| 121 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 122 | static void ExpectTrue(const char* code) { |
| 123 | ExpectBoolean(code, true); |
| 124 | } |
| 125 | |
| 126 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 127 | static void ExpectFalse(const char* code) { |
| 128 | ExpectBoolean(code, false); |
| 129 | } |
| 130 | |
| 131 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 132 | static void ExpectObject(const char* code, Local<Value> expected) { |
| 133 | Local<Value> result = CompileRun(code); |
| 134 | CHECK(result->Equals(expected)); |
| 135 | } |
| 136 | |
| 137 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 138 | static void ExpectUndefined(const char* code) { |
| 139 | Local<Value> result = CompileRun(code); |
| 140 | CHECK(result->IsUndefined()); |
| 141 | } |
| 142 | |
| 143 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 144 | static int signature_callback_count; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 145 | static Local<Value> signature_expected_receiver; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 146 | static void IncrementingSignatureCallback( |
| 147 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 148 | ApiTestFuzzer::Fuzz(); |
| 149 | signature_callback_count++; |
jkummerow@chromium.org | 2c9426b | 2013-09-05 16:31:13 +0000 | [diff] [blame] | 150 | CHECK_EQ(signature_expected_receiver, args.Holder()); |
| 151 | CHECK_EQ(signature_expected_receiver, args.This()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 152 | v8::Handle<v8::Array> result = v8::Array::New(args.Length()); |
| 153 | for (int i = 0; i < args.Length(); i++) |
| 154 | result->Set(v8::Integer::New(i), args[i]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 155 | args.GetReturnValue().Set(result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 159 | static void SignatureCallback( |
| 160 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 161 | ApiTestFuzzer::Fuzz(); |
| 162 | v8::Handle<v8::Array> result = v8::Array::New(args.Length()); |
| 163 | for (int i = 0; i < args.Length(); i++) { |
| 164 | result->Set(v8::Integer::New(i), args[i]); |
| 165 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 166 | args.GetReturnValue().Set(result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 170 | // Tests that call v8::V8::Dispose() cannot be threaded. |
| 171 | TEST(InitializeAndDisposeOnce) { |
| 172 | CHECK(v8::V8::Initialize()); |
| 173 | CHECK(v8::V8::Dispose()); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | // Tests that call v8::V8::Dispose() cannot be threaded. |
| 178 | TEST(InitializeAndDisposeMultiple) { |
| 179 | for (int i = 0; i < 3; ++i) CHECK(v8::V8::Dispose()); |
| 180 | for (int i = 0; i < 3; ++i) CHECK(v8::V8::Initialize()); |
| 181 | for (int i = 0; i < 3; ++i) CHECK(v8::V8::Dispose()); |
| 182 | // TODO(mstarzinger): This should fail gracefully instead of asserting. |
| 183 | // for (int i = 0; i < 3; ++i) CHECK(v8::V8::Initialize()); |
| 184 | for (int i = 0; i < 3; ++i) CHECK(v8::V8::Dispose()); |
| 185 | } |
| 186 | |
| 187 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 188 | THREADED_TEST(Handles) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 189 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 190 | Local<Context> local_env; |
| 191 | { |
| 192 | LocalContext env; |
| 193 | local_env = env.local(); |
| 194 | } |
| 195 | |
| 196 | // Local context should still be live. |
| 197 | CHECK(!local_env.IsEmpty()); |
| 198 | local_env->Enter(); |
| 199 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 200 | v8::Handle<v8::Primitive> undef = v8::Undefined(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 201 | CHECK(!undef.IsEmpty()); |
| 202 | CHECK(undef->IsUndefined()); |
| 203 | |
| 204 | const char* c_source = "1 + 2 + 3"; |
| 205 | Local<String> source = String::New(c_source); |
| 206 | Local<Script> script = Script::Compile(source); |
| 207 | CHECK_EQ(6, script->Run()->Int32Value()); |
| 208 | |
| 209 | local_env->Exit(); |
| 210 | } |
| 211 | |
| 212 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 213 | THREADED_TEST(IsolateOfContext) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 214 | v8::HandleScope scope(CcTest::isolate()); |
| 215 | v8::Handle<Context> env = Context::New(CcTest::isolate()); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 216 | |
| 217 | CHECK(!env->InContext()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 218 | CHECK(env->GetIsolate() == CcTest::isolate()); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 219 | env->Enter(); |
| 220 | CHECK(env->InContext()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 221 | CHECK(env->GetIsolate() == CcTest::isolate()); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 222 | env->Exit(); |
| 223 | CHECK(!env->InContext()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 224 | CHECK(env->GetIsolate() == CcTest::isolate()); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 228 | static void TestSignature(const char* loop_js, Local<Value> receiver) { |
| 229 | i::ScopedVector<char> source(200); |
| 230 | i::OS::SNPrintF(source, |
| 231 | "for (var i = 0; i < 10; i++) {" |
| 232 | " %s" |
| 233 | "}", |
| 234 | loop_js); |
| 235 | signature_callback_count = 0; |
| 236 | signature_expected_receiver = receiver; |
| 237 | bool expected_to_throw = receiver.IsEmpty(); |
| 238 | v8::TryCatch try_catch; |
| 239 | CompileRun(source.start()); |
| 240 | CHECK_EQ(expected_to_throw, try_catch.HasCaught()); |
| 241 | if (!expected_to_throw) { |
| 242 | CHECK_EQ(10, signature_callback_count); |
| 243 | } else { |
| 244 | CHECK_EQ(v8_str("TypeError: Illegal invocation"), |
| 245 | try_catch.Exception()->ToString()); |
| 246 | } |
| 247 | } |
| 248 | |
| 249 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 250 | THREADED_TEST(ReceiverSignature) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 251 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 252 | v8::HandleScope scope(env->GetIsolate()); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 253 | // Setup templates. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 254 | v8::Handle<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(); |
| 255 | v8::Handle<v8::Signature> sig = v8::Signature::New(fun); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 256 | v8::Handle<v8::FunctionTemplate> callback_sig = |
| 257 | v8::FunctionTemplate::New( |
| 258 | IncrementingSignatureCallback, Local<Value>(), sig); |
| 259 | v8::Handle<v8::FunctionTemplate> callback = |
| 260 | v8::FunctionTemplate::New(IncrementingSignatureCallback); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 261 | v8::Handle<v8::FunctionTemplate> sub_fun = v8::FunctionTemplate::New(); |
| 262 | sub_fun->Inherit(fun); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 263 | v8::Handle<v8::FunctionTemplate> unrel_fun = v8::FunctionTemplate::New(); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 264 | // Install properties. |
| 265 | v8::Handle<v8::ObjectTemplate> fun_proto = fun->PrototypeTemplate(); |
| 266 | fun_proto->Set(v8_str("prop_sig"), callback_sig); |
| 267 | fun_proto->Set(v8_str("prop"), callback); |
| 268 | fun_proto->SetAccessorProperty( |
| 269 | v8_str("accessor_sig"), callback_sig, callback_sig); |
| 270 | fun_proto->SetAccessorProperty(v8_str("accessor"), callback, callback); |
| 271 | // Instantiate templates. |
| 272 | Local<Value> fun_instance = fun->InstanceTemplate()->NewInstance(); |
| 273 | Local<Value> sub_fun_instance = sub_fun->InstanceTemplate()->NewInstance(); |
| 274 | // Setup global variables. |
| 275 | env->Global()->Set(v8_str("Fun"), fun->GetFunction()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 276 | env->Global()->Set(v8_str("UnrelFun"), unrel_fun->GetFunction()); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 277 | env->Global()->Set(v8_str("fun_instance"), fun_instance); |
| 278 | env->Global()->Set(v8_str("sub_fun_instance"), sub_fun_instance); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 279 | CompileRun( |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 280 | "var accessor_sig_key = 'accessor_sig';" |
| 281 | "var accessor_key = 'accessor';" |
| 282 | "var prop_sig_key = 'prop_sig';" |
| 283 | "var prop_key = 'prop';" |
| 284 | "" |
| 285 | "function copy_props(obj) {" |
| 286 | " var keys = [accessor_sig_key, accessor_key, prop_sig_key, prop_key];" |
| 287 | " var source = Fun.prototype;" |
| 288 | " for (var i in keys) {" |
| 289 | " var key = keys[i];" |
| 290 | " var desc = Object.getOwnPropertyDescriptor(source, key);" |
| 291 | " Object.defineProperty(obj, key, desc);" |
| 292 | " }" |
| 293 | "}" |
| 294 | "" |
| 295 | "var obj = {};" |
| 296 | "copy_props(obj);" |
| 297 | "var unrel = new UnrelFun();" |
| 298 | "copy_props(unrel);"); |
| 299 | // Test with and without ICs |
| 300 | const char* test_objects[] = { |
| 301 | "fun_instance", "sub_fun_instance", "obj", "unrel" }; |
| 302 | unsigned bad_signature_start_offset = 2; |
| 303 | for (unsigned i = 0; i < ARRAY_SIZE(test_objects); i++) { |
| 304 | i::ScopedVector<char> source(200); |
| 305 | i::OS::SNPrintF( |
| 306 | source, "var test_object = %s; test_object", test_objects[i]); |
| 307 | Local<Value> test_object = CompileRun(source.start()); |
| 308 | TestSignature("test_object.prop();", test_object); |
| 309 | TestSignature("test_object.accessor;", test_object); |
| 310 | TestSignature("test_object[accessor_key];", test_object); |
| 311 | TestSignature("test_object.accessor = 1;", test_object); |
| 312 | TestSignature("test_object[accessor_key] = 1;", test_object); |
| 313 | if (i >= bad_signature_start_offset) test_object = Local<Value>(); |
| 314 | TestSignature("test_object.prop_sig();", test_object); |
| 315 | TestSignature("test_object.accessor_sig;", test_object); |
| 316 | TestSignature("test_object[accessor_sig_key];", test_object); |
| 317 | TestSignature("test_object.accessor_sig = 1;", test_object); |
| 318 | TestSignature("test_object[accessor_sig_key] = 1;", test_object); |
| 319 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 320 | } |
| 321 | |
| 322 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 323 | THREADED_TEST(ArgumentSignature) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 324 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 325 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 326 | v8::Handle<v8::FunctionTemplate> cons = v8::FunctionTemplate::New(); |
| 327 | cons->SetClassName(v8_str("Cons")); |
| 328 | v8::Handle<v8::Signature> sig = |
| 329 | v8::Signature::New(v8::Handle<v8::FunctionTemplate>(), 1, &cons); |
| 330 | v8::Handle<v8::FunctionTemplate> fun = |
| 331 | v8::FunctionTemplate::New(SignatureCallback, v8::Handle<Value>(), sig); |
| 332 | env->Global()->Set(v8_str("Cons"), cons->GetFunction()); |
| 333 | env->Global()->Set(v8_str("Fun1"), fun->GetFunction()); |
| 334 | |
| 335 | v8::Handle<Value> value1 = CompileRun("Fun1(4) == '';"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 336 | CHECK(value1->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 337 | |
| 338 | v8::Handle<Value> value2 = CompileRun("Fun1(new Cons()) == '[object Cons]';"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 339 | CHECK(value2->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 340 | |
| 341 | v8::Handle<Value> value3 = CompileRun("Fun1() == '';"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 342 | CHECK(value3->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 343 | |
| 344 | v8::Handle<v8::FunctionTemplate> cons1 = v8::FunctionTemplate::New(); |
| 345 | cons1->SetClassName(v8_str("Cons1")); |
| 346 | v8::Handle<v8::FunctionTemplate> cons2 = v8::FunctionTemplate::New(); |
| 347 | cons2->SetClassName(v8_str("Cons2")); |
| 348 | v8::Handle<v8::FunctionTemplate> cons3 = v8::FunctionTemplate::New(); |
| 349 | cons3->SetClassName(v8_str("Cons3")); |
| 350 | |
| 351 | v8::Handle<v8::FunctionTemplate> args[3] = { cons1, cons2, cons3 }; |
| 352 | v8::Handle<v8::Signature> wsig = |
| 353 | v8::Signature::New(v8::Handle<v8::FunctionTemplate>(), 3, args); |
| 354 | v8::Handle<v8::FunctionTemplate> fun2 = |
| 355 | v8::FunctionTemplate::New(SignatureCallback, v8::Handle<Value>(), wsig); |
| 356 | |
| 357 | env->Global()->Set(v8_str("Cons1"), cons1->GetFunction()); |
| 358 | env->Global()->Set(v8_str("Cons2"), cons2->GetFunction()); |
| 359 | env->Global()->Set(v8_str("Cons3"), cons3->GetFunction()); |
| 360 | env->Global()->Set(v8_str("Fun2"), fun2->GetFunction()); |
| 361 | v8::Handle<Value> value4 = CompileRun( |
| 362 | "Fun2(new Cons1(), new Cons2(), new Cons3()) ==" |
| 363 | "'[object Cons1],[object Cons2],[object Cons3]'"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 364 | CHECK(value4->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 365 | |
| 366 | v8::Handle<Value> value5 = CompileRun( |
| 367 | "Fun2(new Cons1(), new Cons2(), 5) == '[object Cons1],[object Cons2],'"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 368 | CHECK(value5->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 369 | |
| 370 | v8::Handle<Value> value6 = CompileRun( |
| 371 | "Fun2(new Cons3(), new Cons2(), new Cons1()) == ',[object Cons2],'"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 372 | CHECK(value6->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 373 | |
| 374 | v8::Handle<Value> value7 = CompileRun( |
| 375 | "Fun2(new Cons1(), new Cons2(), new Cons3(), 'd') == " |
| 376 | "'[object Cons1],[object Cons2],[object Cons3],d';"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 377 | CHECK(value7->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 378 | |
| 379 | v8::Handle<Value> value8 = CompileRun( |
| 380 | "Fun2(new Cons1(), new Cons2()) == '[object Cons1],[object Cons2]'"); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 381 | CHECK(value8->IsTrue()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | |
| 385 | THREADED_TEST(HulIgennem) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 386 | LocalContext env; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 387 | v8::Isolate* isolate = env->GetIsolate(); |
| 388 | v8::HandleScope scope(isolate); |
| 389 | v8::Handle<v8::Primitive> undef = v8::Undefined(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 390 | Local<String> undef_str = undef->ToString(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 391 | char* value = i::NewArray<char>(undef_str->Utf8Length() + 1); |
| 392 | undef_str->WriteUtf8(value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 393 | CHECK_EQ(0, strcmp(value, "undefined")); |
| 394 | i::DeleteArray(value); |
| 395 | } |
| 396 | |
| 397 | |
| 398 | THREADED_TEST(Access) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 399 | LocalContext env; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 400 | v8::Isolate* isolate = env->GetIsolate(); |
| 401 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 402 | Local<v8::Object> obj = v8::Object::New(); |
| 403 | Local<Value> foo_before = obj->Get(v8_str("foo")); |
| 404 | CHECK(foo_before->IsUndefined()); |
| 405 | Local<String> bar_str = v8_str("bar"); |
| 406 | obj->Set(v8_str("foo"), bar_str); |
| 407 | Local<Value> foo_after = obj->Get(v8_str("foo")); |
| 408 | CHECK(!foo_after->IsUndefined()); |
| 409 | CHECK(foo_after->IsString()); |
| 410 | CHECK_EQ(bar_str, foo_after); |
| 411 | } |
| 412 | |
| 413 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 414 | THREADED_TEST(AccessElement) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 415 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 416 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 417 | Local<v8::Object> obj = v8::Object::New(); |
| 418 | Local<Value> before = obj->Get(1); |
| 419 | CHECK(before->IsUndefined()); |
| 420 | Local<String> bar_str = v8_str("bar"); |
| 421 | obj->Set(1, bar_str); |
| 422 | Local<Value> after = obj->Get(1); |
| 423 | CHECK(!after->IsUndefined()); |
| 424 | CHECK(after->IsString()); |
| 425 | CHECK_EQ(bar_str, after); |
| 426 | |
| 427 | Local<v8::Array> value = CompileRun("[\"a\", \"b\"]").As<v8::Array>(); |
| 428 | CHECK_EQ(v8_str("a"), value->Get(0)); |
| 429 | CHECK_EQ(v8_str("b"), value->Get(1)); |
| 430 | } |
| 431 | |
| 432 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 433 | THREADED_TEST(Script) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 434 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 435 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 436 | const char* c_source = "1 + 2 + 3"; |
| 437 | Local<String> source = String::New(c_source); |
| 438 | Local<Script> script = Script::Compile(source); |
| 439 | CHECK_EQ(6, script->Run()->Int32Value()); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | static uint16_t* AsciiToTwoByteString(const char* source) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 444 | int array_length = i::StrLength(source) + 1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 445 | uint16_t* converted = i::NewArray<uint16_t>(array_length); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 446 | for (int i = 0; i < array_length; i++) converted[i] = source[i]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 447 | return converted; |
| 448 | } |
| 449 | |
| 450 | |
| 451 | class TestResource: public String::ExternalStringResource { |
| 452 | public: |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 453 | explicit TestResource(uint16_t* data, int* counter = NULL) |
| 454 | : data_(data), length_(0), counter_(counter) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 455 | while (data[length_]) ++length_; |
| 456 | } |
| 457 | |
| 458 | ~TestResource() { |
| 459 | i::DeleteArray(data_); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 460 | if (counter_ != NULL) ++*counter_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | const uint16_t* data() const { |
| 464 | return data_; |
| 465 | } |
| 466 | |
| 467 | size_t length() const { |
| 468 | return length_; |
| 469 | } |
| 470 | private: |
| 471 | uint16_t* data_; |
| 472 | size_t length_; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 473 | int* counter_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 474 | }; |
| 475 | |
| 476 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 477 | class TestAsciiResource: public String::ExternalAsciiStringResource { |
| 478 | public: |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 479 | explicit TestAsciiResource(const char* data, int* counter = NULL) |
| 480 | : data_(data), length_(strlen(data)), counter_(counter) { } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 481 | |
| 482 | ~TestAsciiResource() { |
| 483 | i::DeleteArray(data_); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 484 | if (counter_ != NULL) ++*counter_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | const char* data() const { |
| 488 | return data_; |
| 489 | } |
| 490 | |
| 491 | size_t length() const { |
| 492 | return length_; |
| 493 | } |
| 494 | private: |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 495 | const char* data_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 496 | size_t length_; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 497 | int* counter_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 498 | }; |
| 499 | |
| 500 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 501 | THREADED_TEST(ScriptUsingStringResource) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 502 | int dispose_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 503 | const char* c_source = "1 + 2 * 3"; |
| 504 | uint16_t* two_byte_source = AsciiToTwoByteString(c_source); |
| 505 | { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 506 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 507 | v8::HandleScope scope(env->GetIsolate()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 508 | TestResource* resource = new TestResource(two_byte_source, &dispose_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 509 | Local<String> source = String::NewExternal(resource); |
| 510 | Local<Script> script = Script::Compile(source); |
| 511 | Local<Value> value = script->Run(); |
| 512 | CHECK(value->IsNumber()); |
| 513 | CHECK_EQ(7, value->Int32Value()); |
| 514 | CHECK(source->IsExternal()); |
| 515 | CHECK_EQ(resource, |
| 516 | static_cast<TestResource*>(source->GetExternalStringResource())); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 517 | String::Encoding encoding = String::UNKNOWN_ENCODING; |
| 518 | CHECK_EQ(static_cast<const String::ExternalStringResourceBase*>(resource), |
| 519 | source->GetExternalStringResourceBase(&encoding)); |
| 520 | CHECK_EQ(String::TWO_BYTE_ENCODING, encoding); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 521 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 522 | CHECK_EQ(0, dispose_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 523 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 524 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 525 | CcTest::heap()->CollectAllAvailableGarbage(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 526 | CHECK_EQ(1, dispose_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
| 530 | THREADED_TEST(ScriptUsingAsciiStringResource) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 531 | int dispose_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 532 | const char* c_source = "1 + 2 * 3"; |
| 533 | { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 534 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 535 | v8::HandleScope scope(env->GetIsolate()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 536 | TestAsciiResource* resource = new TestAsciiResource(i::StrDup(c_source), |
| 537 | &dispose_count); |
| 538 | Local<String> source = String::NewExternal(resource); |
| 539 | CHECK(source->IsExternalAscii()); |
| 540 | CHECK_EQ(static_cast<const String::ExternalStringResourceBase*>(resource), |
| 541 | source->GetExternalAsciiStringResource()); |
| 542 | String::Encoding encoding = String::UNKNOWN_ENCODING; |
| 543 | CHECK_EQ(static_cast<const String::ExternalStringResourceBase*>(resource), |
| 544 | source->GetExternalStringResourceBase(&encoding)); |
| 545 | CHECK_EQ(String::ASCII_ENCODING, encoding); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 546 | Local<Script> script = Script::Compile(source); |
| 547 | Local<Value> value = script->Run(); |
| 548 | CHECK(value->IsNumber()); |
| 549 | CHECK_EQ(7, value->Int32Value()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 550 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 551 | CHECK_EQ(0, dispose_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 552 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 553 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 554 | CcTest::heap()->CollectAllAvailableGarbage(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 555 | CHECK_EQ(1, dispose_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 559 | THREADED_TEST(ScriptMakingExternalString) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 560 | int dispose_count = 0; |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 561 | uint16_t* two_byte_source = AsciiToTwoByteString("1 + 2 * 3"); |
| 562 | { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 563 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 564 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 565 | Local<String> source = String::New(two_byte_source); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 566 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 567 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 568 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 569 | CHECK_EQ(source->IsExternal(), false); |
| 570 | CHECK_EQ(source->IsExternalAscii(), false); |
| 571 | String::Encoding encoding = String::UNKNOWN_ENCODING; |
| 572 | CHECK_EQ(NULL, source->GetExternalStringResourceBase(&encoding)); |
| 573 | CHECK_EQ(String::ASCII_ENCODING, encoding); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 574 | bool success = source->MakeExternal(new TestResource(two_byte_source, |
| 575 | &dispose_count)); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 576 | CHECK(success); |
| 577 | Local<Script> script = Script::Compile(source); |
| 578 | Local<Value> value = script->Run(); |
| 579 | CHECK(value->IsNumber()); |
| 580 | CHECK_EQ(7, value->Int32Value()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 581 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 582 | CHECK_EQ(0, dispose_count); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 583 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 584 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 585 | CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 586 | CHECK_EQ(1, dispose_count); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 587 | } |
| 588 | |
| 589 | |
| 590 | THREADED_TEST(ScriptMakingExternalAsciiString) { |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 591 | int dispose_count = 0; |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 592 | const char* c_source = "1 + 2 * 3"; |
| 593 | { |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 594 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 595 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 596 | Local<String> source = v8_str(c_source); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 597 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 598 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 599 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 600 | bool success = source->MakeExternal( |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 601 | new TestAsciiResource(i::StrDup(c_source), &dispose_count)); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 602 | CHECK(success); |
| 603 | Local<Script> script = Script::Compile(source); |
| 604 | Local<Value> value = script->Run(); |
| 605 | CHECK(value->IsNumber()); |
| 606 | CHECK_EQ(7, value->Int32Value()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 607 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 608 | CHECK_EQ(0, dispose_count); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 609 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 610 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 611 | CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 612 | CHECK_EQ(1, dispose_count); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 616 | TEST(MakingExternalStringConditions) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 617 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 618 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 619 | |
| 620 | // Free some space in the new space so that we can check freshness. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 621 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
| 622 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 623 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 624 | uint16_t* two_byte_string = AsciiToTwoByteString("s1"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 625 | Local<String> small_string = String::New(two_byte_string); |
| 626 | i::DeleteArray(two_byte_string); |
| 627 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 628 | // We should refuse to externalize newly created small string. |
| 629 | CHECK(!small_string->CanMakeExternal()); |
| 630 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 631 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 632 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 633 | // Old space strings should be accepted. |
| 634 | CHECK(small_string->CanMakeExternal()); |
| 635 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 636 | two_byte_string = AsciiToTwoByteString("small string 2"); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 637 | small_string = String::New(two_byte_string); |
| 638 | i::DeleteArray(two_byte_string); |
| 639 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 640 | // We should refuse externalizing newly created small string. |
| 641 | CHECK(!small_string->CanMakeExternal()); |
| 642 | for (int i = 0; i < 100; i++) { |
| 643 | String::Value value(small_string); |
| 644 | } |
| 645 | // Frequently used strings should be accepted. |
| 646 | CHECK(small_string->CanMakeExternal()); |
| 647 | |
| 648 | const int buf_size = 10 * 1024; |
| 649 | char* buf = i::NewArray<char>(buf_size); |
| 650 | memset(buf, 'a', buf_size); |
| 651 | buf[buf_size - 1] = '\0'; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 652 | |
| 653 | two_byte_string = AsciiToTwoByteString(buf); |
| 654 | Local<String> large_string = String::New(two_byte_string); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 655 | i::DeleteArray(buf); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 656 | i::DeleteArray(two_byte_string); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 657 | // Large strings should be immediately accepted. |
| 658 | CHECK(large_string->CanMakeExternal()); |
| 659 | } |
| 660 | |
| 661 | |
| 662 | TEST(MakingExternalAsciiStringConditions) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 663 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 664 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 665 | |
| 666 | // Free some space in the new space so that we can check freshness. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 667 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
| 668 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 669 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 670 | Local<String> small_string = String::New("s1"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 671 | // We should refuse to externalize newly created small string. |
| 672 | CHECK(!small_string->CanMakeExternal()); |
| 673 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 674 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 675 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 676 | // Old space strings should be accepted. |
| 677 | CHECK(small_string->CanMakeExternal()); |
| 678 | |
mstarzinger@chromium.org | 1b3afd1 | 2011-11-29 14:28:56 +0000 | [diff] [blame] | 679 | small_string = String::New("small string 2"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 680 | // We should refuse externalizing newly created small string. |
| 681 | CHECK(!small_string->CanMakeExternal()); |
| 682 | for (int i = 0; i < 100; i++) { |
| 683 | String::Value value(small_string); |
| 684 | } |
| 685 | // Frequently used strings should be accepted. |
| 686 | CHECK(small_string->CanMakeExternal()); |
| 687 | |
| 688 | const int buf_size = 10 * 1024; |
| 689 | char* buf = i::NewArray<char>(buf_size); |
| 690 | memset(buf, 'a', buf_size); |
| 691 | buf[buf_size - 1] = '\0'; |
| 692 | Local<String> large_string = String::New(buf); |
| 693 | i::DeleteArray(buf); |
| 694 | // Large strings should be immediately accepted. |
| 695 | CHECK(large_string->CanMakeExternal()); |
| 696 | } |
| 697 | |
| 698 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 699 | TEST(MakingExternalUnalignedAsciiString) { |
| 700 | LocalContext env; |
| 701 | v8::HandleScope scope(env->GetIsolate()); |
| 702 | |
| 703 | CompileRun("function cons(a, b) { return a + b; }" |
| 704 | "function slice(a) { return a.substring(1); }"); |
| 705 | // Create a cons string that will land in old pointer space. |
| 706 | Local<String> cons = Local<String>::Cast(CompileRun( |
| 707 | "cons('abcdefghijklm', 'nopqrstuvwxyz');")); |
| 708 | // Create a sliced string that will land in old pointer space. |
| 709 | Local<String> slice = Local<String>::Cast(CompileRun( |
| 710 | "slice('abcdefghijklmnopqrstuvwxyz');")); |
| 711 | |
| 712 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 713 | SimulateFullSpace(CcTest::heap()->old_pointer_space()); |
| 714 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 715 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 716 | |
| 717 | // Turn into external string with unaligned resource data. |
| 718 | int dispose_count = 0; |
| 719 | const char* c_cons = "_abcdefghijklmnopqrstuvwxyz"; |
| 720 | bool success = cons->MakeExternal( |
| 721 | new TestAsciiResource(i::StrDup(c_cons) + 1, &dispose_count)); |
| 722 | CHECK(success); |
| 723 | const char* c_slice = "_bcdefghijklmnopqrstuvwxyz"; |
| 724 | success = slice->MakeExternal( |
| 725 | new TestAsciiResource(i::StrDup(c_slice) + 1, &dispose_count)); |
| 726 | CHECK(success); |
| 727 | |
| 728 | // Trigger GCs and force evacuation. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 729 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 730 | CcTest::heap()->CollectAllGarbage(i::Heap::kReduceMemoryFootprintMask); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 731 | } |
| 732 | |
| 733 | |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 734 | THREADED_TEST(UsingExternalString) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 735 | i::Factory* factory = CcTest::i_isolate()->factory(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 736 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 737 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 738 | uint16_t* two_byte_string = AsciiToTwoByteString("test string"); |
| 739 | Local<String> string = |
| 740 | String::NewExternal(new TestResource(two_byte_string)); |
| 741 | i::Handle<i::String> istring = v8::Utils::OpenHandle(*string); |
| 742 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 743 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 744 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 745 | i::Handle<i::String> isymbol = |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 746 | factory->InternalizedStringFromString(istring); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 747 | CHECK(isymbol->IsInternalizedString()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 748 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 749 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 750 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | |
| 754 | THREADED_TEST(UsingExternalAsciiString) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 755 | i::Factory* factory = CcTest::i_isolate()->factory(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 756 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 757 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 758 | const char* one_byte_string = "test string"; |
| 759 | Local<String> string = String::NewExternal( |
| 760 | new TestAsciiResource(i::StrDup(one_byte_string))); |
| 761 | i::Handle<i::String> istring = v8::Utils::OpenHandle(*string); |
| 762 | // Trigger GCs so that the newly allocated string moves to old gen. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 763 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in survivor space now |
| 764 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); // in old gen now |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 765 | i::Handle<i::String> isymbol = |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 766 | factory->InternalizedStringFromString(istring); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 767 | CHECK(isymbol->IsInternalizedString()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 768 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 769 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 770 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 6f10e41 | 2009-02-13 10:11:16 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 774 | THREADED_TEST(ScavengeExternalString) { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 775 | i::FLAG_stress_compaction = false; |
| 776 | i::FLAG_gc_global = false; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 777 | int dispose_count = 0; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 778 | bool in_new_space = false; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 779 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 780 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 781 | uint16_t* two_byte_string = AsciiToTwoByteString("test string"); |
| 782 | Local<String> string = |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 783 | String::NewExternal(new TestResource(two_byte_string, |
| 784 | &dispose_count)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 785 | i::Handle<i::String> istring = v8::Utils::OpenHandle(*string); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 786 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
| 787 | in_new_space = CcTest::heap()->InNewSpace(*istring); |
| 788 | CHECK(in_new_space || CcTest::heap()->old_data_space()->Contains(*istring)); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 789 | CHECK_EQ(0, dispose_count); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 790 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 791 | CcTest::heap()->CollectGarbage( |
| 792 | in_new_space ? i::NEW_SPACE : i::OLD_DATA_SPACE); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 793 | CHECK_EQ(1, dispose_count); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 794 | } |
| 795 | |
| 796 | |
| 797 | THREADED_TEST(ScavengeExternalAsciiString) { |
svenpanne@chromium.org | 83130cf | 2012-11-30 10:13:25 +0000 | [diff] [blame] | 798 | i::FLAG_stress_compaction = false; |
| 799 | i::FLAG_gc_global = false; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 800 | int dispose_count = 0; |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 801 | bool in_new_space = false; |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 802 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 803 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 804 | const char* one_byte_string = "test string"; |
| 805 | Local<String> string = String::NewExternal( |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 806 | new TestAsciiResource(i::StrDup(one_byte_string), &dispose_count)); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 807 | i::Handle<i::String> istring = v8::Utils::OpenHandle(*string); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 808 | CcTest::heap()->CollectGarbage(i::NEW_SPACE); |
| 809 | in_new_space = CcTest::heap()->InNewSpace(*istring); |
| 810 | CHECK(in_new_space || CcTest::heap()->old_data_space()->Contains(*istring)); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 811 | CHECK_EQ(0, dispose_count); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 812 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 813 | CcTest::heap()->CollectGarbage( |
| 814 | in_new_space ? i::NEW_SPACE : i::OLD_DATA_SPACE); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 815 | CHECK_EQ(1, dispose_count); |
kmillikin@chromium.org | 13bd294 | 2009-12-16 15:36:05 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 819 | class TestAsciiResourceWithDisposeControl: public TestAsciiResource { |
| 820 | public: |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 821 | // Only used by non-threaded tests, so it can use static fields. |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 822 | static int dispose_calls; |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 823 | static int dispose_count; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 824 | |
| 825 | TestAsciiResourceWithDisposeControl(const char* data, bool dispose) |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 826 | : TestAsciiResource(data, &dispose_count), |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 827 | dispose_(dispose) { } |
| 828 | |
| 829 | void Dispose() { |
| 830 | ++dispose_calls; |
| 831 | if (dispose_) delete this; |
| 832 | } |
| 833 | private: |
| 834 | bool dispose_; |
| 835 | }; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 836 | |
| 837 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 838 | int TestAsciiResourceWithDisposeControl::dispose_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 839 | int TestAsciiResourceWithDisposeControl::dispose_calls = 0; |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 840 | |
| 841 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 842 | TEST(ExternalStringWithDisposeHandling) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 843 | const char* c_source = "1 + 2 * 3"; |
| 844 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 845 | // Use a stack allocated external string resource allocated object. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 846 | TestAsciiResourceWithDisposeControl::dispose_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 847 | TestAsciiResourceWithDisposeControl::dispose_calls = 0; |
| 848 | TestAsciiResourceWithDisposeControl res_stack(i::StrDup(c_source), false); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 849 | { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 850 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 851 | v8::HandleScope scope(env->GetIsolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 852 | Local<String> source = String::NewExternal(&res_stack); |
| 853 | Local<Script> script = Script::Compile(source); |
| 854 | Local<Value> value = script->Run(); |
| 855 | CHECK(value->IsNumber()); |
| 856 | CHECK_EQ(7, value->Int32Value()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 857 | CcTest::heap()->CollectAllAvailableGarbage(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 858 | CHECK_EQ(0, TestAsciiResourceWithDisposeControl::dispose_count); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 859 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 860 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 861 | CcTest::heap()->CollectAllAvailableGarbage(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 862 | CHECK_EQ(1, TestAsciiResourceWithDisposeControl::dispose_calls); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 863 | CHECK_EQ(0, TestAsciiResourceWithDisposeControl::dispose_count); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 864 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 865 | // Use a heap allocated external string resource allocated object. |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 866 | TestAsciiResourceWithDisposeControl::dispose_count = 0; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 867 | TestAsciiResourceWithDisposeControl::dispose_calls = 0; |
| 868 | TestAsciiResource* res_heap = |
| 869 | new TestAsciiResourceWithDisposeControl(i::StrDup(c_source), true); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 870 | { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 871 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 872 | v8::HandleScope scope(env->GetIsolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 873 | Local<String> source = String::NewExternal(res_heap); |
| 874 | Local<Script> script = Script::Compile(source); |
| 875 | Local<Value> value = script->Run(); |
| 876 | CHECK(value->IsNumber()); |
| 877 | CHECK_EQ(7, value->Int32Value()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 878 | CcTest::heap()->CollectAllAvailableGarbage(); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 879 | CHECK_EQ(0, TestAsciiResourceWithDisposeControl::dispose_count); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 880 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 881 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 882 | CcTest::heap()->CollectAllAvailableGarbage(); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 883 | CHECK_EQ(1, TestAsciiResourceWithDisposeControl::dispose_calls); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 884 | CHECK_EQ(1, TestAsciiResourceWithDisposeControl::dispose_count); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 888 | THREADED_TEST(StringConcat) { |
| 889 | { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 890 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 891 | v8::HandleScope scope(env->GetIsolate()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 892 | const char* one_byte_string_1 = "function a_times_t"; |
| 893 | const char* two_byte_string_1 = "wo_plus_b(a, b) {return "; |
| 894 | const char* one_byte_extern_1 = "a * 2 + b;} a_times_two_plus_b(4, 8) + "; |
| 895 | const char* two_byte_extern_1 = "a_times_two_plus_b(4, 8) + "; |
| 896 | const char* one_byte_string_2 = "a_times_two_plus_b(4, 8) + "; |
| 897 | const char* two_byte_string_2 = "a_times_two_plus_b(4, 8) + "; |
| 898 | const char* two_byte_extern_2 = "a_times_two_plus_b(1, 2);"; |
| 899 | Local<String> left = v8_str(one_byte_string_1); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 900 | |
| 901 | uint16_t* two_byte_source = AsciiToTwoByteString(two_byte_string_1); |
| 902 | Local<String> right = String::New(two_byte_source); |
| 903 | i::DeleteArray(two_byte_source); |
| 904 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 905 | Local<String> source = String::Concat(left, right); |
| 906 | right = String::NewExternal( |
| 907 | new TestAsciiResource(i::StrDup(one_byte_extern_1))); |
| 908 | source = String::Concat(source, right); |
| 909 | right = String::NewExternal( |
| 910 | new TestResource(AsciiToTwoByteString(two_byte_extern_1))); |
| 911 | source = String::Concat(source, right); |
| 912 | right = v8_str(one_byte_string_2); |
| 913 | source = String::Concat(source, right); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 914 | |
| 915 | two_byte_source = AsciiToTwoByteString(two_byte_string_2); |
| 916 | right = String::New(two_byte_source); |
| 917 | i::DeleteArray(two_byte_source); |
| 918 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 919 | source = String::Concat(source, right); |
| 920 | right = String::NewExternal( |
| 921 | new TestResource(AsciiToTwoByteString(two_byte_extern_2))); |
| 922 | source = String::Concat(source, right); |
| 923 | Local<Script> script = Script::Compile(source); |
| 924 | Local<Value> value = script->Run(); |
| 925 | CHECK(value->IsNumber()); |
| 926 | CHECK_EQ(68, value->Int32Value()); |
| 927 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 928 | CcTest::i_isolate()->compilation_cache()->Clear(); |
| 929 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 930 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 934 | THREADED_TEST(GlobalProperties) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 935 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 936 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 937 | v8::Handle<v8::Object> global = env->Global(); |
| 938 | global->Set(v8_str("pi"), v8_num(3.1415926)); |
| 939 | Local<Value> pi = global->Get(v8_str("pi")); |
| 940 | CHECK_EQ(3.1415926, pi->NumberValue()); |
| 941 | } |
| 942 | |
| 943 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 944 | template<typename T> |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 945 | static void CheckReturnValue(const T& t, i::Address callback) { |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 946 | v8::ReturnValue<v8::Value> rv = t.GetReturnValue(); |
| 947 | i::Object** o = *reinterpret_cast<i::Object***>(&rv); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 948 | CHECK_EQ(CcTest::isolate(), t.GetIsolate()); |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 949 | CHECK_EQ(t.GetIsolate(), rv.GetIsolate()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 950 | CHECK((*o)->IsTheHole() || (*o)->IsUndefined()); |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 951 | // Verify reset |
| 952 | bool is_runtime = (*o)->IsTheHole(); |
| 953 | rv.Set(true); |
| 954 | CHECK(!(*o)->IsTheHole() && !(*o)->IsUndefined()); |
| 955 | rv.Set(v8::Handle<v8::Object>()); |
| 956 | CHECK((*o)->IsTheHole() || (*o)->IsUndefined()); |
| 957 | CHECK_EQ(is_runtime, (*o)->IsTheHole()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 958 | |
| 959 | i::Isolate* isolate = reinterpret_cast<i::Isolate*>(t.GetIsolate()); |
| 960 | // If CPU profiler is active check that when API callback is invoked |
| 961 | // VMState is set to EXTERNAL. |
| 962 | if (isolate->cpu_profiler()->is_profiling()) { |
| 963 | CHECK_EQ(i::EXTERNAL, isolate->current_vm_state()); |
yangguo@chromium.org | c73d55b | 2013-07-24 08:18:28 +0000 | [diff] [blame] | 964 | CHECK(isolate->external_callback_scope()); |
| 965 | CHECK_EQ(callback, isolate->external_callback_scope()->callback()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 966 | } |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 967 | } |
| 968 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 969 | |
| 970 | static void handle_callback_impl(const v8::FunctionCallbackInfo<Value>& info, |
| 971 | i::Address callback) { |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 972 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 973 | CheckReturnValue(info, callback); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 974 | info.GetReturnValue().Set(v8_str("bad value")); |
| 975 | info.GetReturnValue().Set(v8_num(102)); |
| 976 | } |
| 977 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 978 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 979 | static void handle_callback(const v8::FunctionCallbackInfo<Value>& info) { |
| 980 | return handle_callback_impl(info, FUNCTION_ADDR(handle_callback)); |
| 981 | } |
| 982 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 983 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 984 | static void handle_callback_2(const v8::FunctionCallbackInfo<Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 985 | return handle_callback_impl(info, FUNCTION_ADDR(handle_callback_2)); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 986 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 987 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 988 | static void construct_callback( |
| 989 | const v8::FunctionCallbackInfo<Value>& info) { |
| 990 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 991 | CheckReturnValue(info, FUNCTION_ADDR(construct_callback)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 992 | info.This()->Set(v8_str("x"), v8_num(1)); |
| 993 | info.This()->Set(v8_str("y"), v8_num(2)); |
| 994 | info.GetReturnValue().Set(v8_str("bad value")); |
| 995 | info.GetReturnValue().Set(info.This()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 999 | static void Return239Callback( |
| 1000 | Local<String> name, const v8::PropertyCallbackInfo<Value>& info) { |
| 1001 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1002 | CheckReturnValue(info, FUNCTION_ADDR(Return239Callback)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1003 | info.GetReturnValue().Set(v8_str("bad value")); |
| 1004 | info.GetReturnValue().Set(v8_num(239)); |
| 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | template<typename Handler> |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 1009 | static void TestFunctionTemplateInitializer(Handler handler, |
| 1010 | Handler handler_2) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1011 | // Test constructor calls. |
| 1012 | { |
| 1013 | LocalContext env; |
| 1014 | v8::HandleScope scope(env->GetIsolate()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1015 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1016 | Local<v8::FunctionTemplate> fun_templ = |
| 1017 | v8::FunctionTemplate::New(handler); |
| 1018 | Local<Function> fun = fun_templ->GetFunction(); |
| 1019 | env->Global()->Set(v8_str("obj"), fun); |
| 1020 | Local<Script> script = v8_compile("obj()"); |
| 1021 | for (int i = 0; i < 30; i++) { |
| 1022 | CHECK_EQ(102, script->Run()->Int32Value()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1023 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1024 | } |
| 1025 | // Use SetCallHandler to initialize a function template, should work like |
| 1026 | // the previous one. |
| 1027 | { |
| 1028 | LocalContext env; |
| 1029 | v8::HandleScope scope(env->GetIsolate()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1030 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1031 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 1032 | fun_templ->SetCallHandler(handler_2); |
| 1033 | Local<Function> fun = fun_templ->GetFunction(); |
| 1034 | env->Global()->Set(v8_str("obj"), fun); |
| 1035 | Local<Script> script = v8_compile("obj()"); |
| 1036 | for (int i = 0; i < 30; i++) { |
| 1037 | CHECK_EQ(102, script->Run()->Int32Value()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1038 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1039 | } |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | |
| 1043 | template<typename Constructor, typename Accessor> |
| 1044 | static void TestFunctionTemplateAccessor(Constructor constructor, |
| 1045 | Accessor accessor) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1046 | LocalContext env; |
| 1047 | v8::HandleScope scope(env->GetIsolate()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1048 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1049 | Local<v8::FunctionTemplate> fun_templ = |
| 1050 | v8::FunctionTemplate::New(constructor); |
| 1051 | fun_templ->SetClassName(v8_str("funky")); |
| 1052 | fun_templ->InstanceTemplate()->SetAccessor(v8_str("m"), accessor); |
| 1053 | Local<Function> fun = fun_templ->GetFunction(); |
| 1054 | env->Global()->Set(v8_str("obj"), fun); |
| 1055 | Local<Value> result = v8_compile("(new obj()).toString()")->Run(); |
| 1056 | CHECK_EQ(v8_str("[object funky]"), result); |
| 1057 | CompileRun("var obj_instance = new obj();"); |
| 1058 | Local<Script> script; |
| 1059 | script = v8_compile("obj_instance.x"); |
| 1060 | for (int i = 0; i < 30; i++) { |
| 1061 | CHECK_EQ(1, script->Run()->Int32Value()); |
| 1062 | } |
| 1063 | script = v8_compile("obj_instance.m"); |
| 1064 | for (int i = 0; i < 30; i++) { |
| 1065 | CHECK_EQ(239, script->Run()->Int32Value()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1066 | } |
| 1067 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1068 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 1069 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1070 | THREADED_PROFILED_TEST(FunctionTemplate) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 1071 | TestFunctionTemplateInitializer(handle_callback, handle_callback_2); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1072 | TestFunctionTemplateAccessor(construct_callback, Return239Callback); |
| 1073 | } |
| 1074 | |
| 1075 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1076 | static void SimpleCallback(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 1077 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1078 | CheckReturnValue(info, FUNCTION_ADDR(SimpleCallback)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1079 | info.GetReturnValue().Set(v8_num(51423 + info.Length())); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | template<typename Callback> |
| 1084 | static void TestSimpleCallback(Callback callback) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1085 | LocalContext env; |
| 1086 | v8::HandleScope scope(env->GetIsolate()); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1087 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1088 | v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 1089 | object_template->Set("callback", v8::FunctionTemplate::New(callback)); |
| 1090 | v8::Local<v8::Object> object = object_template->NewInstance(); |
| 1091 | (*env)->Global()->Set(v8_str("callback_object"), object); |
| 1092 | v8::Handle<v8::Script> script; |
| 1093 | script = v8_compile("callback_object.callback(17)"); |
| 1094 | for (int i = 0; i < 30; i++) { |
| 1095 | CHECK_EQ(51424, script->Run()->Int32Value()); |
| 1096 | } |
| 1097 | script = v8_compile("callback_object.callback(17, 24)"); |
| 1098 | for (int i = 0; i < 30; i++) { |
| 1099 | CHECK_EQ(51425, script->Run()->Int32Value()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1104 | THREADED_PROFILED_TEST(SimpleCallback) { |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1105 | TestSimpleCallback(SimpleCallback); |
| 1106 | } |
| 1107 | |
| 1108 | |
| 1109 | template<typename T> |
| 1110 | void FastReturnValueCallback(const v8::FunctionCallbackInfo<v8::Value>& info); |
| 1111 | |
| 1112 | // constant return values |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 1113 | static int32_t fast_return_value_int32 = 471; |
| 1114 | static uint32_t fast_return_value_uint32 = 571; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1115 | static const double kFastReturnValueDouble = 2.7; |
| 1116 | // variable return values |
| 1117 | static bool fast_return_value_bool = false; |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1118 | enum ReturnValueOddball { |
| 1119 | kNullReturnValue, |
| 1120 | kUndefinedReturnValue, |
| 1121 | kEmptyStringReturnValue |
| 1122 | }; |
| 1123 | static ReturnValueOddball fast_return_value_void; |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 1124 | static bool fast_return_value_object_is_empty = false; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1125 | |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1126 | // Helper function to avoid compiler error: insufficient contextual information |
| 1127 | // to determine type when applying FUNCTION_ADDR to a template function. |
| 1128 | static i::Address address_of(v8::FunctionCallback callback) { |
| 1129 | return FUNCTION_ADDR(callback); |
| 1130 | } |
| 1131 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1132 | template<> |
| 1133 | void FastReturnValueCallback<int32_t>( |
| 1134 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1135 | CheckReturnValue(info, address_of(FastReturnValueCallback<int32_t>)); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 1136 | info.GetReturnValue().Set(fast_return_value_int32); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | template<> |
| 1140 | void FastReturnValueCallback<uint32_t>( |
| 1141 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1142 | CheckReturnValue(info, address_of(FastReturnValueCallback<uint32_t>)); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 1143 | info.GetReturnValue().Set(fast_return_value_uint32); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1144 | } |
| 1145 | |
| 1146 | template<> |
| 1147 | void FastReturnValueCallback<double>( |
| 1148 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1149 | CheckReturnValue(info, address_of(FastReturnValueCallback<double>)); |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 1150 | info.GetReturnValue().Set(kFastReturnValueDouble); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | template<> |
| 1154 | void FastReturnValueCallback<bool>( |
| 1155 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1156 | CheckReturnValue(info, address_of(FastReturnValueCallback<bool>)); |
svenpanne@chromium.org | 53ad175 | 2013-05-27 12:20:38 +0000 | [diff] [blame] | 1157 | info.GetReturnValue().Set(fast_return_value_bool); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
| 1160 | template<> |
| 1161 | void FastReturnValueCallback<void>( |
| 1162 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 1163 | CheckReturnValue(info, address_of(FastReturnValueCallback<void>)); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1164 | switch (fast_return_value_void) { |
| 1165 | case kNullReturnValue: |
| 1166 | info.GetReturnValue().SetNull(); |
| 1167 | break; |
| 1168 | case kUndefinedReturnValue: |
| 1169 | info.GetReturnValue().SetUndefined(); |
| 1170 | break; |
| 1171 | case kEmptyStringReturnValue: |
| 1172 | info.GetReturnValue().SetEmptyString(); |
| 1173 | break; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1174 | } |
| 1175 | } |
| 1176 | |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 1177 | template<> |
| 1178 | void FastReturnValueCallback<Object>( |
| 1179 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 1180 | v8::Handle<v8::Object> object; |
| 1181 | if (!fast_return_value_object_is_empty) object = Object::New(); |
| 1182 | info.GetReturnValue().Set(object); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 1183 | } |
| 1184 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1185 | template<typename T> |
| 1186 | Handle<Value> TestFastReturnValues() { |
| 1187 | LocalContext env; |
| 1188 | v8::HandleScope scope(env->GetIsolate()); |
| 1189 | v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 1190 | v8::FunctionCallback callback = &FastReturnValueCallback<T>; |
| 1191 | object_template->Set("callback", v8::FunctionTemplate::New(callback)); |
| 1192 | v8::Local<v8::Object> object = object_template->NewInstance(); |
| 1193 | (*env)->Global()->Set(v8_str("callback_object"), object); |
| 1194 | return scope.Close(CompileRun("callback_object.callback()")); |
| 1195 | } |
| 1196 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1197 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1198 | THREADED_PROFILED_TEST(FastReturnValues) { |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 1199 | LocalContext env; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1200 | v8::HandleScope scope(CcTest::isolate()); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1201 | v8::Handle<v8::Value> value; |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 1202 | // check int32_t and uint32_t |
| 1203 | int32_t int_values[] = { |
| 1204 | 0, 234, -723, |
| 1205 | i::Smi::kMinValue, i::Smi::kMaxValue |
| 1206 | }; |
| 1207 | for (size_t i = 0; i < ARRAY_SIZE(int_values); i++) { |
| 1208 | for (int modifier = -1; modifier <= 1; modifier++) { |
| 1209 | int int_value = int_values[i] + modifier; |
| 1210 | // check int32_t |
| 1211 | fast_return_value_int32 = int_value; |
| 1212 | value = TestFastReturnValues<int32_t>(); |
| 1213 | CHECK(value->IsInt32()); |
| 1214 | CHECK(fast_return_value_int32 == value->Int32Value()); |
| 1215 | // check uint32_t |
| 1216 | fast_return_value_uint32 = static_cast<uint32_t>(int_value); |
| 1217 | value = TestFastReturnValues<uint32_t>(); |
| 1218 | CHECK(value->IsUint32()); |
| 1219 | CHECK(fast_return_value_uint32 == value->Uint32Value()); |
| 1220 | } |
| 1221 | } |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1222 | // check double |
| 1223 | value = TestFastReturnValues<double>(); |
| 1224 | CHECK(value->IsNumber()); |
| 1225 | CHECK_EQ(kFastReturnValueDouble, value->ToNumber()->Value()); |
| 1226 | // check bool values |
| 1227 | for (int i = 0; i < 2; i++) { |
| 1228 | fast_return_value_bool = i == 0; |
| 1229 | value = TestFastReturnValues<bool>(); |
| 1230 | CHECK(value->IsBoolean()); |
| 1231 | CHECK_EQ(fast_return_value_bool, value->ToBoolean()->Value()); |
| 1232 | } |
| 1233 | // check oddballs |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1234 | ReturnValueOddball oddballs[] = { |
| 1235 | kNullReturnValue, |
| 1236 | kUndefinedReturnValue, |
| 1237 | kEmptyStringReturnValue |
| 1238 | }; |
| 1239 | for (size_t i = 0; i < ARRAY_SIZE(oddballs); i++) { |
| 1240 | fast_return_value_void = oddballs[i]; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1241 | value = TestFastReturnValues<void>(); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 1242 | switch (fast_return_value_void) { |
| 1243 | case kNullReturnValue: |
| 1244 | CHECK(value->IsNull()); |
| 1245 | break; |
| 1246 | case kUndefinedReturnValue: |
| 1247 | CHECK(value->IsUndefined()); |
| 1248 | break; |
| 1249 | case kEmptyStringReturnValue: |
| 1250 | CHECK(value->IsString()); |
| 1251 | CHECK_EQ(0, v8::String::Cast(*value)->Length()); |
| 1252 | break; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 1253 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1254 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 1255 | // check handles |
| 1256 | fast_return_value_object_is_empty = false; |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 1257 | value = TestFastReturnValues<Object>(); |
| 1258 | CHECK(value->IsObject()); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 1259 | fast_return_value_object_is_empty = true; |
| 1260 | value = TestFastReturnValues<Object>(); |
| 1261 | CHECK(value->IsUndefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1265 | THREADED_TEST(FunctionTemplateSetLength) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1266 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1267 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1268 | { |
| 1269 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New( |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1270 | handle_callback, Handle<v8::Value>(), Handle<v8::Signature>(), 23); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1271 | Local<Function> fun = fun_templ->GetFunction(); |
| 1272 | env->Global()->Set(v8_str("obj"), fun); |
| 1273 | Local<Script> script = v8_compile("obj.length"); |
| 1274 | CHECK_EQ(23, script->Run()->Int32Value()); |
| 1275 | } |
| 1276 | { |
| 1277 | Local<v8::FunctionTemplate> fun_templ = |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1278 | v8::FunctionTemplate::New(handle_callback); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1279 | fun_templ->SetLength(22); |
| 1280 | Local<Function> fun = fun_templ->GetFunction(); |
| 1281 | env->Global()->Set(v8_str("obj"), fun); |
| 1282 | Local<Script> script = v8_compile("obj.length"); |
| 1283 | CHECK_EQ(22, script->Run()->Int32Value()); |
| 1284 | } |
| 1285 | { |
| 1286 | // Without setting length it defaults to 0. |
| 1287 | Local<v8::FunctionTemplate> fun_templ = |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1288 | v8::FunctionTemplate::New(handle_callback); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 1289 | Local<Function> fun = fun_templ->GetFunction(); |
| 1290 | env->Global()->Set(v8_str("obj"), fun); |
| 1291 | Local<Script> script = v8_compile("obj.length"); |
| 1292 | CHECK_EQ(0, script->Run()->Int32Value()); |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1297 | static void* expected_ptr; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1298 | static void callback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 1299 | void* ptr = v8::External::Cast(*args.Data())->Value(); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1300 | CHECK_EQ(expected_ptr, ptr); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1301 | args.GetReturnValue().Set(true); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | static void TestExternalPointerWrapping() { |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1306 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1307 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1308 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 1309 | v8::Handle<v8::Value> data = v8::External::New(expected_ptr); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1310 | |
| 1311 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 1312 | obj->Set(v8_str("func"), |
| 1313 | v8::FunctionTemplate::New(callback, data)->GetFunction()); |
| 1314 | env->Global()->Set(v8_str("obj"), obj); |
| 1315 | |
| 1316 | CHECK(CompileRun( |
| 1317 | "function foo() {\n" |
| 1318 | " for (var i = 0; i < 13; i++) obj.func();\n" |
| 1319 | "}\n" |
| 1320 | "foo(), true")->BooleanValue()); |
| 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | THREADED_TEST(ExternalWrap) { |
| 1325 | // Check heap allocated object. |
| 1326 | int* ptr = new int; |
| 1327 | expected_ptr = ptr; |
| 1328 | TestExternalPointerWrapping(); |
| 1329 | delete ptr; |
| 1330 | |
| 1331 | // Check stack allocated object. |
| 1332 | int foo; |
| 1333 | expected_ptr = &foo; |
| 1334 | TestExternalPointerWrapping(); |
| 1335 | |
| 1336 | // Check not aligned addresses. |
| 1337 | const int n = 100; |
| 1338 | char* s = new char[n]; |
| 1339 | for (int i = 0; i < n; i++) { |
| 1340 | expected_ptr = s + i; |
| 1341 | TestExternalPointerWrapping(); |
| 1342 | } |
| 1343 | |
| 1344 | delete[] s; |
| 1345 | |
| 1346 | // Check several invalid addresses. |
| 1347 | expected_ptr = reinterpret_cast<void*>(1); |
| 1348 | TestExternalPointerWrapping(); |
| 1349 | |
| 1350 | expected_ptr = reinterpret_cast<void*>(0xdeadbeef); |
| 1351 | TestExternalPointerWrapping(); |
| 1352 | |
| 1353 | expected_ptr = reinterpret_cast<void*>(0xdeadbeef + 1); |
| 1354 | TestExternalPointerWrapping(); |
| 1355 | |
| 1356 | #if defined(V8_HOST_ARCH_X64) |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 1357 | // Check a value with a leading 1 bit in x64 Smi encoding. |
| 1358 | expected_ptr = reinterpret_cast<void*>(0x400000000); |
| 1359 | TestExternalPointerWrapping(); |
| 1360 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 1361 | expected_ptr = reinterpret_cast<void*>(0xdeadbeefdeadbeef); |
| 1362 | TestExternalPointerWrapping(); |
| 1363 | |
| 1364 | expected_ptr = reinterpret_cast<void*>(0xdeadbeefdeadbeef + 1); |
| 1365 | TestExternalPointerWrapping(); |
| 1366 | #endif |
| 1367 | } |
| 1368 | |
| 1369 | |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 1370 | THREADED_TEST(FindInstanceInPrototypeChain) { |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 1371 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1372 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 900d3b7 | 2009-08-07 11:24:25 +0000 | [diff] [blame] | 1373 | |
| 1374 | Local<v8::FunctionTemplate> base = v8::FunctionTemplate::New(); |
| 1375 | Local<v8::FunctionTemplate> derived = v8::FunctionTemplate::New(); |
| 1376 | Local<v8::FunctionTemplate> other = v8::FunctionTemplate::New(); |
| 1377 | derived->Inherit(base); |
| 1378 | |
| 1379 | Local<v8::Function> base_function = base->GetFunction(); |
| 1380 | Local<v8::Function> derived_function = derived->GetFunction(); |
| 1381 | Local<v8::Function> other_function = other->GetFunction(); |
| 1382 | |
| 1383 | Local<v8::Object> base_instance = base_function->NewInstance(); |
| 1384 | Local<v8::Object> derived_instance = derived_function->NewInstance(); |
| 1385 | Local<v8::Object> derived_instance2 = derived_function->NewInstance(); |
| 1386 | Local<v8::Object> other_instance = other_function->NewInstance(); |
| 1387 | derived_instance2->Set(v8_str("__proto__"), derived_instance); |
| 1388 | other_instance->Set(v8_str("__proto__"), derived_instance2); |
| 1389 | |
| 1390 | // base_instance is only an instance of base. |
| 1391 | CHECK_EQ(base_instance, |
| 1392 | base_instance->FindInstanceInPrototypeChain(base)); |
| 1393 | CHECK(base_instance->FindInstanceInPrototypeChain(derived).IsEmpty()); |
| 1394 | CHECK(base_instance->FindInstanceInPrototypeChain(other).IsEmpty()); |
| 1395 | |
| 1396 | // derived_instance is an instance of base and derived. |
| 1397 | CHECK_EQ(derived_instance, |
| 1398 | derived_instance->FindInstanceInPrototypeChain(base)); |
| 1399 | CHECK_EQ(derived_instance, |
| 1400 | derived_instance->FindInstanceInPrototypeChain(derived)); |
| 1401 | CHECK(derived_instance->FindInstanceInPrototypeChain(other).IsEmpty()); |
| 1402 | |
| 1403 | // other_instance is an instance of other and its immediate |
| 1404 | // prototype derived_instance2 is an instance of base and derived. |
| 1405 | // Note, derived_instance is an instance of base and derived too, |
| 1406 | // but it comes after derived_instance2 in the prototype chain of |
| 1407 | // other_instance. |
| 1408 | CHECK_EQ(derived_instance2, |
| 1409 | other_instance->FindInstanceInPrototypeChain(base)); |
| 1410 | CHECK_EQ(derived_instance2, |
| 1411 | other_instance->FindInstanceInPrototypeChain(derived)); |
| 1412 | CHECK_EQ(other_instance, |
| 1413 | other_instance->FindInstanceInPrototypeChain(other)); |
| 1414 | } |
| 1415 | |
| 1416 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1417 | THREADED_TEST(TinyInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1418 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1419 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1420 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1421 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1422 | int32_t value = 239; |
| 1423 | Local<v8::Integer> value_obj = v8::Integer::New(value); |
| 1424 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1425 | |
| 1426 | value_obj = v8::Integer::New(value, isolate); |
| 1427 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1428 | } |
| 1429 | |
| 1430 | |
| 1431 | THREADED_TEST(BigSmiInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1432 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1433 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1434 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1435 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1436 | int32_t value = i::Smi::kMaxValue; |
| 1437 | // We cannot add one to a Smi::kMaxValue without wrapping. |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1438 | if (i::SmiValuesAre31Bits()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1439 | CHECK(i::Smi::IsValid(value)); |
| 1440 | CHECK(!i::Smi::IsValid(value + 1)); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1441 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1442 | Local<v8::Integer> value_obj = v8::Integer::New(value); |
| 1443 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1444 | |
| 1445 | value_obj = v8::Integer::New(value, isolate); |
| 1446 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1447 | } |
| 1448 | } |
| 1449 | |
| 1450 | |
| 1451 | THREADED_TEST(BigInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1452 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1453 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1454 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1455 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1456 | // We cannot add one to a Smi::kMaxValue without wrapping. |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 1457 | if (i::SmiValuesAre31Bits()) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1458 | // The casts allow this to compile, even if Smi::kMaxValue is 2^31-1. |
| 1459 | // The code will not be run in that case, due to the "if" guard. |
| 1460 | int32_t value = |
| 1461 | static_cast<int32_t>(static_cast<uint32_t>(i::Smi::kMaxValue) + 1); |
| 1462 | CHECK(value > i::Smi::kMaxValue); |
| 1463 | CHECK(!i::Smi::IsValid(value)); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1464 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1465 | Local<v8::Integer> value_obj = v8::Integer::New(value); |
| 1466 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1467 | |
| 1468 | value_obj = v8::Integer::New(value, isolate); |
| 1469 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | |
| 1474 | THREADED_TEST(TinyUnsignedInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1475 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1476 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1477 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1478 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1479 | uint32_t value = 239; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1480 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1481 | Local<v8::Integer> value_obj = v8::Integer::NewFromUnsigned(value); |
| 1482 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1483 | |
| 1484 | value_obj = v8::Integer::NewFromUnsigned(value, isolate); |
| 1485 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | THREADED_TEST(BigUnsignedSmiInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1490 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1491 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1492 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1493 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1494 | uint32_t value = static_cast<uint32_t>(i::Smi::kMaxValue); |
| 1495 | CHECK(i::Smi::IsValid(value)); |
| 1496 | CHECK(!i::Smi::IsValid(value + 1)); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1497 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1498 | Local<v8::Integer> value_obj = v8::Integer::NewFromUnsigned(value); |
| 1499 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1500 | |
| 1501 | value_obj = v8::Integer::NewFromUnsigned(value, isolate); |
| 1502 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | THREADED_TEST(BigUnsignedInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1507 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1508 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1509 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1510 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1511 | uint32_t value = static_cast<uint32_t>(i::Smi::kMaxValue) + 1; |
| 1512 | CHECK(value > static_cast<uint32_t>(i::Smi::kMaxValue)); |
| 1513 | CHECK(!i::Smi::IsValid(value)); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1514 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1515 | Local<v8::Integer> value_obj = v8::Integer::NewFromUnsigned(value); |
| 1516 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1517 | |
| 1518 | value_obj = v8::Integer::NewFromUnsigned(value, isolate); |
| 1519 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | |
| 1523 | THREADED_TEST(OutOfSignedRangeUnsignedInteger) { |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1524 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1525 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1526 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1527 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1528 | uint32_t INT32_MAX_AS_UINT = (1U << 31) - 1; |
| 1529 | uint32_t value = INT32_MAX_AS_UINT + 1; |
| 1530 | CHECK(value > INT32_MAX_AS_UINT); // No overflow. |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1531 | |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1532 | Local<v8::Integer> value_obj = v8::Integer::NewFromUnsigned(value); |
| 1533 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 1534 | |
| 1535 | value_obj = v8::Integer::NewFromUnsigned(value, isolate); |
| 1536 | CHECK_EQ(static_cast<int64_t>(value), value_obj->Value()); |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
| 1539 | |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1540 | THREADED_TEST(IsNativeError) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1541 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1542 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1543 | v8::Handle<Value> syntax_error = CompileRun( |
| 1544 | "var out = 0; try { eval(\"#\"); } catch(x) { out = x; } out; "); |
| 1545 | CHECK(syntax_error->IsNativeError()); |
| 1546 | v8::Handle<Value> not_error = CompileRun("{a:42}"); |
| 1547 | CHECK(!not_error->IsNativeError()); |
| 1548 | v8::Handle<Value> not_object = CompileRun("42"); |
| 1549 | CHECK(!not_object->IsNativeError()); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | THREADED_TEST(StringObject) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1554 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1555 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1556 | v8::Handle<Value> boxed_string = CompileRun("new String(\"test\")"); |
| 1557 | CHECK(boxed_string->IsStringObject()); |
| 1558 | v8::Handle<Value> unboxed_string = CompileRun("\"test\""); |
| 1559 | CHECK(!unboxed_string->IsStringObject()); |
| 1560 | v8::Handle<Value> boxed_not_string = CompileRun("new Number(42)"); |
| 1561 | CHECK(!boxed_not_string->IsStringObject()); |
| 1562 | v8::Handle<Value> not_object = CompileRun("0"); |
| 1563 | CHECK(!not_object->IsStringObject()); |
| 1564 | v8::Handle<v8::StringObject> as_boxed = boxed_string.As<v8::StringObject>(); |
| 1565 | CHECK(!as_boxed.IsEmpty()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1566 | Local<v8::String> the_string = as_boxed->ValueOf(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1567 | CHECK(!the_string.IsEmpty()); |
| 1568 | ExpectObject("\"test\"", the_string); |
| 1569 | v8::Handle<v8::Value> new_boxed_string = v8::StringObject::New(the_string); |
| 1570 | CHECK(new_boxed_string->IsStringObject()); |
| 1571 | as_boxed = new_boxed_string.As<v8::StringObject>(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1572 | the_string = as_boxed->ValueOf(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1573 | CHECK(!the_string.IsEmpty()); |
| 1574 | ExpectObject("\"test\"", the_string); |
| 1575 | } |
| 1576 | |
| 1577 | |
| 1578 | THREADED_TEST(NumberObject) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1579 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1580 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1581 | v8::Handle<Value> boxed_number = CompileRun("new Number(42)"); |
| 1582 | CHECK(boxed_number->IsNumberObject()); |
| 1583 | v8::Handle<Value> unboxed_number = CompileRun("42"); |
| 1584 | CHECK(!unboxed_number->IsNumberObject()); |
| 1585 | v8::Handle<Value> boxed_not_number = CompileRun("new Boolean(false)"); |
| 1586 | CHECK(!boxed_not_number->IsNumberObject()); |
| 1587 | v8::Handle<v8::NumberObject> as_boxed = boxed_number.As<v8::NumberObject>(); |
| 1588 | CHECK(!as_boxed.IsEmpty()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1589 | double the_number = as_boxed->ValueOf(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1590 | CHECK_EQ(42.0, the_number); |
| 1591 | v8::Handle<v8::Value> new_boxed_number = v8::NumberObject::New(43); |
| 1592 | CHECK(new_boxed_number->IsNumberObject()); |
| 1593 | as_boxed = new_boxed_number.As<v8::NumberObject>(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1594 | the_number = as_boxed->ValueOf(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1595 | CHECK_EQ(43.0, the_number); |
| 1596 | } |
| 1597 | |
| 1598 | |
| 1599 | THREADED_TEST(BooleanObject) { |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1600 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1601 | v8::HandleScope scope(env->GetIsolate()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1602 | v8::Handle<Value> boxed_boolean = CompileRun("new Boolean(true)"); |
| 1603 | CHECK(boxed_boolean->IsBooleanObject()); |
| 1604 | v8::Handle<Value> unboxed_boolean = CompileRun("true"); |
| 1605 | CHECK(!unboxed_boolean->IsBooleanObject()); |
| 1606 | v8::Handle<Value> boxed_not_boolean = CompileRun("new Number(42)"); |
| 1607 | CHECK(!boxed_not_boolean->IsBooleanObject()); |
| 1608 | v8::Handle<v8::BooleanObject> as_boxed = |
| 1609 | boxed_boolean.As<v8::BooleanObject>(); |
| 1610 | CHECK(!as_boxed.IsEmpty()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1611 | bool the_boolean = as_boxed->ValueOf(); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1612 | CHECK_EQ(true, the_boolean); |
| 1613 | v8::Handle<v8::Value> boxed_true = v8::BooleanObject::New(true); |
| 1614 | v8::Handle<v8::Value> boxed_false = v8::BooleanObject::New(false); |
| 1615 | CHECK(boxed_true->IsBooleanObject()); |
| 1616 | CHECK(boxed_false->IsBooleanObject()); |
| 1617 | as_boxed = boxed_true.As<v8::BooleanObject>(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1618 | CHECK_EQ(true, as_boxed->ValueOf()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1619 | as_boxed = boxed_false.As<v8::BooleanObject>(); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 1620 | CHECK_EQ(false, as_boxed->ValueOf()); |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | THREADED_TEST(PrimitiveAndWrappedBooleans) { |
| 1625 | LocalContext env; |
| 1626 | v8::HandleScope scope(env->GetIsolate()); |
| 1627 | |
| 1628 | Local<Value> primitive_false = Boolean::New(false); |
| 1629 | CHECK(primitive_false->IsBoolean()); |
| 1630 | CHECK(!primitive_false->IsBooleanObject()); |
| 1631 | CHECK(!primitive_false->BooleanValue()); |
| 1632 | CHECK(!primitive_false->IsTrue()); |
| 1633 | CHECK(primitive_false->IsFalse()); |
| 1634 | |
| 1635 | Local<Value> false_value = BooleanObject::New(false); |
| 1636 | CHECK(!false_value->IsBoolean()); |
| 1637 | CHECK(false_value->IsBooleanObject()); |
| 1638 | CHECK(false_value->BooleanValue()); |
| 1639 | CHECK(!false_value->IsTrue()); |
| 1640 | CHECK(!false_value->IsFalse()); |
| 1641 | |
| 1642 | Local<BooleanObject> false_boolean_object = false_value.As<BooleanObject>(); |
| 1643 | CHECK(!false_boolean_object->IsBoolean()); |
| 1644 | CHECK(false_boolean_object->IsBooleanObject()); |
| 1645 | // TODO(svenpanne) Uncomment when BooleanObject::BooleanValue() is deleted. |
| 1646 | // CHECK(false_boolean_object->BooleanValue()); |
| 1647 | CHECK(!false_boolean_object->ValueOf()); |
| 1648 | CHECK(!false_boolean_object->IsTrue()); |
| 1649 | CHECK(!false_boolean_object->IsFalse()); |
| 1650 | |
| 1651 | Local<Value> primitive_true = Boolean::New(true); |
| 1652 | CHECK(primitive_true->IsBoolean()); |
| 1653 | CHECK(!primitive_true->IsBooleanObject()); |
| 1654 | CHECK(primitive_true->BooleanValue()); |
| 1655 | CHECK(primitive_true->IsTrue()); |
| 1656 | CHECK(!primitive_true->IsFalse()); |
| 1657 | |
| 1658 | Local<Value> true_value = BooleanObject::New(true); |
| 1659 | CHECK(!true_value->IsBoolean()); |
| 1660 | CHECK(true_value->IsBooleanObject()); |
| 1661 | CHECK(true_value->BooleanValue()); |
| 1662 | CHECK(!true_value->IsTrue()); |
| 1663 | CHECK(!true_value->IsFalse()); |
| 1664 | |
| 1665 | Local<BooleanObject> true_boolean_object = true_value.As<BooleanObject>(); |
| 1666 | CHECK(!true_boolean_object->IsBoolean()); |
| 1667 | CHECK(true_boolean_object->IsBooleanObject()); |
| 1668 | // TODO(svenpanne) Uncomment when BooleanObject::BooleanValue() is deleted. |
| 1669 | // CHECK(true_boolean_object->BooleanValue()); |
| 1670 | CHECK(true_boolean_object->ValueOf()); |
| 1671 | CHECK(!true_boolean_object->IsTrue()); |
| 1672 | CHECK(!true_boolean_object->IsFalse()); |
svenpanne@chromium.org | 84bcc55 | 2011-07-18 09:50:57 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
| 1675 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1676 | THREADED_TEST(Number) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1677 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1678 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1679 | double PI = 3.1415926; |
| 1680 | Local<v8::Number> pi_obj = v8::Number::New(PI); |
| 1681 | CHECK_EQ(PI, pi_obj->NumberValue()); |
| 1682 | } |
| 1683 | |
| 1684 | |
| 1685 | THREADED_TEST(ToNumber) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1686 | LocalContext env; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1687 | v8::Isolate* isolate = CcTest::isolate(); |
| 1688 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1689 | Local<String> str = v8_str("3.1415926"); |
| 1690 | CHECK_EQ(3.1415926, str->NumberValue()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1691 | v8::Handle<v8::Boolean> t = v8::True(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1692 | CHECK_EQ(1.0, t->NumberValue()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1693 | v8::Handle<v8::Boolean> f = v8::False(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1694 | CHECK_EQ(0.0, f->NumberValue()); |
| 1695 | } |
| 1696 | |
| 1697 | |
| 1698 | THREADED_TEST(Date) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1699 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1700 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1701 | double PI = 3.1415926; |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 1702 | Local<Value> date = v8::Date::New(PI); |
| 1703 | CHECK_EQ(3.0, date->NumberValue()); |
| 1704 | date.As<v8::Date>()->Set(v8_str("property"), v8::Integer::New(42)); |
| 1705 | CHECK_EQ(42, date.As<v8::Date>()->Get(v8_str("property"))->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1706 | } |
| 1707 | |
| 1708 | |
| 1709 | THREADED_TEST(Boolean) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1710 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 1711 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1712 | v8::Handle<v8::Boolean> t = v8::True(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1713 | CHECK(t->Value()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1714 | v8::Handle<v8::Boolean> f = v8::False(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1715 | CHECK(!f->Value()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1716 | v8::Handle<v8::Primitive> u = v8::Undefined(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1717 | CHECK(!u->BooleanValue()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 1718 | v8::Handle<v8::Primitive> n = v8::Null(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1719 | CHECK(!n->BooleanValue()); |
| 1720 | v8::Handle<String> str1 = v8_str(""); |
| 1721 | CHECK(!str1->BooleanValue()); |
| 1722 | v8::Handle<String> str2 = v8_str("x"); |
| 1723 | CHECK(str2->BooleanValue()); |
| 1724 | CHECK(!v8::Number::New(0)->BooleanValue()); |
| 1725 | CHECK(v8::Number::New(-1)->BooleanValue()); |
| 1726 | CHECK(v8::Number::New(1)->BooleanValue()); |
| 1727 | CHECK(v8::Number::New(42)->BooleanValue()); |
| 1728 | CHECK(!v8_compile("NaN")->Run()->BooleanValue()); |
| 1729 | } |
| 1730 | |
| 1731 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1732 | static void DummyCallHandler(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1733 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1734 | args.GetReturnValue().Set(v8_num(13.4)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1738 | static void GetM(Local<String> name, |
| 1739 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1740 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1741 | info.GetReturnValue().Set(v8_num(876)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | |
| 1745 | THREADED_TEST(GlobalPrototype) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1746 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1747 | v8::Handle<v8::FunctionTemplate> func_templ = v8::FunctionTemplate::New(); |
| 1748 | func_templ->PrototypeTemplate()->Set( |
| 1749 | "dummy", |
| 1750 | v8::FunctionTemplate::New(DummyCallHandler)); |
| 1751 | v8::Handle<ObjectTemplate> templ = func_templ->InstanceTemplate(); |
| 1752 | templ->Set("x", v8_num(200)); |
| 1753 | templ->SetAccessor(v8_str("m"), GetM); |
| 1754 | LocalContext env(0, templ); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 1755 | v8::Handle<Script> script(v8_compile("dummy()")); |
| 1756 | v8::Handle<Value> result(script->Run()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1757 | CHECK_EQ(13.4, result->NumberValue()); |
| 1758 | CHECK_EQ(200, v8_compile("x")->Run()->Int32Value()); |
| 1759 | CHECK_EQ(876, v8_compile("m")->Run()->Int32Value()); |
| 1760 | } |
| 1761 | |
| 1762 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1763 | THREADED_TEST(ObjectTemplate) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1764 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1765 | Local<ObjectTemplate> templ1 = ObjectTemplate::New(); |
| 1766 | templ1->Set("x", v8_num(10)); |
| 1767 | templ1->Set("y", v8_num(13)); |
| 1768 | LocalContext env; |
| 1769 | Local<v8::Object> instance1 = templ1->NewInstance(); |
| 1770 | env->Global()->Set(v8_str("p"), instance1); |
| 1771 | CHECK(v8_compile("(p.x == 10)")->Run()->BooleanValue()); |
| 1772 | CHECK(v8_compile("(p.y == 13)")->Run()->BooleanValue()); |
| 1773 | Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(); |
| 1774 | fun->PrototypeTemplate()->Set("nirk", v8_num(123)); |
| 1775 | Local<ObjectTemplate> templ2 = fun->InstanceTemplate(); |
| 1776 | templ2->Set("a", v8_num(12)); |
| 1777 | templ2->Set("b", templ1); |
| 1778 | Local<v8::Object> instance2 = templ2->NewInstance(); |
| 1779 | env->Global()->Set(v8_str("q"), instance2); |
| 1780 | CHECK(v8_compile("(q.nirk == 123)")->Run()->BooleanValue()); |
| 1781 | CHECK(v8_compile("(q.a == 12)")->Run()->BooleanValue()); |
| 1782 | CHECK(v8_compile("(q.b.x == 10)")->Run()->BooleanValue()); |
| 1783 | CHECK(v8_compile("(q.b.y == 13)")->Run()->BooleanValue()); |
| 1784 | } |
| 1785 | |
| 1786 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1787 | static void GetFlabby(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1788 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1789 | args.GetReturnValue().Set(v8_num(17.2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1790 | } |
| 1791 | |
| 1792 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1793 | static void GetKnurd(Local<String> property, |
| 1794 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1795 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1796 | info.GetReturnValue().Set(v8_num(15.2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1797 | } |
| 1798 | |
| 1799 | |
| 1800 | THREADED_TEST(DescriptorInheritance) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1801 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1802 | v8::Handle<v8::FunctionTemplate> super = v8::FunctionTemplate::New(); |
| 1803 | super->PrototypeTemplate()->Set("flabby", |
| 1804 | v8::FunctionTemplate::New(GetFlabby)); |
| 1805 | super->PrototypeTemplate()->Set("PI", v8_num(3.14)); |
| 1806 | |
| 1807 | super->InstanceTemplate()->SetAccessor(v8_str("knurd"), GetKnurd); |
| 1808 | |
| 1809 | v8::Handle<v8::FunctionTemplate> base1 = v8::FunctionTemplate::New(); |
| 1810 | base1->Inherit(super); |
| 1811 | base1->PrototypeTemplate()->Set("v1", v8_num(20.1)); |
| 1812 | |
| 1813 | v8::Handle<v8::FunctionTemplate> base2 = v8::FunctionTemplate::New(); |
| 1814 | base2->Inherit(super); |
| 1815 | base2->PrototypeTemplate()->Set("v2", v8_num(10.1)); |
| 1816 | |
| 1817 | LocalContext env; |
| 1818 | |
| 1819 | env->Global()->Set(v8_str("s"), super->GetFunction()); |
| 1820 | env->Global()->Set(v8_str("base1"), base1->GetFunction()); |
| 1821 | env->Global()->Set(v8_str("base2"), base2->GetFunction()); |
| 1822 | |
| 1823 | // Checks right __proto__ chain. |
| 1824 | CHECK(CompileRun("base1.prototype.__proto__ == s.prototype")->BooleanValue()); |
| 1825 | CHECK(CompileRun("base2.prototype.__proto__ == s.prototype")->BooleanValue()); |
| 1826 | |
| 1827 | CHECK(v8_compile("s.prototype.PI == 3.14")->Run()->BooleanValue()); |
| 1828 | |
| 1829 | // Instance accessor should not be visible on function object or its prototype |
| 1830 | CHECK(CompileRun("s.knurd == undefined")->BooleanValue()); |
| 1831 | CHECK(CompileRun("s.prototype.knurd == undefined")->BooleanValue()); |
| 1832 | CHECK(CompileRun("base1.prototype.knurd == undefined")->BooleanValue()); |
| 1833 | |
| 1834 | env->Global()->Set(v8_str("obj"), |
| 1835 | base1->GetFunction()->NewInstance()); |
| 1836 | CHECK_EQ(17.2, v8_compile("obj.flabby()")->Run()->NumberValue()); |
| 1837 | CHECK(v8_compile("'flabby' in obj")->Run()->BooleanValue()); |
| 1838 | CHECK_EQ(15.2, v8_compile("obj.knurd")->Run()->NumberValue()); |
| 1839 | CHECK(v8_compile("'knurd' in obj")->Run()->BooleanValue()); |
| 1840 | CHECK_EQ(20.1, v8_compile("obj.v1")->Run()->NumberValue()); |
| 1841 | |
| 1842 | env->Global()->Set(v8_str("obj2"), |
| 1843 | base2->GetFunction()->NewInstance()); |
| 1844 | CHECK_EQ(17.2, v8_compile("obj2.flabby()")->Run()->NumberValue()); |
| 1845 | CHECK(v8_compile("'flabby' in obj2")->Run()->BooleanValue()); |
| 1846 | CHECK_EQ(15.2, v8_compile("obj2.knurd")->Run()->NumberValue()); |
| 1847 | CHECK(v8_compile("'knurd' in obj2")->Run()->BooleanValue()); |
| 1848 | CHECK_EQ(10.1, v8_compile("obj2.v2")->Run()->NumberValue()); |
| 1849 | |
| 1850 | // base1 and base2 cannot cross reference to each's prototype |
| 1851 | CHECK(v8_compile("obj.v2")->Run()->IsUndefined()); |
| 1852 | CHECK(v8_compile("obj2.v1")->Run()->IsUndefined()); |
| 1853 | } |
| 1854 | |
| 1855 | |
| 1856 | int echo_named_call_count; |
| 1857 | |
| 1858 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1859 | static void EchoNamedProperty(Local<String> name, |
| 1860 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1861 | ApiTestFuzzer::Fuzz(); |
| 1862 | CHECK_EQ(v8_str("data"), info.Data()); |
| 1863 | echo_named_call_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1864 | info.GetReturnValue().Set(name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 1865 | } |
| 1866 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1867 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1868 | // Helper functions for Interceptor/Accessor interaction tests |
| 1869 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1870 | void SimpleAccessorGetter(Local<String> name, |
| 1871 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1872 | Handle<Object> self = info.This(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1873 | info.GetReturnValue().Set( |
| 1874 | self->Get(String::Concat(v8_str("accessor_"), name))); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | void SimpleAccessorSetter(Local<String> name, Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1878 | const v8::PropertyCallbackInfo<void>& info) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1879 | Handle<Object> self = info.This(); |
| 1880 | self->Set(String::Concat(v8_str("accessor_"), name), value); |
| 1881 | } |
| 1882 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1883 | void EmptyInterceptorGetter(Local<String> name, |
| 1884 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1885 | } |
| 1886 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1887 | void EmptyInterceptorSetter(Local<String> name, |
| 1888 | Local<Value> value, |
| 1889 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1892 | void InterceptorGetter(Local<String> name, |
| 1893 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1894 | // Intercept names that start with 'interceptor_'. |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 1895 | String::Utf8Value utf8(name); |
| 1896 | char* name_str = *utf8; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1897 | char prefix[] = "interceptor_"; |
| 1898 | int i; |
| 1899 | for (i = 0; name_str[i] && prefix[i]; ++i) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1900 | if (name_str[i] != prefix[i]) return; |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1901 | } |
| 1902 | Handle<Object> self = info.This(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1903 | info.GetReturnValue().Set(self->GetHiddenValue(v8_str(name_str + i))); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1906 | void InterceptorSetter(Local<String> name, |
| 1907 | Local<Value> value, |
| 1908 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 1909 | // Intercept accesses that set certain integer values, for which the name does |
| 1910 | // not start with 'accessor_'. |
| 1911 | String::Utf8Value utf8(name); |
| 1912 | char* name_str = *utf8; |
| 1913 | char prefix[] = "accessor_"; |
| 1914 | int i; |
| 1915 | for (i = 0; name_str[i] && prefix[i]; ++i) { |
| 1916 | if (name_str[i] != prefix[i]) break; |
| 1917 | } |
| 1918 | if (!prefix[i]) return; |
| 1919 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1920 | if (value->IsInt32() && value->Int32Value() < 10000) { |
| 1921 | Handle<Object> self = info.This(); |
| 1922 | self->SetHiddenValue(name, value); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1923 | info.GetReturnValue().Set(value); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1924 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1925 | } |
| 1926 | |
| 1927 | void AddAccessor(Handle<FunctionTemplate> templ, |
| 1928 | Handle<String> name, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1929 | v8::AccessorGetterCallback getter, |
| 1930 | v8::AccessorSetterCallback setter) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1931 | templ->PrototypeTemplate()->SetAccessor(name, getter, setter); |
| 1932 | } |
| 1933 | |
| 1934 | void AddInterceptor(Handle<FunctionTemplate> templ, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 1935 | v8::NamedPropertyGetterCallback getter, |
| 1936 | v8::NamedPropertySetterCallback setter) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1937 | templ->InstanceTemplate()->SetNamedPropertyHandler(getter, setter); |
| 1938 | } |
| 1939 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1940 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1941 | THREADED_TEST(EmptyInterceptorDoesNotShadowAccessors) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1942 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1943 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 1944 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 1945 | child->Inherit(parent); |
| 1946 | AddAccessor(parent, v8_str("age"), |
| 1947 | SimpleAccessorGetter, SimpleAccessorSetter); |
| 1948 | AddInterceptor(child, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 1949 | LocalContext env; |
| 1950 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 1951 | CompileRun("var child = new Child;" |
| 1952 | "child.age = 10;"); |
| 1953 | ExpectBoolean("child.hasOwnProperty('age')", false); |
| 1954 | ExpectInt32("child.age", 10); |
| 1955 | ExpectInt32("child.accessor_age", 10); |
| 1956 | } |
| 1957 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1958 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1959 | THREADED_TEST(EmptyInterceptorDoesNotShadowJSAccessors) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1960 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1961 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 1962 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 1963 | child->Inherit(parent); |
| 1964 | AddInterceptor(child, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 1965 | LocalContext env; |
| 1966 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 1967 | CompileRun("var child = new Child;" |
| 1968 | "var parent = child.__proto__;" |
| 1969 | "Object.defineProperty(parent, 'age', " |
| 1970 | " {get: function(){ return this.accessor_age; }, " |
| 1971 | " set: function(v){ this.accessor_age = v; }, " |
| 1972 | " enumerable: true, configurable: true});" |
| 1973 | "child.age = 10;"); |
| 1974 | ExpectBoolean("child.hasOwnProperty('age')", false); |
| 1975 | ExpectInt32("child.age", 10); |
| 1976 | ExpectInt32("child.accessor_age", 10); |
| 1977 | } |
| 1978 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1979 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1980 | THREADED_TEST(EmptyInterceptorDoesNotAffectJSProperties) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 1981 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 1982 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 1983 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 1984 | child->Inherit(parent); |
| 1985 | AddInterceptor(child, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 1986 | LocalContext env; |
| 1987 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 1988 | CompileRun("var child = new Child;" |
| 1989 | "var parent = child.__proto__;" |
| 1990 | "parent.name = 'Alice';"); |
| 1991 | ExpectBoolean("child.hasOwnProperty('name')", false); |
| 1992 | ExpectString("child.name", "Alice"); |
| 1993 | CompileRun("child.name = 'Bob';"); |
| 1994 | ExpectString("child.name", "Bob"); |
| 1995 | ExpectBoolean("child.hasOwnProperty('name')", true); |
| 1996 | ExpectString("parent.name", "Alice"); |
| 1997 | } |
| 1998 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 1999 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2000 | THREADED_TEST(SwitchFromInterceptorToAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2001 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2002 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 2003 | AddAccessor(templ, v8_str("age"), |
| 2004 | SimpleAccessorGetter, SimpleAccessorSetter); |
| 2005 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 2006 | LocalContext env; |
| 2007 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 2008 | CompileRun("var obj = new Obj;" |
| 2009 | "function setAge(i){ obj.age = i; };" |
| 2010 | "for(var i = 0; i <= 10000; i++) setAge(i);"); |
| 2011 | // All i < 10000 go to the interceptor. |
| 2012 | ExpectInt32("obj.interceptor_age", 9999); |
| 2013 | // The last i goes to the accessor. |
| 2014 | ExpectInt32("obj.accessor_age", 10000); |
| 2015 | } |
| 2016 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2017 | |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2018 | THREADED_TEST(SwitchFromAccessorToInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2019 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2020 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 2021 | AddAccessor(templ, v8_str("age"), |
| 2022 | SimpleAccessorGetter, SimpleAccessorSetter); |
| 2023 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 2024 | LocalContext env; |
| 2025 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 2026 | CompileRun("var obj = new Obj;" |
| 2027 | "function setAge(i){ obj.age = i; };" |
| 2028 | "for(var i = 20000; i >= 9999; i--) setAge(i);"); |
| 2029 | // All i >= 10000 go to the accessor. |
| 2030 | ExpectInt32("obj.accessor_age", 10000); |
| 2031 | // The last i goes to the interceptor. |
| 2032 | ExpectInt32("obj.interceptor_age", 9999); |
| 2033 | } |
| 2034 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2035 | |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2036 | THREADED_TEST(SwitchFromInterceptorToAccessorWithInheritance) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2037 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2038 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 2039 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 2040 | child->Inherit(parent); |
| 2041 | AddAccessor(parent, v8_str("age"), |
| 2042 | SimpleAccessorGetter, SimpleAccessorSetter); |
| 2043 | AddInterceptor(child, InterceptorGetter, InterceptorSetter); |
| 2044 | LocalContext env; |
| 2045 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 2046 | CompileRun("var child = new Child;" |
| 2047 | "function setAge(i){ child.age = i; };" |
| 2048 | "for(var i = 0; i <= 10000; i++) setAge(i);"); |
| 2049 | // All i < 10000 go to the interceptor. |
| 2050 | ExpectInt32("child.interceptor_age", 9999); |
| 2051 | // The last i goes to the accessor. |
| 2052 | ExpectInt32("child.accessor_age", 10000); |
| 2053 | } |
| 2054 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2055 | |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2056 | THREADED_TEST(SwitchFromAccessorToInterceptorWithInheritance) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2057 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2058 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 2059 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 2060 | child->Inherit(parent); |
| 2061 | AddAccessor(parent, v8_str("age"), |
| 2062 | SimpleAccessorGetter, SimpleAccessorSetter); |
| 2063 | AddInterceptor(child, InterceptorGetter, InterceptorSetter); |
| 2064 | LocalContext env; |
| 2065 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 2066 | CompileRun("var child = new Child;" |
| 2067 | "function setAge(i){ child.age = i; };" |
| 2068 | "for(var i = 20000; i >= 9999; i--) setAge(i);"); |
| 2069 | // All i >= 10000 go to the accessor. |
| 2070 | ExpectInt32("child.accessor_age", 10000); |
| 2071 | // The last i goes to the interceptor. |
| 2072 | ExpectInt32("child.interceptor_age", 9999); |
| 2073 | } |
| 2074 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2075 | |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2076 | THREADED_TEST(SwitchFromInterceptorToJSAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2077 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2078 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 2079 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 2080 | LocalContext env; |
| 2081 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 2082 | CompileRun("var obj = new Obj;" |
| 2083 | "function setter(i) { this.accessor_age = i; };" |
| 2084 | "function getter() { return this.accessor_age; };" |
| 2085 | "function setAge(i) { obj.age = i; };" |
| 2086 | "Object.defineProperty(obj, 'age', { get:getter, set:setter });" |
| 2087 | "for(var i = 0; i <= 10000; i++) setAge(i);"); |
| 2088 | // All i < 10000 go to the interceptor. |
| 2089 | ExpectInt32("obj.interceptor_age", 9999); |
| 2090 | // The last i goes to the JavaScript accessor. |
| 2091 | ExpectInt32("obj.accessor_age", 10000); |
| 2092 | // The installed JavaScript getter is still intact. |
| 2093 | // This last part is a regression test for issue 1651 and relies on the fact |
| 2094 | // that both interceptor and accessor are being installed on the same object. |
| 2095 | ExpectInt32("obj.age", 10000); |
| 2096 | ExpectBoolean("obj.hasOwnProperty('age')", true); |
| 2097 | ExpectUndefined("Object.getOwnPropertyDescriptor(obj, 'age').value"); |
| 2098 | } |
| 2099 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2100 | |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2101 | THREADED_TEST(SwitchFromJSAccessorToInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2102 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | bf0c820 | 2011-12-27 10:09:42 +0000 | [diff] [blame] | 2103 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 2104 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 2105 | LocalContext env; |
| 2106 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 2107 | CompileRun("var obj = new Obj;" |
| 2108 | "function setter(i) { this.accessor_age = i; };" |
| 2109 | "function getter() { return this.accessor_age; };" |
| 2110 | "function setAge(i) { obj.age = i; };" |
| 2111 | "Object.defineProperty(obj, 'age', { get:getter, set:setter });" |
| 2112 | "for(var i = 20000; i >= 9999; i--) setAge(i);"); |
| 2113 | // All i >= 10000 go to the accessor. |
| 2114 | ExpectInt32("obj.accessor_age", 10000); |
| 2115 | // The last i goes to the interceptor. |
| 2116 | ExpectInt32("obj.interceptor_age", 9999); |
| 2117 | // The installed JavaScript getter is still intact. |
| 2118 | // This last part is a regression test for issue 1651 and relies on the fact |
| 2119 | // that both interceptor and accessor are being installed on the same object. |
| 2120 | ExpectInt32("obj.age", 10000); |
| 2121 | ExpectBoolean("obj.hasOwnProperty('age')", true); |
| 2122 | ExpectUndefined("Object.getOwnPropertyDescriptor(obj, 'age').value"); |
| 2123 | } |
| 2124 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2125 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2126 | THREADED_TEST(SwitchFromInterceptorToProperty) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2127 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2128 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 2129 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 2130 | child->Inherit(parent); |
| 2131 | AddInterceptor(child, InterceptorGetter, InterceptorSetter); |
| 2132 | LocalContext env; |
| 2133 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 2134 | CompileRun("var child = new Child;" |
| 2135 | "function setAge(i){ child.age = i; };" |
| 2136 | "for(var i = 0; i <= 10000; i++) setAge(i);"); |
| 2137 | // All i < 10000 go to the interceptor. |
| 2138 | ExpectInt32("child.interceptor_age", 9999); |
| 2139 | // The last i goes to child's own property. |
| 2140 | ExpectInt32("child.age", 10000); |
| 2141 | } |
| 2142 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2143 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2144 | THREADED_TEST(SwitchFromPropertyToInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2145 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 2146 | Handle<FunctionTemplate> parent = FunctionTemplate::New(); |
| 2147 | Handle<FunctionTemplate> child = FunctionTemplate::New(); |
| 2148 | child->Inherit(parent); |
| 2149 | AddInterceptor(child, InterceptorGetter, InterceptorSetter); |
| 2150 | LocalContext env; |
| 2151 | env->Global()->Set(v8_str("Child"), child->GetFunction()); |
| 2152 | CompileRun("var child = new Child;" |
| 2153 | "function setAge(i){ child.age = i; };" |
| 2154 | "for(var i = 20000; i >= 9999; i--) setAge(i);"); |
| 2155 | // All i >= 10000 go to child's own property. |
| 2156 | ExpectInt32("child.age", 10000); |
| 2157 | // The last i goes to the interceptor. |
| 2158 | ExpectInt32("child.interceptor_age", 9999); |
| 2159 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2160 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2161 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2162 | THREADED_TEST(NamedPropertyHandlerGetter) { |
| 2163 | echo_named_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2164 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2165 | v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 2166 | templ->InstanceTemplate()->SetNamedPropertyHandler(EchoNamedProperty, |
| 2167 | 0, 0, 0, 0, |
| 2168 | v8_str("data")); |
| 2169 | LocalContext env; |
| 2170 | env->Global()->Set(v8_str("obj"), |
| 2171 | templ->GetFunction()->NewInstance()); |
| 2172 | CHECK_EQ(echo_named_call_count, 0); |
| 2173 | v8_compile("obj.x")->Run(); |
| 2174 | CHECK_EQ(echo_named_call_count, 1); |
| 2175 | const char* code = "var str = 'oddle'; obj[str] + obj.poddle;"; |
| 2176 | v8::Handle<Value> str = CompileRun(code); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 2177 | String::Utf8Value value(str); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2178 | CHECK_EQ(*value, "oddlepoddle"); |
| 2179 | // Check default behavior |
| 2180 | CHECK_EQ(v8_compile("obj.flob = 10;")->Run()->Int32Value(), 10); |
| 2181 | CHECK(v8_compile("'myProperty' in obj")->Run()->BooleanValue()); |
| 2182 | CHECK(v8_compile("delete obj.myProperty")->Run()->BooleanValue()); |
| 2183 | } |
| 2184 | |
| 2185 | |
| 2186 | int echo_indexed_call_count = 0; |
| 2187 | |
| 2188 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2189 | static void EchoIndexedProperty( |
| 2190 | uint32_t index, |
| 2191 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2192 | ApiTestFuzzer::Fuzz(); |
| 2193 | CHECK_EQ(v8_num(637), info.Data()); |
| 2194 | echo_indexed_call_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2195 | info.GetReturnValue().Set(v8_num(index)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | |
| 2199 | THREADED_TEST(IndexedPropertyHandlerGetter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2200 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2201 | v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 2202 | templ->InstanceTemplate()->SetIndexedPropertyHandler(EchoIndexedProperty, |
| 2203 | 0, 0, 0, 0, |
| 2204 | v8_num(637)); |
| 2205 | LocalContext env; |
| 2206 | env->Global()->Set(v8_str("obj"), |
| 2207 | templ->GetFunction()->NewInstance()); |
| 2208 | Local<Script> script = v8_compile("obj[900]"); |
| 2209 | CHECK_EQ(script->Run()->Int32Value(), 900); |
| 2210 | } |
| 2211 | |
| 2212 | |
| 2213 | v8::Handle<v8::Object> bottom; |
| 2214 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2215 | static void CheckThisIndexedPropertyHandler( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2216 | uint32_t index, |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2217 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2218 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisIndexedPropertyHandler)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2219 | ApiTestFuzzer::Fuzz(); |
| 2220 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2223 | static void CheckThisNamedPropertyHandler( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2224 | Local<String> name, |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2225 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2226 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisNamedPropertyHandler)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2227 | ApiTestFuzzer::Fuzz(); |
| 2228 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2229 | } |
| 2230 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2231 | void CheckThisIndexedPropertySetter( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2232 | uint32_t index, |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2233 | Local<Value> value, |
| 2234 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2235 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisIndexedPropertySetter)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2236 | ApiTestFuzzer::Fuzz(); |
| 2237 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2241 | void CheckThisNamedPropertySetter( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2242 | Local<String> property, |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2243 | Local<Value> value, |
| 2244 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2245 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisNamedPropertySetter)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2246 | ApiTestFuzzer::Fuzz(); |
| 2247 | CHECK(info.This()->Equals(bottom)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | void CheckThisIndexedPropertyQuery( |
| 2251 | uint32_t index, |
| 2252 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2253 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisIndexedPropertyQuery)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2254 | ApiTestFuzzer::Fuzz(); |
| 2255 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2259 | void CheckThisNamedPropertyQuery( |
| 2260 | Local<String> property, |
| 2261 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2262 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisNamedPropertyQuery)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2263 | ApiTestFuzzer::Fuzz(); |
| 2264 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2268 | void CheckThisIndexedPropertyDeleter( |
| 2269 | uint32_t index, |
| 2270 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2271 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisIndexedPropertyDeleter)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2272 | ApiTestFuzzer::Fuzz(); |
| 2273 | CHECK(info.This()->Equals(bottom)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | |
| 2277 | void CheckThisNamedPropertyDeleter( |
| 2278 | Local<String> property, |
| 2279 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2280 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisNamedPropertyDeleter)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2281 | ApiTestFuzzer::Fuzz(); |
| 2282 | CHECK(info.This()->Equals(bottom)); |
| 2283 | } |
| 2284 | |
| 2285 | |
| 2286 | void CheckThisIndexedPropertyEnumerator( |
| 2287 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2288 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisIndexedPropertyEnumerator)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2289 | ApiTestFuzzer::Fuzz(); |
| 2290 | CHECK(info.This()->Equals(bottom)); |
| 2291 | } |
| 2292 | |
| 2293 | |
| 2294 | void CheckThisNamedPropertyEnumerator( |
| 2295 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 2296 | CheckReturnValue(info, FUNCTION_ADDR(CheckThisNamedPropertyEnumerator)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 2297 | ApiTestFuzzer::Fuzz(); |
| 2298 | CHECK(info.This()->Equals(bottom)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2302 | THREADED_PROFILED_TEST(PropertyHandlerInPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2303 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2304 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2305 | |
| 2306 | // Set up a prototype chain with three interceptors. |
| 2307 | v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 2308 | templ->InstanceTemplate()->SetIndexedPropertyHandler( |
| 2309 | CheckThisIndexedPropertyHandler, |
| 2310 | CheckThisIndexedPropertySetter, |
| 2311 | CheckThisIndexedPropertyQuery, |
| 2312 | CheckThisIndexedPropertyDeleter, |
| 2313 | CheckThisIndexedPropertyEnumerator); |
| 2314 | |
| 2315 | templ->InstanceTemplate()->SetNamedPropertyHandler( |
| 2316 | CheckThisNamedPropertyHandler, |
| 2317 | CheckThisNamedPropertySetter, |
| 2318 | CheckThisNamedPropertyQuery, |
| 2319 | CheckThisNamedPropertyDeleter, |
| 2320 | CheckThisNamedPropertyEnumerator); |
| 2321 | |
| 2322 | bottom = templ->GetFunction()->NewInstance(); |
| 2323 | Local<v8::Object> top = templ->GetFunction()->NewInstance(); |
| 2324 | Local<v8::Object> middle = templ->GetFunction()->NewInstance(); |
| 2325 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 2326 | bottom->SetPrototype(middle); |
| 2327 | middle->SetPrototype(top); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2328 | env->Global()->Set(v8_str("obj"), bottom); |
| 2329 | |
| 2330 | // Indexed and named get. |
| 2331 | Script::Compile(v8_str("obj[0]"))->Run(); |
| 2332 | Script::Compile(v8_str("obj.x"))->Run(); |
| 2333 | |
| 2334 | // Indexed and named set. |
| 2335 | Script::Compile(v8_str("obj[1] = 42"))->Run(); |
| 2336 | Script::Compile(v8_str("obj.y = 42"))->Run(); |
| 2337 | |
| 2338 | // Indexed and named query. |
| 2339 | Script::Compile(v8_str("0 in obj"))->Run(); |
| 2340 | Script::Compile(v8_str("'x' in obj"))->Run(); |
| 2341 | |
| 2342 | // Indexed and named deleter. |
| 2343 | Script::Compile(v8_str("delete obj[0]"))->Run(); |
| 2344 | Script::Compile(v8_str("delete obj.x"))->Run(); |
| 2345 | |
| 2346 | // Enumerators. |
| 2347 | Script::Compile(v8_str("for (var p in obj) ;"))->Run(); |
| 2348 | } |
| 2349 | |
| 2350 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2351 | static void PrePropertyHandlerGet( |
| 2352 | Local<String> key, |
| 2353 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2354 | ApiTestFuzzer::Fuzz(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2355 | if (v8_str("pre")->Equals(key)) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2356 | info.GetReturnValue().Set(v8_str("PrePropertyHandler: pre")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2357 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2358 | } |
| 2359 | |
| 2360 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2361 | static void PrePropertyHandlerQuery( |
| 2362 | Local<String> key, |
| 2363 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2364 | if (v8_str("pre")->Equals(key)) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2365 | info.GetReturnValue().Set(static_cast<int32_t>(v8::None)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2366 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | |
| 2370 | THREADED_TEST(PrePropertyHandler) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2371 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2372 | v8::Handle<v8::FunctionTemplate> desc = v8::FunctionTemplate::New(); |
| 2373 | desc->InstanceTemplate()->SetNamedPropertyHandler(PrePropertyHandlerGet, |
| 2374 | 0, |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 2375 | PrePropertyHandlerQuery); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2376 | LocalContext env(NULL, desc->InstanceTemplate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2377 | Script::Compile(v8_str( |
| 2378 | "var pre = 'Object: pre'; var on = 'Object: on';"))->Run(); |
| 2379 | v8::Handle<Value> result_pre = Script::Compile(v8_str("pre"))->Run(); |
| 2380 | CHECK_EQ(v8_str("PrePropertyHandler: pre"), result_pre); |
| 2381 | v8::Handle<Value> result_on = Script::Compile(v8_str("on"))->Run(); |
| 2382 | CHECK_EQ(v8_str("Object: on"), result_on); |
| 2383 | v8::Handle<Value> result_post = Script::Compile(v8_str("post"))->Run(); |
| 2384 | CHECK(result_post.IsEmpty()); |
| 2385 | } |
| 2386 | |
| 2387 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2388 | THREADED_TEST(UndefinedIsNotEnumerable) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2389 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2390 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 2391 | v8::Handle<Value> result = Script::Compile(v8_str( |
| 2392 | "this.propertyIsEnumerable(undefined)"))->Run(); |
| 2393 | CHECK(result->IsFalse()); |
| 2394 | } |
| 2395 | |
| 2396 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2397 | v8::Handle<Script> call_recursively_script; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 2398 | static const int kTargetRecursionDepth = 200; // near maximum |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2399 | |
| 2400 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2401 | static void CallScriptRecursivelyCall( |
| 2402 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2403 | ApiTestFuzzer::Fuzz(); |
| 2404 | int depth = args.This()->Get(v8_str("depth"))->Int32Value(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2405 | if (depth == kTargetRecursionDepth) return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2406 | args.This()->Set(v8_str("depth"), v8::Integer::New(depth + 1)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2407 | args.GetReturnValue().Set(call_recursively_script->Run()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2411 | static void CallFunctionRecursivelyCall( |
| 2412 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2413 | ApiTestFuzzer::Fuzz(); |
| 2414 | int depth = args.This()->Get(v8_str("depth"))->Int32Value(); |
| 2415 | if (depth == kTargetRecursionDepth) { |
| 2416 | printf("[depth = %d]\n", depth); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2417 | return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2418 | } |
| 2419 | args.This()->Set(v8_str("depth"), v8::Integer::New(depth + 1)); |
| 2420 | v8::Handle<Value> function = |
| 2421 | args.This()->Get(v8_str("callFunctionRecursively")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2422 | args.GetReturnValue().Set( |
| 2423 | function.As<Function>()->Call(args.This(), 0, NULL)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2424 | } |
| 2425 | |
| 2426 | |
| 2427 | THREADED_TEST(DeepCrossLanguageRecursion) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2428 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2429 | v8::Handle<v8::ObjectTemplate> global = ObjectTemplate::New(); |
| 2430 | global->Set(v8_str("callScriptRecursively"), |
| 2431 | v8::FunctionTemplate::New(CallScriptRecursivelyCall)); |
| 2432 | global->Set(v8_str("callFunctionRecursively"), |
| 2433 | v8::FunctionTemplate::New(CallFunctionRecursivelyCall)); |
| 2434 | LocalContext env(NULL, global); |
| 2435 | |
| 2436 | env->Global()->Set(v8_str("depth"), v8::Integer::New(0)); |
| 2437 | call_recursively_script = v8_compile("callScriptRecursively()"); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 2438 | call_recursively_script->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2439 | call_recursively_script = v8::Handle<Script>(); |
| 2440 | |
| 2441 | env->Global()->Set(v8_str("depth"), v8::Integer::New(0)); |
| 2442 | Script::Compile(v8_str("callFunctionRecursively()"))->Run(); |
| 2443 | } |
| 2444 | |
| 2445 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2446 | static void ThrowingPropertyHandlerGet( |
| 2447 | Local<String> key, |
| 2448 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2449 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 2450 | info.GetReturnValue().Set(info.GetIsolate()->ThrowException(key)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
| 2453 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2454 | static void ThrowingPropertyHandlerSet( |
| 2455 | Local<String> key, |
| 2456 | Local<Value>, |
| 2457 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 2458 | info.GetIsolate()->ThrowException(key); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2459 | info.GetReturnValue().SetUndefined(); // not the same as empty handle |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | |
| 2463 | THREADED_TEST(CallbackExceptionRegression) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2464 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2465 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 2466 | obj->SetNamedPropertyHandler(ThrowingPropertyHandlerGet, |
| 2467 | ThrowingPropertyHandlerSet); |
| 2468 | LocalContext env; |
| 2469 | env->Global()->Set(v8_str("obj"), obj->NewInstance()); |
| 2470 | v8::Handle<Value> otto = Script::Compile(v8_str( |
| 2471 | "try { with (obj) { otto; } } catch (e) { e; }"))->Run(); |
| 2472 | CHECK_EQ(v8_str("otto"), otto); |
| 2473 | v8::Handle<Value> netto = Script::Compile(v8_str( |
| 2474 | "try { with (obj) { netto = 4; } } catch (e) { e; }"))->Run(); |
| 2475 | CHECK_EQ(v8_str("netto"), netto); |
| 2476 | } |
| 2477 | |
| 2478 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2479 | THREADED_TEST(FunctionPrototype) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2480 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2481 | Local<v8::FunctionTemplate> Foo = v8::FunctionTemplate::New(); |
| 2482 | Foo->PrototypeTemplate()->Set(v8_str("plak"), v8_num(321)); |
| 2483 | LocalContext env; |
| 2484 | env->Global()->Set(v8_str("Foo"), Foo->GetFunction()); |
| 2485 | Local<Script> script = Script::Compile(v8_str("Foo.prototype.plak")); |
| 2486 | CHECK_EQ(script->Run()->Int32Value(), 321); |
| 2487 | } |
| 2488 | |
| 2489 | |
| 2490 | THREADED_TEST(InternalFields) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2491 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2492 | v8::HandleScope scope(env->GetIsolate()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 2493 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2494 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 2495 | Local<v8::ObjectTemplate> instance_templ = templ->InstanceTemplate(); |
| 2496 | instance_templ->SetInternalFieldCount(1); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 2497 | Local<v8::Object> obj = templ->GetFunction()->NewInstance(); |
| 2498 | CHECK_EQ(1, obj->InternalFieldCount()); |
| 2499 | CHECK(obj->GetInternalField(0)->IsUndefined()); |
| 2500 | obj->SetInternalField(0, v8_num(17)); |
| 2501 | CHECK_EQ(17, obj->GetInternalField(0)->Int32Value()); |
| 2502 | } |
| 2503 | |
| 2504 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2505 | THREADED_TEST(GlobalObjectInternalFields) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2506 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 2507 | Local<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 2508 | global_template->SetInternalFieldCount(1); |
| 2509 | LocalContext env(NULL, global_template); |
| 2510 | v8::Handle<v8::Object> global_proxy = env->Global(); |
| 2511 | v8::Handle<v8::Object> global = global_proxy->GetPrototype().As<v8::Object>(); |
| 2512 | CHECK_EQ(1, global->InternalFieldCount()); |
| 2513 | CHECK(global->GetInternalField(0)->IsUndefined()); |
| 2514 | global->SetInternalField(0, v8_num(17)); |
| 2515 | CHECK_EQ(17, global->GetInternalField(0)->Int32Value()); |
| 2516 | } |
| 2517 | |
| 2518 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 2519 | THREADED_TEST(GlobalObjectHasRealIndexedProperty) { |
| 2520 | LocalContext env; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2521 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 2522 | |
| 2523 | v8::Local<v8::Object> global = env->Global(); |
| 2524 | global->Set(0, v8::String::New("value")); |
| 2525 | CHECK(global->HasRealIndexedProperty(0)); |
| 2526 | } |
| 2527 | |
| 2528 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2529 | static void CheckAlignedPointerInInternalField(Handle<v8::Object> obj, |
| 2530 | void* value) { |
| 2531 | CHECK_EQ(0, static_cast<int>(reinterpret_cast<uintptr_t>(value) & 0x1)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 2532 | obj->SetAlignedPointerInInternalField(0, value); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2533 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 2534 | CHECK_EQ(value, obj->GetAlignedPointerFromInternalField(0)); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2535 | } |
| 2536 | |
| 2537 | |
| 2538 | THREADED_TEST(InternalFieldsAlignedPointers) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2539 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2540 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2541 | |
| 2542 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 2543 | Local<v8::ObjectTemplate> instance_templ = templ->InstanceTemplate(); |
| 2544 | instance_templ->SetInternalFieldCount(1); |
| 2545 | Local<v8::Object> obj = templ->GetFunction()->NewInstance(); |
| 2546 | CHECK_EQ(1, obj->InternalFieldCount()); |
| 2547 | |
| 2548 | CheckAlignedPointerInInternalField(obj, NULL); |
| 2549 | |
| 2550 | int* heap_allocated = new int[100]; |
| 2551 | CheckAlignedPointerInInternalField(obj, heap_allocated); |
| 2552 | delete[] heap_allocated; |
| 2553 | |
| 2554 | int stack_allocated[100]; |
| 2555 | CheckAlignedPointerInInternalField(obj, stack_allocated); |
| 2556 | |
| 2557 | void* huge = reinterpret_cast<void*>(~static_cast<uintptr_t>(1)); |
| 2558 | CheckAlignedPointerInInternalField(obj, huge); |
| 2559 | } |
| 2560 | |
| 2561 | |
| 2562 | static void CheckAlignedPointerInEmbedderData(LocalContext* env, |
| 2563 | int index, |
| 2564 | void* value) { |
| 2565 | CHECK_EQ(0, static_cast<int>(reinterpret_cast<uintptr_t>(value) & 0x1)); |
| 2566 | (*env)->SetAlignedPointerInEmbedderData(index, value); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2567 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2568 | CHECK_EQ(value, (*env)->GetAlignedPointerFromEmbedderData(index)); |
| 2569 | } |
| 2570 | |
| 2571 | |
| 2572 | static void* AlignedTestPointer(int i) { |
| 2573 | return reinterpret_cast<void*>(i * 1234); |
| 2574 | } |
| 2575 | |
| 2576 | |
| 2577 | THREADED_TEST(EmbedderDataAlignedPointers) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2578 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2579 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2580 | |
| 2581 | CheckAlignedPointerInEmbedderData(&env, 0, NULL); |
| 2582 | |
| 2583 | int* heap_allocated = new int[100]; |
| 2584 | CheckAlignedPointerInEmbedderData(&env, 1, heap_allocated); |
| 2585 | delete[] heap_allocated; |
| 2586 | |
| 2587 | int stack_allocated[100]; |
| 2588 | CheckAlignedPointerInEmbedderData(&env, 2, stack_allocated); |
| 2589 | |
| 2590 | void* huge = reinterpret_cast<void*>(~static_cast<uintptr_t>(1)); |
| 2591 | CheckAlignedPointerInEmbedderData(&env, 3, huge); |
| 2592 | |
| 2593 | // Test growing of the embedder data's backing store. |
| 2594 | for (int i = 0; i < 100; i++) { |
| 2595 | env->SetAlignedPointerInEmbedderData(i, AlignedTestPointer(i)); |
| 2596 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2597 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2598 | for (int i = 0; i < 100; i++) { |
| 2599 | CHECK_EQ(AlignedTestPointer(i), env->GetAlignedPointerFromEmbedderData(i)); |
| 2600 | } |
| 2601 | } |
| 2602 | |
| 2603 | |
| 2604 | static void CheckEmbedderData(LocalContext* env, |
| 2605 | int index, |
| 2606 | v8::Handle<Value> data) { |
| 2607 | (*env)->SetEmbedderData(index, data); |
| 2608 | CHECK((*env)->GetEmbedderData(index)->StrictEquals(data)); |
| 2609 | } |
| 2610 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2611 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2612 | THREADED_TEST(EmbedderData) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2613 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2614 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 2615 | |
| 2616 | CheckEmbedderData(&env, 3, v8::String::New("The quick brown fox jumps")); |
| 2617 | CheckEmbedderData(&env, 2, v8::String::New("over the lazy dog.")); |
| 2618 | CheckEmbedderData(&env, 1, v8::Number::New(1.2345)); |
| 2619 | CheckEmbedderData(&env, 0, v8::Boolean::New(true)); |
| 2620 | } |
| 2621 | |
| 2622 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2623 | THREADED_TEST(IdentityHash) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2624 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 2625 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2626 | |
| 2627 | // Ensure that the test starts with an fresh heap to test whether the hash |
| 2628 | // code is based on the address. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2629 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2630 | Local<v8::Object> obj = v8::Object::New(); |
| 2631 | int hash = obj->GetIdentityHash(); |
| 2632 | int hash1 = obj->GetIdentityHash(); |
| 2633 | CHECK_EQ(hash, hash1); |
| 2634 | int hash2 = v8::Object::New()->GetIdentityHash(); |
| 2635 | // Since the identity hash is essentially a random number two consecutive |
| 2636 | // objects should not be assigned the same hash code. If the test below fails |
| 2637 | // the random number generator should be evaluated. |
| 2638 | CHECK_NE(hash, hash2); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2639 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2640 | int hash3 = v8::Object::New()->GetIdentityHash(); |
| 2641 | // Make sure that the identity hash is not based on the initial address of |
| 2642 | // the object alone. If the test below fails the random number generator |
| 2643 | // should be evaluated. |
| 2644 | CHECK_NE(hash, hash3); |
| 2645 | int hash4 = obj->GetIdentityHash(); |
| 2646 | CHECK_EQ(hash, hash4); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 2647 | |
| 2648 | // Check identity hashes behaviour in the presence of JS accessors. |
| 2649 | // Put a getter for 'v8::IdentityHash' on the Object's prototype: |
| 2650 | { |
| 2651 | CompileRun("Object.prototype['v8::IdentityHash'] = 42;\n"); |
| 2652 | Local<v8::Object> o1 = v8::Object::New(); |
| 2653 | Local<v8::Object> o2 = v8::Object::New(); |
| 2654 | CHECK_NE(o1->GetIdentityHash(), o2->GetIdentityHash()); |
| 2655 | } |
| 2656 | { |
| 2657 | CompileRun( |
| 2658 | "function cnst() { return 42; };\n" |
| 2659 | "Object.prototype.__defineGetter__('v8::IdentityHash', cnst);\n"); |
| 2660 | Local<v8::Object> o1 = v8::Object::New(); |
| 2661 | Local<v8::Object> o2 = v8::Object::New(); |
| 2662 | CHECK_NE(o1->GetIdentityHash(), o2->GetIdentityHash()); |
| 2663 | } |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
| 2666 | |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2667 | THREADED_TEST(SymbolProperties) { |
| 2668 | i::FLAG_harmony_symbols = true; |
| 2669 | |
| 2670 | LocalContext env; |
| 2671 | v8::Isolate* isolate = env->GetIsolate(); |
| 2672 | v8::HandleScope scope(isolate); |
| 2673 | |
| 2674 | v8::Local<v8::Object> obj = v8::Object::New(); |
| 2675 | v8::Local<v8::Symbol> sym1 = v8::Symbol::New(isolate); |
| 2676 | v8::Local<v8::Symbol> sym2 = v8::Symbol::New(isolate, "my-symbol"); |
| 2677 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2678 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2679 | |
| 2680 | // Check basic symbol functionality. |
| 2681 | CHECK(sym1->IsSymbol()); |
| 2682 | CHECK(sym2->IsSymbol()); |
| 2683 | CHECK(!obj->IsSymbol()); |
| 2684 | |
| 2685 | CHECK(sym1->Equals(sym1)); |
| 2686 | CHECK(sym2->Equals(sym2)); |
| 2687 | CHECK(!sym1->Equals(sym2)); |
| 2688 | CHECK(!sym2->Equals(sym1)); |
| 2689 | CHECK(sym1->StrictEquals(sym1)); |
| 2690 | CHECK(sym2->StrictEquals(sym2)); |
| 2691 | CHECK(!sym1->StrictEquals(sym2)); |
| 2692 | CHECK(!sym2->StrictEquals(sym1)); |
| 2693 | |
| 2694 | CHECK(sym2->Name()->Equals(v8::String::New("my-symbol"))); |
| 2695 | |
| 2696 | v8::Local<v8::Value> sym_val = sym2; |
| 2697 | CHECK(sym_val->IsSymbol()); |
| 2698 | CHECK(sym_val->Equals(sym2)); |
| 2699 | CHECK(sym_val->StrictEquals(sym2)); |
| 2700 | CHECK(v8::Symbol::Cast(*sym_val)->Equals(sym2)); |
| 2701 | |
| 2702 | v8::Local<v8::Value> sym_obj = v8::SymbolObject::New(isolate, sym2); |
| 2703 | CHECK(sym_obj->IsSymbolObject()); |
| 2704 | CHECK(!sym2->IsSymbolObject()); |
| 2705 | CHECK(!obj->IsSymbolObject()); |
| 2706 | CHECK(sym_obj->Equals(sym2)); |
| 2707 | CHECK(!sym_obj->StrictEquals(sym2)); |
| 2708 | CHECK(v8::SymbolObject::Cast(*sym_obj)->Equals(sym_obj)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 2709 | CHECK(v8::SymbolObject::Cast(*sym_obj)->ValueOf()->Equals(sym2)); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2710 | |
| 2711 | // Make sure delete of a non-existent symbol property works. |
| 2712 | CHECK(obj->Delete(sym1)); |
| 2713 | CHECK(!obj->Has(sym1)); |
| 2714 | |
| 2715 | CHECK(obj->Set(sym1, v8::Integer::New(1503))); |
| 2716 | CHECK(obj->Has(sym1)); |
| 2717 | CHECK_EQ(1503, obj->Get(sym1)->Int32Value()); |
| 2718 | CHECK(obj->Set(sym1, v8::Integer::New(2002))); |
| 2719 | CHECK(obj->Has(sym1)); |
| 2720 | CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); |
| 2721 | CHECK_EQ(v8::None, obj->GetPropertyAttributes(sym1)); |
| 2722 | |
| 2723 | CHECK_EQ(0, obj->GetOwnPropertyNames()->Length()); |
| 2724 | int num_props = obj->GetPropertyNames()->Length(); |
| 2725 | CHECK(obj->Set(v8::String::New("bla"), v8::Integer::New(20))); |
| 2726 | CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); |
| 2727 | CHECK_EQ(num_props + 1, obj->GetPropertyNames()->Length()); |
| 2728 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2729 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 2730 | |
| 2731 | // Add another property and delete it afterwards to force the object in |
| 2732 | // slow case. |
| 2733 | CHECK(obj->Set(sym2, v8::Integer::New(2008))); |
| 2734 | CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); |
| 2735 | CHECK_EQ(2008, obj->Get(sym2)->Int32Value()); |
| 2736 | CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); |
| 2737 | CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); |
| 2738 | |
| 2739 | CHECK(obj->Has(sym1)); |
| 2740 | CHECK(obj->Has(sym2)); |
| 2741 | CHECK(obj->Delete(sym2)); |
| 2742 | CHECK(obj->Has(sym1)); |
| 2743 | CHECK(!obj->Has(sym2)); |
| 2744 | CHECK_EQ(2002, obj->Get(sym1)->Int32Value()); |
| 2745 | CHECK_EQ(1, obj->GetOwnPropertyNames()->Length()); |
| 2746 | } |
| 2747 | |
| 2748 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2749 | class ScopedArrayBufferContents { |
| 2750 | public: |
| 2751 | explicit ScopedArrayBufferContents( |
| 2752 | const v8::ArrayBuffer::Contents& contents) |
| 2753 | : contents_(contents) {} |
| 2754 | ~ScopedArrayBufferContents() { free(contents_.Data()); } |
| 2755 | void* Data() const { return contents_.Data(); } |
| 2756 | size_t ByteLength() const { return contents_.ByteLength(); } |
| 2757 | private: |
| 2758 | const v8::ArrayBuffer::Contents contents_; |
| 2759 | }; |
| 2760 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2761 | template <typename T> |
| 2762 | static void CheckInternalFieldsAreZero(v8::Handle<T> value) { |
| 2763 | CHECK_EQ(T::kInternalFieldCount, value->InternalFieldCount()); |
| 2764 | for (int i = 0; i < value->InternalFieldCount(); i++) { |
| 2765 | CHECK_EQ(0, value->GetInternalField(i)->Int32Value()); |
| 2766 | } |
| 2767 | } |
| 2768 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2769 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2770 | THREADED_TEST(ArrayBuffer_ApiInternalToExternal) { |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2771 | LocalContext env; |
| 2772 | v8::Isolate* isolate = env->GetIsolate(); |
| 2773 | v8::HandleScope handle_scope(isolate); |
| 2774 | |
| 2775 | Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New(1024); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2776 | CheckInternalFieldsAreZero(ab); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2777 | CHECK_EQ(1024, static_cast<int>(ab->ByteLength())); |
| 2778 | CHECK(!ab->IsExternal()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 2779 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2780 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2781 | ScopedArrayBufferContents ab_contents(ab->Externalize()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2782 | CHECK(ab->IsExternal()); |
| 2783 | |
| 2784 | CHECK_EQ(1024, static_cast<int>(ab_contents.ByteLength())); |
| 2785 | uint8_t* data = static_cast<uint8_t*>(ab_contents.Data()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2786 | ASSERT(data != NULL); |
| 2787 | env->Global()->Set(v8_str("ab"), ab); |
| 2788 | |
| 2789 | v8::Handle<v8::Value> result = CompileRun("ab.byteLength"); |
| 2790 | CHECK_EQ(1024, result->Int32Value()); |
| 2791 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2792 | result = CompileRun("var u8 = new Uint8Array(ab);" |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2793 | "u8[0] = 0xFF;" |
| 2794 | "u8[1] = 0xAA;" |
| 2795 | "u8.length"); |
| 2796 | CHECK_EQ(1024, result->Int32Value()); |
| 2797 | CHECK_EQ(0xFF, data[0]); |
| 2798 | CHECK_EQ(0xAA, data[1]); |
| 2799 | data[0] = 0xCC; |
| 2800 | data[1] = 0x11; |
| 2801 | result = CompileRun("u8[0] + u8[1]"); |
| 2802 | CHECK_EQ(0xDD, result->Int32Value()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2803 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2804 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2805 | |
| 2806 | THREADED_TEST(ArrayBuffer_JSInternalToExternal) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2807 | LocalContext env; |
| 2808 | v8::Isolate* isolate = env->GetIsolate(); |
| 2809 | v8::HandleScope handle_scope(isolate); |
| 2810 | |
| 2811 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2812 | v8::Local<v8::Value> result = |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2813 | CompileRun("var ab1 = new ArrayBuffer(2);" |
| 2814 | "var u8_a = new Uint8Array(ab1);" |
| 2815 | "u8_a[0] = 0xAA;" |
| 2816 | "u8_a[1] = 0xFF; u8_a.buffer"); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2817 | Local<v8::ArrayBuffer> ab1 = Local<v8::ArrayBuffer>::Cast(result); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2818 | CheckInternalFieldsAreZero(ab1); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2819 | CHECK_EQ(2, static_cast<int>(ab1->ByteLength())); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2820 | CHECK(!ab1->IsExternal()); |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2821 | ScopedArrayBufferContents ab1_contents(ab1->Externalize()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2822 | CHECK(ab1->IsExternal()); |
| 2823 | |
| 2824 | result = CompileRun("ab1.byteLength"); |
| 2825 | CHECK_EQ(2, result->Int32Value()); |
| 2826 | result = CompileRun("u8_a[0]"); |
| 2827 | CHECK_EQ(0xAA, result->Int32Value()); |
| 2828 | result = CompileRun("u8_a[1]"); |
| 2829 | CHECK_EQ(0xFF, result->Int32Value()); |
| 2830 | result = CompileRun("var u8_b = new Uint8Array(ab1);" |
| 2831 | "u8_b[0] = 0xBB;" |
| 2832 | "u8_a[0]"); |
| 2833 | CHECK_EQ(0xBB, result->Int32Value()); |
| 2834 | result = CompileRun("u8_b[1]"); |
| 2835 | CHECK_EQ(0xFF, result->Int32Value()); |
| 2836 | |
| 2837 | CHECK_EQ(2, static_cast<int>(ab1_contents.ByteLength())); |
| 2838 | uint8_t* ab1_data = static_cast<uint8_t*>(ab1_contents.Data()); |
| 2839 | CHECK_EQ(0xBB, ab1_data[0]); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2840 | CHECK_EQ(0xFF, ab1_data[1]); |
| 2841 | ab1_data[0] = 0xCC; |
| 2842 | ab1_data[1] = 0x11; |
| 2843 | result = CompileRun("u8_a[0] + u8_a[1]"); |
| 2844 | CHECK_EQ(0xDD, result->Int32Value()); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2845 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2846 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2847 | |
| 2848 | THREADED_TEST(ArrayBuffer_External) { |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2849 | LocalContext env; |
| 2850 | v8::Isolate* isolate = env->GetIsolate(); |
| 2851 | v8::HandleScope handle_scope(isolate); |
| 2852 | |
| 2853 | i::ScopedVector<uint8_t> my_data(100); |
| 2854 | memset(my_data.start(), 0, 100); |
| 2855 | Local<v8::ArrayBuffer> ab3 = v8::ArrayBuffer::New(my_data.start(), 100); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2856 | CheckInternalFieldsAreZero(ab3); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2857 | CHECK_EQ(100, static_cast<int>(ab3->ByteLength())); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2858 | CHECK(ab3->IsExternal()); |
| 2859 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2860 | env->Global()->Set(v8_str("ab3"), ab3); |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 2861 | |
| 2862 | v8::Handle<v8::Value> result = CompileRun("ab3.byteLength"); |
| 2863 | CHECK_EQ(100, result->Int32Value()); |
| 2864 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 2865 | result = CompileRun("var u8_b = new Uint8Array(ab3);" |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2866 | "u8_b[0] = 0xBB;" |
| 2867 | "u8_b[1] = 0xCC;" |
| 2868 | "u8_b.length"); |
| 2869 | CHECK_EQ(100, result->Int32Value()); |
| 2870 | CHECK_EQ(0xBB, my_data[0]); |
| 2871 | CHECK_EQ(0xCC, my_data[1]); |
| 2872 | my_data[0] = 0xCC; |
| 2873 | my_data[1] = 0x11; |
| 2874 | result = CompileRun("u8_b[0] + u8_b[1]"); |
| 2875 | CHECK_EQ(0xDD, result->Int32Value()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2879 | static void CheckDataViewIsNeutered(v8::Handle<v8::DataView> dv) { |
| 2880 | CHECK_EQ(0, static_cast<int>(dv->ByteLength())); |
| 2881 | CHECK_EQ(0, static_cast<int>(dv->ByteOffset())); |
| 2882 | } |
| 2883 | |
| 2884 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2885 | static void CheckIsNeutered(v8::Handle<v8::TypedArray> ta) { |
| 2886 | CHECK_EQ(0, static_cast<int>(ta->ByteLength())); |
| 2887 | CHECK_EQ(0, static_cast<int>(ta->Length())); |
| 2888 | CHECK_EQ(0, static_cast<int>(ta->ByteOffset())); |
| 2889 | } |
| 2890 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2891 | |
| 2892 | static void CheckIsTypedArrayVarNeutered(const char* name) { |
| 2893 | i::ScopedVector<char> source(1024); |
| 2894 | i::OS::SNPrintF(source, |
| 2895 | "%s.byteLength == 0 && %s.byteOffset == 0 && %s.length == 0", |
| 2896 | name, name, name); |
| 2897 | CHECK(CompileRun(source.start())->IsTrue()); |
| 2898 | v8::Handle<v8::TypedArray> ta = |
| 2899 | v8::Handle<v8::TypedArray>::Cast(CompileRun(name)); |
| 2900 | CheckIsNeutered(ta); |
| 2901 | } |
| 2902 | |
| 2903 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2904 | template <typename TypedArray, int kElementSize> |
| 2905 | static Handle<TypedArray> CreateAndCheck(Handle<v8::ArrayBuffer> ab, |
| 2906 | int byteOffset, |
| 2907 | int length) { |
| 2908 | v8::Handle<TypedArray> ta = TypedArray::New(ab, byteOffset, length); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2909 | CheckInternalFieldsAreZero<v8::ArrayBufferView>(ta); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2910 | CHECK_EQ(byteOffset, static_cast<int>(ta->ByteOffset())); |
| 2911 | CHECK_EQ(length, static_cast<int>(ta->Length())); |
| 2912 | CHECK_EQ(length * kElementSize, static_cast<int>(ta->ByteLength())); |
| 2913 | return ta; |
| 2914 | } |
| 2915 | |
| 2916 | |
| 2917 | THREADED_TEST(ArrayBuffer_NeuteringApi) { |
| 2918 | LocalContext env; |
| 2919 | v8::Isolate* isolate = env->GetIsolate(); |
| 2920 | v8::HandleScope handle_scope(isolate); |
| 2921 | |
| 2922 | v8::Handle<v8::ArrayBuffer> buffer = v8::ArrayBuffer::New(1024); |
| 2923 | |
| 2924 | v8::Handle<v8::Uint8Array> u8a = |
| 2925 | CreateAndCheck<v8::Uint8Array, 1>(buffer, 1, 1023); |
| 2926 | v8::Handle<v8::Uint8ClampedArray> u8c = |
| 2927 | CreateAndCheck<v8::Uint8ClampedArray, 1>(buffer, 1, 1023); |
| 2928 | v8::Handle<v8::Int8Array> i8a = |
| 2929 | CreateAndCheck<v8::Int8Array, 1>(buffer, 1, 1023); |
| 2930 | |
| 2931 | v8::Handle<v8::Uint16Array> u16a = |
| 2932 | CreateAndCheck<v8::Uint16Array, 2>(buffer, 2, 511); |
| 2933 | v8::Handle<v8::Int16Array> i16a = |
| 2934 | CreateAndCheck<v8::Int16Array, 2>(buffer, 2, 511); |
| 2935 | |
| 2936 | v8::Handle<v8::Uint32Array> u32a = |
| 2937 | CreateAndCheck<v8::Uint32Array, 4>(buffer, 4, 255); |
| 2938 | v8::Handle<v8::Int32Array> i32a = |
| 2939 | CreateAndCheck<v8::Int32Array, 4>(buffer, 4, 255); |
| 2940 | |
| 2941 | v8::Handle<v8::Float32Array> f32a = |
| 2942 | CreateAndCheck<v8::Float32Array, 4>(buffer, 4, 255); |
| 2943 | v8::Handle<v8::Float64Array> f64a = |
| 2944 | CreateAndCheck<v8::Float64Array, 8>(buffer, 8, 127); |
| 2945 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2946 | v8::Handle<v8::DataView> dv = v8::DataView::New(buffer, 1, 1023); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2947 | CheckInternalFieldsAreZero<v8::ArrayBufferView>(dv); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2948 | CHECK_EQ(1, static_cast<int>(dv->ByteOffset())); |
| 2949 | CHECK_EQ(1023, static_cast<int>(dv->ByteLength())); |
| 2950 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2951 | ScopedArrayBufferContents contents(buffer->Externalize()); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2952 | buffer->Neuter(); |
| 2953 | CHECK_EQ(0, static_cast<int>(buffer->ByteLength())); |
| 2954 | CheckIsNeutered(u8a); |
| 2955 | CheckIsNeutered(u8c); |
| 2956 | CheckIsNeutered(i8a); |
| 2957 | CheckIsNeutered(u16a); |
| 2958 | CheckIsNeutered(i16a); |
| 2959 | CheckIsNeutered(u32a); |
| 2960 | CheckIsNeutered(i32a); |
| 2961 | CheckIsNeutered(f32a); |
| 2962 | CheckIsNeutered(f64a); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2963 | CheckDataViewIsNeutered(dv); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 2966 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2967 | THREADED_TEST(ArrayBuffer_NeuteringScript) { |
| 2968 | LocalContext env; |
| 2969 | v8::Isolate* isolate = env->GetIsolate(); |
| 2970 | v8::HandleScope handle_scope(isolate); |
| 2971 | |
| 2972 | CompileRun( |
| 2973 | "var ab = new ArrayBuffer(1024);" |
| 2974 | "var u8a = new Uint8Array(ab, 1, 1023);" |
| 2975 | "var u8c = new Uint8ClampedArray(ab, 1, 1023);" |
| 2976 | "var i8a = new Int8Array(ab, 1, 1023);" |
| 2977 | "var u16a = new Uint16Array(ab, 2, 511);" |
| 2978 | "var i16a = new Int16Array(ab, 2, 511);" |
| 2979 | "var u32a = new Uint32Array(ab, 4, 255);" |
| 2980 | "var i32a = new Int32Array(ab, 4, 255);" |
| 2981 | "var f32a = new Float32Array(ab, 4, 255);" |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2982 | "var f64a = new Float64Array(ab, 8, 127);" |
| 2983 | "var dv = new DataView(ab, 1, 1023);"); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2984 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 2985 | v8::Handle<v8::ArrayBuffer> ab = |
| 2986 | Local<v8::ArrayBuffer>::Cast(CompileRun("ab")); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2987 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2988 | v8::Handle<v8::DataView> dv = |
| 2989 | v8::Handle<v8::DataView>::Cast(CompileRun("dv")); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2990 | |
ulan@chromium.org | 837a67e | 2013-06-11 15:39:48 +0000 | [diff] [blame] | 2991 | ScopedArrayBufferContents contents(ab->Externalize()); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 2992 | ab->Neuter(); |
| 2993 | CHECK_EQ(0, static_cast<int>(ab->ByteLength())); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 2994 | CHECK_EQ(0, CompileRun("ab.byteLength")->Int32Value()); |
| 2995 | |
| 2996 | CheckIsTypedArrayVarNeutered("u8a"); |
| 2997 | CheckIsTypedArrayVarNeutered("u8c"); |
| 2998 | CheckIsTypedArrayVarNeutered("i8a"); |
| 2999 | CheckIsTypedArrayVarNeutered("u16a"); |
| 3000 | CheckIsTypedArrayVarNeutered("i16a"); |
| 3001 | CheckIsTypedArrayVarNeutered("u32a"); |
| 3002 | CheckIsTypedArrayVarNeutered("i32a"); |
| 3003 | CheckIsTypedArrayVarNeutered("f32a"); |
| 3004 | CheckIsTypedArrayVarNeutered("f64a"); |
| 3005 | |
| 3006 | CHECK(CompileRun("dv.byteLength == 0 && dv.byteOffset == 0")->IsTrue()); |
| 3007 | CheckDataViewIsNeutered(dv); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
| 3010 | |
| 3011 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3012 | THREADED_TEST(HiddenProperties) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3013 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3014 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3015 | |
| 3016 | v8::Local<v8::Object> obj = v8::Object::New(); |
| 3017 | v8::Local<v8::String> key = v8_str("api-test::hidden-key"); |
| 3018 | v8::Local<v8::String> empty = v8_str(""); |
| 3019 | v8::Local<v8::String> prop_name = v8_str("prop_name"); |
| 3020 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3021 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3022 | |
kasperl@chromium.org | acae378 | 2009-04-11 09:17:08 +0000 | [diff] [blame] | 3023 | // Make sure delete of a non-existent hidden value works |
| 3024 | CHECK(obj->DeleteHiddenValue(key)); |
| 3025 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3026 | CHECK(obj->SetHiddenValue(key, v8::Integer::New(1503))); |
| 3027 | CHECK_EQ(1503, obj->GetHiddenValue(key)->Int32Value()); |
| 3028 | CHECK(obj->SetHiddenValue(key, v8::Integer::New(2002))); |
| 3029 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3030 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3031 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3032 | |
| 3033 | // Make sure we do not find the hidden property. |
| 3034 | CHECK(!obj->Has(empty)); |
| 3035 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3036 | CHECK(obj->Get(empty)->IsUndefined()); |
| 3037 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3038 | CHECK(obj->Set(empty, v8::Integer::New(2003))); |
| 3039 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3040 | CHECK_EQ(2003, obj->Get(empty)->Int32Value()); |
| 3041 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3042 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3043 | |
| 3044 | // Add another property and delete it afterwards to force the object in |
| 3045 | // slow case. |
| 3046 | CHECK(obj->Set(prop_name, v8::Integer::New(2008))); |
| 3047 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3048 | CHECK_EQ(2008, obj->Get(prop_name)->Int32Value()); |
| 3049 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3050 | CHECK(obj->Delete(prop_name)); |
| 3051 | CHECK_EQ(2002, obj->GetHiddenValue(key)->Int32Value()); |
| 3052 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3053 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3054 | |
mstarzinger@chromium.org | 471f2f1 | 2012-08-10 14:46:33 +0000 | [diff] [blame] | 3055 | CHECK(obj->SetHiddenValue(key, Handle<Value>())); |
| 3056 | CHECK(obj->GetHiddenValue(key).IsEmpty()); |
| 3057 | |
| 3058 | CHECK(obj->SetHiddenValue(key, v8::Integer::New(2002))); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 3059 | CHECK(obj->DeleteHiddenValue(key)); |
| 3060 | CHECK(obj->GetHiddenValue(key).IsEmpty()); |
| 3061 | } |
| 3062 | |
| 3063 | |
ricow@chromium.org | 9fe9cdf | 2011-09-27 08:04:16 +0000 | [diff] [blame] | 3064 | THREADED_TEST(Regress97784) { |
| 3065 | // Regression test for crbug.com/97784 |
| 3066 | // Messing with the Object.prototype should not have effect on |
| 3067 | // hidden properties. |
ricow@chromium.org | 9fe9cdf | 2011-09-27 08:04:16 +0000 | [diff] [blame] | 3068 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3069 | v8::HandleScope scope(env->GetIsolate()); |
ricow@chromium.org | 9fe9cdf | 2011-09-27 08:04:16 +0000 | [diff] [blame] | 3070 | |
| 3071 | v8::Local<v8::Object> obj = v8::Object::New(); |
| 3072 | v8::Local<v8::String> key = v8_str("hidden"); |
| 3073 | |
| 3074 | CompileRun( |
| 3075 | "set_called = false;" |
| 3076 | "Object.defineProperty(" |
| 3077 | " Object.prototype," |
| 3078 | " 'hidden'," |
| 3079 | " {get: function() { return 45; }," |
| 3080 | " set: function() { set_called = true; }})"); |
| 3081 | |
| 3082 | CHECK(obj->GetHiddenValue(key).IsEmpty()); |
| 3083 | // Make sure that the getter and setter from Object.prototype is not invoked. |
| 3084 | // If it did we would have full access to the hidden properties in |
| 3085 | // the accessor. |
| 3086 | CHECK(obj->SetHiddenValue(key, v8::Integer::New(42))); |
| 3087 | ExpectFalse("set_called"); |
| 3088 | CHECK_EQ(42, obj->GetHiddenValue(key)->Int32Value()); |
| 3089 | } |
| 3090 | |
| 3091 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3092 | static bool interceptor_for_hidden_properties_called; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3093 | static void InterceptorForHiddenProperties( |
| 3094 | Local<String> name, const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3095 | interceptor_for_hidden_properties_called = true; |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3096 | } |
| 3097 | |
| 3098 | |
| 3099 | THREADED_TEST(HiddenPropertiesWithInterceptors) { |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3100 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3101 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3102 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3103 | interceptor_for_hidden_properties_called = false; |
| 3104 | |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3105 | v8::Local<v8::String> key = v8_str("api-test::hidden-key"); |
| 3106 | |
| 3107 | // Associate an interceptor with an object and start setting hidden values. |
| 3108 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 3109 | Local<v8::ObjectTemplate> instance_templ = fun_templ->InstanceTemplate(); |
| 3110 | instance_templ->SetNamedPropertyHandler(InterceptorForHiddenProperties); |
| 3111 | Local<v8::Function> function = fun_templ->GetFunction(); |
| 3112 | Local<v8::Object> obj = function->NewInstance(); |
| 3113 | CHECK(obj->SetHiddenValue(key, v8::Integer::New(2302))); |
| 3114 | CHECK_EQ(2302, obj->GetHiddenValue(key)->Int32Value()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 3115 | CHECK(!interceptor_for_hidden_properties_called); |
kasperl@chromium.org | b3284ad | 2009-05-18 06:12:45 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3119 | THREADED_TEST(External) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3120 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3121 | int x = 3; |
| 3122 | Local<v8::External> ext = v8::External::New(&x); |
| 3123 | LocalContext env; |
| 3124 | env->Global()->Set(v8_str("ext"), ext); |
| 3125 | Local<Value> reext_obj = Script::Compile(v8_str("this.ext"))->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3126 | v8::Handle<v8::External> reext = reext_obj.As<v8::External>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3127 | int* ptr = static_cast<int*>(reext->Value()); |
| 3128 | CHECK_EQ(x, 3); |
| 3129 | *ptr = 10; |
| 3130 | CHECK_EQ(x, 10); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3131 | |
| 3132 | // Make sure unaligned pointers are wrapped properly. |
| 3133 | char* data = i::StrDup("0123456789"); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 3134 | Local<v8::Value> zero = v8::External::New(&data[0]); |
| 3135 | Local<v8::Value> one = v8::External::New(&data[1]); |
| 3136 | Local<v8::Value> two = v8::External::New(&data[2]); |
| 3137 | Local<v8::Value> three = v8::External::New(&data[3]); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3138 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 3139 | char* char_ptr = reinterpret_cast<char*>(v8::External::Cast(*zero)->Value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3140 | CHECK_EQ('0', *char_ptr); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 3141 | char_ptr = reinterpret_cast<char*>(v8::External::Cast(*one)->Value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3142 | CHECK_EQ('1', *char_ptr); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 3143 | char_ptr = reinterpret_cast<char*>(v8::External::Cast(*two)->Value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3144 | CHECK_EQ('2', *char_ptr); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 3145 | char_ptr = reinterpret_cast<char*>(v8::External::Cast(*three)->Value()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 3146 | CHECK_EQ('3', *char_ptr); |
| 3147 | i::DeleteArray(data); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | |
| 3151 | THREADED_TEST(GlobalHandle) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3152 | v8::Isolate* isolate = CcTest::isolate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3153 | v8::Persistent<String> global; |
| 3154 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3155 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3156 | global.Reset(isolate, v8_str("str")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3157 | } |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 3158 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3159 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3160 | CHECK_EQ(v8::Local<String>::New(isolate, global)->Length(), 3); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 3161 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3162 | global.Dispose(); |
| 3163 | global.Clear(); |
| 3164 | { |
| 3165 | v8::HandleScope scope(isolate); |
| 3166 | global.Reset(isolate, v8_str("str")); |
| 3167 | } |
| 3168 | { |
| 3169 | v8::HandleScope scope(isolate); |
| 3170 | CHECK_EQ(v8::Local<String>::New(isolate, global)->Length(), 3); |
| 3171 | } |
| 3172 | global.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
| 3175 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3176 | THREADED_TEST(ResettingGlobalHandle) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3177 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3178 | v8::Persistent<String> global; |
| 3179 | { |
| 3180 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3181 | global.Reset(isolate, v8_str("str")); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3182 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3183 | v8::internal::GlobalHandles* global_handles = |
| 3184 | reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3185 | int initial_handle_count = global_handles->global_handles_count(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3186 | { |
| 3187 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3188 | CHECK_EQ(v8::Local<String>::New(isolate, global)->Length(), 3); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3189 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3190 | { |
| 3191 | v8::HandleScope scope(isolate); |
| 3192 | global.Reset(isolate, v8_str("longer")); |
| 3193 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3194 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3195 | { |
| 3196 | v8::HandleScope scope(isolate); |
| 3197 | CHECK_EQ(v8::Local<String>::New(isolate, global)->Length(), 6); |
| 3198 | } |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3199 | global.Dispose(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3200 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count - 1); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
| 3203 | |
| 3204 | THREADED_TEST(ResettingGlobalHandleToEmpty) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3205 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3206 | v8::Persistent<String> global; |
| 3207 | { |
| 3208 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3209 | global.Reset(isolate, v8_str("str")); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3210 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3211 | v8::internal::GlobalHandles* global_handles = |
| 3212 | reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3213 | int initial_handle_count = global_handles->global_handles_count(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3214 | { |
| 3215 | v8::HandleScope scope(isolate); |
| 3216 | CHECK_EQ(v8::Local<String>::New(isolate, global)->Length(), 3); |
| 3217 | } |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3218 | { |
| 3219 | v8::HandleScope scope(isolate); |
| 3220 | Local<String> empty; |
| 3221 | global.Reset(isolate, empty); |
| 3222 | } |
| 3223 | CHECK(global.IsEmpty()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3224 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count - 1); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3228 | THREADED_TEST(ClearAndLeakGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3229 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3230 | v8::internal::GlobalHandles* global_handles = NULL; |
| 3231 | int initial_handle_count = 0; |
| 3232 | v8::Persistent<String> global; |
| 3233 | { |
| 3234 | v8::HandleScope scope(isolate); |
| 3235 | Local<String> str = v8_str("str"); |
| 3236 | global_handles = |
| 3237 | reinterpret_cast<v8::internal::Isolate*>(isolate)->global_handles(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3238 | initial_handle_count = global_handles->global_handles_count(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3239 | global.Reset(isolate, str); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3240 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3241 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count + 1); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3242 | String* str = global.ClearAndLeak(); |
| 3243 | CHECK(global.IsEmpty()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3244 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count + 1); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 3245 | global_handles->Destroy(reinterpret_cast<i::Object**>(str)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3246 | CHECK_EQ(global_handles->global_handles_count(), initial_handle_count); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3250 | THREADED_TEST(GlobalHandleUpcast) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3251 | v8::Isolate* isolate = CcTest::isolate(); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3252 | v8::HandleScope scope(isolate); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3253 | v8::Local<String> local = v8::Local<String>::New(isolate, v8_str("str")); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3254 | v8::Persistent<String> global_string(isolate, local); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3255 | v8::Persistent<Value>& global_value = |
| 3256 | v8::Persistent<Value>::Cast(global_string); |
danno@chromium.org | 1fd77d5 | 2013-06-07 16:01:45 +0000 | [diff] [blame] | 3257 | CHECK(v8::Local<v8::Value>::New(isolate, global_value)->IsString()); |
| 3258 | CHECK(global_string == v8::Persistent<String>::Cast(global_value)); |
| 3259 | global_string.Dispose(); |
| 3260 | } |
| 3261 | |
| 3262 | |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 3263 | THREADED_TEST(HandleEquality) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3264 | v8::Isolate* isolate = CcTest::isolate(); |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 3265 | v8::Persistent<String> global1; |
| 3266 | v8::Persistent<String> global2; |
| 3267 | { |
| 3268 | v8::HandleScope scope(isolate); |
| 3269 | global1.Reset(isolate, v8_str("str")); |
| 3270 | global2.Reset(isolate, v8_str("str2")); |
| 3271 | } |
| 3272 | CHECK_EQ(global1 == global1, true); |
| 3273 | CHECK_EQ(global1 != global1, false); |
| 3274 | { |
| 3275 | v8::HandleScope scope(isolate); |
| 3276 | Local<String> local1 = Local<String>::New(isolate, global1); |
| 3277 | Local<String> local2 = Local<String>::New(isolate, global2); |
| 3278 | |
| 3279 | CHECK_EQ(global1 == local1, true); |
| 3280 | CHECK_EQ(global1 != local1, false); |
| 3281 | CHECK_EQ(local1 == global1, true); |
| 3282 | CHECK_EQ(local1 != global1, false); |
| 3283 | |
| 3284 | CHECK_EQ(global1 == local2, false); |
| 3285 | CHECK_EQ(global1 != local2, true); |
| 3286 | CHECK_EQ(local2 == global1, false); |
| 3287 | CHECK_EQ(local2 != global1, true); |
| 3288 | |
| 3289 | CHECK_EQ(local1 == local2, false); |
| 3290 | CHECK_EQ(local1 != local2, true); |
| 3291 | |
| 3292 | Local<String> anotherLocal1 = Local<String>::New(isolate, global1); |
| 3293 | CHECK_EQ(local1 == anotherLocal1, true); |
| 3294 | CHECK_EQ(local1 != anotherLocal1, false); |
| 3295 | } |
| 3296 | global1.Dispose(); |
| 3297 | global2.Dispose(); |
| 3298 | } |
| 3299 | |
| 3300 | |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 3301 | THREADED_TEST(LocalHandle) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3302 | v8::HandleScope scope(CcTest::isolate()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3303 | v8::Local<String> local = |
| 3304 | v8::Local<String>::New(CcTest::isolate(), v8_str("str")); |
mstarzinger@chromium.org | 32280cf | 2012-12-06 17:32:37 +0000 | [diff] [blame] | 3305 | CHECK_EQ(local->Length(), 3); |
| 3306 | } |
| 3307 | |
| 3308 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3309 | class WeakCallCounter { |
| 3310 | public: |
| 3311 | explicit WeakCallCounter(int id) : id_(id), number_of_weak_calls_(0) { } |
| 3312 | int id() { return id_; } |
| 3313 | void increment() { number_of_weak_calls_++; } |
| 3314 | int NumberOfWeakCalls() { return number_of_weak_calls_; } |
| 3315 | private: |
| 3316 | int id_; |
| 3317 | int number_of_weak_calls_; |
| 3318 | }; |
| 3319 | |
| 3320 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 3321 | static void WeakPointerCallback(v8::Isolate* isolate, |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3322 | Persistent<Value>* handle, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 3323 | WeakCallCounter* counter) { |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3324 | CHECK_EQ(1234, counter->id()); |
| 3325 | counter->increment(); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3326 | handle->Dispose(); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 3327 | } |
| 3328 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 3329 | |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3330 | static UniqueId MakeUniqueId(const Persistent<Value>& p) { |
| 3331 | return UniqueId(reinterpret_cast<uintptr_t>(*v8::Utils::OpenPersistent(p))); |
| 3332 | } |
| 3333 | |
| 3334 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3335 | THREADED_TEST(ApiObjectGroups) { |
| 3336 | LocalContext env; |
| 3337 | v8::Isolate* iso = env->GetIsolate(); |
| 3338 | HandleScope scope(iso); |
| 3339 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3340 | Persistent<Value> g1s1; |
| 3341 | Persistent<Value> g1s2; |
| 3342 | Persistent<Value> g1c1; |
| 3343 | Persistent<Value> g2s1; |
| 3344 | Persistent<Value> g2s2; |
| 3345 | Persistent<Value> g2c1; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3346 | |
| 3347 | WeakCallCounter counter(1234); |
| 3348 | |
| 3349 | { |
| 3350 | HandleScope scope(iso); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3351 | g1s1.Reset(iso, Object::New()); |
| 3352 | g1s2.Reset(iso, Object::New()); |
| 3353 | g1c1.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3354 | g1s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3355 | g1s2.MakeWeak(&counter, &WeakPointerCallback); |
| 3356 | g1c1.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3357 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3358 | g2s1.Reset(iso, Object::New()); |
| 3359 | g2s2.Reset(iso, Object::New()); |
| 3360 | g2c1.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3361 | g2s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3362 | g2s2.MakeWeak(&counter, &WeakPointerCallback); |
| 3363 | g2c1.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3364 | } |
| 3365 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3366 | Persistent<Value> root(iso, g1s1); // make a root. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3367 | |
| 3368 | // Connect group 1 and 2, make a cycle. |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3369 | { |
| 3370 | HandleScope scope(iso); |
| 3371 | CHECK(Local<Object>::New(iso, g1s2.As<Object>())-> |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3372 | Set(0, Local<Value>::New(iso, g2s2))); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3373 | CHECK(Local<Object>::New(iso, g2s1.As<Object>())-> |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3374 | Set(0, Local<Value>::New(iso, g1s1))); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3375 | } |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3376 | |
| 3377 | { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3378 | UniqueId id1 = MakeUniqueId(g1s1); |
| 3379 | UniqueId id2 = MakeUniqueId(g2s2); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3380 | iso->SetObjectGroupId(g1s1, id1); |
| 3381 | iso->SetObjectGroupId(g1s2, id1); |
| 3382 | iso->SetReferenceFromGroup(id1, g1c1); |
| 3383 | iso->SetObjectGroupId(g2s1, id2); |
| 3384 | iso->SetObjectGroupId(g2s2, id2); |
| 3385 | iso->SetReferenceFromGroup(id2, g2c1); |
| 3386 | } |
| 3387 | // Do a single full GC, ensure incremental marking is stopped. |
| 3388 | v8::internal::Heap* heap = reinterpret_cast<v8::internal::Isolate*>( |
| 3389 | iso)->heap(); |
| 3390 | heap->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3391 | |
| 3392 | // All object should be alive. |
| 3393 | CHECK_EQ(0, counter.NumberOfWeakCalls()); |
| 3394 | |
| 3395 | // Weaken the root. |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3396 | root.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3397 | // But make children strong roots---all the objects (except for children) |
| 3398 | // should be collectable now. |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3399 | g1c1.ClearWeak(); |
| 3400 | g2c1.ClearWeak(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3401 | |
| 3402 | // Groups are deleted, rebuild groups. |
| 3403 | { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3404 | UniqueId id1 = MakeUniqueId(g1s1); |
| 3405 | UniqueId id2 = MakeUniqueId(g2s2); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3406 | iso->SetObjectGroupId(g1s1, id1); |
| 3407 | iso->SetObjectGroupId(g1s2, id1); |
| 3408 | iso->SetReferenceFromGroup(id1, g1c1); |
| 3409 | iso->SetObjectGroupId(g2s1, id2); |
| 3410 | iso->SetObjectGroupId(g2s2, id2); |
| 3411 | iso->SetReferenceFromGroup(id2, g2c1); |
| 3412 | } |
| 3413 | |
| 3414 | heap->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3415 | |
| 3416 | // All objects should be gone. 5 global handles in total. |
| 3417 | CHECK_EQ(5, counter.NumberOfWeakCalls()); |
| 3418 | |
| 3419 | // And now make children weak again and collect them. |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3420 | g1c1.MakeWeak(&counter, &WeakPointerCallback); |
| 3421 | g2c1.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3422 | |
| 3423 | heap->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3424 | CHECK_EQ(7, counter.NumberOfWeakCalls()); |
| 3425 | } |
| 3426 | |
| 3427 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3428 | THREADED_TEST(ApiObjectGroupsCycle) { |
| 3429 | LocalContext env; |
| 3430 | v8::Isolate* iso = env->GetIsolate(); |
| 3431 | HandleScope scope(iso); |
| 3432 | |
| 3433 | WeakCallCounter counter(1234); |
| 3434 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3435 | Persistent<Value> g1s1; |
| 3436 | Persistent<Value> g1s2; |
| 3437 | Persistent<Value> g2s1; |
| 3438 | Persistent<Value> g2s2; |
| 3439 | Persistent<Value> g3s1; |
| 3440 | Persistent<Value> g3s2; |
| 3441 | Persistent<Value> g4s1; |
| 3442 | Persistent<Value> g4s2; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3443 | |
| 3444 | { |
| 3445 | HandleScope scope(iso); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3446 | g1s1.Reset(iso, Object::New()); |
| 3447 | g1s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3448 | g1s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3449 | g1s2.MakeWeak(&counter, &WeakPointerCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3450 | CHECK(g1s1.IsWeak()); |
| 3451 | CHECK(g1s2.IsWeak()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3452 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3453 | g2s1.Reset(iso, Object::New()); |
| 3454 | g2s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3455 | g2s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3456 | g2s2.MakeWeak(&counter, &WeakPointerCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3457 | CHECK(g2s1.IsWeak()); |
| 3458 | CHECK(g2s2.IsWeak()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3459 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3460 | g3s1.Reset(iso, Object::New()); |
| 3461 | g3s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3462 | g3s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3463 | g3s2.MakeWeak(&counter, &WeakPointerCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3464 | CHECK(g3s1.IsWeak()); |
| 3465 | CHECK(g3s2.IsWeak()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3466 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3467 | g4s1.Reset(iso, Object::New()); |
| 3468 | g4s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3469 | g4s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3470 | g4s2.MakeWeak(&counter, &WeakPointerCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3471 | CHECK(g4s1.IsWeak()); |
| 3472 | CHECK(g4s2.IsWeak()); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3475 | Persistent<Value> root(iso, g1s1); // make a root. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3476 | |
| 3477 | // Connect groups. We're building the following cycle: |
| 3478 | // G1: { g1s1, g2s1 }, g1s1 implicitly references g2s1, ditto for other |
| 3479 | // groups. |
| 3480 | { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3481 | UniqueId id1 = MakeUniqueId(g1s1); |
| 3482 | UniqueId id2 = MakeUniqueId(g2s1); |
| 3483 | UniqueId id3 = MakeUniqueId(g3s1); |
| 3484 | UniqueId id4 = MakeUniqueId(g4s1); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3485 | iso->SetObjectGroupId(g1s1, id1); |
| 3486 | iso->SetObjectGroupId(g1s2, id1); |
| 3487 | iso->SetReferenceFromGroup(id1, g2s1); |
| 3488 | iso->SetObjectGroupId(g2s1, id2); |
| 3489 | iso->SetObjectGroupId(g2s2, id2); |
| 3490 | iso->SetReferenceFromGroup(id2, g3s1); |
| 3491 | iso->SetObjectGroupId(g3s1, id3); |
| 3492 | iso->SetObjectGroupId(g3s2, id3); |
| 3493 | iso->SetReferenceFromGroup(id3, g4s1); |
| 3494 | iso->SetObjectGroupId(g4s1, id4); |
| 3495 | iso->SetObjectGroupId(g4s2, id4); |
| 3496 | iso->SetReferenceFromGroup(id4, g1s1); |
| 3497 | } |
| 3498 | // Do a single full GC |
| 3499 | v8::internal::Heap* heap = reinterpret_cast<v8::internal::Isolate*>( |
| 3500 | iso)->heap(); |
| 3501 | heap->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3502 | |
| 3503 | // All object should be alive. |
| 3504 | CHECK_EQ(0, counter.NumberOfWeakCalls()); |
| 3505 | |
| 3506 | // Weaken the root. |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3507 | root.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3508 | |
| 3509 | // Groups are deleted, rebuild groups. |
| 3510 | { |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3511 | UniqueId id1 = MakeUniqueId(g1s1); |
| 3512 | UniqueId id2 = MakeUniqueId(g2s1); |
| 3513 | UniqueId id3 = MakeUniqueId(g3s1); |
| 3514 | UniqueId id4 = MakeUniqueId(g4s1); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3515 | iso->SetObjectGroupId(g1s1, id1); |
| 3516 | iso->SetObjectGroupId(g1s2, id1); |
| 3517 | iso->SetReferenceFromGroup(id1, g2s1); |
| 3518 | iso->SetObjectGroupId(g2s1, id2); |
| 3519 | iso->SetObjectGroupId(g2s2, id2); |
| 3520 | iso->SetReferenceFromGroup(id2, g3s1); |
| 3521 | iso->SetObjectGroupId(g3s1, id3); |
| 3522 | iso->SetObjectGroupId(g3s2, id3); |
| 3523 | iso->SetReferenceFromGroup(id3, g4s1); |
| 3524 | iso->SetObjectGroupId(g4s1, id4); |
| 3525 | iso->SetObjectGroupId(g4s2, id4); |
| 3526 | iso->SetReferenceFromGroup(id4, g1s1); |
| 3527 | } |
| 3528 | |
| 3529 | heap->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
| 3530 | |
| 3531 | // All objects should be gone. 9 global handles in total. |
| 3532 | CHECK_EQ(9, counter.NumberOfWeakCalls()); |
| 3533 | } |
| 3534 | |
| 3535 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 3536 | // TODO(mstarzinger): This should be a THREADED_TEST but causes failures |
| 3537 | // on the buildbots, so was made non-threaded for the time being. |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3538 | TEST(ApiObjectGroupsCycleForScavenger) { |
| 3539 | i::FLAG_stress_compaction = false; |
| 3540 | i::FLAG_gc_global = false; |
| 3541 | LocalContext env; |
| 3542 | v8::Isolate* iso = env->GetIsolate(); |
| 3543 | HandleScope scope(iso); |
| 3544 | |
| 3545 | WeakCallCounter counter(1234); |
| 3546 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3547 | Persistent<Value> g1s1; |
| 3548 | Persistent<Value> g1s2; |
| 3549 | Persistent<Value> g2s1; |
| 3550 | Persistent<Value> g2s2; |
| 3551 | Persistent<Value> g3s1; |
| 3552 | Persistent<Value> g3s2; |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3553 | |
| 3554 | { |
| 3555 | HandleScope scope(iso); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3556 | g1s1.Reset(iso, Object::New()); |
| 3557 | g1s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3558 | g1s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3559 | g1s2.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3560 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3561 | g2s1.Reset(iso, Object::New()); |
| 3562 | g2s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3563 | g2s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3564 | g2s2.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3565 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3566 | g3s1.Reset(iso, Object::New()); |
| 3567 | g3s2.Reset(iso, Object::New()); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3568 | g3s1.MakeWeak(&counter, &WeakPointerCallback); |
| 3569 | g3s2.MakeWeak(&counter, &WeakPointerCallback); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3570 | } |
| 3571 | |
| 3572 | // Make a root. |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3573 | Persistent<Value> root(iso, g1s1); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3574 | root.MarkPartiallyDependent(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3575 | |
| 3576 | // Connect groups. We're building the following cycle: |
| 3577 | // G1: { g1s1, g2s1 }, g1s1 implicitly references g2s1, ditto for other |
| 3578 | // groups. |
| 3579 | { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3580 | HandleScope handle_scope(iso); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3581 | g1s1.MarkPartiallyDependent(); |
| 3582 | g1s2.MarkPartiallyDependent(); |
| 3583 | g2s1.MarkPartiallyDependent(); |
| 3584 | g2s2.MarkPartiallyDependent(); |
| 3585 | g3s1.MarkPartiallyDependent(); |
| 3586 | g3s2.MarkPartiallyDependent(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3587 | iso->SetObjectGroupId(g1s1, UniqueId(1)); |
| 3588 | iso->SetObjectGroupId(g1s2, UniqueId(1)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3589 | Local<Object>::New(iso, g1s1.As<Object>())->Set( |
| 3590 | v8_str("x"), Local<Value>::New(iso, g2s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3591 | iso->SetObjectGroupId(g2s1, UniqueId(2)); |
| 3592 | iso->SetObjectGroupId(g2s2, UniqueId(2)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3593 | Local<Object>::New(iso, g2s1.As<Object>())->Set( |
| 3594 | v8_str("x"), Local<Value>::New(iso, g3s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3595 | iso->SetObjectGroupId(g3s1, UniqueId(3)); |
| 3596 | iso->SetObjectGroupId(g3s2, UniqueId(3)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3597 | Local<Object>::New(iso, g3s1.As<Object>())->Set( |
| 3598 | v8_str("x"), Local<Value>::New(iso, g1s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3599 | } |
| 3600 | |
| 3601 | v8::internal::Heap* heap = reinterpret_cast<v8::internal::Isolate*>( |
| 3602 | iso)->heap(); |
| 3603 | heap->CollectGarbage(i::NEW_SPACE); |
| 3604 | |
| 3605 | // All objects should be alive. |
| 3606 | CHECK_EQ(0, counter.NumberOfWeakCalls()); |
| 3607 | |
| 3608 | // Weaken the root. |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3609 | root.MakeWeak(&counter, &WeakPointerCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3610 | root.MarkPartiallyDependent(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3611 | |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3612 | // Groups are deleted, rebuild groups. |
| 3613 | { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 3614 | HandleScope handle_scope(iso); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 3615 | g1s1.MarkPartiallyDependent(); |
| 3616 | g1s2.MarkPartiallyDependent(); |
| 3617 | g2s1.MarkPartiallyDependent(); |
| 3618 | g2s2.MarkPartiallyDependent(); |
| 3619 | g3s1.MarkPartiallyDependent(); |
| 3620 | g3s2.MarkPartiallyDependent(); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3621 | iso->SetObjectGroupId(g1s1, UniqueId(1)); |
| 3622 | iso->SetObjectGroupId(g1s2, UniqueId(1)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3623 | Local<Object>::New(iso, g1s1.As<Object>())->Set( |
| 3624 | v8_str("x"), Local<Value>::New(iso, g2s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3625 | iso->SetObjectGroupId(g2s1, UniqueId(2)); |
| 3626 | iso->SetObjectGroupId(g2s2, UniqueId(2)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3627 | Local<Object>::New(iso, g2s1.As<Object>())->Set( |
| 3628 | v8_str("x"), Local<Value>::New(iso, g3s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3629 | iso->SetObjectGroupId(g3s1, UniqueId(3)); |
| 3630 | iso->SetObjectGroupId(g3s2, UniqueId(3)); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 3631 | Local<Object>::New(iso, g3s1.As<Object>())->Set( |
| 3632 | v8_str("x"), Local<Value>::New(iso, g1s1)); |
danno@chromium.org | ca29dd8 | 2013-04-26 11:59:48 +0000 | [diff] [blame] | 3633 | } |
| 3634 | |
| 3635 | heap->CollectGarbage(i::NEW_SPACE); |
| 3636 | |
| 3637 | // All objects should be gone. 7 global handles in total. |
| 3638 | CHECK_EQ(7, counter.NumberOfWeakCalls()); |
| 3639 | } |
| 3640 | |
| 3641 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3642 | THREADED_TEST(ScriptException) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3643 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3644 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3645 | Local<Script> script = Script::Compile(v8_str("throw 'panama!';")); |
| 3646 | v8::TryCatch try_catch; |
| 3647 | Local<Value> result = script->Run(); |
| 3648 | CHECK(result.IsEmpty()); |
| 3649 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3650 | String::Utf8Value exception_value(try_catch.Exception()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3651 | CHECK_EQ(*exception_value, "panama!"); |
| 3652 | } |
| 3653 | |
| 3654 | |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 3655 | TEST(TryCatchCustomException) { |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 3656 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3657 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | eeb44b6 | 2012-11-13 13:56:09 +0000 | [diff] [blame] | 3658 | v8::TryCatch try_catch; |
| 3659 | CompileRun("function CustomError() { this.a = 'b'; }" |
| 3660 | "(function f() { throw new CustomError(); })();"); |
| 3661 | CHECK(try_catch.HasCaught()); |
| 3662 | CHECK(try_catch.Exception()->ToObject()-> |
| 3663 | Get(v8_str("a"))->Equals(v8_str("b"))); |
| 3664 | } |
| 3665 | |
| 3666 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3667 | bool message_received; |
| 3668 | |
| 3669 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3670 | static void check_message_0(v8::Handle<v8::Message> message, |
| 3671 | v8::Handle<Value> data) { |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3672 | CHECK_EQ(5.76, data->NumberValue()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 3673 | CHECK_EQ(6.75, message->GetScriptResourceName()->NumberValue()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3674 | CHECK_EQ(7.56, message->GetScriptData()->NumberValue()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3675 | CHECK(!message->IsSharedCrossOrigin()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3676 | message_received = true; |
| 3677 | } |
| 3678 | |
| 3679 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3680 | THREADED_TEST(MessageHandler0) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3681 | message_received = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3682 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3683 | CHECK(!message_received); |
hpayer@chromium.org | 8432c91 | 2013-02-28 15:55:26 +0000 | [diff] [blame] | 3684 | v8::V8::AddMessageListener(check_message_0, v8_num(5.76)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3685 | LocalContext context; |
| 3686 | v8::ScriptOrigin origin = |
| 3687 | v8::ScriptOrigin(v8_str("6.75")); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 3688 | v8::Handle<v8::Script> script = Script::Compile(v8_str("throw 'error'"), |
| 3689 | &origin); |
| 3690 | script->SetData(v8_str("7.56")); |
| 3691 | script->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3692 | CHECK(message_received); |
| 3693 | // clear out the message listener |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3694 | v8::V8::RemoveMessageListeners(check_message_0); |
| 3695 | } |
| 3696 | |
| 3697 | |
| 3698 | static void check_message_1(v8::Handle<v8::Message> message, |
| 3699 | v8::Handle<Value> data) { |
| 3700 | CHECK(data->IsNumber()); |
| 3701 | CHECK_EQ(1337, data->Int32Value()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3702 | CHECK(!message->IsSharedCrossOrigin()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3703 | message_received = true; |
| 3704 | } |
| 3705 | |
| 3706 | |
| 3707 | TEST(MessageHandler1) { |
| 3708 | message_received = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3709 | v8::HandleScope scope(CcTest::isolate()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3710 | CHECK(!message_received); |
| 3711 | v8::V8::AddMessageListener(check_message_1); |
| 3712 | LocalContext context; |
| 3713 | CompileRun("throw 1337;"); |
| 3714 | CHECK(message_received); |
| 3715 | // clear out the message listener |
| 3716 | v8::V8::RemoveMessageListeners(check_message_1); |
| 3717 | } |
| 3718 | |
| 3719 | |
| 3720 | static void check_message_2(v8::Handle<v8::Message> message, |
| 3721 | v8::Handle<Value> data) { |
| 3722 | LocalContext context; |
| 3723 | CHECK(data->IsObject()); |
| 3724 | v8::Local<v8::Value> hidden_property = |
| 3725 | v8::Object::Cast(*data)->GetHiddenValue(v8_str("hidden key")); |
| 3726 | CHECK(v8_str("hidden value")->Equals(hidden_property)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3727 | CHECK(!message->IsSharedCrossOrigin()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3728 | message_received = true; |
| 3729 | } |
| 3730 | |
| 3731 | |
| 3732 | TEST(MessageHandler2) { |
| 3733 | message_received = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3734 | v8::HandleScope scope(CcTest::isolate()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 3735 | CHECK(!message_received); |
| 3736 | v8::V8::AddMessageListener(check_message_2); |
| 3737 | LocalContext context; |
| 3738 | v8::Local<v8::Value> error = v8::Exception::Error(v8_str("custom error")); |
| 3739 | v8::Object::Cast(*error)->SetHiddenValue(v8_str("hidden key"), |
| 3740 | v8_str("hidden value")); |
| 3741 | context->Global()->Set(v8_str("error"), error); |
| 3742 | CompileRun("throw error;"); |
| 3743 | CHECK(message_received); |
| 3744 | // clear out the message listener |
| 3745 | v8::V8::RemoveMessageListeners(check_message_2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
| 3748 | |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3749 | static void check_message_3(v8::Handle<v8::Message> message, |
| 3750 | v8::Handle<Value> data) { |
| 3751 | CHECK(message->IsSharedCrossOrigin()); |
| 3752 | CHECK_EQ(6.75, message->GetScriptResourceName()->NumberValue()); |
| 3753 | message_received = true; |
| 3754 | } |
| 3755 | |
| 3756 | |
| 3757 | TEST(MessageHandler3) { |
| 3758 | message_received = false; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3759 | v8::Isolate* isolate = CcTest::isolate(); |
| 3760 | v8::HandleScope scope(isolate); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3761 | CHECK(!message_received); |
| 3762 | v8::V8::AddMessageListener(check_message_3); |
| 3763 | LocalContext context; |
| 3764 | v8::ScriptOrigin origin = |
| 3765 | v8::ScriptOrigin(v8_str("6.75"), |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3766 | v8::Integer::New(1, isolate), |
| 3767 | v8::Integer::New(2, isolate), |
| 3768 | v8::True(isolate)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3769 | v8::Handle<v8::Script> script = Script::Compile(v8_str("throw 'error'"), |
| 3770 | &origin); |
| 3771 | script->Run(); |
| 3772 | CHECK(message_received); |
| 3773 | // clear out the message listener |
| 3774 | v8::V8::RemoveMessageListeners(check_message_3); |
| 3775 | } |
| 3776 | |
| 3777 | |
| 3778 | static void check_message_4(v8::Handle<v8::Message> message, |
| 3779 | v8::Handle<Value> data) { |
| 3780 | CHECK(!message->IsSharedCrossOrigin()); |
| 3781 | CHECK_EQ(6.75, message->GetScriptResourceName()->NumberValue()); |
| 3782 | message_received = true; |
| 3783 | } |
| 3784 | |
| 3785 | |
| 3786 | TEST(MessageHandler4) { |
| 3787 | message_received = false; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3788 | v8::Isolate* isolate = CcTest::isolate(); |
| 3789 | v8::HandleScope scope(isolate); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3790 | CHECK(!message_received); |
| 3791 | v8::V8::AddMessageListener(check_message_4); |
| 3792 | LocalContext context; |
| 3793 | v8::ScriptOrigin origin = |
| 3794 | v8::ScriptOrigin(v8_str("6.75"), |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3795 | v8::Integer::New(1, isolate), |
| 3796 | v8::Integer::New(2, isolate), |
| 3797 | v8::False(isolate)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3798 | v8::Handle<v8::Script> script = Script::Compile(v8_str("throw 'error'"), |
| 3799 | &origin); |
| 3800 | script->Run(); |
| 3801 | CHECK(message_received); |
| 3802 | // clear out the message listener |
| 3803 | v8::V8::RemoveMessageListeners(check_message_4); |
| 3804 | } |
| 3805 | |
| 3806 | |
| 3807 | static void check_message_5a(v8::Handle<v8::Message> message, |
| 3808 | v8::Handle<Value> data) { |
| 3809 | CHECK(message->IsSharedCrossOrigin()); |
| 3810 | CHECK_EQ(6.75, message->GetScriptResourceName()->NumberValue()); |
| 3811 | message_received = true; |
| 3812 | } |
| 3813 | |
| 3814 | |
| 3815 | static void check_message_5b(v8::Handle<v8::Message> message, |
| 3816 | v8::Handle<Value> data) { |
| 3817 | CHECK(!message->IsSharedCrossOrigin()); |
| 3818 | CHECK_EQ(6.75, message->GetScriptResourceName()->NumberValue()); |
| 3819 | message_received = true; |
| 3820 | } |
| 3821 | |
| 3822 | |
| 3823 | TEST(MessageHandler5) { |
| 3824 | message_received = false; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3825 | v8::Isolate* isolate = CcTest::isolate(); |
| 3826 | v8::HandleScope scope(isolate); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3827 | CHECK(!message_received); |
| 3828 | v8::V8::AddMessageListener(check_message_5a); |
| 3829 | LocalContext context; |
| 3830 | v8::ScriptOrigin origin = |
| 3831 | v8::ScriptOrigin(v8_str("6.75"), |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3832 | v8::Integer::New(1, isolate), |
| 3833 | v8::Integer::New(2, isolate), |
| 3834 | v8::True(isolate)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3835 | v8::Handle<v8::Script> script = Script::Compile(v8_str("throw 'error'"), |
| 3836 | &origin); |
| 3837 | script->Run(); |
| 3838 | CHECK(message_received); |
| 3839 | // clear out the message listener |
| 3840 | v8::V8::RemoveMessageListeners(check_message_5a); |
| 3841 | |
| 3842 | message_received = false; |
| 3843 | v8::V8::AddMessageListener(check_message_5b); |
| 3844 | origin = |
| 3845 | v8::ScriptOrigin(v8_str("6.75"), |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 3846 | v8::Integer::New(1, isolate), |
| 3847 | v8::Integer::New(2, isolate), |
| 3848 | v8::False(isolate)); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 3849 | script = Script::Compile(v8_str("throw 'error'"), |
| 3850 | &origin); |
| 3851 | script->Run(); |
| 3852 | CHECK(message_received); |
| 3853 | // clear out the message listener |
| 3854 | v8::V8::RemoveMessageListeners(check_message_5b); |
| 3855 | } |
| 3856 | |
| 3857 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3858 | THREADED_TEST(GetSetProperty) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3859 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3860 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3861 | context->Global()->Set(v8_str("foo"), v8_num(14)); |
| 3862 | context->Global()->Set(v8_str("12"), v8_num(92)); |
| 3863 | context->Global()->Set(v8::Integer::New(16), v8_num(32)); |
| 3864 | context->Global()->Set(v8_num(13), v8_num(56)); |
| 3865 | Local<Value> foo = Script::Compile(v8_str("this.foo"))->Run(); |
| 3866 | CHECK_EQ(14, foo->Int32Value()); |
| 3867 | Local<Value> twelve = Script::Compile(v8_str("this[12]"))->Run(); |
| 3868 | CHECK_EQ(92, twelve->Int32Value()); |
| 3869 | Local<Value> sixteen = Script::Compile(v8_str("this[16]"))->Run(); |
| 3870 | CHECK_EQ(32, sixteen->Int32Value()); |
| 3871 | Local<Value> thirteen = Script::Compile(v8_str("this[13]"))->Run(); |
| 3872 | CHECK_EQ(56, thirteen->Int32Value()); |
| 3873 | CHECK_EQ(92, context->Global()->Get(v8::Integer::New(12))->Int32Value()); |
| 3874 | CHECK_EQ(92, context->Global()->Get(v8_str("12"))->Int32Value()); |
| 3875 | CHECK_EQ(92, context->Global()->Get(v8_num(12))->Int32Value()); |
| 3876 | CHECK_EQ(32, context->Global()->Get(v8::Integer::New(16))->Int32Value()); |
| 3877 | CHECK_EQ(32, context->Global()->Get(v8_str("16"))->Int32Value()); |
| 3878 | CHECK_EQ(32, context->Global()->Get(v8_num(16))->Int32Value()); |
| 3879 | CHECK_EQ(56, context->Global()->Get(v8::Integer::New(13))->Int32Value()); |
| 3880 | CHECK_EQ(56, context->Global()->Get(v8_str("13"))->Int32Value()); |
| 3881 | CHECK_EQ(56, context->Global()->Get(v8_num(13))->Int32Value()); |
| 3882 | } |
| 3883 | |
| 3884 | |
| 3885 | THREADED_TEST(PropertyAttributes) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3886 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3887 | v8::HandleScope scope(context->GetIsolate()); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3888 | // none |
| 3889 | Local<String> prop = v8_str("none"); |
| 3890 | context->Global()->Set(prop, v8_num(7)); |
| 3891 | CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(prop)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3892 | // read-only |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3893 | prop = v8_str("read_only"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3894 | context->Global()->Set(prop, v8_num(7), v8::ReadOnly); |
| 3895 | CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3896 | CHECK_EQ(v8::ReadOnly, context->Global()->GetPropertyAttributes(prop)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3897 | Script::Compile(v8_str("read_only = 9"))->Run(); |
| 3898 | CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); |
| 3899 | context->Global()->Set(prop, v8_num(10)); |
| 3900 | CHECK_EQ(7, context->Global()->Get(prop)->Int32Value()); |
| 3901 | // dont-delete |
| 3902 | prop = v8_str("dont_delete"); |
| 3903 | context->Global()->Set(prop, v8_num(13), v8::DontDelete); |
| 3904 | CHECK_EQ(13, context->Global()->Get(prop)->Int32Value()); |
| 3905 | Script::Compile(v8_str("delete dont_delete"))->Run(); |
| 3906 | CHECK_EQ(13, context->Global()->Get(prop)->Int32Value()); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3907 | CHECK_EQ(v8::DontDelete, context->Global()->GetPropertyAttributes(prop)); |
| 3908 | // dont-enum |
| 3909 | prop = v8_str("dont_enum"); |
| 3910 | context->Global()->Set(prop, v8_num(28), v8::DontEnum); |
| 3911 | CHECK_EQ(v8::DontEnum, context->Global()->GetPropertyAttributes(prop)); |
| 3912 | // absent |
| 3913 | prop = v8_str("absent"); |
| 3914 | CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(prop)); |
| 3915 | Local<Value> fake_prop = v8_num(1); |
| 3916 | CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(fake_prop)); |
| 3917 | // exception |
| 3918 | TryCatch try_catch; |
| 3919 | Local<Value> exception = |
| 3920 | CompileRun("({ toString: function() { throw 'exception';} })"); |
| 3921 | CHECK_EQ(v8::None, context->Global()->GetPropertyAttributes(exception)); |
| 3922 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 3923 | String::Utf8Value exception_value(try_catch.Exception()); |
rossberg@chromium.org | 717967f | 2011-07-20 13:44:42 +0000 | [diff] [blame] | 3924 | CHECK_EQ("exception", *exception_value); |
| 3925 | try_catch.Reset(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3926 | } |
| 3927 | |
| 3928 | |
| 3929 | THREADED_TEST(Array) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3930 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3931 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3932 | Local<v8::Array> array = v8::Array::New(); |
| 3933 | CHECK_EQ(0, array->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3934 | CHECK(array->Get(0)->IsUndefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3935 | CHECK(!array->Has(0)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3936 | CHECK(array->Get(100)->IsUndefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3937 | CHECK(!array->Has(100)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3938 | array->Set(2, v8_num(7)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3939 | CHECK_EQ(3, array->Length()); |
| 3940 | CHECK(!array->Has(0)); |
| 3941 | CHECK(!array->Has(1)); |
| 3942 | CHECK(array->Has(2)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3943 | CHECK_EQ(7, array->Get(2)->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3944 | Local<Value> obj = Script::Compile(v8_str("[1, 2, 3]"))->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3945 | Local<v8::Array> arr = obj.As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3946 | CHECK_EQ(3, arr->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3947 | CHECK_EQ(1, arr->Get(0)->Int32Value()); |
| 3948 | CHECK_EQ(2, arr->Get(1)->Int32Value()); |
| 3949 | CHECK_EQ(3, arr->Get(2)->Int32Value()); |
ricow@chromium.org | badaffc | 2011-03-17 12:15:27 +0000 | [diff] [blame] | 3950 | array = v8::Array::New(27); |
| 3951 | CHECK_EQ(27, array->Length()); |
| 3952 | array = v8::Array::New(-27); |
| 3953 | CHECK_EQ(0, array->Length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3954 | } |
| 3955 | |
| 3956 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3957 | void HandleF(const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 3958 | v8::HandleScope scope(args.GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3959 | ApiTestFuzzer::Fuzz(); |
| 3960 | Local<v8::Array> result = v8::Array::New(args.Length()); |
| 3961 | for (int i = 0; i < args.Length(); i++) |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3962 | result->Set(i, args[i]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 3963 | args.GetReturnValue().Set(scope.Close(result)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3964 | } |
| 3965 | |
| 3966 | |
| 3967 | THREADED_TEST(Vector) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 3968 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3969 | Local<ObjectTemplate> global = ObjectTemplate::New(); |
| 3970 | global->Set(v8_str("f"), v8::FunctionTemplate::New(HandleF)); |
| 3971 | LocalContext context(0, global); |
| 3972 | |
| 3973 | const char* fun = "f()"; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3974 | Local<v8::Array> a0 = CompileRun(fun).As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3975 | CHECK_EQ(0, a0->Length()); |
| 3976 | |
| 3977 | const char* fun2 = "f(11)"; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3978 | Local<v8::Array> a1 = CompileRun(fun2).As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3979 | CHECK_EQ(1, a1->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3980 | CHECK_EQ(11, a1->Get(0)->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3981 | |
| 3982 | const char* fun3 = "f(12, 13)"; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3983 | Local<v8::Array> a2 = CompileRun(fun3).As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3984 | CHECK_EQ(2, a2->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3985 | CHECK_EQ(12, a2->Get(0)->Int32Value()); |
| 3986 | CHECK_EQ(13, a2->Get(1)->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3987 | |
| 3988 | const char* fun4 = "f(14, 15, 16)"; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3989 | Local<v8::Array> a3 = CompileRun(fun4).As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3990 | CHECK_EQ(3, a3->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3991 | CHECK_EQ(14, a3->Get(0)->Int32Value()); |
| 3992 | CHECK_EQ(15, a3->Get(1)->Int32Value()); |
| 3993 | CHECK_EQ(16, a3->Get(2)->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3994 | |
| 3995 | const char* fun5 = "f(17, 18, 19, 20)"; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3996 | Local<v8::Array> a4 = CompileRun(fun5).As<v8::Array>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 3997 | CHECK_EQ(4, a4->Length()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 3998 | CHECK_EQ(17, a4->Get(0)->Int32Value()); |
| 3999 | CHECK_EQ(18, a4->Get(1)->Int32Value()); |
| 4000 | CHECK_EQ(19, a4->Get(2)->Int32Value()); |
| 4001 | CHECK_EQ(20, a4->Get(3)->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | |
| 4005 | THREADED_TEST(FunctionCall) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4006 | LocalContext context; |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 4007 | v8::Isolate* isolate = context->GetIsolate(); |
| 4008 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4009 | CompileRun( |
| 4010 | "function Foo() {" |
| 4011 | " var result = [];" |
| 4012 | " for (var i = 0; i < arguments.length; i++) {" |
| 4013 | " result.push(arguments[i]);" |
| 4014 | " }" |
| 4015 | " return result;" |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 4016 | "}" |
| 4017 | "function ReturnThisSloppy() {" |
| 4018 | " return this;" |
| 4019 | "}" |
| 4020 | "function ReturnThisStrict() {" |
| 4021 | " 'use strict';" |
| 4022 | " return this;" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4023 | "}"); |
| 4024 | Local<Function> Foo = |
| 4025 | Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 4026 | Local<Function> ReturnThisSloppy = |
| 4027 | Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisSloppy"))); |
| 4028 | Local<Function> ReturnThisStrict = |
| 4029 | Local<Function>::Cast(context->Global()->Get(v8_str("ReturnThisStrict"))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4030 | |
| 4031 | v8::Handle<Value>* args0 = NULL; |
| 4032 | Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->Call(Foo, 0, args0)); |
| 4033 | CHECK_EQ(0, a0->Length()); |
| 4034 | |
| 4035 | v8::Handle<Value> args1[] = { v8_num(1.1) }; |
| 4036 | Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->Call(Foo, 1, args1)); |
| 4037 | CHECK_EQ(1, a1->Length()); |
| 4038 | CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); |
| 4039 | |
| 4040 | v8::Handle<Value> args2[] = { v8_num(2.2), |
| 4041 | v8_num(3.3) }; |
| 4042 | Local<v8::Array> a2 = Local<v8::Array>::Cast(Foo->Call(Foo, 2, args2)); |
| 4043 | CHECK_EQ(2, a2->Length()); |
| 4044 | CHECK_EQ(2.2, a2->Get(v8::Integer::New(0))->NumberValue()); |
| 4045 | CHECK_EQ(3.3, a2->Get(v8::Integer::New(1))->NumberValue()); |
| 4046 | |
| 4047 | v8::Handle<Value> args3[] = { v8_num(4.4), |
| 4048 | v8_num(5.5), |
| 4049 | v8_num(6.6) }; |
| 4050 | Local<v8::Array> a3 = Local<v8::Array>::Cast(Foo->Call(Foo, 3, args3)); |
| 4051 | CHECK_EQ(3, a3->Length()); |
| 4052 | CHECK_EQ(4.4, a3->Get(v8::Integer::New(0))->NumberValue()); |
| 4053 | CHECK_EQ(5.5, a3->Get(v8::Integer::New(1))->NumberValue()); |
| 4054 | CHECK_EQ(6.6, a3->Get(v8::Integer::New(2))->NumberValue()); |
| 4055 | |
| 4056 | v8::Handle<Value> args4[] = { v8_num(7.7), |
| 4057 | v8_num(8.8), |
| 4058 | v8_num(9.9), |
| 4059 | v8_num(10.11) }; |
| 4060 | Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->Call(Foo, 4, args4)); |
| 4061 | CHECK_EQ(4, a4->Length()); |
| 4062 | CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); |
| 4063 | CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); |
| 4064 | CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); |
| 4065 | CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue()); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 4066 | |
| 4067 | Local<v8::Value> r1 = ReturnThisSloppy->Call(v8::Undefined(isolate), 0, NULL); |
| 4068 | CHECK(r1->StrictEquals(context->Global())); |
| 4069 | Local<v8::Value> r2 = ReturnThisSloppy->Call(v8::Null(isolate), 0, NULL); |
| 4070 | CHECK(r2->StrictEquals(context->Global())); |
| 4071 | Local<v8::Value> r3 = ReturnThisSloppy->Call(v8_num(42), 0, NULL); |
| 4072 | CHECK(r3->IsNumberObject()); |
| 4073 | CHECK_EQ(42.0, r3.As<v8::NumberObject>()->ValueOf()); |
| 4074 | Local<v8::Value> r4 = ReturnThisSloppy->Call(v8_str("hello"), 0, NULL); |
| 4075 | CHECK(r4->IsStringObject()); |
| 4076 | CHECK(r4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello"))); |
| 4077 | Local<v8::Value> r5 = ReturnThisSloppy->Call(v8::True(isolate), 0, NULL); |
| 4078 | CHECK(r5->IsBooleanObject()); |
| 4079 | CHECK(r5.As<v8::BooleanObject>()->ValueOf()); |
| 4080 | |
| 4081 | Local<v8::Value> r6 = ReturnThisStrict->Call(v8::Undefined(isolate), 0, NULL); |
| 4082 | CHECK(r6->IsUndefined()); |
| 4083 | Local<v8::Value> r7 = ReturnThisStrict->Call(v8::Null(isolate), 0, NULL); |
| 4084 | CHECK(r7->IsNull()); |
| 4085 | Local<v8::Value> r8 = ReturnThisStrict->Call(v8_num(42), 0, NULL); |
| 4086 | CHECK(r8->StrictEquals(v8_num(42))); |
| 4087 | Local<v8::Value> r9 = ReturnThisStrict->Call(v8_str("hello"), 0, NULL); |
| 4088 | CHECK(r9->StrictEquals(v8_str("hello"))); |
| 4089 | Local<v8::Value> r10 = ReturnThisStrict->Call(v8::True(isolate), 0, NULL); |
| 4090 | CHECK(r10->StrictEquals(v8::True(isolate))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4091 | } |
| 4092 | |
| 4093 | |
| 4094 | static const char* js_code_causing_out_of_memory = |
| 4095 | "var a = new Array(); while(true) a.push(a);"; |
| 4096 | |
| 4097 | |
| 4098 | // These tests run for a long time and prevent us from running tests |
| 4099 | // that come after them so they cannot run in parallel. |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4100 | TEST(OutOfMemory) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4101 | // It's not possible to read a snapshot into a heap with different dimensions. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4102 | if (i::Snapshot::IsEnabled()) return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4103 | // Set heap limits. |
| 4104 | static const int K = 1024; |
| 4105 | v8::ResourceConstraints constraints; |
| 4106 | constraints.set_max_young_space_size(256 * K); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 4107 | constraints.set_max_old_space_size(5 * K * K); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4108 | v8::SetResourceConstraints(&constraints); |
| 4109 | |
| 4110 | // Execute a script that causes out of memory. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4111 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4112 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4113 | v8::V8::IgnoreOutOfMemoryException(); |
| 4114 | Local<Script> script = |
| 4115 | Script::Compile(String::New(js_code_causing_out_of_memory)); |
| 4116 | Local<Value> result = script->Run(); |
| 4117 | |
| 4118 | // Check for out of memory state. |
| 4119 | CHECK(result.IsEmpty()); |
| 4120 | CHECK(context->HasOutOfMemoryException()); |
| 4121 | } |
| 4122 | |
| 4123 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4124 | void ProvokeOutOfMemory(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4125 | ApiTestFuzzer::Fuzz(); |
| 4126 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4127 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4128 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4129 | Local<Script> script = |
| 4130 | Script::Compile(String::New(js_code_causing_out_of_memory)); |
| 4131 | Local<Value> result = script->Run(); |
| 4132 | |
| 4133 | // Check for out of memory state. |
| 4134 | CHECK(result.IsEmpty()); |
| 4135 | CHECK(context->HasOutOfMemoryException()); |
| 4136 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4137 | args.GetReturnValue().Set(result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4138 | } |
| 4139 | |
| 4140 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 4141 | TEST(OutOfMemoryNested) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4142 | // It's not possible to read a snapshot into a heap with different dimensions. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4143 | if (i::Snapshot::IsEnabled()) return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4144 | // Set heap limits. |
| 4145 | static const int K = 1024; |
| 4146 | v8::ResourceConstraints constraints; |
| 4147 | constraints.set_max_young_space_size(256 * K); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 4148 | constraints.set_max_old_space_size(5 * K * K); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4149 | v8::SetResourceConstraints(&constraints); |
| 4150 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4151 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4152 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4153 | templ->Set(v8_str("ProvokeOutOfMemory"), |
| 4154 | v8::FunctionTemplate::New(ProvokeOutOfMemory)); |
| 4155 | LocalContext context(0, templ); |
| 4156 | v8::V8::IgnoreOutOfMemoryException(); |
| 4157 | Local<Value> result = CompileRun( |
| 4158 | "var thrown = false;" |
| 4159 | "try {" |
| 4160 | " ProvokeOutOfMemory();" |
| 4161 | "} catch (e) {" |
| 4162 | " thrown = true;" |
| 4163 | "}"); |
| 4164 | // Check for out of memory state. |
| 4165 | CHECK(result.IsEmpty()); |
| 4166 | CHECK(context->HasOutOfMemoryException()); |
| 4167 | } |
| 4168 | |
| 4169 | |
| 4170 | TEST(HugeConsStringOutOfMemory) { |
| 4171 | // It's not possible to read a snapshot into a heap with different dimensions. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 4172 | if (i::Snapshot::IsEnabled()) return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4173 | // Set heap limits. |
| 4174 | static const int K = 1024; |
| 4175 | v8::ResourceConstraints constraints; |
| 4176 | constraints.set_max_young_space_size(256 * K); |
machenbach@chromium.org | c1789ee | 2013-07-05 07:09:57 +0000 | [diff] [blame] | 4177 | constraints.set_max_old_space_size(4 * K * K); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4178 | v8::SetResourceConstraints(&constraints); |
| 4179 | |
| 4180 | // Execute a script that causes out of memory. |
| 4181 | v8::V8::IgnoreOutOfMemoryException(); |
| 4182 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4183 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4184 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 4185 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4186 | // Build huge string. This should fail with out of memory exception. |
| 4187 | Local<Value> result = CompileRun( |
| 4188 | "var str = Array.prototype.join.call({length: 513}, \"A\").toUpperCase();" |
christian.plesner.hansen@gmail.com | 9d58c2b | 2009-10-16 11:48:38 +0000 | [diff] [blame] | 4189 | "for (var i = 0; i < 22; i++) { str = str + str; }"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4190 | |
| 4191 | // Check for out of memory state. |
| 4192 | CHECK(result.IsEmpty()); |
| 4193 | CHECK(context->HasOutOfMemoryException()); |
| 4194 | } |
| 4195 | |
| 4196 | |
| 4197 | THREADED_TEST(ConstructCall) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4198 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4199 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4200 | CompileRun( |
| 4201 | "function Foo() {" |
| 4202 | " var result = [];" |
| 4203 | " for (var i = 0; i < arguments.length; i++) {" |
| 4204 | " result.push(arguments[i]);" |
| 4205 | " }" |
| 4206 | " return result;" |
| 4207 | "}"); |
| 4208 | Local<Function> Foo = |
| 4209 | Local<Function>::Cast(context->Global()->Get(v8_str("Foo"))); |
| 4210 | |
| 4211 | v8::Handle<Value>* args0 = NULL; |
| 4212 | Local<v8::Array> a0 = Local<v8::Array>::Cast(Foo->NewInstance(0, args0)); |
| 4213 | CHECK_EQ(0, a0->Length()); |
| 4214 | |
| 4215 | v8::Handle<Value> args1[] = { v8_num(1.1) }; |
| 4216 | Local<v8::Array> a1 = Local<v8::Array>::Cast(Foo->NewInstance(1, args1)); |
| 4217 | CHECK_EQ(1, a1->Length()); |
| 4218 | CHECK_EQ(1.1, a1->Get(v8::Integer::New(0))->NumberValue()); |
| 4219 | |
| 4220 | v8::Handle<Value> args2[] = { v8_num(2.2), |
| 4221 | v8_num(3.3) }; |
| 4222 | Local<v8::Array> a2 = Local<v8::Array>::Cast(Foo->NewInstance(2, args2)); |
| 4223 | CHECK_EQ(2, a2->Length()); |
| 4224 | CHECK_EQ(2.2, a2->Get(v8::Integer::New(0))->NumberValue()); |
| 4225 | CHECK_EQ(3.3, a2->Get(v8::Integer::New(1))->NumberValue()); |
| 4226 | |
| 4227 | v8::Handle<Value> args3[] = { v8_num(4.4), |
| 4228 | v8_num(5.5), |
| 4229 | v8_num(6.6) }; |
| 4230 | Local<v8::Array> a3 = Local<v8::Array>::Cast(Foo->NewInstance(3, args3)); |
| 4231 | CHECK_EQ(3, a3->Length()); |
| 4232 | CHECK_EQ(4.4, a3->Get(v8::Integer::New(0))->NumberValue()); |
| 4233 | CHECK_EQ(5.5, a3->Get(v8::Integer::New(1))->NumberValue()); |
| 4234 | CHECK_EQ(6.6, a3->Get(v8::Integer::New(2))->NumberValue()); |
| 4235 | |
| 4236 | v8::Handle<Value> args4[] = { v8_num(7.7), |
| 4237 | v8_num(8.8), |
| 4238 | v8_num(9.9), |
| 4239 | v8_num(10.11) }; |
| 4240 | Local<v8::Array> a4 = Local<v8::Array>::Cast(Foo->NewInstance(4, args4)); |
| 4241 | CHECK_EQ(4, a4->Length()); |
| 4242 | CHECK_EQ(7.7, a4->Get(v8::Integer::New(0))->NumberValue()); |
| 4243 | CHECK_EQ(8.8, a4->Get(v8::Integer::New(1))->NumberValue()); |
| 4244 | CHECK_EQ(9.9, a4->Get(v8::Integer::New(2))->NumberValue()); |
| 4245 | CHECK_EQ(10.11, a4->Get(v8::Integer::New(3))->NumberValue()); |
| 4246 | } |
| 4247 | |
| 4248 | |
| 4249 | static void CheckUncle(v8::TryCatch* try_catch) { |
| 4250 | CHECK(try_catch->HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 4251 | String::Utf8Value str_value(try_catch->Exception()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4252 | CHECK_EQ(*str_value, "uncle?"); |
| 4253 | try_catch->Reset(); |
| 4254 | } |
| 4255 | |
| 4256 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4257 | THREADED_TEST(ConversionNumber) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4258 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4259 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4260 | // Very large number. |
| 4261 | CompileRun("var obj = Math.pow(2,32) * 1237;"); |
| 4262 | Local<Value> obj = env->Global()->Get(v8_str("obj")); |
| 4263 | CHECK_EQ(5312874545152.0, obj->ToNumber()->Value()); |
| 4264 | CHECK_EQ(0, obj->ToInt32()->Value()); |
| 4265 | CHECK(0u == obj->ToUint32()->Value()); // NOLINT - no CHECK_EQ for unsigned. |
| 4266 | // Large number. |
| 4267 | CompileRun("var obj = -1234567890123;"); |
| 4268 | obj = env->Global()->Get(v8_str("obj")); |
| 4269 | CHECK_EQ(-1234567890123.0, obj->ToNumber()->Value()); |
| 4270 | CHECK_EQ(-1912276171, obj->ToInt32()->Value()); |
| 4271 | CHECK(2382691125u == obj->ToUint32()->Value()); // NOLINT |
| 4272 | // Small positive integer. |
| 4273 | CompileRun("var obj = 42;"); |
| 4274 | obj = env->Global()->Get(v8_str("obj")); |
| 4275 | CHECK_EQ(42.0, obj->ToNumber()->Value()); |
| 4276 | CHECK_EQ(42, obj->ToInt32()->Value()); |
| 4277 | CHECK(42u == obj->ToUint32()->Value()); // NOLINT |
| 4278 | // Negative integer. |
| 4279 | CompileRun("var obj = -37;"); |
| 4280 | obj = env->Global()->Get(v8_str("obj")); |
| 4281 | CHECK_EQ(-37.0, obj->ToNumber()->Value()); |
| 4282 | CHECK_EQ(-37, obj->ToInt32()->Value()); |
| 4283 | CHECK(4294967259u == obj->ToUint32()->Value()); // NOLINT |
| 4284 | // Positive non-int32 integer. |
| 4285 | CompileRun("var obj = 0x81234567;"); |
| 4286 | obj = env->Global()->Get(v8_str("obj")); |
| 4287 | CHECK_EQ(2166572391.0, obj->ToNumber()->Value()); |
| 4288 | CHECK_EQ(-2128394905, obj->ToInt32()->Value()); |
| 4289 | CHECK(2166572391u == obj->ToUint32()->Value()); // NOLINT |
| 4290 | // Fraction. |
| 4291 | CompileRun("var obj = 42.3;"); |
| 4292 | obj = env->Global()->Get(v8_str("obj")); |
| 4293 | CHECK_EQ(42.3, obj->ToNumber()->Value()); |
| 4294 | CHECK_EQ(42, obj->ToInt32()->Value()); |
| 4295 | CHECK(42u == obj->ToUint32()->Value()); // NOLINT |
| 4296 | // Large negative fraction. |
| 4297 | CompileRun("var obj = -5726623061.75;"); |
| 4298 | obj = env->Global()->Get(v8_str("obj")); |
| 4299 | CHECK_EQ(-5726623061.75, obj->ToNumber()->Value()); |
| 4300 | CHECK_EQ(-1431655765, obj->ToInt32()->Value()); |
| 4301 | CHECK(2863311531u == obj->ToUint32()->Value()); // NOLINT |
| 4302 | } |
| 4303 | |
| 4304 | |
| 4305 | THREADED_TEST(isNumberType) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4306 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4307 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4308 | // Very large number. |
| 4309 | CompileRun("var obj = Math.pow(2,32) * 1237;"); |
| 4310 | Local<Value> obj = env->Global()->Get(v8_str("obj")); |
| 4311 | CHECK(!obj->IsInt32()); |
| 4312 | CHECK(!obj->IsUint32()); |
| 4313 | // Large negative number. |
| 4314 | CompileRun("var obj = -1234567890123;"); |
| 4315 | obj = env->Global()->Get(v8_str("obj")); |
| 4316 | CHECK(!obj->IsInt32()); |
| 4317 | CHECK(!obj->IsUint32()); |
| 4318 | // Small positive integer. |
| 4319 | CompileRun("var obj = 42;"); |
| 4320 | obj = env->Global()->Get(v8_str("obj")); |
| 4321 | CHECK(obj->IsInt32()); |
| 4322 | CHECK(obj->IsUint32()); |
| 4323 | // Negative integer. |
| 4324 | CompileRun("var obj = -37;"); |
| 4325 | obj = env->Global()->Get(v8_str("obj")); |
| 4326 | CHECK(obj->IsInt32()); |
| 4327 | CHECK(!obj->IsUint32()); |
| 4328 | // Positive non-int32 integer. |
| 4329 | CompileRun("var obj = 0x81234567;"); |
| 4330 | obj = env->Global()->Get(v8_str("obj")); |
| 4331 | CHECK(!obj->IsInt32()); |
| 4332 | CHECK(obj->IsUint32()); |
| 4333 | // Fraction. |
| 4334 | CompileRun("var obj = 42.3;"); |
| 4335 | obj = env->Global()->Get(v8_str("obj")); |
| 4336 | CHECK(!obj->IsInt32()); |
| 4337 | CHECK(!obj->IsUint32()); |
| 4338 | // Large negative fraction. |
| 4339 | CompileRun("var obj = -5726623061.75;"); |
| 4340 | obj = env->Global()->Get(v8_str("obj")); |
| 4341 | CHECK(!obj->IsInt32()); |
| 4342 | CHECK(!obj->IsUint32()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 4343 | // Positive zero |
| 4344 | CompileRun("var obj = 0.0;"); |
| 4345 | obj = env->Global()->Get(v8_str("obj")); |
| 4346 | CHECK(obj->IsInt32()); |
| 4347 | CHECK(obj->IsUint32()); |
| 4348 | // Positive zero |
| 4349 | CompileRun("var obj = -0.0;"); |
| 4350 | obj = env->Global()->Get(v8_str("obj")); |
| 4351 | CHECK(!obj->IsInt32()); |
| 4352 | CHECK(!obj->IsUint32()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4356 | THREADED_TEST(ConversionException) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4357 | LocalContext env; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4358 | v8::Isolate* isolate = env->GetIsolate(); |
| 4359 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4360 | CompileRun( |
| 4361 | "function TestClass() { };" |
| 4362 | "TestClass.prototype.toString = function () { throw 'uncle?'; };" |
| 4363 | "var obj = new TestClass();"); |
| 4364 | Local<Value> obj = env->Global()->Get(v8_str("obj")); |
| 4365 | |
| 4366 | v8::TryCatch try_catch; |
| 4367 | |
| 4368 | Local<Value> to_string_result = obj->ToString(); |
| 4369 | CHECK(to_string_result.IsEmpty()); |
| 4370 | CheckUncle(&try_catch); |
| 4371 | |
| 4372 | Local<Value> to_number_result = obj->ToNumber(); |
| 4373 | CHECK(to_number_result.IsEmpty()); |
| 4374 | CheckUncle(&try_catch); |
| 4375 | |
| 4376 | Local<Value> to_integer_result = obj->ToInteger(); |
| 4377 | CHECK(to_integer_result.IsEmpty()); |
| 4378 | CheckUncle(&try_catch); |
| 4379 | |
| 4380 | Local<Value> to_uint32_result = obj->ToUint32(); |
| 4381 | CHECK(to_uint32_result.IsEmpty()); |
| 4382 | CheckUncle(&try_catch); |
| 4383 | |
| 4384 | Local<Value> to_int32_result = obj->ToInt32(); |
| 4385 | CHECK(to_int32_result.IsEmpty()); |
| 4386 | CheckUncle(&try_catch); |
| 4387 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4388 | Local<Value> to_object_result = v8::Undefined(isolate)->ToObject(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4389 | CHECK(to_object_result.IsEmpty()); |
| 4390 | CHECK(try_catch.HasCaught()); |
| 4391 | try_catch.Reset(); |
| 4392 | |
| 4393 | int32_t int32_value = obj->Int32Value(); |
| 4394 | CHECK_EQ(0, int32_value); |
| 4395 | CheckUncle(&try_catch); |
| 4396 | |
| 4397 | uint32_t uint32_value = obj->Uint32Value(); |
| 4398 | CHECK_EQ(0, uint32_value); |
| 4399 | CheckUncle(&try_catch); |
| 4400 | |
| 4401 | double number_value = obj->NumberValue(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 4402 | CHECK_NE(0, std::isnan(number_value)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4403 | CheckUncle(&try_catch); |
| 4404 | |
| 4405 | int64_t integer_value = obj->IntegerValue(); |
| 4406 | CHECK_EQ(0.0, static_cast<double>(integer_value)); |
| 4407 | CheckUncle(&try_catch); |
| 4408 | } |
| 4409 | |
| 4410 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4411 | void ThrowFromC(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4412 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4413 | args.GetIsolate()->ThrowException(v8_str("konto")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4414 | } |
| 4415 | |
| 4416 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4417 | void CCatcher(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 4418 | if (args.Length() < 1) { |
| 4419 | args.GetReturnValue().Set(false); |
| 4420 | return; |
| 4421 | } |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4422 | v8::HandleScope scope(args.GetIsolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4423 | v8::TryCatch try_catch; |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4424 | Local<Value> result = v8::Script::Compile(args[0]->ToString())->Run(); |
| 4425 | CHECK(!try_catch.HasCaught() || result.IsEmpty()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4426 | args.GetReturnValue().Set(try_catch.HasCaught()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4427 | } |
| 4428 | |
| 4429 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4430 | THREADED_TEST(APICatch) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4431 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4432 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4433 | templ->Set(v8_str("ThrowFromC"), |
| 4434 | v8::FunctionTemplate::New(ThrowFromC)); |
| 4435 | LocalContext context(0, templ); |
| 4436 | CompileRun( |
| 4437 | "var thrown = false;" |
| 4438 | "try {" |
| 4439 | " ThrowFromC();" |
| 4440 | "} catch (e) {" |
| 4441 | " thrown = true;" |
| 4442 | "}"); |
| 4443 | Local<Value> thrown = context->Global()->Get(v8_str("thrown")); |
| 4444 | CHECK(thrown->BooleanValue()); |
| 4445 | } |
| 4446 | |
| 4447 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4448 | THREADED_TEST(APIThrowTryCatch) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4449 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4450 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4451 | templ->Set(v8_str("ThrowFromC"), |
| 4452 | v8::FunctionTemplate::New(ThrowFromC)); |
| 4453 | LocalContext context(0, templ); |
| 4454 | v8::TryCatch try_catch; |
| 4455 | CompileRun("ThrowFromC();"); |
| 4456 | CHECK(try_catch.HasCaught()); |
| 4457 | } |
| 4458 | |
| 4459 | |
| 4460 | // Test that a try-finally block doesn't shadow a try-catch block |
| 4461 | // when setting up an external handler. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4462 | // |
| 4463 | // BUG(271): Some of the exception propagation does not work on the |
| 4464 | // ARM simulator because the simulator separates the C++ stack and the |
| 4465 | // JS stack. This test therefore fails on the simulator. The test is |
| 4466 | // not threaded to allow the threading tests to run on the simulator. |
| 4467 | TEST(TryCatchInTryFinally) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4468 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4469 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4470 | templ->Set(v8_str("CCatcher"), |
| 4471 | v8::FunctionTemplate::New(CCatcher)); |
| 4472 | LocalContext context(0, templ); |
| 4473 | Local<Value> result = CompileRun("try {" |
| 4474 | " try {" |
| 4475 | " CCatcher('throw 7;');" |
| 4476 | " } finally {" |
| 4477 | " }" |
| 4478 | "} catch (e) {" |
| 4479 | "}"); |
| 4480 | CHECK(result->IsTrue()); |
| 4481 | } |
| 4482 | |
| 4483 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4484 | static void check_reference_error_message( |
| 4485 | v8::Handle<v8::Message> message, |
| 4486 | v8::Handle<v8::Value> data) { |
| 4487 | const char* reference_error = "Uncaught ReferenceError: asdf is not defined"; |
| 4488 | CHECK(message->Get()->Equals(v8_str(reference_error))); |
| 4489 | } |
| 4490 | |
| 4491 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4492 | static void Fail(const v8::FunctionCallbackInfo<v8::Value>& args) { |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 4493 | ApiTestFuzzer::Fuzz(); |
| 4494 | CHECK(false); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 4495 | } |
| 4496 | |
| 4497 | |
| 4498 | // Test that overwritten methods are not invoked on uncaught exception |
| 4499 | // formatting. However, they are invoked when performing normal error |
| 4500 | // string conversions. |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4501 | TEST(APIThrowMessageOverwrittenToString) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4502 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4503 | v8::V8::AddMessageListener(check_reference_error_message); |
kmillikin@chromium.org | 31b1277 | 2011-02-02 16:08:26 +0000 | [diff] [blame] | 4504 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4505 | templ->Set(v8_str("fail"), v8::FunctionTemplate::New(Fail)); |
| 4506 | LocalContext context(NULL, templ); |
| 4507 | CompileRun("asdf;"); |
| 4508 | CompileRun("var limit = {};" |
| 4509 | "limit.valueOf = fail;" |
| 4510 | "Error.stackTraceLimit = limit;"); |
| 4511 | CompileRun("asdf"); |
| 4512 | CompileRun("Array.prototype.pop = fail;"); |
| 4513 | CompileRun("Object.prototype.hasOwnProperty = fail;"); |
| 4514 | CompileRun("Object.prototype.toString = function f() { return 'Yikes'; }"); |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 4515 | CompileRun("Number.prototype.toString = function f() { return 'Yikes'; }"); |
| 4516 | CompileRun("String.prototype.toString = function f() { return 'Yikes'; }"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4517 | CompileRun("ReferenceError.prototype.toString =" |
| 4518 | " function() { return 'Whoops' }"); |
| 4519 | CompileRun("asdf;"); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 4520 | CompileRun("ReferenceError.prototype.constructor.name = void 0;"); |
| 4521 | CompileRun("asdf;"); |
| 4522 | CompileRun("ReferenceError.prototype.constructor = void 0;"); |
| 4523 | CompileRun("asdf;"); |
ager@chromium.org | 0ee099b | 2011-01-25 14:06:47 +0000 | [diff] [blame] | 4524 | CompileRun("ReferenceError.prototype.__proto__ = new Object();"); |
| 4525 | CompileRun("asdf;"); |
| 4526 | CompileRun("ReferenceError.prototype = new Object();"); |
| 4527 | CompileRun("asdf;"); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4528 | v8::Handle<Value> string = CompileRun("try { asdf; } catch(e) { e + ''; }"); |
| 4529 | CHECK(string->Equals(v8_str("Whoops"))); |
ager@chromium.org | 378b34e | 2011-01-28 08:04:38 +0000 | [diff] [blame] | 4530 | CompileRun("ReferenceError.prototype.constructor = new Object();" |
| 4531 | "ReferenceError.prototype.constructor.name = 1;" |
| 4532 | "Number.prototype.toString = function() { return 'Whoops'; };" |
| 4533 | "ReferenceError.prototype.toString = Object.prototype.toString;"); |
| 4534 | CompileRun("asdf;"); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4535 | v8::V8::RemoveMessageListeners(check_reference_error_message); |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 4536 | } |
| 4537 | |
| 4538 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 4539 | static void check_custom_error_tostring( |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4540 | v8::Handle<v8::Message> message, |
| 4541 | v8::Handle<v8::Value> data) { |
| 4542 | const char* uncaught_error = "Uncaught MyError toString"; |
| 4543 | CHECK(message->Get()->Equals(v8_str(uncaught_error))); |
| 4544 | } |
| 4545 | |
| 4546 | |
| 4547 | TEST(CustomErrorToString) { |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4548 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4549 | v8::HandleScope scope(context->GetIsolate()); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 4550 | v8::V8::AddMessageListener(check_custom_error_tostring); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4551 | CompileRun( |
| 4552 | "function MyError(name, message) { " |
| 4553 | " this.name = name; " |
| 4554 | " this.message = message; " |
| 4555 | "} " |
| 4556 | "MyError.prototype = Object.create(Error.prototype); " |
| 4557 | "MyError.prototype.toString = function() { " |
| 4558 | " return 'MyError toString'; " |
| 4559 | "}; " |
| 4560 | "throw new MyError('my name', 'my message'); "); |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 4561 | v8::V8::RemoveMessageListeners(check_custom_error_tostring); |
| 4562 | } |
| 4563 | |
| 4564 | |
| 4565 | static void check_custom_error_message( |
| 4566 | v8::Handle<v8::Message> message, |
| 4567 | v8::Handle<v8::Value> data) { |
| 4568 | const char* uncaught_error = "Uncaught MyError: my message"; |
| 4569 | printf("%s\n", *v8::String::Utf8Value(message->Get())); |
| 4570 | CHECK(message->Get()->Equals(v8_str(uncaught_error))); |
| 4571 | } |
| 4572 | |
| 4573 | |
| 4574 | TEST(CustomErrorMessage) { |
| 4575 | LocalContext context; |
| 4576 | v8::HandleScope scope(context->GetIsolate()); |
| 4577 | v8::V8::AddMessageListener(check_custom_error_message); |
| 4578 | |
| 4579 | // Handlebars. |
| 4580 | CompileRun( |
| 4581 | "function MyError(msg) { " |
| 4582 | " this.name = 'MyError'; " |
| 4583 | " this.message = msg; " |
| 4584 | "} " |
| 4585 | "MyError.prototype = new Error(); " |
| 4586 | "throw new MyError('my message'); "); |
| 4587 | |
| 4588 | // Closure. |
| 4589 | CompileRun( |
| 4590 | "function MyError(msg) { " |
| 4591 | " this.name = 'MyError'; " |
| 4592 | " this.message = msg; " |
| 4593 | "} " |
| 4594 | "inherits = function(childCtor, parentCtor) { " |
| 4595 | " function tempCtor() {}; " |
| 4596 | " tempCtor.prototype = parentCtor.prototype; " |
| 4597 | " childCtor.superClass_ = parentCtor.prototype; " |
| 4598 | " childCtor.prototype = new tempCtor(); " |
| 4599 | " childCtor.prototype.constructor = childCtor; " |
| 4600 | "}; " |
| 4601 | "inherits(MyError, Error); " |
| 4602 | "throw new MyError('my message'); "); |
| 4603 | |
| 4604 | // Object.create. |
| 4605 | CompileRun( |
| 4606 | "function MyError(msg) { " |
| 4607 | " this.name = 'MyError'; " |
| 4608 | " this.message = msg; " |
| 4609 | "} " |
| 4610 | "MyError.prototype = Object.create(Error.prototype); " |
| 4611 | "throw new MyError('my message'); "); |
| 4612 | |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 4613 | v8::V8::RemoveMessageListeners(check_custom_error_message); |
| 4614 | } |
| 4615 | |
| 4616 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4617 | static void receive_message(v8::Handle<v8::Message> message, |
| 4618 | v8::Handle<v8::Value> data) { |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4619 | message->Get(); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4620 | message_received = true; |
| 4621 | } |
| 4622 | |
| 4623 | |
| 4624 | TEST(APIThrowMessage) { |
| 4625 | message_received = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4626 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4627 | v8::V8::AddMessageListener(receive_message); |
| 4628 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4629 | templ->Set(v8_str("ThrowFromC"), |
| 4630 | v8::FunctionTemplate::New(ThrowFromC)); |
| 4631 | LocalContext context(0, templ); |
| 4632 | CompileRun("ThrowFromC();"); |
| 4633 | CHECK(message_received); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4634 | v8::V8::RemoveMessageListeners(receive_message); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | |
| 4638 | TEST(APIThrowMessageAndVerboseTryCatch) { |
| 4639 | message_received = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4640 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4641 | v8::V8::AddMessageListener(receive_message); |
| 4642 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4643 | templ->Set(v8_str("ThrowFromC"), |
| 4644 | v8::FunctionTemplate::New(ThrowFromC)); |
| 4645 | LocalContext context(0, templ); |
| 4646 | v8::TryCatch try_catch; |
| 4647 | try_catch.SetVerbose(true); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4648 | Local<Value> result = CompileRun("ThrowFromC();"); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4649 | CHECK(try_catch.HasCaught()); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4650 | CHECK(result.IsEmpty()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4651 | CHECK(message_received); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 4652 | v8::V8::RemoveMessageListeners(receive_message); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4653 | } |
| 4654 | |
| 4655 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4656 | TEST(APIStackOverflowAndVerboseTryCatch) { |
| 4657 | message_received = false; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4658 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4659 | v8::HandleScope scope(context->GetIsolate()); |
| 4660 | v8::V8::AddMessageListener(receive_message); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 4661 | v8::TryCatch try_catch; |
| 4662 | try_catch.SetVerbose(true); |
| 4663 | Local<Value> result = CompileRun("function foo() { foo(); } foo();"); |
| 4664 | CHECK(try_catch.HasCaught()); |
| 4665 | CHECK(result.IsEmpty()); |
| 4666 | CHECK(message_received); |
| 4667 | v8::V8::RemoveMessageListeners(receive_message); |
| 4668 | } |
| 4669 | |
| 4670 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4671 | THREADED_TEST(ExternalScriptException) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4672 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4673 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4674 | templ->Set(v8_str("ThrowFromC"), |
| 4675 | v8::FunctionTemplate::New(ThrowFromC)); |
| 4676 | LocalContext context(0, templ); |
| 4677 | |
| 4678 | v8::TryCatch try_catch; |
| 4679 | Local<Script> script |
| 4680 | = Script::Compile(v8_str("ThrowFromC(); throw 'panama';")); |
| 4681 | Local<Value> result = script->Run(); |
| 4682 | CHECK(result.IsEmpty()); |
| 4683 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 4684 | String::Utf8Value exception_value(try_catch.Exception()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4685 | CHECK_EQ("konto", *exception_value); |
| 4686 | } |
| 4687 | |
| 4688 | |
| 4689 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4690 | void CThrowCountDown(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4691 | ApiTestFuzzer::Fuzz(); |
| 4692 | CHECK_EQ(4, args.Length()); |
| 4693 | int count = args[0]->Int32Value(); |
| 4694 | int cInterval = args[2]->Int32Value(); |
| 4695 | if (count == 0) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4696 | args.GetIsolate()->ThrowException(v8_str("FromC")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4697 | return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4698 | } else { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4699 | Local<v8::Object> global = |
| 4700 | args.GetIsolate()->GetCurrentContext()->Global(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4701 | Local<Value> fun = global->Get(v8_str("JSThrowCountDown")); |
| 4702 | v8::Handle<Value> argv[] = { v8_num(count - 1), |
| 4703 | args[1], |
| 4704 | args[2], |
| 4705 | args[3] }; |
| 4706 | if (count % cInterval == 0) { |
| 4707 | v8::TryCatch try_catch; |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 4708 | Local<Value> result = fun.As<Function>()->Call(global, 4, argv); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4709 | int expected = args[3]->Int32Value(); |
| 4710 | if (try_catch.HasCaught()) { |
| 4711 | CHECK_EQ(expected, count); |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4712 | CHECK(result.IsEmpty()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4713 | CHECK(!CcTest::i_isolate()->has_scheduled_exception()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4714 | } else { |
| 4715 | CHECK_NE(expected, count); |
| 4716 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4717 | args.GetReturnValue().Set(result); |
| 4718 | return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4719 | } else { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4720 | args.GetReturnValue().Set(fun.As<Function>()->Call(global, 4, argv)); |
| 4721 | return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4722 | } |
| 4723 | } |
| 4724 | } |
| 4725 | |
| 4726 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4727 | void JSCheck(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4728 | ApiTestFuzzer::Fuzz(); |
| 4729 | CHECK_EQ(3, args.Length()); |
| 4730 | bool equality = args[0]->BooleanValue(); |
| 4731 | int count = args[1]->Int32Value(); |
| 4732 | int expected = args[2]->Int32Value(); |
| 4733 | if (equality) { |
| 4734 | CHECK_EQ(count, expected); |
| 4735 | } else { |
| 4736 | CHECK_NE(count, expected); |
| 4737 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4738 | } |
| 4739 | |
| 4740 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4741 | THREADED_TEST(EvalInTryFinally) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4742 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4743 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 4744 | v8::TryCatch try_catch; |
| 4745 | CompileRun("(function() {" |
| 4746 | " try {" |
| 4747 | " eval('asldkf (*&^&*^');" |
| 4748 | " } finally {" |
| 4749 | " return;" |
| 4750 | " }" |
| 4751 | "})()"); |
| 4752 | CHECK(!try_catch.HasCaught()); |
| 4753 | } |
| 4754 | |
| 4755 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4756 | // This test works by making a stack of alternating JavaScript and C |
| 4757 | // activations. These activations set up exception handlers with regular |
| 4758 | // intervals, one interval for C activations and another for JavaScript |
| 4759 | // activations. When enough activations have been created an exception is |
| 4760 | // thrown and we check that the right activation catches the exception and that |
| 4761 | // no other activations do. The right activation is always the topmost one with |
| 4762 | // a handler, regardless of whether it is in JavaScript or C. |
| 4763 | // |
| 4764 | // The notation used to describe a test case looks like this: |
| 4765 | // |
| 4766 | // *JS[4] *C[3] @JS[2] C[1] JS[0] |
| 4767 | // |
| 4768 | // Each entry is an activation, either JS or C. The index is the count at that |
| 4769 | // level. Stars identify activations with exception handlers, the @ identifies |
| 4770 | // the exception handler that should catch the exception. |
ager@chromium.org | 71daaf6 | 2009-04-01 07:22:49 +0000 | [diff] [blame] | 4771 | // |
| 4772 | // BUG(271): Some of the exception propagation does not work on the |
| 4773 | // ARM simulator because the simulator separates the C++ stack and the |
| 4774 | // JS stack. This test therefore fails on the simulator. The test is |
| 4775 | // not threaded to allow the threading tests to run on the simulator. |
| 4776 | TEST(ExceptionOrder) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4777 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4778 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4779 | templ->Set(v8_str("check"), v8::FunctionTemplate::New(JSCheck)); |
| 4780 | templ->Set(v8_str("CThrowCountDown"), |
| 4781 | v8::FunctionTemplate::New(CThrowCountDown)); |
| 4782 | LocalContext context(0, templ); |
| 4783 | CompileRun( |
| 4784 | "function JSThrowCountDown(count, jsInterval, cInterval, expected) {" |
| 4785 | " if (count == 0) throw 'FromJS';" |
| 4786 | " if (count % jsInterval == 0) {" |
| 4787 | " try {" |
| 4788 | " var value = CThrowCountDown(count - 1," |
| 4789 | " jsInterval," |
| 4790 | " cInterval," |
| 4791 | " expected);" |
| 4792 | " check(false, count, expected);" |
| 4793 | " return value;" |
| 4794 | " } catch (e) {" |
| 4795 | " check(true, count, expected);" |
| 4796 | " }" |
| 4797 | " } else {" |
| 4798 | " return CThrowCountDown(count - 1, jsInterval, cInterval, expected);" |
| 4799 | " }" |
| 4800 | "}"); |
| 4801 | Local<Function> fun = |
| 4802 | Local<Function>::Cast(context->Global()->Get(v8_str("JSThrowCountDown"))); |
| 4803 | |
| 4804 | const int argc = 4; |
| 4805 | // count jsInterval cInterval expected |
| 4806 | |
| 4807 | // *JS[4] *C[3] @JS[2] C[1] JS[0] |
| 4808 | v8::Handle<Value> a0[argc] = { v8_num(4), v8_num(2), v8_num(3), v8_num(2) }; |
| 4809 | fun->Call(fun, argc, a0); |
| 4810 | |
| 4811 | // JS[5] *C[4] JS[3] @C[2] JS[1] C[0] |
| 4812 | v8::Handle<Value> a1[argc] = { v8_num(5), v8_num(6), v8_num(1), v8_num(2) }; |
| 4813 | fun->Call(fun, argc, a1); |
| 4814 | |
| 4815 | // JS[6] @C[5] JS[4] C[3] JS[2] C[1] JS[0] |
| 4816 | v8::Handle<Value> a2[argc] = { v8_num(6), v8_num(7), v8_num(5), v8_num(5) }; |
| 4817 | fun->Call(fun, argc, a2); |
| 4818 | |
| 4819 | // @JS[6] C[5] JS[4] C[3] JS[2] C[1] JS[0] |
| 4820 | v8::Handle<Value> a3[argc] = { v8_num(6), v8_num(6), v8_num(7), v8_num(6) }; |
| 4821 | fun->Call(fun, argc, a3); |
| 4822 | |
| 4823 | // JS[6] *C[5] @JS[4] C[3] JS[2] C[1] JS[0] |
| 4824 | v8::Handle<Value> a4[argc] = { v8_num(6), v8_num(4), v8_num(5), v8_num(4) }; |
| 4825 | fun->Call(fun, argc, a4); |
| 4826 | |
| 4827 | // JS[6] C[5] *JS[4] @C[3] JS[2] C[1] JS[0] |
| 4828 | v8::Handle<Value> a5[argc] = { v8_num(6), v8_num(4), v8_num(3), v8_num(3) }; |
| 4829 | fun->Call(fun, argc, a5); |
| 4830 | } |
| 4831 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 4832 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4833 | void ThrowValue(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4834 | ApiTestFuzzer::Fuzz(); |
| 4835 | CHECK_EQ(1, args.Length()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4836 | args.GetIsolate()->ThrowException(args[0]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4837 | } |
| 4838 | |
| 4839 | |
| 4840 | THREADED_TEST(ThrowValues) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4841 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4842 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4843 | templ->Set(v8_str("Throw"), v8::FunctionTemplate::New(ThrowValue)); |
| 4844 | LocalContext context(0, templ); |
| 4845 | v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
| 4846 | "function Run(obj) {" |
| 4847 | " try {" |
| 4848 | " Throw(obj);" |
| 4849 | " } catch (e) {" |
| 4850 | " return e;" |
| 4851 | " }" |
| 4852 | " return 'no exception';" |
| 4853 | "}" |
| 4854 | "[Run('str'), Run(1), Run(0), Run(null), Run(void 0)];")); |
| 4855 | CHECK_EQ(5, result->Length()); |
| 4856 | CHECK(result->Get(v8::Integer::New(0))->IsString()); |
| 4857 | CHECK(result->Get(v8::Integer::New(1))->IsNumber()); |
| 4858 | CHECK_EQ(1, result->Get(v8::Integer::New(1))->Int32Value()); |
| 4859 | CHECK(result->Get(v8::Integer::New(2))->IsNumber()); |
| 4860 | CHECK_EQ(0, result->Get(v8::Integer::New(2))->Int32Value()); |
| 4861 | CHECK(result->Get(v8::Integer::New(3))->IsNull()); |
| 4862 | CHECK(result->Get(v8::Integer::New(4))->IsUndefined()); |
| 4863 | } |
| 4864 | |
| 4865 | |
| 4866 | THREADED_TEST(CatchZero) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4867 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4868 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4869 | v8::TryCatch try_catch; |
| 4870 | CHECK(!try_catch.HasCaught()); |
| 4871 | Script::Compile(v8_str("throw 10"))->Run(); |
| 4872 | CHECK(try_catch.HasCaught()); |
| 4873 | CHECK_EQ(10, try_catch.Exception()->Int32Value()); |
| 4874 | try_catch.Reset(); |
| 4875 | CHECK(!try_catch.HasCaught()); |
| 4876 | Script::Compile(v8_str("throw 0"))->Run(); |
| 4877 | CHECK(try_catch.HasCaught()); |
| 4878 | CHECK_EQ(0, try_catch.Exception()->Int32Value()); |
| 4879 | } |
| 4880 | |
| 4881 | |
| 4882 | THREADED_TEST(CatchExceptionFromWith) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4883 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4884 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4885 | v8::TryCatch try_catch; |
| 4886 | CHECK(!try_catch.HasCaught()); |
| 4887 | Script::Compile(v8_str("var o = {}; with (o) { throw 42; }"))->Run(); |
| 4888 | CHECK(try_catch.HasCaught()); |
| 4889 | } |
| 4890 | |
| 4891 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4892 | THREADED_TEST(TryCatchAndFinallyHidingException) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4893 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4894 | v8::HandleScope scope(context->GetIsolate()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4895 | v8::TryCatch try_catch; |
| 4896 | CHECK(!try_catch.HasCaught()); |
| 4897 | CompileRun("function f(k) { try { this[k]; } finally { return 0; } };"); |
| 4898 | CompileRun("f({toString: function() { throw 42; }});"); |
| 4899 | CHECK(!try_catch.HasCaught()); |
| 4900 | } |
| 4901 | |
| 4902 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 4903 | void WithTryCatch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4904 | v8::TryCatch try_catch; |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | |
| 4908 | THREADED_TEST(TryCatchAndFinally) { |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4909 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4910 | v8::HandleScope scope(context->GetIsolate()); |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 4911 | context->Global()->Set( |
| 4912 | v8_str("native_with_try_catch"), |
| 4913 | v8::FunctionTemplate::New(WithTryCatch)->GetFunction()); |
| 4914 | v8::TryCatch try_catch; |
| 4915 | CHECK(!try_catch.HasCaught()); |
| 4916 | CompileRun( |
| 4917 | "try {\n" |
| 4918 | " throw new Error('a');\n" |
| 4919 | "} finally {\n" |
| 4920 | " native_with_try_catch();\n" |
| 4921 | "}\n"); |
| 4922 | CHECK(try_catch.HasCaught()); |
| 4923 | } |
| 4924 | |
| 4925 | |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 4926 | static void TryCatchNestedHelper(int depth) { |
| 4927 | if (depth > 0) { |
| 4928 | v8::TryCatch try_catch; |
| 4929 | try_catch.SetVerbose(true); |
| 4930 | TryCatchNestedHelper(depth - 1); |
| 4931 | CHECK(try_catch.HasCaught()); |
| 4932 | try_catch.ReThrow(); |
| 4933 | } else { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 4934 | CcTest::isolate()->ThrowException(v8_str("back")); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 4935 | } |
| 4936 | } |
| 4937 | |
| 4938 | |
| 4939 | TEST(TryCatchNested) { |
| 4940 | v8::V8::Initialize(); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 4941 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4942 | v8::HandleScope scope(context->GetIsolate()); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 4943 | v8::TryCatch try_catch; |
| 4944 | TryCatchNestedHelper(5); |
| 4945 | CHECK(try_catch.HasCaught()); |
| 4946 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(try_catch.Exception()), "back")); |
| 4947 | } |
| 4948 | |
| 4949 | |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 4950 | void TryCatchMixedNestingCheck(v8::TryCatch* try_catch) { |
| 4951 | CHECK(try_catch->HasCaught()); |
| 4952 | Handle<Message> message = try_catch->Message(); |
| 4953 | Handle<Value> resource = message->GetScriptResourceName(); |
| 4954 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(resource), "inner")); |
| 4955 | CHECK_EQ(0, strcmp(*v8::String::Utf8Value(message->Get()), |
| 4956 | "Uncaught Error: a")); |
| 4957 | CHECK_EQ(1, message->GetLineNumber()); |
| 4958 | CHECK_EQ(6, message->GetStartColumn()); |
| 4959 | } |
| 4960 | |
| 4961 | |
| 4962 | void TryCatchMixedNestingHelper( |
| 4963 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 4964 | ApiTestFuzzer::Fuzz(); |
| 4965 | v8::TryCatch try_catch; |
| 4966 | CompileRunWithOrigin("throw new Error('a');\n", "inner", 0, 0); |
| 4967 | CHECK(try_catch.HasCaught()); |
| 4968 | TryCatchMixedNestingCheck(&try_catch); |
| 4969 | try_catch.ReThrow(); |
| 4970 | } |
| 4971 | |
| 4972 | |
| 4973 | // This test ensures that an outer TryCatch in the following situation: |
| 4974 | // C++/TryCatch -> JS -> C++/TryCatch -> JS w/ SyntaxError |
| 4975 | // does not clobber the Message object generated for the inner TryCatch. |
| 4976 | // This exercises the ability of TryCatch.ReThrow() to restore the |
| 4977 | // inner pending Message before throwing the exception again. |
| 4978 | TEST(TryCatchMixedNesting) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 4979 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 93a47f4 | 2013-07-02 14:43:41 +0000 | [diff] [blame] | 4980 | v8::V8::Initialize(); |
| 4981 | v8::TryCatch try_catch; |
| 4982 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 4983 | templ->Set(v8_str("TryCatchMixedNestingHelper"), |
| 4984 | v8::FunctionTemplate::New(TryCatchMixedNestingHelper)); |
| 4985 | LocalContext context(0, templ); |
| 4986 | CompileRunWithOrigin("TryCatchMixedNestingHelper();\n", "outer", 1, 1); |
| 4987 | TryCatchMixedNestingCheck(&try_catch); |
| 4988 | } |
| 4989 | |
| 4990 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4991 | THREADED_TEST(Equality) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4992 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 4993 | v8::Isolate* isolate = context->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 4994 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 4995 | // Check that equality works at all before relying on CHECK_EQ |
| 4996 | CHECK(v8_str("a")->Equals(v8_str("a"))); |
| 4997 | CHECK(!v8_str("a")->Equals(v8_str("b"))); |
| 4998 | |
| 4999 | CHECK_EQ(v8_str("a"), v8_str("a")); |
| 5000 | CHECK_NE(v8_str("a"), v8_str("b")); |
| 5001 | CHECK_EQ(v8_num(1), v8_num(1)); |
| 5002 | CHECK_EQ(v8_num(1.00), v8_num(1)); |
| 5003 | CHECK_NE(v8_num(1), v8_num(2)); |
| 5004 | |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 5005 | // Assume String is not internalized. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5006 | CHECK(v8_str("a")->StrictEquals(v8_str("a"))); |
| 5007 | CHECK(!v8_str("a")->StrictEquals(v8_str("b"))); |
| 5008 | CHECK(!v8_str("5")->StrictEquals(v8_num(5))); |
| 5009 | CHECK(v8_num(1)->StrictEquals(v8_num(1))); |
| 5010 | CHECK(!v8_num(1)->StrictEquals(v8_num(2))); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 5011 | CHECK(v8_num(0.0)->StrictEquals(v8_num(-0.0))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5012 | Local<Value> not_a_number = v8_num(i::OS::nan_value()); |
| 5013 | CHECK(!not_a_number->StrictEquals(not_a_number)); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 5014 | CHECK(v8::False(isolate)->StrictEquals(v8::False(isolate))); |
| 5015 | CHECK(!v8::False(isolate)->StrictEquals(v8::Undefined(isolate))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5016 | |
| 5017 | v8::Handle<v8::Object> obj = v8::Object::New(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 5018 | v8::Persistent<v8::Object> alias(isolate, obj); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 5019 | CHECK(v8::Local<v8::Object>::New(isolate, alias)->StrictEquals(obj)); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 5020 | alias.Dispose(); |
machenbach@chromium.org | 3d079fe | 2013-09-25 08:19:55 +0000 | [diff] [blame] | 5021 | |
| 5022 | CHECK(v8_str("a")->SameValue(v8_str("a"))); |
| 5023 | CHECK(!v8_str("a")->SameValue(v8_str("b"))); |
| 5024 | CHECK(!v8_str("5")->SameValue(v8_num(5))); |
| 5025 | CHECK(v8_num(1)->SameValue(v8_num(1))); |
| 5026 | CHECK(!v8_num(1)->SameValue(v8_num(2))); |
| 5027 | CHECK(!v8_num(0.0)->SameValue(v8_num(-0.0))); |
| 5028 | CHECK(not_a_number->SameValue(not_a_number)); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 5029 | CHECK(v8::False(isolate)->SameValue(v8::False(isolate))); |
| 5030 | CHECK(!v8::False(isolate)->SameValue(v8::Undefined(isolate))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5031 | } |
| 5032 | |
| 5033 | |
| 5034 | THREADED_TEST(MultiRun) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5035 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5036 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5037 | Local<Script> script = Script::Compile(v8_str("x")); |
| 5038 | for (int i = 0; i < 10; i++) |
| 5039 | script->Run(); |
| 5040 | } |
| 5041 | |
| 5042 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5043 | static void GetXValue(Local<String> name, |
| 5044 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5045 | ApiTestFuzzer::Fuzz(); |
| 5046 | CHECK_EQ(info.Data(), v8_str("donut")); |
| 5047 | CHECK_EQ(name, v8_str("x")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5048 | info.GetReturnValue().Set(name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
| 5051 | |
| 5052 | THREADED_TEST(SimplePropertyRead) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5053 | LocalContext context; |
| 5054 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5055 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5056 | templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5057 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5058 | Local<Script> script = Script::Compile(v8_str("obj.x")); |
| 5059 | for (int i = 0; i < 10; i++) { |
| 5060 | Local<Value> result = script->Run(); |
| 5061 | CHECK_EQ(result, v8_str("x")); |
| 5062 | } |
| 5063 | } |
| 5064 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 5065 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5066 | THREADED_TEST(DefinePropertyOnAPIAccessor) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 5067 | LocalContext context; |
| 5068 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5069 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5070 | templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5071 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5072 | |
| 5073 | // Uses getOwnPropertyDescriptor to check the configurable status |
| 5074 | Local<Script> script_desc |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5075 | = Script::Compile(v8_str("var prop = Object.getOwnPropertyDescriptor( " |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5076 | "obj, 'x');" |
| 5077 | "prop.configurable;")); |
| 5078 | Local<Value> result = script_desc->Run(); |
| 5079 | CHECK_EQ(result->BooleanValue(), true); |
| 5080 | |
| 5081 | // Redefine get - but still configurable |
| 5082 | Local<Script> script_define |
| 5083 | = Script::Compile(v8_str("var desc = { get: function(){return 42; }," |
| 5084 | " configurable: true };" |
| 5085 | "Object.defineProperty(obj, 'x', desc);" |
| 5086 | "obj.x")); |
| 5087 | result = script_define->Run(); |
| 5088 | CHECK_EQ(result, v8_num(42)); |
| 5089 | |
| 5090 | // Check that the accessor is still configurable |
| 5091 | result = script_desc->Run(); |
| 5092 | CHECK_EQ(result->BooleanValue(), true); |
| 5093 | |
| 5094 | // Redefine to a non-configurable |
| 5095 | script_define |
| 5096 | = Script::Compile(v8_str("var desc = { get: function(){return 43; }," |
| 5097 | " configurable: false };" |
| 5098 | "Object.defineProperty(obj, 'x', desc);" |
| 5099 | "obj.x")); |
| 5100 | result = script_define->Run(); |
| 5101 | CHECK_EQ(result, v8_num(43)); |
| 5102 | result = script_desc->Run(); |
| 5103 | CHECK_EQ(result->BooleanValue(), false); |
| 5104 | |
| 5105 | // Make sure that it is not possible to redefine again |
| 5106 | v8::TryCatch try_catch; |
| 5107 | result = script_define->Run(); |
| 5108 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 5109 | String::Utf8Value exception_value(try_catch.Exception()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5110 | CHECK_EQ(*exception_value, "TypeError: Cannot redefine property: x"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5111 | } |
| 5112 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 5113 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5114 | THREADED_TEST(DefinePropertyOnDefineGetterSetter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5115 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5116 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5117 | templ->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut")); |
| 5118 | LocalContext context; |
| 5119 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5120 | |
| 5121 | Local<Script> script_desc = Script::Compile(v8_str("var prop =" |
| 5122 | "Object.getOwnPropertyDescriptor( " |
| 5123 | "obj, 'x');" |
| 5124 | "prop.configurable;")); |
| 5125 | Local<Value> result = script_desc->Run(); |
| 5126 | CHECK_EQ(result->BooleanValue(), true); |
| 5127 | |
| 5128 | Local<Script> script_define = |
| 5129 | Script::Compile(v8_str("var desc = {get: function(){return 42; }," |
| 5130 | " configurable: true };" |
| 5131 | "Object.defineProperty(obj, 'x', desc);" |
| 5132 | "obj.x")); |
| 5133 | result = script_define->Run(); |
| 5134 | CHECK_EQ(result, v8_num(42)); |
| 5135 | |
| 5136 | |
| 5137 | result = script_desc->Run(); |
| 5138 | CHECK_EQ(result->BooleanValue(), true); |
| 5139 | |
| 5140 | |
| 5141 | script_define = |
| 5142 | Script::Compile(v8_str("var desc = {get: function(){return 43; }," |
| 5143 | " configurable: false };" |
| 5144 | "Object.defineProperty(obj, 'x', desc);" |
| 5145 | "obj.x")); |
| 5146 | result = script_define->Run(); |
| 5147 | CHECK_EQ(result, v8_num(43)); |
| 5148 | result = script_desc->Run(); |
| 5149 | |
| 5150 | CHECK_EQ(result->BooleanValue(), false); |
| 5151 | |
| 5152 | v8::TryCatch try_catch; |
| 5153 | result = script_define->Run(); |
| 5154 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 5155 | String::Utf8Value exception_value(try_catch.Exception()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5156 | CHECK_EQ(*exception_value, "TypeError: Cannot redefine property: x"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5157 | } |
| 5158 | |
| 5159 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5160 | static v8::Handle<v8::Object> GetGlobalProperty(LocalContext* context, |
| 5161 | char const* name) { |
| 5162 | return v8::Handle<v8::Object>::Cast((*context)->Global()->Get(v8_str(name))); |
| 5163 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5164 | |
| 5165 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5166 | THREADED_TEST(DefineAPIAccessorOnObject) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5167 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5168 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5169 | LocalContext context; |
| 5170 | |
| 5171 | context->Global()->Set(v8_str("obj1"), templ->NewInstance()); |
| 5172 | CompileRun("var obj2 = {};"); |
| 5173 | |
| 5174 | CHECK(CompileRun("obj1.x")->IsUndefined()); |
| 5175 | CHECK(CompileRun("obj2.x")->IsUndefined()); |
| 5176 | |
| 5177 | CHECK(GetGlobalProperty(&context, "obj1")-> |
| 5178 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5179 | |
| 5180 | ExpectString("obj1.x", "x"); |
| 5181 | CHECK(CompileRun("obj2.x")->IsUndefined()); |
| 5182 | |
| 5183 | CHECK(GetGlobalProperty(&context, "obj2")-> |
| 5184 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5185 | |
| 5186 | ExpectString("obj1.x", "x"); |
| 5187 | ExpectString("obj2.x", "x"); |
| 5188 | |
| 5189 | ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable"); |
| 5190 | ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable"); |
| 5191 | |
| 5192 | CompileRun("Object.defineProperty(obj1, 'x'," |
| 5193 | "{ get: function() { return 'y'; }, configurable: true })"); |
| 5194 | |
| 5195 | ExpectString("obj1.x", "y"); |
| 5196 | ExpectString("obj2.x", "x"); |
| 5197 | |
| 5198 | CompileRun("Object.defineProperty(obj2, 'x'," |
| 5199 | "{ get: function() { return 'y'; }, configurable: true })"); |
| 5200 | |
| 5201 | ExpectString("obj1.x", "y"); |
| 5202 | ExpectString("obj2.x", "y"); |
| 5203 | |
| 5204 | ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable"); |
| 5205 | ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable"); |
| 5206 | |
| 5207 | CHECK(GetGlobalProperty(&context, "obj1")-> |
| 5208 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5209 | CHECK(GetGlobalProperty(&context, "obj2")-> |
| 5210 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5211 | |
| 5212 | ExpectString("obj1.x", "x"); |
| 5213 | ExpectString("obj2.x", "x"); |
| 5214 | |
| 5215 | ExpectTrue("Object.getOwnPropertyDescriptor(obj1, 'x').configurable"); |
| 5216 | ExpectTrue("Object.getOwnPropertyDescriptor(obj2, 'x').configurable"); |
| 5217 | |
| 5218 | // Define getters/setters, but now make them not configurable. |
| 5219 | CompileRun("Object.defineProperty(obj1, 'x'," |
| 5220 | "{ get: function() { return 'z'; }, configurable: false })"); |
| 5221 | CompileRun("Object.defineProperty(obj2, 'x'," |
| 5222 | "{ get: function() { return 'z'; }, configurable: false })"); |
| 5223 | |
| 5224 | ExpectTrue("!Object.getOwnPropertyDescriptor(obj1, 'x').configurable"); |
| 5225 | ExpectTrue("!Object.getOwnPropertyDescriptor(obj2, 'x').configurable"); |
| 5226 | |
| 5227 | ExpectString("obj1.x", "z"); |
| 5228 | ExpectString("obj2.x", "z"); |
| 5229 | |
| 5230 | CHECK(!GetGlobalProperty(&context, "obj1")-> |
| 5231 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5232 | CHECK(!GetGlobalProperty(&context, "obj2")-> |
| 5233 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5234 | |
| 5235 | ExpectString("obj1.x", "z"); |
| 5236 | ExpectString("obj2.x", "z"); |
| 5237 | } |
| 5238 | |
| 5239 | |
| 5240 | THREADED_TEST(DontDeleteAPIAccessorsCannotBeOverriden) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5241 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5242 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5243 | LocalContext context; |
| 5244 | |
| 5245 | context->Global()->Set(v8_str("obj1"), templ->NewInstance()); |
| 5246 | CompileRun("var obj2 = {};"); |
| 5247 | |
| 5248 | CHECK(GetGlobalProperty(&context, "obj1")->SetAccessor( |
| 5249 | v8_str("x"), |
| 5250 | GetXValue, NULL, |
| 5251 | v8_str("donut"), v8::DEFAULT, v8::DontDelete)); |
| 5252 | CHECK(GetGlobalProperty(&context, "obj2")->SetAccessor( |
| 5253 | v8_str("x"), |
| 5254 | GetXValue, NULL, |
| 5255 | v8_str("donut"), v8::DEFAULT, v8::DontDelete)); |
| 5256 | |
| 5257 | ExpectString("obj1.x", "x"); |
| 5258 | ExpectString("obj2.x", "x"); |
| 5259 | |
| 5260 | ExpectTrue("!Object.getOwnPropertyDescriptor(obj1, 'x').configurable"); |
| 5261 | ExpectTrue("!Object.getOwnPropertyDescriptor(obj2, 'x').configurable"); |
| 5262 | |
| 5263 | CHECK(!GetGlobalProperty(&context, "obj1")-> |
| 5264 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5265 | CHECK(!GetGlobalProperty(&context, "obj2")-> |
| 5266 | SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("donut"))); |
| 5267 | |
| 5268 | { |
| 5269 | v8::TryCatch try_catch; |
| 5270 | CompileRun("Object.defineProperty(obj1, 'x'," |
| 5271 | "{get: function() { return 'func'; }})"); |
| 5272 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 5273 | String::Utf8Value exception_value(try_catch.Exception()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5274 | CHECK_EQ(*exception_value, "TypeError: Cannot redefine property: x"); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5275 | } |
| 5276 | { |
| 5277 | v8::TryCatch try_catch; |
| 5278 | CompileRun("Object.defineProperty(obj2, 'x'," |
| 5279 | "{get: function() { return 'func'; }})"); |
| 5280 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 5281 | String::Utf8Value exception_value(try_catch.Exception()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 5282 | CHECK_EQ(*exception_value, "TypeError: Cannot redefine property: x"); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5287 | static void Get239Value(Local<String> name, |
| 5288 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5289 | ApiTestFuzzer::Fuzz(); |
| 5290 | CHECK_EQ(info.Data(), v8_str("donut")); |
| 5291 | CHECK_EQ(name, v8_str("239")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5292 | info.GetReturnValue().Set(name); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5293 | } |
| 5294 | |
| 5295 | |
| 5296 | THREADED_TEST(ElementAPIAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5297 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 5298 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5299 | LocalContext context; |
| 5300 | |
| 5301 | context->Global()->Set(v8_str("obj1"), templ->NewInstance()); |
| 5302 | CompileRun("var obj2 = {};"); |
| 5303 | |
| 5304 | CHECK(GetGlobalProperty(&context, "obj1")->SetAccessor( |
| 5305 | v8_str("239"), |
| 5306 | Get239Value, NULL, |
| 5307 | v8_str("donut"))); |
| 5308 | CHECK(GetGlobalProperty(&context, "obj2")->SetAccessor( |
| 5309 | v8_str("239"), |
| 5310 | Get239Value, NULL, |
| 5311 | v8_str("donut"))); |
| 5312 | |
| 5313 | ExpectString("obj1[239]", "239"); |
| 5314 | ExpectString("obj2[239]", "239"); |
| 5315 | ExpectString("obj1['239']", "239"); |
| 5316 | ExpectString("obj2['239']", "239"); |
| 5317 | } |
| 5318 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5319 | |
| 5320 | v8::Persistent<Value> xValue; |
| 5321 | |
| 5322 | |
| 5323 | static void SetXValue(Local<String> name, |
| 5324 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5325 | const v8::PropertyCallbackInfo<void>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5326 | CHECK_EQ(value, v8_num(4)); |
| 5327 | CHECK_EQ(info.Data(), v8_str("donut")); |
| 5328 | CHECK_EQ(name, v8_str("x")); |
| 5329 | CHECK(xValue.IsEmpty()); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 5330 | xValue.Reset(info.GetIsolate(), value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5331 | } |
| 5332 | |
| 5333 | |
| 5334 | THREADED_TEST(SimplePropertyWrite) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5335 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5336 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5337 | templ->SetAccessor(v8_str("x"), GetXValue, SetXValue, v8_str("donut")); |
| 5338 | LocalContext context; |
| 5339 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5340 | Local<Script> script = Script::Compile(v8_str("obj.x = 4")); |
| 5341 | for (int i = 0; i < 10; i++) { |
| 5342 | CHECK(xValue.IsEmpty()); |
| 5343 | script->Run(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5344 | CHECK_EQ(v8_num(4), Local<Value>::New(CcTest::isolate(), xValue)); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 5345 | xValue.Dispose(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 5346 | xValue.Clear(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5347 | } |
| 5348 | } |
| 5349 | |
| 5350 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5351 | THREADED_TEST(SetterOnly) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5352 | v8::HandleScope scope(CcTest::isolate()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5353 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5354 | templ->SetAccessor(v8_str("x"), NULL, SetXValue, v8_str("donut")); |
| 5355 | LocalContext context; |
| 5356 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5357 | Local<Script> script = Script::Compile(v8_str("obj.x = 4; obj.x")); |
| 5358 | for (int i = 0; i < 10; i++) { |
| 5359 | CHECK(xValue.IsEmpty()); |
| 5360 | script->Run(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5361 | CHECK_EQ(v8_num(4), Local<Value>::New(CcTest::isolate(), xValue)); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 5362 | xValue.Dispose(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 5363 | xValue.Clear(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5364 | } |
| 5365 | } |
| 5366 | |
| 5367 | |
| 5368 | THREADED_TEST(NoAccessors) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5369 | v8::HandleScope scope(CcTest::isolate()); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5370 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 5371 | templ->SetAccessor(v8_str("x"), |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5372 | static_cast<v8::AccessorGetterCallback>(NULL), |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 5373 | NULL, |
| 5374 | v8_str("donut")); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 5375 | LocalContext context; |
| 5376 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5377 | Local<Script> script = Script::Compile(v8_str("obj.x = 4; obj.x")); |
| 5378 | for (int i = 0; i < 10; i++) { |
| 5379 | script->Run(); |
| 5380 | } |
| 5381 | } |
| 5382 | |
| 5383 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5384 | static void XPropertyGetter(Local<String> property, |
| 5385 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5386 | ApiTestFuzzer::Fuzz(); |
| 5387 | CHECK(info.Data()->IsUndefined()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5388 | info.GetReturnValue().Set(property); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5389 | } |
| 5390 | |
| 5391 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5392 | THREADED_TEST(NamedInterceptorPropertyRead) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5393 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5394 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5395 | templ->SetNamedPropertyHandler(XPropertyGetter); |
| 5396 | LocalContext context; |
| 5397 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5398 | Local<Script> script = Script::Compile(v8_str("obj.x")); |
| 5399 | for (int i = 0; i < 10; i++) { |
| 5400 | Local<Value> result = script->Run(); |
| 5401 | CHECK_EQ(result, v8_str("x")); |
| 5402 | } |
| 5403 | } |
| 5404 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5405 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5406 | THREADED_TEST(NamedInterceptorDictionaryIC) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5407 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 5408 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5409 | templ->SetNamedPropertyHandler(XPropertyGetter); |
| 5410 | LocalContext context; |
| 5411 | // Create an object with a named interceptor. |
| 5412 | context->Global()->Set(v8_str("interceptor_obj"), templ->NewInstance()); |
| 5413 | Local<Script> script = Script::Compile(v8_str("interceptor_obj.x")); |
| 5414 | for (int i = 0; i < 10; i++) { |
| 5415 | Local<Value> result = script->Run(); |
| 5416 | CHECK_EQ(result, v8_str("x")); |
| 5417 | } |
| 5418 | // Create a slow case object and a function accessing a property in |
| 5419 | // that slow case object (with dictionary probing in generated |
| 5420 | // code). Then force object with a named interceptor into slow-case, |
| 5421 | // pass it to the function, and check that the interceptor is called |
| 5422 | // instead of accessing the local property. |
| 5423 | Local<Value> result = |
| 5424 | CompileRun("function get_x(o) { return o.x; };" |
| 5425 | "var obj = { x : 42, y : 0 };" |
| 5426 | "delete obj.y;" |
| 5427 | "for (var i = 0; i < 10; i++) get_x(obj);" |
| 5428 | "interceptor_obj.x = 42;" |
| 5429 | "interceptor_obj.y = 10;" |
| 5430 | "delete interceptor_obj.y;" |
| 5431 | "get_x(interceptor_obj)"); |
| 5432 | CHECK_EQ(result, v8_str("x")); |
| 5433 | } |
| 5434 | |
| 5435 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5436 | THREADED_TEST(NamedInterceptorDictionaryICMultipleContext) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5437 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 5438 | v8::HandleScope scope(isolate); |
| 5439 | v8::Local<Context> context1 = Context::New(isolate); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5440 | |
| 5441 | context1->Enter(); |
| 5442 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5443 | templ->SetNamedPropertyHandler(XPropertyGetter); |
| 5444 | // Create an object with a named interceptor. |
| 5445 | v8::Local<v8::Object> object = templ->NewInstance(); |
| 5446 | context1->Global()->Set(v8_str("interceptor_obj"), object); |
| 5447 | |
| 5448 | // Force the object into the slow case. |
| 5449 | CompileRun("interceptor_obj.y = 0;" |
| 5450 | "delete interceptor_obj.y;"); |
| 5451 | context1->Exit(); |
| 5452 | |
| 5453 | { |
| 5454 | // Introduce the object into a different context. |
| 5455 | // Repeat named loads to exercise ICs. |
| 5456 | LocalContext context2; |
| 5457 | context2->Global()->Set(v8_str("interceptor_obj"), object); |
| 5458 | Local<Value> result = |
| 5459 | CompileRun("function get_x(o) { return o.x; }" |
| 5460 | "interceptor_obj.x = 42;" |
| 5461 | "for (var i=0; i != 10; i++) {" |
| 5462 | " get_x(interceptor_obj);" |
| 5463 | "}" |
| 5464 | "get_x(interceptor_obj)"); |
| 5465 | // Check that the interceptor was actually invoked. |
| 5466 | CHECK_EQ(result, v8_str("x")); |
| 5467 | } |
| 5468 | |
| 5469 | // Return to the original context and force some object to the slow case |
| 5470 | // to cause the NormalizedMapCache to verify. |
| 5471 | context1->Enter(); |
| 5472 | CompileRun("var obj = { x : 0 }; delete obj.x;"); |
| 5473 | context1->Exit(); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5477 | static void SetXOnPrototypeGetter( |
| 5478 | Local<String> property, |
| 5479 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5480 | // Set x on the prototype object and do not handle the get request. |
| 5481 | v8::Handle<v8::Value> proto = info.Holder()->GetPrototype(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5482 | proto.As<v8::Object>()->Set(v8_str("x"), v8::Integer::New(23)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
| 5485 | |
| 5486 | // This is a regression test for http://crbug.com/20104. Map |
| 5487 | // transitions should not interfere with post interceptor lookup. |
| 5488 | THREADED_TEST(NamedInterceptorMapTransitionRead) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5489 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5490 | Local<v8::FunctionTemplate> function_template = v8::FunctionTemplate::New(); |
| 5491 | Local<v8::ObjectTemplate> instance_template |
| 5492 | = function_template->InstanceTemplate(); |
| 5493 | instance_template->SetNamedPropertyHandler(SetXOnPrototypeGetter); |
| 5494 | LocalContext context; |
| 5495 | context->Global()->Set(v8_str("F"), function_template->GetFunction()); |
| 5496 | // Create an instance of F and introduce a map transition for x. |
| 5497 | CompileRun("var o = new F(); o.x = 23;"); |
| 5498 | // Create an instance of F and invoke the getter. The result should be 23. |
| 5499 | Local<Value> result = CompileRun("o = new F(); o.x"); |
| 5500 | CHECK_EQ(result->Int32Value(), 23); |
| 5501 | } |
| 5502 | |
| 5503 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5504 | static void IndexedPropertyGetter( |
| 5505 | uint32_t index, |
| 5506 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5507 | ApiTestFuzzer::Fuzz(); |
| 5508 | if (index == 37) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5509 | info.GetReturnValue().Set(v8_num(625)); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5510 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5511 | } |
| 5512 | |
| 5513 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5514 | static void IndexedPropertySetter( |
| 5515 | uint32_t index, |
| 5516 | Local<Value> value, |
| 5517 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5518 | ApiTestFuzzer::Fuzz(); |
| 5519 | if (index == 39) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5520 | info.GetReturnValue().Set(value); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5521 | } |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5522 | } |
| 5523 | |
| 5524 | |
| 5525 | THREADED_TEST(IndexedInterceptorWithIndexedAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5526 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 5527 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5528 | templ->SetIndexedPropertyHandler(IndexedPropertyGetter, |
| 5529 | IndexedPropertySetter); |
| 5530 | LocalContext context; |
| 5531 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5532 | Local<Script> getter_script = Script::Compile(v8_str( |
| 5533 | "obj.__defineGetter__(\"3\", function(){return 5;});obj[3];")); |
| 5534 | Local<Script> setter_script = Script::Compile(v8_str( |
| 5535 | "obj.__defineSetter__(\"17\", function(val){this.foo = val;});" |
| 5536 | "obj[17] = 23;" |
| 5537 | "obj.foo;")); |
| 5538 | Local<Script> interceptor_setter_script = Script::Compile(v8_str( |
| 5539 | "obj.__defineSetter__(\"39\", function(val){this.foo = \"hit\";});" |
| 5540 | "obj[39] = 47;" |
| 5541 | "obj.foo;")); // This setter should not run, due to the interceptor. |
| 5542 | Local<Script> interceptor_getter_script = Script::Compile(v8_str( |
| 5543 | "obj[37];")); |
| 5544 | Local<Value> result = getter_script->Run(); |
| 5545 | CHECK_EQ(v8_num(5), result); |
| 5546 | result = setter_script->Run(); |
| 5547 | CHECK_EQ(v8_num(23), result); |
| 5548 | result = interceptor_setter_script->Run(); |
| 5549 | CHECK_EQ(v8_num(23), result); |
| 5550 | result = interceptor_getter_script->Run(); |
| 5551 | CHECK_EQ(v8_num(625), result); |
| 5552 | } |
| 5553 | |
| 5554 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5555 | static void UnboxedDoubleIndexedPropertyGetter( |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5556 | uint32_t index, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5557 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5558 | ApiTestFuzzer::Fuzz(); |
| 5559 | if (index < 25) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5560 | info.GetReturnValue().Set(v8_num(index)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5561 | } |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5562 | } |
| 5563 | |
| 5564 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5565 | static void UnboxedDoubleIndexedPropertySetter( |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5566 | uint32_t index, |
| 5567 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5568 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5569 | ApiTestFuzzer::Fuzz(); |
| 5570 | if (index < 25) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5571 | info.GetReturnValue().Set(v8_num(index)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5572 | } |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5573 | } |
| 5574 | |
| 5575 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5576 | void UnboxedDoubleIndexedPropertyEnumerator( |
| 5577 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5578 | // Force the list of returned keys to be stored in a FastDoubleArray. |
| 5579 | Local<Script> indexed_property_names_script = Script::Compile(v8_str( |
| 5580 | "keys = new Array(); keys[125000] = 1;" |
| 5581 | "for(i = 0; i < 80000; i++) { keys[i] = i; };" |
| 5582 | "keys.length = 25; keys;")); |
| 5583 | Local<Value> result = indexed_property_names_script->Run(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5584 | info.GetReturnValue().Set(Local<v8::Array>::Cast(result)); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5585 | } |
| 5586 | |
| 5587 | |
| 5588 | // Make sure that the the interceptor code in the runtime properly handles |
| 5589 | // merging property name lists for double-array-backed arrays. |
| 5590 | THREADED_TEST(IndexedInterceptorUnboxedDoubleWithIndexedAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5591 | v8::HandleScope scope(CcTest::isolate()); |
ricow@chromium.org | 9fa0967 | 2011-07-25 11:05:35 +0000 | [diff] [blame] | 5592 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5593 | templ->SetIndexedPropertyHandler(UnboxedDoubleIndexedPropertyGetter, |
| 5594 | UnboxedDoubleIndexedPropertySetter, |
| 5595 | 0, |
| 5596 | 0, |
| 5597 | UnboxedDoubleIndexedPropertyEnumerator); |
| 5598 | LocalContext context; |
| 5599 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5600 | // When obj is created, force it to be Stored in a FastDoubleArray. |
| 5601 | Local<Script> create_unboxed_double_script = Script::Compile(v8_str( |
| 5602 | "obj[125000] = 1; for(i = 0; i < 80000; i+=2) { obj[i] = i; } " |
| 5603 | "key_count = 0; " |
| 5604 | "for (x in obj) {key_count++;};" |
| 5605 | "obj;")); |
| 5606 | Local<Value> result = create_unboxed_double_script->Run(); |
| 5607 | CHECK(result->ToObject()->HasRealIndexedProperty(2000)); |
| 5608 | Local<Script> key_count_check = Script::Compile(v8_str( |
| 5609 | "key_count;")); |
| 5610 | result = key_count_check->Run(); |
| 5611 | CHECK_EQ(v8_num(40013), result); |
| 5612 | } |
| 5613 | |
| 5614 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5615 | void NonStrictArgsIndexedPropertyEnumerator( |
| 5616 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5617 | // Force the list of returned keys to be stored in a Arguments object. |
| 5618 | Local<Script> indexed_property_names_script = Script::Compile(v8_str( |
| 5619 | "function f(w,x) {" |
| 5620 | " return arguments;" |
| 5621 | "}" |
| 5622 | "keys = f(0, 1, 2, 3);" |
| 5623 | "keys;")); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 5624 | Local<Object> result = |
| 5625 | Local<Object>::Cast(indexed_property_names_script->Run()); |
| 5626 | // Have to populate the handle manually, as it's not Cast-able. |
| 5627 | i::Handle<i::JSObject> o = |
| 5628 | v8::Utils::OpenHandle<Object, i::JSObject>(result); |
| 5629 | i::Handle<i::JSArray> array(reinterpret_cast<i::JSArray*>(*o)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5630 | info.GetReturnValue().Set(v8::Utils::ToLocal(array)); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5631 | } |
| 5632 | |
| 5633 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5634 | static void NonStrictIndexedPropertyGetter( |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5635 | uint32_t index, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5636 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5637 | ApiTestFuzzer::Fuzz(); |
| 5638 | if (index < 4) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5639 | info.GetReturnValue().Set(v8_num(index)); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5640 | } |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5641 | } |
| 5642 | |
| 5643 | |
| 5644 | // Make sure that the the interceptor code in the runtime properly handles |
| 5645 | // merging property name lists for non-string arguments arrays. |
| 5646 | THREADED_TEST(IndexedInterceptorNonStrictArgsWithIndexedAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5647 | v8::HandleScope scope(CcTest::isolate()); |
rossberg@chromium.org | 28a3708 | 2011-08-22 11:03:23 +0000 | [diff] [blame] | 5648 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5649 | templ->SetIndexedPropertyHandler(NonStrictIndexedPropertyGetter, |
| 5650 | 0, |
| 5651 | 0, |
| 5652 | 0, |
| 5653 | NonStrictArgsIndexedPropertyEnumerator); |
| 5654 | LocalContext context; |
| 5655 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5656 | Local<Script> create_args_script = |
| 5657 | Script::Compile(v8_str( |
| 5658 | "var key_count = 0;" |
| 5659 | "for (x in obj) {key_count++;} key_count;")); |
| 5660 | Local<Value> result = create_args_script->Run(); |
| 5661 | CHECK_EQ(v8_num(4), result); |
| 5662 | } |
| 5663 | |
| 5664 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5665 | static void IdentityIndexedPropertyGetter( |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 5666 | uint32_t index, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 5667 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 5668 | info.GetReturnValue().Set(index); |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 5669 | } |
| 5670 | |
| 5671 | |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5672 | THREADED_TEST(IndexedInterceptorWithGetOwnPropertyDescriptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5673 | v8::HandleScope scope(CcTest::isolate()); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 5674 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5675 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5676 | |
| 5677 | LocalContext context; |
| 5678 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5679 | |
| 5680 | // Check fast object case. |
| 5681 | const char* fast_case_code = |
| 5682 | "Object.getOwnPropertyDescriptor(obj, 0).value.toString()"; |
| 5683 | ExpectString(fast_case_code, "0"); |
| 5684 | |
| 5685 | // Check slow case. |
| 5686 | const char* slow_case_code = |
| 5687 | "obj.x = 1; delete obj.x;" |
| 5688 | "Object.getOwnPropertyDescriptor(obj, 1).value.toString()"; |
| 5689 | ExpectString(slow_case_code, "1"); |
| 5690 | } |
| 5691 | |
| 5692 | |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 5693 | THREADED_TEST(IndexedInterceptorWithNoSetter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5694 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | eac059f | 2010-01-25 11:02:06 +0000 | [diff] [blame] | 5695 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5696 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5697 | |
| 5698 | LocalContext context; |
| 5699 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 5700 | |
| 5701 | const char* code = |
| 5702 | "try {" |
| 5703 | " obj[0] = 239;" |
| 5704 | " for (var i = 0; i < 100; i++) {" |
| 5705 | " var v = obj[0];" |
| 5706 | " if (v != 0) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5707 | " }" |
| 5708 | " 'PASSED'" |
| 5709 | "} catch(e) {" |
| 5710 | " e" |
| 5711 | "}"; |
| 5712 | ExpectString(code, "PASSED"); |
| 5713 | } |
| 5714 | |
| 5715 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5716 | THREADED_TEST(IndexedInterceptorWithAccessorCheck) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5717 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5718 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5719 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5720 | |
| 5721 | LocalContext context; |
| 5722 | Local<v8::Object> obj = templ->NewInstance(); |
| 5723 | obj->TurnOnAccessCheck(); |
| 5724 | context->Global()->Set(v8_str("obj"), obj); |
| 5725 | |
| 5726 | const char* code = |
| 5727 | "try {" |
| 5728 | " for (var i = 0; i < 100; i++) {" |
| 5729 | " var v = obj[0];" |
| 5730 | " if (v != undefined) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5731 | " }" |
| 5732 | " 'PASSED'" |
| 5733 | "} catch(e) {" |
| 5734 | " e" |
| 5735 | "}"; |
| 5736 | ExpectString(code, "PASSED"); |
| 5737 | } |
| 5738 | |
| 5739 | |
| 5740 | THREADED_TEST(IndexedInterceptorWithAccessorCheckSwitchedOn) { |
| 5741 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5742 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5743 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5744 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5745 | |
| 5746 | LocalContext context; |
| 5747 | Local<v8::Object> obj = templ->NewInstance(); |
| 5748 | context->Global()->Set(v8_str("obj"), obj); |
| 5749 | |
| 5750 | const char* code = |
| 5751 | "try {" |
| 5752 | " for (var i = 0; i < 100; i++) {" |
| 5753 | " var expected = i;" |
| 5754 | " if (i == 5) {" |
| 5755 | " %EnableAccessChecks(obj);" |
| 5756 | " expected = undefined;" |
| 5757 | " }" |
| 5758 | " var v = obj[i];" |
| 5759 | " if (v != expected) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5760 | " if (i == 5) %DisableAccessChecks(obj);" |
| 5761 | " }" |
| 5762 | " 'PASSED'" |
| 5763 | "} catch(e) {" |
| 5764 | " e" |
| 5765 | "}"; |
| 5766 | ExpectString(code, "PASSED"); |
| 5767 | } |
| 5768 | |
| 5769 | |
| 5770 | THREADED_TEST(IndexedInterceptorWithDifferentIndices) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5771 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5772 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5773 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5774 | |
| 5775 | LocalContext context; |
| 5776 | Local<v8::Object> obj = templ->NewInstance(); |
| 5777 | context->Global()->Set(v8_str("obj"), obj); |
| 5778 | |
| 5779 | const char* code = |
| 5780 | "try {" |
| 5781 | " for (var i = 0; i < 100; i++) {" |
| 5782 | " var v = obj[i];" |
| 5783 | " if (v != i) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5784 | " }" |
| 5785 | " 'PASSED'" |
| 5786 | "} catch(e) {" |
| 5787 | " e" |
| 5788 | "}"; |
| 5789 | ExpectString(code, "PASSED"); |
| 5790 | } |
| 5791 | |
| 5792 | |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5793 | THREADED_TEST(IndexedInterceptorWithNegativeIndices) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5794 | v8::HandleScope scope(CcTest::isolate()); |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5795 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5796 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5797 | |
| 5798 | LocalContext context; |
| 5799 | Local<v8::Object> obj = templ->NewInstance(); |
| 5800 | context->Global()->Set(v8_str("obj"), obj); |
| 5801 | |
| 5802 | const char* code = |
| 5803 | "try {" |
| 5804 | " for (var i = 0; i < 100; i++) {" |
| 5805 | " var expected = i;" |
| 5806 | " var key = i;" |
| 5807 | " if (i == 25) {" |
| 5808 | " key = -1;" |
| 5809 | " expected = undefined;" |
| 5810 | " }" |
| 5811 | " if (i == 50) {" |
| 5812 | " /* probe minimal Smi number on 32-bit platforms */" |
| 5813 | " key = -(1 << 30);" |
| 5814 | " expected = undefined;" |
| 5815 | " }" |
| 5816 | " if (i == 75) {" |
| 5817 | " /* probe minimal Smi number on 64-bit platforms */" |
| 5818 | " key = 1 << 31;" |
| 5819 | " expected = undefined;" |
| 5820 | " }" |
| 5821 | " var v = obj[key];" |
| 5822 | " if (v != expected) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5823 | " }" |
| 5824 | " 'PASSED'" |
| 5825 | "} catch(e) {" |
| 5826 | " e" |
| 5827 | "}"; |
| 5828 | ExpectString(code, "PASSED"); |
| 5829 | } |
| 5830 | |
| 5831 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5832 | THREADED_TEST(IndexedInterceptorWithNotSmiLookup) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5833 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5834 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5835 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5836 | |
| 5837 | LocalContext context; |
| 5838 | Local<v8::Object> obj = templ->NewInstance(); |
| 5839 | context->Global()->Set(v8_str("obj"), obj); |
| 5840 | |
| 5841 | const char* code = |
| 5842 | "try {" |
| 5843 | " for (var i = 0; i < 100; i++) {" |
| 5844 | " var expected = i;" |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5845 | " var key = i;" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5846 | " if (i == 50) {" |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5847 | " key = 'foobar';" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5848 | " expected = undefined;" |
| 5849 | " }" |
kmillikin@chromium.org | f05f291 | 2010-09-30 10:07:24 +0000 | [diff] [blame] | 5850 | " var v = obj[key];" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5851 | " if (v != expected) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5852 | " }" |
| 5853 | " 'PASSED'" |
| 5854 | "} catch(e) {" |
| 5855 | " e" |
| 5856 | "}"; |
| 5857 | ExpectString(code, "PASSED"); |
| 5858 | } |
| 5859 | |
| 5860 | |
| 5861 | THREADED_TEST(IndexedInterceptorGoingMegamorphic) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5862 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5863 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5864 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5865 | |
| 5866 | LocalContext context; |
| 5867 | Local<v8::Object> obj = templ->NewInstance(); |
| 5868 | context->Global()->Set(v8_str("obj"), obj); |
| 5869 | |
| 5870 | const char* code = |
| 5871 | "var original = obj;" |
| 5872 | "try {" |
| 5873 | " for (var i = 0; i < 100; i++) {" |
| 5874 | " var expected = i;" |
| 5875 | " if (i == 50) {" |
| 5876 | " obj = {50: 'foobar'};" |
| 5877 | " expected = 'foobar';" |
| 5878 | " }" |
| 5879 | " var v = obj[i];" |
| 5880 | " if (v != expected) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5881 | " if (i == 50) obj = original;" |
| 5882 | " }" |
| 5883 | " 'PASSED'" |
| 5884 | "} catch(e) {" |
| 5885 | " e" |
| 5886 | "}"; |
| 5887 | ExpectString(code, "PASSED"); |
| 5888 | } |
| 5889 | |
| 5890 | |
| 5891 | THREADED_TEST(IndexedInterceptorReceiverTurningSmi) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5892 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5893 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5894 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5895 | |
| 5896 | LocalContext context; |
| 5897 | Local<v8::Object> obj = templ->NewInstance(); |
| 5898 | context->Global()->Set(v8_str("obj"), obj); |
| 5899 | |
| 5900 | const char* code = |
| 5901 | "var original = obj;" |
| 5902 | "try {" |
| 5903 | " for (var i = 0; i < 100; i++) {" |
| 5904 | " var expected = i;" |
| 5905 | " if (i == 5) {" |
| 5906 | " obj = 239;" |
| 5907 | " expected = undefined;" |
| 5908 | " }" |
| 5909 | " var v = obj[i];" |
| 5910 | " if (v != expected) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5911 | " if (i == 5) obj = original;" |
| 5912 | " }" |
| 5913 | " 'PASSED'" |
| 5914 | "} catch(e) {" |
| 5915 | " e" |
| 5916 | "}"; |
| 5917 | ExpectString(code, "PASSED"); |
| 5918 | } |
| 5919 | |
| 5920 | |
| 5921 | THREADED_TEST(IndexedInterceptorOnProto) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5922 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 5923 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5924 | templ->SetIndexedPropertyHandler(IdentityIndexedPropertyGetter); |
| 5925 | |
| 5926 | LocalContext context; |
| 5927 | Local<v8::Object> obj = templ->NewInstance(); |
| 5928 | context->Global()->Set(v8_str("obj"), obj); |
| 5929 | |
| 5930 | const char* code = |
| 5931 | "var o = {__proto__: obj};" |
| 5932 | "try {" |
| 5933 | " for (var i = 0; i < 100; i++) {" |
| 5934 | " var v = o[i];" |
| 5935 | " if (v != i) throw 'Wrong value ' + v + ' at iteration ' + i;" |
| 5936 | " }" |
| 5937 | " 'PASSED'" |
| 5938 | "} catch(e) {" |
| 5939 | " e" |
| 5940 | "}"; |
| 5941 | ExpectString(code, "PASSED"); |
| 5942 | } |
| 5943 | |
| 5944 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5945 | THREADED_TEST(MultiContexts) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5946 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5947 | v8::Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 5948 | templ->Set(v8_str("dummy"), v8::FunctionTemplate::New(DummyCallHandler)); |
| 5949 | |
| 5950 | Local<String> password = v8_str("Password"); |
| 5951 | |
| 5952 | // Create an environment |
| 5953 | LocalContext context0(0, templ); |
| 5954 | context0->SetSecurityToken(password); |
| 5955 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 5956 | global0->Set(v8_str("custom"), v8_num(1234)); |
| 5957 | CHECK_EQ(1234, global0->Get(v8_str("custom"))->Int32Value()); |
| 5958 | |
| 5959 | // Create an independent environment |
| 5960 | LocalContext context1(0, templ); |
| 5961 | context1->SetSecurityToken(password); |
| 5962 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 5963 | global1->Set(v8_str("custom"), v8_num(1234)); |
| 5964 | CHECK_NE(global0, global1); |
| 5965 | CHECK_EQ(1234, global0->Get(v8_str("custom"))->Int32Value()); |
| 5966 | CHECK_EQ(1234, global1->Get(v8_str("custom"))->Int32Value()); |
| 5967 | |
| 5968 | // Now create a new context with the old global |
| 5969 | LocalContext context2(0, templ, global1); |
| 5970 | context2->SetSecurityToken(password); |
| 5971 | v8::Handle<v8::Object> global2 = context2->Global(); |
| 5972 | CHECK_EQ(global1, global2); |
| 5973 | CHECK_EQ(0, global1->Get(v8_str("custom"))->Int32Value()); |
| 5974 | CHECK_EQ(0, global2->Get(v8_str("custom"))->Int32Value()); |
| 5975 | } |
| 5976 | |
| 5977 | |
| 5978 | THREADED_TEST(FunctionPrototypeAcrossContexts) { |
| 5979 | // Make sure that functions created by cloning boilerplates cannot |
| 5980 | // communicate through their __proto__ field. |
| 5981 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 5982 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5983 | |
| 5984 | LocalContext env0; |
| 5985 | v8::Handle<v8::Object> global0 = |
| 5986 | env0->Global(); |
| 5987 | v8::Handle<v8::Object> object0 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5988 | global0->Get(v8_str("Object")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5989 | v8::Handle<v8::Object> tostring0 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5990 | object0->Get(v8_str("toString")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5991 | v8::Handle<v8::Object> proto0 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5992 | tostring0->Get(v8_str("__proto__")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 5993 | proto0->Set(v8_str("custom"), v8_num(1234)); |
| 5994 | |
| 5995 | LocalContext env1; |
| 5996 | v8::Handle<v8::Object> global1 = |
| 5997 | env1->Global(); |
| 5998 | v8::Handle<v8::Object> object1 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 5999 | global1->Get(v8_str("Object")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6000 | v8::Handle<v8::Object> tostring1 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6001 | object1->Get(v8_str("toString")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6002 | v8::Handle<v8::Object> proto1 = |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6003 | tostring1->Get(v8_str("__proto__")).As<v8::Object>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6004 | CHECK(!proto1->Has(v8_str("custom"))); |
| 6005 | } |
| 6006 | |
| 6007 | |
| 6008 | THREADED_TEST(Regress892105) { |
| 6009 | // Make sure that object and array literals created by cloning |
| 6010 | // boilerplates cannot communicate through their __proto__ |
| 6011 | // field. This is rather difficult to check, but we try to add stuff |
| 6012 | // to Object.prototype and Array.prototype and create a new |
| 6013 | // environment. This should succeed. |
| 6014 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6015 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6016 | |
| 6017 | Local<String> source = v8_str("Object.prototype.obj = 1234;" |
| 6018 | "Array.prototype.arr = 4567;" |
| 6019 | "8901"); |
| 6020 | |
| 6021 | LocalContext env0; |
| 6022 | Local<Script> script0 = Script::Compile(source); |
| 6023 | CHECK_EQ(8901.0, script0->Run()->NumberValue()); |
| 6024 | |
| 6025 | LocalContext env1; |
| 6026 | Local<Script> script1 = Script::Compile(source); |
| 6027 | CHECK_EQ(8901.0, script1->Run()->NumberValue()); |
| 6028 | } |
| 6029 | |
| 6030 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6031 | THREADED_TEST(UndetectableObject) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6032 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6033 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6034 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6035 | Local<v8::FunctionTemplate> desc = v8::FunctionTemplate::New(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6036 | desc->InstanceTemplate()->MarkAsUndetectable(); // undetectable |
| 6037 | |
| 6038 | Local<v8::Object> obj = desc->GetFunction()->NewInstance(); |
| 6039 | env->Global()->Set(v8_str("undetectable"), obj); |
| 6040 | |
| 6041 | ExpectString("undetectable.toString()", "[object Object]"); |
| 6042 | ExpectString("typeof undetectable", "undefined"); |
| 6043 | ExpectString("typeof(undetectable)", "undefined"); |
| 6044 | ExpectBoolean("typeof undetectable == 'undefined'", true); |
| 6045 | ExpectBoolean("typeof undetectable == 'object'", false); |
| 6046 | ExpectBoolean("if (undetectable) { true; } else { false; }", false); |
| 6047 | ExpectBoolean("!undetectable", true); |
| 6048 | |
| 6049 | ExpectObject("true&&undetectable", obj); |
| 6050 | ExpectBoolean("false&&undetectable", false); |
| 6051 | ExpectBoolean("true||undetectable", true); |
| 6052 | ExpectObject("false||undetectable", obj); |
| 6053 | |
| 6054 | ExpectObject("undetectable&&true", obj); |
| 6055 | ExpectObject("undetectable&&false", obj); |
| 6056 | ExpectBoolean("undetectable||true", true); |
| 6057 | ExpectBoolean("undetectable||false", false); |
| 6058 | |
| 6059 | ExpectBoolean("undetectable==null", true); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6060 | ExpectBoolean("null==undetectable", true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6061 | ExpectBoolean("undetectable==undefined", true); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6062 | ExpectBoolean("undefined==undetectable", true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6063 | ExpectBoolean("undetectable==undetectable", true); |
| 6064 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6065 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6066 | ExpectBoolean("undetectable===null", false); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6067 | ExpectBoolean("null===undetectable", false); |
| 6068 | ExpectBoolean("undetectable===undefined", false); |
| 6069 | ExpectBoolean("undefined===undetectable", false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6070 | ExpectBoolean("undetectable===undetectable", true); |
| 6071 | } |
| 6072 | |
| 6073 | |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6074 | THREADED_TEST(VoidLiteral) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6075 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6076 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6077 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6078 | Local<v8::FunctionTemplate> desc = v8::FunctionTemplate::New(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 6079 | desc->InstanceTemplate()->MarkAsUndetectable(); // undetectable |
| 6080 | |
| 6081 | Local<v8::Object> obj = desc->GetFunction()->NewInstance(); |
| 6082 | env->Global()->Set(v8_str("undetectable"), obj); |
| 6083 | |
| 6084 | ExpectBoolean("undefined == void 0", true); |
| 6085 | ExpectBoolean("undetectable == void 0", true); |
| 6086 | ExpectBoolean("null == void 0", true); |
| 6087 | ExpectBoolean("undefined === void 0", true); |
| 6088 | ExpectBoolean("undetectable === void 0", false); |
| 6089 | ExpectBoolean("null === void 0", false); |
| 6090 | |
| 6091 | ExpectBoolean("void 0 == undefined", true); |
| 6092 | ExpectBoolean("void 0 == undetectable", true); |
| 6093 | ExpectBoolean("void 0 == null", true); |
| 6094 | ExpectBoolean("void 0 === undefined", true); |
| 6095 | ExpectBoolean("void 0 === undetectable", false); |
| 6096 | ExpectBoolean("void 0 === null", false); |
| 6097 | |
| 6098 | ExpectString("(function() {" |
| 6099 | " try {" |
| 6100 | " return x === void 0;" |
| 6101 | " } catch(e) {" |
| 6102 | " return e.toString();" |
| 6103 | " }" |
| 6104 | "})()", |
| 6105 | "ReferenceError: x is not defined"); |
| 6106 | ExpectString("(function() {" |
| 6107 | " try {" |
| 6108 | " return void 0 === x;" |
| 6109 | " } catch(e) {" |
| 6110 | " return e.toString();" |
| 6111 | " }" |
| 6112 | "})()", |
| 6113 | "ReferenceError: x is not defined"); |
| 6114 | } |
| 6115 | |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6116 | |
| 6117 | THREADED_TEST(ExtensibleOnUndetectable) { |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6118 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6119 | v8::HandleScope scope(env->GetIsolate()); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6120 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 6121 | Local<v8::FunctionTemplate> desc = v8::FunctionTemplate::New(); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6122 | desc->InstanceTemplate()->MarkAsUndetectable(); // undetectable |
| 6123 | |
| 6124 | Local<v8::Object> obj = desc->GetFunction()->NewInstance(); |
| 6125 | env->Global()->Set(v8_str("undetectable"), obj); |
| 6126 | |
| 6127 | Local<String> source = v8_str("undetectable.x = 42;" |
| 6128 | "undetectable.x"); |
| 6129 | |
| 6130 | Local<Script> script = Script::Compile(source); |
| 6131 | |
| 6132 | CHECK_EQ(v8::Integer::New(42), script->Run()); |
| 6133 | |
| 6134 | ExpectBoolean("Object.isExtensible(undetectable)", true); |
| 6135 | |
| 6136 | source = v8_str("Object.preventExtensions(undetectable);"); |
| 6137 | script = Script::Compile(source); |
| 6138 | script->Run(); |
| 6139 | ExpectBoolean("Object.isExtensible(undetectable)", false); |
| 6140 | |
| 6141 | source = v8_str("undetectable.y = 2000;"); |
| 6142 | script = Script::Compile(source); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6143 | script->Run(); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 6144 | ExpectBoolean("undetectable.y == undefined", true); |
kmillikin@chromium.org | 69ea396 | 2010-07-05 11:01:40 +0000 | [diff] [blame] | 6145 | } |
| 6146 | |
| 6147 | |
| 6148 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6149 | THREADED_TEST(UndetectableString) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6150 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6151 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6152 | |
| 6153 | Local<String> obj = String::NewUndetectable("foo"); |
| 6154 | env->Global()->Set(v8_str("undetectable"), obj); |
| 6155 | |
| 6156 | ExpectString("undetectable", "foo"); |
| 6157 | ExpectString("typeof undetectable", "undefined"); |
| 6158 | ExpectString("typeof(undetectable)", "undefined"); |
| 6159 | ExpectBoolean("typeof undetectable == 'undefined'", true); |
| 6160 | ExpectBoolean("typeof undetectable == 'string'", false); |
| 6161 | ExpectBoolean("if (undetectable) { true; } else { false; }", false); |
| 6162 | ExpectBoolean("!undetectable", true); |
| 6163 | |
| 6164 | ExpectObject("true&&undetectable", obj); |
| 6165 | ExpectBoolean("false&&undetectable", false); |
| 6166 | ExpectBoolean("true||undetectable", true); |
| 6167 | ExpectObject("false||undetectable", obj); |
| 6168 | |
| 6169 | ExpectObject("undetectable&&true", obj); |
| 6170 | ExpectObject("undetectable&&false", obj); |
| 6171 | ExpectBoolean("undetectable||true", true); |
| 6172 | ExpectBoolean("undetectable||false", false); |
| 6173 | |
| 6174 | ExpectBoolean("undetectable==null", true); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6175 | ExpectBoolean("null==undetectable", true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6176 | ExpectBoolean("undetectable==undefined", true); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6177 | ExpectBoolean("undefined==undetectable", true); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6178 | ExpectBoolean("undetectable==undetectable", true); |
| 6179 | |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6180 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6181 | ExpectBoolean("undetectable===null", false); |
kasperl@chromium.org | 8ccb0be | 2009-04-07 07:21:39 +0000 | [diff] [blame] | 6182 | ExpectBoolean("null===undetectable", false); |
| 6183 | ExpectBoolean("undetectable===undefined", false); |
| 6184 | ExpectBoolean("undefined===undetectable", false); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6185 | ExpectBoolean("undetectable===undetectable", true); |
| 6186 | } |
| 6187 | |
| 6188 | |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6189 | TEST(UndetectableOptimized) { |
| 6190 | i::FLAG_allow_natives_syntax = true; |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6191 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6192 | v8::HandleScope scope(env->GetIsolate()); |
vegorov@chromium.org | 7304bca | 2011-05-16 12:14:13 +0000 | [diff] [blame] | 6193 | |
| 6194 | Local<String> obj = String::NewUndetectable("foo"); |
| 6195 | env->Global()->Set(v8_str("undetectable"), obj); |
| 6196 | env->Global()->Set(v8_str("detectable"), v8_str("bar")); |
| 6197 | |
| 6198 | ExpectString( |
| 6199 | "function testBranch() {" |
| 6200 | " if (!%_IsUndetectableObject(undetectable)) throw 1;" |
| 6201 | " if (%_IsUndetectableObject(detectable)) throw 2;" |
| 6202 | "}\n" |
| 6203 | "function testBool() {" |
| 6204 | " var b1 = !%_IsUndetectableObject(undetectable);" |
| 6205 | " var b2 = %_IsUndetectableObject(detectable);" |
| 6206 | " if (b1) throw 3;" |
| 6207 | " if (b2) throw 4;" |
| 6208 | " return b1 == b2;" |
| 6209 | "}\n" |
| 6210 | "%OptimizeFunctionOnNextCall(testBranch);" |
| 6211 | "%OptimizeFunctionOnNextCall(testBool);" |
| 6212 | "for (var i = 0; i < 10; i++) {" |
| 6213 | " testBranch();" |
| 6214 | " testBool();" |
| 6215 | "}\n" |
| 6216 | "\"PASS\"", |
| 6217 | "PASS"); |
| 6218 | } |
| 6219 | |
| 6220 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6221 | template <typename T> static void USE(T) { } |
| 6222 | |
| 6223 | |
| 6224 | // This test is not intended to be run, just type checked. |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6225 | static inline void PersistentHandles(v8::Isolate* isolate) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6226 | USE(PersistentHandles); |
| 6227 | Local<String> str = v8_str("foo"); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6228 | v8::Persistent<String> p_str(isolate, str); |
| 6229 | p_str.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6230 | Local<Script> scr = Script::Compile(v8_str("")); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6231 | v8::Persistent<Script> p_scr(isolate, scr); |
| 6232 | p_scr.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6233 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6234 | v8::Persistent<ObjectTemplate> p_templ(isolate, templ); |
| 6235 | p_templ.Dispose(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6236 | } |
| 6237 | |
| 6238 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6239 | static void HandleLogDelegator( |
| 6240 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6241 | ApiTestFuzzer::Fuzz(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6242 | } |
| 6243 | |
| 6244 | |
| 6245 | THREADED_TEST(GlobalObjectTemplate) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6246 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6247 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6248 | Local<ObjectTemplate> global_template = ObjectTemplate::New(); |
| 6249 | global_template->Set(v8_str("JSNI_Log"), |
| 6250 | v8::FunctionTemplate::New(HandleLogDelegator)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6251 | v8::Local<Context> context = Context::New(isolate, 0, global_template); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6252 | Context::Scope context_scope(context); |
| 6253 | Script::Compile(v8_str("JSNI_Log('LOG')"))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6254 | } |
| 6255 | |
| 6256 | |
| 6257 | static const char* kSimpleExtensionSource = |
| 6258 | "function Foo() {" |
| 6259 | " return 4;" |
| 6260 | "}"; |
| 6261 | |
| 6262 | |
| 6263 | THREADED_TEST(SimpleExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6264 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6265 | v8::RegisterExtension(new Extension("simpletest", kSimpleExtensionSource)); |
| 6266 | const char* extension_names[] = { "simpletest" }; |
| 6267 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6268 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6269 | Context::New(CcTest::isolate(), &extensions); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6270 | Context::Scope lock(context); |
| 6271 | v8::Handle<Value> result = Script::Compile(v8_str("Foo()"))->Run(); |
| 6272 | CHECK_EQ(result, v8::Integer::New(4)); |
| 6273 | } |
| 6274 | |
| 6275 | |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 6276 | THREADED_TEST(NullExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6277 | v8::HandleScope handle_scope(CcTest::isolate()); |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 6278 | v8::RegisterExtension(new Extension("nulltest", NULL)); |
| 6279 | const char* extension_names[] = { "nulltest" }; |
| 6280 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6281 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6282 | Context::New(CcTest::isolate(), &extensions); |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 6283 | Context::Scope lock(context); |
| 6284 | v8::Handle<Value> result = Script::Compile(v8_str("1+3"))->Run(); |
| 6285 | CHECK_EQ(result, v8::Integer::New(4)); |
| 6286 | } |
| 6287 | |
| 6288 | |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6289 | static const char* kEmbeddedExtensionSource = |
| 6290 | "function Ret54321(){return 54321;}~~@@$" |
| 6291 | "$%% THIS IS A SERIES OF NON-NULL-TERMINATED STRINGS."; |
| 6292 | static const int kEmbeddedExtensionSourceValidLen = 34; |
| 6293 | |
| 6294 | |
| 6295 | THREADED_TEST(ExtensionMissingSourceLength) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6296 | v8::HandleScope handle_scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6297 | v8::RegisterExtension(new Extension("srclentest_fail", |
| 6298 | kEmbeddedExtensionSource)); |
| 6299 | const char* extension_names[] = { "srclentest_fail" }; |
| 6300 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6301 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6302 | Context::New(CcTest::isolate(), &extensions); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6303 | CHECK_EQ(0, *context); |
| 6304 | } |
| 6305 | |
| 6306 | |
| 6307 | THREADED_TEST(ExtensionWithSourceLength) { |
| 6308 | for (int source_len = kEmbeddedExtensionSourceValidLen - 1; |
| 6309 | source_len <= kEmbeddedExtensionSourceValidLen + 1; ++source_len) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6310 | v8::HandleScope handle_scope(CcTest::isolate()); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6311 | i::ScopedVector<char> extension_name(32); |
| 6312 | i::OS::SNPrintF(extension_name, "ext #%d", source_len); |
| 6313 | v8::RegisterExtension(new Extension(extension_name.start(), |
| 6314 | kEmbeddedExtensionSource, 0, 0, |
| 6315 | source_len)); |
| 6316 | const char* extension_names[1] = { extension_name.start() }; |
| 6317 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6318 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6319 | Context::New(CcTest::isolate(), &extensions); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 6320 | if (source_len == kEmbeddedExtensionSourceValidLen) { |
| 6321 | Context::Scope lock(context); |
| 6322 | v8::Handle<Value> result = Script::Compile(v8_str("Ret54321()"))->Run(); |
| 6323 | CHECK_EQ(v8::Integer::New(54321), result); |
| 6324 | } else { |
| 6325 | // Anything but exactly the right length should fail to compile. |
| 6326 | CHECK_EQ(0, *context); |
| 6327 | } |
| 6328 | } |
| 6329 | } |
| 6330 | |
| 6331 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6332 | static const char* kEvalExtensionSource1 = |
| 6333 | "function UseEval1() {" |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6334 | " var x = 42;" |
| 6335 | " return eval('x');" |
| 6336 | "}"; |
| 6337 | |
| 6338 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6339 | static const char* kEvalExtensionSource2 = |
| 6340 | "(function() {" |
| 6341 | " var x = 42;" |
| 6342 | " function e() {" |
| 6343 | " return eval('x');" |
| 6344 | " }" |
| 6345 | " this.UseEval2 = e;" |
| 6346 | "})()"; |
| 6347 | |
| 6348 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6349 | THREADED_TEST(UseEvalFromExtension) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6350 | v8::HandleScope handle_scope(CcTest::isolate()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6351 | v8::RegisterExtension(new Extension("evaltest1", kEvalExtensionSource1)); |
| 6352 | v8::RegisterExtension(new Extension("evaltest2", kEvalExtensionSource2)); |
| 6353 | const char* extension_names[] = { "evaltest1", "evaltest2" }; |
| 6354 | v8::ExtensionConfiguration extensions(2, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6355 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6356 | Context::New(CcTest::isolate(), &extensions); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6357 | Context::Scope lock(context); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6358 | v8::Handle<Value> result = Script::Compile(v8_str("UseEval1()"))->Run(); |
| 6359 | CHECK_EQ(result, v8::Integer::New(42)); |
| 6360 | result = Script::Compile(v8_str("UseEval2()"))->Run(); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6361 | CHECK_EQ(result, v8::Integer::New(42)); |
| 6362 | } |
| 6363 | |
| 6364 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6365 | static const char* kWithExtensionSource1 = |
| 6366 | "function UseWith1() {" |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6367 | " var x = 42;" |
| 6368 | " with({x:87}) { return x; }" |
| 6369 | "}"; |
| 6370 | |
| 6371 | |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6372 | |
| 6373 | static const char* kWithExtensionSource2 = |
| 6374 | "(function() {" |
| 6375 | " var x = 42;" |
| 6376 | " function e() {" |
| 6377 | " with ({x:87}) { return x; }" |
| 6378 | " }" |
| 6379 | " this.UseWith2 = e;" |
| 6380 | "})()"; |
| 6381 | |
| 6382 | |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6383 | THREADED_TEST(UseWithFromExtension) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6384 | v8::HandleScope handle_scope(CcTest::isolate()); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6385 | v8::RegisterExtension(new Extension("withtest1", kWithExtensionSource1)); |
| 6386 | v8::RegisterExtension(new Extension("withtest2", kWithExtensionSource2)); |
| 6387 | const char* extension_names[] = { "withtest1", "withtest2" }; |
| 6388 | v8::ExtensionConfiguration extensions(2, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6389 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6390 | Context::New(CcTest::isolate(), &extensions); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6391 | Context::Scope lock(context); |
ager@chromium.org | 18ad94b | 2009-09-02 08:22:29 +0000 | [diff] [blame] | 6392 | v8::Handle<Value> result = Script::Compile(v8_str("UseWith1()"))->Run(); |
| 6393 | CHECK_EQ(result, v8::Integer::New(87)); |
| 6394 | result = Script::Compile(v8_str("UseWith2()"))->Run(); |
kasperl@chromium.org | 2d18d10 | 2009-04-15 13:27:32 +0000 | [diff] [blame] | 6395 | CHECK_EQ(result, v8::Integer::New(87)); |
| 6396 | } |
| 6397 | |
| 6398 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6399 | THREADED_TEST(AutoExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6400 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6401 | Extension* extension = new Extension("autotest", kSimpleExtensionSource); |
| 6402 | extension->set_auto_enable(true); |
| 6403 | v8::RegisterExtension(extension); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6404 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6405 | Context::New(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6406 | Context::Scope lock(context); |
| 6407 | v8::Handle<Value> result = Script::Compile(v8_str("Foo()"))->Run(); |
| 6408 | CHECK_EQ(result, v8::Integer::New(4)); |
| 6409 | } |
| 6410 | |
| 6411 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6412 | static const char* kSyntaxErrorInExtensionSource = |
| 6413 | "["; |
| 6414 | |
| 6415 | |
| 6416 | // Test that a syntax error in an extension does not cause a fatal |
| 6417 | // error but results in an empty context. |
| 6418 | THREADED_TEST(SyntaxErrorExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6419 | v8::HandleScope handle_scope(CcTest::isolate()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6420 | v8::RegisterExtension(new Extension("syntaxerror", |
| 6421 | kSyntaxErrorInExtensionSource)); |
| 6422 | const char* extension_names[] = { "syntaxerror" }; |
| 6423 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6424 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6425 | Context::New(CcTest::isolate(), &extensions); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6426 | CHECK(context.IsEmpty()); |
| 6427 | } |
| 6428 | |
| 6429 | |
| 6430 | static const char* kExceptionInExtensionSource = |
| 6431 | "throw 42"; |
| 6432 | |
| 6433 | |
| 6434 | // Test that an exception when installing an extension does not cause |
| 6435 | // a fatal error but results in an empty context. |
| 6436 | THREADED_TEST(ExceptionExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6437 | v8::HandleScope handle_scope(CcTest::isolate()); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6438 | v8::RegisterExtension(new Extension("exception", |
| 6439 | kExceptionInExtensionSource)); |
| 6440 | const char* extension_names[] = { "exception" }; |
| 6441 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6442 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6443 | Context::New(CcTest::isolate(), &extensions); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 6444 | CHECK(context.IsEmpty()); |
| 6445 | } |
| 6446 | |
| 6447 | |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 6448 | static const char* kNativeCallInExtensionSource = |
| 6449 | "function call_runtime_last_index_of(x) {" |
| 6450 | " return %StringLastIndexOf(x, 'bob', 10);" |
| 6451 | "}"; |
| 6452 | |
| 6453 | |
| 6454 | static const char* kNativeCallTest = |
| 6455 | "call_runtime_last_index_of('bobbobboellebobboellebobbob');"; |
| 6456 | |
| 6457 | // Test that a native runtime calls are supported in extensions. |
| 6458 | THREADED_TEST(NativeCallInExtensions) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6459 | v8::HandleScope handle_scope(CcTest::isolate()); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 6460 | v8::RegisterExtension(new Extension("nativecall", |
| 6461 | kNativeCallInExtensionSource)); |
| 6462 | const char* extension_names[] = { "nativecall" }; |
| 6463 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6464 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6465 | Context::New(CcTest::isolate(), &extensions); |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 6466 | Context::Scope lock(context); |
| 6467 | v8::Handle<Value> result = Script::Compile(v8_str(kNativeCallTest))->Run(); |
| 6468 | CHECK_EQ(result, v8::Integer::New(3)); |
| 6469 | } |
| 6470 | |
| 6471 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6472 | class NativeFunctionExtension : public Extension { |
| 6473 | public: |
| 6474 | NativeFunctionExtension(const char* name, |
| 6475 | const char* source, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6476 | v8::FunctionCallback fun = &Echo) |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6477 | : Extension(name, source), |
| 6478 | function_(fun) { } |
| 6479 | |
| 6480 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 6481 | v8::Handle<v8::String> name) { |
| 6482 | return v8::FunctionTemplate::New(function_); |
| 6483 | } |
| 6484 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6485 | static void Echo(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 6486 | if (args.Length() >= 1) args.GetReturnValue().Set(args[0]); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6487 | } |
| 6488 | private: |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6489 | v8::FunctionCallback function_; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6490 | }; |
| 6491 | |
| 6492 | |
| 6493 | THREADED_TEST(NativeFunctionDeclaration) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6494 | v8::HandleScope handle_scope(CcTest::isolate()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6495 | const char* name = "nativedecl"; |
| 6496 | v8::RegisterExtension(new NativeFunctionExtension(name, |
| 6497 | "native function foo();")); |
| 6498 | const char* extension_names[] = { name }; |
| 6499 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6500 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6501 | Context::New(CcTest::isolate(), &extensions); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6502 | Context::Scope lock(context); |
| 6503 | v8::Handle<Value> result = Script::Compile(v8_str("foo(42);"))->Run(); |
| 6504 | CHECK_EQ(result, v8::Integer::New(42)); |
| 6505 | } |
| 6506 | |
| 6507 | |
| 6508 | THREADED_TEST(NativeFunctionDeclarationError) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6509 | v8::HandleScope handle_scope(CcTest::isolate()); |
lrn@chromium.org | 67e236d | 2011-06-23 10:18:16 +0000 | [diff] [blame] | 6510 | const char* name = "nativedeclerr"; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6511 | // Syntax error in extension code. |
| 6512 | v8::RegisterExtension(new NativeFunctionExtension(name, |
| 6513 | "native\nfunction foo();")); |
| 6514 | const char* extension_names[] = { name }; |
| 6515 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6516 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6517 | Context::New(CcTest::isolate(), &extensions); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6518 | CHECK(context.IsEmpty()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6519 | } |
| 6520 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6521 | |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6522 | THREADED_TEST(NativeFunctionDeclarationErrorEscape) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6523 | v8::HandleScope handle_scope(CcTest::isolate()); |
lrn@chromium.org | 67e236d | 2011-06-23 10:18:16 +0000 | [diff] [blame] | 6524 | const char* name = "nativedeclerresc"; |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6525 | // Syntax error in extension code - escape code in "native" means that |
| 6526 | // it's not treated as a keyword. |
| 6527 | v8::RegisterExtension(new NativeFunctionExtension( |
| 6528 | name, |
| 6529 | "nativ\\u0065 function foo();")); |
| 6530 | const char* extension_names[] = { name }; |
| 6531 | v8::ExtensionConfiguration extensions(1, extension_names); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6532 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6533 | Context::New(CcTest::isolate(), &extensions); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6534 | CHECK(context.IsEmpty()); |
whesse@chromium.org | 7b26015 | 2011-06-20 15:33:18 +0000 | [diff] [blame] | 6535 | } |
| 6536 | |
| 6537 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6538 | static void CheckDependencies(const char* name, const char* expected) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6539 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6540 | v8::ExtensionConfiguration config(1, &name); |
| 6541 | LocalContext context(&config); |
| 6542 | CHECK_EQ(String::New(expected), context->Global()->Get(v8_str("loaded"))); |
| 6543 | } |
| 6544 | |
| 6545 | |
| 6546 | /* |
| 6547 | * Configuration: |
| 6548 | * |
| 6549 | * /-- B <--\ |
| 6550 | * A <- -- D <-- E |
| 6551 | * \-- C <--/ |
| 6552 | */ |
| 6553 | THREADED_TEST(ExtensionDependency) { |
| 6554 | static const char* kEDeps[] = { "D" }; |
| 6555 | v8::RegisterExtension(new Extension("E", "this.loaded += 'E';", 1, kEDeps)); |
| 6556 | static const char* kDDeps[] = { "B", "C" }; |
| 6557 | v8::RegisterExtension(new Extension("D", "this.loaded += 'D';", 2, kDDeps)); |
| 6558 | static const char* kBCDeps[] = { "A" }; |
| 6559 | v8::RegisterExtension(new Extension("B", "this.loaded += 'B';", 1, kBCDeps)); |
| 6560 | v8::RegisterExtension(new Extension("C", "this.loaded += 'C';", 1, kBCDeps)); |
| 6561 | v8::RegisterExtension(new Extension("A", "this.loaded += 'A';")); |
| 6562 | CheckDependencies("A", "undefinedA"); |
| 6563 | CheckDependencies("B", "undefinedAB"); |
| 6564 | CheckDependencies("C", "undefinedAC"); |
| 6565 | CheckDependencies("D", "undefinedABCD"); |
| 6566 | CheckDependencies("E", "undefinedABCDE"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6567 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6568 | static const char* exts[2] = { "C", "E" }; |
| 6569 | v8::ExtensionConfiguration config(2, exts); |
| 6570 | LocalContext context(&config); |
| 6571 | CHECK_EQ(v8_str("undefinedACBDE"), context->Global()->Get(v8_str("loaded"))); |
| 6572 | } |
| 6573 | |
| 6574 | |
| 6575 | static const char* kExtensionTestScript = |
| 6576 | "native function A();" |
| 6577 | "native function B();" |
| 6578 | "native function C();" |
| 6579 | "function Foo(i) {" |
| 6580 | " if (i == 0) return A();" |
| 6581 | " if (i == 1) return B();" |
| 6582 | " if (i == 2) return C();" |
| 6583 | "}"; |
| 6584 | |
| 6585 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6586 | static void CallFun(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6587 | ApiTestFuzzer::Fuzz(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6588 | if (args.IsConstructCall()) { |
| 6589 | args.This()->Set(v8_str("data"), args.Data()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6590 | args.GetReturnValue().SetNull(); |
| 6591 | return; |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6592 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6593 | args.GetReturnValue().Set(args.Data()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| 6596 | |
| 6597 | class FunctionExtension : public Extension { |
| 6598 | public: |
| 6599 | FunctionExtension() : Extension("functiontest", kExtensionTestScript) { } |
| 6600 | virtual v8::Handle<v8::FunctionTemplate> GetNativeFunction( |
| 6601 | v8::Handle<String> name); |
| 6602 | }; |
| 6603 | |
| 6604 | |
| 6605 | static int lookup_count = 0; |
| 6606 | v8::Handle<v8::FunctionTemplate> FunctionExtension::GetNativeFunction( |
| 6607 | v8::Handle<String> name) { |
| 6608 | lookup_count++; |
| 6609 | if (name->Equals(v8_str("A"))) { |
| 6610 | return v8::FunctionTemplate::New(CallFun, v8::Integer::New(8)); |
| 6611 | } else if (name->Equals(v8_str("B"))) { |
| 6612 | return v8::FunctionTemplate::New(CallFun, v8::Integer::New(7)); |
| 6613 | } else if (name->Equals(v8_str("C"))) { |
| 6614 | return v8::FunctionTemplate::New(CallFun, v8::Integer::New(6)); |
| 6615 | } else { |
| 6616 | return v8::Handle<v8::FunctionTemplate>(); |
| 6617 | } |
| 6618 | } |
| 6619 | |
| 6620 | |
| 6621 | THREADED_TEST(FunctionLookup) { |
| 6622 | v8::RegisterExtension(new FunctionExtension()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6623 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6624 | static const char* exts[1] = { "functiontest" }; |
| 6625 | v8::ExtensionConfiguration config(1, exts); |
| 6626 | LocalContext context(&config); |
| 6627 | CHECK_EQ(3, lookup_count); |
| 6628 | CHECK_EQ(v8::Integer::New(8), Script::Compile(v8_str("Foo(0)"))->Run()); |
| 6629 | CHECK_EQ(v8::Integer::New(7), Script::Compile(v8_str("Foo(1)"))->Run()); |
| 6630 | CHECK_EQ(v8::Integer::New(6), Script::Compile(v8_str("Foo(2)"))->Run()); |
| 6631 | } |
| 6632 | |
| 6633 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6634 | THREADED_TEST(NativeFunctionConstructCall) { |
| 6635 | v8::RegisterExtension(new FunctionExtension()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6636 | v8::HandleScope handle_scope(CcTest::isolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 6637 | static const char* exts[1] = { "functiontest" }; |
| 6638 | v8::ExtensionConfiguration config(1, exts); |
| 6639 | LocalContext context(&config); |
| 6640 | for (int i = 0; i < 10; i++) { |
| 6641 | // Run a few times to ensure that allocation of objects doesn't |
| 6642 | // change behavior of a constructor function. |
| 6643 | CHECK_EQ(v8::Integer::New(8), |
| 6644 | Script::Compile(v8_str("(new A()).data"))->Run()); |
| 6645 | CHECK_EQ(v8::Integer::New(7), |
| 6646 | Script::Compile(v8_str("(new B()).data"))->Run()); |
| 6647 | CHECK_EQ(v8::Integer::New(6), |
| 6648 | Script::Compile(v8_str("(new C()).data"))->Run()); |
| 6649 | } |
| 6650 | } |
| 6651 | |
| 6652 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6653 | static const char* last_location; |
| 6654 | static const char* last_message; |
| 6655 | void StoringErrorCallback(const char* location, const char* message) { |
| 6656 | if (last_location == NULL) { |
| 6657 | last_location = location; |
| 6658 | last_message = message; |
| 6659 | } |
| 6660 | } |
| 6661 | |
| 6662 | |
| 6663 | // ErrorReporting creates a circular extensions configuration and |
| 6664 | // tests that the fatal error handler gets called. This renders V8 |
| 6665 | // unusable and therefore this test cannot be run in parallel. |
| 6666 | TEST(ErrorReporting) { |
| 6667 | v8::V8::SetFatalErrorHandler(StoringErrorCallback); |
| 6668 | static const char* aDeps[] = { "B" }; |
| 6669 | v8::RegisterExtension(new Extension("A", "", 1, aDeps)); |
| 6670 | static const char* bDeps[] = { "A" }; |
| 6671 | v8::RegisterExtension(new Extension("B", "", 1, bDeps)); |
| 6672 | last_location = NULL; |
| 6673 | v8::ExtensionConfiguration config(1, bDeps); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6674 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6675 | Context::New(CcTest::isolate(), &config); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6676 | CHECK(context.IsEmpty()); |
| 6677 | CHECK_NE(last_location, NULL); |
| 6678 | } |
| 6679 | |
| 6680 | |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6681 | static const char* js_code_causing_huge_string_flattening = |
| 6682 | "var str = 'X';" |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 6683 | "for (var i = 0; i < 30; i++) {" |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6684 | " str = str + str;" |
| 6685 | "}" |
| 6686 | "str.match(/X/);"; |
| 6687 | |
| 6688 | |
| 6689 | void OOMCallback(const char* location, const char* message) { |
| 6690 | exit(0); |
| 6691 | } |
| 6692 | |
| 6693 | |
| 6694 | TEST(RegexpOutOfMemory) { |
| 6695 | // Execute a script that causes out of memory when flattening a string. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6696 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6697 | v8::V8::SetFatalErrorHandler(OOMCallback); |
| 6698 | LocalContext context; |
| 6699 | Local<Script> script = |
| 6700 | Script::Compile(String::New(js_code_causing_huge_string_flattening)); |
| 6701 | last_location = NULL; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 6702 | script->Run(); |
ager@chromium.org | 7c537e2 | 2008-10-16 08:43:32 +0000 | [diff] [blame] | 6703 | |
| 6704 | CHECK(false); // Should not return. |
| 6705 | } |
| 6706 | |
| 6707 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6708 | static void MissingScriptInfoMessageListener(v8::Handle<v8::Message> message, |
| 6709 | v8::Handle<Value> data) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 6710 | CHECK(message->GetScriptResourceName()->IsUndefined()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 6711 | CHECK_EQ(v8::Undefined(CcTest::isolate()), message->GetScriptResourceName()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6712 | message->GetLineNumber(); |
| 6713 | message->GetSourceLine(); |
| 6714 | } |
| 6715 | |
| 6716 | |
| 6717 | THREADED_TEST(ErrorWithMissingScriptInfo) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6718 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6719 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6720 | v8::V8::AddMessageListener(MissingScriptInfoMessageListener); |
| 6721 | Script::Compile(v8_str("throw Error()"))->Run(); |
| 6722 | v8::V8::RemoveMessageListeners(MissingScriptInfoMessageListener); |
| 6723 | } |
| 6724 | |
| 6725 | |
| 6726 | int global_index = 0; |
| 6727 | |
| 6728 | class Snorkel { |
| 6729 | public: |
| 6730 | Snorkel() { index_ = global_index++; } |
| 6731 | int index_; |
| 6732 | }; |
| 6733 | |
| 6734 | class Whammy { |
| 6735 | public: |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6736 | explicit Whammy(v8::Isolate* isolate) : cursor_(0), isolate_(isolate) { } |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6737 | ~Whammy() { script_.Dispose(); } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6738 | v8::Handle<Script> getScript() { |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6739 | if (script_.IsEmpty()) script_.Reset(isolate_, v8_compile("({}).blammo")); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6740 | return Local<Script>::New(isolate_, script_); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6741 | } |
| 6742 | |
| 6743 | public: |
| 6744 | static const int kObjectCount = 256; |
| 6745 | int cursor_; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6746 | v8::Isolate* isolate_; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6747 | v8::Persistent<v8::Object> objects_[kObjectCount]; |
| 6748 | v8::Persistent<Script> script_; |
| 6749 | }; |
| 6750 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6751 | static void HandleWeakReference(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6752 | v8::Persistent<v8::Value>* obj, |
| 6753 | Snorkel* snorkel) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6754 | delete snorkel; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6755 | obj->ClearWeak(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6758 | void WhammyPropertyGetter(Local<String> name, |
| 6759 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6760 | Whammy* whammy = |
| 6761 | static_cast<Whammy*>(v8::Handle<v8::External>::Cast(info.Data())->Value()); |
| 6762 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6763 | v8::Persistent<v8::Object>& prev = whammy->objects_[whammy->cursor_]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6764 | |
| 6765 | v8::Handle<v8::Object> obj = v8::Object::New(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6766 | if (!prev.IsEmpty()) { |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6767 | v8::Local<v8::Object>::New(info.GetIsolate(), prev) |
| 6768 | ->Set(v8_str("next"), obj); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6769 | prev.MakeWeak<Value, Snorkel>(new Snorkel(), &HandleWeakReference); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6770 | whammy->objects_[whammy->cursor_].Clear(); |
| 6771 | } |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6772 | whammy->objects_[whammy->cursor_].Reset(info.GetIsolate(), obj); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6773 | whammy->cursor_ = (whammy->cursor_ + 1) % Whammy::kObjectCount; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6774 | info.GetReturnValue().Set(whammy->getScript()->Run()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6775 | } |
| 6776 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 6777 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6778 | THREADED_TEST(WeakReference) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6779 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6780 | v8::Handle<v8::ObjectTemplate> templ= v8::ObjectTemplate::New(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6781 | Whammy* whammy = new Whammy(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6782 | templ->SetNamedPropertyHandler(WhammyPropertyGetter, |
| 6783 | 0, 0, 0, 0, |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6784 | v8::External::New(whammy)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6785 | const char* extension_list[] = { "v8/gc" }; |
| 6786 | v8::ExtensionConfiguration extensions(1, extension_list); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6787 | v8::Handle<Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6788 | Context::New(CcTest::isolate(), &extensions); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6789 | Context::Scope context_scope(context); |
| 6790 | |
| 6791 | v8::Handle<v8::Object> interceptor = templ->NewInstance(); |
| 6792 | context->Global()->Set(v8_str("whammy"), interceptor); |
| 6793 | const char* code = |
| 6794 | "var last;" |
| 6795 | "for (var i = 0; i < 10000; i++) {" |
| 6796 | " var obj = whammy.length;" |
| 6797 | " if (last) last.next = obj;" |
| 6798 | " last = obj;" |
| 6799 | "}" |
| 6800 | "gc();" |
| 6801 | "4"; |
| 6802 | v8::Handle<Value> result = CompileRun(code); |
| 6803 | CHECK_EQ(4.0, result->NumberValue()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 6804 | delete whammy; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6808 | static void DisposeAndSetFlag(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6809 | v8::Persistent<v8::Object>* obj, |
| 6810 | bool* data) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6811 | obj->Dispose(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6812 | *(data) = true; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6813 | } |
| 6814 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6815 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6816 | THREADED_TEST(IndependentWeakHandle) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6817 | v8::Isolate* iso = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6818 | v8::HandleScope scope(iso); |
| 6819 | v8::Handle<Context> context = Context::New(iso); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6820 | Context::Scope context_scope(context); |
| 6821 | |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6822 | v8::Persistent<v8::Object> object_a, object_b; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6823 | |
| 6824 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6825 | v8::HandleScope handle_scope(iso); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6826 | object_a.Reset(iso, v8::Object::New()); |
| 6827 | object_b.Reset(iso, v8::Object::New()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6828 | } |
| 6829 | |
| 6830 | bool object_a_disposed = false; |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6831 | bool object_b_disposed = false; |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6832 | object_a.MakeWeak(&object_a_disposed, &DisposeAndSetFlag); |
| 6833 | object_b.MakeWeak(&object_b_disposed, &DisposeAndSetFlag); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6834 | CHECK(!object_b.IsIndependent()); |
| 6835 | object_a.MarkIndependent(); |
| 6836 | object_b.MarkIndependent(); |
| 6837 | CHECK(object_b.IsIndependent()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6838 | CcTest::heap()->PerformScavenge(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6839 | CHECK(object_a_disposed); |
mvstanton@chromium.org | e4ac3ef | 2012-11-12 14:53:34 +0000 | [diff] [blame] | 6840 | CHECK(object_b_disposed); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6841 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6842 | |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6843 | |
| 6844 | static void InvokeScavenge() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6845 | CcTest::heap()->PerformScavenge(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6846 | } |
| 6847 | |
| 6848 | |
| 6849 | static void InvokeMarkSweep() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6850 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
| 6853 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6854 | static void ForceScavenge(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6855 | v8::Persistent<v8::Object>* obj, |
| 6856 | bool* data) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6857 | obj->Dispose(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6858 | *(data) = true; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6859 | InvokeScavenge(); |
| 6860 | } |
| 6861 | |
| 6862 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6863 | static void ForceMarkSweep(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6864 | v8::Persistent<v8::Object>* obj, |
| 6865 | bool* data) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6866 | obj->Dispose(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6867 | *(data) = true; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6868 | InvokeMarkSweep(); |
| 6869 | } |
| 6870 | |
| 6871 | |
| 6872 | THREADED_TEST(GCFromWeakCallbacks) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6873 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6874 | v8::HandleScope scope(isolate); |
| 6875 | v8::Handle<Context> context = Context::New(isolate); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6876 | Context::Scope context_scope(context); |
| 6877 | |
| 6878 | static const int kNumberOfGCTypes = 2; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6879 | typedef v8::WeakReferenceCallbacks<v8::Object, bool>::Revivable Callback; |
| 6880 | Callback gc_forcing_callback[kNumberOfGCTypes] = |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6881 | {&ForceScavenge, &ForceMarkSweep}; |
| 6882 | |
| 6883 | typedef void (*GCInvoker)(); |
| 6884 | GCInvoker invoke_gc[kNumberOfGCTypes] = {&InvokeScavenge, &InvokeMarkSweep}; |
| 6885 | |
| 6886 | for (int outer_gc = 0; outer_gc < kNumberOfGCTypes; outer_gc++) { |
| 6887 | for (int inner_gc = 0; inner_gc < kNumberOfGCTypes; inner_gc++) { |
| 6888 | v8::Persistent<v8::Object> object; |
| 6889 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6890 | v8::HandleScope handle_scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 6891 | object.Reset(isolate, v8::Object::New()); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6892 | } |
| 6893 | bool disposed = false; |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6894 | object.MakeWeak(&disposed, gc_forcing_callback[inner_gc]); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6895 | object.MarkIndependent(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6896 | invoke_gc[outer_gc](); |
| 6897 | CHECK(disposed); |
| 6898 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6899 | } |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6900 | } |
| 6901 | |
| 6902 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 6903 | static void RevivingCallback(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6904 | v8::Persistent<v8::Object>* obj, |
| 6905 | bool* data) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6906 | obj->ClearWeak(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6907 | *(data) = true; |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6908 | } |
| 6909 | |
| 6910 | |
| 6911 | THREADED_TEST(IndependentHandleRevival) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6912 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 6913 | v8::HandleScope scope(isolate); |
| 6914 | v8::Handle<Context> context = Context::New(isolate); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6915 | Context::Scope context_scope(context); |
| 6916 | |
| 6917 | v8::Persistent<v8::Object> object; |
| 6918 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6919 | v8::HandleScope handle_scope(isolate); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6920 | v8::Local<v8::Object> o = v8::Object::New(); |
| 6921 | object.Reset(isolate, o); |
| 6922 | o->Set(v8_str("x"), v8::Integer::New(1)); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6923 | v8::Local<String> y_str = v8_str("y"); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6924 | o->Set(y_str, y_str); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6925 | } |
| 6926 | bool revived = false; |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 6927 | object.MakeWeak(&revived, &RevivingCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 6928 | object.MarkIndependent(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6929 | CcTest::heap()->PerformScavenge(); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6930 | CHECK(revived); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6931 | CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6932 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6933 | v8::HandleScope handle_scope(isolate); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6934 | v8::Local<v8::Object> o = v8::Local<v8::Object>::New(isolate, object); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6935 | v8::Local<String> y_str = v8_str("y"); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 6936 | CHECK_EQ(v8::Integer::New(1), o->Get(v8_str("x"))); |
| 6937 | CHECK(o->Get(y_str)->Equals(y_str)); |
kmillikin@chromium.org | c53e10d | 2011-05-18 09:12:58 +0000 | [diff] [blame] | 6938 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 6939 | } |
| 6940 | |
| 6941 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6942 | v8::Handle<Function> args_fun; |
| 6943 | |
| 6944 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6945 | static void ArgumentsTestCallback( |
| 6946 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6947 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 6948 | v8::Isolate* isolate = args.GetIsolate(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6949 | CHECK_EQ(args_fun, args.Callee()); |
| 6950 | CHECK_EQ(3, args.Length()); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 6951 | CHECK_EQ(v8::Integer::New(1, isolate), args[0]); |
| 6952 | CHECK_EQ(v8::Integer::New(2, isolate), args[1]); |
| 6953 | CHECK_EQ(v8::Integer::New(3, isolate), args[2]); |
| 6954 | CHECK_EQ(v8::Undefined(isolate), args[3]); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 6955 | v8::HandleScope scope(args.GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6956 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6957 | } |
| 6958 | |
| 6959 | |
| 6960 | THREADED_TEST(Arguments) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 6961 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6962 | v8::Handle<v8::ObjectTemplate> global = ObjectTemplate::New(); |
| 6963 | global->Set(v8_str("f"), v8::FunctionTemplate::New(ArgumentsTestCallback)); |
| 6964 | LocalContext context(NULL, global); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 6965 | args_fun = context->Global()->Get(v8_str("f")).As<Function>(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6966 | v8_compile("f(1, 2, 3)")->Run(); |
| 6967 | } |
| 6968 | |
| 6969 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6970 | static void NoBlockGetterX(Local<String> name, |
| 6971 | const v8::PropertyCallbackInfo<v8::Value>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6972 | } |
| 6973 | |
| 6974 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6975 | static void NoBlockGetterI(uint32_t index, |
| 6976 | const v8::PropertyCallbackInfo<v8::Value>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6977 | } |
| 6978 | |
| 6979 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6980 | static void PDeleter(Local<String> name, |
| 6981 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6982 | if (!name->Equals(v8_str("foo"))) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6983 | return; // not intercepted |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6984 | } |
| 6985 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6986 | info.GetReturnValue().Set(false); // intercepted, don't delete the property |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6990 | static void IDeleter(uint32_t index, |
| 6991 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6992 | if (index != 2) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6993 | return; // not intercepted |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 6996 | info.GetReturnValue().Set(false); // intercepted, don't delete the property |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
| 6999 | |
| 7000 | THREADED_TEST(Deleter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7001 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7002 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 7003 | obj->SetNamedPropertyHandler(NoBlockGetterX, NULL, NULL, PDeleter, NULL); |
| 7004 | obj->SetIndexedPropertyHandler(NoBlockGetterI, NULL, NULL, IDeleter, NULL); |
| 7005 | LocalContext context; |
| 7006 | context->Global()->Set(v8_str("k"), obj->NewInstance()); |
| 7007 | CompileRun( |
| 7008 | "k.foo = 'foo';" |
| 7009 | "k.bar = 'bar';" |
| 7010 | "k[2] = 2;" |
| 7011 | "k[4] = 4;"); |
| 7012 | CHECK(v8_compile("delete k.foo")->Run()->IsFalse()); |
| 7013 | CHECK(v8_compile("delete k.bar")->Run()->IsTrue()); |
| 7014 | |
| 7015 | CHECK_EQ(v8_compile("k.foo")->Run(), v8_str("foo")); |
| 7016 | CHECK(v8_compile("k.bar")->Run()->IsUndefined()); |
| 7017 | |
| 7018 | CHECK(v8_compile("delete k[2]")->Run()->IsFalse()); |
| 7019 | CHECK(v8_compile("delete k[4]")->Run()->IsTrue()); |
| 7020 | |
| 7021 | CHECK_EQ(v8_compile("k[2]")->Run(), v8_num(2)); |
| 7022 | CHECK(v8_compile("k[4]")->Run()->IsUndefined()); |
| 7023 | } |
| 7024 | |
| 7025 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7026 | static void GetK(Local<String> name, |
| 7027 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7028 | ApiTestFuzzer::Fuzz(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7029 | if (name->Equals(v8_str("foo")) || |
| 7030 | name->Equals(v8_str("bar")) || |
| 7031 | name->Equals(v8_str("baz"))) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7032 | info.GetReturnValue().SetUndefined(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7033 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7034 | } |
| 7035 | |
| 7036 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7037 | static void IndexedGetK(uint32_t index, |
| 7038 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7039 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7040 | if (index == 0 || index == 1) info.GetReturnValue().SetUndefined(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7041 | } |
| 7042 | |
| 7043 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7044 | static void NamedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7045 | ApiTestFuzzer::Fuzz(); |
| 7046 | v8::Handle<v8::Array> result = v8::Array::New(3); |
| 7047 | result->Set(v8::Integer::New(0), v8_str("foo")); |
| 7048 | result->Set(v8::Integer::New(1), v8_str("bar")); |
| 7049 | result->Set(v8::Integer::New(2), v8_str("baz")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7050 | info.GetReturnValue().Set(result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7051 | } |
| 7052 | |
| 7053 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7054 | static void IndexedEnum(const v8::PropertyCallbackInfo<v8::Array>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7055 | ApiTestFuzzer::Fuzz(); |
| 7056 | v8::Handle<v8::Array> result = v8::Array::New(2); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7057 | result->Set(v8::Integer::New(0), v8_str("0")); |
| 7058 | result->Set(v8::Integer::New(1), v8_str("1")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7059 | info.GetReturnValue().Set(result); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7060 | } |
| 7061 | |
| 7062 | |
| 7063 | THREADED_TEST(Enumerators) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7064 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7065 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 7066 | obj->SetNamedPropertyHandler(GetK, NULL, NULL, NULL, NamedEnum); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7067 | obj->SetIndexedPropertyHandler(IndexedGetK, NULL, NULL, NULL, IndexedEnum); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7068 | LocalContext context; |
| 7069 | context->Global()->Set(v8_str("k"), obj->NewInstance()); |
| 7070 | v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7071 | "k[10] = 0;" |
| 7072 | "k.a = 0;" |
| 7073 | "k[5] = 0;" |
| 7074 | "k.b = 0;" |
| 7075 | "k[4294967295] = 0;" |
| 7076 | "k.c = 0;" |
| 7077 | "k[4294967296] = 0;" |
| 7078 | "k.d = 0;" |
| 7079 | "k[140000] = 0;" |
| 7080 | "k.e = 0;" |
| 7081 | "k[30000000000] = 0;" |
| 7082 | "k.f = 0;" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7083 | "var result = [];" |
| 7084 | "for (var prop in k) {" |
| 7085 | " result.push(prop);" |
| 7086 | "}" |
| 7087 | "result")); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 7088 | // Check that we get all the property names returned including the |
| 7089 | // ones from the enumerators in the right order: indexed properties |
| 7090 | // in numerical order, indexed interceptor properties, named |
| 7091 | // properties in insertion order, named interceptor properties. |
| 7092 | // This order is not mandated by the spec, so this test is just |
| 7093 | // documenting our behavior. |
| 7094 | CHECK_EQ(17, result->Length()); |
| 7095 | // Indexed properties in numerical order. |
| 7096 | CHECK_EQ(v8_str("5"), result->Get(v8::Integer::New(0))); |
| 7097 | CHECK_EQ(v8_str("10"), result->Get(v8::Integer::New(1))); |
| 7098 | CHECK_EQ(v8_str("140000"), result->Get(v8::Integer::New(2))); |
| 7099 | CHECK_EQ(v8_str("4294967295"), result->Get(v8::Integer::New(3))); |
| 7100 | // Indexed interceptor properties in the order they are returned |
| 7101 | // from the enumerator interceptor. |
| 7102 | CHECK_EQ(v8_str("0"), result->Get(v8::Integer::New(4))); |
| 7103 | CHECK_EQ(v8_str("1"), result->Get(v8::Integer::New(5))); |
| 7104 | // Named properties in insertion order. |
| 7105 | CHECK_EQ(v8_str("a"), result->Get(v8::Integer::New(6))); |
| 7106 | CHECK_EQ(v8_str("b"), result->Get(v8::Integer::New(7))); |
| 7107 | CHECK_EQ(v8_str("c"), result->Get(v8::Integer::New(8))); |
| 7108 | CHECK_EQ(v8_str("4294967296"), result->Get(v8::Integer::New(9))); |
| 7109 | CHECK_EQ(v8_str("d"), result->Get(v8::Integer::New(10))); |
| 7110 | CHECK_EQ(v8_str("e"), result->Get(v8::Integer::New(11))); |
| 7111 | CHECK_EQ(v8_str("30000000000"), result->Get(v8::Integer::New(12))); |
| 7112 | CHECK_EQ(v8_str("f"), result->Get(v8::Integer::New(13))); |
| 7113 | // Named interceptor properties. |
| 7114 | CHECK_EQ(v8_str("foo"), result->Get(v8::Integer::New(14))); |
| 7115 | CHECK_EQ(v8_str("bar"), result->Get(v8::Integer::New(15))); |
| 7116 | CHECK_EQ(v8_str("baz"), result->Get(v8::Integer::New(16))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7117 | } |
| 7118 | |
| 7119 | |
| 7120 | int p_getter_count; |
| 7121 | int p_getter_count2; |
| 7122 | |
| 7123 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7124 | static void PGetter(Local<String> name, |
| 7125 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7126 | ApiTestFuzzer::Fuzz(); |
| 7127 | p_getter_count++; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7128 | v8::Handle<v8::Object> global = |
| 7129 | info.GetIsolate()->GetCurrentContext()->Global(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7130 | CHECK_EQ(info.Holder(), global->Get(v8_str("o1"))); |
| 7131 | if (name->Equals(v8_str("p1"))) { |
| 7132 | CHECK_EQ(info.This(), global->Get(v8_str("o1"))); |
| 7133 | } else if (name->Equals(v8_str("p2"))) { |
| 7134 | CHECK_EQ(info.This(), global->Get(v8_str("o2"))); |
| 7135 | } else if (name->Equals(v8_str("p3"))) { |
| 7136 | CHECK_EQ(info.This(), global->Get(v8_str("o3"))); |
| 7137 | } else if (name->Equals(v8_str("p4"))) { |
| 7138 | CHECK_EQ(info.This(), global->Get(v8_str("o4"))); |
| 7139 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7140 | } |
| 7141 | |
| 7142 | |
| 7143 | static void RunHolderTest(v8::Handle<v8::ObjectTemplate> obj) { |
| 7144 | ApiTestFuzzer::Fuzz(); |
| 7145 | LocalContext context; |
| 7146 | context->Global()->Set(v8_str("o1"), obj->NewInstance()); |
| 7147 | CompileRun( |
| 7148 | "o1.__proto__ = { };" |
| 7149 | "var o2 = { __proto__: o1 };" |
| 7150 | "var o3 = { __proto__: o2 };" |
| 7151 | "var o4 = { __proto__: o3 };" |
| 7152 | "for (var i = 0; i < 10; i++) o4.p4;" |
| 7153 | "for (var i = 0; i < 10; i++) o3.p3;" |
| 7154 | "for (var i = 0; i < 10; i++) o2.p2;" |
| 7155 | "for (var i = 0; i < 10; i++) o1.p1;"); |
| 7156 | } |
| 7157 | |
| 7158 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7159 | static void PGetter2(Local<String> name, |
| 7160 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7161 | ApiTestFuzzer::Fuzz(); |
| 7162 | p_getter_count2++; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7163 | v8::Handle<v8::Object> global = |
| 7164 | info.GetIsolate()->GetCurrentContext()->Global(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7165 | CHECK_EQ(info.Holder(), global->Get(v8_str("o1"))); |
| 7166 | if (name->Equals(v8_str("p1"))) { |
| 7167 | CHECK_EQ(info.This(), global->Get(v8_str("o1"))); |
| 7168 | } else if (name->Equals(v8_str("p2"))) { |
| 7169 | CHECK_EQ(info.This(), global->Get(v8_str("o2"))); |
| 7170 | } else if (name->Equals(v8_str("p3"))) { |
| 7171 | CHECK_EQ(info.This(), global->Get(v8_str("o3"))); |
| 7172 | } else if (name->Equals(v8_str("p4"))) { |
| 7173 | CHECK_EQ(info.This(), global->Get(v8_str("o4"))); |
| 7174 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7175 | } |
| 7176 | |
| 7177 | |
| 7178 | THREADED_TEST(GetterHolders) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7179 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7180 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 7181 | obj->SetAccessor(v8_str("p1"), PGetter); |
| 7182 | obj->SetAccessor(v8_str("p2"), PGetter); |
| 7183 | obj->SetAccessor(v8_str("p3"), PGetter); |
| 7184 | obj->SetAccessor(v8_str("p4"), PGetter); |
| 7185 | p_getter_count = 0; |
| 7186 | RunHolderTest(obj); |
| 7187 | CHECK_EQ(40, p_getter_count); |
| 7188 | } |
| 7189 | |
| 7190 | |
| 7191 | THREADED_TEST(PreInterceptorHolders) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7192 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7193 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 7194 | obj->SetNamedPropertyHandler(PGetter2); |
| 7195 | p_getter_count2 = 0; |
| 7196 | RunHolderTest(obj); |
| 7197 | CHECK_EQ(40, p_getter_count2); |
| 7198 | } |
| 7199 | |
| 7200 | |
| 7201 | THREADED_TEST(ObjectInstantiation) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 7202 | v8::Isolate* isolate = CcTest::isolate(); |
| 7203 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7204 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 7205 | templ->SetAccessor(v8_str("t"), PGetter2); |
| 7206 | LocalContext context; |
| 7207 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 7208 | for (int i = 0; i < 100; i++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7209 | v8::HandleScope inner_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7210 | v8::Handle<v8::Object> obj = templ->NewInstance(); |
| 7211 | CHECK_NE(obj, context->Global()->Get(v8_str("o"))); |
| 7212 | context->Global()->Set(v8_str("o2"), obj); |
| 7213 | v8::Handle<Value> value = |
| 7214 | Script::Compile(v8_str("o.__proto__ === o2.__proto__"))->Run(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 7215 | CHECK_EQ(v8::True(isolate), value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7216 | context->Global()->Set(v8_str("o"), obj); |
| 7217 | } |
| 7218 | } |
| 7219 | |
| 7220 | |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7221 | static int StrCmp16(uint16_t* a, uint16_t* b) { |
| 7222 | while (true) { |
| 7223 | if (*a == 0 && *b == 0) return 0; |
| 7224 | if (*a != *b) return 0 + *a - *b; |
| 7225 | a++; |
| 7226 | b++; |
| 7227 | } |
| 7228 | } |
| 7229 | |
| 7230 | |
| 7231 | static int StrNCmp16(uint16_t* a, uint16_t* b, int n) { |
| 7232 | while (true) { |
| 7233 | if (n-- == 0) return 0; |
| 7234 | if (*a == 0 && *b == 0) return 0; |
| 7235 | if (*a != *b) return 0 + *a - *b; |
| 7236 | a++; |
| 7237 | b++; |
| 7238 | } |
| 7239 | } |
| 7240 | |
| 7241 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7242 | int GetUtf8Length(Handle<String> str) { |
| 7243 | int len = str->Utf8Length(); |
| 7244 | if (len < 0) { |
| 7245 | i::Handle<i::String> istr(v8::Utils::OpenHandle(*str)); |
| 7246 | i::FlattenString(istr); |
| 7247 | len = str->Utf8Length(); |
| 7248 | } |
| 7249 | return len; |
| 7250 | } |
| 7251 | |
| 7252 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7253 | THREADED_TEST(StringWrite) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7254 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7255 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7256 | v8::Handle<String> str = v8_str("abcde"); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7257 | // abc<Icelandic eth><Unicode snowman>. |
| 7258 | v8::Handle<String> str2 = v8_str("abc\303\260\342\230\203"); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7259 | v8::Handle<String> str3 = v8::String::New("abc\0def", 7); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7260 | const int kStride = 4; // Must match stride in for loops in JS below. |
| 7261 | CompileRun( |
| 7262 | "var left = '';" |
| 7263 | "for (var i = 0; i < 0xd800; i += 4) {" |
| 7264 | " left = left + String.fromCharCode(i);" |
| 7265 | "}"); |
| 7266 | CompileRun( |
| 7267 | "var right = '';" |
| 7268 | "for (var i = 0; i < 0xd800; i += 4) {" |
| 7269 | " right = String.fromCharCode(i) + right;" |
| 7270 | "}"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7271 | v8::Handle<v8::Object> global = context->Global(); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7272 | Handle<String> left_tree = global->Get(v8_str("left")).As<String>(); |
| 7273 | Handle<String> right_tree = global->Get(v8_str("right")).As<String>(); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7274 | |
| 7275 | CHECK_EQ(5, str2->Length()); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7276 | CHECK_EQ(0xd800 / kStride, left_tree->Length()); |
| 7277 | CHECK_EQ(0xd800 / kStride, right_tree->Length()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7278 | |
| 7279 | char buf[100]; |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7280 | char utf8buf[0xd800 * 3]; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7281 | uint16_t wbuf[100]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7282 | int len; |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7283 | int charlen; |
| 7284 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7285 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7286 | len = str2->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7287 | CHECK_EQ(9, len); |
| 7288 | CHECK_EQ(5, charlen); |
| 7289 | CHECK_EQ(0, strcmp(utf8buf, "abc\303\260\342\230\203")); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7290 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7291 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7292 | len = str2->WriteUtf8(utf8buf, 8, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7293 | CHECK_EQ(8, len); |
| 7294 | CHECK_EQ(5, charlen); |
| 7295 | CHECK_EQ(0, strncmp(utf8buf, "abc\303\260\342\230\203\1", 9)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7296 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7297 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7298 | len = str2->WriteUtf8(utf8buf, 7, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7299 | CHECK_EQ(5, len); |
| 7300 | CHECK_EQ(4, charlen); |
| 7301 | CHECK_EQ(0, strncmp(utf8buf, "abc\303\260\1", 5)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7302 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7303 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7304 | len = str2->WriteUtf8(utf8buf, 6, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7305 | CHECK_EQ(5, len); |
| 7306 | CHECK_EQ(4, charlen); |
| 7307 | CHECK_EQ(0, strncmp(utf8buf, "abc\303\260\1", 5)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7308 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7309 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7310 | len = str2->WriteUtf8(utf8buf, 5, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7311 | CHECK_EQ(5, len); |
| 7312 | CHECK_EQ(4, charlen); |
| 7313 | CHECK_EQ(0, strncmp(utf8buf, "abc\303\260\1", 5)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7314 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7315 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7316 | len = str2->WriteUtf8(utf8buf, 4, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7317 | CHECK_EQ(3, len); |
| 7318 | CHECK_EQ(3, charlen); |
| 7319 | CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7320 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7321 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7322 | len = str2->WriteUtf8(utf8buf, 3, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7323 | CHECK_EQ(3, len); |
| 7324 | CHECK_EQ(3, charlen); |
| 7325 | CHECK_EQ(0, strncmp(utf8buf, "abc\1", 4)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7326 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7327 | memset(utf8buf, 0x1, 1000); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7328 | len = str2->WriteUtf8(utf8buf, 2, &charlen); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7329 | CHECK_EQ(2, len); |
| 7330 | CHECK_EQ(2, charlen); |
| 7331 | CHECK_EQ(0, strncmp(utf8buf, "ab\1", 3)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7332 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7333 | memset(utf8buf, 0x1, sizeof(utf8buf)); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7334 | len = GetUtf8Length(left_tree); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7335 | int utf8_expected = |
| 7336 | (0x80 + (0x800 - 0x80) * 2 + (0xd800 - 0x800) * 3) / kStride; |
| 7337 | CHECK_EQ(utf8_expected, len); |
| 7338 | len = left_tree->WriteUtf8(utf8buf, utf8_expected, &charlen); |
| 7339 | CHECK_EQ(utf8_expected, len); |
| 7340 | CHECK_EQ(0xd800 / kStride, charlen); |
| 7341 | CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[utf8_expected - 3])); |
| 7342 | CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[utf8_expected - 2])); |
| 7343 | CHECK_EQ(0xc0 - kStride, |
| 7344 | static_cast<unsigned char>(utf8buf[utf8_expected - 1])); |
| 7345 | CHECK_EQ(1, utf8buf[utf8_expected]); |
| 7346 | |
| 7347 | memset(utf8buf, 0x1, sizeof(utf8buf)); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7348 | len = GetUtf8Length(right_tree); |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 7349 | CHECK_EQ(utf8_expected, len); |
| 7350 | len = right_tree->WriteUtf8(utf8buf, utf8_expected, &charlen); |
| 7351 | CHECK_EQ(utf8_expected, len); |
| 7352 | CHECK_EQ(0xd800 / kStride, charlen); |
| 7353 | CHECK_EQ(0xed, static_cast<unsigned char>(utf8buf[0])); |
| 7354 | CHECK_EQ(0x9f, static_cast<unsigned char>(utf8buf[1])); |
| 7355 | CHECK_EQ(0xc0 - kStride, static_cast<unsigned char>(utf8buf[2])); |
| 7356 | CHECK_EQ(1, utf8buf[utf8_expected]); |
| 7357 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7358 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7359 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7360 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf)); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7361 | CHECK_EQ(5, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7362 | len = str->Write(wbuf); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7363 | CHECK_EQ(5, len); |
| 7364 | CHECK_EQ(0, strcmp("abcde", buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7365 | uint16_t answer1[] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7366 | CHECK_EQ(0, StrCmp16(answer1, wbuf)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7367 | |
| 7368 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7369 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7370 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 0, 4); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7371 | CHECK_EQ(4, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7372 | len = str->Write(wbuf, 0, 4); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7373 | CHECK_EQ(4, len); |
| 7374 | CHECK_EQ(0, strncmp("abcd\1", buf, 5)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7375 | uint16_t answer2[] = {'a', 'b', 'c', 'd', 0x101}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7376 | CHECK_EQ(0, StrNCmp16(answer2, wbuf, 5)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7377 | |
| 7378 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7379 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7380 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 0, 5); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7381 | CHECK_EQ(5, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7382 | len = str->Write(wbuf, 0, 5); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7383 | CHECK_EQ(5, len); |
| 7384 | CHECK_EQ(0, strncmp("abcde\1", buf, 6)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7385 | uint16_t answer3[] = {'a', 'b', 'c', 'd', 'e', 0x101}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7386 | CHECK_EQ(0, StrNCmp16(answer3, wbuf, 6)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7387 | |
| 7388 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7389 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7390 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 0, 6); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7391 | CHECK_EQ(5, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7392 | len = str->Write(wbuf, 0, 6); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7393 | CHECK_EQ(5, len); |
| 7394 | CHECK_EQ(0, strcmp("abcde", buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7395 | uint16_t answer4[] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7396 | CHECK_EQ(0, StrCmp16(answer4, wbuf)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7397 | |
| 7398 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7399 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7400 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 4, -1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7401 | CHECK_EQ(1, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7402 | len = str->Write(wbuf, 4, -1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7403 | CHECK_EQ(1, len); |
| 7404 | CHECK_EQ(0, strcmp("e", buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7405 | uint16_t answer5[] = {'e', '\0'}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7406 | CHECK_EQ(0, StrCmp16(answer5, wbuf)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7407 | |
| 7408 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7409 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7410 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 4, 6); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7411 | CHECK_EQ(1, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7412 | len = str->Write(wbuf, 4, 6); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7413 | CHECK_EQ(1, len); |
| 7414 | CHECK_EQ(0, strcmp("e", buf)); |
| 7415 | CHECK_EQ(0, StrCmp16(answer5, wbuf)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7416 | |
| 7417 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7418 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7419 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 4, 1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7420 | CHECK_EQ(1, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7421 | len = str->Write(wbuf, 4, 1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7422 | CHECK_EQ(1, len); |
| 7423 | CHECK_EQ(0, strncmp("e\1", buf, 2)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7424 | uint16_t answer6[] = {'e', 0x101}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7425 | CHECK_EQ(0, StrNCmp16(answer6, wbuf, 2)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7426 | |
| 7427 | memset(buf, 0x1, sizeof(buf)); |
| 7428 | memset(wbuf, 0x1, sizeof(wbuf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7429 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), 3, 1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7430 | CHECK_EQ(1, len); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7431 | len = str->Write(wbuf, 3, 1); |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7432 | CHECK_EQ(1, len); |
| 7433 | CHECK_EQ(0, strncmp("d\1", buf, 2)); |
ager@chromium.org | 5f0c45f | 2010-12-17 08:51:21 +0000 | [diff] [blame] | 7434 | uint16_t answer7[] = {'d', 0x101}; |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 7435 | CHECK_EQ(0, StrNCmp16(answer7, wbuf, 2)); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7436 | |
| 7437 | memset(wbuf, 0x1, sizeof(wbuf)); |
| 7438 | wbuf[5] = 'X'; |
| 7439 | len = str->Write(wbuf, 0, 6, String::NO_NULL_TERMINATION); |
| 7440 | CHECK_EQ(5, len); |
| 7441 | CHECK_EQ('X', wbuf[5]); |
| 7442 | uint16_t answer8a[] = {'a', 'b', 'c', 'd', 'e'}; |
| 7443 | uint16_t answer8b[] = {'a', 'b', 'c', 'd', 'e', '\0'}; |
| 7444 | CHECK_EQ(0, StrNCmp16(answer8a, wbuf, 5)); |
| 7445 | CHECK_NE(0, StrCmp16(answer8b, wbuf)); |
| 7446 | wbuf[5] = '\0'; |
| 7447 | CHECK_EQ(0, StrCmp16(answer8b, wbuf)); |
| 7448 | |
| 7449 | memset(buf, 0x1, sizeof(buf)); |
| 7450 | buf[5] = 'X'; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7451 | len = str->WriteOneByte(reinterpret_cast<uint8_t*>(buf), |
| 7452 | 0, |
| 7453 | 6, |
| 7454 | String::NO_NULL_TERMINATION); |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 7455 | CHECK_EQ(5, len); |
| 7456 | CHECK_EQ('X', buf[5]); |
| 7457 | CHECK_EQ(0, strncmp("abcde", buf, 5)); |
| 7458 | CHECK_NE(0, strcmp("abcde", buf)); |
| 7459 | buf[5] = '\0'; |
| 7460 | CHECK_EQ(0, strcmp("abcde", buf)); |
| 7461 | |
| 7462 | memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 7463 | utf8buf[8] = 'X'; |
| 7464 | len = str2->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen, |
| 7465 | String::NO_NULL_TERMINATION); |
| 7466 | CHECK_EQ(8, len); |
| 7467 | CHECK_EQ('X', utf8buf[8]); |
| 7468 | CHECK_EQ(5, charlen); |
| 7469 | CHECK_EQ(0, strncmp(utf8buf, "abc\303\260\342\230\203", 8)); |
| 7470 | CHECK_NE(0, strcmp(utf8buf, "abc\303\260\342\230\203")); |
| 7471 | utf8buf[8] = '\0'; |
| 7472 | CHECK_EQ(0, strcmp(utf8buf, "abc\303\260\342\230\203")); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7473 | |
| 7474 | memset(utf8buf, 0x1, sizeof(utf8buf)); |
| 7475 | utf8buf[5] = 'X'; |
| 7476 | len = str->WriteUtf8(utf8buf, sizeof(utf8buf), &charlen, |
| 7477 | String::NO_NULL_TERMINATION); |
| 7478 | CHECK_EQ(5, len); |
| 7479 | CHECK_EQ('X', utf8buf[5]); // Test that the sixth character is untouched. |
| 7480 | CHECK_EQ(5, charlen); |
| 7481 | utf8buf[5] = '\0'; |
| 7482 | CHECK_EQ(0, strcmp(utf8buf, "abcde")); |
| 7483 | |
| 7484 | memset(buf, 0x1, sizeof(buf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7485 | len = str3->WriteOneByte(reinterpret_cast<uint8_t*>(buf)); |
yangguo@chromium.org | 304cc33 | 2012-07-24 07:59:48 +0000 | [diff] [blame] | 7486 | CHECK_EQ(7, len); |
| 7487 | CHECK_EQ(0, strcmp("abc", buf)); |
| 7488 | CHECK_EQ(0, buf[3]); |
| 7489 | CHECK_EQ(0, strcmp("def", buf + 4)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 7490 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7491 | CHECK_EQ(0, str->WriteOneByte(NULL, 0, 0, String::NO_NULL_TERMINATION)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 7492 | CHECK_EQ(0, str->WriteUtf8(NULL, 0, 0, String::NO_NULL_TERMINATION)); |
| 7493 | CHECK_EQ(0, str->Write(NULL, 0, 0, String::NO_NULL_TERMINATION)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7494 | } |
| 7495 | |
| 7496 | |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7497 | static void Utf16Helper( |
| 7498 | LocalContext& context, |
| 7499 | const char* name, |
| 7500 | const char* lengths_name, |
| 7501 | int len) { |
| 7502 | Local<v8::Array> a = |
| 7503 | Local<v8::Array>::Cast(context->Global()->Get(v8_str(name))); |
| 7504 | Local<v8::Array> alens = |
| 7505 | Local<v8::Array>::Cast(context->Global()->Get(v8_str(lengths_name))); |
| 7506 | for (int i = 0; i < len; i++) { |
| 7507 | Local<v8::String> string = |
| 7508 | Local<v8::String>::Cast(a->Get(i)); |
| 7509 | Local<v8::Number> expected_len = |
| 7510 | Local<v8::Number>::Cast(alens->Get(i)); |
| 7511 | int length = GetUtf8Length(string); |
| 7512 | CHECK_EQ(static_cast<int>(expected_len->Value()), length); |
| 7513 | } |
| 7514 | } |
| 7515 | |
| 7516 | |
| 7517 | static uint16_t StringGet(Handle<String> str, int index) { |
| 7518 | i::Handle<i::String> istring = |
| 7519 | v8::Utils::OpenHandle(String::Cast(*str)); |
| 7520 | return istring->Get(index); |
| 7521 | } |
| 7522 | |
| 7523 | |
| 7524 | static void WriteUtf8Helper( |
| 7525 | LocalContext& context, |
| 7526 | const char* name, |
| 7527 | const char* lengths_name, |
| 7528 | int len) { |
| 7529 | Local<v8::Array> b = |
| 7530 | Local<v8::Array>::Cast(context->Global()->Get(v8_str(name))); |
| 7531 | Local<v8::Array> alens = |
| 7532 | Local<v8::Array>::Cast(context->Global()->Get(v8_str(lengths_name))); |
| 7533 | char buffer[1000]; |
| 7534 | char buffer2[1000]; |
| 7535 | for (int i = 0; i < len; i++) { |
| 7536 | Local<v8::String> string = |
| 7537 | Local<v8::String>::Cast(b->Get(i)); |
| 7538 | Local<v8::Number> expected_len = |
| 7539 | Local<v8::Number>::Cast(alens->Get(i)); |
| 7540 | int utf8_length = static_cast<int>(expected_len->Value()); |
| 7541 | for (int j = utf8_length + 1; j >= 0; j--) { |
| 7542 | memset(reinterpret_cast<void*>(&buffer), 42, sizeof(buffer)); |
| 7543 | memset(reinterpret_cast<void*>(&buffer2), 42, sizeof(buffer2)); |
| 7544 | int nchars; |
| 7545 | int utf8_written = |
| 7546 | string->WriteUtf8(buffer, j, &nchars, String::NO_OPTIONS); |
| 7547 | int utf8_written2 = |
| 7548 | string->WriteUtf8(buffer2, j, &nchars, String::NO_NULL_TERMINATION); |
| 7549 | CHECK_GE(utf8_length + 1, utf8_written); |
| 7550 | CHECK_GE(utf8_length, utf8_written2); |
| 7551 | for (int k = 0; k < utf8_written2; k++) { |
| 7552 | CHECK_EQ(buffer[k], buffer2[k]); |
| 7553 | } |
| 7554 | CHECK(nchars * 3 >= utf8_written - 1); |
| 7555 | CHECK(nchars <= utf8_written); |
| 7556 | if (j == utf8_length + 1) { |
| 7557 | CHECK_EQ(utf8_written2, utf8_length); |
| 7558 | CHECK_EQ(utf8_written2 + 1, utf8_written); |
| 7559 | } |
| 7560 | CHECK_EQ(buffer[utf8_written], 42); |
| 7561 | if (j > utf8_length) { |
| 7562 | if (utf8_written != 0) CHECK_EQ(buffer[utf8_written - 1], 0); |
| 7563 | if (utf8_written > 1) CHECK_NE(buffer[utf8_written - 2], 42); |
| 7564 | Handle<String> roundtrip = v8_str(buffer); |
| 7565 | CHECK(roundtrip->Equals(string)); |
| 7566 | } else { |
| 7567 | if (utf8_written != 0) CHECK_NE(buffer[utf8_written - 1], 42); |
| 7568 | } |
| 7569 | if (utf8_written2 != 0) CHECK_NE(buffer[utf8_written - 1], 42); |
| 7570 | if (nchars >= 2) { |
| 7571 | uint16_t trail = StringGet(string, nchars - 1); |
| 7572 | uint16_t lead = StringGet(string, nchars - 2); |
| 7573 | if (((lead & 0xfc00) == 0xd800) && |
| 7574 | ((trail & 0xfc00) == 0xdc00)) { |
| 7575 | unsigned char u1 = buffer2[utf8_written2 - 4]; |
| 7576 | unsigned char u2 = buffer2[utf8_written2 - 3]; |
| 7577 | unsigned char u3 = buffer2[utf8_written2 - 2]; |
| 7578 | unsigned char u4 = buffer2[utf8_written2 - 1]; |
| 7579 | CHECK_EQ((u1 & 0xf8), 0xf0); |
| 7580 | CHECK_EQ((u2 & 0xc0), 0x80); |
| 7581 | CHECK_EQ((u3 & 0xc0), 0x80); |
| 7582 | CHECK_EQ((u4 & 0xc0), 0x80); |
| 7583 | uint32_t c = 0x10000 + ((lead & 0x3ff) << 10) + (trail & 0x3ff); |
| 7584 | CHECK_EQ((u4 & 0x3f), (c & 0x3f)); |
| 7585 | CHECK_EQ((u3 & 0x3f), ((c >> 6) & 0x3f)); |
| 7586 | CHECK_EQ((u2 & 0x3f), ((c >> 12) & 0x3f)); |
| 7587 | CHECK_EQ((u1 & 0x3), c >> 18); |
| 7588 | } |
| 7589 | } |
| 7590 | } |
| 7591 | } |
| 7592 | } |
| 7593 | |
| 7594 | |
| 7595 | THREADED_TEST(Utf16) { |
| 7596 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7597 | v8::HandleScope scope(context->GetIsolate()); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7598 | CompileRun( |
| 7599 | "var pad = '01234567890123456789';" |
| 7600 | "var p = [];" |
| 7601 | "var plens = [20, 3, 3];" |
| 7602 | "p.push('01234567890123456789');" |
| 7603 | "var lead = 0xd800;" |
| 7604 | "var trail = 0xdc00;" |
| 7605 | "p.push(String.fromCharCode(0xd800));" |
| 7606 | "p.push(String.fromCharCode(0xdc00));" |
| 7607 | "var a = [];" |
| 7608 | "var b = [];" |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7609 | "var c = [];" |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7610 | "var alens = [];" |
| 7611 | "for (var i = 0; i < 3; i++) {" |
| 7612 | " p[1] = String.fromCharCode(lead++);" |
| 7613 | " for (var j = 0; j < 3; j++) {" |
| 7614 | " p[2] = String.fromCharCode(trail++);" |
| 7615 | " a.push(p[i] + p[j]);" |
| 7616 | " b.push(p[i] + p[j]);" |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7617 | " c.push(p[i] + p[j]);" |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7618 | " alens.push(plens[i] + plens[j]);" |
| 7619 | " }" |
| 7620 | "}" |
| 7621 | "alens[5] -= 2;" // Here the surrogate pairs match up. |
| 7622 | "var a2 = [];" |
| 7623 | "var b2 = [];" |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7624 | "var c2 = [];" |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7625 | "var a2lens = [];" |
| 7626 | "for (var m = 0; m < 9; m++) {" |
| 7627 | " for (var n = 0; n < 9; n++) {" |
| 7628 | " a2.push(a[m] + a[n]);" |
| 7629 | " b2.push(b[m] + b[n]);" |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7630 | " var newc = 'x' + c[m] + c[n] + 'y';" |
| 7631 | " c2.push(newc.substring(1, newc.length - 1));" |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7632 | " var utf = alens[m] + alens[n];" // And here. |
| 7633 | // The 'n's that start with 0xdc.. are 6-8 |
| 7634 | // The 'm's that end with 0xd8.. are 1, 4 and 7 |
| 7635 | " if ((m % 3) == 1 && n >= 6) utf -= 2;" |
| 7636 | " a2lens.push(utf);" |
| 7637 | " }" |
| 7638 | "}"); |
| 7639 | Utf16Helper(context, "a", "alens", 9); |
| 7640 | Utf16Helper(context, "a2", "a2lens", 81); |
| 7641 | WriteUtf8Helper(context, "b", "alens", 9); |
| 7642 | WriteUtf8Helper(context, "b2", "a2lens", 81); |
danno@chromium.org | 88aa058 | 2012-03-23 15:11:57 +0000 | [diff] [blame] | 7643 | WriteUtf8Helper(context, "c2", "a2lens", 81); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7644 | } |
| 7645 | |
| 7646 | |
| 7647 | static bool SameSymbol(Handle<String> s1, Handle<String> s2) { |
| 7648 | i::Handle<i::String> is1(v8::Utils::OpenHandle(*s1)); |
| 7649 | i::Handle<i::String> is2(v8::Utils::OpenHandle(*s2)); |
| 7650 | return *is1 == *is2; |
| 7651 | } |
| 7652 | |
| 7653 | |
| 7654 | static void SameSymbolHelper(const char* a, const char* b) { |
| 7655 | Handle<String> symbol1 = v8::String::NewSymbol(a); |
| 7656 | Handle<String> symbol2 = v8::String::NewSymbol(b); |
| 7657 | CHECK(SameSymbol(symbol1, symbol2)); |
| 7658 | } |
| 7659 | |
| 7660 | |
| 7661 | THREADED_TEST(Utf16Symbol) { |
| 7662 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7663 | v8::HandleScope scope(context->GetIsolate()); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7664 | |
| 7665 | Handle<String> symbol1 = v8::String::NewSymbol("abc"); |
| 7666 | Handle<String> symbol2 = v8::String::NewSymbol("abc"); |
| 7667 | CHECK(SameSymbol(symbol1, symbol2)); |
| 7668 | |
| 7669 | SameSymbolHelper("\360\220\220\205", // 4 byte encoding. |
| 7670 | "\355\240\201\355\260\205"); // 2 3-byte surrogates. |
| 7671 | SameSymbolHelper("\355\240\201\355\260\206", // 2 3-byte surrogates. |
| 7672 | "\360\220\220\206"); // 4 byte encoding. |
| 7673 | SameSymbolHelper("x\360\220\220\205", // 4 byte encoding. |
| 7674 | "x\355\240\201\355\260\205"); // 2 3-byte surrogates. |
| 7675 | SameSymbolHelper("x\355\240\201\355\260\206", // 2 3-byte surrogates. |
| 7676 | "x\360\220\220\206"); // 4 byte encoding. |
| 7677 | CompileRun( |
| 7678 | "var sym0 = 'benedictus';" |
| 7679 | "var sym0b = 'S\303\270ren';" |
| 7680 | "var sym1 = '\355\240\201\355\260\207';" |
| 7681 | "var sym2 = '\360\220\220\210';" |
| 7682 | "var sym3 = 'x\355\240\201\355\260\207';" |
| 7683 | "var sym4 = 'x\360\220\220\210';" |
| 7684 | "if (sym1.length != 2) throw sym1;" |
| 7685 | "if (sym1.charCodeAt(1) != 0xdc07) throw sym1.charCodeAt(1);" |
| 7686 | "if (sym2.length != 2) throw sym2;" |
| 7687 | "if (sym2.charCodeAt(1) != 0xdc08) throw sym2.charCodeAt(2);" |
| 7688 | "if (sym3.length != 3) throw sym3;" |
| 7689 | "if (sym3.charCodeAt(2) != 0xdc07) throw sym1.charCodeAt(2);" |
| 7690 | "if (sym4.length != 3) throw sym4;" |
| 7691 | "if (sym4.charCodeAt(2) != 0xdc08) throw sym2.charCodeAt(2);"); |
| 7692 | Handle<String> sym0 = v8::String::NewSymbol("benedictus"); |
| 7693 | Handle<String> sym0b = v8::String::NewSymbol("S\303\270ren"); |
| 7694 | Handle<String> sym1 = v8::String::NewSymbol("\355\240\201\355\260\207"); |
| 7695 | Handle<String> sym2 = v8::String::NewSymbol("\360\220\220\210"); |
| 7696 | Handle<String> sym3 = v8::String::NewSymbol("x\355\240\201\355\260\207"); |
| 7697 | Handle<String> sym4 = v8::String::NewSymbol("x\360\220\220\210"); |
| 7698 | v8::Local<v8::Object> global = context->Global(); |
| 7699 | Local<Value> s0 = global->Get(v8_str("sym0")); |
| 7700 | Local<Value> s0b = global->Get(v8_str("sym0b")); |
| 7701 | Local<Value> s1 = global->Get(v8_str("sym1")); |
| 7702 | Local<Value> s2 = global->Get(v8_str("sym2")); |
| 7703 | Local<Value> s3 = global->Get(v8_str("sym3")); |
| 7704 | Local<Value> s4 = global->Get(v8_str("sym4")); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 7705 | CHECK(SameSymbol(sym0, Handle<String>::Cast(s0))); |
| 7706 | CHECK(SameSymbol(sym0b, Handle<String>::Cast(s0b))); |
| 7707 | CHECK(SameSymbol(sym1, Handle<String>::Cast(s1))); |
| 7708 | CHECK(SameSymbol(sym2, Handle<String>::Cast(s2))); |
| 7709 | CHECK(SameSymbol(sym3, Handle<String>::Cast(s3))); |
| 7710 | CHECK(SameSymbol(sym4, Handle<String>::Cast(s4))); |
yangguo@chromium.org | 154ff99 | 2012-03-13 08:09:54 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
| 7713 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7714 | THREADED_TEST(ToArrayIndex) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7715 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7716 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7717 | |
| 7718 | v8::Handle<String> str = v8_str("42"); |
| 7719 | v8::Handle<v8::Uint32> index = str->ToArrayIndex(); |
| 7720 | CHECK(!index.IsEmpty()); |
| 7721 | CHECK_EQ(42.0, index->Uint32Value()); |
| 7722 | str = v8_str("42asdf"); |
| 7723 | index = str->ToArrayIndex(); |
| 7724 | CHECK(index.IsEmpty()); |
| 7725 | str = v8_str("-42"); |
| 7726 | index = str->ToArrayIndex(); |
| 7727 | CHECK(index.IsEmpty()); |
| 7728 | str = v8_str("4294967295"); |
| 7729 | index = str->ToArrayIndex(); |
| 7730 | CHECK(!index.IsEmpty()); |
| 7731 | CHECK_EQ(4294967295.0, index->Uint32Value()); |
| 7732 | v8::Handle<v8::Number> num = v8::Number::New(1); |
| 7733 | index = num->ToArrayIndex(); |
| 7734 | CHECK(!index.IsEmpty()); |
| 7735 | CHECK_EQ(1.0, index->Uint32Value()); |
| 7736 | num = v8::Number::New(-1); |
| 7737 | index = num->ToArrayIndex(); |
| 7738 | CHECK(index.IsEmpty()); |
| 7739 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 7740 | index = obj->ToArrayIndex(); |
| 7741 | CHECK(index.IsEmpty()); |
| 7742 | } |
| 7743 | |
| 7744 | |
| 7745 | THREADED_TEST(ErrorConstruction) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7746 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7747 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7748 | |
| 7749 | v8::Handle<String> foo = v8_str("foo"); |
| 7750 | v8::Handle<String> message = v8_str("message"); |
| 7751 | v8::Handle<Value> range_error = v8::Exception::RangeError(foo); |
| 7752 | CHECK(range_error->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7753 | CHECK(range_error.As<v8::Object>()->Get(message)->Equals(foo)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7754 | v8::Handle<Value> reference_error = v8::Exception::ReferenceError(foo); |
| 7755 | CHECK(reference_error->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7756 | CHECK(reference_error.As<v8::Object>()->Get(message)->Equals(foo)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7757 | v8::Handle<Value> syntax_error = v8::Exception::SyntaxError(foo); |
| 7758 | CHECK(syntax_error->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7759 | CHECK(syntax_error.As<v8::Object>()->Get(message)->Equals(foo)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7760 | v8::Handle<Value> type_error = v8::Exception::TypeError(foo); |
| 7761 | CHECK(type_error->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7762 | CHECK(type_error.As<v8::Object>()->Get(message)->Equals(foo)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7763 | v8::Handle<Value> error = v8::Exception::Error(foo); |
| 7764 | CHECK(error->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 7765 | CHECK(error.As<v8::Object>()->Get(message)->Equals(foo)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7766 | } |
| 7767 | |
| 7768 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7769 | static void YGetter(Local<String> name, |
| 7770 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7771 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7772 | info.GetReturnValue().Set(v8_num(10)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7773 | } |
| 7774 | |
| 7775 | |
| 7776 | static void YSetter(Local<String> name, |
| 7777 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7778 | const v8::PropertyCallbackInfo<void>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7779 | if (info.This()->Has(name)) { |
| 7780 | info.This()->Delete(name); |
| 7781 | } |
| 7782 | info.This()->Set(name, value); |
| 7783 | } |
| 7784 | |
| 7785 | |
| 7786 | THREADED_TEST(DeleteAccessor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7787 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7788 | v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 7789 | obj->SetAccessor(v8_str("y"), YGetter, YSetter); |
| 7790 | LocalContext context; |
| 7791 | v8::Handle<v8::Object> holder = obj->NewInstance(); |
| 7792 | context->Global()->Set(v8_str("holder"), holder); |
| 7793 | v8::Handle<Value> result = CompileRun( |
| 7794 | "holder.y = 11; holder.y = 12; holder.y"); |
| 7795 | CHECK_EQ(12, result->Uint32Value()); |
| 7796 | } |
| 7797 | |
| 7798 | |
| 7799 | THREADED_TEST(TypeSwitch) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7800 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7801 | v8::Handle<v8::FunctionTemplate> templ1 = v8::FunctionTemplate::New(); |
| 7802 | v8::Handle<v8::FunctionTemplate> templ2 = v8::FunctionTemplate::New(); |
| 7803 | v8::Handle<v8::FunctionTemplate> templ3 = v8::FunctionTemplate::New(); |
| 7804 | v8::Handle<v8::FunctionTemplate> templs[3] = { templ1, templ2, templ3 }; |
| 7805 | v8::Handle<v8::TypeSwitch> type_switch = v8::TypeSwitch::New(3, templs); |
| 7806 | LocalContext context; |
| 7807 | v8::Handle<v8::Object> obj0 = v8::Object::New(); |
| 7808 | v8::Handle<v8::Object> obj1 = templ1->GetFunction()->NewInstance(); |
| 7809 | v8::Handle<v8::Object> obj2 = templ2->GetFunction()->NewInstance(); |
| 7810 | v8::Handle<v8::Object> obj3 = templ3->GetFunction()->NewInstance(); |
| 7811 | for (int i = 0; i < 10; i++) { |
| 7812 | CHECK_EQ(0, type_switch->match(obj0)); |
| 7813 | CHECK_EQ(1, type_switch->match(obj1)); |
| 7814 | CHECK_EQ(2, type_switch->match(obj2)); |
| 7815 | CHECK_EQ(3, type_switch->match(obj3)); |
| 7816 | CHECK_EQ(3, type_switch->match(obj3)); |
| 7817 | CHECK_EQ(2, type_switch->match(obj2)); |
| 7818 | CHECK_EQ(1, type_switch->match(obj1)); |
| 7819 | CHECK_EQ(0, type_switch->match(obj0)); |
| 7820 | } |
| 7821 | } |
| 7822 | |
| 7823 | |
| 7824 | // For use within the TestSecurityHandler() test. |
| 7825 | static bool g_security_callback_result = false; |
| 7826 | static bool NamedSecurityTestCallback(Local<v8::Object> global, |
| 7827 | Local<Value> name, |
| 7828 | v8::AccessType type, |
| 7829 | Local<Value> data) { |
| 7830 | // Always allow read access. |
| 7831 | if (type == v8::ACCESS_GET) |
| 7832 | return true; |
| 7833 | |
| 7834 | // Sometimes allow other access. |
| 7835 | return g_security_callback_result; |
| 7836 | } |
| 7837 | |
| 7838 | |
| 7839 | static bool IndexedSecurityTestCallback(Local<v8::Object> global, |
| 7840 | uint32_t key, |
| 7841 | v8::AccessType type, |
| 7842 | Local<Value> data) { |
| 7843 | // Always allow read access. |
| 7844 | if (type == v8::ACCESS_GET) |
| 7845 | return true; |
| 7846 | |
| 7847 | // Sometimes allow other access. |
| 7848 | return g_security_callback_result; |
| 7849 | } |
| 7850 | |
| 7851 | |
| 7852 | static int trouble_nesting = 0; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7853 | static void TroubleCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7854 | ApiTestFuzzer::Fuzz(); |
| 7855 | trouble_nesting++; |
| 7856 | |
| 7857 | // Call a JS function that throws an uncaught exception. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7858 | Local<v8::Object> arg_this = |
| 7859 | args.GetIsolate()->GetCurrentContext()->Global(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7860 | Local<Value> trouble_callee = (trouble_nesting == 3) ? |
| 7861 | arg_this->Get(v8_str("trouble_callee")) : |
| 7862 | arg_this->Get(v8_str("trouble_caller")); |
| 7863 | CHECK(trouble_callee->IsFunction()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 7864 | args.GetReturnValue().Set( |
| 7865 | Function::Cast(*trouble_callee)->Call(arg_this, 0, NULL)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7866 | } |
| 7867 | |
| 7868 | |
| 7869 | static int report_count = 0; |
| 7870 | static void ApiUncaughtExceptionTestListener(v8::Handle<v8::Message>, |
| 7871 | v8::Handle<Value>) { |
| 7872 | report_count++; |
| 7873 | } |
| 7874 | |
| 7875 | |
| 7876 | // Counts uncaught exceptions, but other tests running in parallel |
| 7877 | // also have uncaught exceptions. |
| 7878 | TEST(ApiUncaughtException) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7879 | report_count = 0; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7880 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7881 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7882 | v8::V8::AddMessageListener(ApiUncaughtExceptionTestListener); |
| 7883 | |
| 7884 | Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(TroubleCallback); |
| 7885 | v8::Local<v8::Object> global = env->Global(); |
| 7886 | global->Set(v8_str("trouble"), fun->GetFunction()); |
| 7887 | |
| 7888 | Script::Compile(v8_str("function trouble_callee() {" |
| 7889 | " var x = null;" |
| 7890 | " return x.foo;" |
| 7891 | "};" |
| 7892 | "function trouble_caller() {" |
| 7893 | " trouble();" |
| 7894 | "};"))->Run(); |
| 7895 | Local<Value> trouble = global->Get(v8_str("trouble")); |
| 7896 | CHECK(trouble->IsFunction()); |
| 7897 | Local<Value> trouble_callee = global->Get(v8_str("trouble_callee")); |
| 7898 | CHECK(trouble_callee->IsFunction()); |
| 7899 | Local<Value> trouble_caller = global->Get(v8_str("trouble_caller")); |
| 7900 | CHECK(trouble_caller->IsFunction()); |
| 7901 | Function::Cast(*trouble_caller)->Call(global, 0, NULL); |
| 7902 | CHECK_EQ(1, report_count); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7903 | v8::V8::RemoveMessageListeners(ApiUncaughtExceptionTestListener); |
| 7904 | } |
| 7905 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7906 | static const char* script_resource_name = "ExceptionInNativeScript.js"; |
| 7907 | static void ExceptionInNativeScriptTestListener(v8::Handle<v8::Message> message, |
| 7908 | v8::Handle<Value>) { |
| 7909 | v8::Handle<v8::Value> name_val = message->GetScriptResourceName(); |
| 7910 | CHECK(!name_val.IsEmpty() && name_val->IsString()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 7911 | v8::String::Utf8Value name(message->GetScriptResourceName()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7912 | CHECK_EQ(script_resource_name, *name); |
| 7913 | CHECK_EQ(3, message->GetLineNumber()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 7914 | v8::String::Utf8Value source_line(message->GetSourceLine()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7915 | CHECK_EQ(" new o.foo();", *source_line); |
| 7916 | } |
| 7917 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 7918 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7919 | TEST(ExceptionInNativeScript) { |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7920 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7921 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 7922 | v8::V8::AddMessageListener(ExceptionInNativeScriptTestListener); |
| 7923 | |
| 7924 | Local<v8::FunctionTemplate> fun = v8::FunctionTemplate::New(TroubleCallback); |
| 7925 | v8::Local<v8::Object> global = env->Global(); |
| 7926 | global->Set(v8_str("trouble"), fun->GetFunction()); |
| 7927 | |
| 7928 | Script::Compile(v8_str("function trouble() {\n" |
| 7929 | " var o = {};\n" |
| 7930 | " new o.foo();\n" |
| 7931 | "};"), v8::String::New(script_resource_name))->Run(); |
| 7932 | Local<Value> trouble = global->Get(v8_str("trouble")); |
| 7933 | CHECK(trouble->IsFunction()); |
| 7934 | Function::Cast(*trouble)->Call(global, 0, NULL); |
| 7935 | v8::V8::RemoveMessageListeners(ExceptionInNativeScriptTestListener); |
| 7936 | } |
| 7937 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7938 | |
| 7939 | TEST(CompilationErrorUsingTryCatchHandler) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7940 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7941 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7942 | v8::TryCatch try_catch; |
| 7943 | Script::Compile(v8_str("This doesn't &*&@#$&*^ compile.")); |
| 7944 | CHECK_NE(NULL, *try_catch.Exception()); |
| 7945 | CHECK(try_catch.HasCaught()); |
| 7946 | } |
| 7947 | |
| 7948 | |
| 7949 | TEST(TryCatchFinallyUsingTryCatchHandler) { |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7950 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 7951 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 7952 | v8::TryCatch try_catch; |
| 7953 | Script::Compile(v8_str("try { throw ''; } catch (e) {}"))->Run(); |
| 7954 | CHECK(!try_catch.HasCaught()); |
| 7955 | Script::Compile(v8_str("try { throw ''; } finally {}"))->Run(); |
| 7956 | CHECK(try_catch.HasCaught()); |
| 7957 | try_catch.Reset(); |
| 7958 | Script::Compile(v8_str("(function() {" |
| 7959 | "try { throw ''; } finally { return; }" |
| 7960 | "})()"))->Run(); |
| 7961 | CHECK(!try_catch.HasCaught()); |
| 7962 | Script::Compile(v8_str("(function()" |
| 7963 | " { try { throw ''; } finally { throw 0; }" |
| 7964 | "})()"))->Run(); |
| 7965 | CHECK(try_catch.HasCaught()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
| 7968 | |
| 7969 | // SecurityHandler can't be run twice |
| 7970 | TEST(SecurityHandler) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7971 | v8::HandleScope scope0(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7972 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 7973 | global_template->SetAccessCheckCallbacks(NamedSecurityTestCallback, |
| 7974 | IndexedSecurityTestCallback); |
| 7975 | // Create an environment |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7976 | v8::Handle<Context> context0 = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7977 | Context::New(CcTest::isolate(), NULL, global_template); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7978 | context0->Enter(); |
| 7979 | |
| 7980 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 7981 | v8::Handle<Script> script0 = v8_compile("foo = 111"); |
| 7982 | script0->Run(); |
| 7983 | global0->Set(v8_str("0"), v8_num(999)); |
| 7984 | v8::Handle<Value> foo0 = global0->Get(v8_str("foo")); |
| 7985 | CHECK_EQ(111, foo0->Int32Value()); |
| 7986 | v8::Handle<Value> z0 = global0->Get(v8_str("0")); |
| 7987 | CHECK_EQ(999, z0->Int32Value()); |
| 7988 | |
| 7989 | // Create another environment, should fail security checks. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7990 | v8::HandleScope scope1(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7991 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 7992 | v8::Handle<Context> context1 = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 7993 | Context::New(CcTest::isolate(), NULL, global_template); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 7994 | context1->Enter(); |
| 7995 | |
| 7996 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 7997 | global1->Set(v8_str("othercontext"), global0); |
| 7998 | // This set will fail the security check. |
| 7999 | v8::Handle<Script> script1 = |
| 8000 | v8_compile("othercontext.foo = 222; othercontext[0] = 888;"); |
| 8001 | script1->Run(); |
| 8002 | // This read will pass the security check. |
| 8003 | v8::Handle<Value> foo1 = global0->Get(v8_str("foo")); |
| 8004 | CHECK_EQ(111, foo1->Int32Value()); |
| 8005 | // This read will pass the security check. |
| 8006 | v8::Handle<Value> z1 = global0->Get(v8_str("0")); |
| 8007 | CHECK_EQ(999, z1->Int32Value()); |
| 8008 | |
| 8009 | // Create another environment, should pass security checks. |
| 8010 | { g_security_callback_result = true; // allow security handler to pass. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8011 | v8::HandleScope scope2(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8012 | LocalContext context2; |
| 8013 | v8::Handle<v8::Object> global2 = context2->Global(); |
| 8014 | global2->Set(v8_str("othercontext"), global0); |
| 8015 | v8::Handle<Script> script2 = |
| 8016 | v8_compile("othercontext.foo = 333; othercontext[0] = 888;"); |
| 8017 | script2->Run(); |
| 8018 | v8::Handle<Value> foo2 = global0->Get(v8_str("foo")); |
| 8019 | CHECK_EQ(333, foo2->Int32Value()); |
| 8020 | v8::Handle<Value> z2 = global0->Get(v8_str("0")); |
| 8021 | CHECK_EQ(888, z2->Int32Value()); |
| 8022 | } |
| 8023 | |
| 8024 | context1->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8025 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8026 | } |
| 8027 | |
| 8028 | |
| 8029 | THREADED_TEST(SecurityChecks) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8030 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8031 | v8::HandleScope handle_scope(env1->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8032 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8033 | |
| 8034 | Local<Value> foo = v8_str("foo"); |
| 8035 | Local<Value> bar = v8_str("bar"); |
| 8036 | |
| 8037 | // Set to the same domain. |
| 8038 | env1->SetSecurityToken(foo); |
| 8039 | |
| 8040 | // Create a function in env1. |
| 8041 | Script::Compile(v8_str("spy=function(){return spy;}"))->Run(); |
| 8042 | Local<Value> spy = env1->Global()->Get(v8_str("spy")); |
| 8043 | CHECK(spy->IsFunction()); |
| 8044 | |
| 8045 | // Create another function accessing global objects. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8046 | Script::Compile(v8_str("spy2=function(){return new this.Array();}"))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8047 | Local<Value> spy2 = env1->Global()->Get(v8_str("spy2")); |
| 8048 | CHECK(spy2->IsFunction()); |
| 8049 | |
| 8050 | // Switch to env2 in the same domain and invoke spy on env2. |
| 8051 | { |
| 8052 | env2->SetSecurityToken(foo); |
| 8053 | // Enter env2 |
| 8054 | Context::Scope scope_env2(env2); |
| 8055 | Local<Value> result = Function::Cast(*spy)->Call(env2->Global(), 0, NULL); |
| 8056 | CHECK(result->IsFunction()); |
| 8057 | } |
| 8058 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8059 | { |
| 8060 | env2->SetSecurityToken(bar); |
| 8061 | Context::Scope scope_env2(env2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8062 | |
| 8063 | // Call cross_domain_call, it should throw an exception |
| 8064 | v8::TryCatch try_catch; |
| 8065 | Function::Cast(*spy2)->Call(env2->Global(), 0, NULL); |
| 8066 | CHECK(try_catch.HasCaught()); |
| 8067 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8068 | } |
| 8069 | |
| 8070 | |
| 8071 | // Regression test case for issue 1183439. |
| 8072 | THREADED_TEST(SecurityChecksForPrototypeChain) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8073 | LocalContext current; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8074 | v8::HandleScope scope(current->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8075 | v8::Handle<Context> other = Context::New(current->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8076 | |
| 8077 | // Change context to be able to get to the Object function in the |
| 8078 | // other context without hitting the security checks. |
| 8079 | v8::Local<Value> other_object; |
| 8080 | { Context::Scope scope(other); |
| 8081 | other_object = other->Global()->Get(v8_str("Object")); |
| 8082 | other->Global()->Set(v8_num(42), v8_num(87)); |
| 8083 | } |
| 8084 | |
| 8085 | current->Global()->Set(v8_str("other"), other->Global()); |
| 8086 | CHECK(v8_compile("other")->Run()->Equals(other->Global())); |
| 8087 | |
| 8088 | // Make sure the security check fails here and we get an undefined |
| 8089 | // result instead of getting the Object function. Repeat in a loop |
| 8090 | // to make sure to exercise the IC code. |
| 8091 | v8::Local<Script> access_other0 = v8_compile("other.Object"); |
| 8092 | v8::Local<Script> access_other1 = v8_compile("other[42]"); |
| 8093 | for (int i = 0; i < 5; i++) { |
| 8094 | CHECK(!access_other0->Run()->Equals(other_object)); |
| 8095 | CHECK(access_other0->Run()->IsUndefined()); |
| 8096 | CHECK(!access_other1->Run()->Equals(v8_num(87))); |
| 8097 | CHECK(access_other1->Run()->IsUndefined()); |
| 8098 | } |
| 8099 | |
| 8100 | // Create an object that has 'other' in its prototype chain and make |
| 8101 | // sure we cannot access the Object function indirectly through |
| 8102 | // that. Repeat in a loop to make sure to exercise the IC code. |
| 8103 | v8_compile("function F() { };" |
| 8104 | "F.prototype = other;" |
| 8105 | "var f = new F();")->Run(); |
| 8106 | v8::Local<Script> access_f0 = v8_compile("f.Object"); |
| 8107 | v8::Local<Script> access_f1 = v8_compile("f[42]"); |
| 8108 | for (int j = 0; j < 5; j++) { |
| 8109 | CHECK(!access_f0->Run()->Equals(other_object)); |
| 8110 | CHECK(access_f0->Run()->IsUndefined()); |
| 8111 | CHECK(!access_f1->Run()->Equals(v8_num(87))); |
| 8112 | CHECK(access_f1->Run()->IsUndefined()); |
| 8113 | } |
| 8114 | |
| 8115 | // Now it gets hairy: Set the prototype for the other global object |
| 8116 | // to be the current global object. The prototype chain for 'f' now |
| 8117 | // goes through 'other' but ends up in the current global object. |
| 8118 | { Context::Scope scope(other); |
| 8119 | other->Global()->Set(v8_str("__proto__"), current->Global()); |
| 8120 | } |
| 8121 | // Set a named and an index property on the current global |
| 8122 | // object. To force the lookup to go through the other global object, |
| 8123 | // the properties must not exist in the other global object. |
| 8124 | current->Global()->Set(v8_str("foo"), v8_num(100)); |
| 8125 | current->Global()->Set(v8_num(99), v8_num(101)); |
| 8126 | // Try to read the properties from f and make sure that the access |
| 8127 | // gets stopped by the security checks on the other global object. |
| 8128 | Local<Script> access_f2 = v8_compile("f.foo"); |
| 8129 | Local<Script> access_f3 = v8_compile("f[99]"); |
| 8130 | for (int k = 0; k < 5; k++) { |
| 8131 | CHECK(!access_f2->Run()->Equals(v8_num(100))); |
| 8132 | CHECK(access_f2->Run()->IsUndefined()); |
| 8133 | CHECK(!access_f3->Run()->Equals(v8_num(101))); |
| 8134 | CHECK(access_f3->Run()->IsUndefined()); |
| 8135 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
| 8138 | |
| 8139 | THREADED_TEST(CrossDomainDelete) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8140 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8141 | v8::HandleScope handle_scope(env1->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8142 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8143 | |
| 8144 | Local<Value> foo = v8_str("foo"); |
| 8145 | Local<Value> bar = v8_str("bar"); |
| 8146 | |
| 8147 | // Set to the same domain. |
| 8148 | env1->SetSecurityToken(foo); |
| 8149 | env2->SetSecurityToken(foo); |
| 8150 | |
| 8151 | env1->Global()->Set(v8_str("prop"), v8_num(3)); |
| 8152 | env2->Global()->Set(v8_str("env1"), env1->Global()); |
| 8153 | |
| 8154 | // Change env2 to a different domain and delete env1.prop. |
| 8155 | env2->SetSecurityToken(bar); |
| 8156 | { |
| 8157 | Context::Scope scope_env2(env2); |
| 8158 | Local<Value> result = |
| 8159 | Script::Compile(v8_str("delete env1.prop"))->Run(); |
| 8160 | CHECK(result->IsFalse()); |
| 8161 | } |
| 8162 | |
| 8163 | // Check that env1.prop still exists. |
| 8164 | Local<Value> v = env1->Global()->Get(v8_str("prop")); |
| 8165 | CHECK(v->IsNumber()); |
| 8166 | CHECK_EQ(3, v->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8167 | } |
| 8168 | |
| 8169 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8170 | THREADED_TEST(CrossDomainIsPropertyEnumerable) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8171 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8172 | v8::HandleScope handle_scope(env1->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8173 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8174 | |
| 8175 | Local<Value> foo = v8_str("foo"); |
| 8176 | Local<Value> bar = v8_str("bar"); |
| 8177 | |
| 8178 | // Set to the same domain. |
| 8179 | env1->SetSecurityToken(foo); |
| 8180 | env2->SetSecurityToken(foo); |
| 8181 | |
| 8182 | env1->Global()->Set(v8_str("prop"), v8_num(3)); |
| 8183 | env2->Global()->Set(v8_str("env1"), env1->Global()); |
| 8184 | |
| 8185 | // env1.prop is enumerable in env2. |
| 8186 | Local<String> test = v8_str("propertyIsEnumerable.call(env1, 'prop')"); |
| 8187 | { |
| 8188 | Context::Scope scope_env2(env2); |
| 8189 | Local<Value> result = Script::Compile(test)->Run(); |
| 8190 | CHECK(result->IsTrue()); |
| 8191 | } |
| 8192 | |
| 8193 | // Change env2 to a different domain and test again. |
| 8194 | env2->SetSecurityToken(bar); |
| 8195 | { |
| 8196 | Context::Scope scope_env2(env2); |
| 8197 | Local<Value> result = Script::Compile(test)->Run(); |
| 8198 | CHECK(result->IsFalse()); |
| 8199 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8200 | } |
| 8201 | |
| 8202 | |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8203 | THREADED_TEST(CrossDomainForIn) { |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8204 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8205 | v8::HandleScope handle_scope(env1->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8206 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8207 | |
| 8208 | Local<Value> foo = v8_str("foo"); |
| 8209 | Local<Value> bar = v8_str("bar"); |
| 8210 | |
| 8211 | // Set to the same domain. |
| 8212 | env1->SetSecurityToken(foo); |
| 8213 | env2->SetSecurityToken(foo); |
| 8214 | |
| 8215 | env1->Global()->Set(v8_str("prop"), v8_num(3)); |
| 8216 | env2->Global()->Set(v8_str("env1"), env1->Global()); |
| 8217 | |
| 8218 | // Change env2 to a different domain and set env1's global object |
| 8219 | // as the __proto__ of an object in env2 and enumerate properties |
| 8220 | // in for-in. It shouldn't enumerate properties on env1's global |
| 8221 | // object. |
| 8222 | env2->SetSecurityToken(bar); |
| 8223 | { |
| 8224 | Context::Scope scope_env2(env2); |
| 8225 | Local<Value> result = |
| 8226 | CompileRun("(function(){var obj = {'__proto__':env1};" |
| 8227 | "for (var p in obj)" |
| 8228 | " if (p == 'prop') return false;" |
| 8229 | "return true;})()"); |
| 8230 | CHECK(result->IsTrue()); |
| 8231 | } |
ager@chromium.org | 236ad96 | 2008-09-25 09:45:57 +0000 | [diff] [blame] | 8232 | } |
| 8233 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8234 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8235 | TEST(ContextDetachGlobal) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8236 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8237 | v8::HandleScope handle_scope(env1->GetIsolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8238 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8239 | |
| 8240 | Local<v8::Object> global1 = env1->Global(); |
| 8241 | |
| 8242 | Local<Value> foo = v8_str("foo"); |
| 8243 | |
| 8244 | // Set to the same domain. |
| 8245 | env1->SetSecurityToken(foo); |
| 8246 | env2->SetSecurityToken(foo); |
| 8247 | |
| 8248 | // Enter env2 |
| 8249 | env2->Enter(); |
| 8250 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8251 | // Create a function in env2 and add a reference to it in env1. |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8252 | Local<v8::Object> global2 = env2->Global(); |
| 8253 | global2->Set(v8_str("prop"), v8::Integer::New(1)); |
| 8254 | CompileRun("function getProp() {return prop;}"); |
| 8255 | |
| 8256 | env1->Global()->Set(v8_str("getProp"), |
| 8257 | global2->Get(v8_str("getProp"))); |
| 8258 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8259 | // Detach env2's global, and reuse the global object of env2 |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8260 | env2->Exit(); |
| 8261 | env2->DetachGlobal(); |
| 8262 | // env2 has a new global object. |
| 8263 | CHECK(!env2->Global()->Equals(global2)); |
| 8264 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8265 | v8::Handle<Context> env3 = Context::New(env1->GetIsolate(), |
| 8266 | 0, |
| 8267 | v8::Handle<v8::ObjectTemplate>(), |
| 8268 | global2); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8269 | env3->SetSecurityToken(v8_str("bar")); |
| 8270 | env3->Enter(); |
| 8271 | |
| 8272 | Local<v8::Object> global3 = env3->Global(); |
| 8273 | CHECK_EQ(global2, global3); |
| 8274 | CHECK(global3->Get(v8_str("prop"))->IsUndefined()); |
| 8275 | CHECK(global3->Get(v8_str("getProp"))->IsUndefined()); |
| 8276 | global3->Set(v8_str("prop"), v8::Integer::New(-1)); |
| 8277 | global3->Set(v8_str("prop2"), v8::Integer::New(2)); |
| 8278 | env3->Exit(); |
| 8279 | |
| 8280 | // Call getProp in env1, and it should return the value 1 |
| 8281 | { |
| 8282 | Local<Value> get_prop = global1->Get(v8_str("getProp")); |
| 8283 | CHECK(get_prop->IsFunction()); |
| 8284 | v8::TryCatch try_catch; |
| 8285 | Local<Value> r = Function::Cast(*get_prop)->Call(global1, 0, NULL); |
| 8286 | CHECK(!try_catch.HasCaught()); |
| 8287 | CHECK_EQ(1, r->Int32Value()); |
| 8288 | } |
| 8289 | |
| 8290 | // Check that env3 is not accessible from env1 |
| 8291 | { |
| 8292 | Local<Value> r = global3->Get(v8_str("prop2")); |
| 8293 | CHECK(r->IsUndefined()); |
| 8294 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 8295 | } |
| 8296 | |
| 8297 | |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8298 | TEST(DetachAndReattachGlobal) { |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8299 | LocalContext env1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 8300 | v8::HandleScope scope(env1->GetIsolate()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8301 | |
| 8302 | // Create second environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8303 | v8::Handle<Context> env2 = Context::New(env1->GetIsolate()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8304 | |
| 8305 | Local<Value> foo = v8_str("foo"); |
| 8306 | |
| 8307 | // Set same security token for env1 and env2. |
| 8308 | env1->SetSecurityToken(foo); |
| 8309 | env2->SetSecurityToken(foo); |
| 8310 | |
| 8311 | // Create a property on the global object in env2. |
| 8312 | { |
| 8313 | v8::Context::Scope scope(env2); |
| 8314 | env2->Global()->Set(v8_str("p"), v8::Integer::New(42)); |
| 8315 | } |
| 8316 | |
| 8317 | // Create a reference to env2 global from env1 global. |
| 8318 | env1->Global()->Set(v8_str("other"), env2->Global()); |
| 8319 | |
| 8320 | // Check that we have access to other.p in env2 from env1. |
| 8321 | Local<Value> result = CompileRun("other.p"); |
| 8322 | CHECK(result->IsInt32()); |
| 8323 | CHECK_EQ(42, result->Int32Value()); |
| 8324 | |
| 8325 | // Hold on to global from env2 and detach global from env2. |
| 8326 | Local<v8::Object> global2 = env2->Global(); |
| 8327 | env2->DetachGlobal(); |
| 8328 | |
| 8329 | // Check that the global has been detached. No other.p property can |
| 8330 | // be found. |
| 8331 | result = CompileRun("other.p"); |
| 8332 | CHECK(result->IsUndefined()); |
| 8333 | |
| 8334 | // Reuse global2 for env3. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8335 | v8::Handle<Context> env3 = Context::New(env1->GetIsolate(), |
| 8336 | 0, |
| 8337 | v8::Handle<v8::ObjectTemplate>(), |
| 8338 | global2); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8339 | CHECK_EQ(global2, env3->Global()); |
| 8340 | |
| 8341 | // Start by using the same security token for env3 as for env1 and env2. |
| 8342 | env3->SetSecurityToken(foo); |
| 8343 | |
| 8344 | // Create a property on the global object in env3. |
| 8345 | { |
| 8346 | v8::Context::Scope scope(env3); |
| 8347 | env3->Global()->Set(v8_str("p"), v8::Integer::New(24)); |
| 8348 | } |
| 8349 | |
| 8350 | // Check that other.p is now the property in env3 and that we have access. |
| 8351 | result = CompileRun("other.p"); |
| 8352 | CHECK(result->IsInt32()); |
| 8353 | CHECK_EQ(24, result->Int32Value()); |
| 8354 | |
| 8355 | // Change security token for env3 to something different from env1 and env2. |
| 8356 | env3->SetSecurityToken(v8_str("bar")); |
| 8357 | |
| 8358 | // Check that we do not have access to other.p in env1. |other| is now |
| 8359 | // the global object for env3 which has a different security token, |
| 8360 | // so access should be blocked. |
| 8361 | result = CompileRun("other.p"); |
| 8362 | CHECK(result->IsUndefined()); |
| 8363 | |
| 8364 | // Detach the global for env3 and reattach it to env2. |
| 8365 | env3->DetachGlobal(); |
| 8366 | env2->ReattachGlobal(global2); |
| 8367 | |
| 8368 | // Check that we have access to other.p again in env1. |other| is now |
| 8369 | // the global object for env2 which has the same security token as env1. |
| 8370 | result = CompileRun("other.p"); |
| 8371 | CHECK(result->IsInt32()); |
| 8372 | CHECK_EQ(42, result->Int32Value()); |
sgjesse@chromium.org | df7a284 | 2010-03-25 14:34:15 +0000 | [diff] [blame] | 8373 | } |
| 8374 | |
| 8375 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8376 | static bool allowed_access_type[v8::ACCESS_KEYS + 1] = { false }; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8377 | static bool NamedAccessBlocker(Local<v8::Object> global, |
| 8378 | Local<Value> name, |
| 8379 | v8::AccessType type, |
| 8380 | Local<Value> data) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8381 | return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) || |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8382 | allowed_access_type[type]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8383 | } |
| 8384 | |
| 8385 | |
| 8386 | static bool IndexedAccessBlocker(Local<v8::Object> global, |
| 8387 | uint32_t key, |
| 8388 | v8::AccessType type, |
| 8389 | Local<Value> data) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8390 | return CcTest::isolate()->GetCurrentContext()->Global()->Equals(global) || |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8391 | allowed_access_type[type]; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8392 | } |
| 8393 | |
| 8394 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8395 | static int g_echo_value_1 = -1; |
| 8396 | static int g_echo_value_2 = -1; |
| 8397 | |
| 8398 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8399 | static void EchoGetter( |
| 8400 | Local<String> name, |
| 8401 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8402 | info.GetReturnValue().Set(v8_num(g_echo_value_1)); |
| 8403 | } |
| 8404 | |
| 8405 | |
| 8406 | static void EchoGetter(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 8407 | info.GetReturnValue().Set(v8_num(g_echo_value_2)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8408 | } |
| 8409 | |
| 8410 | |
| 8411 | static void EchoSetter(Local<String> name, |
| 8412 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8413 | const v8::PropertyCallbackInfo<void>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8414 | if (value->IsNumber()) |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8415 | g_echo_value_1 = value->Int32Value(); |
| 8416 | } |
| 8417 | |
| 8418 | |
| 8419 | static void EchoSetter(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 8420 | v8::Handle<v8::Value> value = info[0]; |
| 8421 | if (value->IsNumber()) |
| 8422 | g_echo_value_2 = value->Int32Value(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8423 | } |
| 8424 | |
| 8425 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8426 | static void UnreachableGetter( |
| 8427 | Local<String> name, |
| 8428 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8429 | CHECK(false); // This function should not be called.. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8430 | } |
| 8431 | |
| 8432 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8433 | static void UnreachableSetter(Local<String>, |
| 8434 | Local<Value>, |
| 8435 | const v8::PropertyCallbackInfo<void>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8436 | CHECK(false); // This function should nto be called. |
| 8437 | } |
| 8438 | |
| 8439 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8440 | static void UnreachableFunction( |
| 8441 | const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 8442 | CHECK(false); // This function should not be called.. |
| 8443 | } |
| 8444 | |
| 8445 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8446 | TEST(AccessControl) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8447 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8448 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8449 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 8450 | |
| 8451 | global_template->SetAccessCheckCallbacks(NamedAccessBlocker, |
| 8452 | IndexedAccessBlocker); |
| 8453 | |
| 8454 | // Add an accessor accessible by cross-domain JS code. |
| 8455 | global_template->SetAccessor( |
| 8456 | v8_str("accessible_prop"), |
| 8457 | EchoGetter, EchoSetter, |
| 8458 | v8::Handle<Value>(), |
| 8459 | v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
| 8460 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8461 | |
| 8462 | global_template->SetAccessorProperty( |
| 8463 | v8_str("accessible_js_prop"), |
| 8464 | v8::FunctionTemplate::New(EchoGetter), |
| 8465 | v8::FunctionTemplate::New(EchoSetter), |
| 8466 | v8::None, |
| 8467 | v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
| 8468 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8469 | // Add an accessor that is not accessible by cross-domain JS code. |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8470 | global_template->SetAccessor(v8_str("blocked_prop"), |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8471 | UnreachableGetter, UnreachableSetter, |
| 8472 | v8::Handle<Value>(), |
| 8473 | v8::DEFAULT); |
| 8474 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8475 | global_template->SetAccessorProperty( |
| 8476 | v8_str("blocked_js_prop"), |
| 8477 | v8::FunctionTemplate::New(UnreachableFunction), |
| 8478 | v8::FunctionTemplate::New(UnreachableFunction), |
| 8479 | v8::None, |
| 8480 | v8::DEFAULT); |
| 8481 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8482 | // Create an environment |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8483 | v8::Local<Context> context0 = Context::New(isolate, NULL, global_template); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8484 | context0->Enter(); |
| 8485 | |
| 8486 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 8487 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8488 | // Define a property with JS getter and setter. |
| 8489 | CompileRun( |
| 8490 | "function getter() { return 'getter'; };\n" |
| 8491 | "function setter() { return 'setter'; }\n" |
| 8492 | "Object.defineProperty(this, 'js_accessor_p', {get:getter, set:setter})"); |
| 8493 | |
| 8494 | Local<Value> getter = global0->Get(v8_str("getter")); |
| 8495 | Local<Value> setter = global0->Get(v8_str("setter")); |
| 8496 | |
| 8497 | // And define normal element. |
| 8498 | global0->Set(239, v8_str("239")); |
| 8499 | |
| 8500 | // Define an element with JS getter and setter. |
| 8501 | CompileRun( |
| 8502 | "function el_getter() { return 'el_getter'; };\n" |
| 8503 | "function el_setter() { return 'el_setter'; };\n" |
| 8504 | "Object.defineProperty(this, '42', {get: el_getter, set: el_setter});"); |
| 8505 | |
| 8506 | Local<Value> el_getter = global0->Get(v8_str("el_getter")); |
| 8507 | Local<Value> el_setter = global0->Get(v8_str("el_setter")); |
| 8508 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8509 | v8::HandleScope scope1(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8510 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8511 | v8::Local<Context> context1 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8512 | context1->Enter(); |
| 8513 | |
| 8514 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 8515 | global1->Set(v8_str("other"), global0); |
| 8516 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8517 | // Access blocked property. |
| 8518 | CompileRun("other.blocked_prop = 1"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8519 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8520 | ExpectUndefined("other.blocked_prop"); |
| 8521 | ExpectUndefined( |
| 8522 | "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); |
| 8523 | ExpectFalse("propertyIsEnumerable.call(other, 'blocked_prop')"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8524 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8525 | // Enable ACCESS_HAS |
| 8526 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8527 | ExpectUndefined("other.blocked_prop"); |
| 8528 | // ... and now we can get the descriptor... |
| 8529 | ExpectUndefined( |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8530 | "Object.getOwnPropertyDescriptor(other, 'blocked_prop').value"); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8531 | // ... and enumerate the property. |
| 8532 | ExpectTrue("propertyIsEnumerable.call(other, 'blocked_prop')"); |
| 8533 | allowed_access_type[v8::ACCESS_HAS] = false; |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8534 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 8535 | // Access blocked element. |
| 8536 | CompileRun("other[239] = 1"); |
| 8537 | |
| 8538 | ExpectUndefined("other[239]"); |
| 8539 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '239')"); |
| 8540 | ExpectFalse("propertyIsEnumerable.call(other, '239')"); |
| 8541 | |
| 8542 | // Enable ACCESS_HAS |
| 8543 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8544 | ExpectUndefined("other[239]"); |
| 8545 | // ... and now we can get the descriptor... |
| 8546 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '239').value"); |
| 8547 | // ... and enumerate the property. |
| 8548 | ExpectTrue("propertyIsEnumerable.call(other, '239')"); |
| 8549 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8550 | |
| 8551 | // Access a property with JS accessor. |
| 8552 | CompileRun("other.js_accessor_p = 2"); |
| 8553 | |
| 8554 | ExpectUndefined("other.js_accessor_p"); |
| 8555 | ExpectUndefined( |
| 8556 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p')"); |
| 8557 | |
| 8558 | // Enable ACCESS_HAS. |
| 8559 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8560 | ExpectUndefined("other.js_accessor_p"); |
| 8561 | ExpectUndefined( |
| 8562 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 8563 | ExpectUndefined( |
| 8564 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 8565 | ExpectUndefined( |
| 8566 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 8567 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8568 | |
| 8569 | // Enable both ACCESS_HAS and ACCESS_GET. |
| 8570 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8571 | allowed_access_type[v8::ACCESS_GET] = true; |
| 8572 | |
| 8573 | ExpectString("other.js_accessor_p", "getter"); |
| 8574 | ExpectObject( |
| 8575 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 8576 | ExpectUndefined( |
| 8577 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set"); |
| 8578 | ExpectUndefined( |
| 8579 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 8580 | |
| 8581 | allowed_access_type[v8::ACCESS_GET] = false; |
| 8582 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8583 | |
| 8584 | // Enable both ACCESS_HAS and ACCESS_SET. |
| 8585 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8586 | allowed_access_type[v8::ACCESS_SET] = true; |
| 8587 | |
| 8588 | ExpectUndefined("other.js_accessor_p"); |
| 8589 | ExpectUndefined( |
| 8590 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get"); |
| 8591 | ExpectObject( |
| 8592 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 8593 | ExpectUndefined( |
| 8594 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 8595 | |
| 8596 | allowed_access_type[v8::ACCESS_SET] = false; |
| 8597 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8598 | |
| 8599 | // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET. |
| 8600 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8601 | allowed_access_type[v8::ACCESS_GET] = true; |
| 8602 | allowed_access_type[v8::ACCESS_SET] = true; |
| 8603 | |
| 8604 | ExpectString("other.js_accessor_p", "getter"); |
| 8605 | ExpectObject( |
| 8606 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').get", getter); |
| 8607 | ExpectObject( |
| 8608 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').set", setter); |
| 8609 | ExpectUndefined( |
| 8610 | "Object.getOwnPropertyDescriptor(other, 'js_accessor_p').value"); |
| 8611 | |
| 8612 | allowed_access_type[v8::ACCESS_SET] = false; |
| 8613 | allowed_access_type[v8::ACCESS_GET] = false; |
| 8614 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8615 | |
| 8616 | // Access an element with JS accessor. |
| 8617 | CompileRun("other[42] = 2"); |
| 8618 | |
| 8619 | ExpectUndefined("other[42]"); |
| 8620 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42')"); |
| 8621 | |
| 8622 | // Enable ACCESS_HAS. |
| 8623 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8624 | ExpectUndefined("other[42]"); |
| 8625 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').get"); |
| 8626 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').set"); |
| 8627 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value"); |
| 8628 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8629 | |
| 8630 | // Enable both ACCESS_HAS and ACCESS_GET. |
| 8631 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8632 | allowed_access_type[v8::ACCESS_GET] = true; |
| 8633 | |
| 8634 | ExpectString("other[42]", "el_getter"); |
| 8635 | ExpectObject("Object.getOwnPropertyDescriptor(other, '42').get", el_getter); |
| 8636 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').set"); |
| 8637 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value"); |
| 8638 | |
| 8639 | allowed_access_type[v8::ACCESS_GET] = false; |
| 8640 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8641 | |
| 8642 | // Enable both ACCESS_HAS and ACCESS_SET. |
| 8643 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8644 | allowed_access_type[v8::ACCESS_SET] = true; |
| 8645 | |
| 8646 | ExpectUndefined("other[42]"); |
| 8647 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').get"); |
| 8648 | ExpectObject("Object.getOwnPropertyDescriptor(other, '42').set", el_setter); |
| 8649 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value"); |
| 8650 | |
| 8651 | allowed_access_type[v8::ACCESS_SET] = false; |
| 8652 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8653 | |
| 8654 | // Enable both ACCESS_HAS, ACCESS_GET and ACCESS_SET. |
| 8655 | allowed_access_type[v8::ACCESS_HAS] = true; |
| 8656 | allowed_access_type[v8::ACCESS_GET] = true; |
| 8657 | allowed_access_type[v8::ACCESS_SET] = true; |
| 8658 | |
| 8659 | ExpectString("other[42]", "el_getter"); |
| 8660 | ExpectObject("Object.getOwnPropertyDescriptor(other, '42').get", el_getter); |
| 8661 | ExpectObject("Object.getOwnPropertyDescriptor(other, '42').set", el_setter); |
| 8662 | ExpectUndefined("Object.getOwnPropertyDescriptor(other, '42').value"); |
| 8663 | |
| 8664 | allowed_access_type[v8::ACCESS_SET] = false; |
| 8665 | allowed_access_type[v8::ACCESS_GET] = false; |
| 8666 | allowed_access_type[v8::ACCESS_HAS] = false; |
| 8667 | |
| 8668 | v8::Handle<Value> value; |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8669 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8670 | // Access accessible property |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8671 | value = CompileRun("other.accessible_prop = 3"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8672 | CHECK(value->IsNumber()); |
| 8673 | CHECK_EQ(3, value->Int32Value()); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8674 | CHECK_EQ(3, g_echo_value_1); |
| 8675 | |
| 8676 | // Access accessible js property |
| 8677 | value = CompileRun("other.accessible_js_prop = 3"); |
| 8678 | CHECK(value->IsNumber()); |
| 8679 | CHECK_EQ(3, value->Int32Value()); |
| 8680 | CHECK_EQ(3, g_echo_value_2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8681 | |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8682 | value = CompileRun("other.accessible_prop"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8683 | CHECK(value->IsNumber()); |
| 8684 | CHECK_EQ(3, value->Int32Value()); |
| 8685 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8686 | value = CompileRun("other.accessible_js_prop"); |
| 8687 | CHECK(value->IsNumber()); |
| 8688 | CHECK_EQ(3, value->Int32Value()); |
| 8689 | |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8690 | value = CompileRun( |
| 8691 | "Object.getOwnPropertyDescriptor(other, 'accessible_prop').value"); |
| 8692 | CHECK(value->IsNumber()); |
| 8693 | CHECK_EQ(3, value->Int32Value()); |
| 8694 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8695 | value = CompileRun( |
| 8696 | "Object.getOwnPropertyDescriptor(other, 'accessible_js_prop').get()"); |
| 8697 | CHECK(value->IsNumber()); |
| 8698 | CHECK_EQ(3, value->Int32Value()); |
| 8699 | |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8700 | value = CompileRun("propertyIsEnumerable.call(other, 'accessible_prop')"); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8701 | CHECK(value->IsTrue()); |
| 8702 | |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8703 | value = CompileRun("propertyIsEnumerable.call(other, 'accessible_js_prop')"); |
| 8704 | CHECK(value->IsTrue()); |
| 8705 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8706 | // Enumeration doesn't enumerate accessors from inaccessible objects in |
| 8707 | // the prototype chain even if the accessors are in themselves accessible. |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8708 | value = |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8709 | CompileRun("(function(){var obj = {'__proto__':other};" |
| 8710 | "for (var p in obj)" |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8711 | " if (p == 'accessible_prop' ||" |
| 8712 | " p == 'accessible_js_prop' ||" |
| 8713 | " p == 'blocked_js_prop' ||" |
| 8714 | " p == 'blocked_js_prop') {" |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8715 | " return false;" |
| 8716 | " }" |
| 8717 | "return true;})()"); |
antonm@chromium.org | dca0135 | 2011-01-31 17:15:05 +0000 | [diff] [blame] | 8718 | CHECK(value->IsTrue()); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 8719 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8720 | context1->Exit(); |
| 8721 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8722 | } |
| 8723 | |
| 8724 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8725 | TEST(AccessControlES5) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8726 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8727 | v8::HandleScope handle_scope(isolate); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8728 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 8729 | |
| 8730 | global_template->SetAccessCheckCallbacks(NamedAccessBlocker, |
| 8731 | IndexedAccessBlocker); |
| 8732 | |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 8733 | // Add accessible accessor. |
| 8734 | global_template->SetAccessor( |
| 8735 | v8_str("accessible_prop"), |
| 8736 | EchoGetter, EchoSetter, |
| 8737 | v8::Handle<Value>(), |
| 8738 | v8::AccessControl(v8::ALL_CAN_READ | v8::ALL_CAN_WRITE)); |
| 8739 | |
| 8740 | |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8741 | // Add an accessor that is not accessible by cross-domain JS code. |
| 8742 | global_template->SetAccessor(v8_str("blocked_prop"), |
| 8743 | UnreachableGetter, UnreachableSetter, |
| 8744 | v8::Handle<Value>(), |
| 8745 | v8::DEFAULT); |
| 8746 | |
| 8747 | // Create an environment |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8748 | v8::Local<Context> context0 = Context::New(isolate, NULL, global_template); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8749 | context0->Enter(); |
| 8750 | |
| 8751 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 8752 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8753 | v8::Local<Context> context1 = Context::New(isolate); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8754 | context1->Enter(); |
| 8755 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 8756 | global1->Set(v8_str("other"), global0); |
| 8757 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8758 | // Regression test for issue 1154. |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8759 | ExpectTrue("Object.keys(other).indexOf('blocked_prop') == -1"); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 8760 | |
| 8761 | ExpectUndefined("other.blocked_prop"); |
| 8762 | |
| 8763 | // Regression test for issue 1027. |
| 8764 | CompileRun("Object.defineProperty(\n" |
| 8765 | " other, 'blocked_prop', {configurable: false})"); |
| 8766 | ExpectUndefined("other.blocked_prop"); |
| 8767 | ExpectUndefined( |
| 8768 | "Object.getOwnPropertyDescriptor(other, 'blocked_prop')"); |
| 8769 | |
| 8770 | // Regression test for issue 1171. |
| 8771 | ExpectTrue("Object.isExtensible(other)"); |
| 8772 | CompileRun("Object.preventExtensions(other)"); |
| 8773 | ExpectTrue("Object.isExtensible(other)"); |
| 8774 | |
| 8775 | // Object.seal and Object.freeze. |
| 8776 | CompileRun("Object.freeze(other)"); |
| 8777 | ExpectTrue("Object.isExtensible(other)"); |
| 8778 | |
| 8779 | CompileRun("Object.seal(other)"); |
| 8780 | ExpectTrue("Object.isExtensible(other)"); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 8781 | |
| 8782 | // Regression test for issue 1250. |
| 8783 | // Make sure that we can set the accessible accessors value using normal |
| 8784 | // assignment. |
| 8785 | CompileRun("other.accessible_prop = 42"); |
jkummerow@chromium.org | 1e8da74 | 2013-08-26 17:13:35 +0000 | [diff] [blame] | 8786 | CHECK_EQ(42, g_echo_value_1); |
ricow@chromium.org | f5a18a2 | 2011-03-15 10:00:20 +0000 | [diff] [blame] | 8787 | |
| 8788 | v8::Handle<Value> value; |
| 8789 | // We follow Safari in ignoring assignments to host object accessors. |
| 8790 | CompileRun("Object.defineProperty(other, 'accessible_prop', {value: -1})"); |
| 8791 | value = CompileRun("other.accessible_prop == 42"); |
| 8792 | CHECK(value->IsTrue()); |
ricow@chromium.org | 6500178 | 2011-02-15 13:36:41 +0000 | [diff] [blame] | 8793 | } |
| 8794 | |
| 8795 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8796 | static bool GetOwnPropertyNamesNamedBlocker(Local<v8::Object> global, |
| 8797 | Local<Value> name, |
| 8798 | v8::AccessType type, |
| 8799 | Local<Value> data) { |
| 8800 | return false; |
| 8801 | } |
| 8802 | |
| 8803 | |
| 8804 | static bool GetOwnPropertyNamesIndexedBlocker(Local<v8::Object> global, |
| 8805 | uint32_t key, |
| 8806 | v8::AccessType type, |
| 8807 | Local<Value> data) { |
| 8808 | return false; |
| 8809 | } |
| 8810 | |
| 8811 | |
| 8812 | THREADED_TEST(AccessControlGetOwnPropertyNames) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8813 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8814 | v8::HandleScope handle_scope(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8815 | v8::Handle<v8::ObjectTemplate> obj_template = v8::ObjectTemplate::New(); |
| 8816 | |
| 8817 | obj_template->Set(v8_str("x"), v8::Integer::New(42)); |
| 8818 | obj_template->SetAccessCheckCallbacks(GetOwnPropertyNamesNamedBlocker, |
| 8819 | GetOwnPropertyNamesIndexedBlocker); |
| 8820 | |
| 8821 | // Create an environment |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8822 | v8::Local<Context> context0 = Context::New(isolate, NULL, obj_template); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8823 | context0->Enter(); |
| 8824 | |
| 8825 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 8826 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8827 | v8::HandleScope scope1(CcTest::isolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8828 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8829 | v8::Local<Context> context1 = Context::New(isolate); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8830 | context1->Enter(); |
| 8831 | |
| 8832 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 8833 | global1->Set(v8_str("other"), global0); |
| 8834 | global1->Set(v8_str("object"), obj_template->NewInstance()); |
| 8835 | |
| 8836 | v8::Handle<Value> value; |
| 8837 | |
| 8838 | // Attempt to get the property names of the other global object and |
| 8839 | // of an object that requires access checks. Accessing the other |
| 8840 | // global object should be blocked by access checks on the global |
| 8841 | // proxy object. Accessing the object that requires access checks |
| 8842 | // is blocked by the access checks on the object itself. |
| 8843 | value = CompileRun("Object.getOwnPropertyNames(other).length == 0"); |
| 8844 | CHECK(value->IsTrue()); |
| 8845 | |
| 8846 | value = CompileRun("Object.getOwnPropertyNames(object).length == 0"); |
| 8847 | CHECK(value->IsTrue()); |
| 8848 | |
| 8849 | context1->Exit(); |
| 8850 | context0->Exit(); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 8851 | } |
| 8852 | |
| 8853 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8854 | static void IndexedPropertyEnumerator( |
| 8855 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8856 | v8::Handle<v8::Array> result = v8::Array::New(2); |
| 8857 | result->Set(0, v8::Integer::New(7)); |
| 8858 | result->Set(1, v8::Object::New()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8859 | info.GetReturnValue().Set(result); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8860 | } |
| 8861 | |
| 8862 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8863 | static void NamedPropertyEnumerator( |
| 8864 | const v8::PropertyCallbackInfo<v8::Array>& info) { |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8865 | v8::Handle<v8::Array> result = v8::Array::New(2); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8866 | result->Set(0, v8_str("x")); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8867 | result->Set(1, v8::Object::New()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8868 | info.GetReturnValue().Set(result); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8869 | } |
| 8870 | |
| 8871 | |
| 8872 | THREADED_TEST(GetOwnPropertyNamesWithInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8873 | v8::HandleScope handle_scope(CcTest::isolate()); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8874 | v8::Handle<v8::ObjectTemplate> obj_template = v8::ObjectTemplate::New(); |
| 8875 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8876 | obj_template->Set(v8_str("7"), v8::Integer::New(7)); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8877 | obj_template->Set(v8_str("x"), v8::Integer::New(42)); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8878 | obj_template->SetIndexedPropertyHandler(NULL, NULL, NULL, NULL, |
| 8879 | IndexedPropertyEnumerator); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8880 | obj_template->SetNamedPropertyHandler(NULL, NULL, NULL, NULL, |
| 8881 | NamedPropertyEnumerator); |
| 8882 | |
| 8883 | LocalContext context; |
| 8884 | v8::Handle<v8::Object> global = context->Global(); |
| 8885 | global->Set(v8_str("object"), obj_template->NewInstance()); |
| 8886 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 8887 | v8::Handle<v8::Value> result = |
| 8888 | CompileRun("Object.getOwnPropertyNames(object)"); |
| 8889 | CHECK(result->IsArray()); |
| 8890 | v8::Handle<v8::Array> result_array = v8::Handle<v8::Array>::Cast(result); |
| 8891 | CHECK_EQ(3, result_array->Length()); |
| 8892 | CHECK(result_array->Get(0)->IsString()); |
| 8893 | CHECK(result_array->Get(1)->IsString()); |
| 8894 | CHECK(result_array->Get(2)->IsString()); |
| 8895 | CHECK_EQ(v8_str("7"), result_array->Get(0)); |
| 8896 | CHECK_EQ(v8_str("[object Object]"), result_array->Get(1)); |
| 8897 | CHECK_EQ(v8_str("x"), result_array->Get(2)); |
fschneider@chromium.org | 40b9da3 | 2010-06-28 11:29:21 +0000 | [diff] [blame] | 8898 | } |
| 8899 | |
| 8900 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 8901 | static void ConstTenGetter(Local<String> name, |
| 8902 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 8903 | info.GetReturnValue().Set(v8_num(10)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8904 | } |
| 8905 | |
| 8906 | |
| 8907 | THREADED_TEST(CrossDomainAccessors) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8908 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8909 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8910 | |
| 8911 | v8::Handle<v8::FunctionTemplate> func_template = v8::FunctionTemplate::New(); |
| 8912 | |
| 8913 | v8::Handle<v8::ObjectTemplate> global_template = |
| 8914 | func_template->InstanceTemplate(); |
| 8915 | |
| 8916 | v8::Handle<v8::ObjectTemplate> proto_template = |
| 8917 | func_template->PrototypeTemplate(); |
| 8918 | |
| 8919 | // Add an accessor to proto that's accessible by cross-domain JS code. |
| 8920 | proto_template->SetAccessor(v8_str("accessible"), |
| 8921 | ConstTenGetter, 0, |
| 8922 | v8::Handle<Value>(), |
| 8923 | v8::ALL_CAN_READ); |
| 8924 | |
| 8925 | // Add an accessor that is not accessible by cross-domain JS code. |
| 8926 | global_template->SetAccessor(v8_str("unreachable"), |
| 8927 | UnreachableGetter, 0, |
| 8928 | v8::Handle<Value>(), |
| 8929 | v8::DEFAULT); |
| 8930 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8931 | v8::Local<Context> context0 = Context::New(isolate, NULL, global_template); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8932 | context0->Enter(); |
| 8933 | |
| 8934 | Local<v8::Object> global = context0->Global(); |
| 8935 | // Add a normal property that shadows 'accessible' |
| 8936 | global->Set(v8_str("accessible"), v8_num(11)); |
| 8937 | |
| 8938 | // Enter a new context. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8939 | v8::HandleScope scope1(CcTest::isolate()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8940 | v8::Local<Context> context1 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8941 | context1->Enter(); |
| 8942 | |
| 8943 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 8944 | global1->Set(v8_str("other"), global); |
| 8945 | |
| 8946 | // Should return 10, instead of 11 |
| 8947 | v8::Handle<Value> value = v8_compile("other.accessible")->Run(); |
| 8948 | CHECK(value->IsNumber()); |
| 8949 | CHECK_EQ(10, value->Int32Value()); |
| 8950 | |
| 8951 | value = v8_compile("other.unreachable")->Run(); |
| 8952 | CHECK(value->IsUndefined()); |
| 8953 | |
| 8954 | context1->Exit(); |
| 8955 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8956 | } |
| 8957 | |
| 8958 | |
| 8959 | static int named_access_count = 0; |
| 8960 | static int indexed_access_count = 0; |
| 8961 | |
| 8962 | static bool NamedAccessCounter(Local<v8::Object> global, |
| 8963 | Local<Value> name, |
| 8964 | v8::AccessType type, |
| 8965 | Local<Value> data) { |
| 8966 | named_access_count++; |
| 8967 | return true; |
| 8968 | } |
| 8969 | |
| 8970 | |
| 8971 | static bool IndexedAccessCounter(Local<v8::Object> global, |
| 8972 | uint32_t key, |
| 8973 | v8::AccessType type, |
| 8974 | Local<Value> data) { |
| 8975 | indexed_access_count++; |
| 8976 | return true; |
| 8977 | } |
| 8978 | |
| 8979 | |
| 8980 | // This one is too easily disturbed by other tests. |
| 8981 | TEST(AccessControlIC) { |
| 8982 | named_access_count = 0; |
| 8983 | indexed_access_count = 0; |
| 8984 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 8985 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8986 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8987 | |
| 8988 | // Create an environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8989 | v8::Local<Context> context0 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 8990 | context0->Enter(); |
| 8991 | |
| 8992 | // Create an object that requires access-check functions to be |
| 8993 | // called for cross-domain access. |
| 8994 | v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 8995 | object_template->SetAccessCheckCallbacks(NamedAccessCounter, |
| 8996 | IndexedAccessCounter); |
| 8997 | Local<v8::Object> object = object_template->NewInstance(); |
| 8998 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 8999 | v8::HandleScope scope1(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9000 | |
| 9001 | // Create another environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9002 | v8::Local<Context> context1 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9003 | context1->Enter(); |
| 9004 | |
| 9005 | // Make easy access to the object from the other environment. |
| 9006 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 9007 | global1->Set(v8_str("obj"), object); |
| 9008 | |
| 9009 | v8::Handle<Value> value; |
| 9010 | |
| 9011 | // Check that the named access-control function is called every time. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9012 | CompileRun("function testProp(obj) {" |
| 9013 | " for (var i = 0; i < 10; i++) obj.prop = 1;" |
| 9014 | " for (var j = 0; j < 10; j++) obj.prop;" |
| 9015 | " return obj.prop" |
| 9016 | "}"); |
| 9017 | value = CompileRun("testProp(obj)"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9018 | CHECK(value->IsNumber()); |
| 9019 | CHECK_EQ(1, value->Int32Value()); |
| 9020 | CHECK_EQ(21, named_access_count); |
| 9021 | |
| 9022 | // Check that the named access-control function is called every time. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9023 | CompileRun("var p = 'prop';" |
| 9024 | "function testKeyed(obj) {" |
| 9025 | " for (var i = 0; i < 10; i++) obj[p] = 1;" |
| 9026 | " for (var j = 0; j < 10; j++) obj[p];" |
| 9027 | " return obj[p];" |
| 9028 | "}"); |
| 9029 | // Use obj which requires access checks. No inline caching is used |
| 9030 | // in that case. |
| 9031 | value = CompileRun("testKeyed(obj)"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9032 | CHECK(value->IsNumber()); |
| 9033 | CHECK_EQ(1, value->Int32Value()); |
| 9034 | CHECK_EQ(42, named_access_count); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9035 | // Force the inline caches into generic state and try again. |
| 9036 | CompileRun("testKeyed({ a: 0 })"); |
| 9037 | CompileRun("testKeyed({ b: 0 })"); |
| 9038 | value = CompileRun("testKeyed(obj)"); |
| 9039 | CHECK(value->IsNumber()); |
| 9040 | CHECK_EQ(1, value->Int32Value()); |
| 9041 | CHECK_EQ(63, named_access_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9042 | |
| 9043 | // Check that the indexed access-control function is called every time. |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9044 | CompileRun("function testIndexed(obj) {" |
| 9045 | " for (var i = 0; i < 10; i++) obj[0] = 1;" |
| 9046 | " for (var j = 0; j < 10; j++) obj[0];" |
| 9047 | " return obj[0]" |
| 9048 | "}"); |
| 9049 | value = CompileRun("testIndexed(obj)"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9050 | CHECK(value->IsNumber()); |
| 9051 | CHECK_EQ(1, value->Int32Value()); |
| 9052 | CHECK_EQ(21, indexed_access_count); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 9053 | // Force the inline caches into generic state. |
| 9054 | CompileRun("testIndexed(new Array(1))"); |
| 9055 | // Test that the indexed access check is called. |
| 9056 | value = CompileRun("testIndexed(obj)"); |
| 9057 | CHECK(value->IsNumber()); |
| 9058 | CHECK_EQ(1, value->Int32Value()); |
| 9059 | CHECK_EQ(42, indexed_access_count); |
| 9060 | |
| 9061 | // Check that the named access check is called when invoking |
| 9062 | // functions on an object that requires access checks. |
| 9063 | CompileRun("obj.f = function() {}"); |
| 9064 | CompileRun("function testCallNormal(obj) {" |
| 9065 | " for (var i = 0; i < 10; i++) obj.f();" |
| 9066 | "}"); |
| 9067 | CompileRun("testCallNormal(obj)"); |
| 9068 | CHECK_EQ(74, named_access_count); |
| 9069 | |
| 9070 | // Force obj into slow case. |
| 9071 | value = CompileRun("delete obj.prop"); |
| 9072 | CHECK(value->BooleanValue()); |
| 9073 | // Force inline caches into dictionary probing mode. |
| 9074 | CompileRun("var o = { x: 0 }; delete o.x; testProp(o);"); |
| 9075 | // Test that the named access check is called. |
| 9076 | value = CompileRun("testProp(obj);"); |
| 9077 | CHECK(value->IsNumber()); |
| 9078 | CHECK_EQ(1, value->Int32Value()); |
| 9079 | CHECK_EQ(96, named_access_count); |
| 9080 | |
| 9081 | // Force the call inline cache into dictionary probing mode. |
| 9082 | CompileRun("o.f = function() {}; testCallNormal(o)"); |
| 9083 | // Test that the named access check is still called for each |
| 9084 | // invocation of the function. |
| 9085 | value = CompileRun("testCallNormal(obj)"); |
| 9086 | CHECK_EQ(106, named_access_count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9087 | |
| 9088 | context1->Exit(); |
| 9089 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9090 | } |
| 9091 | |
| 9092 | |
| 9093 | static bool NamedAccessFlatten(Local<v8::Object> global, |
| 9094 | Local<Value> name, |
| 9095 | v8::AccessType type, |
| 9096 | Local<Value> data) { |
| 9097 | char buf[100]; |
| 9098 | int len; |
| 9099 | |
| 9100 | CHECK(name->IsString()); |
| 9101 | |
| 9102 | memset(buf, 0x1, sizeof(buf)); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9103 | len = name.As<String>()->WriteOneByte(reinterpret_cast<uint8_t*>(buf)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9104 | CHECK_EQ(4, len); |
| 9105 | |
| 9106 | uint16_t buf2[100]; |
| 9107 | |
| 9108 | memset(buf, 0x1, sizeof(buf)); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9109 | len = name.As<String>()->Write(buf2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9110 | CHECK_EQ(4, len); |
| 9111 | |
| 9112 | return true; |
| 9113 | } |
| 9114 | |
| 9115 | |
| 9116 | static bool IndexedAccessFlatten(Local<v8::Object> global, |
| 9117 | uint32_t key, |
| 9118 | v8::AccessType type, |
| 9119 | Local<Value> data) { |
| 9120 | return true; |
| 9121 | } |
| 9122 | |
| 9123 | |
| 9124 | // Regression test. In access checks, operations that may cause |
| 9125 | // garbage collection are not allowed. It used to be the case that |
| 9126 | // using the Write operation on a string could cause a garbage |
| 9127 | // collection due to flattening of the string. This is no longer the |
| 9128 | // case. |
| 9129 | THREADED_TEST(AccessControlFlatten) { |
| 9130 | named_access_count = 0; |
| 9131 | indexed_access_count = 0; |
| 9132 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9133 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9134 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9135 | |
| 9136 | // Create an environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9137 | v8::Local<Context> context0 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9138 | context0->Enter(); |
| 9139 | |
| 9140 | // Create an object that requires access-check functions to be |
| 9141 | // called for cross-domain access. |
| 9142 | v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 9143 | object_template->SetAccessCheckCallbacks(NamedAccessFlatten, |
| 9144 | IndexedAccessFlatten); |
| 9145 | Local<v8::Object> object = object_template->NewInstance(); |
| 9146 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9147 | v8::HandleScope scope1(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9148 | |
| 9149 | // Create another environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9150 | v8::Local<Context> context1 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9151 | context1->Enter(); |
| 9152 | |
| 9153 | // Make easy access to the object from the other environment. |
| 9154 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 9155 | global1->Set(v8_str("obj"), object); |
| 9156 | |
| 9157 | v8::Handle<Value> value; |
| 9158 | |
| 9159 | value = v8_compile("var p = 'as' + 'df';")->Run(); |
| 9160 | value = v8_compile("obj[p];")->Run(); |
| 9161 | |
| 9162 | context1->Exit(); |
| 9163 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9164 | } |
| 9165 | |
| 9166 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9167 | static void AccessControlNamedGetter( |
| 9168 | Local<String>, |
| 9169 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 9170 | info.GetReturnValue().Set(42); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9171 | } |
| 9172 | |
| 9173 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9174 | static void AccessControlNamedSetter( |
| 9175 | Local<String>, |
| 9176 | Local<Value> value, |
| 9177 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 9178 | info.GetReturnValue().Set(value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9179 | } |
| 9180 | |
| 9181 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9182 | static void AccessControlIndexedGetter( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9183 | uint32_t index, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9184 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 9185 | info.GetReturnValue().Set(v8_num(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9186 | } |
| 9187 | |
| 9188 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9189 | static void AccessControlIndexedSetter( |
| 9190 | uint32_t, |
| 9191 | Local<Value> value, |
| 9192 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 9193 | info.GetReturnValue().Set(value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9194 | } |
| 9195 | |
| 9196 | |
| 9197 | THREADED_TEST(AccessControlInterceptorIC) { |
| 9198 | named_access_count = 0; |
| 9199 | indexed_access_count = 0; |
| 9200 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9201 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9202 | v8::HandleScope handle_scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9203 | |
| 9204 | // Create an environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9205 | v8::Local<Context> context0 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9206 | context0->Enter(); |
| 9207 | |
| 9208 | // Create an object that requires access-check functions to be |
| 9209 | // called for cross-domain access. The object also has interceptors |
| 9210 | // interceptor. |
| 9211 | v8::Handle<v8::ObjectTemplate> object_template = v8::ObjectTemplate::New(); |
| 9212 | object_template->SetAccessCheckCallbacks(NamedAccessCounter, |
| 9213 | IndexedAccessCounter); |
| 9214 | object_template->SetNamedPropertyHandler(AccessControlNamedGetter, |
| 9215 | AccessControlNamedSetter); |
| 9216 | object_template->SetIndexedPropertyHandler(AccessControlIndexedGetter, |
| 9217 | AccessControlIndexedSetter); |
| 9218 | Local<v8::Object> object = object_template->NewInstance(); |
| 9219 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9220 | v8::HandleScope scope1(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9221 | |
| 9222 | // Create another environment. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 9223 | v8::Local<Context> context1 = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9224 | context1->Enter(); |
| 9225 | |
| 9226 | // Make easy access to the object from the other environment. |
| 9227 | v8::Handle<v8::Object> global1 = context1->Global(); |
| 9228 | global1->Set(v8_str("obj"), object); |
| 9229 | |
| 9230 | v8::Handle<Value> value; |
| 9231 | |
| 9232 | // Check that the named access-control function is called every time |
| 9233 | // eventhough there is an interceptor on the object. |
| 9234 | value = v8_compile("for (var i = 0; i < 10; i++) obj.x = 1;")->Run(); |
| 9235 | value = v8_compile("for (var i = 0; i < 10; i++) obj.x;" |
| 9236 | "obj.x")->Run(); |
| 9237 | CHECK(value->IsNumber()); |
| 9238 | CHECK_EQ(42, value->Int32Value()); |
| 9239 | CHECK_EQ(21, named_access_count); |
| 9240 | |
| 9241 | value = v8_compile("var p = 'x';")->Run(); |
| 9242 | value = v8_compile("for (var i = 0; i < 10; i++) obj[p] = 1;")->Run(); |
| 9243 | value = v8_compile("for (var i = 0; i < 10; i++) obj[p];" |
| 9244 | "obj[p]")->Run(); |
| 9245 | CHECK(value->IsNumber()); |
| 9246 | CHECK_EQ(42, value->Int32Value()); |
| 9247 | CHECK_EQ(42, named_access_count); |
| 9248 | |
| 9249 | // Check that the indexed access-control function is called every |
| 9250 | // time eventhough there is an interceptor on the object. |
| 9251 | value = v8_compile("for (var i = 0; i < 10; i++) obj[0] = 1;")->Run(); |
| 9252 | value = v8_compile("for (var i = 0; i < 10; i++) obj[0];" |
| 9253 | "obj[0]")->Run(); |
| 9254 | CHECK(value->IsNumber()); |
| 9255 | CHECK_EQ(42, value->Int32Value()); |
| 9256 | CHECK_EQ(21, indexed_access_count); |
| 9257 | |
| 9258 | context1->Exit(); |
| 9259 | context0->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9260 | } |
| 9261 | |
| 9262 | |
| 9263 | THREADED_TEST(Version) { |
| 9264 | v8::V8::GetVersion(); |
| 9265 | } |
| 9266 | |
| 9267 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9268 | static void InstanceFunctionCallback( |
| 9269 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9270 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9271 | args.GetReturnValue().Set(v8_num(12)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9272 | } |
| 9273 | |
| 9274 | |
| 9275 | THREADED_TEST(InstanceProperties) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9276 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9277 | v8::HandleScope handle_scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9278 | |
| 9279 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 9280 | Local<ObjectTemplate> instance = t->InstanceTemplate(); |
| 9281 | |
| 9282 | instance->Set(v8_str("x"), v8_num(42)); |
| 9283 | instance->Set(v8_str("f"), |
| 9284 | v8::FunctionTemplate::New(InstanceFunctionCallback)); |
| 9285 | |
| 9286 | Local<Value> o = t->GetFunction()->NewInstance(); |
| 9287 | |
| 9288 | context->Global()->Set(v8_str("i"), o); |
| 9289 | Local<Value> value = Script::Compile(v8_str("i.x"))->Run(); |
| 9290 | CHECK_EQ(42, value->Int32Value()); |
| 9291 | |
| 9292 | value = Script::Compile(v8_str("i.f()"))->Run(); |
| 9293 | CHECK_EQ(12, value->Int32Value()); |
| 9294 | } |
| 9295 | |
| 9296 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9297 | static void GlobalObjectInstancePropertiesGet( |
| 9298 | Local<String> key, |
| 9299 | const v8::PropertyCallbackInfo<v8::Value>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9300 | ApiTestFuzzer::Fuzz(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9301 | } |
| 9302 | |
| 9303 | |
| 9304 | THREADED_TEST(GlobalObjectInstanceProperties) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9305 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9306 | |
| 9307 | Local<Value> global_object; |
| 9308 | |
| 9309 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 9310 | t->InstanceTemplate()->SetNamedPropertyHandler( |
| 9311 | GlobalObjectInstancePropertiesGet); |
| 9312 | Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 9313 | instance_template->Set(v8_str("x"), v8_num(42)); |
| 9314 | instance_template->Set(v8_str("f"), |
| 9315 | v8::FunctionTemplate::New(InstanceFunctionCallback)); |
| 9316 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9317 | // The script to check how Crankshaft compiles missing global function |
| 9318 | // invocations. function g is not defined and should throw on call. |
| 9319 | const char* script = |
| 9320 | "function wrapper(call) {" |
| 9321 | " var x = 0, y = 1;" |
| 9322 | " for (var i = 0; i < 1000; i++) {" |
| 9323 | " x += i * 100;" |
| 9324 | " y += i * 100;" |
| 9325 | " }" |
| 9326 | " if (call) g();" |
| 9327 | "}" |
| 9328 | "for (var i = 0; i < 17; i++) wrapper(false);" |
| 9329 | "var thrown = 0;" |
| 9330 | "try { wrapper(true); } catch (e) { thrown = 1; };" |
| 9331 | "thrown"; |
| 9332 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9333 | { |
| 9334 | LocalContext env(NULL, instance_template); |
| 9335 | // Hold on to the global object so it can be used again in another |
| 9336 | // environment initialization. |
| 9337 | global_object = env->Global(); |
| 9338 | |
| 9339 | Local<Value> value = Script::Compile(v8_str("x"))->Run(); |
| 9340 | CHECK_EQ(42, value->Int32Value()); |
| 9341 | value = Script::Compile(v8_str("f()"))->Run(); |
| 9342 | CHECK_EQ(12, value->Int32Value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9343 | value = Script::Compile(v8_str(script))->Run(); |
| 9344 | CHECK_EQ(1, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9345 | } |
| 9346 | |
| 9347 | { |
| 9348 | // Create new environment reusing the global object. |
| 9349 | LocalContext env(NULL, instance_template, global_object); |
| 9350 | Local<Value> value = Script::Compile(v8_str("x"))->Run(); |
| 9351 | CHECK_EQ(42, value->Int32Value()); |
| 9352 | value = Script::Compile(v8_str("f()"))->Run(); |
| 9353 | CHECK_EQ(12, value->Int32Value()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9354 | value = Script::Compile(v8_str(script))->Run(); |
| 9355 | CHECK_EQ(1, value->Int32Value()); |
| 9356 | } |
| 9357 | } |
| 9358 | |
| 9359 | |
| 9360 | THREADED_TEST(CallKnownGlobalReceiver) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9361 | v8::HandleScope handle_scope(CcTest::isolate()); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 9362 | |
| 9363 | Local<Value> global_object; |
| 9364 | |
| 9365 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 9366 | Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 9367 | |
| 9368 | // The script to check that we leave global object not |
| 9369 | // global object proxy on stack when we deoptimize from inside |
| 9370 | // arguments evaluation. |
| 9371 | // To provoke error we need to both force deoptimization |
| 9372 | // from arguments evaluation and to force CallIC to take |
| 9373 | // CallIC_Miss code path that can't cope with global proxy. |
| 9374 | const char* script = |
| 9375 | "function bar(x, y) { try { } finally { } }" |
| 9376 | "function baz(x) { try { } finally { } }" |
| 9377 | "function bom(x) { try { } finally { } }" |
| 9378 | "function foo(x) { bar([x], bom(2)); }" |
| 9379 | "for (var i = 0; i < 10000; i++) foo(1);" |
| 9380 | "foo"; |
| 9381 | |
| 9382 | Local<Value> foo; |
| 9383 | { |
| 9384 | LocalContext env(NULL, instance_template); |
| 9385 | // Hold on to the global object so it can be used again in another |
| 9386 | // environment initialization. |
| 9387 | global_object = env->Global(); |
| 9388 | foo = Script::Compile(v8_str(script))->Run(); |
| 9389 | } |
| 9390 | |
| 9391 | { |
| 9392 | // Create new environment reusing the global object. |
| 9393 | LocalContext env(NULL, instance_template, global_object); |
| 9394 | env->Global()->Set(v8_str("foo"), foo); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9395 | Script::Compile(v8_str("foo()"))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9396 | } |
| 9397 | } |
| 9398 | |
| 9399 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9400 | static void ShadowFunctionCallback( |
| 9401 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9402 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9403 | args.GetReturnValue().Set(v8_num(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9404 | } |
| 9405 | |
| 9406 | |
| 9407 | static int shadow_y; |
| 9408 | static int shadow_y_setter_call_count; |
| 9409 | static int shadow_y_getter_call_count; |
| 9410 | |
| 9411 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9412 | static void ShadowYSetter(Local<String>, |
| 9413 | Local<Value>, |
| 9414 | const v8::PropertyCallbackInfo<void>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9415 | shadow_y_setter_call_count++; |
| 9416 | shadow_y = 42; |
| 9417 | } |
| 9418 | |
| 9419 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9420 | static void ShadowYGetter(Local<String> name, |
| 9421 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9422 | ApiTestFuzzer::Fuzz(); |
| 9423 | shadow_y_getter_call_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9424 | info.GetReturnValue().Set(v8_num(shadow_y)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9425 | } |
| 9426 | |
| 9427 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9428 | static void ShadowIndexedGet(uint32_t index, |
| 9429 | const v8::PropertyCallbackInfo<v8::Value>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9430 | } |
| 9431 | |
| 9432 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9433 | static void ShadowNamedGet(Local<String> key, |
| 9434 | const v8::PropertyCallbackInfo<v8::Value>&) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9435 | } |
| 9436 | |
| 9437 | |
| 9438 | THREADED_TEST(ShadowObject) { |
| 9439 | shadow_y = shadow_y_setter_call_count = shadow_y_getter_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9440 | v8::HandleScope handle_scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9441 | |
| 9442 | Local<ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 9443 | LocalContext context(NULL, global_template); |
| 9444 | |
| 9445 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 9446 | t->InstanceTemplate()->SetNamedPropertyHandler(ShadowNamedGet); |
| 9447 | t->InstanceTemplate()->SetIndexedPropertyHandler(ShadowIndexedGet); |
| 9448 | Local<ObjectTemplate> proto = t->PrototypeTemplate(); |
| 9449 | Local<ObjectTemplate> instance = t->InstanceTemplate(); |
| 9450 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9451 | proto->Set(v8_str("f"), |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 9452 | v8::FunctionTemplate::New(ShadowFunctionCallback, Local<Value>())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9453 | proto->Set(v8_str("x"), v8_num(12)); |
| 9454 | |
| 9455 | instance->SetAccessor(v8_str("y"), ShadowYGetter, ShadowYSetter); |
| 9456 | |
| 9457 | Local<Value> o = t->GetFunction()->NewInstance(); |
| 9458 | context->Global()->Set(v8_str("__proto__"), o); |
| 9459 | |
| 9460 | Local<Value> value = |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 9461 | Script::Compile(v8_str("this.propertyIsEnumerable(0)"))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9462 | CHECK(value->IsBoolean()); |
| 9463 | CHECK(!value->BooleanValue()); |
| 9464 | |
| 9465 | value = Script::Compile(v8_str("x"))->Run(); |
| 9466 | CHECK_EQ(12, value->Int32Value()); |
| 9467 | |
| 9468 | value = Script::Compile(v8_str("f()"))->Run(); |
| 9469 | CHECK_EQ(42, value->Int32Value()); |
| 9470 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 9471 | Script::Compile(v8_str("y = 43"))->Run(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9472 | CHECK_EQ(1, shadow_y_setter_call_count); |
| 9473 | value = Script::Compile(v8_str("y"))->Run(); |
| 9474 | CHECK_EQ(1, shadow_y_getter_call_count); |
| 9475 | CHECK_EQ(42, value->Int32Value()); |
| 9476 | } |
| 9477 | |
| 9478 | |
| 9479 | THREADED_TEST(HiddenPrototype) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9480 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9481 | v8::HandleScope handle_scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9482 | |
| 9483 | Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 9484 | t0->InstanceTemplate()->Set(v8_str("x"), v8_num(0)); |
| 9485 | Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 9486 | t1->SetHiddenPrototype(true); |
| 9487 | t1->InstanceTemplate()->Set(v8_str("y"), v8_num(1)); |
| 9488 | Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 9489 | t2->SetHiddenPrototype(true); |
| 9490 | t2->InstanceTemplate()->Set(v8_str("z"), v8_num(2)); |
| 9491 | Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 9492 | t3->InstanceTemplate()->Set(v8_str("u"), v8_num(3)); |
| 9493 | |
| 9494 | Local<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
| 9495 | Local<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 9496 | Local<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 9497 | Local<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 9498 | |
| 9499 | // Setting the prototype on an object skips hidden prototypes. |
| 9500 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9501 | o0->Set(v8_str("__proto__"), o1); |
| 9502 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9503 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9504 | o0->Set(v8_str("__proto__"), o2); |
| 9505 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9506 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9507 | CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 9508 | o0->Set(v8_str("__proto__"), o3); |
| 9509 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9510 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9511 | CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 9512 | CHECK_EQ(3, o0->Get(v8_str("u"))->Int32Value()); |
| 9513 | |
| 9514 | // Getting the prototype of o0 should get the first visible one |
| 9515 | // which is o3. Therefore, z should not be defined on the prototype |
| 9516 | // object. |
| 9517 | Local<Value> proto = o0->Get(v8_str("__proto__")); |
| 9518 | CHECK(proto->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9519 | CHECK(proto.As<v8::Object>()->Get(v8_str("z"))->IsUndefined()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9523 | THREADED_TEST(HiddenPrototypeSet) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9524 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9525 | v8::HandleScope handle_scope(context->GetIsolate()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9526 | |
| 9527 | Local<v8::FunctionTemplate> ot = v8::FunctionTemplate::New(); |
| 9528 | Local<v8::FunctionTemplate> ht = v8::FunctionTemplate::New(); |
| 9529 | ht->SetHiddenPrototype(true); |
| 9530 | Local<v8::FunctionTemplate> pt = v8::FunctionTemplate::New(); |
| 9531 | ht->InstanceTemplate()->Set(v8_str("x"), v8_num(0)); |
| 9532 | |
| 9533 | Local<v8::Object> o = ot->GetFunction()->NewInstance(); |
| 9534 | Local<v8::Object> h = ht->GetFunction()->NewInstance(); |
| 9535 | Local<v8::Object> p = pt->GetFunction()->NewInstance(); |
| 9536 | o->Set(v8_str("__proto__"), h); |
| 9537 | h->Set(v8_str("__proto__"), p); |
| 9538 | |
| 9539 | // Setting a property that exists on the hidden prototype goes there. |
| 9540 | o->Set(v8_str("x"), v8_num(7)); |
| 9541 | CHECK_EQ(7, o->Get(v8_str("x"))->Int32Value()); |
| 9542 | CHECK_EQ(7, h->Get(v8_str("x"))->Int32Value()); |
| 9543 | CHECK(p->Get(v8_str("x"))->IsUndefined()); |
| 9544 | |
| 9545 | // Setting a new property should not be forwarded to the hidden prototype. |
| 9546 | o->Set(v8_str("y"), v8_num(6)); |
| 9547 | CHECK_EQ(6, o->Get(v8_str("y"))->Int32Value()); |
| 9548 | CHECK(h->Get(v8_str("y"))->IsUndefined()); |
| 9549 | CHECK(p->Get(v8_str("y"))->IsUndefined()); |
| 9550 | |
| 9551 | // Setting a property that only exists on a prototype of the hidden prototype |
| 9552 | // is treated normally again. |
| 9553 | p->Set(v8_str("z"), v8_num(8)); |
| 9554 | CHECK_EQ(8, o->Get(v8_str("z"))->Int32Value()); |
| 9555 | CHECK_EQ(8, h->Get(v8_str("z"))->Int32Value()); |
| 9556 | CHECK_EQ(8, p->Get(v8_str("z"))->Int32Value()); |
| 9557 | o->Set(v8_str("z"), v8_num(9)); |
| 9558 | CHECK_EQ(9, o->Get(v8_str("z"))->Int32Value()); |
| 9559 | CHECK_EQ(8, h->Get(v8_str("z"))->Int32Value()); |
| 9560 | CHECK_EQ(8, p->Get(v8_str("z"))->Int32Value()); |
| 9561 | } |
| 9562 | |
| 9563 | |
| 9564 | // Regression test for issue 2457. |
| 9565 | THREADED_TEST(HiddenPrototypeIdentityHash) { |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9566 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9567 | v8::HandleScope handle_scope(context->GetIsolate()); |
yangguo@chromium.org | a6bbcc8 | 2012-12-21 12:35:02 +0000 | [diff] [blame] | 9568 | |
| 9569 | Handle<FunctionTemplate> t = FunctionTemplate::New(); |
| 9570 | t->SetHiddenPrototype(true); |
| 9571 | t->InstanceTemplate()->Set(v8_str("foo"), v8_num(75)); |
| 9572 | Handle<Object> p = t->GetFunction()->NewInstance(); |
| 9573 | Handle<Object> o = Object::New(); |
| 9574 | o->SetPrototype(p); |
| 9575 | |
| 9576 | int hash = o->GetIdentityHash(); |
| 9577 | USE(hash); |
| 9578 | o->Set(v8_str("foo"), v8_num(42)); |
| 9579 | ASSERT_EQ(hash, o->GetIdentityHash()); |
| 9580 | } |
| 9581 | |
| 9582 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9583 | THREADED_TEST(SetPrototype) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9584 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9585 | v8::HandleScope handle_scope(context->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9586 | |
| 9587 | Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(); |
| 9588 | t0->InstanceTemplate()->Set(v8_str("x"), v8_num(0)); |
| 9589 | Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 9590 | t1->SetHiddenPrototype(true); |
| 9591 | t1->InstanceTemplate()->Set(v8_str("y"), v8_num(1)); |
| 9592 | Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 9593 | t2->SetHiddenPrototype(true); |
| 9594 | t2->InstanceTemplate()->Set(v8_str("z"), v8_num(2)); |
| 9595 | Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 9596 | t3->InstanceTemplate()->Set(v8_str("u"), v8_num(3)); |
| 9597 | |
| 9598 | Local<v8::Object> o0 = t0->GetFunction()->NewInstance(); |
| 9599 | Local<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 9600 | Local<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 9601 | Local<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 9602 | |
| 9603 | // Setting the prototype on an object does not skip hidden prototypes. |
| 9604 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9605 | CHECK(o0->SetPrototype(o1)); |
| 9606 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9607 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9608 | CHECK(o1->SetPrototype(o2)); |
| 9609 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9610 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9611 | CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 9612 | CHECK(o2->SetPrototype(o3)); |
| 9613 | CHECK_EQ(0, o0->Get(v8_str("x"))->Int32Value()); |
| 9614 | CHECK_EQ(1, o0->Get(v8_str("y"))->Int32Value()); |
| 9615 | CHECK_EQ(2, o0->Get(v8_str("z"))->Int32Value()); |
| 9616 | CHECK_EQ(3, o0->Get(v8_str("u"))->Int32Value()); |
| 9617 | |
| 9618 | // Getting the prototype of o0 should get the first visible one |
| 9619 | // which is o3. Therefore, z should not be defined on the prototype |
| 9620 | // object. |
| 9621 | Local<Value> proto = o0->Get(v8_str("__proto__")); |
| 9622 | CHECK(proto->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9623 | CHECK_EQ(proto.As<v8::Object>(), o3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9624 | |
| 9625 | // However, Object::GetPrototype ignores hidden prototype. |
| 9626 | Local<Value> proto0 = o0->GetPrototype(); |
| 9627 | CHECK(proto0->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9628 | CHECK_EQ(proto0.As<v8::Object>(), o1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9629 | |
| 9630 | Local<Value> proto1 = o1->GetPrototype(); |
| 9631 | CHECK(proto1->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9632 | CHECK_EQ(proto1.As<v8::Object>(), o2); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9633 | |
| 9634 | Local<Value> proto2 = o2->GetPrototype(); |
| 9635 | CHECK(proto2->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 9636 | CHECK_EQ(proto2.As<v8::Object>(), o3); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9637 | } |
| 9638 | |
| 9639 | |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 9640 | // Getting property names of an object with a prototype chain that |
| 9641 | // triggers dictionary elements in GetLocalPropertyNames() shouldn't |
| 9642 | // crash the runtime. |
| 9643 | THREADED_TEST(Regress91517) { |
| 9644 | i::FLAG_allow_natives_syntax = true; |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 9645 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9646 | v8::HandleScope handle_scope(context->GetIsolate()); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 9647 | |
| 9648 | Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 9649 | t1->SetHiddenPrototype(true); |
| 9650 | t1->InstanceTemplate()->Set(v8_str("foo"), v8_num(1)); |
| 9651 | Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
| 9652 | t2->SetHiddenPrototype(true); |
| 9653 | t2->InstanceTemplate()->Set(v8_str("fuz1"), v8_num(2)); |
| 9654 | t2->InstanceTemplate()->Set(v8_str("objects"), v8::Object::New()); |
| 9655 | t2->InstanceTemplate()->Set(v8_str("fuz2"), v8_num(2)); |
| 9656 | Local<v8::FunctionTemplate> t3 = v8::FunctionTemplate::New(); |
| 9657 | t3->SetHiddenPrototype(true); |
| 9658 | t3->InstanceTemplate()->Set(v8_str("boo"), v8_num(3)); |
| 9659 | Local<v8::FunctionTemplate> t4 = v8::FunctionTemplate::New(); |
| 9660 | t4->InstanceTemplate()->Set(v8_str("baz"), v8_num(4)); |
| 9661 | |
| 9662 | // Force dictionary-based properties. |
| 9663 | i::ScopedVector<char> name_buf(1024); |
| 9664 | for (int i = 1; i <= 1000; i++) { |
| 9665 | i::OS::SNPrintF(name_buf, "sdf%d", i); |
| 9666 | t2->InstanceTemplate()->Set(v8_str(name_buf.start()), v8_num(2)); |
| 9667 | } |
| 9668 | |
| 9669 | Local<v8::Object> o1 = t1->GetFunction()->NewInstance(); |
| 9670 | Local<v8::Object> o2 = t2->GetFunction()->NewInstance(); |
| 9671 | Local<v8::Object> o3 = t3->GetFunction()->NewInstance(); |
| 9672 | Local<v8::Object> o4 = t4->GetFunction()->NewInstance(); |
| 9673 | |
| 9674 | // Create prototype chain of hidden prototypes. |
| 9675 | CHECK(o4->SetPrototype(o3)); |
| 9676 | CHECK(o3->SetPrototype(o2)); |
| 9677 | CHECK(o2->SetPrototype(o1)); |
| 9678 | |
| 9679 | // Call the runtime version of GetLocalPropertyNames() on the natively |
| 9680 | // created object through JavaScript. |
| 9681 | context->Global()->Set(v8_str("obj"), o4); |
mstarzinger@chromium.org | f705b50 | 2013-04-04 11:38:09 +0000 | [diff] [blame] | 9682 | CompileRun("var names = %GetLocalPropertyNames(obj, true);"); |
ricow@chromium.org | 27bf288 | 2011-11-17 08:34:43 +0000 | [diff] [blame] | 9683 | |
| 9684 | ExpectInt32("names.length", 1006); |
| 9685 | ExpectTrue("names.indexOf(\"baz\") >= 0"); |
| 9686 | ExpectTrue("names.indexOf(\"boo\") >= 0"); |
| 9687 | ExpectTrue("names.indexOf(\"foo\") >= 0"); |
| 9688 | ExpectTrue("names.indexOf(\"fuz1\") >= 0"); |
| 9689 | ExpectTrue("names.indexOf(\"fuz2\") >= 0"); |
| 9690 | ExpectFalse("names[1005] == undefined"); |
| 9691 | } |
| 9692 | |
| 9693 | |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9694 | THREADED_TEST(FunctionReadOnlyPrototype) { |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9695 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9696 | v8::HandleScope handle_scope(context->GetIsolate()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9697 | |
| 9698 | Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9699 | t1->PrototypeTemplate()->Set(v8_str("x"), v8::Integer::New(42)); |
| 9700 | t1->ReadOnlyPrototype(); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9701 | context->Global()->Set(v8_str("func1"), t1->GetFunction()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9702 | // Configured value of ReadOnly flag. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9703 | CHECK(CompileRun( |
| 9704 | "(function() {" |
| 9705 | " descriptor = Object.getOwnPropertyDescriptor(func1, 'prototype');" |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9706 | " return (descriptor['writable'] == false);" |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9707 | "})()")->BooleanValue()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9708 | CHECK_EQ(42, CompileRun("func1.prototype.x")->Int32Value()); |
| 9709 | CHECK_EQ(42, |
| 9710 | CompileRun("func1.prototype = {}; func1.prototype.x")->Int32Value()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9711 | |
| 9712 | Local<v8::FunctionTemplate> t2 = v8::FunctionTemplate::New(); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9713 | t2->PrototypeTemplate()->Set(v8_str("x"), v8::Integer::New(42)); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9714 | context->Global()->Set(v8_str("func2"), t2->GetFunction()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9715 | // Default value of ReadOnly flag. |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9716 | CHECK(CompileRun( |
| 9717 | "(function() {" |
| 9718 | " descriptor = Object.getOwnPropertyDescriptor(func2, 'prototype');" |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9719 | " return (descriptor['writable'] == true);" |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9720 | "})()")->BooleanValue()); |
ricow@chromium.org | 2c99e28 | 2011-07-28 09:15:17 +0000 | [diff] [blame] | 9721 | CHECK_EQ(42, CompileRun("func2.prototype.x")->Int32Value()); |
ager@chromium.org | 04921a8 | 2011-06-27 13:21:41 +0000 | [diff] [blame] | 9722 | } |
| 9723 | |
| 9724 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9725 | THREADED_TEST(SetPrototypeThrows) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9726 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9727 | v8::HandleScope handle_scope(context->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9728 | |
| 9729 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 9730 | |
| 9731 | Local<v8::Object> o0 = t->GetFunction()->NewInstance(); |
| 9732 | Local<v8::Object> o1 = t->GetFunction()->NewInstance(); |
| 9733 | |
| 9734 | CHECK(o0->SetPrototype(o1)); |
| 9735 | // If setting the prototype leads to the cycle, SetPrototype should |
| 9736 | // return false and keep VM in sane state. |
| 9737 | v8::TryCatch try_catch; |
| 9738 | CHECK(!o1->SetPrototype(o0)); |
| 9739 | CHECK(!try_catch.HasCaught()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 9740 | ASSERT(!CcTest::i_isolate()->has_pending_exception()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 9741 | |
| 9742 | CHECK_EQ(42, CompileRun("function f() { return 42; }; f()")->Int32Value()); |
| 9743 | } |
| 9744 | |
| 9745 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 9746 | THREADED_TEST(FunctionRemovePrototype) { |
| 9747 | LocalContext context; |
| 9748 | v8::HandleScope handle_scope(context->GetIsolate()); |
| 9749 | |
| 9750 | Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(); |
| 9751 | t1->RemovePrototype(); |
| 9752 | Local<v8::Function> fun = t1->GetFunction(); |
| 9753 | context->Global()->Set(v8_str("fun"), fun); |
| 9754 | CHECK(!CompileRun("'prototype' in fun")->BooleanValue()); |
| 9755 | |
| 9756 | v8::TryCatch try_catch; |
| 9757 | CompileRun("new fun()"); |
| 9758 | CHECK(try_catch.HasCaught()); |
| 9759 | |
| 9760 | try_catch.Reset(); |
| 9761 | fun->NewInstance(); |
| 9762 | CHECK(try_catch.HasCaught()); |
| 9763 | } |
| 9764 | |
| 9765 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9766 | THREADED_TEST(GetterSetterExceptions) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9767 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9768 | v8::HandleScope handle_scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9769 | CompileRun( |
| 9770 | "function Foo() { };" |
| 9771 | "function Throw() { throw 5; };" |
| 9772 | "var x = { };" |
| 9773 | "x.__defineSetter__('set', Throw);" |
| 9774 | "x.__defineGetter__('get', Throw);"); |
| 9775 | Local<v8::Object> x = |
| 9776 | Local<v8::Object>::Cast(context->Global()->Get(v8_str("x"))); |
| 9777 | v8::TryCatch try_catch; |
| 9778 | x->Set(v8_str("set"), v8::Integer::New(8)); |
| 9779 | x->Get(v8_str("get")); |
| 9780 | x->Set(v8_str("set"), v8::Integer::New(8)); |
| 9781 | x->Get(v8_str("get")); |
| 9782 | x->Set(v8_str("set"), v8::Integer::New(8)); |
| 9783 | x->Get(v8_str("get")); |
| 9784 | x->Set(v8_str("set"), v8::Integer::New(8)); |
| 9785 | x->Get(v8_str("get")); |
| 9786 | } |
| 9787 | |
| 9788 | |
| 9789 | THREADED_TEST(Constructor) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9790 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 9791 | v8::HandleScope handle_scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9792 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 9793 | templ->SetClassName(v8_str("Fun")); |
| 9794 | Local<Function> cons = templ->GetFunction(); |
| 9795 | context->Global()->Set(v8_str("Fun"), cons); |
| 9796 | Local<v8::Object> inst = cons->NewInstance(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 9797 | i::Handle<i::JSObject> obj(v8::Utils::OpenHandle(*inst)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 9798 | CHECK(obj->IsJSObject()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 9799 | Local<Value> value = CompileRun("(new Fun()).constructor === Fun"); |
| 9800 | CHECK(value->BooleanValue()); |
| 9801 | } |
| 9802 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9803 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9804 | static void ConstructorCallback( |
| 9805 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9806 | ApiTestFuzzer::Fuzz(); |
| 9807 | Local<Object> This; |
| 9808 | |
| 9809 | if (args.IsConstructCall()) { |
| 9810 | Local<Object> Holder = args.Holder(); |
| 9811 | This = Object::New(); |
| 9812 | Local<Value> proto = Holder->GetPrototype(); |
| 9813 | if (proto->IsObject()) { |
| 9814 | This->SetPrototype(proto); |
| 9815 | } |
| 9816 | } else { |
| 9817 | This = args.This(); |
| 9818 | } |
| 9819 | |
| 9820 | This->Set(v8_str("a"), args[0]); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9821 | args.GetReturnValue().Set(This); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9822 | } |
| 9823 | |
| 9824 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9825 | static void FakeConstructorCallback( |
| 9826 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9827 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 9828 | args.GetReturnValue().Set(args[0]); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9829 | } |
| 9830 | |
| 9831 | |
| 9832 | THREADED_TEST(ConstructorForObject) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9833 | LocalContext context; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 9834 | v8::Isolate* isolate = context->GetIsolate(); |
| 9835 | v8::HandleScope handle_scope(isolate); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9836 | |
| 9837 | { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 9838 | instance_template->SetCallAsFunctionHandler(ConstructorCallback); |
| 9839 | Local<Object> instance = instance_template->NewInstance(); |
| 9840 | context->Global()->Set(v8_str("obj"), instance); |
| 9841 | v8::TryCatch try_catch; |
| 9842 | Local<Value> value; |
| 9843 | CHECK(!try_catch.HasCaught()); |
| 9844 | |
| 9845 | // Call the Object's constructor with a 32-bit signed integer. |
| 9846 | value = CompileRun("(function() { var o = new obj(28); return o.a; })()"); |
| 9847 | CHECK(!try_catch.HasCaught()); |
| 9848 | CHECK(value->IsInt32()); |
| 9849 | CHECK_EQ(28, value->Int32Value()); |
| 9850 | |
| 9851 | Local<Value> args1[] = { v8_num(28) }; |
| 9852 | Local<Value> value_obj1 = instance->CallAsConstructor(1, args1); |
| 9853 | CHECK(value_obj1->IsObject()); |
| 9854 | Local<Object> object1 = Local<Object>::Cast(value_obj1); |
| 9855 | value = object1->Get(v8_str("a")); |
| 9856 | CHECK(value->IsInt32()); |
| 9857 | CHECK(!try_catch.HasCaught()); |
| 9858 | CHECK_EQ(28, value->Int32Value()); |
| 9859 | |
| 9860 | // Call the Object's constructor with a String. |
| 9861 | value = CompileRun( |
| 9862 | "(function() { var o = new obj('tipli'); return o.a; })()"); |
| 9863 | CHECK(!try_catch.HasCaught()); |
| 9864 | CHECK(value->IsString()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9865 | String::Utf8Value string_value1(value->ToString()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9866 | CHECK_EQ("tipli", *string_value1); |
| 9867 | |
| 9868 | Local<Value> args2[] = { v8_str("tipli") }; |
| 9869 | Local<Value> value_obj2 = instance->CallAsConstructor(1, args2); |
| 9870 | CHECK(value_obj2->IsObject()); |
| 9871 | Local<Object> object2 = Local<Object>::Cast(value_obj2); |
| 9872 | value = object2->Get(v8_str("a")); |
| 9873 | CHECK(!try_catch.HasCaught()); |
| 9874 | CHECK(value->IsString()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9875 | String::Utf8Value string_value2(value->ToString()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9876 | CHECK_EQ("tipli", *string_value2); |
| 9877 | |
| 9878 | // Call the Object's constructor with a Boolean. |
| 9879 | value = CompileRun("(function() { var o = new obj(true); return o.a; })()"); |
| 9880 | CHECK(!try_catch.HasCaught()); |
| 9881 | CHECK(value->IsBoolean()); |
| 9882 | CHECK_EQ(true, value->BooleanValue()); |
| 9883 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 9884 | Handle<Value> args3[] = { v8::True(isolate) }; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9885 | Local<Value> value_obj3 = instance->CallAsConstructor(1, args3); |
| 9886 | CHECK(value_obj3->IsObject()); |
| 9887 | Local<Object> object3 = Local<Object>::Cast(value_obj3); |
| 9888 | value = object3->Get(v8_str("a")); |
| 9889 | CHECK(!try_catch.HasCaught()); |
| 9890 | CHECK(value->IsBoolean()); |
| 9891 | CHECK_EQ(true, value->BooleanValue()); |
| 9892 | |
| 9893 | // Call the Object's constructor with undefined. |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 9894 | Handle<Value> args4[] = { v8::Undefined(isolate) }; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9895 | Local<Value> value_obj4 = instance->CallAsConstructor(1, args4); |
| 9896 | CHECK(value_obj4->IsObject()); |
| 9897 | Local<Object> object4 = Local<Object>::Cast(value_obj4); |
| 9898 | value = object4->Get(v8_str("a")); |
| 9899 | CHECK(!try_catch.HasCaught()); |
| 9900 | CHECK(value->IsUndefined()); |
| 9901 | |
| 9902 | // Call the Object's constructor with null. |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 9903 | Handle<Value> args5[] = { v8::Null(isolate) }; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9904 | Local<Value> value_obj5 = instance->CallAsConstructor(1, args5); |
| 9905 | CHECK(value_obj5->IsObject()); |
| 9906 | Local<Object> object5 = Local<Object>::Cast(value_obj5); |
| 9907 | value = object5->Get(v8_str("a")); |
| 9908 | CHECK(!try_catch.HasCaught()); |
| 9909 | CHECK(value->IsNull()); |
| 9910 | } |
| 9911 | |
| 9912 | // Check exception handling when there is no constructor set for the Object. |
| 9913 | { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 9914 | Local<Object> instance = instance_template->NewInstance(); |
| 9915 | context->Global()->Set(v8_str("obj2"), instance); |
| 9916 | v8::TryCatch try_catch; |
| 9917 | Local<Value> value; |
| 9918 | CHECK(!try_catch.HasCaught()); |
| 9919 | |
| 9920 | value = CompileRun("new obj2(28)"); |
| 9921 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9922 | String::Utf8Value exception_value1(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9923 | CHECK_EQ("TypeError: object is not a function", *exception_value1); |
| 9924 | try_catch.Reset(); |
| 9925 | |
| 9926 | Local<Value> args[] = { v8_num(29) }; |
| 9927 | value = instance->CallAsConstructor(1, args); |
| 9928 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9929 | String::Utf8Value exception_value2(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9930 | CHECK_EQ("TypeError: #<Object> is not a function", *exception_value2); |
| 9931 | try_catch.Reset(); |
| 9932 | } |
| 9933 | |
| 9934 | // Check the case when constructor throws exception. |
| 9935 | { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 9936 | instance_template->SetCallAsFunctionHandler(ThrowValue); |
| 9937 | Local<Object> instance = instance_template->NewInstance(); |
| 9938 | context->Global()->Set(v8_str("obj3"), instance); |
| 9939 | v8::TryCatch try_catch; |
| 9940 | Local<Value> value; |
| 9941 | CHECK(!try_catch.HasCaught()); |
| 9942 | |
| 9943 | value = CompileRun("new obj3(22)"); |
| 9944 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9945 | String::Utf8Value exception_value1(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9946 | CHECK_EQ("22", *exception_value1); |
| 9947 | try_catch.Reset(); |
| 9948 | |
| 9949 | Local<Value> args[] = { v8_num(23) }; |
| 9950 | value = instance->CallAsConstructor(1, args); |
| 9951 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 9952 | String::Utf8Value exception_value2(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 9953 | CHECK_EQ("23", *exception_value2); |
| 9954 | try_catch.Reset(); |
| 9955 | } |
| 9956 | |
| 9957 | // Check whether constructor returns with an object or non-object. |
| 9958 | { Local<FunctionTemplate> function_template = |
| 9959 | FunctionTemplate::New(FakeConstructorCallback); |
| 9960 | Local<Function> function = function_template->GetFunction(); |
| 9961 | Local<Object> instance1 = function; |
| 9962 | context->Global()->Set(v8_str("obj4"), instance1); |
| 9963 | v8::TryCatch try_catch; |
| 9964 | Local<Value> value; |
| 9965 | CHECK(!try_catch.HasCaught()); |
| 9966 | |
| 9967 | CHECK(instance1->IsObject()); |
| 9968 | CHECK(instance1->IsFunction()); |
| 9969 | |
| 9970 | value = CompileRun("new obj4(28)"); |
| 9971 | CHECK(!try_catch.HasCaught()); |
| 9972 | CHECK(value->IsObject()); |
| 9973 | |
| 9974 | Local<Value> args1[] = { v8_num(28) }; |
| 9975 | value = instance1->CallAsConstructor(1, args1); |
| 9976 | CHECK(!try_catch.HasCaught()); |
| 9977 | CHECK(value->IsObject()); |
| 9978 | |
| 9979 | Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 9980 | instance_template->SetCallAsFunctionHandler(FakeConstructorCallback); |
| 9981 | Local<Object> instance2 = instance_template->NewInstance(); |
| 9982 | context->Global()->Set(v8_str("obj5"), instance2); |
| 9983 | CHECK(!try_catch.HasCaught()); |
| 9984 | |
| 9985 | CHECK(instance2->IsObject()); |
| 9986 | CHECK(!instance2->IsFunction()); |
| 9987 | |
| 9988 | value = CompileRun("new obj5(28)"); |
| 9989 | CHECK(!try_catch.HasCaught()); |
| 9990 | CHECK(!value->IsObject()); |
| 9991 | |
| 9992 | Local<Value> args2[] = { v8_num(28) }; |
| 9993 | value = instance2->CallAsConstructor(1, args2); |
| 9994 | CHECK(!try_catch.HasCaught()); |
| 9995 | CHECK(!value->IsObject()); |
| 9996 | } |
| 9997 | } |
| 9998 | |
| 9999 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10000 | THREADED_TEST(FunctionDescriptorException) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10001 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10002 | v8::HandleScope handle_scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10003 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 10004 | templ->SetClassName(v8_str("Fun")); |
| 10005 | Local<Function> cons = templ->GetFunction(); |
| 10006 | context->Global()->Set(v8_str("Fun"), cons); |
| 10007 | Local<Value> value = CompileRun( |
| 10008 | "function test() {" |
| 10009 | " try {" |
| 10010 | " (new Fun()).blah()" |
| 10011 | " } catch (e) {" |
| 10012 | " var str = String(e);" |
| 10013 | " if (str.indexOf('TypeError') == -1) return 1;" |
| 10014 | " if (str.indexOf('[object Fun]') != -1) return 2;" |
whesse@chromium.org | 7a392b3 | 2011-01-31 11:30:36 +0000 | [diff] [blame] | 10015 | " if (str.indexOf('#<Fun>') == -1) return 3;" |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10016 | " return 0;" |
| 10017 | " }" |
| 10018 | " return 4;" |
| 10019 | "}" |
| 10020 | "test();"); |
| 10021 | CHECK_EQ(0, value->Int32Value()); |
| 10022 | } |
| 10023 | |
| 10024 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10025 | THREADED_TEST(EvalAliasedDynamic) { |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10026 | LocalContext current; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10027 | v8::HandleScope scope(current->GetIsolate()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10028 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10029 | // Tests where aliased eval can only be resolved dynamically. |
| 10030 | Local<Script> script = |
| 10031 | Script::Compile(v8_str("function f(x) { " |
| 10032 | " var foo = 2;" |
| 10033 | " with (x) { return eval('foo'); }" |
| 10034 | "}" |
| 10035 | "foo = 0;" |
| 10036 | "result1 = f(new Object());" |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10037 | "result2 = f(this);" |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10038 | "var x = new Object();" |
| 10039 | "x.eval = function(x) { return 1; };" |
| 10040 | "result3 = f(x);")); |
| 10041 | script->Run(); |
| 10042 | CHECK_EQ(2, current->Global()->Get(v8_str("result1"))->Int32Value()); |
| 10043 | CHECK_EQ(0, current->Global()->Get(v8_str("result2"))->Int32Value()); |
| 10044 | CHECK_EQ(1, current->Global()->Get(v8_str("result3"))->Int32Value()); |
| 10045 | |
| 10046 | v8::TryCatch try_catch; |
| 10047 | script = |
| 10048 | Script::Compile(v8_str("function f(x) { " |
| 10049 | " var bar = 2;" |
| 10050 | " with (x) { return eval('bar'); }" |
| 10051 | "}" |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10052 | "result4 = f(this)")); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10053 | script->Run(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10054 | CHECK(!try_catch.HasCaught()); |
| 10055 | CHECK_EQ(2, current->Global()->Get(v8_str("result4"))->Int32Value()); |
| 10056 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10057 | try_catch.Reset(); |
| 10058 | } |
| 10059 | |
| 10060 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10061 | THREADED_TEST(CrossEval) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10062 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10063 | LocalContext other; |
| 10064 | LocalContext current; |
| 10065 | |
| 10066 | Local<String> token = v8_str("<security token>"); |
| 10067 | other->SetSecurityToken(token); |
| 10068 | current->SetSecurityToken(token); |
| 10069 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10070 | // Set up reference from current to other. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10071 | current->Global()->Set(v8_str("other"), other->Global()); |
| 10072 | |
| 10073 | // Check that new variables are introduced in other context. |
| 10074 | Local<Script> script = |
| 10075 | Script::Compile(v8_str("other.eval('var foo = 1234')")); |
| 10076 | script->Run(); |
| 10077 | Local<Value> foo = other->Global()->Get(v8_str("foo")); |
| 10078 | CHECK_EQ(1234, foo->Int32Value()); |
| 10079 | CHECK(!current->Global()->Has(v8_str("foo"))); |
| 10080 | |
| 10081 | // Check that writing to non-existing properties introduces them in |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10082 | // the other context. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10083 | script = |
| 10084 | Script::Compile(v8_str("other.eval('na = 1234')")); |
| 10085 | script->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10086 | CHECK_EQ(1234, other->Global()->Get(v8_str("na"))->Int32Value()); |
| 10087 | CHECK(!current->Global()->Has(v8_str("na"))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10088 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10089 | // Check that global variables in current context are not visible in other |
| 10090 | // context. |
| 10091 | v8::TryCatch try_catch; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10092 | script = |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10093 | Script::Compile(v8_str("var bar = 42; other.eval('bar');")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10094 | Local<Value> result = script->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10095 | CHECK(try_catch.HasCaught()); |
| 10096 | try_catch.Reset(); |
| 10097 | |
| 10098 | // Check that local variables in current context are not visible in other |
| 10099 | // context. |
| 10100 | script = |
| 10101 | Script::Compile(v8_str("(function() { " |
| 10102 | " var baz = 87;" |
| 10103 | " return other.eval('baz');" |
| 10104 | "})();")); |
| 10105 | result = script->Run(); |
| 10106 | CHECK(try_catch.HasCaught()); |
| 10107 | try_catch.Reset(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10108 | |
| 10109 | // Check that global variables in the other environment are visible |
| 10110 | // when evaluting code. |
| 10111 | other->Global()->Set(v8_str("bis"), v8_num(1234)); |
| 10112 | script = Script::Compile(v8_str("other.eval('bis')")); |
| 10113 | CHECK_EQ(1234, script->Run()->Int32Value()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10114 | CHECK(!try_catch.HasCaught()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10115 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10116 | // Check that the 'this' pointer points to the global object evaluating |
| 10117 | // code. |
| 10118 | other->Global()->Set(v8_str("t"), other->Global()); |
| 10119 | script = Script::Compile(v8_str("other.eval('this == t')")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10120 | result = script->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10121 | CHECK(result->IsTrue()); |
| 10122 | CHECK(!try_catch.HasCaught()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10123 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10124 | // Check that variables introduced in with-statement are not visible in |
| 10125 | // other context. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10126 | script = |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10127 | Script::Compile(v8_str("with({x:2}){other.eval('x')}")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10128 | result = script->Run(); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 10129 | CHECK(try_catch.HasCaught()); |
| 10130 | try_catch.Reset(); |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10131 | |
| 10132 | // Check that you cannot use 'eval.call' with another object than the |
| 10133 | // current global object. |
ager@chromium.org | 3bf7b91 | 2008-11-17 09:09:45 +0000 | [diff] [blame] | 10134 | script = |
| 10135 | Script::Compile(v8_str("other.y = 1; eval.call(other, 'y')")); |
| 10136 | result = script->Run(); |
| 10137 | CHECK(try_catch.HasCaught()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10138 | } |
| 10139 | |
| 10140 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10141 | // Test that calling eval in a context which has been detached from |
| 10142 | // its global throws an exception. This behavior is consistent with |
| 10143 | // other JavaScript implementations. |
| 10144 | THREADED_TEST(EvalInDetachedGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10145 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10146 | v8::HandleScope scope(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10147 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 10148 | v8::Local<Context> context0 = Context::New(isolate); |
| 10149 | v8::Local<Context> context1 = Context::New(isolate); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10150 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10151 | // Set up function in context0 that uses eval from context0. |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10152 | context0->Enter(); |
| 10153 | v8::Handle<v8::Value> fun = |
| 10154 | CompileRun("var x = 42;" |
| 10155 | "(function() {" |
| 10156 | " var e = eval;" |
| 10157 | " return function(s) { return e(s); }" |
| 10158 | "})()"); |
| 10159 | context0->Exit(); |
| 10160 | |
| 10161 | // Put the function into context1 and call it before and after |
| 10162 | // detaching the global. Before detaching, the call succeeds and |
| 10163 | // after detaching and exception is thrown. |
| 10164 | context1->Enter(); |
| 10165 | context1->Global()->Set(v8_str("fun"), fun); |
| 10166 | v8::Handle<v8::Value> x_value = CompileRun("fun('x')"); |
| 10167 | CHECK_EQ(42, x_value->Int32Value()); |
| 10168 | context0->DetachGlobal(); |
| 10169 | v8::TryCatch catcher; |
| 10170 | x_value = CompileRun("fun('x')"); |
| 10171 | CHECK(x_value.IsEmpty()); |
| 10172 | CHECK(catcher.HasCaught()); |
| 10173 | context1->Exit(); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10174 | } |
| 10175 | |
| 10176 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10177 | THREADED_TEST(CrossLazyLoad) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10178 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10179 | LocalContext other; |
| 10180 | LocalContext current; |
| 10181 | |
| 10182 | Local<String> token = v8_str("<security token>"); |
| 10183 | other->SetSecurityToken(token); |
| 10184 | current->SetSecurityToken(token); |
| 10185 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 10186 | // Set up reference from current to other. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10187 | current->Global()->Set(v8_str("other"), other->Global()); |
| 10188 | |
| 10189 | // Trigger lazy loading in other context. |
| 10190 | Local<Script> script = |
| 10191 | Script::Compile(v8_str("other.eval('new Date(42)')")); |
| 10192 | Local<Value> value = script->Run(); |
| 10193 | CHECK_EQ(42.0, value->NumberValue()); |
| 10194 | } |
| 10195 | |
| 10196 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10197 | static void call_as_function(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10198 | ApiTestFuzzer::Fuzz(); |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 10199 | if (args.IsConstructCall()) { |
| 10200 | if (args[0]->IsInt32()) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10201 | args.GetReturnValue().Set(v8_num(-args[0]->Int32Value())); |
| 10202 | return; |
sgjesse@chromium.org | 05521fc | 2009-05-21 07:37:44 +0000 | [diff] [blame] | 10203 | } |
| 10204 | } |
| 10205 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10206 | args.GetReturnValue().Set(args[0]); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10207 | } |
| 10208 | |
| 10209 | |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 10210 | static void ReturnThis(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 10211 | args.GetReturnValue().Set(args.This()); |
| 10212 | } |
| 10213 | |
| 10214 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10215 | // Test that a call handler can be set for objects which will allow |
| 10216 | // non-function objects created through the API to be called as |
| 10217 | // functions. |
| 10218 | THREADED_TEST(CallAsFunction) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10219 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10220 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10221 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10222 | { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 10223 | Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 10224 | instance_template->SetCallAsFunctionHandler(call_as_function); |
| 10225 | Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 10226 | context->Global()->Set(v8_str("obj"), instance); |
| 10227 | v8::TryCatch try_catch; |
| 10228 | Local<Value> value; |
| 10229 | CHECK(!try_catch.HasCaught()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10230 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10231 | value = CompileRun("obj(42)"); |
| 10232 | CHECK(!try_catch.HasCaught()); |
| 10233 | CHECK_EQ(42, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10234 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10235 | value = CompileRun("(function(o){return o(49)})(obj)"); |
| 10236 | CHECK(!try_catch.HasCaught()); |
| 10237 | CHECK_EQ(49, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10238 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10239 | // test special case of call as function |
| 10240 | value = CompileRun("[obj]['0'](45)"); |
| 10241 | CHECK(!try_catch.HasCaught()); |
| 10242 | CHECK_EQ(45, value->Int32Value()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 10243 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10244 | value = CompileRun("obj.call = Function.prototype.call;" |
| 10245 | "obj.call(null, 87)"); |
| 10246 | CHECK(!try_catch.HasCaught()); |
| 10247 | CHECK_EQ(87, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10248 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10249 | // Regression tests for bug #1116356: Calling call through call/apply |
| 10250 | // must work for non-function receivers. |
| 10251 | const char* apply_99 = "Function.prototype.call.apply(obj, [this, 99])"; |
| 10252 | value = CompileRun(apply_99); |
| 10253 | CHECK(!try_catch.HasCaught()); |
| 10254 | CHECK_EQ(99, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10255 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10256 | const char* call_17 = "Function.prototype.call.call(obj, this, 17)"; |
| 10257 | value = CompileRun(call_17); |
| 10258 | CHECK(!try_catch.HasCaught()); |
| 10259 | CHECK_EQ(17, value->Int32Value()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10260 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10261 | // Check that the call-as-function handler can be called through |
| 10262 | // new. |
| 10263 | value = CompileRun("new obj(43)"); |
| 10264 | CHECK(!try_catch.HasCaught()); |
| 10265 | CHECK_EQ(-43, value->Int32Value()); |
| 10266 | |
| 10267 | // Check that the call-as-function handler can be called through |
| 10268 | // the API. |
| 10269 | v8::Handle<Value> args[] = { v8_num(28) }; |
| 10270 | value = instance->CallAsFunction(instance, 1, args); |
| 10271 | CHECK(!try_catch.HasCaught()); |
| 10272 | CHECK_EQ(28, value->Int32Value()); |
| 10273 | } |
| 10274 | |
| 10275 | { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10276 | Local<ObjectTemplate> instance_template(t->InstanceTemplate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10277 | USE(instance_template); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10278 | Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 10279 | context->Global()->Set(v8_str("obj2"), instance); |
| 10280 | v8::TryCatch try_catch; |
| 10281 | Local<Value> value; |
| 10282 | CHECK(!try_catch.HasCaught()); |
| 10283 | |
| 10284 | // Call an object without call-as-function handler through the JS |
| 10285 | value = CompileRun("obj2(28)"); |
| 10286 | CHECK(value.IsEmpty()); |
| 10287 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10288 | String::Utf8Value exception_value1(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10289 | CHECK_EQ("TypeError: Property 'obj2' of object #<Object> is not a function", |
| 10290 | *exception_value1); |
| 10291 | try_catch.Reset(); |
| 10292 | |
| 10293 | // Call an object without call-as-function handler through the API |
| 10294 | value = CompileRun("obj2(28)"); |
| 10295 | v8::Handle<Value> args[] = { v8_num(28) }; |
| 10296 | value = instance->CallAsFunction(instance, 1, args); |
| 10297 | CHECK(value.IsEmpty()); |
| 10298 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10299 | String::Utf8Value exception_value2(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10300 | CHECK_EQ("TypeError: [object Object] is not a function", *exception_value2); |
| 10301 | try_catch.Reset(); |
| 10302 | } |
| 10303 | |
| 10304 | { Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 10305 | Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 10306 | instance_template->SetCallAsFunctionHandler(ThrowValue); |
| 10307 | Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 10308 | context->Global()->Set(v8_str("obj3"), instance); |
| 10309 | v8::TryCatch try_catch; |
| 10310 | Local<Value> value; |
| 10311 | CHECK(!try_catch.HasCaught()); |
| 10312 | |
| 10313 | // Catch the exception which is thrown by call-as-function handler |
| 10314 | value = CompileRun("obj3(22)"); |
| 10315 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10316 | String::Utf8Value exception_value1(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10317 | CHECK_EQ("22", *exception_value1); |
| 10318 | try_catch.Reset(); |
| 10319 | |
| 10320 | v8::Handle<Value> args[] = { v8_num(23) }; |
| 10321 | value = instance->CallAsFunction(instance, 1, args); |
| 10322 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 10323 | String::Utf8Value exception_value2(try_catch.Exception()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 10324 | CHECK_EQ("23", *exception_value2); |
| 10325 | try_catch.Reset(); |
| 10326 | } |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 10327 | |
| 10328 | { v8::Isolate* isolate = context->GetIsolate(); |
| 10329 | Local<v8::FunctionTemplate> t = v8::FunctionTemplate::New(); |
| 10330 | Local<ObjectTemplate> instance_template = t->InstanceTemplate(); |
| 10331 | instance_template->SetCallAsFunctionHandler(ReturnThis); |
| 10332 | Local<v8::Object> instance = t->GetFunction()->NewInstance(); |
| 10333 | |
| 10334 | Local<v8::Value> a1 = |
| 10335 | instance->CallAsFunction(v8::Undefined(isolate), 0, NULL); |
| 10336 | CHECK(a1->StrictEquals(instance)); |
| 10337 | Local<v8::Value> a2 = |
| 10338 | instance->CallAsFunction(v8::Null(isolate), 0, NULL); |
| 10339 | CHECK(a2->StrictEquals(instance)); |
| 10340 | Local<v8::Value> a3 = |
| 10341 | instance->CallAsFunction(v8_num(42), 0, NULL); |
| 10342 | CHECK(a3->StrictEquals(instance)); |
| 10343 | Local<v8::Value> a4 = |
| 10344 | instance->CallAsFunction(v8_str("hello"), 0, NULL); |
| 10345 | CHECK(a4->StrictEquals(instance)); |
| 10346 | Local<v8::Value> a5 = |
| 10347 | instance->CallAsFunction(v8::True(isolate), 0, NULL); |
| 10348 | CHECK(a5->StrictEquals(instance)); |
| 10349 | } |
| 10350 | |
| 10351 | { v8::Isolate* isolate = context->GetIsolate(); |
| 10352 | CompileRun( |
| 10353 | "function ReturnThisSloppy() {" |
| 10354 | " return this;" |
| 10355 | "}" |
| 10356 | "function ReturnThisStrict() {" |
| 10357 | " 'use strict';" |
| 10358 | " return this;" |
| 10359 | "}"); |
| 10360 | Local<Function> ReturnThisSloppy = |
| 10361 | Local<Function>::Cast( |
| 10362 | context->Global()->Get(v8_str("ReturnThisSloppy"))); |
| 10363 | Local<Function> ReturnThisStrict = |
| 10364 | Local<Function>::Cast( |
| 10365 | context->Global()->Get(v8_str("ReturnThisStrict"))); |
| 10366 | |
| 10367 | Local<v8::Value> a1 = |
| 10368 | ReturnThisSloppy->CallAsFunction(v8::Undefined(isolate), 0, NULL); |
| 10369 | CHECK(a1->StrictEquals(context->Global())); |
| 10370 | Local<v8::Value> a2 = |
| 10371 | ReturnThisSloppy->CallAsFunction(v8::Null(isolate), 0, NULL); |
| 10372 | CHECK(a2->StrictEquals(context->Global())); |
| 10373 | Local<v8::Value> a3 = |
| 10374 | ReturnThisSloppy->CallAsFunction(v8_num(42), 0, NULL); |
| 10375 | CHECK(a3->IsNumberObject()); |
| 10376 | CHECK_EQ(42.0, a3.As<v8::NumberObject>()->ValueOf()); |
| 10377 | Local<v8::Value> a4 = |
| 10378 | ReturnThisSloppy->CallAsFunction(v8_str("hello"), 0, NULL); |
| 10379 | CHECK(a4->IsStringObject()); |
| 10380 | CHECK(a4.As<v8::StringObject>()->ValueOf()->StrictEquals(v8_str("hello"))); |
| 10381 | Local<v8::Value> a5 = |
| 10382 | ReturnThisSloppy->CallAsFunction(v8::True(isolate), 0, NULL); |
| 10383 | CHECK(a5->IsBooleanObject()); |
| 10384 | CHECK(a5.As<v8::BooleanObject>()->ValueOf()); |
| 10385 | |
| 10386 | Local<v8::Value> a6 = |
| 10387 | ReturnThisStrict->CallAsFunction(v8::Undefined(isolate), 0, NULL); |
| 10388 | CHECK(a6->IsUndefined()); |
| 10389 | Local<v8::Value> a7 = |
| 10390 | ReturnThisStrict->CallAsFunction(v8::Null(isolate), 0, NULL); |
| 10391 | CHECK(a7->IsNull()); |
| 10392 | Local<v8::Value> a8 = |
| 10393 | ReturnThisStrict->CallAsFunction(v8_num(42), 0, NULL); |
| 10394 | CHECK(a8->StrictEquals(v8_num(42))); |
| 10395 | Local<v8::Value> a9 = |
| 10396 | ReturnThisStrict->CallAsFunction(v8_str("hello"), 0, NULL); |
| 10397 | CHECK(a9->StrictEquals(v8_str("hello"))); |
| 10398 | Local<v8::Value> a10 = |
| 10399 | ReturnThisStrict->CallAsFunction(v8::True(isolate), 0, NULL); |
| 10400 | CHECK(a10->StrictEquals(v8::True(isolate))); |
| 10401 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10402 | } |
| 10403 | |
| 10404 | |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 10405 | // Check whether a non-function object is callable. |
| 10406 | THREADED_TEST(CallableObject) { |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 10407 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10408 | v8::HandleScope scope(context->GetIsolate()); |
karlklose@chromium.org | 83a4728 | 2011-05-11 11:54:09 +0000 | [diff] [blame] | 10409 | |
| 10410 | { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 10411 | instance_template->SetCallAsFunctionHandler(call_as_function); |
| 10412 | Local<Object> instance = instance_template->NewInstance(); |
| 10413 | v8::TryCatch try_catch; |
| 10414 | |
| 10415 | CHECK(instance->IsCallable()); |
| 10416 | CHECK(!try_catch.HasCaught()); |
| 10417 | } |
| 10418 | |
| 10419 | { Local<ObjectTemplate> instance_template = ObjectTemplate::New(); |
| 10420 | Local<Object> instance = instance_template->NewInstance(); |
| 10421 | v8::TryCatch try_catch; |
| 10422 | |
| 10423 | CHECK(!instance->IsCallable()); |
| 10424 | CHECK(!try_catch.HasCaught()); |
| 10425 | } |
| 10426 | |
| 10427 | { Local<FunctionTemplate> function_template = |
| 10428 | FunctionTemplate::New(call_as_function); |
| 10429 | Local<Function> function = function_template->GetFunction(); |
| 10430 | Local<Object> instance = function; |
| 10431 | v8::TryCatch try_catch; |
| 10432 | |
| 10433 | CHECK(instance->IsCallable()); |
| 10434 | CHECK(!try_catch.HasCaught()); |
| 10435 | } |
| 10436 | |
| 10437 | { Local<FunctionTemplate> function_template = FunctionTemplate::New(); |
| 10438 | Local<Function> function = function_template->GetFunction(); |
| 10439 | Local<Object> instance = function; |
| 10440 | v8::TryCatch try_catch; |
| 10441 | |
| 10442 | CHECK(instance->IsCallable()); |
| 10443 | CHECK(!try_catch.HasCaught()); |
| 10444 | } |
| 10445 | } |
| 10446 | |
| 10447 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10448 | static int CountHandles() { |
| 10449 | return v8::HandleScope::NumberOfHandles(); |
| 10450 | } |
| 10451 | |
| 10452 | |
| 10453 | static int Recurse(int depth, int iterations) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10454 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10455 | if (depth == 0) return CountHandles(); |
| 10456 | for (int i = 0; i < iterations; i++) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10457 | Local<v8::Number> n(v8::Integer::New(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10458 | } |
| 10459 | return Recurse(depth - 1, iterations); |
| 10460 | } |
| 10461 | |
| 10462 | |
| 10463 | THREADED_TEST(HandleIteration) { |
| 10464 | static const int kIterations = 500; |
| 10465 | static const int kNesting = 200; |
| 10466 | CHECK_EQ(0, CountHandles()); |
| 10467 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10468 | v8::HandleScope scope1(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10469 | CHECK_EQ(0, CountHandles()); |
| 10470 | for (int i = 0; i < kIterations; i++) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10471 | Local<v8::Number> n(v8::Integer::New(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10472 | CHECK_EQ(i + 1, CountHandles()); |
| 10473 | } |
| 10474 | |
| 10475 | CHECK_EQ(kIterations, CountHandles()); |
| 10476 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10477 | v8::HandleScope scope2(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10478 | for (int j = 0; j < kIterations; j++) { |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 10479 | Local<v8::Number> n(v8::Integer::New(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10480 | CHECK_EQ(j + 1 + kIterations, CountHandles()); |
| 10481 | } |
| 10482 | } |
| 10483 | CHECK_EQ(kIterations, CountHandles()); |
| 10484 | } |
| 10485 | CHECK_EQ(0, CountHandles()); |
| 10486 | CHECK_EQ(kNesting * kIterations, Recurse(kNesting, kIterations)); |
| 10487 | } |
| 10488 | |
| 10489 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10490 | static void InterceptorHasOwnPropertyGetter( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10491 | Local<String> name, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10492 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10493 | ApiTestFuzzer::Fuzz(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10494 | } |
| 10495 | |
| 10496 | |
| 10497 | THREADED_TEST(InterceptorHasOwnProperty) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10498 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10499 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10500 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 10501 | Local<v8::ObjectTemplate> instance_templ = fun_templ->InstanceTemplate(); |
| 10502 | instance_templ->SetNamedPropertyHandler(InterceptorHasOwnPropertyGetter); |
| 10503 | Local<Function> function = fun_templ->GetFunction(); |
| 10504 | context->Global()->Set(v8_str("constructor"), function); |
| 10505 | v8::Handle<Value> value = CompileRun( |
| 10506 | "var o = new constructor();" |
| 10507 | "o.hasOwnProperty('ostehaps');"); |
| 10508 | CHECK_EQ(false, value->BooleanValue()); |
| 10509 | value = CompileRun( |
| 10510 | "o.ostehaps = 42;" |
| 10511 | "o.hasOwnProperty('ostehaps');"); |
| 10512 | CHECK_EQ(true, value->BooleanValue()); |
| 10513 | value = CompileRun( |
| 10514 | "var p = new constructor();" |
| 10515 | "p.hasOwnProperty('ostehaps');"); |
| 10516 | CHECK_EQ(false, value->BooleanValue()); |
| 10517 | } |
| 10518 | |
| 10519 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10520 | static void InterceptorHasOwnPropertyGetterGC( |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10521 | Local<String> name, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10522 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10523 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10524 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10525 | } |
| 10526 | |
| 10527 | |
| 10528 | THREADED_TEST(InterceptorHasOwnPropertyCausingGC) { |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10529 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 10530 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 9085a01 | 2009-05-11 19:22:57 +0000 | [diff] [blame] | 10531 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 10532 | Local<v8::ObjectTemplate> instance_templ = fun_templ->InstanceTemplate(); |
| 10533 | instance_templ->SetNamedPropertyHandler(InterceptorHasOwnPropertyGetterGC); |
| 10534 | Local<Function> function = fun_templ->GetFunction(); |
| 10535 | context->Global()->Set(v8_str("constructor"), function); |
| 10536 | // Let's first make some stuff so we can be sure to get a good GC. |
| 10537 | CompileRun( |
| 10538 | "function makestr(size) {" |
| 10539 | " switch (size) {" |
| 10540 | " case 1: return 'f';" |
| 10541 | " case 2: return 'fo';" |
| 10542 | " case 3: return 'foo';" |
| 10543 | " }" |
| 10544 | " return makestr(size >> 1) + makestr((size + 1) >> 1);" |
| 10545 | "}" |
| 10546 | "var x = makestr(12345);" |
| 10547 | "x = makestr(31415);" |
| 10548 | "x = makestr(23456);"); |
| 10549 | v8::Handle<Value> value = CompileRun( |
| 10550 | "var o = new constructor();" |
| 10551 | "o.__proto__ = new String(x);" |
| 10552 | "o.hasOwnProperty('ostehaps');"); |
| 10553 | CHECK_EQ(false, value->BooleanValue()); |
| 10554 | } |
| 10555 | |
| 10556 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10557 | typedef void (*NamedPropertyGetter)( |
| 10558 | Local<String> property, |
| 10559 | const v8::PropertyCallbackInfo<v8::Value>& info); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10560 | |
| 10561 | |
| 10562 | static void CheckInterceptorLoadIC(NamedPropertyGetter getter, |
| 10563 | const char* source, |
| 10564 | int expected) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10565 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10566 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 10567 | templ->SetNamedPropertyHandler(getter, 0, 0, 0, 0, v8_str("data")); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10568 | LocalContext context; |
| 10569 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 10570 | v8::Handle<Value> value = CompileRun(source); |
| 10571 | CHECK_EQ(expected, value->Int32Value()); |
| 10572 | } |
| 10573 | |
| 10574 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10575 | static void InterceptorLoadICGetter( |
| 10576 | Local<String> name, |
| 10577 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10578 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10579 | v8::Isolate* isolate = CcTest::isolate(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 10580 | CHECK_EQ(isolate, info.GetIsolate()); |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 10581 | CHECK_EQ(v8_str("data"), info.Data()); |
| 10582 | CHECK_EQ(v8_str("x"), name); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10583 | info.GetReturnValue().Set(v8::Integer::New(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10584 | } |
| 10585 | |
| 10586 | |
| 10587 | // This test should hit the load IC for the interceptor case. |
| 10588 | THREADED_TEST(InterceptorLoadIC) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10589 | CheckInterceptorLoadIC(InterceptorLoadICGetter, |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10590 | "var result = 0;" |
| 10591 | "for (var i = 0; i < 1000; i++) {" |
| 10592 | " result = o.x;" |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10593 | "}", |
| 10594 | 42); |
| 10595 | } |
| 10596 | |
| 10597 | |
| 10598 | // Below go several tests which verify that JITing for various |
| 10599 | // configurations of interceptor and explicit fields works fine |
| 10600 | // (those cases are special cased to get better performance). |
| 10601 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10602 | static void InterceptorLoadXICGetter( |
| 10603 | Local<String> name, |
| 10604 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10605 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10606 | info.GetReturnValue().Set( |
| 10607 | v8_str("x")->Equals(name) ? |
| 10608 | v8::Handle<v8::Value>(v8::Integer::New(42)) : |
| 10609 | v8::Handle<v8::Value>()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10610 | } |
| 10611 | |
| 10612 | |
| 10613 | THREADED_TEST(InterceptorLoadICWithFieldOnHolder) { |
| 10614 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10615 | "var result = 0;" |
| 10616 | "o.y = 239;" |
| 10617 | "for (var i = 0; i < 1000; i++) {" |
| 10618 | " result = o.y;" |
| 10619 | "}", |
| 10620 | 239); |
| 10621 | } |
| 10622 | |
| 10623 | |
| 10624 | THREADED_TEST(InterceptorLoadICWithSubstitutedProto) { |
| 10625 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10626 | "var result = 0;" |
| 10627 | "o.__proto__ = { 'y': 239 };" |
| 10628 | "for (var i = 0; i < 1000; i++) {" |
| 10629 | " result = o.y + o.x;" |
| 10630 | "}", |
| 10631 | 239 + 42); |
| 10632 | } |
| 10633 | |
| 10634 | |
| 10635 | THREADED_TEST(InterceptorLoadICWithPropertyOnProto) { |
| 10636 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10637 | "var result = 0;" |
| 10638 | "o.__proto__.y = 239;" |
| 10639 | "for (var i = 0; i < 1000; i++) {" |
| 10640 | " result = o.y + o.x;" |
| 10641 | "}", |
| 10642 | 239 + 42); |
| 10643 | } |
| 10644 | |
| 10645 | |
| 10646 | THREADED_TEST(InterceptorLoadICUndefined) { |
| 10647 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10648 | "var result = 0;" |
| 10649 | "for (var i = 0; i < 1000; i++) {" |
| 10650 | " result = (o.y == undefined) ? 239 : 42;" |
| 10651 | "}", |
| 10652 | 239); |
| 10653 | } |
| 10654 | |
| 10655 | |
| 10656 | THREADED_TEST(InterceptorLoadICWithOverride) { |
| 10657 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10658 | "fst = new Object(); fst.__proto__ = o;" |
| 10659 | "snd = new Object(); snd.__proto__ = fst;" |
| 10660 | "var result1 = 0;" |
| 10661 | "for (var i = 0; i < 1000; i++) {" |
| 10662 | " result1 = snd.x;" |
| 10663 | "}" |
| 10664 | "fst.x = 239;" |
| 10665 | "var result = 0;" |
| 10666 | "for (var i = 0; i < 1000; i++) {" |
| 10667 | " result = snd.x;" |
| 10668 | "}" |
| 10669 | "result + result1", |
| 10670 | 239 + 42); |
| 10671 | } |
| 10672 | |
| 10673 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10674 | // Test the case when we stored field into |
| 10675 | // a stub, but interceptor produced value on its own. |
| 10676 | THREADED_TEST(InterceptorLoadICFieldNotNeeded) { |
| 10677 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10678 | "proto = new Object();" |
| 10679 | "o.__proto__ = proto;" |
| 10680 | "proto.x = 239;" |
| 10681 | "for (var i = 0; i < 1000; i++) {" |
| 10682 | " o.x;" |
| 10683 | // Now it should be ICed and keep a reference to x defined on proto |
| 10684 | "}" |
| 10685 | "var result = 0;" |
| 10686 | "for (var i = 0; i < 1000; i++) {" |
| 10687 | " result += o.x;" |
| 10688 | "}" |
| 10689 | "result;", |
| 10690 | 42 * 1000); |
| 10691 | } |
| 10692 | |
| 10693 | |
| 10694 | // Test the case when we stored field into |
| 10695 | // a stub, but it got invalidated later on. |
| 10696 | THREADED_TEST(InterceptorLoadICInvalidatedField) { |
| 10697 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10698 | "proto1 = new Object();" |
| 10699 | "proto2 = new Object();" |
| 10700 | "o.__proto__ = proto1;" |
| 10701 | "proto1.__proto__ = proto2;" |
| 10702 | "proto2.y = 239;" |
| 10703 | "for (var i = 0; i < 1000; i++) {" |
| 10704 | " o.y;" |
| 10705 | // Now it should be ICed and keep a reference to y defined on proto2 |
| 10706 | "}" |
| 10707 | "proto1.y = 42;" |
| 10708 | "var result = 0;" |
| 10709 | "for (var i = 0; i < 1000; i++) {" |
| 10710 | " result += o.y;" |
| 10711 | "}" |
| 10712 | "result;", |
| 10713 | 42 * 1000); |
| 10714 | } |
| 10715 | |
| 10716 | |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 10717 | static int interceptor_load_not_handled_calls = 0; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10718 | static void InterceptorLoadNotHandled( |
| 10719 | Local<String> name, |
| 10720 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 10721 | ++interceptor_load_not_handled_calls; |
ager@chromium.org | b26c50a | 2010-03-26 09:27:16 +0000 | [diff] [blame] | 10722 | } |
| 10723 | |
| 10724 | |
| 10725 | // Test how post-interceptor lookups are done in the non-cacheable |
| 10726 | // case: the interceptor should not be invoked during this lookup. |
| 10727 | THREADED_TEST(InterceptorLoadICPostInterceptor) { |
| 10728 | interceptor_load_not_handled_calls = 0; |
| 10729 | CheckInterceptorLoadIC(InterceptorLoadNotHandled, |
| 10730 | "receiver = new Object();" |
| 10731 | "receiver.__proto__ = o;" |
| 10732 | "proto = new Object();" |
| 10733 | "/* Make proto a slow-case object. */" |
| 10734 | "for (var i = 0; i < 1000; i++) {" |
| 10735 | " proto[\"xxxxxxxx\" + i] = [];" |
| 10736 | "}" |
| 10737 | "proto.x = 17;" |
| 10738 | "o.__proto__ = proto;" |
| 10739 | "var result = 0;" |
| 10740 | "for (var i = 0; i < 1000; i++) {" |
| 10741 | " result += receiver.x;" |
| 10742 | "}" |
| 10743 | "result;", |
| 10744 | 17 * 1000); |
| 10745 | CHECK_EQ(1000, interceptor_load_not_handled_calls); |
| 10746 | } |
| 10747 | |
| 10748 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10749 | // Test the case when we stored field into |
| 10750 | // a stub, but it got invalidated later on due to override on |
| 10751 | // global object which is between interceptor and fields' holders. |
| 10752 | THREADED_TEST(InterceptorLoadICInvalidatedFieldViaGlobal) { |
| 10753 | CheckInterceptorLoadIC(InterceptorLoadXICGetter, |
| 10754 | "o.__proto__ = this;" // set a global to be a proto of o. |
| 10755 | "this.__proto__.y = 239;" |
| 10756 | "for (var i = 0; i < 10; i++) {" |
| 10757 | " if (o.y != 239) throw 'oops: ' + o.y;" |
| 10758 | // Now it should be ICed and keep a reference to y defined on field_holder. |
| 10759 | "}" |
| 10760 | "this.y = 42;" // Assign on a global. |
| 10761 | "var result = 0;" |
| 10762 | "for (var i = 0; i < 10; i++) {" |
| 10763 | " result += o.y;" |
| 10764 | "}" |
| 10765 | "result;", |
| 10766 | 42 * 10); |
| 10767 | } |
| 10768 | |
| 10769 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10770 | static void SetOnThis(Local<String> name, |
| 10771 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10772 | const v8::PropertyCallbackInfo<void>& info) { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10773 | info.This()->ForceSet(name, value); |
| 10774 | } |
| 10775 | |
| 10776 | |
| 10777 | THREADED_TEST(InterceptorLoadICWithCallbackOnHolder) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10778 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10779 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 10780 | templ->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10781 | templ->SetAccessor(v8_str("y"), Return239Callback); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10782 | LocalContext context; |
| 10783 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10784 | |
| 10785 | // Check the case when receiver and interceptor's holder |
| 10786 | // are the same objects. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10787 | v8::Handle<Value> value = CompileRun( |
| 10788 | "var result = 0;" |
| 10789 | "for (var i = 0; i < 7; i++) {" |
| 10790 | " result = o.y;" |
| 10791 | "}"); |
| 10792 | CHECK_EQ(239, value->Int32Value()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10793 | |
| 10794 | // Check the case when interceptor's holder is in proto chain |
| 10795 | // of receiver. |
| 10796 | value = CompileRun( |
| 10797 | "r = { __proto__: o };" |
| 10798 | "var result = 0;" |
| 10799 | "for (var i = 0; i < 7; i++) {" |
| 10800 | " result = r.y;" |
| 10801 | "}"); |
| 10802 | CHECK_EQ(239, value->Int32Value()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10803 | } |
| 10804 | |
| 10805 | |
| 10806 | THREADED_TEST(InterceptorLoadICWithCallbackOnProto) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10807 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10808 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 10809 | templ_o->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 10810 | v8::Handle<v8::ObjectTemplate> templ_p = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10811 | templ_p->SetAccessor(v8_str("y"), Return239Callback); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10812 | |
| 10813 | LocalContext context; |
| 10814 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 10815 | context->Global()->Set(v8_str("p"), templ_p->NewInstance()); |
| 10816 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10817 | // Check the case when receiver and interceptor's holder |
| 10818 | // are the same objects. |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10819 | v8::Handle<Value> value = CompileRun( |
| 10820 | "o.__proto__ = p;" |
| 10821 | "var result = 0;" |
| 10822 | "for (var i = 0; i < 7; i++) {" |
| 10823 | " result = o.x + o.y;" |
| 10824 | "}"); |
| 10825 | CHECK_EQ(239 + 42, value->Int32Value()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 10826 | |
| 10827 | // Check the case when interceptor's holder is in proto chain |
| 10828 | // of receiver. |
| 10829 | value = CompileRun( |
| 10830 | "r = { __proto__: o };" |
| 10831 | "var result = 0;" |
| 10832 | "for (var i = 0; i < 7; i++) {" |
| 10833 | " result = r.x + r.y;" |
| 10834 | "}"); |
| 10835 | CHECK_EQ(239 + 42, value->Int32Value()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10836 | } |
| 10837 | |
| 10838 | |
| 10839 | THREADED_TEST(InterceptorLoadICForCallbackWithOverride) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10840 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10841 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 10842 | templ->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10843 | templ->SetAccessor(v8_str("y"), Return239Callback); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10844 | |
| 10845 | LocalContext context; |
| 10846 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 10847 | |
| 10848 | v8::Handle<Value> value = CompileRun( |
| 10849 | "fst = new Object(); fst.__proto__ = o;" |
| 10850 | "snd = new Object(); snd.__proto__ = fst;" |
| 10851 | "var result1 = 0;" |
| 10852 | "for (var i = 0; i < 7; i++) {" |
| 10853 | " result1 = snd.x;" |
| 10854 | "}" |
| 10855 | "fst.x = 239;" |
| 10856 | "var result = 0;" |
| 10857 | "for (var i = 0; i < 7; i++) {" |
| 10858 | " result = snd.x;" |
| 10859 | "}" |
| 10860 | "result + result1"); |
| 10861 | CHECK_EQ(239 + 42, value->Int32Value()); |
| 10862 | } |
| 10863 | |
| 10864 | |
| 10865 | // Test the case when we stored callback into |
| 10866 | // a stub, but interceptor produced value on its own. |
| 10867 | THREADED_TEST(InterceptorLoadICCallbackNotNeeded) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10868 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10869 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 10870 | templ_o->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 10871 | v8::Handle<v8::ObjectTemplate> templ_p = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10872 | templ_p->SetAccessor(v8_str("y"), Return239Callback); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10873 | |
| 10874 | LocalContext context; |
| 10875 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 10876 | context->Global()->Set(v8_str("p"), templ_p->NewInstance()); |
| 10877 | |
| 10878 | v8::Handle<Value> value = CompileRun( |
| 10879 | "o.__proto__ = p;" |
| 10880 | "for (var i = 0; i < 7; i++) {" |
| 10881 | " o.x;" |
| 10882 | // Now it should be ICed and keep a reference to x defined on p |
| 10883 | "}" |
| 10884 | "var result = 0;" |
| 10885 | "for (var i = 0; i < 7; i++) {" |
| 10886 | " result += o.x;" |
| 10887 | "}" |
| 10888 | "result"); |
| 10889 | CHECK_EQ(42 * 7, value->Int32Value()); |
| 10890 | } |
| 10891 | |
| 10892 | |
| 10893 | // Test the case when we stored callback into |
| 10894 | // a stub, but it got invalidated later on. |
| 10895 | THREADED_TEST(InterceptorLoadICInvalidatedCallback) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10896 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10897 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 10898 | templ_o->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 10899 | v8::Handle<v8::ObjectTemplate> templ_p = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10900 | templ_p->SetAccessor(v8_str("y"), Return239Callback, SetOnThis); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10901 | |
| 10902 | LocalContext context; |
| 10903 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 10904 | context->Global()->Set(v8_str("p"), templ_p->NewInstance()); |
| 10905 | |
| 10906 | v8::Handle<Value> value = CompileRun( |
| 10907 | "inbetween = new Object();" |
| 10908 | "o.__proto__ = inbetween;" |
| 10909 | "inbetween.__proto__ = p;" |
| 10910 | "for (var i = 0; i < 10; i++) {" |
| 10911 | " o.y;" |
| 10912 | // Now it should be ICed and keep a reference to y defined on p |
| 10913 | "}" |
| 10914 | "inbetween.y = 42;" |
| 10915 | "var result = 0;" |
| 10916 | "for (var i = 0; i < 10; i++) {" |
| 10917 | " result += o.y;" |
| 10918 | "}" |
| 10919 | "result"); |
| 10920 | CHECK_EQ(42 * 10, value->Int32Value()); |
| 10921 | } |
| 10922 | |
| 10923 | |
| 10924 | // Test the case when we stored callback into |
| 10925 | // a stub, but it got invalidated later on due to override on |
| 10926 | // global object which is between interceptor and callbacks' holders. |
| 10927 | THREADED_TEST(InterceptorLoadICInvalidatedCallbackViaGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10928 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10929 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 10930 | templ_o->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 10931 | v8::Handle<v8::ObjectTemplate> templ_p = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10932 | templ_p->SetAccessor(v8_str("y"), Return239Callback, SetOnThis); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 10933 | |
| 10934 | LocalContext context; |
| 10935 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 10936 | context->Global()->Set(v8_str("p"), templ_p->NewInstance()); |
| 10937 | |
| 10938 | v8::Handle<Value> value = CompileRun( |
| 10939 | "o.__proto__ = this;" |
| 10940 | "this.__proto__ = p;" |
| 10941 | "for (var i = 0; i < 10; i++) {" |
| 10942 | " if (o.y != 239) throw 'oops: ' + o.y;" |
| 10943 | // Now it should be ICed and keep a reference to y defined on p |
| 10944 | "}" |
| 10945 | "this.y = 42;" |
| 10946 | "var result = 0;" |
| 10947 | "for (var i = 0; i < 10; i++) {" |
| 10948 | " result += o.y;" |
| 10949 | "}" |
| 10950 | "result"); |
| 10951 | CHECK_EQ(42 * 10, value->Int32Value()); |
| 10952 | } |
| 10953 | |
| 10954 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10955 | static void InterceptorLoadICGetter0( |
| 10956 | Local<String> name, |
| 10957 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10958 | ApiTestFuzzer::Fuzz(); |
| 10959 | CHECK(v8_str("x")->Equals(name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10960 | info.GetReturnValue().Set(v8::Integer::New(0)); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 10961 | } |
| 10962 | |
| 10963 | |
| 10964 | THREADED_TEST(InterceptorReturningZero) { |
| 10965 | CheckInterceptorLoadIC(InterceptorLoadICGetter0, |
| 10966 | "o.x == undefined ? 1 : 0", |
| 10967 | 0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10968 | } |
| 10969 | |
| 10970 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10971 | static void InterceptorStoreICSetter( |
| 10972 | Local<String> key, |
| 10973 | Local<Value> value, |
| 10974 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10975 | CHECK(v8_str("x")->Equals(key)); |
| 10976 | CHECK_EQ(42, value->Int32Value()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 10977 | info.GetReturnValue().Set(value); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10978 | } |
| 10979 | |
| 10980 | |
| 10981 | // This test should hit the store IC for the interceptor case. |
| 10982 | THREADED_TEST(InterceptorStoreIC) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10983 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10984 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 10985 | templ->SetNamedPropertyHandler(InterceptorLoadICGetter, |
vegorov@chromium.org | 21b5e95 | 2010-11-23 10:24:40 +0000 | [diff] [blame] | 10986 | InterceptorStoreICSetter, |
| 10987 | 0, 0, 0, v8_str("data")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10988 | LocalContext context; |
| 10989 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 10990 | CompileRun( |
| 10991 | "for (var i = 0; i < 1000; i++) {" |
| 10992 | " o.x = 42;" |
| 10993 | "}"); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 10994 | } |
| 10995 | |
| 10996 | |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10997 | THREADED_TEST(InterceptorStoreICWithNoSetter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 10998 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | eadaf22 | 2009-06-16 09:43:10 +0000 | [diff] [blame] | 10999 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11000 | templ->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 11001 | LocalContext context; |
| 11002 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11003 | v8::Handle<Value> value = CompileRun( |
| 11004 | "for (var i = 0; i < 1000; i++) {" |
| 11005 | " o.y = 239;" |
| 11006 | "}" |
| 11007 | "42 + o.y"); |
| 11008 | CHECK_EQ(239 + 42, value->Int32Value()); |
| 11009 | } |
| 11010 | |
| 11011 | |
| 11012 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 11013 | |
| 11014 | v8::Handle<Value> call_ic_function; |
| 11015 | v8::Handle<Value> call_ic_function2; |
| 11016 | v8::Handle<Value> call_ic_function3; |
| 11017 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11018 | static void InterceptorCallICGetter( |
| 11019 | Local<String> name, |
| 11020 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 11021 | ApiTestFuzzer::Fuzz(); |
| 11022 | CHECK(v8_str("x")->Equals(name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11023 | info.GetReturnValue().Set(call_ic_function); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 11024 | } |
| 11025 | |
| 11026 | |
| 11027 | // This test should hit the call IC for the interceptor case. |
| 11028 | THREADED_TEST(InterceptorCallIC) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11029 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 11030 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11031 | templ->SetNamedPropertyHandler(InterceptorCallICGetter); |
| 11032 | LocalContext context; |
| 11033 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11034 | call_ic_function = |
| 11035 | v8_compile("function f(x) { return x + 1; }; f")->Run(); |
| 11036 | v8::Handle<Value> value = CompileRun( |
| 11037 | "var result = 0;" |
| 11038 | "for (var i = 0; i < 1000; i++) {" |
| 11039 | " result = o.x(41);" |
| 11040 | "}"); |
| 11041 | CHECK_EQ(42, value->Int32Value()); |
| 11042 | } |
| 11043 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11044 | |
| 11045 | // This test checks that if interceptor doesn't provide |
| 11046 | // a value, we can fetch regular value. |
| 11047 | THREADED_TEST(InterceptorCallICSeesOthers) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11048 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11049 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11050 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11051 | LocalContext context; |
| 11052 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11053 | v8::Handle<Value> value = CompileRun( |
| 11054 | "o.x = function f(x) { return x + 1; };" |
| 11055 | "var result = 0;" |
| 11056 | "for (var i = 0; i < 7; i++) {" |
| 11057 | " result = o.x(41);" |
| 11058 | "}"); |
| 11059 | CHECK_EQ(42, value->Int32Value()); |
| 11060 | } |
| 11061 | |
| 11062 | |
| 11063 | static v8::Handle<Value> call_ic_function4; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11064 | static void InterceptorCallICGetter4( |
| 11065 | Local<String> name, |
| 11066 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11067 | ApiTestFuzzer::Fuzz(); |
| 11068 | CHECK(v8_str("x")->Equals(name)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11069 | info.GetReturnValue().Set(call_ic_function4); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11070 | } |
| 11071 | |
| 11072 | |
| 11073 | // This test checks that if interceptor provides a function, |
| 11074 | // even if we cached shadowed variant, interceptor's function |
| 11075 | // is invoked |
| 11076 | THREADED_TEST(InterceptorCallICCacheableNotNeeded) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11077 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11078 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11079 | templ->SetNamedPropertyHandler(InterceptorCallICGetter4); |
| 11080 | LocalContext context; |
| 11081 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11082 | call_ic_function4 = |
| 11083 | v8_compile("function f(x) { return x - 1; }; f")->Run(); |
| 11084 | v8::Handle<Value> value = CompileRun( |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 11085 | "Object.getPrototypeOf(o).x = function(x) { return x + 1; };" |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11086 | "var result = 0;" |
| 11087 | "for (var i = 0; i < 1000; i++) {" |
| 11088 | " result = o.x(42);" |
| 11089 | "}"); |
| 11090 | CHECK_EQ(41, value->Int32Value()); |
| 11091 | } |
| 11092 | |
| 11093 | |
| 11094 | // Test the case when we stored cacheable lookup into |
| 11095 | // a stub, but it got invalidated later on |
| 11096 | THREADED_TEST(InterceptorCallICInvalidatedCacheable) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11097 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11098 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11099 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11100 | LocalContext context; |
| 11101 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11102 | v8::Handle<Value> value = CompileRun( |
| 11103 | "proto1 = new Object();" |
| 11104 | "proto2 = new Object();" |
| 11105 | "o.__proto__ = proto1;" |
| 11106 | "proto1.__proto__ = proto2;" |
| 11107 | "proto2.y = function(x) { return x + 1; };" |
| 11108 | // Invoke it many times to compile a stub |
| 11109 | "for (var i = 0; i < 7; i++) {" |
| 11110 | " o.y(42);" |
| 11111 | "}" |
| 11112 | "proto1.y = function(x) { return x - 1; };" |
| 11113 | "var result = 0;" |
| 11114 | "for (var i = 0; i < 7; i++) {" |
| 11115 | " result += o.y(42);" |
| 11116 | "}"); |
| 11117 | CHECK_EQ(41 * 7, value->Int32Value()); |
| 11118 | } |
| 11119 | |
| 11120 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11121 | // This test checks that if interceptor doesn't provide a function, |
| 11122 | // cached constant function is used |
| 11123 | THREADED_TEST(InterceptorCallICConstantFunctionUsed) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11124 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11125 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11126 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11127 | LocalContext context; |
| 11128 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11129 | v8::Handle<Value> value = CompileRun( |
| 11130 | "function inc(x) { return x + 1; };" |
| 11131 | "inc(1);" |
| 11132 | "o.x = inc;" |
| 11133 | "var result = 0;" |
| 11134 | "for (var i = 0; i < 1000; i++) {" |
| 11135 | " result = o.x(42);" |
| 11136 | "}"); |
| 11137 | CHECK_EQ(43, value->Int32Value()); |
| 11138 | } |
| 11139 | |
| 11140 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11141 | static v8::Handle<Value> call_ic_function5; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11142 | static void InterceptorCallICGetter5( |
| 11143 | Local<String> name, |
| 11144 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11145 | ApiTestFuzzer::Fuzz(); |
| 11146 | if (v8_str("x")->Equals(name)) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11147 | info.GetReturnValue().Set(call_ic_function5); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11148 | } |
| 11149 | |
| 11150 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11151 | // This test checks that if interceptor provides a function, |
| 11152 | // even if we cached constant function, interceptor's function |
| 11153 | // is invoked |
| 11154 | THREADED_TEST(InterceptorCallICConstantFunctionNotNeeded) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11155 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11156 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11157 | templ->SetNamedPropertyHandler(InterceptorCallICGetter5); |
| 11158 | LocalContext context; |
| 11159 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11160 | call_ic_function5 = |
| 11161 | v8_compile("function f(x) { return x - 1; }; f")->Run(); |
| 11162 | v8::Handle<Value> value = CompileRun( |
| 11163 | "function inc(x) { return x + 1; };" |
| 11164 | "inc(1);" |
| 11165 | "o.x = inc;" |
| 11166 | "var result = 0;" |
| 11167 | "for (var i = 0; i < 1000; i++) {" |
| 11168 | " result = o.x(42);" |
| 11169 | "}"); |
| 11170 | CHECK_EQ(41, value->Int32Value()); |
| 11171 | } |
| 11172 | |
| 11173 | |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11174 | static v8::Handle<Value> call_ic_function6; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11175 | static void InterceptorCallICGetter6( |
| 11176 | Local<String> name, |
| 11177 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11178 | ApiTestFuzzer::Fuzz(); |
| 11179 | if (v8_str("x")->Equals(name)) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11180 | info.GetReturnValue().Set(call_ic_function6); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11181 | } |
| 11182 | |
| 11183 | |
| 11184 | // Same test as above, except the code is wrapped in a function |
| 11185 | // to test the optimized compiler. |
| 11186 | THREADED_TEST(InterceptorCallICConstantFunctionNotNeededWrapped) { |
| 11187 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11188 | v8::HandleScope scope(CcTest::isolate()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 11189 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11190 | templ->SetNamedPropertyHandler(InterceptorCallICGetter6); |
| 11191 | LocalContext context; |
| 11192 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11193 | call_ic_function6 = |
| 11194 | v8_compile("function f(x) { return x - 1; }; f")->Run(); |
| 11195 | v8::Handle<Value> value = CompileRun( |
| 11196 | "function inc(x) { return x + 1; };" |
| 11197 | "inc(1);" |
| 11198 | "o.x = inc;" |
| 11199 | "function test() {" |
| 11200 | " var result = 0;" |
| 11201 | " for (var i = 0; i < 1000; i++) {" |
| 11202 | " result = o.x(42);" |
| 11203 | " }" |
| 11204 | " return result;" |
| 11205 | "};" |
| 11206 | "test();" |
| 11207 | "test();" |
| 11208 | "test();" |
| 11209 | "%OptimizeFunctionOnNextCall(test);" |
| 11210 | "test()"); |
| 11211 | CHECK_EQ(41, value->Int32Value()); |
| 11212 | } |
| 11213 | |
| 11214 | |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11215 | // Test the case when we stored constant function into |
| 11216 | // a stub, but it got invalidated later on |
| 11217 | THREADED_TEST(InterceptorCallICInvalidatedConstantFunction) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11218 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11219 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11220 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11221 | LocalContext context; |
| 11222 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11223 | v8::Handle<Value> value = CompileRun( |
| 11224 | "function inc(x) { return x + 1; };" |
| 11225 | "inc(1);" |
| 11226 | "proto1 = new Object();" |
| 11227 | "proto2 = new Object();" |
| 11228 | "o.__proto__ = proto1;" |
| 11229 | "proto1.__proto__ = proto2;" |
| 11230 | "proto2.y = inc;" |
| 11231 | // Invoke it many times to compile a stub |
| 11232 | "for (var i = 0; i < 7; i++) {" |
| 11233 | " o.y(42);" |
| 11234 | "}" |
| 11235 | "proto1.y = function(x) { return x - 1; };" |
| 11236 | "var result = 0;" |
| 11237 | "for (var i = 0; i < 7; i++) {" |
| 11238 | " result += o.y(42);" |
| 11239 | "}"); |
| 11240 | CHECK_EQ(41 * 7, value->Int32Value()); |
| 11241 | } |
| 11242 | |
| 11243 | |
| 11244 | // Test the case when we stored constant function into |
| 11245 | // a stub, but it got invalidated later on due to override on |
| 11246 | // global object which is between interceptor and constant function' holders. |
| 11247 | THREADED_TEST(InterceptorCallICInvalidatedConstantFunctionViaGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11248 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 11249 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11250 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11251 | LocalContext context; |
| 11252 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
| 11253 | v8::Handle<Value> value = CompileRun( |
| 11254 | "function inc(x) { return x + 1; };" |
| 11255 | "inc(1);" |
| 11256 | "o.__proto__ = this;" |
| 11257 | "this.__proto__.y = inc;" |
| 11258 | // Invoke it many times to compile a stub |
| 11259 | "for (var i = 0; i < 7; i++) {" |
| 11260 | " if (o.y(42) != 43) throw 'oops: ' + o.y(42);" |
| 11261 | "}" |
| 11262 | "this.y = function(x) { return x - 1; };" |
| 11263 | "var result = 0;" |
| 11264 | "for (var i = 0; i < 7; i++) {" |
| 11265 | " result += o.y(42);" |
| 11266 | "}"); |
| 11267 | CHECK_EQ(41 * 7, value->Int32Value()); |
| 11268 | } |
| 11269 | |
| 11270 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11271 | // Test the case when actual function to call sits on global object. |
| 11272 | THREADED_TEST(InterceptorCallICCachedFromGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11273 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11274 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 11275 | templ_o->SetNamedPropertyHandler(NoBlockGetterX); |
| 11276 | |
| 11277 | LocalContext context; |
| 11278 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 11279 | |
| 11280 | v8::Handle<Value> value = CompileRun( |
| 11281 | "try {" |
| 11282 | " o.__proto__ = this;" |
| 11283 | " for (var i = 0; i < 10; i++) {" |
| 11284 | " var v = o.parseFloat('239');" |
| 11285 | " if (v != 239) throw v;" |
| 11286 | // Now it should be ICed and keep a reference to parseFloat. |
| 11287 | " }" |
| 11288 | " var result = 0;" |
| 11289 | " for (var i = 0; i < 10; i++) {" |
| 11290 | " result += o.parseFloat('239');" |
| 11291 | " }" |
| 11292 | " result" |
| 11293 | "} catch(e) {" |
| 11294 | " e" |
| 11295 | "};"); |
| 11296 | CHECK_EQ(239 * 10, value->Int32Value()); |
| 11297 | } |
| 11298 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11299 | static void InterceptorCallICFastApi( |
| 11300 | Local<String> name, |
| 11301 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11302 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 11303 | CheckReturnValue(info, FUNCTION_ADDR(InterceptorCallICFastApi)); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11304 | int* call_count = |
| 11305 | reinterpret_cast<int*>(v8::External::Cast(*info.Data())->Value()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11306 | ++(*call_count); |
| 11307 | if ((*call_count) % 20 == 0) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11308 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11309 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11310 | } |
| 11311 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11312 | static void FastApiCallback_TrivialSignature( |
| 11313 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11314 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 11315 | CheckReturnValue(args, FUNCTION_ADDR(FastApiCallback_TrivialSignature)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11316 | v8::Isolate* isolate = CcTest::isolate(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 11317 | CHECK_EQ(isolate, args.GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11318 | CHECK_EQ(args.This(), args.Holder()); |
| 11319 | CHECK(args.Data()->Equals(v8_str("method_data"))); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11320 | args.GetReturnValue().Set(args[0]->Int32Value() + 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11321 | } |
| 11322 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11323 | static void FastApiCallback_SimpleSignature( |
| 11324 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11325 | ApiTestFuzzer::Fuzz(); |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 11326 | CheckReturnValue(args, FUNCTION_ADDR(FastApiCallback_SimpleSignature)); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11327 | v8::Isolate* isolate = CcTest::isolate(); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 11328 | CHECK_EQ(isolate, args.GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11329 | CHECK_EQ(args.This()->GetPrototype(), args.Holder()); |
| 11330 | CHECK(args.Data()->Equals(v8_str("method_data"))); |
| 11331 | // Note, we're using HasRealNamedProperty instead of Has to avoid |
| 11332 | // invoking the interceptor again. |
| 11333 | CHECK(args.Holder()->HasRealNamedProperty(v8_str("foo"))); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11334 | args.GetReturnValue().Set(args[0]->Int32Value() + 1); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11335 | } |
| 11336 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11337 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11338 | // Helper to maximize the odds of object moving. |
| 11339 | static void GenerateSomeGarbage() { |
| 11340 | CompileRun( |
| 11341 | "var garbage;" |
| 11342 | "for (var i = 0; i < 1000; i++) {" |
| 11343 | " garbage = [1/i, \"garbage\" + i, garbage, {foo: garbage}];" |
| 11344 | "}" |
| 11345 | "garbage = undefined;"); |
| 11346 | } |
| 11347 | |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11348 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11349 | void DirectApiCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11350 | static int count = 0; |
| 11351 | if (count++ % 3 == 0) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11352 | CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
yangguo@chromium.org | d2899aa | 2012-06-21 11:16:20 +0000 | [diff] [blame] | 11353 | // This should move the stub |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11354 | GenerateSomeGarbage(); // This should ensure the old stub memory is flushed |
| 11355 | } |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11356 | } |
| 11357 | |
| 11358 | |
| 11359 | THREADED_TEST(CallICFastApi_DirectCall_GCMoveStub) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11360 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 11361 | v8::HandleScope scope(context->GetIsolate()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11362 | v8::Handle<v8::ObjectTemplate> nativeobject_templ = v8::ObjectTemplate::New(); |
| 11363 | nativeobject_templ->Set("callback", |
| 11364 | v8::FunctionTemplate::New(DirectApiCallback)); |
| 11365 | v8::Local<v8::Object> nativeobject_obj = nativeobject_templ->NewInstance(); |
| 11366 | context->Global()->Set(v8_str("nativeobject"), nativeobject_obj); |
| 11367 | // call the api function multiple times to ensure direct call stub creation. |
| 11368 | CompileRun( |
| 11369 | "function f() {" |
| 11370 | " for (var i = 1; i <= 30; i++) {" |
| 11371 | " nativeobject.callback();" |
| 11372 | " }" |
| 11373 | "}" |
| 11374 | "f();"); |
| 11375 | } |
| 11376 | |
| 11377 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11378 | void ThrowingDirectApiCallback( |
| 11379 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 11380 | args.GetIsolate()->ThrowException(v8_str("g")); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11381 | } |
| 11382 | |
| 11383 | |
| 11384 | THREADED_TEST(CallICFastApi_DirectCall_Throw) { |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11385 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 11386 | v8::HandleScope scope(context->GetIsolate()); |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 11387 | v8::Handle<v8::ObjectTemplate> nativeobject_templ = v8::ObjectTemplate::New(); |
| 11388 | nativeobject_templ->Set("callback", |
| 11389 | v8::FunctionTemplate::New(ThrowingDirectApiCallback)); |
| 11390 | v8::Local<v8::Object> nativeobject_obj = nativeobject_templ->NewInstance(); |
| 11391 | context->Global()->Set(v8_str("nativeobject"), nativeobject_obj); |
| 11392 | // call the api function multiple times to ensure direct call stub creation. |
| 11393 | v8::Handle<Value> result = CompileRun( |
| 11394 | "var result = '';" |
| 11395 | "function f() {" |
| 11396 | " for (var i = 1; i <= 5; i++) {" |
| 11397 | " try { nativeobject.callback(); } catch (e) { result += e; }" |
| 11398 | " }" |
| 11399 | "}" |
| 11400 | "f(); result;"); |
| 11401 | CHECK_EQ(v8_str("ggggg"), result); |
| 11402 | } |
| 11403 | |
| 11404 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11405 | static Handle<Value> DoDirectGetter() { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11406 | if (++p_getter_count % 3 == 0) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11407 | CcTest::heap()->CollectAllGarbage(i::Heap::kAbortIncrementalMarkingMask); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11408 | GenerateSomeGarbage(); |
| 11409 | } |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11410 | return v8_str("Direct Getter Result"); |
| 11411 | } |
| 11412 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11413 | static void DirectGetterCallback( |
| 11414 | Local<String> name, |
| 11415 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
dslomov@chromium.org | b752d40 | 2013-06-18 11:54:54 +0000 | [diff] [blame] | 11416 | CheckReturnValue(info, FUNCTION_ADDR(DirectGetterCallback)); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11417 | info.GetReturnValue().Set(DoDirectGetter()); |
| 11418 | } |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11419 | |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11420 | |
| 11421 | template<typename Accessor> |
| 11422 | static void LoadICFastApi_DirectCall_GCMoveStub(Accessor accessor) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11423 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 11424 | v8::HandleScope scope(context->GetIsolate()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11425 | v8::Handle<v8::ObjectTemplate> obj = v8::ObjectTemplate::New(); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11426 | obj->SetAccessor(v8_str("p1"), accessor); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11427 | context->Global()->Set(v8_str("o1"), obj->NewInstance()); |
| 11428 | p_getter_count = 0; |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11429 | v8::Handle<v8::Value> result = CompileRun( |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11430 | "function f() {" |
| 11431 | " for (var i = 0; i < 30; i++) o1.p1;" |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11432 | " return o1.p1" |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11433 | "}" |
| 11434 | "f();"); |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11435 | CHECK_EQ(v8_str("Direct Getter Result"), result); |
| 11436 | CHECK_EQ(31, p_getter_count); |
| 11437 | } |
| 11438 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11439 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11440 | THREADED_PROFILED_TEST(LoadICFastApi_DirectCall_GCMoveStub) { |
ulan@chromium.org | bf9432e | 2013-05-22 14:05:23 +0000 | [diff] [blame] | 11441 | LoadICFastApi_DirectCall_GCMoveStub(DirectGetterCallback); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11442 | } |
| 11443 | |
| 11444 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11445 | void ThrowingDirectGetterCallback( |
| 11446 | Local<String> name, |
| 11447 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 11448 | info.GetIsolate()->ThrowException(v8_str("g")); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11449 | } |
| 11450 | |
| 11451 | |
| 11452 | THREADED_TEST(LoadICFastApi_DirectCall_Throw) { |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11453 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 11454 | v8::HandleScope scope(context->GetIsolate()); |
vegorov@chromium.org | 5d6c1f5 | 2011-02-28 13:13:38 +0000 | [diff] [blame] | 11455 | v8::Handle<v8::ObjectTemplate> obj = v8::ObjectTemplate::New(); |
| 11456 | obj->SetAccessor(v8_str("p1"), ThrowingDirectGetterCallback); |
| 11457 | context->Global()->Set(v8_str("o1"), obj->NewInstance()); |
| 11458 | v8::Handle<Value> result = CompileRun( |
| 11459 | "var result = '';" |
| 11460 | "for (var i = 0; i < 5; i++) {" |
| 11461 | " try { o1.p1; } catch (e) { result += e; }" |
| 11462 | "}" |
| 11463 | "result;"); |
| 11464 | CHECK_EQ(v8_str("ggggg"), result); |
| 11465 | } |
| 11466 | |
| 11467 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11468 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_TrivialSignature) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11469 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11470 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11471 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11472 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11473 | v8::FunctionTemplate::New(FastApiCallback_TrivialSignature, |
| 11474 | v8_str("method_data"), |
| 11475 | v8::Handle<v8::Signature>()); |
| 11476 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11477 | proto_templ->Set(v8_str("method"), method_templ); |
| 11478 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11479 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11480 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11481 | v8::External::New(&interceptor_call_count)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11482 | LocalContext context; |
| 11483 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11484 | GenerateSomeGarbage(); |
| 11485 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11486 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11487 | "var result = 0;" |
| 11488 | "for (var i = 0; i < 100; i++) {" |
| 11489 | " result = o.method(41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11490 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11491 | CHECK_EQ(42, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11492 | CHECK_EQ(100, interceptor_call_count); |
| 11493 | } |
| 11494 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11495 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11496 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_SimpleSignature) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11497 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11498 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11499 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11500 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11501 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11502 | v8_str("method_data"), |
| 11503 | v8::Signature::New(fun_templ)); |
| 11504 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11505 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11506 | fun_templ->SetHiddenPrototype(true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11507 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11508 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11509 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11510 | v8::External::New(&interceptor_call_count)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11511 | LocalContext context; |
| 11512 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11513 | GenerateSomeGarbage(); |
| 11514 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11515 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11516 | "o.foo = 17;" |
| 11517 | "var receiver = {};" |
| 11518 | "receiver.__proto__ = o;" |
| 11519 | "var result = 0;" |
| 11520 | "for (var i = 0; i < 100; i++) {" |
| 11521 | " result = receiver.method(41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11522 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11523 | CHECK_EQ(42, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11524 | CHECK_EQ(100, interceptor_call_count); |
| 11525 | } |
| 11526 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11527 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11528 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_SimpleSignature_Miss1) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11529 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11530 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11531 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11532 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11533 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11534 | v8_str("method_data"), |
| 11535 | v8::Signature::New(fun_templ)); |
| 11536 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11537 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11538 | fun_templ->SetHiddenPrototype(true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11539 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11540 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11541 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11542 | v8::External::New(&interceptor_call_count)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11543 | LocalContext context; |
| 11544 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11545 | GenerateSomeGarbage(); |
| 11546 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11547 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11548 | "o.foo = 17;" |
| 11549 | "var receiver = {};" |
| 11550 | "receiver.__proto__ = o;" |
| 11551 | "var result = 0;" |
| 11552 | "var saved_result = 0;" |
| 11553 | "for (var i = 0; i < 100; i++) {" |
| 11554 | " result = receiver.method(41);" |
| 11555 | " if (i == 50) {" |
| 11556 | " saved_result = result;" |
| 11557 | " receiver = {method: function(x) { return x - 1 }};" |
| 11558 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11559 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11560 | CHECK_EQ(40, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11561 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11562 | CHECK_GE(interceptor_call_count, 50); |
| 11563 | } |
| 11564 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11565 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11566 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_SimpleSignature_Miss2) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11567 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11568 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11569 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11570 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11571 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11572 | v8_str("method_data"), |
| 11573 | v8::Signature::New(fun_templ)); |
| 11574 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11575 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11576 | fun_templ->SetHiddenPrototype(true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11577 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11578 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11579 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11580 | v8::External::New(&interceptor_call_count)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11581 | LocalContext context; |
| 11582 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11583 | GenerateSomeGarbage(); |
| 11584 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11585 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11586 | "o.foo = 17;" |
| 11587 | "var receiver = {};" |
| 11588 | "receiver.__proto__ = o;" |
| 11589 | "var result = 0;" |
| 11590 | "var saved_result = 0;" |
| 11591 | "for (var i = 0; i < 100; i++) {" |
| 11592 | " result = receiver.method(41);" |
| 11593 | " if (i == 50) {" |
| 11594 | " saved_result = result;" |
| 11595 | " o.method = function(x) { return x - 1 };" |
| 11596 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11597 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11598 | CHECK_EQ(40, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11599 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11600 | CHECK_GE(interceptor_call_count, 50); |
| 11601 | } |
| 11602 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11603 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11604 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_SimpleSignature_Miss3) { |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11605 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11606 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11607 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11608 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11609 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11610 | v8_str("method_data"), |
| 11611 | v8::Signature::New(fun_templ)); |
| 11612 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11613 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11614 | fun_templ->SetHiddenPrototype(true); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11615 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11616 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11617 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11618 | v8::External::New(&interceptor_call_count)); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11619 | LocalContext context; |
| 11620 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11621 | GenerateSomeGarbage(); |
| 11622 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
| 11623 | v8::TryCatch try_catch; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11624 | CompileRun( |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11625 | "o.foo = 17;" |
| 11626 | "var receiver = {};" |
| 11627 | "receiver.__proto__ = o;" |
| 11628 | "var result = 0;" |
| 11629 | "var saved_result = 0;" |
| 11630 | "for (var i = 0; i < 100; i++) {" |
| 11631 | " result = receiver.method(41);" |
| 11632 | " if (i == 50) {" |
| 11633 | " saved_result = result;" |
| 11634 | " receiver = 333;" |
| 11635 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11636 | "}"); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11637 | CHECK(try_catch.HasCaught()); |
| 11638 | CHECK_EQ(v8_str("TypeError: Object 333 has no method 'method'"), |
| 11639 | try_catch.Exception()->ToString()); |
| 11640 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11641 | CHECK_GE(interceptor_call_count, 50); |
| 11642 | } |
| 11643 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11644 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11645 | THREADED_PROFILED_TEST(InterceptorCallICFastApi_SimpleSignature_TypeError) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11646 | int interceptor_call_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11647 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11648 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11649 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11650 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11651 | v8_str("method_data"), |
| 11652 | v8::Signature::New(fun_templ)); |
| 11653 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11654 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11655 | fun_templ->SetHiddenPrototype(true); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11656 | v8::Handle<v8::ObjectTemplate> templ = fun_templ->InstanceTemplate(); |
| 11657 | templ->SetNamedPropertyHandler(InterceptorCallICFastApi, |
| 11658 | NULL, NULL, NULL, NULL, |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 11659 | v8::External::New(&interceptor_call_count)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11660 | LocalContext context; |
| 11661 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11662 | GenerateSomeGarbage(); |
| 11663 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
| 11664 | v8::TryCatch try_catch; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11665 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11666 | "o.foo = 17;" |
| 11667 | "var receiver = {};" |
| 11668 | "receiver.__proto__ = o;" |
| 11669 | "var result = 0;" |
| 11670 | "var saved_result = 0;" |
| 11671 | "for (var i = 0; i < 100; i++) {" |
| 11672 | " result = receiver.method(41);" |
| 11673 | " if (i == 50) {" |
| 11674 | " saved_result = result;" |
| 11675 | " receiver = {method: receiver.method};" |
| 11676 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11677 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11678 | CHECK(try_catch.HasCaught()); |
| 11679 | CHECK_EQ(v8_str("TypeError: Illegal invocation"), |
| 11680 | try_catch.Exception()->ToString()); |
| 11681 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11682 | CHECK_GE(interceptor_call_count, 50); |
| 11683 | } |
| 11684 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11685 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11686 | THREADED_PROFILED_TEST(CallICFastApi_TrivialSignature) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11687 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11688 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11689 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11690 | v8::FunctionTemplate::New(FastApiCallback_TrivialSignature, |
| 11691 | v8_str("method_data"), |
| 11692 | v8::Handle<v8::Signature>()); |
| 11693 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11694 | proto_templ->Set(v8_str("method"), method_templ); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11695 | v8::Handle<v8::ObjectTemplate> templ(fun_templ->InstanceTemplate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11696 | USE(templ); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11697 | LocalContext context; |
| 11698 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11699 | GenerateSomeGarbage(); |
| 11700 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11701 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11702 | "var result = 0;" |
| 11703 | "for (var i = 0; i < 100; i++) {" |
| 11704 | " result = o.method(41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11705 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11706 | |
| 11707 | CHECK_EQ(42, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11708 | } |
| 11709 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11710 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11711 | THREADED_PROFILED_TEST(CallICFastApi_SimpleSignature) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11712 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11713 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11714 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11715 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11716 | v8_str("method_data"), |
| 11717 | v8::Signature::New(fun_templ)); |
| 11718 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11719 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11720 | fun_templ->SetHiddenPrototype(true); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11721 | v8::Handle<v8::ObjectTemplate> templ(fun_templ->InstanceTemplate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11722 | CHECK(!templ.IsEmpty()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11723 | LocalContext context; |
| 11724 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11725 | GenerateSomeGarbage(); |
| 11726 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11727 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11728 | "o.foo = 17;" |
| 11729 | "var receiver = {};" |
| 11730 | "receiver.__proto__ = o;" |
| 11731 | "var result = 0;" |
| 11732 | "for (var i = 0; i < 100; i++) {" |
| 11733 | " result = receiver.method(41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11734 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11735 | |
| 11736 | CHECK_EQ(42, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11737 | } |
| 11738 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11739 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11740 | THREADED_PROFILED_TEST(CallICFastApi_SimpleSignature_Miss1) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11741 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11742 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11743 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11744 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11745 | v8_str("method_data"), |
| 11746 | v8::Signature::New(fun_templ)); |
| 11747 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11748 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11749 | fun_templ->SetHiddenPrototype(true); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11750 | v8::Handle<v8::ObjectTemplate> templ(fun_templ->InstanceTemplate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11751 | CHECK(!templ.IsEmpty()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11752 | LocalContext context; |
| 11753 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11754 | GenerateSomeGarbage(); |
| 11755 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11756 | CompileRun( |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11757 | "o.foo = 17;" |
| 11758 | "var receiver = {};" |
| 11759 | "receiver.__proto__ = o;" |
| 11760 | "var result = 0;" |
| 11761 | "var saved_result = 0;" |
| 11762 | "for (var i = 0; i < 100; i++) {" |
| 11763 | " result = receiver.method(41);" |
| 11764 | " if (i == 50) {" |
| 11765 | " saved_result = result;" |
| 11766 | " receiver = {method: function(x) { return x - 1 }};" |
| 11767 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11768 | "}"); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 11769 | CHECK_EQ(40, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11770 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11771 | } |
| 11772 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11773 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11774 | THREADED_PROFILED_TEST(CallICFastApi_SimpleSignature_Miss2) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11775 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11776 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11777 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11778 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11779 | v8_str("method_data"), |
| 11780 | v8::Signature::New(fun_templ)); |
| 11781 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11782 | proto_templ->Set(v8_str("method"), method_templ); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11783 | fun_templ->SetHiddenPrototype(true); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 11784 | v8::Handle<v8::ObjectTemplate> templ(fun_templ->InstanceTemplate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11785 | CHECK(!templ.IsEmpty()); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11786 | LocalContext context; |
| 11787 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11788 | GenerateSomeGarbage(); |
| 11789 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
| 11790 | v8::TryCatch try_catch; |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11791 | CompileRun( |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11792 | "o.foo = 17;" |
| 11793 | "var receiver = {};" |
| 11794 | "receiver.__proto__ = o;" |
| 11795 | "var result = 0;" |
| 11796 | "var saved_result = 0;" |
| 11797 | "for (var i = 0; i < 100; i++) {" |
| 11798 | " result = receiver.method(41);" |
| 11799 | " if (i == 50) {" |
| 11800 | " saved_result = result;" |
| 11801 | " receiver = 333;" |
| 11802 | " }" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11803 | "}"); |
sgjesse@chromium.org | 534ae57 | 2010-02-24 22:09:39 +0000 | [diff] [blame] | 11804 | CHECK(try_catch.HasCaught()); |
| 11805 | CHECK_EQ(v8_str("TypeError: Object 333 has no method 'method'"), |
| 11806 | try_catch.Exception()->ToString()); |
| 11807 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11808 | } |
| 11809 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11810 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11811 | THREADED_PROFILED_TEST(CallICFastApi_SimpleSignature_TypeError) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11812 | v8::HandleScope scope(CcTest::isolate()); |
mmassi@chromium.org | 49a4467 | 2012-12-04 13:52:03 +0000 | [diff] [blame] | 11813 | v8::Handle<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 11814 | v8::Handle<v8::FunctionTemplate> method_templ = |
| 11815 | v8::FunctionTemplate::New(FastApiCallback_SimpleSignature, |
| 11816 | v8_str("method_data"), |
| 11817 | v8::Signature::New(fun_templ)); |
| 11818 | v8::Handle<v8::ObjectTemplate> proto_templ = fun_templ->PrototypeTemplate(); |
| 11819 | proto_templ->Set(v8_str("method"), method_templ); |
| 11820 | fun_templ->SetHiddenPrototype(true); |
| 11821 | v8::Handle<v8::ObjectTemplate> templ(fun_templ->InstanceTemplate()); |
| 11822 | CHECK(!templ.IsEmpty()); |
| 11823 | LocalContext context; |
| 11824 | v8::Handle<v8::Function> fun = fun_templ->GetFunction(); |
| 11825 | GenerateSomeGarbage(); |
| 11826 | context->Global()->Set(v8_str("o"), fun->NewInstance()); |
| 11827 | v8::TryCatch try_catch; |
| 11828 | CompileRun( |
| 11829 | "o.foo = 17;" |
| 11830 | "var receiver = {};" |
| 11831 | "receiver.__proto__ = o;" |
| 11832 | "var result = 0;" |
| 11833 | "var saved_result = 0;" |
| 11834 | "for (var i = 0; i < 100; i++) {" |
| 11835 | " result = receiver.method(41);" |
| 11836 | " if (i == 50) {" |
| 11837 | " saved_result = result;" |
| 11838 | " receiver = Object.create(receiver);" |
| 11839 | " }" |
| 11840 | "}"); |
| 11841 | CHECK(try_catch.HasCaught()); |
| 11842 | CHECK_EQ(v8_str("TypeError: Illegal invocation"), |
| 11843 | try_catch.Exception()->ToString()); |
| 11844 | CHECK_EQ(42, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11845 | } |
| 11846 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 11847 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11848 | v8::Handle<Value> keyed_call_ic_function; |
| 11849 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11850 | static void InterceptorKeyedCallICGetter( |
| 11851 | Local<String> name, |
| 11852 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11853 | ApiTestFuzzer::Fuzz(); |
| 11854 | if (v8_str("x")->Equals(name)) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 11855 | info.GetReturnValue().Set(keyed_call_ic_function); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11856 | } |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11857 | } |
| 11858 | |
| 11859 | |
| 11860 | // Test the case when we stored cacheable lookup into |
| 11861 | // a stub, but the function name changed (to another cacheable function). |
| 11862 | THREADED_TEST(InterceptorKeyedCallICKeyChange1) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11863 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11864 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11865 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11866 | LocalContext context; |
| 11867 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11868 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11869 | "proto = new Object();" |
| 11870 | "proto.y = function(x) { return x + 1; };" |
| 11871 | "proto.z = function(x) { return x - 1; };" |
| 11872 | "o.__proto__ = proto;" |
| 11873 | "var result = 0;" |
| 11874 | "var method = 'y';" |
| 11875 | "for (var i = 0; i < 10; i++) {" |
| 11876 | " if (i == 5) { method = 'z'; };" |
| 11877 | " result += o[method](41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11878 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11879 | CHECK_EQ(42*5 + 40*5, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11880 | } |
| 11881 | |
| 11882 | |
| 11883 | // Test the case when we stored cacheable lookup into |
| 11884 | // a stub, but the function name changed (and the new function is present |
| 11885 | // both before and after the interceptor in the prototype chain). |
| 11886 | THREADED_TEST(InterceptorKeyedCallICKeyChange2) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11887 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11888 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11889 | templ->SetNamedPropertyHandler(InterceptorKeyedCallICGetter); |
| 11890 | LocalContext context; |
| 11891 | context->Global()->Set(v8_str("proto1"), templ->NewInstance()); |
| 11892 | keyed_call_ic_function = |
| 11893 | v8_compile("function f(x) { return x - 1; }; f")->Run(); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11894 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11895 | "o = new Object();" |
| 11896 | "proto2 = new Object();" |
| 11897 | "o.y = function(x) { return x + 1; };" |
| 11898 | "proto2.y = function(x) { return x + 2; };" |
| 11899 | "o.__proto__ = proto1;" |
| 11900 | "proto1.__proto__ = proto2;" |
| 11901 | "var result = 0;" |
| 11902 | "var method = 'x';" |
| 11903 | "for (var i = 0; i < 10; i++) {" |
| 11904 | " if (i == 5) { method = 'y'; };" |
| 11905 | " result += o[method](41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11906 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11907 | CHECK_EQ(42*5 + 40*5, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11908 | } |
| 11909 | |
| 11910 | |
| 11911 | // Same as InterceptorKeyedCallICKeyChange1 only the cacheable function sit |
| 11912 | // on the global object. |
| 11913 | THREADED_TEST(InterceptorKeyedCallICKeyChangeOnGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11914 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11915 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 11916 | templ->SetNamedPropertyHandler(NoBlockGetterX); |
| 11917 | LocalContext context; |
| 11918 | context->Global()->Set(v8_str("o"), templ->NewInstance()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11919 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11920 | "function inc(x) { return x + 1; };" |
| 11921 | "inc(1);" |
| 11922 | "function dec(x) { return x - 1; };" |
| 11923 | "dec(1);" |
| 11924 | "o.__proto__ = this;" |
| 11925 | "this.__proto__.x = inc;" |
| 11926 | "this.__proto__.y = dec;" |
| 11927 | "var result = 0;" |
| 11928 | "var method = 'x';" |
| 11929 | "for (var i = 0; i < 10; i++) {" |
| 11930 | " if (i == 5) { method = 'y'; };" |
| 11931 | " result += o[method](41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11932 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11933 | CHECK_EQ(42*5 + 40*5, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11934 | } |
| 11935 | |
| 11936 | |
| 11937 | // Test the case when actual function to call sits on global object. |
| 11938 | THREADED_TEST(InterceptorKeyedCallICFromGlobal) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11939 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11940 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 11941 | templ_o->SetNamedPropertyHandler(NoBlockGetterX); |
| 11942 | LocalContext context; |
| 11943 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 11944 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11945 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11946 | "function len(x) { return x.length; };" |
| 11947 | "o.__proto__ = this;" |
| 11948 | "var m = 'parseFloat';" |
| 11949 | "var result = 0;" |
| 11950 | "for (var i = 0; i < 10; i++) {" |
| 11951 | " if (i == 5) {" |
| 11952 | " m = 'len';" |
| 11953 | " saved_result = result;" |
| 11954 | " };" |
| 11955 | " result = o[m]('239');" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11956 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11957 | CHECK_EQ(3, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11958 | CHECK_EQ(239, context->Global()->Get(v8_str("saved_result"))->Int32Value()); |
| 11959 | } |
| 11960 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 11961 | |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11962 | // Test the map transition before the interceptor. |
| 11963 | THREADED_TEST(InterceptorKeyedCallICMapChangeBefore) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11964 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11965 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 11966 | templ_o->SetNamedPropertyHandler(NoBlockGetterX); |
| 11967 | LocalContext context; |
| 11968 | context->Global()->Set(v8_str("proto"), templ_o->NewInstance()); |
| 11969 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11970 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11971 | "var o = new Object();" |
| 11972 | "o.__proto__ = proto;" |
| 11973 | "o.method = function(x) { return x + 1; };" |
| 11974 | "var m = 'method';" |
| 11975 | "var result = 0;" |
| 11976 | "for (var i = 0; i < 10; i++) {" |
| 11977 | " if (i == 5) { o.method = function(x) { return x - 1; }; };" |
| 11978 | " result += o[m](41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11979 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11980 | CHECK_EQ(42*5 + 40*5, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 11981 | } |
| 11982 | |
| 11983 | |
| 11984 | // Test the map transition after the interceptor. |
| 11985 | THREADED_TEST(InterceptorKeyedCallICMapChangeAfter) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 11986 | v8::HandleScope scope(CcTest::isolate()); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11987 | v8::Handle<v8::ObjectTemplate> templ_o = ObjectTemplate::New(); |
| 11988 | templ_o->SetNamedPropertyHandler(NoBlockGetterX); |
| 11989 | LocalContext context; |
| 11990 | context->Global()->Set(v8_str("o"), templ_o->NewInstance()); |
| 11991 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 11992 | CompileRun( |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 11993 | "var proto = new Object();" |
| 11994 | "o.__proto__ = proto;" |
| 11995 | "proto.method = function(x) { return x + 1; };" |
| 11996 | "var m = 'method';" |
| 11997 | "var result = 0;" |
| 11998 | "for (var i = 0; i < 10; i++) {" |
| 11999 | " if (i == 5) { proto.method = function(x) { return x - 1; }; };" |
| 12000 | " result += o[m](41);" |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 12001 | "}"); |
lrn@chromium.org | 1af7e1b | 2010-06-07 11:12:01 +0000 | [diff] [blame] | 12002 | CHECK_EQ(42*5 + 40*5, context->Global()->Get(v8_str("result"))->Int32Value()); |
| 12003 | } |
| 12004 | |
| 12005 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12006 | static int interceptor_call_count = 0; |
| 12007 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12008 | static void InterceptorICRefErrorGetter( |
| 12009 | Local<String> name, |
| 12010 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12011 | ApiTestFuzzer::Fuzz(); |
| 12012 | if (v8_str("x")->Equals(name) && interceptor_call_count++ < 20) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12013 | info.GetReturnValue().Set(call_ic_function2); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12014 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12015 | } |
| 12016 | |
| 12017 | |
| 12018 | // This test should hit load and call ICs for the interceptor case. |
| 12019 | // Once in a while, the interceptor will reply that a property was not |
| 12020 | // found in which case we should get a reference error. |
| 12021 | THREADED_TEST(InterceptorICReferenceErrors) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12022 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12023 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 12024 | templ->SetNamedPropertyHandler(InterceptorICRefErrorGetter); |
| 12025 | LocalContext context(0, templ, v8::Handle<Value>()); |
| 12026 | call_ic_function2 = v8_compile("function h(x) { return x; }; h")->Run(); |
| 12027 | v8::Handle<Value> value = CompileRun( |
| 12028 | "function f() {" |
| 12029 | " for (var i = 0; i < 1000; i++) {" |
| 12030 | " try { x; } catch(e) { return true; }" |
| 12031 | " }" |
| 12032 | " return false;" |
| 12033 | "};" |
| 12034 | "f();"); |
| 12035 | CHECK_EQ(true, value->BooleanValue()); |
| 12036 | interceptor_call_count = 0; |
| 12037 | value = CompileRun( |
| 12038 | "function g() {" |
| 12039 | " for (var i = 0; i < 1000; i++) {" |
| 12040 | " try { x(42); } catch(e) { return true; }" |
| 12041 | " }" |
| 12042 | " return false;" |
| 12043 | "};" |
| 12044 | "g();"); |
| 12045 | CHECK_EQ(true, value->BooleanValue()); |
| 12046 | } |
| 12047 | |
| 12048 | |
| 12049 | static int interceptor_ic_exception_get_count = 0; |
| 12050 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12051 | static void InterceptorICExceptionGetter( |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12052 | Local<String> name, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12053 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12054 | ApiTestFuzzer::Fuzz(); |
| 12055 | if (v8_str("x")->Equals(name) && ++interceptor_ic_exception_get_count < 20) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12056 | info.GetReturnValue().Set(call_ic_function3); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12057 | } |
| 12058 | if (interceptor_ic_exception_get_count == 20) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12059 | info.GetIsolate()->ThrowException(v8_num(42)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12060 | return; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12061 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12062 | } |
| 12063 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12064 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12065 | // Test interceptor load/call IC where the interceptor throws an |
| 12066 | // exception once in a while. |
| 12067 | THREADED_TEST(InterceptorICGetterExceptions) { |
| 12068 | interceptor_ic_exception_get_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12069 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12070 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 12071 | templ->SetNamedPropertyHandler(InterceptorICExceptionGetter); |
| 12072 | LocalContext context(0, templ, v8::Handle<Value>()); |
| 12073 | call_ic_function3 = v8_compile("function h(x) { return x; }; h")->Run(); |
| 12074 | v8::Handle<Value> value = CompileRun( |
| 12075 | "function f() {" |
| 12076 | " for (var i = 0; i < 100; i++) {" |
| 12077 | " try { x; } catch(e) { return true; }" |
| 12078 | " }" |
| 12079 | " return false;" |
| 12080 | "};" |
| 12081 | "f();"); |
| 12082 | CHECK_EQ(true, value->BooleanValue()); |
| 12083 | interceptor_ic_exception_get_count = 0; |
| 12084 | value = CompileRun( |
| 12085 | "function f() {" |
| 12086 | " for (var i = 0; i < 100; i++) {" |
| 12087 | " try { x(42); } catch(e) { return true; }" |
| 12088 | " }" |
| 12089 | " return false;" |
| 12090 | "};" |
| 12091 | "f();"); |
| 12092 | CHECK_EQ(true, value->BooleanValue()); |
| 12093 | } |
| 12094 | |
| 12095 | |
| 12096 | static int interceptor_ic_exception_set_count = 0; |
| 12097 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12098 | static void InterceptorICExceptionSetter( |
| 12099 | Local<String> key, |
| 12100 | Local<Value> value, |
| 12101 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12102 | ApiTestFuzzer::Fuzz(); |
| 12103 | if (++interceptor_ic_exception_set_count > 20) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12104 | info.GetIsolate()->ThrowException(v8_num(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12105 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12106 | } |
| 12107 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12108 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12109 | // Test interceptor store IC where the interceptor throws an exception |
| 12110 | // once in a while. |
| 12111 | THREADED_TEST(InterceptorICSetterExceptions) { |
| 12112 | interceptor_ic_exception_set_count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12113 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12114 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
| 12115 | templ->SetNamedPropertyHandler(0, InterceptorICExceptionSetter); |
| 12116 | LocalContext context(0, templ, v8::Handle<Value>()); |
| 12117 | v8::Handle<Value> value = CompileRun( |
| 12118 | "function f() {" |
| 12119 | " for (var i = 0; i < 100; i++) {" |
| 12120 | " try { x = 42; } catch(e) { return true; }" |
| 12121 | " }" |
| 12122 | " return false;" |
| 12123 | "};" |
| 12124 | "f();"); |
| 12125 | CHECK_EQ(true, value->BooleanValue()); |
| 12126 | } |
| 12127 | |
| 12128 | |
| 12129 | // Test that we ignore null interceptors. |
| 12130 | THREADED_TEST(NullNamedInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12131 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12132 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12133 | templ->SetNamedPropertyHandler( |
| 12134 | static_cast<v8::NamedPropertyGetterCallback>(0)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12135 | LocalContext context; |
| 12136 | templ->Set("x", v8_num(42)); |
| 12137 | v8::Handle<v8::Object> obj = templ->NewInstance(); |
| 12138 | context->Global()->Set(v8_str("obj"), obj); |
| 12139 | v8::Handle<Value> value = CompileRun("obj.x"); |
| 12140 | CHECK(value->IsInt32()); |
| 12141 | CHECK_EQ(42, value->Int32Value()); |
| 12142 | } |
| 12143 | |
| 12144 | |
| 12145 | // Test that we ignore null interceptors. |
| 12146 | THREADED_TEST(NullIndexedInterceptor) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12147 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12148 | v8::Handle<v8::ObjectTemplate> templ = ObjectTemplate::New(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12149 | templ->SetIndexedPropertyHandler( |
| 12150 | static_cast<v8::IndexedPropertyGetterCallback>(0)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12151 | LocalContext context; |
| 12152 | templ->Set("42", v8_num(42)); |
| 12153 | v8::Handle<v8::Object> obj = templ->NewInstance(); |
| 12154 | context->Global()->Set(v8_str("obj"), obj); |
| 12155 | v8::Handle<Value> value = CompileRun("obj[42]"); |
| 12156 | CHECK(value->IsInt32()); |
| 12157 | CHECK_EQ(42, value->Int32Value()); |
| 12158 | } |
| 12159 | |
| 12160 | |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 12161 | THREADED_TEST(NamedPropertyHandlerGetterAttributes) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12162 | v8::HandleScope scope(CcTest::isolate()); |
ricow@chromium.org | 30ce411 | 2010-05-31 10:38:25 +0000 | [diff] [blame] | 12163 | v8::Handle<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 12164 | templ->InstanceTemplate()->SetNamedPropertyHandler(InterceptorLoadXICGetter); |
| 12165 | LocalContext env; |
| 12166 | env->Global()->Set(v8_str("obj"), |
| 12167 | templ->GetFunction()->NewInstance()); |
| 12168 | ExpectTrue("obj.x === 42"); |
| 12169 | ExpectTrue("!obj.propertyIsEnumerable('x')"); |
| 12170 | } |
| 12171 | |
| 12172 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12173 | static void ThrowingGetter(Local<String> name, |
| 12174 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12175 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12176 | info.GetIsolate()->ThrowException(Handle<Value>()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12177 | info.GetReturnValue().SetUndefined(); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12178 | } |
| 12179 | |
| 12180 | |
| 12181 | THREADED_TEST(VariousGetPropertiesAndThrowingCallbacks) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12182 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12183 | HandleScope scope(context->GetIsolate()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12184 | |
| 12185 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 12186 | Local<ObjectTemplate> instance_templ = templ->InstanceTemplate(); |
| 12187 | instance_templ->SetAccessor(v8_str("f"), ThrowingGetter); |
| 12188 | |
| 12189 | Local<Object> instance = templ->GetFunction()->NewInstance(); |
| 12190 | |
| 12191 | Local<Object> another = Object::New(); |
| 12192 | another->SetPrototype(instance); |
| 12193 | |
| 12194 | Local<Object> with_js_getter = CompileRun( |
| 12195 | "o = {};\n" |
| 12196 | "o.__defineGetter__('f', function() { throw undefined; });\n" |
| 12197 | "o\n").As<Object>(); |
| 12198 | CHECK(!with_js_getter.IsEmpty()); |
| 12199 | |
| 12200 | TryCatch try_catch; |
| 12201 | |
| 12202 | Local<Value> result = instance->GetRealNamedProperty(v8_str("f")); |
| 12203 | CHECK(try_catch.HasCaught()); |
| 12204 | try_catch.Reset(); |
| 12205 | CHECK(result.IsEmpty()); |
| 12206 | |
| 12207 | result = another->GetRealNamedProperty(v8_str("f")); |
| 12208 | CHECK(try_catch.HasCaught()); |
| 12209 | try_catch.Reset(); |
| 12210 | CHECK(result.IsEmpty()); |
| 12211 | |
| 12212 | result = another->GetRealNamedPropertyInPrototypeChain(v8_str("f")); |
| 12213 | CHECK(try_catch.HasCaught()); |
| 12214 | try_catch.Reset(); |
| 12215 | CHECK(result.IsEmpty()); |
| 12216 | |
| 12217 | result = another->Get(v8_str("f")); |
| 12218 | CHECK(try_catch.HasCaught()); |
| 12219 | try_catch.Reset(); |
| 12220 | CHECK(result.IsEmpty()); |
| 12221 | |
| 12222 | result = with_js_getter->GetRealNamedProperty(v8_str("f")); |
| 12223 | CHECK(try_catch.HasCaught()); |
| 12224 | try_catch.Reset(); |
| 12225 | CHECK(result.IsEmpty()); |
| 12226 | |
| 12227 | result = with_js_getter->Get(v8_str("f")); |
| 12228 | CHECK(try_catch.HasCaught()); |
| 12229 | try_catch.Reset(); |
| 12230 | CHECK(result.IsEmpty()); |
| 12231 | } |
| 12232 | |
| 12233 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12234 | static void ThrowingCallbackWithTryCatch( |
| 12235 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12236 | TryCatch try_catch; |
| 12237 | // Verboseness is important: it triggers message delivery which can call into |
| 12238 | // external code. |
| 12239 | try_catch.SetVerbose(true); |
| 12240 | CompileRun("throw 'from JS';"); |
| 12241 | CHECK(try_catch.HasCaught()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12242 | CHECK(!CcTest::i_isolate()->has_pending_exception()); |
| 12243 | CHECK(!CcTest::i_isolate()->has_scheduled_exception()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12244 | } |
| 12245 | |
| 12246 | |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 12247 | static int call_depth; |
| 12248 | |
| 12249 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12250 | static void WithTryCatch(Handle<Message> message, Handle<Value> data) { |
| 12251 | TryCatch try_catch; |
| 12252 | } |
| 12253 | |
| 12254 | |
| 12255 | static void ThrowFromJS(Handle<Message> message, Handle<Value> data) { |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 12256 | if (--call_depth) CompileRun("throw 'ThrowInJS';"); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12257 | } |
| 12258 | |
| 12259 | |
| 12260 | static void ThrowViaApi(Handle<Message> message, Handle<Value> data) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12261 | if (--call_depth) CcTest::isolate()->ThrowException(v8_str("ThrowViaApi")); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12262 | } |
| 12263 | |
| 12264 | |
| 12265 | static void WebKitLike(Handle<Message> message, Handle<Value> data) { |
| 12266 | Handle<String> errorMessageString = message->Get(); |
| 12267 | CHECK(!errorMessageString.IsEmpty()); |
| 12268 | message->GetStackTrace(); |
| 12269 | message->GetScriptResourceName(); |
| 12270 | } |
| 12271 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12272 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12273 | THREADED_TEST(ExceptionsDoNotPropagatePastTryCatch) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12274 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12275 | HandleScope scope(context->GetIsolate()); |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12276 | |
| 12277 | Local<Function> func = |
| 12278 | FunctionTemplate::New(ThrowingCallbackWithTryCatch)->GetFunction(); |
| 12279 | context->Global()->Set(v8_str("func"), func); |
| 12280 | |
| 12281 | MessageCallback callbacks[] = |
| 12282 | { NULL, WebKitLike, ThrowViaApi, ThrowFromJS, WithTryCatch }; |
| 12283 | for (unsigned i = 0; i < sizeof(callbacks)/sizeof(callbacks[0]); i++) { |
| 12284 | MessageCallback callback = callbacks[i]; |
| 12285 | if (callback != NULL) { |
| 12286 | V8::AddMessageListener(callback); |
| 12287 | } |
ricow@chromium.org | dcebac0 | 2011-04-20 09:44:50 +0000 | [diff] [blame] | 12288 | // Some small number to control number of times message handler should |
| 12289 | // throw an exception. |
ager@chromium.org | a9aa5fa | 2011-04-13 08:46:07 +0000 | [diff] [blame] | 12290 | call_depth = 5; |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 12291 | ExpectFalse( |
| 12292 | "var thrown = false;\n" |
| 12293 | "try { func(); } catch(e) { thrown = true; }\n" |
| 12294 | "thrown\n"); |
| 12295 | if (callback != NULL) { |
| 12296 | V8::RemoveMessageListeners(callback); |
| 12297 | } |
| 12298 | } |
| 12299 | } |
| 12300 | |
| 12301 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12302 | static void ParentGetter(Local<String> name, |
| 12303 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12304 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12305 | info.GetReturnValue().Set(v8_num(1)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12306 | } |
| 12307 | |
| 12308 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12309 | static void ChildGetter(Local<String> name, |
| 12310 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12311 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12312 | info.GetReturnValue().Set(v8_num(42)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12313 | } |
| 12314 | |
| 12315 | |
| 12316 | THREADED_TEST(Overriding) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12317 | i::FLAG_es5_readonly = true; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12318 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12319 | v8::HandleScope scope(context->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12320 | |
| 12321 | // Parent template. |
| 12322 | Local<v8::FunctionTemplate> parent_templ = v8::FunctionTemplate::New(); |
| 12323 | Local<ObjectTemplate> parent_instance_templ = |
| 12324 | parent_templ->InstanceTemplate(); |
| 12325 | parent_instance_templ->SetAccessor(v8_str("f"), ParentGetter); |
| 12326 | |
| 12327 | // Template that inherits from the parent template. |
| 12328 | Local<v8::FunctionTemplate> child_templ = v8::FunctionTemplate::New(); |
| 12329 | Local<ObjectTemplate> child_instance_templ = |
| 12330 | child_templ->InstanceTemplate(); |
| 12331 | child_templ->Inherit(parent_templ); |
| 12332 | // Override 'f'. The child version of 'f' should get called for child |
| 12333 | // instances. |
| 12334 | child_instance_templ->SetAccessor(v8_str("f"), ChildGetter); |
| 12335 | // Add 'g' twice. The 'g' added last should get called for instances. |
| 12336 | child_instance_templ->SetAccessor(v8_str("g"), ParentGetter); |
| 12337 | child_instance_templ->SetAccessor(v8_str("g"), ChildGetter); |
| 12338 | |
| 12339 | // Add 'h' as an accessor to the proto template with ReadOnly attributes |
| 12340 | // so 'h' can be shadowed on the instance object. |
| 12341 | Local<ObjectTemplate> child_proto_templ = child_templ->PrototypeTemplate(); |
| 12342 | child_proto_templ->SetAccessor(v8_str("h"), ParentGetter, 0, |
| 12343 | v8::Handle<Value>(), v8::DEFAULT, v8::ReadOnly); |
| 12344 | |
| 12345 | // Add 'i' as an accessor to the instance template with ReadOnly attributes |
| 12346 | // but the attribute does not have effect because it is duplicated with |
| 12347 | // NULL setter. |
| 12348 | child_instance_templ->SetAccessor(v8_str("i"), ChildGetter, 0, |
| 12349 | v8::Handle<Value>(), v8::DEFAULT, v8::ReadOnly); |
| 12350 | |
| 12351 | |
| 12352 | |
| 12353 | // Instantiate the child template. |
| 12354 | Local<v8::Object> instance = child_templ->GetFunction()->NewInstance(); |
| 12355 | |
| 12356 | // Check that the child function overrides the parent one. |
| 12357 | context->Global()->Set(v8_str("o"), instance); |
| 12358 | Local<Value> value = v8_compile("o.f")->Run(); |
| 12359 | // Check that the 'g' that was added last is hit. |
| 12360 | CHECK_EQ(42, value->Int32Value()); |
| 12361 | value = v8_compile("o.g")->Run(); |
| 12362 | CHECK_EQ(42, value->Int32Value()); |
| 12363 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12364 | // Check that 'h' cannot be shadowed. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12365 | value = v8_compile("o.h = 3; o.h")->Run(); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12366 | CHECK_EQ(1, value->Int32Value()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12367 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 12368 | // Check that 'i' cannot be shadowed or changed. |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12369 | value = v8_compile("o.i = 3; o.i")->Run(); |
| 12370 | CHECK_EQ(42, value->Int32Value()); |
| 12371 | } |
| 12372 | |
| 12373 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12374 | static void IsConstructHandler( |
| 12375 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12376 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12377 | args.GetReturnValue().Set(args.IsConstructCall()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12378 | } |
| 12379 | |
| 12380 | |
| 12381 | THREADED_TEST(IsConstructCall) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12382 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12383 | |
| 12384 | // Function template with call handler. |
| 12385 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 12386 | templ->SetCallHandler(IsConstructHandler); |
| 12387 | |
| 12388 | LocalContext context; |
| 12389 | |
| 12390 | context->Global()->Set(v8_str("f"), templ->GetFunction()); |
| 12391 | Local<Value> value = v8_compile("f()")->Run(); |
| 12392 | CHECK(!value->BooleanValue()); |
| 12393 | value = v8_compile("new f()")->Run(); |
| 12394 | CHECK(value->BooleanValue()); |
| 12395 | } |
| 12396 | |
| 12397 | |
| 12398 | THREADED_TEST(ObjectProtoToString) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12399 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12400 | Local<v8::FunctionTemplate> templ = v8::FunctionTemplate::New(); |
| 12401 | templ->SetClassName(v8_str("MyClass")); |
| 12402 | |
| 12403 | LocalContext context; |
| 12404 | |
| 12405 | Local<String> customized_tostring = v8_str("customized toString"); |
| 12406 | |
| 12407 | // Replace Object.prototype.toString |
| 12408 | v8_compile("Object.prototype.toString = function() {" |
| 12409 | " return 'customized toString';" |
| 12410 | "}")->Run(); |
| 12411 | |
| 12412 | // Normal ToString call should call replaced Object.prototype.toString |
| 12413 | Local<v8::Object> instance = templ->GetFunction()->NewInstance(); |
| 12414 | Local<String> value = instance->ToString(); |
| 12415 | CHECK(value->IsString() && value->Equals(customized_tostring)); |
| 12416 | |
| 12417 | // ObjectProtoToString should not call replace toString function. |
| 12418 | value = instance->ObjectProtoToString(); |
| 12419 | CHECK(value->IsString() && value->Equals(v8_str("[object MyClass]"))); |
| 12420 | |
| 12421 | // Check global |
| 12422 | value = context->Global()->ObjectProtoToString(); |
| 12423 | CHECK(value->IsString() && value->Equals(v8_str("[object global]"))); |
| 12424 | |
| 12425 | // Check ordinary object |
| 12426 | Local<Value> object = v8_compile("new Object()")->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 12427 | value = object.As<v8::Object>()->ObjectProtoToString(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12428 | CHECK(value->IsString() && value->Equals(v8_str("[object Object]"))); |
| 12429 | } |
| 12430 | |
| 12431 | |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 12432 | THREADED_TEST(ObjectGetConstructorName) { |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 12433 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12434 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 12435 | v8_compile("function Parent() {};" |
| 12436 | "function Child() {};" |
| 12437 | "Child.prototype = new Parent();" |
| 12438 | "var outer = { inner: function() { } };" |
| 12439 | "var p = new Parent();" |
| 12440 | "var c = new Child();" |
| 12441 | "var x = new outer.inner();")->Run(); |
| 12442 | |
| 12443 | Local<v8::Value> p = context->Global()->Get(v8_str("p")); |
| 12444 | CHECK(p->IsObject() && p->ToObject()->GetConstructorName()->Equals( |
| 12445 | v8_str("Parent"))); |
| 12446 | |
| 12447 | Local<v8::Value> c = context->Global()->Get(v8_str("c")); |
| 12448 | CHECK(c->IsObject() && c->ToObject()->GetConstructorName()->Equals( |
| 12449 | v8_str("Child"))); |
| 12450 | |
| 12451 | Local<v8::Value> x = context->Global()->Get(v8_str("x")); |
| 12452 | CHECK(x->IsObject() && x->ToObject()->GetConstructorName()->Equals( |
| 12453 | v8_str("outer.inner"))); |
| 12454 | } |
| 12455 | |
| 12456 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12457 | bool ApiTestFuzzer::fuzzing_ = false; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12458 | i::Semaphore ApiTestFuzzer::all_tests_done_(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12459 | int ApiTestFuzzer::active_tests_; |
| 12460 | int ApiTestFuzzer::tests_being_run_; |
| 12461 | int ApiTestFuzzer::current_; |
| 12462 | |
| 12463 | |
| 12464 | // We are in a callback and want to switch to another thread (if we |
| 12465 | // are currently running the thread fuzzing test). |
| 12466 | void ApiTestFuzzer::Fuzz() { |
| 12467 | if (!fuzzing_) return; |
| 12468 | ApiTestFuzzer* test = RegisterThreadedTest::nth(current_)->fuzzer_; |
| 12469 | test->ContextSwitch(); |
| 12470 | } |
| 12471 | |
| 12472 | |
| 12473 | // Let the next thread go. Since it is also waiting on the V8 lock it may |
| 12474 | // not start immediately. |
| 12475 | bool ApiTestFuzzer::NextThread() { |
| 12476 | int test_position = GetNextTestNumber(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12477 | const char* test_name = RegisterThreadedTest::nth(current_)->name(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12478 | if (test_position == current_) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12479 | if (kLogThreading) |
| 12480 | printf("Stay with %s\n", test_name); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12481 | return false; |
| 12482 | } |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12483 | if (kLogThreading) { |
| 12484 | printf("Switch from %s to %s\n", |
| 12485 | test_name, |
| 12486 | RegisterThreadedTest::nth(test_position)->name()); |
| 12487 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12488 | current_ = test_position; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12489 | RegisterThreadedTest::nth(current_)->fuzzer_->gate_.Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12490 | return true; |
| 12491 | } |
| 12492 | |
| 12493 | |
| 12494 | void ApiTestFuzzer::Run() { |
| 12495 | // When it is our turn... |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12496 | gate_.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12497 | { |
| 12498 | // ... get the V8 lock and start running the test. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12499 | v8::Locker locker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12500 | CallTest(); |
| 12501 | } |
| 12502 | // This test finished. |
| 12503 | active_ = false; |
| 12504 | active_tests_--; |
| 12505 | // If it was the last then signal that fact. |
| 12506 | if (active_tests_ == 0) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12507 | all_tests_done_.Signal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12508 | } else { |
| 12509 | // Otherwise select a new test and start that. |
| 12510 | NextThread(); |
| 12511 | } |
| 12512 | } |
| 12513 | |
| 12514 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12515 | static unsigned linear_congruential_generator; |
| 12516 | |
| 12517 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12518 | void ApiTestFuzzer::SetUp(PartOfTest part) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 12519 | linear_congruential_generator = i::FLAG_testing_prng_seed; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12520 | fuzzing_ = true; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 12521 | int count = RegisterThreadedTest::count(); |
| 12522 | int start = count * part / (LAST_PART + 1); |
| 12523 | int end = (count * (part + 1) / (LAST_PART + 1)) - 1; |
| 12524 | active_tests_ = tests_being_run_ = end - start + 1; |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12525 | for (int i = 0; i < tests_being_run_; i++) { |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 12526 | RegisterThreadedTest::nth(i)->fuzzer_ = new ApiTestFuzzer(i + start); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12527 | } |
| 12528 | for (int i = 0; i < active_tests_; i++) { |
| 12529 | RegisterThreadedTest::nth(i)->fuzzer_->Start(); |
| 12530 | } |
| 12531 | } |
| 12532 | |
| 12533 | |
| 12534 | static void CallTestNumber(int test_number) { |
| 12535 | (RegisterThreadedTest::nth(test_number)->callback())(); |
| 12536 | } |
| 12537 | |
| 12538 | |
| 12539 | void ApiTestFuzzer::RunAllTests() { |
| 12540 | // Set off the first test. |
| 12541 | current_ = -1; |
| 12542 | NextThread(); |
| 12543 | // Wait till they are all done. |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12544 | all_tests_done_.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12545 | } |
| 12546 | |
| 12547 | |
| 12548 | int ApiTestFuzzer::GetNextTestNumber() { |
| 12549 | int next_test; |
| 12550 | do { |
| 12551 | next_test = (linear_congruential_generator >> 16) % tests_being_run_; |
| 12552 | linear_congruential_generator *= 1664525u; |
| 12553 | linear_congruential_generator += 1013904223u; |
| 12554 | } while (!RegisterThreadedTest::nth(next_test)->fuzzer_->active_); |
| 12555 | return next_test; |
| 12556 | } |
| 12557 | |
| 12558 | |
| 12559 | void ApiTestFuzzer::ContextSwitch() { |
| 12560 | // If the new thread is the same as the current thread there is nothing to do. |
| 12561 | if (NextThread()) { |
| 12562 | // Now it can start. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12563 | v8::Unlocker unlocker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12564 | // Wait till someone starts us again. |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12565 | gate_.Wait(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12566 | // And we're off. |
| 12567 | } |
| 12568 | } |
| 12569 | |
| 12570 | |
| 12571 | void ApiTestFuzzer::TearDown() { |
| 12572 | fuzzing_ = false; |
ager@chromium.org | 41826e7 | 2009-03-30 13:30:57 +0000 | [diff] [blame] | 12573 | for (int i = 0; i < RegisterThreadedTest::count(); i++) { |
| 12574 | ApiTestFuzzer *fuzzer = RegisterThreadedTest::nth(i)->fuzzer_; |
| 12575 | if (fuzzer != NULL) fuzzer->Join(); |
| 12576 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12577 | } |
| 12578 | |
| 12579 | |
| 12580 | // Lets not be needlessly self-referential. |
mmassi@chromium.org | 2f0efde | 2013-02-06 14:12:58 +0000 | [diff] [blame] | 12581 | TEST(Threading1) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12582 | ApiTestFuzzer::SetUp(ApiTestFuzzer::FIRST_PART); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12583 | ApiTestFuzzer::RunAllTests(); |
| 12584 | ApiTestFuzzer::TearDown(); |
| 12585 | } |
| 12586 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12587 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12588 | TEST(Threading2) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12589 | ApiTestFuzzer::SetUp(ApiTestFuzzer::SECOND_PART); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12590 | ApiTestFuzzer::RunAllTests(); |
| 12591 | ApiTestFuzzer::TearDown(); |
| 12592 | } |
| 12593 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12594 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 12595 | TEST(Threading3) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12596 | ApiTestFuzzer::SetUp(ApiTestFuzzer::THIRD_PART); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 12597 | ApiTestFuzzer::RunAllTests(); |
| 12598 | ApiTestFuzzer::TearDown(); |
| 12599 | } |
| 12600 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12601 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 12602 | TEST(Threading4) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 12603 | ApiTestFuzzer::SetUp(ApiTestFuzzer::FOURTH_PART); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 12604 | ApiTestFuzzer::RunAllTests(); |
| 12605 | ApiTestFuzzer::TearDown(); |
| 12606 | } |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12607 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 12608 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12609 | void ApiTestFuzzer::CallTest() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12610 | v8::Isolate::Scope scope(CcTest::isolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12611 | if (kLogThreading) |
| 12612 | printf("Start test %d\n", test_number_); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12613 | CallTestNumber(test_number_); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12614 | if (kLogThreading) |
| 12615 | printf("End test %d\n", test_number_); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12616 | } |
| 12617 | |
| 12618 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12619 | static void ThrowInJS(const v8::FunctionCallbackInfo<v8::Value>& args) { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12620 | v8::Isolate* isolate = args.GetIsolate(); |
| 12621 | CHECK(v8::Locker::IsLocked(isolate)); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12622 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12623 | v8::Unlocker unlocker(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12624 | const char* code = "throw 7;"; |
| 12625 | { |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12626 | v8::Locker nested_locker(isolate); |
| 12627 | v8::HandleScope scope(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12628 | v8::Handle<Value> exception; |
| 12629 | { v8::TryCatch try_catch; |
| 12630 | v8::Handle<Value> value = CompileRun(code); |
| 12631 | CHECK(value.IsEmpty()); |
| 12632 | CHECK(try_catch.HasCaught()); |
| 12633 | // Make sure to wrap the exception in a new handle because |
| 12634 | // the handle returned from the TryCatch is destroyed |
| 12635 | // when the TryCatch is destroyed. |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12636 | exception = Local<Value>::New(isolate, try_catch.Exception()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12637 | } |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 12638 | args.GetIsolate()->ThrowException(exception); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12639 | } |
| 12640 | } |
| 12641 | |
| 12642 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12643 | static void ThrowInJSNoCatch(const v8::FunctionCallbackInfo<v8::Value>& args) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12644 | CHECK(v8::Locker::IsLocked(CcTest::isolate())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12645 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12646 | v8::Unlocker unlocker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12647 | const char* code = "throw 7;"; |
| 12648 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12649 | v8::Locker nested_locker(CcTest::isolate()); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12650 | v8::HandleScope scope(args.GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12651 | v8::Handle<Value> value = CompileRun(code); |
| 12652 | CHECK(value.IsEmpty()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12653 | args.GetReturnValue().Set(v8_str("foo")); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12654 | } |
| 12655 | } |
| 12656 | |
| 12657 | |
| 12658 | // These are locking tests that don't need to be run again |
| 12659 | // as part of the locking aggregation tests. |
| 12660 | TEST(NestedLockers) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12661 | v8::Locker locker(CcTest::isolate()); |
| 12662 | CHECK(v8::Locker::IsLocked(CcTest::isolate())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12663 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12664 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12665 | Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(ThrowInJS); |
| 12666 | Local<Function> fun = fun_templ->GetFunction(); |
| 12667 | env->Global()->Set(v8_str("throw_in_js"), fun); |
| 12668 | Local<Script> script = v8_compile("(function () {" |
| 12669 | " try {" |
| 12670 | " throw_in_js();" |
| 12671 | " return 42;" |
| 12672 | " } catch (e) {" |
| 12673 | " return e * 13;" |
| 12674 | " }" |
| 12675 | "})();"); |
| 12676 | CHECK_EQ(91, script->Run()->Int32Value()); |
| 12677 | } |
| 12678 | |
| 12679 | |
| 12680 | // These are locking tests that don't need to be run again |
| 12681 | // as part of the locking aggregation tests. |
| 12682 | TEST(NestedLockersNoTryCatch) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12683 | v8::Locker locker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12684 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12685 | v8::HandleScope scope(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12686 | Local<v8::FunctionTemplate> fun_templ = |
| 12687 | v8::FunctionTemplate::New(ThrowInJSNoCatch); |
| 12688 | Local<Function> fun = fun_templ->GetFunction(); |
| 12689 | env->Global()->Set(v8_str("throw_in_js"), fun); |
| 12690 | Local<Script> script = v8_compile("(function () {" |
| 12691 | " try {" |
| 12692 | " throw_in_js();" |
| 12693 | " return 42;" |
| 12694 | " } catch (e) {" |
| 12695 | " return e * 13;" |
| 12696 | " }" |
| 12697 | "})();"); |
| 12698 | CHECK_EQ(91, script->Run()->Int32Value()); |
| 12699 | } |
| 12700 | |
| 12701 | |
| 12702 | THREADED_TEST(RecursiveLocking) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12703 | v8::Locker locker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12704 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12705 | v8::Locker locker2(CcTest::isolate()); |
| 12706 | CHECK(v8::Locker::IsLocked(CcTest::isolate())); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12707 | } |
| 12708 | } |
| 12709 | |
| 12710 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 12711 | static void UnlockForAMoment(const v8::FunctionCallbackInfo<v8::Value>& args) { |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12712 | ApiTestFuzzer::Fuzz(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12713 | v8::Unlocker unlocker(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12714 | } |
| 12715 | |
| 12716 | |
| 12717 | THREADED_TEST(LockUnlockLock) { |
| 12718 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12719 | v8::Locker locker(CcTest::isolate()); |
| 12720 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12721 | LocalContext env; |
| 12722 | Local<v8::FunctionTemplate> fun_templ = |
| 12723 | v8::FunctionTemplate::New(UnlockForAMoment); |
| 12724 | Local<Function> fun = fun_templ->GetFunction(); |
| 12725 | env->Global()->Set(v8_str("unlock_for_a_moment"), fun); |
| 12726 | Local<Script> script = v8_compile("(function () {" |
| 12727 | " unlock_for_a_moment();" |
| 12728 | " return 42;" |
| 12729 | "})();"); |
| 12730 | CHECK_EQ(42, script->Run()->Int32Value()); |
| 12731 | } |
| 12732 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12733 | v8::Locker locker(CcTest::isolate()); |
| 12734 | v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12735 | LocalContext env; |
| 12736 | Local<v8::FunctionTemplate> fun_templ = |
| 12737 | v8::FunctionTemplate::New(UnlockForAMoment); |
| 12738 | Local<Function> fun = fun_templ->GetFunction(); |
| 12739 | env->Global()->Set(v8_str("unlock_for_a_moment"), fun); |
| 12740 | Local<Script> script = v8_compile("(function () {" |
| 12741 | " unlock_for_a_moment();" |
| 12742 | " return 42;" |
| 12743 | "})();"); |
| 12744 | CHECK_EQ(42, script->Run()->Int32Value()); |
| 12745 | } |
| 12746 | } |
| 12747 | |
| 12748 | |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12749 | static int GetGlobalObjectsCount() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12750 | CcTest::heap()->EnsureHeapIsIterable(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12751 | int count = 0; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12752 | i::HeapIterator it(CcTest::heap()); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12753 | for (i::HeapObject* object = it.next(); object != NULL; object = it.next()) |
| 12754 | if (object->IsJSGlobalObject()) count++; |
| 12755 | return count; |
| 12756 | } |
| 12757 | |
| 12758 | |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12759 | static void CheckSurvivingGlobalObjectsCount(int expected) { |
christian.plesner.hansen@gmail.com | 37abdec | 2009-01-06 14:43:28 +0000 | [diff] [blame] | 12760 | // We need to collect all garbage twice to be sure that everything |
| 12761 | // has been collected. This is because inline caches are cleared in |
| 12762 | // the first garbage collection but some of the maps have already |
| 12763 | // been marked at that point. Therefore some of the maps are not |
| 12764 | // collected until the second garbage collection. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12765 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 12766 | CcTest::heap()->CollectAllGarbage(i::Heap::kMakeHeapIterableMask); |
sgjesse@chromium.org | b302e56 | 2010-02-03 11:26:59 +0000 | [diff] [blame] | 12767 | int count = GetGlobalObjectsCount(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12768 | #ifdef DEBUG |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12769 | if (count != expected) CcTest::heap()->TracePathToGlobal(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12770 | #endif |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12771 | CHECK_EQ(expected, count); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12772 | } |
| 12773 | |
| 12774 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12775 | TEST(DontLeakGlobalObjects) { |
| 12776 | // Regression test for issues 1139850 and 1174891. |
| 12777 | |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 12778 | v8::V8::Initialize(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 12779 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12780 | for (int i = 0; i < 5; i++) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12781 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12782 | LocalContext context; |
| 12783 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 12784 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12785 | CheckSurvivingGlobalObjectsCount(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12786 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12787 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12788 | LocalContext context; |
| 12789 | v8_compile("Date")->Run(); |
| 12790 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 12791 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12792 | CheckSurvivingGlobalObjectsCount(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12793 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12794 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12795 | LocalContext context; |
| 12796 | v8_compile("/aaa/")->Run(); |
| 12797 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 12798 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12799 | CheckSurvivingGlobalObjectsCount(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12800 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12801 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12802 | const char* extension_list[] = { "v8/gc" }; |
| 12803 | v8::ExtensionConfiguration extensions(1, extension_list); |
| 12804 | LocalContext context(&extensions); |
| 12805 | v8_compile("gc();")->Run(); |
| 12806 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 12807 | v8::V8::ContextDisposedNotification(); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 12808 | CheckSurvivingGlobalObjectsCount(0); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12809 | } |
| 12810 | } |
| 12811 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12812 | |
| 12813 | TEST(CopyablePersistent) { |
| 12814 | LocalContext context; |
| 12815 | v8::Isolate* isolate = context->GetIsolate(); |
| 12816 | i::GlobalHandles* globals = |
| 12817 | reinterpret_cast<i::Isolate*>(isolate)->global_handles(); |
| 12818 | int initial_handles = globals->global_handles_count(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12819 | typedef v8::Persistent<v8::Object, v8::CopyablePersistentTraits<v8::Object> > |
| 12820 | CopyableObject; |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12821 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12822 | CopyableObject handle1; |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12823 | { |
| 12824 | v8::HandleScope scope(isolate); |
| 12825 | handle1.Reset(isolate, v8::Object::New()); |
| 12826 | } |
| 12827 | CHECK_EQ(initial_handles + 1, globals->global_handles_count()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12828 | CopyableObject handle2; |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12829 | handle2 = handle1; |
| 12830 | CHECK(handle1 == handle2); |
| 12831 | CHECK_EQ(initial_handles + 2, globals->global_handles_count()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12832 | CopyableObject handle3(handle2); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12833 | CHECK(handle1 == handle3); |
| 12834 | CHECK_EQ(initial_handles + 3, globals->global_handles_count()); |
| 12835 | } |
| 12836 | // Verify autodispose |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 12837 | CHECK_EQ(initial_handles, globals->global_handles_count()); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 12838 | } |
| 12839 | |
| 12840 | |
| 12841 | static void WeakApiCallback( |
| 12842 | const v8::WeakCallbackData<v8::Object, Persistent<v8::Object> >& data) { |
| 12843 | Local<Value> value = data.GetValue()->Get(v8_str("key")); |
| 12844 | CHECK_EQ(231, static_cast<int32_t>(Local<v8::Integer>::Cast(value)->Value())); |
| 12845 | data.GetParameter()->Reset(); |
| 12846 | delete data.GetParameter(); |
| 12847 | } |
| 12848 | |
| 12849 | |
| 12850 | TEST(WeakCallbackApi) { |
| 12851 | LocalContext context; |
| 12852 | v8::Isolate* isolate = context->GetIsolate(); |
| 12853 | i::GlobalHandles* globals = |
| 12854 | reinterpret_cast<i::Isolate*>(isolate)->global_handles(); |
| 12855 | int initial_handles = globals->global_handles_count(); |
| 12856 | { |
| 12857 | v8::HandleScope scope(isolate); |
| 12858 | v8::Local<v8::Object> obj = v8::Object::New(); |
| 12859 | obj->Set(v8_str("key"), v8::Integer::New(231, isolate)); |
| 12860 | v8::Persistent<v8::Object>* handle = |
| 12861 | new v8::Persistent<v8::Object>(isolate, obj); |
| 12862 | handle->SetWeak<v8::Object, v8::Persistent<v8::Object> >(handle, |
| 12863 | WeakApiCallback); |
| 12864 | } |
| 12865 | reinterpret_cast<i::Isolate*>(isolate)->heap()-> |
| 12866 | CollectAllGarbage(i::Heap::kNoGCFlags); |
| 12867 | // Verify disposed. |
| 12868 | CHECK_EQ(initial_handles, globals->global_handles_count()); |
| 12869 | } |
| 12870 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12871 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12872 | v8::Persistent<v8::Object> some_object; |
| 12873 | v8::Persistent<v8::Object> bad_handle; |
| 12874 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12875 | void NewPersistentHandleCallback(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12876 | v8::Persistent<v8::Value>* handle, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12877 | void*) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12878 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 12879 | bad_handle.Reset(isolate, some_object); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12880 | handle->Dispose(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12881 | } |
| 12882 | |
| 12883 | |
| 12884 | THREADED_TEST(NewPersistentHandleFromWeakCallback) { |
| 12885 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12886 | v8::Isolate* isolate = context->GetIsolate(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12887 | |
| 12888 | v8::Persistent<v8::Object> handle1, handle2; |
| 12889 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12890 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 12891 | some_object.Reset(isolate, v8::Object::New()); |
| 12892 | handle1.Reset(isolate, v8::Object::New()); |
| 12893 | handle2.Reset(isolate, v8::Object::New()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12894 | } |
| 12895 | // Note: order is implementation dependent alas: currently |
| 12896 | // global handle nodes are processed by PostGarbageCollectionProcessing |
| 12897 | // in reverse allocation order, so if second allocated handle is deleted, |
| 12898 | // weak callback of the first handle would be able to 'reallocate' it. |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 12899 | handle1.MakeWeak<v8::Value, void>(NULL, NewPersistentHandleCallback); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12900 | handle2.Dispose(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12901 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12902 | } |
| 12903 | |
| 12904 | |
| 12905 | v8::Persistent<v8::Object> to_be_disposed; |
| 12906 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12907 | void DisposeAndForceGcCallback(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12908 | v8::Persistent<v8::Value>* handle, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12909 | void*) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12910 | to_be_disposed.Dispose(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12911 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12912 | handle->Dispose(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12913 | } |
| 12914 | |
| 12915 | |
| 12916 | THREADED_TEST(DoNotUseDeletedNodesInSecondLevelGc) { |
| 12917 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12918 | v8::Isolate* isolate = context->GetIsolate(); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12919 | |
| 12920 | v8::Persistent<v8::Object> handle1, handle2; |
| 12921 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12922 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 12923 | handle1.Reset(isolate, v8::Object::New()); |
| 12924 | handle2.Reset(isolate, v8::Object::New()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12925 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 12926 | handle1.MakeWeak<v8::Value, void>(NULL, DisposeAndForceGcCallback); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 12927 | to_be_disposed.Reset(isolate, handle2); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12928 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12929 | } |
| 12930 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12931 | void DisposingCallback(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12932 | v8::Persistent<v8::Value>* handle, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12933 | void*) { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12934 | handle->Dispose(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12935 | } |
| 12936 | |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12937 | void HandleCreatingCallback(v8::Isolate* isolate, |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12938 | v8::Persistent<v8::Value>* handle, |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12939 | void*) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12940 | v8::HandleScope scope(isolate); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 12941 | v8::Persistent<v8::Object>(isolate, v8::Object::New()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 12942 | handle->Dispose(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12943 | } |
| 12944 | |
| 12945 | |
| 12946 | THREADED_TEST(NoGlobalHandlesOrphaningDueToWeakCallback) { |
| 12947 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 12948 | v8::Isolate* isolate = context->GetIsolate(); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12949 | |
| 12950 | v8::Persistent<v8::Object> handle1, handle2, handle3; |
| 12951 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12952 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 12953 | handle3.Reset(isolate, v8::Object::New()); |
| 12954 | handle2.Reset(isolate, v8::Object::New()); |
| 12955 | handle1.Reset(isolate, v8::Object::New()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12956 | } |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 12957 | handle2.MakeWeak<v8::Value, void>(NULL, DisposingCallback); |
| 12958 | handle3.MakeWeak<v8::Value, void>(NULL, HandleCreatingCallback); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12959 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 12960 | } |
| 12961 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 12962 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12963 | THREADED_TEST(CheckForCrossContextObjectLiterals) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 12964 | v8::V8::Initialize(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12965 | |
| 12966 | const int nof = 2; |
| 12967 | const char* sources[nof] = { |
| 12968 | "try { [ 2, 3, 4 ].forEach(5); } catch(e) { e.toString(); }", |
| 12969 | "Object()" |
| 12970 | }; |
| 12971 | |
| 12972 | for (int i = 0; i < nof; i++) { |
| 12973 | const char* source = sources[i]; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12974 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12975 | LocalContext context; |
| 12976 | CompileRun(source); |
| 12977 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12978 | { v8::HandleScope scope(CcTest::isolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12979 | LocalContext context; |
| 12980 | CompileRun(source); |
| 12981 | } |
| 12982 | } |
| 12983 | } |
| 12984 | |
| 12985 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12986 | static v8::Handle<Value> NestedScope(v8::Local<Context> env) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 12987 | v8::HandleScope inner(env->GetIsolate()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 12988 | env->Enter(); |
| 12989 | v8::Handle<Value> three = v8_num(3); |
| 12990 | v8::Handle<Value> value = inner.Close(three); |
| 12991 | env->Exit(); |
| 12992 | return value; |
| 12993 | } |
| 12994 | |
| 12995 | |
| 12996 | THREADED_TEST(NestedHandleScopeAndContexts) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 12997 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 12998 | v8::HandleScope outer(isolate); |
| 12999 | v8::Local<Context> env = Context::New(isolate); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13000 | env->Enter(); |
| 13001 | v8::Handle<Value> value = NestedScope(env); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 13002 | v8::Handle<String> str(value->ToString()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 13003 | CHECK(!str.IsEmpty()); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13004 | env->Exit(); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13005 | } |
| 13006 | |
| 13007 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13008 | static bool MatchPointers(void* key1, void* key2) { |
| 13009 | return key1 == key2; |
| 13010 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13011 | |
| 13012 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13013 | struct SymbolInfo { |
| 13014 | size_t id; |
| 13015 | size_t size; |
| 13016 | std::string name; |
| 13017 | }; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13018 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13019 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13020 | class SetFunctionEntryHookTest { |
| 13021 | public: |
| 13022 | SetFunctionEntryHookTest() { |
| 13023 | CHECK(instance_ == NULL); |
| 13024 | instance_ = this; |
| 13025 | } |
| 13026 | ~SetFunctionEntryHookTest() { |
| 13027 | CHECK(instance_ == this); |
| 13028 | instance_ = NULL; |
| 13029 | } |
| 13030 | void Reset() { |
| 13031 | symbols_.clear(); |
| 13032 | symbol_locations_.clear(); |
| 13033 | invocations_.clear(); |
| 13034 | } |
| 13035 | void RunTest(); |
| 13036 | void OnJitEvent(const v8::JitCodeEvent* event); |
| 13037 | static void JitEvent(const v8::JitCodeEvent* event) { |
| 13038 | CHECK(instance_ != NULL); |
| 13039 | instance_->OnJitEvent(event); |
| 13040 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13041 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13042 | void OnEntryHook(uintptr_t function, |
| 13043 | uintptr_t return_addr_location); |
| 13044 | static void EntryHook(uintptr_t function, |
| 13045 | uintptr_t return_addr_location) { |
| 13046 | CHECK(instance_ != NULL); |
| 13047 | instance_->OnEntryHook(function, return_addr_location); |
| 13048 | } |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 13049 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13050 | static void RuntimeCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 13051 | CHECK(instance_ != NULL); |
| 13052 | args.GetReturnValue().Set(v8_num(42)); |
| 13053 | } |
| 13054 | void RunLoopInNewEnv(v8::Isolate* isolate); |
| 13055 | |
| 13056 | // Records addr as location of symbol. |
| 13057 | void InsertSymbolAt(i::Address addr, SymbolInfo* symbol); |
| 13058 | |
| 13059 | // Finds the symbol containing addr |
| 13060 | SymbolInfo* FindSymbolForAddr(i::Address addr); |
| 13061 | // Returns the number of invocations where the caller name contains |
| 13062 | // \p caller_name and the function name contains \p function_name. |
| 13063 | int CountInvocations(const char* caller_name, |
| 13064 | const char* function_name); |
| 13065 | |
| 13066 | i::Handle<i::JSFunction> foo_func_; |
| 13067 | i::Handle<i::JSFunction> bar_func_; |
| 13068 | |
| 13069 | typedef std::map<size_t, SymbolInfo> SymbolMap; |
| 13070 | typedef std::map<i::Address, SymbolInfo*> SymbolLocationMap; |
| 13071 | typedef std::map<std::pair<SymbolInfo*, SymbolInfo*>, int> InvocationMap; |
| 13072 | SymbolMap symbols_; |
| 13073 | SymbolLocationMap symbol_locations_; |
| 13074 | InvocationMap invocations_; |
| 13075 | |
| 13076 | static SetFunctionEntryHookTest* instance_; |
| 13077 | }; |
| 13078 | SetFunctionEntryHookTest* SetFunctionEntryHookTest::instance_ = NULL; |
| 13079 | |
| 13080 | |
| 13081 | // Returns true if addr is in the range [start, start+len). |
| 13082 | static bool Overlaps(i::Address start, size_t len, i::Address addr) { |
| 13083 | if (start <= addr && start + len > addr) |
| 13084 | return true; |
| 13085 | |
| 13086 | return false; |
| 13087 | } |
| 13088 | |
| 13089 | void SetFunctionEntryHookTest::InsertSymbolAt(i::Address addr, |
| 13090 | SymbolInfo* symbol) { |
| 13091 | // Insert the symbol at the new location. |
| 13092 | SymbolLocationMap::iterator it = |
| 13093 | symbol_locations_.insert(std::make_pair(addr, symbol)).first; |
| 13094 | // Now erase symbols to the left and right that overlap this one. |
| 13095 | while (it != symbol_locations_.begin()) { |
| 13096 | SymbolLocationMap::iterator left = it; |
| 13097 | --left; |
| 13098 | if (!Overlaps(left->first, left->second->size, addr)) |
| 13099 | break; |
| 13100 | symbol_locations_.erase(left); |
| 13101 | } |
| 13102 | |
| 13103 | // Now erase symbols to the left and right that overlap this one. |
| 13104 | while (true) { |
| 13105 | SymbolLocationMap::iterator right = it; |
| 13106 | ++right; |
| 13107 | if (right == symbol_locations_.end()) |
| 13108 | break; |
| 13109 | if (!Overlaps(addr, symbol->size, right->first)) |
| 13110 | break; |
| 13111 | symbol_locations_.erase(right); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 13112 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13113 | } |
| 13114 | |
| 13115 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13116 | void SetFunctionEntryHookTest::OnJitEvent(const v8::JitCodeEvent* event) { |
| 13117 | switch (event->type) { |
| 13118 | case v8::JitCodeEvent::CODE_ADDED: { |
| 13119 | CHECK(event->code_start != NULL); |
| 13120 | CHECK_NE(0, static_cast<int>(event->code_len)); |
| 13121 | CHECK(event->name.str != NULL); |
| 13122 | size_t symbol_id = symbols_.size(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13123 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13124 | // Record the new symbol. |
| 13125 | SymbolInfo& info = symbols_[symbol_id]; |
| 13126 | info.id = symbol_id; |
| 13127 | info.size = event->code_len; |
| 13128 | info.name.assign(event->name.str, event->name.str + event->name.len); |
| 13129 | |
| 13130 | // And record it's location. |
| 13131 | InsertSymbolAt(reinterpret_cast<i::Address>(event->code_start), &info); |
| 13132 | } |
| 13133 | break; |
| 13134 | |
| 13135 | case v8::JitCodeEvent::CODE_MOVED: { |
| 13136 | // We would like to never see code move that we haven't seen before, |
| 13137 | // but the code creation event does not happen until the line endings |
| 13138 | // have been calculated (this is so that we can report the line in the |
| 13139 | // script at which the function source is found, see |
| 13140 | // Compiler::RecordFunctionCompilation) and the line endings |
| 13141 | // calculations can cause a GC, which can move the newly created code |
| 13142 | // before its existence can be logged. |
| 13143 | SymbolLocationMap::iterator it( |
| 13144 | symbol_locations_.find( |
| 13145 | reinterpret_cast<i::Address>(event->code_start))); |
| 13146 | if (it != symbol_locations_.end()) { |
| 13147 | // Found a symbol at this location, move it. |
| 13148 | SymbolInfo* info = it->second; |
| 13149 | symbol_locations_.erase(it); |
| 13150 | InsertSymbolAt(reinterpret_cast<i::Address>(event->new_code_start), |
| 13151 | info); |
| 13152 | } |
| 13153 | } |
| 13154 | default: |
| 13155 | break; |
| 13156 | } |
| 13157 | } |
| 13158 | |
| 13159 | void SetFunctionEntryHookTest::OnEntryHook( |
| 13160 | uintptr_t function, uintptr_t return_addr_location) { |
| 13161 | // Get the function's code object. |
| 13162 | i::Code* function_code = i::Code::GetCodeFromTargetAddress( |
| 13163 | reinterpret_cast<i::Address>(function)); |
| 13164 | CHECK(function_code != NULL); |
| 13165 | |
| 13166 | // Then try and look up the caller's code object. |
| 13167 | i::Address caller = *reinterpret_cast<i::Address*>(return_addr_location); |
| 13168 | |
| 13169 | // Count the invocation. |
| 13170 | SymbolInfo* caller_symbol = FindSymbolForAddr(caller); |
| 13171 | SymbolInfo* function_symbol = |
| 13172 | FindSymbolForAddr(reinterpret_cast<i::Address>(function)); |
| 13173 | ++invocations_[std::make_pair(caller_symbol, function_symbol)]; |
| 13174 | |
| 13175 | if (!bar_func_.is_null() && function_code == bar_func_->code()) { |
| 13176 | // Check that we have a symbol for the "bar" function at the right location. |
| 13177 | SymbolLocationMap::iterator it( |
| 13178 | symbol_locations_.find(function_code->instruction_start())); |
| 13179 | CHECK(it != symbol_locations_.end()); |
| 13180 | } |
| 13181 | |
| 13182 | if (!foo_func_.is_null() && function_code == foo_func_->code()) { |
| 13183 | // Check that we have a symbol for "foo" at the right location. |
| 13184 | SymbolLocationMap::iterator it( |
| 13185 | symbol_locations_.find(function_code->instruction_start())); |
| 13186 | CHECK(it != symbol_locations_.end()); |
| 13187 | } |
| 13188 | } |
| 13189 | |
| 13190 | |
| 13191 | SymbolInfo* SetFunctionEntryHookTest::FindSymbolForAddr(i::Address addr) { |
| 13192 | SymbolLocationMap::iterator it(symbol_locations_.lower_bound(addr)); |
| 13193 | // Do we have a direct hit on a symbol? |
| 13194 | if (it != symbol_locations_.end()) { |
| 13195 | if (it->first == addr) |
| 13196 | return it->second; |
| 13197 | } |
| 13198 | |
| 13199 | // If not a direct hit, it'll have to be the previous symbol. |
| 13200 | if (it == symbol_locations_.begin()) |
| 13201 | return NULL; |
| 13202 | |
| 13203 | --it; |
| 13204 | size_t offs = addr - it->first; |
| 13205 | if (offs < it->second->size) |
| 13206 | return it->second; |
| 13207 | |
| 13208 | return NULL; |
| 13209 | } |
| 13210 | |
| 13211 | |
| 13212 | int SetFunctionEntryHookTest::CountInvocations( |
| 13213 | const char* caller_name, const char* function_name) { |
| 13214 | InvocationMap::iterator it(invocations_.begin()); |
| 13215 | int invocations = 0; |
| 13216 | for (; it != invocations_.end(); ++it) { |
| 13217 | SymbolInfo* caller = it->first.first; |
| 13218 | SymbolInfo* function = it->first.second; |
| 13219 | |
| 13220 | // Filter out non-matching functions. |
| 13221 | if (function_name != NULL) { |
| 13222 | if (function->name.find(function_name) == std::string::npos) |
| 13223 | continue; |
| 13224 | } |
| 13225 | |
| 13226 | // Filter out non-matching callers. |
| 13227 | if (caller_name != NULL) { |
| 13228 | if (caller == NULL) |
| 13229 | continue; |
| 13230 | if (caller->name.find(caller_name) == std::string::npos) |
| 13231 | continue; |
| 13232 | } |
| 13233 | |
| 13234 | // It matches add the invocation count to the tally. |
| 13235 | invocations += it->second; |
| 13236 | } |
| 13237 | |
| 13238 | return invocations; |
| 13239 | } |
| 13240 | |
| 13241 | |
| 13242 | void SetFunctionEntryHookTest::RunLoopInNewEnv(v8::Isolate* isolate) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13243 | v8::HandleScope outer(isolate); |
| 13244 | v8::Local<Context> env = Context::New(isolate); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13245 | env->Enter(); |
| 13246 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13247 | Local<ObjectTemplate> t = ObjectTemplate::New(); |
| 13248 | t->Set(v8_str("asdf"), v8::FunctionTemplate::New(RuntimeCallback)); |
| 13249 | env->Global()->Set(v8_str("obj"), t->NewInstance()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13250 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13251 | const char* script = |
| 13252 | "function bar() {\n" |
| 13253 | " var sum = 0;\n" |
| 13254 | " for (i = 0; i < 100; ++i)\n" |
| 13255 | " sum = foo(i);\n" |
| 13256 | " return sum;\n" |
| 13257 | "}\n" |
| 13258 | "function foo(i) { return i * i; }\n" |
| 13259 | "// Invoke on the runtime function.\n" |
| 13260 | "obj.asdf()"; |
| 13261 | CompileRun(script); |
| 13262 | bar_func_ = i::Handle<i::JSFunction>::cast( |
| 13263 | v8::Utils::OpenHandle(*env->Global()->Get(v8_str("bar")))); |
| 13264 | ASSERT(!bar_func_.is_null()); |
| 13265 | |
| 13266 | foo_func_ = |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13267 | i::Handle<i::JSFunction>::cast( |
| 13268 | v8::Utils::OpenHandle(*env->Global()->Get(v8_str("foo")))); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13269 | ASSERT(!foo_func_.is_null()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13270 | |
| 13271 | v8::Handle<v8::Value> value = CompileRun("bar();"); |
| 13272 | CHECK(value->IsNumber()); |
| 13273 | CHECK_EQ(9801.0, v8::Number::Cast(*value)->Value()); |
| 13274 | |
| 13275 | // Test the optimized codegen path. |
| 13276 | value = CompileRun("%OptimizeFunctionOnNextCall(foo);" |
| 13277 | "bar();"); |
| 13278 | CHECK(value->IsNumber()); |
| 13279 | CHECK_EQ(9801.0, v8::Number::Cast(*value)->Value()); |
| 13280 | |
| 13281 | env->Exit(); |
| 13282 | } |
| 13283 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 13284 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13285 | void SetFunctionEntryHookTest::RunTest() { |
| 13286 | // Work in a new isolate throughout. |
| 13287 | v8::Isolate* isolate = v8::Isolate::New(); |
| 13288 | |
| 13289 | // Test setting the entry hook on the new isolate. |
| 13290 | CHECK(v8::V8::SetFunctionEntryHook(isolate, EntryHook)); |
| 13291 | |
| 13292 | // Replacing the hook, once set should fail. |
| 13293 | CHECK_EQ(false, v8::V8::SetFunctionEntryHook(isolate, EntryHook)); |
| 13294 | |
| 13295 | { |
| 13296 | v8::Isolate::Scope scope(isolate); |
| 13297 | |
| 13298 | v8::V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, JitEvent); |
| 13299 | |
| 13300 | RunLoopInNewEnv(isolate); |
| 13301 | |
| 13302 | // Check the exepected invocation counts. |
| 13303 | CHECK_EQ(2, CountInvocations(NULL, "bar")); |
| 13304 | CHECK_EQ(200, CountInvocations("bar", "foo")); |
| 13305 | CHECK_EQ(200, CountInvocations(NULL, "foo")); |
| 13306 | |
| 13307 | // Verify that we have an entry hook on some specific stubs. |
| 13308 | CHECK_NE(0, CountInvocations(NULL, "CEntryStub")); |
| 13309 | CHECK_NE(0, CountInvocations(NULL, "JSEntryStub")); |
| 13310 | CHECK_NE(0, CountInvocations(NULL, "JSEntryTrampoline")); |
| 13311 | } |
| 13312 | isolate->Dispose(); |
| 13313 | |
| 13314 | Reset(); |
| 13315 | |
| 13316 | // Make sure a second isolate is unaffected by the previous entry hook. |
| 13317 | isolate = v8::Isolate::New(); |
| 13318 | { |
| 13319 | v8::Isolate::Scope scope(isolate); |
| 13320 | |
| 13321 | // Reset the entry count to zero and set the entry hook. |
| 13322 | RunLoopInNewEnv(isolate); |
| 13323 | |
| 13324 | // We should record no invocations in this isolate. |
| 13325 | CHECK_EQ(0, static_cast<int>(invocations_.size())); |
| 13326 | } |
| 13327 | // Since the isolate has been used, we shouldn't be able to set an entry |
| 13328 | // hook anymore. |
| 13329 | CHECK_EQ(false, v8::V8::SetFunctionEntryHook(isolate, EntryHook)); |
| 13330 | |
| 13331 | isolate->Dispose(); |
| 13332 | } |
| 13333 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13334 | |
| 13335 | TEST(SetFunctionEntryHook) { |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 13336 | // FunctionEntryHook does not work well with experimental natives. |
| 13337 | // Experimental natives are compiled during snapshot deserialization. |
| 13338 | // This test breaks because InstallGetter (function from snapshot that |
| 13339 | // only gets called from experimental natives) is compiled with entry hooks. |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13340 | i::FLAG_allow_natives_syntax = true; |
yangguo@chromium.org | fb37721 | 2012-11-16 14:43:43 +0000 | [diff] [blame] | 13341 | i::FLAG_use_inlining = false; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13342 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13343 | SetFunctionEntryHookTest test; |
| 13344 | test.RunTest(); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 13345 | } |
| 13346 | |
| 13347 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13348 | static i::HashMap* code_map = NULL; |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13349 | static i::HashMap* jitcode_line_info = NULL; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13350 | static int saw_bar = 0; |
| 13351 | static int move_events = 0; |
| 13352 | |
| 13353 | |
| 13354 | static bool FunctionNameIs(const char* expected, |
| 13355 | const v8::JitCodeEvent* event) { |
| 13356 | // Log lines for functions are of the general form: |
| 13357 | // "LazyCompile:<type><function_name>", where the type is one of |
| 13358 | // "*", "~" or "". |
| 13359 | static const char kPreamble[] = "LazyCompile:"; |
| 13360 | static size_t kPreambleLen = sizeof(kPreamble) - 1; |
| 13361 | |
| 13362 | if (event->name.len < sizeof(kPreamble) - 1 || |
| 13363 | strncmp(kPreamble, event->name.str, kPreambleLen) != 0) { |
| 13364 | return false; |
| 13365 | } |
| 13366 | |
| 13367 | const char* tail = event->name.str + kPreambleLen; |
| 13368 | size_t tail_len = event->name.len - kPreambleLen; |
| 13369 | size_t expected_len = strlen(expected); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 13370 | if (tail_len > 1 && (*tail == '*' || *tail == '~')) { |
| 13371 | --tail_len; |
| 13372 | ++tail; |
| 13373 | } |
| 13374 | |
| 13375 | // Check for tails like 'bar :1'. |
| 13376 | if (tail_len > expected_len + 2 && |
| 13377 | tail[expected_len] == ' ' && |
| 13378 | tail[expected_len + 1] == ':' && |
| 13379 | tail[expected_len + 2] && |
| 13380 | !strncmp(tail, expected, expected_len)) { |
| 13381 | return true; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13382 | } |
| 13383 | |
| 13384 | if (tail_len != expected_len) |
| 13385 | return false; |
| 13386 | |
| 13387 | return strncmp(tail, expected, expected_len) == 0; |
| 13388 | } |
| 13389 | |
| 13390 | |
| 13391 | static void event_handler(const v8::JitCodeEvent* event) { |
| 13392 | CHECK(event != NULL); |
| 13393 | CHECK(code_map != NULL); |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13394 | CHECK(jitcode_line_info != NULL); |
| 13395 | |
| 13396 | class DummyJitCodeLineInfo { |
| 13397 | }; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13398 | |
| 13399 | switch (event->type) { |
| 13400 | case v8::JitCodeEvent::CODE_ADDED: { |
| 13401 | CHECK(event->code_start != NULL); |
| 13402 | CHECK_NE(0, static_cast<int>(event->code_len)); |
| 13403 | CHECK(event->name.str != NULL); |
| 13404 | i::HashMap::Entry* entry = |
| 13405 | code_map->Lookup(event->code_start, |
| 13406 | i::ComputePointerHash(event->code_start), |
| 13407 | true); |
| 13408 | entry->value = reinterpret_cast<void*>(event->code_len); |
| 13409 | |
| 13410 | if (FunctionNameIs("bar", event)) { |
| 13411 | ++saw_bar; |
| 13412 | } |
| 13413 | } |
| 13414 | break; |
| 13415 | |
| 13416 | case v8::JitCodeEvent::CODE_MOVED: { |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13417 | uint32_t hash = i::ComputePointerHash(event->code_start); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 13418 | // We would like to never see code move that we haven't seen before, |
| 13419 | // but the code creation event does not happen until the line endings |
| 13420 | // have been calculated (this is so that we can report the line in the |
| 13421 | // script at which the function source is found, see |
| 13422 | // Compiler::RecordFunctionCompilation) and the line endings |
| 13423 | // calculations can cause a GC, which can move the newly created code |
| 13424 | // before its existence can be logged. |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13425 | i::HashMap::Entry* entry = |
| 13426 | code_map->Lookup(event->code_start, hash, false); |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 13427 | if (entry != NULL) { |
| 13428 | ++move_events; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13429 | |
jkummerow@chromium.org | 78502a9 | 2012-09-06 13:50:42 +0000 | [diff] [blame] | 13430 | CHECK_EQ(reinterpret_cast<void*>(event->code_len), entry->value); |
| 13431 | code_map->Remove(event->code_start, hash); |
| 13432 | |
| 13433 | entry = code_map->Lookup(event->new_code_start, |
| 13434 | i::ComputePointerHash(event->new_code_start), |
| 13435 | true); |
| 13436 | CHECK(entry != NULL); |
| 13437 | entry->value = reinterpret_cast<void*>(event->code_len); |
| 13438 | } |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13439 | } |
| 13440 | break; |
| 13441 | |
| 13442 | case v8::JitCodeEvent::CODE_REMOVED: |
| 13443 | // Object/code removal events are currently not dispatched from the GC. |
| 13444 | CHECK(false); |
| 13445 | break; |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13446 | |
| 13447 | // For CODE_START_LINE_INFO_RECORDING event, we will create one |
| 13448 | // DummyJitCodeLineInfo data structure pointed by event->user_dat. We |
| 13449 | // record it in jitcode_line_info. |
| 13450 | case v8::JitCodeEvent::CODE_START_LINE_INFO_RECORDING: { |
| 13451 | DummyJitCodeLineInfo* line_info = new DummyJitCodeLineInfo(); |
| 13452 | v8::JitCodeEvent* temp_event = const_cast<v8::JitCodeEvent*>(event); |
| 13453 | temp_event->user_data = line_info; |
| 13454 | i::HashMap::Entry* entry = |
| 13455 | jitcode_line_info->Lookup(line_info, |
| 13456 | i::ComputePointerHash(line_info), |
| 13457 | true); |
| 13458 | entry->value = reinterpret_cast<void*>(line_info); |
| 13459 | } |
| 13460 | break; |
| 13461 | // For these two events, we will check whether the event->user_data |
| 13462 | // data structure is created before during CODE_START_LINE_INFO_RECORDING |
| 13463 | // event. And delete it in CODE_END_LINE_INFO_RECORDING event handling. |
| 13464 | case v8::JitCodeEvent::CODE_END_LINE_INFO_RECORDING: { |
| 13465 | CHECK(event->user_data != NULL); |
| 13466 | uint32_t hash = i::ComputePointerHash(event->user_data); |
| 13467 | i::HashMap::Entry* entry = |
| 13468 | jitcode_line_info->Lookup(event->user_data, hash, false); |
| 13469 | CHECK(entry != NULL); |
| 13470 | delete reinterpret_cast<DummyJitCodeLineInfo*>(event->user_data); |
| 13471 | } |
| 13472 | break; |
| 13473 | |
| 13474 | case v8::JitCodeEvent::CODE_ADD_LINE_POS_INFO: { |
| 13475 | CHECK(event->user_data != NULL); |
| 13476 | uint32_t hash = i::ComputePointerHash(event->user_data); |
| 13477 | i::HashMap::Entry* entry = |
| 13478 | jitcode_line_info->Lookup(event->user_data, hash, false); |
| 13479 | CHECK(entry != NULL); |
| 13480 | } |
| 13481 | break; |
| 13482 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13483 | default: |
| 13484 | // Impossible event. |
| 13485 | CHECK(false); |
| 13486 | break; |
| 13487 | } |
| 13488 | } |
| 13489 | |
| 13490 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13491 | UNINITIALIZED_TEST(SetJitCodeEventHandler) { |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13492 | i::FLAG_stress_compaction = true; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 13493 | i::FLAG_incremental_marking = false; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13494 | const char* script = |
| 13495 | "function bar() {" |
| 13496 | " var sum = 0;" |
| 13497 | " for (i = 0; i < 100; ++i)" |
| 13498 | " sum = foo(i);" |
| 13499 | " return sum;" |
| 13500 | "}" |
| 13501 | "function foo(i) { return i * i; };" |
| 13502 | "bar();"; |
| 13503 | |
| 13504 | // Run this test in a new isolate to make sure we don't |
| 13505 | // have remnants of state from other code. |
| 13506 | v8::Isolate* isolate = v8::Isolate::New(); |
| 13507 | isolate->Enter(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13508 | i::Heap* heap = reinterpret_cast<i::Isolate*>(isolate)->heap(); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13509 | |
| 13510 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13511 | v8::HandleScope scope(isolate); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13512 | i::HashMap code(MatchPointers); |
| 13513 | code_map = &code; |
| 13514 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13515 | i::HashMap lineinfo(MatchPointers); |
| 13516 | jitcode_line_info = &lineinfo; |
| 13517 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13518 | saw_bar = 0; |
| 13519 | move_events = 0; |
| 13520 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13521 | V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, event_handler); |
| 13522 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13523 | // Generate new code objects sparsely distributed across several |
| 13524 | // different fragmented code-space pages. |
| 13525 | const int kIterations = 10; |
| 13526 | for (int i = 0; i < kIterations; ++i) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13527 | LocalContext env(isolate); |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13528 | i::AlwaysAllocateScope always_allocate; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13529 | SimulateFullSpace(heap->code_space()); |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13530 | CompileRun(script); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13531 | |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13532 | // Keep a strong reference to the code object in the handle scope. |
| 13533 | i::Handle<i::Code> bar_code(i::Handle<i::JSFunction>::cast( |
| 13534 | v8::Utils::OpenHandle(*env->Global()->Get(v8_str("bar"))))->code()); |
| 13535 | i::Handle<i::Code> foo_code(i::Handle<i::JSFunction>::cast( |
| 13536 | v8::Utils::OpenHandle(*env->Global()->Get(v8_str("foo"))))->code()); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13537 | |
| 13538 | // Clear the compilation cache to get more wastage. |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 13539 | reinterpret_cast<i::Isolate*>(isolate)->compilation_cache()->Clear(); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13540 | } |
| 13541 | |
| 13542 | // Force code movement. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13543 | heap->CollectAllAvailableGarbage("TestSetJitCodeEventHandler"); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13544 | |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13545 | V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, NULL); |
| 13546 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13547 | CHECK_LE(kIterations, saw_bar); |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13548 | CHECK_LT(0, move_events); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13549 | |
| 13550 | code_map = NULL; |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13551 | jitcode_line_info = NULL; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13552 | } |
| 13553 | |
| 13554 | isolate->Exit(); |
| 13555 | isolate->Dispose(); |
| 13556 | |
| 13557 | // Do this in a new isolate. |
| 13558 | isolate = v8::Isolate::New(); |
| 13559 | isolate->Enter(); |
| 13560 | |
| 13561 | // Verify that we get callbacks for existing code objects when we |
| 13562 | // request enumeration of existing code. |
| 13563 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13564 | v8::HandleScope scope(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13565 | LocalContext env(isolate); |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13566 | CompileRun(script); |
| 13567 | |
| 13568 | // Now get code through initial iteration. |
| 13569 | i::HashMap code(MatchPointers); |
| 13570 | code_map = &code; |
| 13571 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13572 | i::HashMap lineinfo(MatchPointers); |
| 13573 | jitcode_line_info = &lineinfo; |
| 13574 | |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13575 | V8::SetJitCodeEventHandler(v8::kJitCodeEventEnumExisting, event_handler); |
| 13576 | V8::SetJitCodeEventHandler(v8::kJitCodeEventDefault, NULL); |
| 13577 | |
yangguo@chromium.org | c03a192 | 2013-02-19 13:55:47 +0000 | [diff] [blame] | 13578 | jitcode_line_info = NULL; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13579 | // We expect that we got some events. Note that if we could get code removal |
| 13580 | // notifications, we could compare two collections, one created by listening |
| 13581 | // from the time of creation of an isolate, and the other by subscribing |
| 13582 | // with EnumExisting. |
ulan@chromium.org | 6ba1fd0 | 2013-02-14 14:56:11 +0000 | [diff] [blame] | 13583 | CHECK_LT(0, code.occupancy()); |
| 13584 | |
| 13585 | code_map = NULL; |
yangguo@chromium.org | 355cfd1 | 2012-08-29 15:32:24 +0000 | [diff] [blame] | 13586 | } |
| 13587 | |
| 13588 | isolate->Exit(); |
| 13589 | isolate->Dispose(); |
| 13590 | } |
| 13591 | |
| 13592 | |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 13593 | static int64_t cast(intptr_t x) { return static_cast<int64_t>(x); } |
| 13594 | |
| 13595 | |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13596 | THREADED_TEST(ExternalAllocatedMemory) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13597 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13598 | v8::HandleScope outer(isolate); |
| 13599 | v8::Local<Context> env(Context::New(isolate)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 13600 | CHECK(!env.IsEmpty()); |
jkummerow@chromium.org | 28faa98 | 2012-04-13 09:58:30 +0000 | [diff] [blame] | 13601 | const intptr_t kSize = 1024*1024; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 13602 | int64_t baseline = cast(isolate->AdjustAmountOfExternalAllocatedMemory(0)); |
| 13603 | CHECK_EQ(baseline + cast(kSize), |
| 13604 | cast(isolate->AdjustAmountOfExternalAllocatedMemory(kSize))); |
| 13605 | CHECK_EQ(baseline, |
| 13606 | cast(isolate->AdjustAmountOfExternalAllocatedMemory(-kSize))); |
mads.s.ager@gmail.com | 9a4089a | 2008-09-01 08:55:01 +0000 | [diff] [blame] | 13607 | } |
| 13608 | |
| 13609 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13610 | // Regression test for issue 54, object templates with internal fields |
| 13611 | // but no accessors or interceptors did not get their internal field |
| 13612 | // count set on instances. |
| 13613 | THREADED_TEST(Regress54) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13614 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 13615 | v8::Isolate* isolate = context->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13616 | v8::HandleScope outer(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13617 | static v8::Persistent<v8::ObjectTemplate> templ; |
| 13618 | if (templ.IsEmpty()) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13619 | v8::HandleScope inner(isolate); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13620 | v8::Handle<v8::ObjectTemplate> local = v8::ObjectTemplate::New(); |
| 13621 | local->SetInternalFieldCount(1); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 13622 | templ.Reset(isolate, inner.Close(local)); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13623 | } |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 13624 | v8::Handle<v8::Object> result = |
| 13625 | v8::Local<v8::ObjectTemplate>::New(isolate, templ)->NewInstance(); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13626 | CHECK_EQ(1, result->InternalFieldCount()); |
| 13627 | } |
| 13628 | |
| 13629 | |
| 13630 | // If part of the threaded tests, this test makes ThreadingTest fail |
| 13631 | // on mac. |
| 13632 | TEST(CatchStackOverflow) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13633 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13634 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13635 | v8::TryCatch try_catch; |
| 13636 | v8::Handle<v8::Script> script = v8::Script::Compile(v8::String::New( |
| 13637 | "function f() {" |
| 13638 | " return f();" |
| 13639 | "}" |
| 13640 | "" |
| 13641 | "f();")); |
| 13642 | v8::Handle<v8::Value> result = script->Run(); |
| 13643 | CHECK(result.IsEmpty()); |
| 13644 | } |
| 13645 | |
| 13646 | |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 13647 | static void CheckTryCatchSourceInfo(v8::Handle<v8::Script> script, |
| 13648 | const char* resource_name, |
| 13649 | int line_offset) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13650 | v8::HandleScope scope(CcTest::isolate()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 13651 | v8::TryCatch try_catch; |
| 13652 | v8::Handle<v8::Value> result = script->Run(); |
| 13653 | CHECK(result.IsEmpty()); |
| 13654 | CHECK(try_catch.HasCaught()); |
| 13655 | v8::Handle<v8::Message> message = try_catch.Message(); |
| 13656 | CHECK(!message.IsEmpty()); |
| 13657 | CHECK_EQ(10 + line_offset, message->GetLineNumber()); |
| 13658 | CHECK_EQ(91, message->GetStartPosition()); |
| 13659 | CHECK_EQ(92, message->GetEndPosition()); |
| 13660 | CHECK_EQ(2, message->GetStartColumn()); |
| 13661 | CHECK_EQ(3, message->GetEndColumn()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 13662 | v8::String::Utf8Value line(message->GetSourceLine()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 13663 | CHECK_EQ(" throw 'nirk';", *line); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 13664 | v8::String::Utf8Value name(message->GetScriptResourceName()); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 13665 | CHECK_EQ(resource_name, *name); |
| 13666 | } |
| 13667 | |
| 13668 | |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13669 | THREADED_TEST(TryCatchSourceInfo) { |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13670 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13671 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13672 | v8::Handle<v8::String> source = v8::String::New( |
| 13673 | "function Foo() {\n" |
| 13674 | " return Bar();\n" |
| 13675 | "}\n" |
| 13676 | "\n" |
| 13677 | "function Bar() {\n" |
| 13678 | " return Baz();\n" |
| 13679 | "}\n" |
| 13680 | "\n" |
| 13681 | "function Baz() {\n" |
| 13682 | " throw 'nirk';\n" |
| 13683 | "}\n" |
| 13684 | "\n" |
| 13685 | "Foo();\n"); |
kasperl@chromium.org | 7be3c99 | 2009-03-12 07:19:55 +0000 | [diff] [blame] | 13686 | |
| 13687 | const char* resource_name; |
| 13688 | v8::Handle<v8::Script> script; |
| 13689 | resource_name = "test.js"; |
| 13690 | script = v8::Script::Compile(source, v8::String::New(resource_name)); |
| 13691 | CheckTryCatchSourceInfo(script, resource_name, 0); |
| 13692 | |
| 13693 | resource_name = "test1.js"; |
| 13694 | v8::ScriptOrigin origin1(v8::String::New(resource_name)); |
| 13695 | script = v8::Script::Compile(source, &origin1); |
| 13696 | CheckTryCatchSourceInfo(script, resource_name, 0); |
| 13697 | |
| 13698 | resource_name = "test2.js"; |
| 13699 | v8::ScriptOrigin origin2(v8::String::New(resource_name), v8::Integer::New(7)); |
| 13700 | script = v8::Script::Compile(source, &origin2); |
| 13701 | CheckTryCatchSourceInfo(script, resource_name, 7); |
ager@chromium.org | 9258b6b | 2008-09-11 09:11:10 +0000 | [diff] [blame] | 13702 | } |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 13703 | |
| 13704 | |
| 13705 | THREADED_TEST(CompilationCache) { |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 13706 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13707 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | b912362 | 2008-09-17 14:05:56 +0000 | [diff] [blame] | 13708 | v8::Handle<v8::String> source0 = v8::String::New("1234"); |
| 13709 | v8::Handle<v8::String> source1 = v8::String::New("1234"); |
| 13710 | v8::Handle<v8::Script> script0 = |
| 13711 | v8::Script::Compile(source0, v8::String::New("test.js")); |
| 13712 | v8::Handle<v8::Script> script1 = |
| 13713 | v8::Script::Compile(source1, v8::String::New("test.js")); |
| 13714 | v8::Handle<v8::Script> script2 = |
| 13715 | v8::Script::Compile(source0); // different origin |
| 13716 | CHECK_EQ(1234, script0->Run()->Int32Value()); |
| 13717 | CHECK_EQ(1234, script1->Run()->Int32Value()); |
| 13718 | CHECK_EQ(1234, script2->Run()->Int32Value()); |
| 13719 | } |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13720 | |
| 13721 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13722 | static void FunctionNameCallback( |
| 13723 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13724 | ApiTestFuzzer::Fuzz(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 13725 | args.GetReturnValue().Set(v8_num(42)); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13726 | } |
| 13727 | |
| 13728 | |
| 13729 | THREADED_TEST(CallbackFunctionName) { |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13730 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13731 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13732 | Local<ObjectTemplate> t = ObjectTemplate::New(); |
| 13733 | t->Set(v8_str("asdf"), v8::FunctionTemplate::New(FunctionNameCallback)); |
| 13734 | context->Global()->Set(v8_str("obj"), t->NewInstance()); |
| 13735 | v8::Handle<v8::Value> value = CompileRun("obj.asdf.name"); |
| 13736 | CHECK(value->IsString()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 13737 | v8::String::Utf8Value name(value); |
kasperl@chromium.org | 41044eb | 2008-10-06 08:24:46 +0000 | [diff] [blame] | 13738 | CHECK_EQ("asdf", *name); |
| 13739 | } |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13740 | |
| 13741 | |
| 13742 | THREADED_TEST(DateAccess) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13743 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13744 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13745 | v8::Handle<v8::Value> date = v8::Date::New(1224744689038.0); |
| 13746 | CHECK(date->IsDate()); |
danno@chromium.org | d3c4210 | 2013-08-01 16:58:23 +0000 | [diff] [blame] | 13747 | CHECK_EQ(1224744689038.0, date.As<v8::Date>()->ValueOf()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13748 | } |
| 13749 | |
| 13750 | |
| 13751 | void CheckProperties(v8::Handle<v8::Value> val, int elmc, const char* elmv[]) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 13752 | v8::Handle<v8::Object> obj = val.As<v8::Object>(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13753 | v8::Handle<v8::Array> props = obj->GetPropertyNames(); |
| 13754 | CHECK_EQ(elmc, props->Length()); |
| 13755 | for (int i = 0; i < elmc; i++) { |
| 13756 | v8::String::Utf8Value elm(props->Get(v8::Integer::New(i))); |
| 13757 | CHECK_EQ(elmv[i], *elm); |
| 13758 | } |
| 13759 | } |
| 13760 | |
| 13761 | |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13762 | void CheckOwnProperties(v8::Handle<v8::Value> val, |
| 13763 | int elmc, |
| 13764 | const char* elmv[]) { |
| 13765 | v8::Handle<v8::Object> obj = val.As<v8::Object>(); |
| 13766 | v8::Handle<v8::Array> props = obj->GetOwnPropertyNames(); |
| 13767 | CHECK_EQ(elmc, props->Length()); |
| 13768 | for (int i = 0; i < elmc; i++) { |
| 13769 | v8::String::Utf8Value elm(props->Get(v8::Integer::New(i))); |
| 13770 | CHECK_EQ(elmv[i], *elm); |
| 13771 | } |
| 13772 | } |
| 13773 | |
| 13774 | |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13775 | THREADED_TEST(PropertyEnumeration) { |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13776 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13777 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13778 | v8::Handle<v8::Value> obj = v8::Script::Compile(v8::String::New( |
| 13779 | "var result = [];" |
| 13780 | "result[0] = {};" |
| 13781 | "result[1] = {a: 1, b: 2};" |
| 13782 | "result[2] = [1, 2, 3];" |
| 13783 | "var proto = {x: 1, y: 2, z: 3};" |
| 13784 | "var x = { __proto__: proto, w: 0, z: 1 };" |
| 13785 | "result[3] = x;" |
| 13786 | "result;"))->Run(); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 13787 | v8::Handle<v8::Array> elms = obj.As<v8::Array>(); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13788 | CHECK_EQ(4, elms->Length()); |
| 13789 | int elmc0 = 0; |
| 13790 | const char** elmv0 = NULL; |
| 13791 | CheckProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13792 | CheckOwnProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13793 | int elmc1 = 2; |
| 13794 | const char* elmv1[] = {"a", "b"}; |
| 13795 | CheckProperties(elms->Get(v8::Integer::New(1)), elmc1, elmv1); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13796 | CheckOwnProperties(elms->Get(v8::Integer::New(1)), elmc1, elmv1); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13797 | int elmc2 = 3; |
| 13798 | const char* elmv2[] = {"0", "1", "2"}; |
| 13799 | CheckProperties(elms->Get(v8::Integer::New(2)), elmc2, elmv2); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13800 | CheckOwnProperties(elms->Get(v8::Integer::New(2)), elmc2, elmv2); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13801 | int elmc3 = 4; |
| 13802 | const char* elmv3[] = {"w", "z", "x", "y"}; |
| 13803 | CheckProperties(elms->Get(v8::Integer::New(3)), elmc3, elmv3); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 13804 | int elmc4 = 2; |
| 13805 | const char* elmv4[] = {"w", "z"}; |
| 13806 | CheckOwnProperties(elms->Get(v8::Integer::New(3)), elmc4, elmv4); |
kasperl@chromium.org | 5a8ca6c | 2008-10-23 13:57:19 +0000 | [diff] [blame] | 13807 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13808 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 13809 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13810 | THREADED_TEST(PropertyEnumeration2) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13811 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13812 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 13813 | v8::Handle<v8::Value> obj = v8::Script::Compile(v8::String::New( |
| 13814 | "var result = [];" |
| 13815 | "result[0] = {};" |
| 13816 | "result[1] = {a: 1, b: 2};" |
| 13817 | "result[2] = [1, 2, 3];" |
| 13818 | "var proto = {x: 1, y: 2, z: 3};" |
| 13819 | "var x = { __proto__: proto, w: 0, z: 1 };" |
| 13820 | "result[3] = x;" |
| 13821 | "result;"))->Run(); |
| 13822 | v8::Handle<v8::Array> elms = obj.As<v8::Array>(); |
| 13823 | CHECK_EQ(4, elms->Length()); |
| 13824 | int elmc0 = 0; |
| 13825 | const char** elmv0 = NULL; |
| 13826 | CheckProperties(elms->Get(v8::Integer::New(0)), elmc0, elmv0); |
| 13827 | |
| 13828 | v8::Handle<v8::Value> val = elms->Get(v8::Integer::New(0)); |
| 13829 | v8::Handle<v8::Array> props = val.As<v8::Object>()->GetPropertyNames(); |
| 13830 | CHECK_EQ(0, props->Length()); |
| 13831 | for (uint32_t i = 0; i < props->Length(); i++) { |
| 13832 | printf("p[%d]\n", i); |
| 13833 | } |
| 13834 | } |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13835 | |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13836 | static bool NamedSetAccessBlocker(Local<v8::Object> obj, |
| 13837 | Local<Value> name, |
| 13838 | v8::AccessType type, |
| 13839 | Local<Value> data) { |
| 13840 | return type != v8::ACCESS_SET; |
| 13841 | } |
| 13842 | |
| 13843 | |
| 13844 | static bool IndexedSetAccessBlocker(Local<v8::Object> obj, |
| 13845 | uint32_t key, |
| 13846 | v8::AccessType type, |
| 13847 | Local<Value> data) { |
| 13848 | return type != v8::ACCESS_SET; |
| 13849 | } |
| 13850 | |
| 13851 | |
| 13852 | THREADED_TEST(DisableAccessChecksWhileConfiguring) { |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13853 | LocalContext context; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 13854 | v8::Isolate* isolate = context->GetIsolate(); |
| 13855 | v8::HandleScope scope(isolate); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13856 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 13857 | templ->SetAccessCheckCallbacks(NamedSetAccessBlocker, |
| 13858 | IndexedSetAccessBlocker); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 13859 | templ->Set(v8_str("x"), v8::True(isolate)); |
ager@chromium.org | 870a0b6 | 2008-11-04 11:43:05 +0000 | [diff] [blame] | 13860 | Local<v8::Object> instance = templ->NewInstance(); |
| 13861 | context->Global()->Set(v8_str("obj"), instance); |
| 13862 | Local<Value> value = CompileRun("obj.x"); |
| 13863 | CHECK(value->BooleanValue()); |
| 13864 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 13865 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13866 | |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13867 | static bool NamedGetAccessBlocker(Local<v8::Object> obj, |
| 13868 | Local<Value> name, |
| 13869 | v8::AccessType type, |
| 13870 | Local<Value> data) { |
| 13871 | return false; |
| 13872 | } |
| 13873 | |
| 13874 | |
| 13875 | static bool IndexedGetAccessBlocker(Local<v8::Object> obj, |
| 13876 | uint32_t key, |
| 13877 | v8::AccessType type, |
| 13878 | Local<Value> data) { |
| 13879 | return false; |
| 13880 | } |
| 13881 | |
| 13882 | |
| 13883 | |
| 13884 | THREADED_TEST(AccessChecksReenabledCorrectly) { |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13885 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 13886 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13887 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 13888 | templ->SetAccessCheckCallbacks(NamedGetAccessBlocker, |
| 13889 | IndexedGetAccessBlocker); |
| 13890 | templ->Set(v8_str("a"), v8_str("a")); |
| 13891 | // Add more than 8 (see kMaxFastProperties) properties |
| 13892 | // so that the constructor will force copying map. |
| 13893 | // Cannot sprintf, gcc complains unsafety. |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13894 | char buf[4]; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13895 | for (char i = '0'; i <= '9' ; i++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13896 | buf[0] = i; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13897 | for (char j = '0'; j <= '9'; j++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13898 | buf[1] = j; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13899 | for (char k = '0'; k <= '9'; k++) { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 13900 | buf[2] = k; |
| 13901 | buf[3] = 0; |
ager@chromium.org | 3291210 | 2009-01-16 10:38:43 +0000 | [diff] [blame] | 13902 | templ->Set(v8_str(buf), v8::Number::New(k)); |
| 13903 | } |
| 13904 | } |
| 13905 | } |
| 13906 | |
| 13907 | Local<v8::Object> instance_1 = templ->NewInstance(); |
| 13908 | context->Global()->Set(v8_str("obj_1"), instance_1); |
| 13909 | |
| 13910 | Local<Value> value_1 = CompileRun("obj_1.a"); |
| 13911 | CHECK(value_1->IsUndefined()); |
| 13912 | |
| 13913 | Local<v8::Object> instance_2 = templ->NewInstance(); |
| 13914 | context->Global()->Set(v8_str("obj_2"), instance_2); |
| 13915 | |
| 13916 | Local<Value> value_2 = CompileRun("obj_2.a"); |
| 13917 | CHECK(value_2->IsUndefined()); |
| 13918 | } |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 13919 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13920 | |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 13921 | // This tests that access check information remains on the global |
| 13922 | // object template when creating contexts. |
| 13923 | THREADED_TEST(AccessControlRepeatedContextCreation) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13924 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13925 | v8::HandleScope handle_scope(isolate); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 13926 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 13927 | global_template->SetAccessCheckCallbacks(NamedSetAccessBlocker, |
| 13928 | IndexedSetAccessBlocker); |
| 13929 | i::Handle<i::ObjectTemplateInfo> internal_template = |
| 13930 | v8::Utils::OpenHandle(*global_template); |
| 13931 | CHECK(!internal_template->constructor()->IsUndefined()); |
| 13932 | i::Handle<i::FunctionTemplateInfo> constructor( |
| 13933 | i::FunctionTemplateInfo::cast(internal_template->constructor())); |
| 13934 | CHECK(!constructor->access_check_info()->IsUndefined()); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13935 | v8::Local<Context> context0(Context::New(isolate, NULL, global_template)); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 13936 | CHECK(!context0.IsEmpty()); |
ager@chromium.org | 8bb6058 | 2008-12-11 12:02:20 +0000 | [diff] [blame] | 13937 | CHECK(!constructor->access_check_info()->IsUndefined()); |
| 13938 | } |
| 13939 | |
| 13940 | |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13941 | THREADED_TEST(TurnOnAccessCheck) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 13942 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13943 | v8::HandleScope handle_scope(isolate); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13944 | |
| 13945 | // Create an environment with access check to the global object disabled by |
| 13946 | // default. |
| 13947 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 13948 | global_template->SetAccessCheckCallbacks(NamedGetAccessBlocker, |
| 13949 | IndexedGetAccessBlocker, |
| 13950 | v8::Handle<v8::Value>(), |
| 13951 | false); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 13952 | v8::Local<Context> context = Context::New(isolate, NULL, global_template); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 13953 | Context::Scope context_scope(context); |
| 13954 | |
| 13955 | // Set up a property and a number of functions. |
| 13956 | context->Global()->Set(v8_str("a"), v8_num(1)); |
| 13957 | CompileRun("function f1() {return a;}" |
| 13958 | "function f2() {return a;}" |
| 13959 | "function g1() {return h();}" |
| 13960 | "function g2() {return h();}" |
| 13961 | "function h() {return 1;}"); |
| 13962 | Local<Function> f1 = |
| 13963 | Local<Function>::Cast(context->Global()->Get(v8_str("f1"))); |
| 13964 | Local<Function> f2 = |
| 13965 | Local<Function>::Cast(context->Global()->Get(v8_str("f2"))); |
| 13966 | Local<Function> g1 = |
| 13967 | Local<Function>::Cast(context->Global()->Get(v8_str("g1"))); |
| 13968 | Local<Function> g2 = |
| 13969 | Local<Function>::Cast(context->Global()->Get(v8_str("g2"))); |
| 13970 | Local<Function> h = |
| 13971 | Local<Function>::Cast(context->Global()->Get(v8_str("h"))); |
| 13972 | |
| 13973 | // Get the global object. |
| 13974 | v8::Handle<v8::Object> global = context->Global(); |
| 13975 | |
| 13976 | // Call f1 one time and f2 a number of times. This will ensure that f1 still |
| 13977 | // uses the runtime system to retreive property a whereas f2 uses global load |
| 13978 | // inline cache. |
| 13979 | CHECK(f1->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 13980 | for (int i = 0; i < 4; i++) { |
| 13981 | CHECK(f2->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 13982 | } |
| 13983 | |
| 13984 | // Same for g1 and g2. |
| 13985 | CHECK(g1->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 13986 | for (int i = 0; i < 4; i++) { |
| 13987 | CHECK(g2->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 13988 | } |
| 13989 | |
| 13990 | // Detach the global and turn on access check. |
| 13991 | context->DetachGlobal(); |
| 13992 | context->Global()->TurnOnAccessCheck(); |
| 13993 | |
| 13994 | // Failing access check to property get results in undefined. |
| 13995 | CHECK(f1->Call(global, 0, NULL)->IsUndefined()); |
| 13996 | CHECK(f2->Call(global, 0, NULL)->IsUndefined()); |
| 13997 | |
| 13998 | // Failing access check to function call results in exception. |
| 13999 | CHECK(g1->Call(global, 0, NULL).IsEmpty()); |
| 14000 | CHECK(g2->Call(global, 0, NULL).IsEmpty()); |
| 14001 | |
| 14002 | // No failing access check when just returning a constant. |
| 14003 | CHECK(h->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14004 | } |
| 14005 | |
| 14006 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 14007 | static const char* kPropertyA = "a"; |
| 14008 | static const char* kPropertyH = "h"; |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14009 | |
| 14010 | static bool NamedGetAccessBlockAandH(Local<v8::Object> obj, |
| 14011 | Local<Value> name, |
| 14012 | v8::AccessType type, |
| 14013 | Local<Value> data) { |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 14014 | if (!name->IsString()) return false; |
| 14015 | i::Handle<i::String> name_handle = |
| 14016 | v8::Utils::OpenHandle(String::Cast(*name)); |
jkummerow@chromium.org | 59297c7 | 2013-01-09 16:32:23 +0000 | [diff] [blame] | 14017 | return !name_handle->IsUtf8EqualTo(i::CStrVector(kPropertyA)) |
| 14018 | && !name_handle->IsUtf8EqualTo(i::CStrVector(kPropertyH)); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14019 | } |
| 14020 | |
| 14021 | |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 14022 | THREADED_TEST(TurnOnAccessCheckAndRecompile) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14023 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14024 | v8::HandleScope handle_scope(isolate); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14025 | |
| 14026 | // Create an environment with access check to the global object disabled by |
| 14027 | // default. When the registered access checker will block access to properties |
jkummerow@chromium.org | f7a5884 | 2012-02-21 10:08:21 +0000 | [diff] [blame] | 14028 | // a and h. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14029 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 14030 | global_template->SetAccessCheckCallbacks(NamedGetAccessBlockAandH, |
| 14031 | IndexedGetAccessBlocker, |
| 14032 | v8::Handle<v8::Value>(), |
| 14033 | false); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14034 | v8::Local<Context> context = Context::New(isolate, NULL, global_template); |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 14035 | Context::Scope context_scope(context); |
| 14036 | |
| 14037 | // Set up a property and a number of functions. |
| 14038 | context->Global()->Set(v8_str("a"), v8_num(1)); |
| 14039 | static const char* source = "function f1() {return a;}" |
| 14040 | "function f2() {return a;}" |
| 14041 | "function g1() {return h();}" |
| 14042 | "function g2() {return h();}" |
| 14043 | "function h() {return 1;}"; |
| 14044 | |
| 14045 | CompileRun(source); |
| 14046 | Local<Function> f1; |
| 14047 | Local<Function> f2; |
| 14048 | Local<Function> g1; |
| 14049 | Local<Function> g2; |
| 14050 | Local<Function> h; |
| 14051 | f1 = Local<Function>::Cast(context->Global()->Get(v8_str("f1"))); |
| 14052 | f2 = Local<Function>::Cast(context->Global()->Get(v8_str("f2"))); |
| 14053 | g1 = Local<Function>::Cast(context->Global()->Get(v8_str("g1"))); |
| 14054 | g2 = Local<Function>::Cast(context->Global()->Get(v8_str("g2"))); |
| 14055 | h = Local<Function>::Cast(context->Global()->Get(v8_str("h"))); |
| 14056 | |
| 14057 | // Get the global object. |
| 14058 | v8::Handle<v8::Object> global = context->Global(); |
| 14059 | |
| 14060 | // Call f1 one time and f2 a number of times. This will ensure that f1 still |
| 14061 | // uses the runtime system to retreive property a whereas f2 uses global load |
| 14062 | // inline cache. |
| 14063 | CHECK(f1->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14064 | for (int i = 0; i < 4; i++) { |
| 14065 | CHECK(f2->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14066 | } |
| 14067 | |
| 14068 | // Same for g1 and g2. |
| 14069 | CHECK(g1->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14070 | for (int i = 0; i < 4; i++) { |
| 14071 | CHECK(g2->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14072 | } |
| 14073 | |
| 14074 | // Detach the global and turn on access check now blocking access to property |
| 14075 | // a and function h. |
| 14076 | context->DetachGlobal(); |
| 14077 | context->Global()->TurnOnAccessCheck(); |
| 14078 | |
| 14079 | // Failing access check to property get results in undefined. |
| 14080 | CHECK(f1->Call(global, 0, NULL)->IsUndefined()); |
| 14081 | CHECK(f2->Call(global, 0, NULL)->IsUndefined()); |
| 14082 | |
| 14083 | // Failing access check to function call results in exception. |
| 14084 | CHECK(g1->Call(global, 0, NULL).IsEmpty()); |
| 14085 | CHECK(g2->Call(global, 0, NULL).IsEmpty()); |
| 14086 | |
| 14087 | // No failing access check when just returning a constant. |
| 14088 | CHECK(h->Call(global, 0, NULL)->Equals(v8_num(1))); |
| 14089 | |
| 14090 | // Now compile the source again. And get the newly compiled functions, except |
| 14091 | // for h for which access is blocked. |
| 14092 | CompileRun(source); |
| 14093 | f1 = Local<Function>::Cast(context->Global()->Get(v8_str("f1"))); |
| 14094 | f2 = Local<Function>::Cast(context->Global()->Get(v8_str("f2"))); |
| 14095 | g1 = Local<Function>::Cast(context->Global()->Get(v8_str("g1"))); |
| 14096 | g2 = Local<Function>::Cast(context->Global()->Get(v8_str("g2"))); |
| 14097 | CHECK(context->Global()->Get(v8_str("h"))->IsUndefined()); |
| 14098 | |
| 14099 | // Failing access check to property get results in undefined. |
| 14100 | CHECK(f1->Call(global, 0, NULL)->IsUndefined()); |
| 14101 | CHECK(f2->Call(global, 0, NULL)->IsUndefined()); |
| 14102 | |
| 14103 | // Failing access check to function call results in exception. |
| 14104 | CHECK(g1->Call(global, 0, NULL).IsEmpty()); |
| 14105 | CHECK(g2->Call(global, 0, NULL).IsEmpty()); |
| 14106 | } |
| 14107 | |
| 14108 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14109 | // This test verifies that pre-compilation (aka preparsing) can be called |
| 14110 | // without initializing the whole VM. Thus we cannot run this test in a |
| 14111 | // multi-threaded setup. |
| 14112 | TEST(PreCompile) { |
| 14113 | // TODO(155): This test would break without the initialization of V8. This is |
| 14114 | // a workaround for now to make this test not fail. |
| 14115 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14116 | v8::Isolate* isolate = CcTest::isolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14117 | const char* script = "function foo(a) { return a+1; }"; |
| 14118 | v8::ScriptData* sd = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14119 | v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14120 | CHECK_NE(sd->Length(), 0); |
| 14121 | CHECK_NE(sd->Data(), NULL); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14122 | CHECK(!sd->HasError()); |
| 14123 | delete sd; |
| 14124 | } |
| 14125 | |
| 14126 | |
| 14127 | TEST(PreCompileWithError) { |
| 14128 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14129 | v8::Isolate* isolate = CcTest::isolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14130 | const char* script = "function foo(a) { return 1 * * 2; }"; |
| 14131 | v8::ScriptData* sd = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14132 | v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14133 | CHECK(sd->HasError()); |
| 14134 | delete sd; |
| 14135 | } |
| 14136 | |
| 14137 | |
| 14138 | TEST(Regress31661) { |
| 14139 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14140 | v8::Isolate* isolate = CcTest::isolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14141 | const char* script = " The Definintive Guide"; |
| 14142 | v8::ScriptData* sd = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14143 | v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
fschneider@chromium.org | 0c20e67 | 2010-01-14 15:28:53 +0000 | [diff] [blame] | 14144 | CHECK(sd->HasError()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14145 | delete sd; |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14146 | } |
| 14147 | |
| 14148 | |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14149 | // Tests that ScriptData can be serialized and deserialized. |
| 14150 | TEST(PreCompileSerialization) { |
| 14151 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14152 | v8::Isolate* isolate = CcTest::isolate(); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14153 | const char* script = "function foo(a) { return a+1; }"; |
| 14154 | v8::ScriptData* sd = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14155 | v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14156 | |
| 14157 | // Serialize. |
| 14158 | int serialized_data_length = sd->Length(); |
| 14159 | char* serialized_data = i::NewArray<char>(serialized_data_length); |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 14160 | i::OS::MemCopy(serialized_data, sd->Data(), serialized_data_length); |
kmillikin@chromium.org | 9155e25 | 2010-05-26 13:27:57 +0000 | [diff] [blame] | 14161 | |
| 14162 | // Deserialize. |
| 14163 | v8::ScriptData* deserialized_sd = |
| 14164 | v8::ScriptData::New(serialized_data, serialized_data_length); |
| 14165 | |
| 14166 | // Verify that the original is the same as the deserialized. |
| 14167 | CHECK_EQ(sd->Length(), deserialized_sd->Length()); |
| 14168 | CHECK_EQ(0, memcmp(sd->Data(), deserialized_sd->Data(), sd->Length())); |
| 14169 | CHECK_EQ(sd->HasError(), deserialized_sd->HasError()); |
| 14170 | |
| 14171 | delete sd; |
| 14172 | delete deserialized_sd; |
| 14173 | } |
| 14174 | |
| 14175 | |
| 14176 | // Attempts to deserialize bad data. |
| 14177 | TEST(PreCompileDeserializationError) { |
| 14178 | v8::V8::Initialize(); |
| 14179 | const char* data = "DONT CARE"; |
| 14180 | int invalid_size = 3; |
| 14181 | v8::ScriptData* sd = v8::ScriptData::New(data, invalid_size); |
| 14182 | |
| 14183 | CHECK_EQ(0, sd->Length()); |
| 14184 | |
| 14185 | delete sd; |
| 14186 | } |
| 14187 | |
| 14188 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14189 | // Attempts to deserialize bad data. |
| 14190 | TEST(PreCompileInvalidPreparseDataError) { |
| 14191 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14192 | v8::Isolate* isolate = CcTest::isolate(); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14193 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14194 | v8::HandleScope scope(context->GetIsolate()); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14195 | |
| 14196 | const char* script = "function foo(){ return 5;}\n" |
| 14197 | "function bar(){ return 6 + 7;} foo();"; |
| 14198 | v8::ScriptData* sd = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14199 | v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14200 | CHECK(!sd->HasError()); |
| 14201 | // ScriptDataImpl private implementation details |
ager@chromium.org | beb2571 | 2010-11-29 08:02:25 +0000 | [diff] [blame] | 14202 | const int kHeaderSize = i::PreparseDataConstants::kHeaderSize; |
ager@chromium.org | 5b2fbee | 2010-09-08 06:38:15 +0000 | [diff] [blame] | 14203 | const int kFunctionEntrySize = i::FunctionEntry::kSize; |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14204 | const int kFunctionEntryStartOffset = 0; |
| 14205 | const int kFunctionEntryEndOffset = 1; |
| 14206 | unsigned* sd_data = |
| 14207 | reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14208 | |
| 14209 | // Overwrite function bar's end position with 0. |
| 14210 | sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryEndOffset] = 0; |
| 14211 | v8::TryCatch try_catch; |
| 14212 | |
| 14213 | Local<String> source = String::New(script); |
| 14214 | Local<Script> compiled_script = Script::New(source, NULL, sd); |
| 14215 | CHECK(try_catch.HasCaught()); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 14216 | String::Utf8Value exception_value(try_catch.Message()->Get()); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14217 | CHECK_EQ("Uncaught SyntaxError: Invalid preparser data for function bar", |
| 14218 | *exception_value); |
| 14219 | |
| 14220 | try_catch.Reset(); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14221 | |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14222 | // Overwrite function bar's start position with 200. The function entry |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14223 | // will not be found when searching for it by position and we should fall |
| 14224 | // back on eager compilation. |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14225 | sd = v8::ScriptData::PreCompile(isolate, script, i::StrLength(script)); |
ricow@chromium.org | d236f4d | 2010-09-01 06:52:08 +0000 | [diff] [blame] | 14226 | sd_data = reinterpret_cast<unsigned*>(const_cast<char*>(sd->Data())); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14227 | sd_data[kHeaderSize + 1 * kFunctionEntrySize + kFunctionEntryStartOffset] = |
| 14228 | 200; |
| 14229 | compiled_script = Script::New(source, NULL, sd); |
lrn@chromium.org | d4e9e22 | 2011-08-03 12:01:58 +0000 | [diff] [blame] | 14230 | CHECK(!try_catch.HasCaught()); |
erik.corry@gmail.com | 4a2e25e | 2010-07-07 12:22:46 +0000 | [diff] [blame] | 14231 | |
| 14232 | delete sd; |
| 14233 | } |
| 14234 | |
| 14235 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14236 | // Verifies that the Handle<String> and const char* versions of the API produce |
| 14237 | // the same results (at least for one trivial case). |
| 14238 | TEST(PreCompileAPIVariationsAreSame) { |
| 14239 | v8::V8::Initialize(); |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14240 | v8::Isolate* isolate = CcTest::isolate(); |
| 14241 | v8::HandleScope scope(isolate); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14242 | |
| 14243 | const char* cstring = "function foo(a) { return a+1; }"; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14244 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14245 | v8::ScriptData* sd_from_cstring = |
jkummerow@chromium.org | fb7a7c4 | 2013-10-02 11:41:02 +0000 | [diff] [blame] | 14246 | v8::ScriptData::PreCompile(isolate, cstring, i::StrLength(cstring)); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14247 | |
| 14248 | TestAsciiResource* resource = new TestAsciiResource(cstring); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14249 | v8::ScriptData* sd_from_external_string = v8::ScriptData::PreCompile( |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14250 | v8::String::NewExternal(resource)); |
| 14251 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14252 | v8::ScriptData* sd_from_string = v8::ScriptData::PreCompile( |
| 14253 | v8::String::New(cstring)); |
| 14254 | |
| 14255 | CHECK_EQ(sd_from_cstring->Length(), sd_from_external_string->Length()); |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14256 | CHECK_EQ(0, memcmp(sd_from_cstring->Data(), |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14257 | sd_from_external_string->Data(), |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14258 | sd_from_cstring->Length())); |
| 14259 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14260 | CHECK_EQ(sd_from_cstring->Length(), sd_from_string->Length()); |
| 14261 | CHECK_EQ(0, memcmp(sd_from_cstring->Data(), |
| 14262 | sd_from_string->Data(), |
| 14263 | sd_from_cstring->Length())); |
| 14264 | |
| 14265 | |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14266 | delete sd_from_cstring; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14267 | delete sd_from_external_string; |
| 14268 | delete sd_from_string; |
vegorov@chromium.org | 2356e6f | 2010-06-09 09:38:56 +0000 | [diff] [blame] | 14269 | } |
| 14270 | |
| 14271 | |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14272 | // This tests that we do not allow dictionary load/call inline caches |
| 14273 | // to use functions that have not yet been compiled. The potential |
| 14274 | // problem of loading a function that has not yet been compiled can |
| 14275 | // arise because we share code between contexts via the compilation |
| 14276 | // cache. |
| 14277 | THREADED_TEST(DictionaryICLoadedFunction) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14278 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14279 | // Test LoadIC. |
| 14280 | for (int i = 0; i < 2; i++) { |
| 14281 | LocalContext context; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 14282 | context->Global()->Set(v8_str("tmp"), v8::True(CcTest::isolate())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14283 | context->Global()->Delete(v8_str("tmp")); |
| 14284 | CompileRun("for (var j = 0; j < 10; j++) new RegExp('');"); |
| 14285 | } |
| 14286 | // Test CallIC. |
| 14287 | for (int i = 0; i < 2; i++) { |
| 14288 | LocalContext context; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 14289 | context->Global()->Set(v8_str("tmp"), v8::True(CcTest::isolate())); |
ager@chromium.org | a74f0da | 2008-12-03 16:05:52 +0000 | [diff] [blame] | 14290 | context->Global()->Delete(v8_str("tmp")); |
| 14291 | CompileRun("for (var j = 0; j < 10; j++) RegExp('')"); |
| 14292 | } |
| 14293 | } |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 14294 | |
| 14295 | |
| 14296 | // Test that cross-context new calls use the context of the callee to |
| 14297 | // create the new JavaScript object. |
| 14298 | THREADED_TEST(CrossContextNew) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14299 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14300 | v8::HandleScope scope(isolate); |
| 14301 | v8::Local<Context> context0 = Context::New(isolate); |
| 14302 | v8::Local<Context> context1 = Context::New(isolate); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 14303 | |
| 14304 | // Allow cross-domain access. |
| 14305 | Local<String> token = v8_str("<security token>"); |
| 14306 | context0->SetSecurityToken(token); |
| 14307 | context1->SetSecurityToken(token); |
| 14308 | |
| 14309 | // Set an 'x' property on the Object prototype and define a |
| 14310 | // constructor function in context0. |
| 14311 | context0->Enter(); |
| 14312 | CompileRun("Object.prototype.x = 42; function C() {};"); |
| 14313 | context0->Exit(); |
| 14314 | |
| 14315 | // Call the constructor function from context0 and check that the |
| 14316 | // result has the 'x' property. |
| 14317 | context1->Enter(); |
| 14318 | context1->Global()->Set(v8_str("other"), context0->Global()); |
| 14319 | Local<Value> value = CompileRun("var instance = new other.C(); instance.x"); |
| 14320 | CHECK(value->IsInt32()); |
| 14321 | CHECK_EQ(42, value->Int32Value()); |
| 14322 | context1->Exit(); |
ager@chromium.org | ddb913d | 2009-01-27 10:01:48 +0000 | [diff] [blame] | 14323 | } |
ager@chromium.org | 381abbb | 2009-02-25 13:23:22 +0000 | [diff] [blame] | 14324 | |
| 14325 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14326 | class ApplyInterruptTest { |
| 14327 | public: |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 14328 | ApplyInterruptTest() : block_(0) {} |
| 14329 | ~ApplyInterruptTest() {} |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14330 | void RunTest() { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14331 | gc_count_ = 0; |
| 14332 | gc_during_apply_ = 0; |
| 14333 | apply_success_ = false; |
| 14334 | gc_success_ = false; |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 14335 | GCThread gc_thread(this); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14336 | gc_thread.Start(); |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 14337 | v8::Isolate* isolate = CcTest::isolate(); |
| 14338 | v8::Locker::StartPreemption(isolate, 1); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14339 | |
| 14340 | LongRunningApply(); |
| 14341 | { |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 14342 | v8::Unlocker unlock(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14343 | gc_thread.Join(); |
| 14344 | } |
jkummerow@chromium.org | c366976 | 2013-09-30 13:42:25 +0000 | [diff] [blame] | 14345 | v8::Locker::StopPreemption(isolate); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14346 | CHECK(apply_success_); |
| 14347 | CHECK(gc_success_); |
| 14348 | } |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 14349 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14350 | private: |
| 14351 | // Number of garbage collections required. |
| 14352 | static const int kRequiredGCs = 2; |
| 14353 | |
| 14354 | class GCThread : public i::Thread { |
| 14355 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 14356 | explicit GCThread(ApplyInterruptTest* test) |
| 14357 | : Thread("GCThread"), test_(test) {} |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14358 | virtual void Run() { |
| 14359 | test_->CollectGarbage(); |
| 14360 | } |
| 14361 | private: |
| 14362 | ApplyInterruptTest* test_; |
| 14363 | }; |
| 14364 | |
| 14365 | void CollectGarbage() { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 14366 | block_.Wait(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14367 | while (gc_during_apply_ < kRequiredGCs) { |
| 14368 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14369 | v8::Locker lock(CcTest::isolate()); |
| 14370 | v8::Isolate::Scope isolate_scope(CcTest::isolate()); |
| 14371 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14372 | gc_count_++; |
| 14373 | } |
| 14374 | i::OS::Sleep(1); |
| 14375 | } |
| 14376 | gc_success_ = true; |
| 14377 | } |
| 14378 | |
| 14379 | void LongRunningApply() { |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 14380 | block_.Signal(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14381 | int rounds = 0; |
| 14382 | while (gc_during_apply_ < kRequiredGCs) { |
| 14383 | int gc_before = gc_count_; |
| 14384 | { |
| 14385 | const char* c_source = |
| 14386 | "function do_very_little(bar) {" |
| 14387 | " this.foo = bar;" |
| 14388 | "}" |
| 14389 | "for (var i = 0; i < 100000; i++) {" |
| 14390 | " do_very_little.apply(this, ['bar']);" |
| 14391 | "}"; |
| 14392 | Local<String> source = String::New(c_source); |
| 14393 | Local<Script> script = Script::Compile(source); |
| 14394 | Local<Value> result = script->Run(); |
ager@chromium.org | 3a37e9b | 2009-04-27 09:26:21 +0000 | [diff] [blame] | 14395 | // Check that no exception was thrown. |
| 14396 | CHECK(!result.IsEmpty()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14397 | } |
| 14398 | int gc_after = gc_count_; |
| 14399 | gc_during_apply_ += gc_after - gc_before; |
| 14400 | rounds++; |
| 14401 | } |
| 14402 | apply_success_ = true; |
| 14403 | } |
| 14404 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 14405 | i::Semaphore block_; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14406 | int gc_count_; |
| 14407 | int gc_during_apply_; |
| 14408 | bool apply_success_; |
| 14409 | bool gc_success_; |
| 14410 | }; |
| 14411 | |
| 14412 | |
| 14413 | // Test that nothing bad happens if we get a preemption just when we were |
| 14414 | // about to do an apply(). |
| 14415 | TEST(ApplyInterruption) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14416 | v8::Locker lock(CcTest::isolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14417 | v8::V8::Initialize(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14418 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14419 | Local<Context> local_env; |
| 14420 | { |
| 14421 | LocalContext env; |
| 14422 | local_env = env.local(); |
| 14423 | } |
| 14424 | |
| 14425 | // Local context should still be live. |
| 14426 | CHECK(!local_env.IsEmpty()); |
| 14427 | local_env->Enter(); |
| 14428 | |
| 14429 | // Should complete without problems. |
| 14430 | ApplyInterruptTest().RunTest(); |
| 14431 | |
| 14432 | local_env->Exit(); |
| 14433 | } |
| 14434 | |
| 14435 | |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 14436 | // Verify that we can clone an object |
| 14437 | TEST(ObjectClone) { |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 14438 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14439 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 14440 | |
| 14441 | const char* sample = |
| 14442 | "var rv = {};" \ |
| 14443 | "rv.alpha = 'hello';" \ |
| 14444 | "rv.beta = 123;" \ |
| 14445 | "rv;"; |
| 14446 | |
| 14447 | // Create an object, verify basics. |
| 14448 | Local<Value> val = CompileRun(sample); |
| 14449 | CHECK(val->IsObject()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 14450 | Local<v8::Object> obj = val.As<v8::Object>(); |
ager@chromium.org | 3b45ab5 | 2009-03-19 22:21:34 +0000 | [diff] [blame] | 14451 | obj->Set(v8_str("gamma"), v8_str("cloneme")); |
| 14452 | |
| 14453 | CHECK_EQ(v8_str("hello"), obj->Get(v8_str("alpha"))); |
| 14454 | CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); |
| 14455 | CHECK_EQ(v8_str("cloneme"), obj->Get(v8_str("gamma"))); |
| 14456 | |
| 14457 | // Clone it. |
| 14458 | Local<v8::Object> clone = obj->Clone(); |
| 14459 | CHECK_EQ(v8_str("hello"), clone->Get(v8_str("alpha"))); |
| 14460 | CHECK_EQ(v8::Integer::New(123), clone->Get(v8_str("beta"))); |
| 14461 | CHECK_EQ(v8_str("cloneme"), clone->Get(v8_str("gamma"))); |
| 14462 | |
| 14463 | // Set a property on the clone, verify each object. |
| 14464 | clone->Set(v8_str("beta"), v8::Integer::New(456)); |
| 14465 | CHECK_EQ(v8::Integer::New(123), obj->Get(v8_str("beta"))); |
| 14466 | CHECK_EQ(v8::Integer::New(456), clone->Get(v8_str("beta"))); |
| 14467 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14468 | |
| 14469 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14470 | class AsciiVectorResource : public v8::String::ExternalAsciiStringResource { |
| 14471 | public: |
| 14472 | explicit AsciiVectorResource(i::Vector<const char> vector) |
| 14473 | : data_(vector) {} |
| 14474 | virtual ~AsciiVectorResource() {} |
| 14475 | virtual size_t length() const { return data_.length(); } |
| 14476 | virtual const char* data() const { return data_.start(); } |
| 14477 | private: |
| 14478 | i::Vector<const char> data_; |
| 14479 | }; |
| 14480 | |
| 14481 | |
| 14482 | class UC16VectorResource : public v8::String::ExternalStringResource { |
| 14483 | public: |
| 14484 | explicit UC16VectorResource(i::Vector<const i::uc16> vector) |
| 14485 | : data_(vector) {} |
| 14486 | virtual ~UC16VectorResource() {} |
| 14487 | virtual size_t length() const { return data_.length(); } |
| 14488 | virtual const i::uc16* data() const { return data_.start(); } |
| 14489 | private: |
| 14490 | i::Vector<const i::uc16> data_; |
| 14491 | }; |
| 14492 | |
| 14493 | |
| 14494 | static void MorphAString(i::String* string, |
| 14495 | AsciiVectorResource* ascii_resource, |
| 14496 | UC16VectorResource* uc16_resource) { |
| 14497 | CHECK(i::StringShape(string).IsExternal()); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 14498 | if (string->IsOneByteRepresentation()) { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14499 | // Check old map is not internalized or long. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14500 | CHECK(string->map() == CcTest::heap()->external_ascii_string_map()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14501 | // Morph external string to be TwoByte string. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14502 | string->set_map(CcTest::heap()->external_string_map()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14503 | i::ExternalTwoByteString* morphed = |
| 14504 | i::ExternalTwoByteString::cast(string); |
| 14505 | morphed->set_resource(uc16_resource); |
| 14506 | } else { |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 14507 | // Check old map is not internalized or long. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14508 | CHECK(string->map() == CcTest::heap()->external_string_map()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14509 | // Morph external string to be ASCII string. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14510 | string->set_map(CcTest::heap()->external_ascii_string_map()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14511 | i::ExternalAsciiString* morphed = |
| 14512 | i::ExternalAsciiString::cast(string); |
| 14513 | morphed->set_resource(ascii_resource); |
| 14514 | } |
| 14515 | } |
| 14516 | |
| 14517 | |
| 14518 | // Test that we can still flatten a string if the components it is built up |
| 14519 | // from have been turned into 16 bit strings in the mean time. |
| 14520 | THREADED_TEST(MorphCompositeStringTest) { |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14521 | char utf_buffer[129]; |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14522 | const char* c_string = "Now is the time for all good men" |
| 14523 | " to come to the aid of the party"; |
| 14524 | uint16_t* two_byte_string = AsciiToTwoByteString(c_string); |
| 14525 | { |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14526 | LocalContext env; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14527 | i::Factory* factory = CcTest::i_isolate()->factory(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14528 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14529 | AsciiVectorResource ascii_resource( |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 14530 | i::Vector<const char>(c_string, i::StrLength(c_string))); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14531 | UC16VectorResource uc16_resource( |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 14532 | i::Vector<const uint16_t>(two_byte_string, |
| 14533 | i::StrLength(c_string))); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14534 | |
| 14535 | Local<String> lhs(v8::Utils::ToLocal( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 14536 | factory->NewExternalStringFromAscii(&ascii_resource))); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14537 | Local<String> rhs(v8::Utils::ToLocal( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 14538 | factory->NewExternalStringFromAscii(&ascii_resource))); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14539 | |
| 14540 | env->Global()->Set(v8_str("lhs"), lhs); |
| 14541 | env->Global()->Set(v8_str("rhs"), rhs); |
| 14542 | |
| 14543 | CompileRun( |
| 14544 | "var cons = lhs + rhs;" |
| 14545 | "var slice = lhs.substring(1, lhs.length - 1);" |
| 14546 | "var slice_on_cons = (lhs + rhs).substring(1, lhs.length *2 - 1);"); |
| 14547 | |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14548 | CHECK(lhs->IsOneByte()); |
| 14549 | CHECK(rhs->IsOneByte()); |
rossberg@chromium.org | 2c067b1 | 2012-03-19 11:01:52 +0000 | [diff] [blame] | 14550 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14551 | MorphAString(*v8::Utils::OpenHandle(*lhs), &ascii_resource, &uc16_resource); |
| 14552 | MorphAString(*v8::Utils::OpenHandle(*rhs), &ascii_resource, &uc16_resource); |
| 14553 | |
erik.corry@gmail.com | 394dbcf | 2011-10-27 07:38:48 +0000 | [diff] [blame] | 14554 | // This should UTF-8 without flattening, since everything is ASCII. |
| 14555 | Handle<String> cons = v8_compile("cons")->Run().As<String>(); |
| 14556 | CHECK_EQ(128, cons->Utf8Length()); |
| 14557 | int nchars = -1; |
| 14558 | CHECK_EQ(129, cons->WriteUtf8(utf_buffer, -1, &nchars)); |
| 14559 | CHECK_EQ(128, nchars); |
| 14560 | CHECK_EQ(0, strcmp( |
| 14561 | utf_buffer, |
| 14562 | "Now is the time for all good men to come to the aid of the party" |
| 14563 | "Now is the time for all good men to come to the aid of the party")); |
| 14564 | |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14565 | // Now do some stuff to make sure the strings are flattened, etc. |
| 14566 | CompileRun( |
| 14567 | "/[^a-z]/.test(cons);" |
| 14568 | "/[^a-z]/.test(slice);" |
| 14569 | "/[^a-z]/.test(slice_on_cons);"); |
| 14570 | const char* expected_cons = |
| 14571 | "Now is the time for all good men to come to the aid of the party" |
| 14572 | "Now is the time for all good men to come to the aid of the party"; |
| 14573 | const char* expected_slice = |
| 14574 | "ow is the time for all good men to come to the aid of the part"; |
| 14575 | const char* expected_slice_on_cons = |
| 14576 | "ow is the time for all good men to come to the aid of the party" |
| 14577 | "Now is the time for all good men to come to the aid of the part"; |
| 14578 | CHECK_EQ(String::New(expected_cons), |
| 14579 | env->Global()->Get(v8_str("cons"))); |
| 14580 | CHECK_EQ(String::New(expected_slice), |
| 14581 | env->Global()->Get(v8_str("slice"))); |
| 14582 | CHECK_EQ(String::New(expected_slice_on_cons), |
| 14583 | env->Global()->Get(v8_str("slice_on_cons"))); |
| 14584 | } |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14585 | i::DeleteArray(two_byte_string); |
ager@chromium.org | 5ec4892 | 2009-05-05 07:25:34 +0000 | [diff] [blame] | 14586 | } |
| 14587 | |
| 14588 | |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 14589 | TEST(CompileExternalTwoByteSource) { |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 14590 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14591 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 14592 | |
| 14593 | // This is a very short list of sources, which currently is to check for a |
| 14594 | // regression caused by r2703. |
| 14595 | const char* ascii_sources[] = { |
| 14596 | "0.5", |
| 14597 | "-0.5", // This mainly testes PushBack in the Scanner. |
| 14598 | "--0.5", // This mainly testes PushBack in the Scanner. |
| 14599 | NULL |
| 14600 | }; |
| 14601 | |
| 14602 | // Compile the sources as external two byte strings. |
| 14603 | for (int i = 0; ascii_sources[i] != NULL; i++) { |
| 14604 | uint16_t* two_byte_string = AsciiToTwoByteString(ascii_sources[i]); |
| 14605 | UC16VectorResource uc16_resource( |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 14606 | i::Vector<const uint16_t>(two_byte_string, |
| 14607 | i::StrLength(ascii_sources[i]))); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 14608 | v8::Local<v8::String> source = v8::String::NewExternal(&uc16_resource); |
| 14609 | v8::Script::Compile(source); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 14610 | i::DeleteArray(two_byte_string); |
sgjesse@chromium.org | c81c894 | 2009-08-21 10:54:26 +0000 | [diff] [blame] | 14611 | } |
| 14612 | } |
| 14613 | |
| 14614 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 14615 | #ifndef V8_INTERPRETED_REGEXP |
| 14616 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14617 | struct RegExpInterruptionData { |
| 14618 | int loop_count; |
| 14619 | UC16VectorResource* string_resource; |
| 14620 | v8::Persistent<v8::String> string; |
| 14621 | } regexp_interruption_data; |
| 14622 | |
| 14623 | |
| 14624 | class RegExpInterruptionThread : public i::Thread { |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14625 | public: |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14626 | explicit RegExpInterruptionThread(v8::Isolate* isolate) |
| 14627 | : Thread("TimeoutThread"), isolate_(isolate) {} |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 14628 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14629 | virtual void Run() { |
| 14630 | for (regexp_interruption_data.loop_count = 0; |
| 14631 | regexp_interruption_data.loop_count < 7; |
| 14632 | regexp_interruption_data.loop_count++) { |
| 14633 | i::OS::Sleep(50); // Wait a bit before requesting GC. |
| 14634 | reinterpret_cast<i::Isolate*>(isolate_)->stack_guard()->RequestGC(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14635 | } |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14636 | i::OS::Sleep(50); // Wait a bit before terminating. |
| 14637 | v8::V8::TerminateExecution(isolate_); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14638 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14639 | |
jkummerow@chromium.org | e297f59 | 2011-06-08 10:05:15 +0000 | [diff] [blame] | 14640 | private: |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14641 | v8::Isolate* isolate_; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14642 | }; |
| 14643 | |
| 14644 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14645 | void RunBeforeGC(v8::GCType type, v8::GCCallbackFlags flags) { |
| 14646 | if (regexp_interruption_data.loop_count != 2) return; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14647 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14648 | v8::Local<v8::String> string = v8::Local<v8::String>::New( |
| 14649 | CcTest::isolate(), regexp_interruption_data.string); |
| 14650 | string->MakeExternal(regexp_interruption_data.string_resource); |
| 14651 | } |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14652 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14653 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14654 | // Test that RegExp execution can be interrupted. Specifically, we test |
| 14655 | // * interrupting with GC |
| 14656 | // * turn the subject string from one-byte internal to two-byte external string |
| 14657 | // * force termination |
| 14658 | TEST(RegExpInterruption) { |
| 14659 | v8::HandleScope scope(CcTest::isolate()); |
| 14660 | LocalContext env; |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14661 | |
mstarzinger@chromium.org | a2e1a40 | 2013-10-15 08:25:05 +0000 | [diff] [blame] | 14662 | RegExpInterruptionThread timeout_thread(CcTest::isolate()); |
| 14663 | |
| 14664 | v8::V8::AddGCPrologueCallback(RunBeforeGC); |
| 14665 | static const char* ascii_content = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
| 14666 | i::uc16* uc16_content = AsciiToTwoByteString(ascii_content); |
| 14667 | v8::Local<v8::String> string = v8_str(ascii_content); |
| 14668 | |
| 14669 | CcTest::global()->Set(v8_str("a"), string); |
| 14670 | regexp_interruption_data.string.Reset(CcTest::isolate(), string); |
| 14671 | regexp_interruption_data.string_resource = new UC16VectorResource( |
| 14672 | i::Vector<const i::uc16>(uc16_content, i::StrLength(ascii_content))); |
| 14673 | |
| 14674 | v8::TryCatch try_catch; |
| 14675 | timeout_thread.Start(); |
| 14676 | |
| 14677 | CompileRun("/((a*)*)*b/.exec(a)"); |
| 14678 | CHECK(try_catch.HasTerminated()); |
| 14679 | |
| 14680 | timeout_thread.Join(); |
| 14681 | |
| 14682 | delete regexp_interruption_data.string_resource; |
| 14683 | regexp_interruption_data.string.Dispose(); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14684 | } |
| 14685 | |
mstarzinger@chromium.org | b4968be | 2013-10-16 09:00:56 +0000 | [diff] [blame^] | 14686 | #endif // V8_INTERPRETED_REGEXP |
| 14687 | |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14688 | |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 14689 | // Test that we cannot set a property on the global object if there |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14690 | // is a read-only property in the prototype chain. |
| 14691 | TEST(ReadOnlyPropertyInGlobalProto) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 14692 | i::FLAG_es5_readonly = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14693 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14694 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 14695 | LocalContext context(0, templ); |
| 14696 | v8::Handle<v8::Object> global = context->Global(); |
| 14697 | v8::Handle<v8::Object> global_proto = |
| 14698 | v8::Handle<v8::Object>::Cast(global->Get(v8_str("__proto__"))); |
| 14699 | global_proto->Set(v8_str("x"), v8::Integer::New(0), v8::ReadOnly); |
| 14700 | global_proto->Set(v8_str("y"), v8::Integer::New(0), v8::ReadOnly); |
| 14701 | // Check without 'eval' or 'with'. |
| 14702 | v8::Handle<v8::Value> res = |
| 14703 | CompileRun("function f() { x = 42; return x; }; f()"); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 14704 | CHECK_EQ(v8::Integer::New(0), res); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14705 | // Check with 'eval'. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 14706 | res = CompileRun("function f() { eval('1'); y = 43; return y; }; f()"); |
| 14707 | CHECK_EQ(v8::Integer::New(0), res); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14708 | // Check with 'with'. |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 14709 | res = CompileRun("function f() { with (this) { y = 44 }; return y; }; f()"); |
| 14710 | CHECK_EQ(v8::Integer::New(0), res); |
ager@chromium.org | bb29dc9 | 2009-03-24 13:25:23 +0000 | [diff] [blame] | 14711 | } |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14712 | |
| 14713 | static int force_set_set_count = 0; |
| 14714 | static int force_set_get_count = 0; |
| 14715 | bool pass_on_get = false; |
| 14716 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14717 | static void ForceSetGetter(v8::Local<v8::String> name, |
| 14718 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14719 | force_set_get_count++; |
| 14720 | if (pass_on_get) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14721 | return; |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14722 | } |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14723 | info.GetReturnValue().Set(3); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14724 | } |
| 14725 | |
| 14726 | static void ForceSetSetter(v8::Local<v8::String> name, |
| 14727 | v8::Local<v8::Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14728 | const v8::PropertyCallbackInfo<void>& info) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14729 | force_set_set_count++; |
| 14730 | } |
| 14731 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14732 | static void ForceSetInterceptSetter( |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14733 | v8::Local<v8::String> name, |
| 14734 | v8::Local<v8::Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14735 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14736 | force_set_set_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14737 | info.GetReturnValue().SetUndefined(); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14738 | } |
| 14739 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14740 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14741 | TEST(ForceSet) { |
| 14742 | force_set_get_count = 0; |
| 14743 | force_set_set_count = 0; |
| 14744 | pass_on_get = false; |
| 14745 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14746 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14747 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 14748 | v8::Handle<v8::String> access_property = v8::String::New("a"); |
| 14749 | templ->SetAccessor(access_property, ForceSetGetter, ForceSetSetter); |
| 14750 | LocalContext context(NULL, templ); |
| 14751 | v8::Handle<v8::Object> global = context->Global(); |
| 14752 | |
| 14753 | // Ordinary properties |
| 14754 | v8::Handle<v8::String> simple_property = v8::String::New("p"); |
| 14755 | global->Set(simple_property, v8::Int32::New(4), v8::ReadOnly); |
| 14756 | CHECK_EQ(4, global->Get(simple_property)->Int32Value()); |
| 14757 | // This should fail because the property is read-only |
| 14758 | global->Set(simple_property, v8::Int32::New(5)); |
| 14759 | CHECK_EQ(4, global->Get(simple_property)->Int32Value()); |
| 14760 | // This should succeed even though the property is read-only |
| 14761 | global->ForceSet(simple_property, v8::Int32::New(6)); |
| 14762 | CHECK_EQ(6, global->Get(simple_property)->Int32Value()); |
| 14763 | |
| 14764 | // Accessors |
| 14765 | CHECK_EQ(0, force_set_set_count); |
| 14766 | CHECK_EQ(0, force_set_get_count); |
| 14767 | CHECK_EQ(3, global->Get(access_property)->Int32Value()); |
| 14768 | // CHECK_EQ the property shouldn't override it, just call the setter |
| 14769 | // which in this case does nothing. |
| 14770 | global->Set(access_property, v8::Int32::New(7)); |
| 14771 | CHECK_EQ(3, global->Get(access_property)->Int32Value()); |
| 14772 | CHECK_EQ(1, force_set_set_count); |
| 14773 | CHECK_EQ(2, force_set_get_count); |
| 14774 | // Forcing the property to be set should override the accessor without |
| 14775 | // calling it |
| 14776 | global->ForceSet(access_property, v8::Int32::New(8)); |
| 14777 | CHECK_EQ(8, global->Get(access_property)->Int32Value()); |
| 14778 | CHECK_EQ(1, force_set_set_count); |
| 14779 | CHECK_EQ(2, force_set_get_count); |
| 14780 | } |
| 14781 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 14782 | |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14783 | TEST(ForceSetWithInterceptor) { |
| 14784 | force_set_get_count = 0; |
| 14785 | force_set_set_count = 0; |
| 14786 | pass_on_get = false; |
| 14787 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14788 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 65dad4b | 2009-04-23 08:48:43 +0000 | [diff] [blame] | 14789 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 14790 | templ->SetNamedPropertyHandler(ForceSetGetter, ForceSetInterceptSetter); |
| 14791 | LocalContext context(NULL, templ); |
| 14792 | v8::Handle<v8::Object> global = context->Global(); |
| 14793 | |
| 14794 | v8::Handle<v8::String> some_property = v8::String::New("a"); |
| 14795 | CHECK_EQ(0, force_set_set_count); |
| 14796 | CHECK_EQ(0, force_set_get_count); |
| 14797 | CHECK_EQ(3, global->Get(some_property)->Int32Value()); |
| 14798 | // Setting the property shouldn't override it, just call the setter |
| 14799 | // which in this case does nothing. |
| 14800 | global->Set(some_property, v8::Int32::New(7)); |
| 14801 | CHECK_EQ(3, global->Get(some_property)->Int32Value()); |
| 14802 | CHECK_EQ(1, force_set_set_count); |
| 14803 | CHECK_EQ(2, force_set_get_count); |
| 14804 | // Getting the property when the interceptor returns an empty handle |
| 14805 | // should yield undefined, since the property isn't present on the |
| 14806 | // object itself yet. |
| 14807 | pass_on_get = true; |
| 14808 | CHECK(global->Get(some_property)->IsUndefined()); |
| 14809 | CHECK_EQ(1, force_set_set_count); |
| 14810 | CHECK_EQ(3, force_set_get_count); |
| 14811 | // Forcing the property to be set should cause the value to be |
| 14812 | // set locally without calling the interceptor. |
| 14813 | global->ForceSet(some_property, v8::Int32::New(8)); |
| 14814 | CHECK_EQ(8, global->Get(some_property)->Int32Value()); |
| 14815 | CHECK_EQ(1, force_set_set_count); |
| 14816 | CHECK_EQ(4, force_set_get_count); |
| 14817 | // Reenabling the interceptor should cause it to take precedence over |
| 14818 | // the property |
| 14819 | pass_on_get = false; |
| 14820 | CHECK_EQ(3, global->Get(some_property)->Int32Value()); |
| 14821 | CHECK_EQ(1, force_set_set_count); |
| 14822 | CHECK_EQ(5, force_set_get_count); |
| 14823 | // The interceptor should also work for other properties |
| 14824 | CHECK_EQ(3, global->Get(v8::String::New("b"))->Int32Value()); |
| 14825 | CHECK_EQ(1, force_set_set_count); |
| 14826 | CHECK_EQ(6, force_set_get_count); |
| 14827 | } |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14828 | |
| 14829 | |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14830 | THREADED_TEST(ForceDelete) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14831 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14832 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 14833 | LocalContext context(NULL, templ); |
| 14834 | v8::Handle<v8::Object> global = context->Global(); |
| 14835 | |
| 14836 | // Ordinary properties |
| 14837 | v8::Handle<v8::String> simple_property = v8::String::New("p"); |
| 14838 | global->Set(simple_property, v8::Int32::New(4), v8::DontDelete); |
| 14839 | CHECK_EQ(4, global->Get(simple_property)->Int32Value()); |
| 14840 | // This should fail because the property is dont-delete. |
| 14841 | CHECK(!global->Delete(simple_property)); |
| 14842 | CHECK_EQ(4, global->Get(simple_property)->Int32Value()); |
| 14843 | // This should succeed even though the property is dont-delete. |
| 14844 | CHECK(global->ForceDelete(simple_property)); |
| 14845 | CHECK(global->Get(simple_property)->IsUndefined()); |
| 14846 | } |
| 14847 | |
| 14848 | |
| 14849 | static int force_delete_interceptor_count = 0; |
| 14850 | static bool pass_on_delete = false; |
| 14851 | |
| 14852 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14853 | static void ForceDeleteDeleter( |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14854 | v8::Local<v8::String> name, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14855 | const v8::PropertyCallbackInfo<v8::Boolean>& info) { |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14856 | force_delete_interceptor_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14857 | if (pass_on_delete) return; |
| 14858 | info.GetReturnValue().Set(true); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14859 | } |
| 14860 | |
| 14861 | |
| 14862 | THREADED_TEST(ForceDeleteWithInterceptor) { |
| 14863 | force_delete_interceptor_count = 0; |
| 14864 | pass_on_delete = false; |
| 14865 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14866 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | e2902be | 2009-06-08 12:21:35 +0000 | [diff] [blame] | 14867 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 14868 | templ->SetNamedPropertyHandler(0, 0, 0, ForceDeleteDeleter); |
| 14869 | LocalContext context(NULL, templ); |
| 14870 | v8::Handle<v8::Object> global = context->Global(); |
| 14871 | |
| 14872 | v8::Handle<v8::String> some_property = v8::String::New("a"); |
| 14873 | global->Set(some_property, v8::Integer::New(42), v8::DontDelete); |
| 14874 | |
| 14875 | // Deleting a property should get intercepted and nothing should |
| 14876 | // happen. |
| 14877 | CHECK_EQ(0, force_delete_interceptor_count); |
| 14878 | CHECK(global->Delete(some_property)); |
| 14879 | CHECK_EQ(1, force_delete_interceptor_count); |
| 14880 | CHECK_EQ(42, global->Get(some_property)->Int32Value()); |
| 14881 | // Deleting the property when the interceptor returns an empty |
| 14882 | // handle should not delete the property since it is DontDelete. |
| 14883 | pass_on_delete = true; |
| 14884 | CHECK(!global->Delete(some_property)); |
| 14885 | CHECK_EQ(2, force_delete_interceptor_count); |
| 14886 | CHECK_EQ(42, global->Get(some_property)->Int32Value()); |
| 14887 | // Forcing the property to be deleted should delete the value |
| 14888 | // without calling the interceptor. |
| 14889 | CHECK(global->ForceDelete(some_property)); |
| 14890 | CHECK(global->Get(some_property)->IsUndefined()); |
| 14891 | CHECK_EQ(2, force_delete_interceptor_count); |
| 14892 | } |
| 14893 | |
| 14894 | |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 14895 | // Make sure that forcing a delete invalidates any IC stubs, so we |
| 14896 | // don't read the hole value. |
| 14897 | THREADED_TEST(ForceDeleteIC) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 14898 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 14899 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 14900 | // Create a DontDelete variable on the global object. |
| 14901 | CompileRun("this.__proto__ = { foo: 'horse' };" |
| 14902 | "var foo = 'fish';" |
| 14903 | "function f() { return foo.length; }"); |
| 14904 | // Initialize the IC for foo in f. |
| 14905 | CompileRun("for (var i = 0; i < 4; i++) f();"); |
| 14906 | // Make sure the value of foo is correct before the deletion. |
| 14907 | CHECK_EQ(4, CompileRun("f()")->Int32Value()); |
| 14908 | // Force the deletion of foo. |
| 14909 | CHECK(context->Global()->ForceDelete(v8_str("foo"))); |
| 14910 | // Make sure the value for foo is read from the prototype, and that |
| 14911 | // we don't get in trouble with reading the deleted cell value |
| 14912 | // sentinel. |
| 14913 | CHECK_EQ(5, CompileRun("f()")->Int32Value()); |
| 14914 | } |
| 14915 | |
| 14916 | |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 14917 | TEST(InlinedFunctionAcrossContexts) { |
| 14918 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14919 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14920 | v8::HandleScope outer_scope(isolate); |
| 14921 | v8::Local<v8::Context> ctx1 = v8::Context::New(isolate); |
| 14922 | v8::Local<v8::Context> ctx2 = v8::Context::New(isolate); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 14923 | ctx1->Enter(); |
| 14924 | |
| 14925 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14926 | v8::HandleScope inner_scope(CcTest::isolate()); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 14927 | CompileRun("var G = 42; function foo() { return G; }"); |
| 14928 | v8::Local<v8::Value> foo = ctx1->Global()->Get(v8_str("foo")); |
| 14929 | ctx2->Enter(); |
| 14930 | ctx2->Global()->Set(v8_str("o"), foo); |
| 14931 | v8::Local<v8::Value> res = CompileRun( |
| 14932 | "function f() { return o(); }" |
| 14933 | "for (var i = 0; i < 10; ++i) f();" |
| 14934 | "%OptimizeFunctionOnNextCall(f);" |
| 14935 | "f();"); |
| 14936 | CHECK_EQ(42, res->Int32Value()); |
| 14937 | ctx2->Exit(); |
| 14938 | v8::Handle<v8::String> G_property = v8::String::New("G"); |
| 14939 | CHECK(ctx1->Global()->ForceDelete(G_property)); |
| 14940 | ctx2->Enter(); |
| 14941 | ExpectString( |
| 14942 | "(function() {" |
| 14943 | " try {" |
| 14944 | " return f();" |
| 14945 | " } catch(e) {" |
| 14946 | " return e.toString();" |
| 14947 | " }" |
| 14948 | " })()", |
| 14949 | "ReferenceError: G is not defined"); |
| 14950 | ctx2->Exit(); |
| 14951 | ctx1->Exit(); |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 14952 | } |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 14953 | } |
| 14954 | |
| 14955 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14956 | static v8::Local<Context> calling_context0; |
| 14957 | static v8::Local<Context> calling_context1; |
| 14958 | static v8::Local<Context> calling_context2; |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14959 | |
| 14960 | |
| 14961 | // Check that the call to the callback is initiated in |
| 14962 | // calling_context2, the directly calling context is calling_context1 |
| 14963 | // and the callback itself is in calling_context0. |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14964 | static void GetCallingContextCallback( |
| 14965 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14966 | ApiTestFuzzer::Fuzz(); |
mvstanton@chromium.org | 40ce96b | 2013-04-09 09:52:22 +0000 | [diff] [blame] | 14967 | CHECK(args.GetIsolate()->GetCurrentContext() == calling_context0); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14968 | CHECK(args.GetIsolate()->GetCallingContext() == calling_context1); |
| 14969 | CHECK(args.GetIsolate()->GetEnteredContext() == calling_context2); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14970 | args.GetReturnValue().Set(42); |
| 14971 | } |
| 14972 | |
| 14973 | |
| 14974 | THREADED_TEST(GetCurrentContextWhenNotInContext) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14975 | i::Isolate* isolate = CcTest::i_isolate(); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 14976 | CHECK(isolate != NULL); |
| 14977 | CHECK(isolate->context() == NULL); |
| 14978 | v8::Isolate* v8_isolate = reinterpret_cast<v8::Isolate*>(isolate); |
| 14979 | v8::HandleScope scope(v8_isolate); |
| 14980 | // The following should not crash, but return an empty handle. |
| 14981 | v8::Local<v8::Context> current = v8_isolate->GetCurrentContext(); |
| 14982 | CHECK(current.IsEmpty()); |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14983 | } |
| 14984 | |
| 14985 | |
| 14986 | THREADED_TEST(GetCallingContext) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 14987 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14988 | v8::HandleScope scope(isolate); |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14989 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 14990 | Local<Context> calling_context0(Context::New(isolate)); |
| 14991 | Local<Context> calling_context1(Context::New(isolate)); |
| 14992 | Local<Context> calling_context2(Context::New(isolate)); |
| 14993 | ::calling_context0 = calling_context0; |
| 14994 | ::calling_context1 = calling_context1; |
| 14995 | ::calling_context2 = calling_context2; |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 14996 | |
| 14997 | // Allow cross-domain access. |
| 14998 | Local<String> token = v8_str("<security token>"); |
| 14999 | calling_context0->SetSecurityToken(token); |
| 15000 | calling_context1->SetSecurityToken(token); |
| 15001 | calling_context2->SetSecurityToken(token); |
| 15002 | |
| 15003 | // Create an object with a C++ callback in context0. |
| 15004 | calling_context0->Enter(); |
| 15005 | Local<v8::FunctionTemplate> callback_templ = |
| 15006 | v8::FunctionTemplate::New(GetCallingContextCallback); |
| 15007 | calling_context0->Global()->Set(v8_str("callback"), |
| 15008 | callback_templ->GetFunction()); |
| 15009 | calling_context0->Exit(); |
| 15010 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 15011 | // Expose context0 in context1 and set up a function that calls the |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 15012 | // callback function. |
| 15013 | calling_context1->Enter(); |
| 15014 | calling_context1->Global()->Set(v8_str("context0"), |
| 15015 | calling_context0->Global()); |
| 15016 | CompileRun("function f() { context0.callback() }"); |
| 15017 | calling_context1->Exit(); |
| 15018 | |
| 15019 | // Expose context1 in context2 and call the callback function in |
| 15020 | // context0 indirectly through f in context1. |
| 15021 | calling_context2->Enter(); |
| 15022 | calling_context2->Global()->Set(v8_str("context1"), |
| 15023 | calling_context1->Global()); |
| 15024 | CompileRun("context1.f()"); |
| 15025 | calling_context2->Exit(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 15026 | ::calling_context0.Clear(); |
| 15027 | ::calling_context1.Clear(); |
| 15028 | ::calling_context2.Clear(); |
ager@chromium.org | 1bf0cd0 | 2009-05-20 11:34:19 +0000 | [diff] [blame] | 15029 | } |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15030 | |
| 15031 | |
| 15032 | // Check that a variable declaration with no explicit initialization |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 15033 | // value does shadow an existing property in the prototype chain. |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15034 | THREADED_TEST(InitGlobalVarInProtoChain) { |
mstarzinger@chromium.org | 88d326b | 2012-04-23 12:57:22 +0000 | [diff] [blame] | 15035 | i::FLAG_es52_globals = true; |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15036 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15037 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15038 | // Introduce a variable in the prototype chain. |
| 15039 | CompileRun("__proto__.x = 42"); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 15040 | v8::Handle<v8::Value> result = CompileRun("var x = 43; x"); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15041 | CHECK(!result->IsUndefined()); |
erik.corry@gmail.com | ed49e96 | 2012-04-17 11:57:53 +0000 | [diff] [blame] | 15042 | CHECK_EQ(43, result->Int32Value()); |
kasperl@chromium.org | 2abc450 | 2009-07-02 07:00:29 +0000 | [diff] [blame] | 15043 | } |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 15044 | |
| 15045 | |
| 15046 | // Regression test for issue 398. |
| 15047 | // If a function is added to an object, creating a constant function |
| 15048 | // field, and the result is cloned, replacing the constant function on the |
| 15049 | // original should not affect the clone. |
| 15050 | // See http://code.google.com/p/v8/issues/detail?id=398 |
| 15051 | THREADED_TEST(ReplaceConstantFunction) { |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 15052 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15053 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | 68ac009 | 2009-07-09 06:00:35 +0000 | [diff] [blame] | 15054 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 15055 | v8::Handle<v8::FunctionTemplate> func_templ = v8::FunctionTemplate::New(); |
| 15056 | v8::Handle<v8::String> foo_string = v8::String::New("foo"); |
| 15057 | obj->Set(foo_string, func_templ->GetFunction()); |
| 15058 | v8::Handle<v8::Object> obj_clone = obj->Clone(); |
| 15059 | obj_clone->Set(foo_string, v8::String::New("Hello")); |
| 15060 | CHECK(!obj->Get(foo_string)->IsUndefined()); |
| 15061 | } |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 15062 | |
| 15063 | |
| 15064 | // Regression test for http://crbug.com/16276. |
| 15065 | THREADED_TEST(Regress16276) { |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 15066 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15067 | v8::HandleScope scope(context->GetIsolate()); |
kasperl@chromium.org | e959c18 | 2009-07-27 08:59:04 +0000 | [diff] [blame] | 15068 | // Force the IC in f to be a dictionary load IC. |
| 15069 | CompileRun("function f(obj) { return obj.x; }\n" |
| 15070 | "var obj = { x: { foo: 42 }, y: 87 };\n" |
| 15071 | "var x = obj.x;\n" |
| 15072 | "delete obj.y;\n" |
| 15073 | "for (var i = 0; i < 5; i++) f(obj);"); |
| 15074 | // Detach the global object to make 'this' refer directly to the |
| 15075 | // global object (not the proxy), and make sure that the dictionary |
| 15076 | // load IC doesn't mess up loading directly from the global object. |
| 15077 | context->DetachGlobal(); |
| 15078 | CHECK_EQ(42, CompileRun("f(this).foo")->Int32Value()); |
| 15079 | } |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15080 | |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15081 | static void CheckElementValue(i::Isolate* isolate, |
| 15082 | int expected, |
| 15083 | i::Handle<i::Object> obj, |
| 15084 | int offset) { |
| 15085 | i::Object* element = obj->GetElement(isolate, offset)->ToObjectChecked(); |
| 15086 | CHECK_EQ(expected, i::Smi::cast(element)->value()); |
| 15087 | } |
| 15088 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15089 | |
| 15090 | THREADED_TEST(PixelArray) { |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15091 | LocalContext context; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15092 | i::Isolate* isolate = CcTest::i_isolate(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15093 | i::Factory* factory = isolate->factory(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15094 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 15095 | const int kElementCount = 260; |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15096 | uint8_t* pixel_data = reinterpret_cast<uint8_t*>(malloc(kElementCount)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15097 | i::Handle<i::ExternalPixelArray> pixels = |
| 15098 | i::Handle<i::ExternalPixelArray>::cast( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 15099 | factory->NewExternalArray(kElementCount, |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 15100 | v8::kExternalPixelArray, |
| 15101 | pixel_data)); |
| 15102 | // Force GC to trigger verification. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15103 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15104 | for (int i = 0; i < kElementCount; i++) { |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 15105 | pixels->set(i, i % 256); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15106 | } |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 15107 | // Force GC to trigger verification. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15108 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15109 | for (int i = 0; i < kElementCount; i++) { |
kmillikin@chromium.org | 7c2628c | 2011-08-10 11:27:35 +0000 | [diff] [blame] | 15110 | CHECK_EQ(i % 256, pixels->get_scalar(i)); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 15111 | CHECK_EQ(i % 256, pixel_data[i]); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15112 | } |
| 15113 | |
| 15114 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 15115 | i::Handle<i::JSObject> jsobj = v8::Utils::OpenHandle(*obj); |
| 15116 | // Set the elements to be the pixels. |
| 15117 | // jsobj->set_elements(*pixels); |
| 15118 | obj->SetIndexedPropertiesToPixelData(pixel_data, kElementCount); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15119 | CheckElementValue(isolate, 1, jsobj, 1); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15120 | obj->Set(v8_str("field"), v8::Int32::New(1503)); |
| 15121 | context->Global()->Set(v8_str("pixels"), obj); |
| 15122 | v8::Handle<v8::Value> result = CompileRun("pixels.field"); |
| 15123 | CHECK_EQ(1503, result->Int32Value()); |
| 15124 | result = CompileRun("pixels[1]"); |
| 15125 | CHECK_EQ(1, result->Int32Value()); |
sgjesse@chromium.org | 9eceac1 | 2009-09-30 07:18:41 +0000 | [diff] [blame] | 15126 | |
| 15127 | result = CompileRun("var sum = 0;" |
| 15128 | "for (var i = 0; i < 8; i++) {" |
| 15129 | " sum += pixels[i] = pixels[i] = -i;" |
| 15130 | "}" |
| 15131 | "sum;"); |
| 15132 | CHECK_EQ(-28, result->Int32Value()); |
| 15133 | |
| 15134 | result = CompileRun("var sum = 0;" |
| 15135 | "for (var i = 0; i < 8; i++) {" |
| 15136 | " sum += pixels[i] = pixels[i] = 0;" |
| 15137 | "}" |
| 15138 | "sum;"); |
| 15139 | CHECK_EQ(0, result->Int32Value()); |
| 15140 | |
| 15141 | result = CompileRun("var sum = 0;" |
| 15142 | "for (var i = 0; i < 8; i++) {" |
| 15143 | " sum += pixels[i] = pixels[i] = 255;" |
| 15144 | "}" |
| 15145 | "sum;"); |
| 15146 | CHECK_EQ(8 * 255, result->Int32Value()); |
| 15147 | |
| 15148 | result = CompileRun("var sum = 0;" |
| 15149 | "for (var i = 0; i < 8; i++) {" |
| 15150 | " sum += pixels[i] = pixels[i] = 256 + i;" |
| 15151 | "}" |
| 15152 | "sum;"); |
| 15153 | CHECK_EQ(2076, result->Int32Value()); |
| 15154 | |
| 15155 | result = CompileRun("var sum = 0;" |
| 15156 | "for (var i = 0; i < 8; i++) {" |
| 15157 | " sum += pixels[i] = pixels[i] = i;" |
| 15158 | "}" |
| 15159 | "sum;"); |
| 15160 | CHECK_EQ(28, result->Int32Value()); |
| 15161 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15162 | result = CompileRun("var sum = 0;" |
| 15163 | "for (var i = 0; i < 8; i++) {" |
| 15164 | " sum += pixels[i];" |
| 15165 | "}" |
| 15166 | "sum;"); |
| 15167 | CHECK_EQ(28, result->Int32Value()); |
| 15168 | |
ulan@chromium.org | 09d7ab5 | 2013-02-25 15:50:35 +0000 | [diff] [blame] | 15169 | i::Handle<i::Smi> value(i::Smi::FromInt(2), |
| 15170 | reinterpret_cast<i::Isolate*>(context->GetIsolate())); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 15171 | i::Handle<i::Object> no_failure; |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 15172 | no_failure = |
| 15173 | i::JSObject::SetElement(jsobj, 1, value, NONE, i::kNonStrictMode); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 15174 | ASSERT(!no_failure.is_null()); |
| 15175 | i::USE(no_failure); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15176 | CheckElementValue(isolate, 2, jsobj, 1); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15177 | *value.location() = i::Smi::FromInt(256); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 15178 | no_failure = |
| 15179 | i::JSObject::SetElement(jsobj, 1, value, NONE, i::kNonStrictMode); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 15180 | ASSERT(!no_failure.is_null()); |
| 15181 | i::USE(no_failure); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15182 | CheckElementValue(isolate, 255, jsobj, 1); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15183 | *value.location() = i::Smi::FromInt(-1); |
yangguo@chromium.org | ab30bb8 | 2012-02-24 14:41:46 +0000 | [diff] [blame] | 15184 | no_failure = |
| 15185 | i::JSObject::SetElement(jsobj, 1, value, NONE, i::kNonStrictMode); |
jkummerow@chromium.org | 486075a | 2011-09-07 12:44:28 +0000 | [diff] [blame] | 15186 | ASSERT(!no_failure.is_null()); |
| 15187 | i::USE(no_failure); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15188 | CheckElementValue(isolate, 0, jsobj, 1); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15189 | |
| 15190 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15191 | " pixels[i] = (i * 65) - 109;" |
| 15192 | "}" |
| 15193 | "pixels[1] + pixels[6];"); |
| 15194 | CHECK_EQ(255, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15195 | CheckElementValue(isolate, 0, jsobj, 0); |
| 15196 | CheckElementValue(isolate, 0, jsobj, 1); |
| 15197 | CheckElementValue(isolate, 21, jsobj, 2); |
| 15198 | CheckElementValue(isolate, 86, jsobj, 3); |
| 15199 | CheckElementValue(isolate, 151, jsobj, 4); |
| 15200 | CheckElementValue(isolate, 216, jsobj, 5); |
| 15201 | CheckElementValue(isolate, 255, jsobj, 6); |
| 15202 | CheckElementValue(isolate, 255, jsobj, 7); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15203 | result = CompileRun("var sum = 0;" |
| 15204 | "for (var i = 0; i < 8; i++) {" |
| 15205 | " sum += pixels[i];" |
| 15206 | "}" |
| 15207 | "sum;"); |
| 15208 | CHECK_EQ(984, result->Int32Value()); |
| 15209 | |
| 15210 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15211 | " pixels[i] = (i * 1.1);" |
| 15212 | "}" |
| 15213 | "pixels[1] + pixels[6];"); |
| 15214 | CHECK_EQ(8, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15215 | CheckElementValue(isolate, 0, jsobj, 0); |
| 15216 | CheckElementValue(isolate, 1, jsobj, 1); |
| 15217 | CheckElementValue(isolate, 2, jsobj, 2); |
| 15218 | CheckElementValue(isolate, 3, jsobj, 3); |
| 15219 | CheckElementValue(isolate, 4, jsobj, 4); |
| 15220 | CheckElementValue(isolate, 6, jsobj, 5); |
| 15221 | CheckElementValue(isolate, 7, jsobj, 6); |
| 15222 | CheckElementValue(isolate, 8, jsobj, 7); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15223 | |
| 15224 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15225 | " pixels[7] = undefined;" |
| 15226 | "}" |
| 15227 | "pixels[7];"); |
| 15228 | CHECK_EQ(0, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15229 | CheckElementValue(isolate, 0, jsobj, 7); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15230 | |
| 15231 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15232 | " pixels[6] = '2.3';" |
| 15233 | "}" |
| 15234 | "pixels[6];"); |
| 15235 | CHECK_EQ(2, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15236 | CheckElementValue(isolate, 2, jsobj, 6); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15237 | |
| 15238 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15239 | " pixels[5] = NaN;" |
| 15240 | "}" |
| 15241 | "pixels[5];"); |
| 15242 | CHECK_EQ(0, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15243 | CheckElementValue(isolate, 0, jsobj, 5); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15244 | |
| 15245 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15246 | " pixels[8] = Infinity;" |
| 15247 | "}" |
| 15248 | "pixels[8];"); |
| 15249 | CHECK_EQ(255, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15250 | CheckElementValue(isolate, 255, jsobj, 8); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15251 | |
| 15252 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15253 | " pixels[9] = -Infinity;" |
| 15254 | "}" |
| 15255 | "pixels[9];"); |
| 15256 | CHECK_EQ(0, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15257 | CheckElementValue(isolate, 0, jsobj, 9); |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15258 | |
| 15259 | result = CompileRun("pixels[3] = 33;" |
| 15260 | "delete pixels[3];" |
| 15261 | "pixels[3];"); |
| 15262 | CHECK_EQ(33, result->Int32Value()); |
| 15263 | |
| 15264 | result = CompileRun("pixels[0] = 10; pixels[1] = 11;" |
| 15265 | "pixels[2] = 12; pixels[3] = 13;" |
| 15266 | "pixels.__defineGetter__('2'," |
| 15267 | "function() { return 120; });" |
| 15268 | "pixels[2];"); |
| 15269 | CHECK_EQ(12, result->Int32Value()); |
| 15270 | |
| 15271 | result = CompileRun("var js_array = new Array(40);" |
| 15272 | "js_array[0] = 77;" |
| 15273 | "js_array;"); |
| 15274 | CHECK_EQ(77, v8::Object::Cast(*result)->Get(v8_str("0"))->Int32Value()); |
| 15275 | |
| 15276 | result = CompileRun("pixels[1] = 23;" |
| 15277 | "pixels.__proto__ = [];" |
| 15278 | "js_array.__proto__ = pixels;" |
| 15279 | "js_array.concat(pixels);"); |
| 15280 | CHECK_EQ(77, v8::Object::Cast(*result)->Get(v8_str("0"))->Int32Value()); |
| 15281 | CHECK_EQ(23, v8::Object::Cast(*result)->Get(v8_str("1"))->Int32Value()); |
| 15282 | |
sgjesse@chromium.org | 9eceac1 | 2009-09-30 07:18:41 +0000 | [diff] [blame] | 15283 | result = CompileRun("pixels[1] = 23;"); |
| 15284 | CHECK_EQ(23, result->Int32Value()); |
| 15285 | |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 15286 | // Test for index greater than 255. Regression test for: |
| 15287 | // http://code.google.com/p/chromium/issues/detail?id=26337. |
| 15288 | result = CompileRun("pixels[256] = 255;"); |
| 15289 | CHECK_EQ(255, result->Int32Value()); |
| 15290 | result = CompileRun("var i = 0;" |
| 15291 | "for (var j = 0; j < 8; j++) { i = pixels[256]; }" |
| 15292 | "i"); |
| 15293 | CHECK_EQ(255, result->Int32Value()); |
| 15294 | |
ricow@chromium.org | 83aa549 | 2011-02-07 12:42:56 +0000 | [diff] [blame] | 15295 | // Make sure that pixel array ICs recognize when a non-pixel array |
| 15296 | // is passed to it. |
| 15297 | result = CompileRun("function pa_load(p) {" |
| 15298 | " var sum = 0;" |
| 15299 | " for (var j = 0; j < 256; j++) { sum += p[j]; }" |
| 15300 | " return sum;" |
| 15301 | "}" |
| 15302 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
| 15303 | "for (var i = 0; i < 10; ++i) { pa_load(pixels); }" |
| 15304 | "just_ints = new Object();" |
| 15305 | "for (var i = 0; i < 256; ++i) { just_ints[i] = i; }" |
| 15306 | "for (var i = 0; i < 10; ++i) {" |
| 15307 | " result = pa_load(just_ints);" |
| 15308 | "}" |
| 15309 | "result"); |
| 15310 | CHECK_EQ(32640, result->Int32Value()); |
| 15311 | |
| 15312 | // Make sure that pixel array ICs recognize out-of-bound accesses. |
| 15313 | result = CompileRun("function pa_load(p, start) {" |
| 15314 | " var sum = 0;" |
| 15315 | " for (var j = start; j < 256; j++) { sum += p[j]; }" |
| 15316 | " return sum;" |
| 15317 | "}" |
| 15318 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
| 15319 | "for (var i = 0; i < 10; ++i) { pa_load(pixels,0); }" |
| 15320 | "for (var i = 0; i < 10; ++i) {" |
| 15321 | " result = pa_load(pixels,-10);" |
| 15322 | "}" |
| 15323 | "result"); |
| 15324 | CHECK_EQ(0, result->Int32Value()); |
| 15325 | |
| 15326 | // Make sure that generic ICs properly handles a pixel array. |
| 15327 | result = CompileRun("function pa_load(p) {" |
| 15328 | " var sum = 0;" |
| 15329 | " for (var j = 0; j < 256; j++) { sum += p[j]; }" |
| 15330 | " return sum;" |
| 15331 | "}" |
| 15332 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
| 15333 | "just_ints = new Object();" |
| 15334 | "for (var i = 0; i < 256; ++i) { just_ints[i] = i; }" |
| 15335 | "for (var i = 0; i < 10; ++i) { pa_load(just_ints); }" |
| 15336 | "for (var i = 0; i < 10; ++i) {" |
| 15337 | " result = pa_load(pixels);" |
| 15338 | "}" |
| 15339 | "result"); |
| 15340 | CHECK_EQ(32640, result->Int32Value()); |
| 15341 | |
| 15342 | // Make sure that generic load ICs recognize out-of-bound accesses in |
| 15343 | // pixel arrays. |
| 15344 | result = CompileRun("function pa_load(p, start) {" |
| 15345 | " var sum = 0;" |
| 15346 | " for (var j = start; j < 256; j++) { sum += p[j]; }" |
| 15347 | " return sum;" |
| 15348 | "}" |
| 15349 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
| 15350 | "just_ints = new Object();" |
| 15351 | "for (var i = 0; i < 256; ++i) { just_ints[i] = i; }" |
| 15352 | "for (var i = 0; i < 10; ++i) { pa_load(just_ints,0); }" |
| 15353 | "for (var i = 0; i < 10; ++i) { pa_load(pixels,0); }" |
| 15354 | "for (var i = 0; i < 10; ++i) {" |
| 15355 | " result = pa_load(pixels,-10);" |
| 15356 | "}" |
| 15357 | "result"); |
| 15358 | CHECK_EQ(0, result->Int32Value()); |
| 15359 | |
| 15360 | // Make sure that generic ICs properly handles other types than pixel |
| 15361 | // arrays (that the inlined fast pixel array test leaves the right information |
| 15362 | // in the right registers). |
| 15363 | result = CompileRun("function pa_load(p) {" |
| 15364 | " var sum = 0;" |
| 15365 | " for (var j = 0; j < 256; j++) { sum += p[j]; }" |
| 15366 | " return sum;" |
| 15367 | "}" |
| 15368 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
| 15369 | "just_ints = new Object();" |
| 15370 | "for (var i = 0; i < 256; ++i) { just_ints[i] = i; }" |
| 15371 | "for (var i = 0; i < 10; ++i) { pa_load(just_ints); }" |
| 15372 | "for (var i = 0; i < 10; ++i) { pa_load(pixels); }" |
| 15373 | "sparse_array = new Object();" |
| 15374 | "for (var i = 0; i < 256; ++i) { sparse_array[i] = i; }" |
| 15375 | "sparse_array[1000000] = 3;" |
| 15376 | "for (var i = 0; i < 10; ++i) {" |
| 15377 | " result = pa_load(sparse_array);" |
| 15378 | "}" |
| 15379 | "result"); |
| 15380 | CHECK_EQ(32640, result->Int32Value()); |
| 15381 | |
kmillikin@chromium.org | 49edbdf | 2011-02-16 12:32:18 +0000 | [diff] [blame] | 15382 | // Make sure that pixel array store ICs clamp values correctly. |
| 15383 | result = CompileRun("function pa_store(p) {" |
| 15384 | " for (var j = 0; j < 256; j++) { p[j] = j * 2; }" |
| 15385 | "}" |
| 15386 | "pa_store(pixels);" |
| 15387 | "var sum = 0;" |
| 15388 | "for (var j = 0; j < 256; j++) { sum += pixels[j]; }" |
| 15389 | "sum"); |
| 15390 | CHECK_EQ(48896, result->Int32Value()); |
| 15391 | |
| 15392 | // Make sure that pixel array stores correctly handle accesses outside |
| 15393 | // of the pixel array.. |
| 15394 | result = CompileRun("function pa_store(p,start) {" |
| 15395 | " for (var j = 0; j < 256; j++) {" |
| 15396 | " p[j+start] = j * 2;" |
| 15397 | " }" |
| 15398 | "}" |
| 15399 | "pa_store(pixels,0);" |
| 15400 | "pa_store(pixels,-128);" |
| 15401 | "var sum = 0;" |
| 15402 | "for (var j = 0; j < 256; j++) { sum += pixels[j]; }" |
| 15403 | "sum"); |
| 15404 | CHECK_EQ(65280, result->Int32Value()); |
| 15405 | |
| 15406 | // Make sure that the generic store stub correctly handle accesses outside |
| 15407 | // of the pixel array.. |
| 15408 | result = CompileRun("function pa_store(p,start) {" |
| 15409 | " for (var j = 0; j < 256; j++) {" |
| 15410 | " p[j+start] = j * 2;" |
| 15411 | " }" |
| 15412 | "}" |
| 15413 | "pa_store(pixels,0);" |
| 15414 | "just_ints = new Object();" |
| 15415 | "for (var i = 0; i < 256; ++i) { just_ints[i] = i; }" |
| 15416 | "pa_store(just_ints, 0);" |
| 15417 | "pa_store(pixels,-128);" |
| 15418 | "var sum = 0;" |
| 15419 | "for (var j = 0; j < 256; j++) { sum += pixels[j]; }" |
| 15420 | "sum"); |
| 15421 | CHECK_EQ(65280, result->Int32Value()); |
| 15422 | |
| 15423 | // Make sure that the generic keyed store stub clamps pixel array values |
| 15424 | // correctly. |
| 15425 | result = CompileRun("function pa_store(p) {" |
| 15426 | " for (var j = 0; j < 256; j++) { p[j] = j * 2; }" |
| 15427 | "}" |
| 15428 | "pa_store(pixels);" |
| 15429 | "just_ints = new Object();" |
| 15430 | "pa_store(just_ints);" |
| 15431 | "pa_store(pixels);" |
| 15432 | "var sum = 0;" |
| 15433 | "for (var j = 0; j < 256; j++) { sum += pixels[j]; }" |
| 15434 | "sum"); |
| 15435 | CHECK_EQ(48896, result->Int32Value()); |
| 15436 | |
| 15437 | // Make sure that pixel array loads are optimized by crankshaft. |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 15438 | result = CompileRun("function pa_load(p) {" |
| 15439 | " var sum = 0;" |
| 15440 | " for (var i=0; i<256; ++i) {" |
| 15441 | " sum += p[i];" |
| 15442 | " }" |
| 15443 | " return sum; " |
| 15444 | "}" |
| 15445 | "for (var i = 0; i < 256; ++i) { pixels[i] = i; }" |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15446 | "for (var i = 0; i < 5000; ++i) {" |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 15447 | " result = pa_load(pixels);" |
| 15448 | "}" |
| 15449 | "result"); |
| 15450 | CHECK_EQ(32640, result->Int32Value()); |
| 15451 | |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15452 | // Make sure that pixel array stores are optimized by crankshaft. |
| 15453 | result = CompileRun("function pa_init(p) {" |
| 15454 | "for (var i = 0; i < 256; ++i) { p[i] = i; }" |
| 15455 | "}" |
| 15456 | "function pa_load(p) {" |
| 15457 | " var sum = 0;" |
| 15458 | " for (var i=0; i<256; ++i) {" |
| 15459 | " sum += p[i];" |
| 15460 | " }" |
| 15461 | " return sum; " |
| 15462 | "}" |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15463 | "for (var i = 0; i < 5000; ++i) {" |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15464 | " pa_init(pixels);" |
| 15465 | "}" |
| 15466 | "result = pa_load(pixels);" |
| 15467 | "result"); |
| 15468 | CHECK_EQ(32640, result->Int32Value()); |
| 15469 | |
sgjesse@chromium.org | 0b6db59 | 2009-07-30 14:48:31 +0000 | [diff] [blame] | 15470 | free(pixel_data); |
| 15471 | } |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 15472 | |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 15473 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15474 | THREADED_TEST(PixelArrayInfo) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15475 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15476 | v8::HandleScope scope(context->GetIsolate()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15477 | for (int size = 0; size < 100; size += 10) { |
| 15478 | uint8_t* pixel_data = reinterpret_cast<uint8_t*>(malloc(size)); |
| 15479 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 15480 | obj->SetIndexedPropertiesToPixelData(pixel_data, size); |
| 15481 | CHECK(obj->HasIndexedPropertiesInPixelData()); |
| 15482 | CHECK_EQ(pixel_data, obj->GetIndexedPropertiesPixelData()); |
| 15483 | CHECK_EQ(size, obj->GetIndexedPropertiesPixelDataLength()); |
| 15484 | free(pixel_data); |
| 15485 | } |
| 15486 | } |
| 15487 | |
| 15488 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 15489 | static void NotHandledIndexedPropertyGetter( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15490 | uint32_t index, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 15491 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15492 | ApiTestFuzzer::Fuzz(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15493 | } |
| 15494 | |
| 15495 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 15496 | static void NotHandledIndexedPropertySetter( |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15497 | uint32_t index, |
| 15498 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 15499 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15500 | ApiTestFuzzer::Fuzz(); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15501 | } |
| 15502 | |
| 15503 | |
| 15504 | THREADED_TEST(PixelArrayWithInterceptor) { |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15505 | LocalContext context; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15506 | i::Factory* factory = CcTest::i_isolate()->factory(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 15507 | v8::HandleScope scope(context->GetIsolate()); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15508 | const int kElementCount = 260; |
| 15509 | uint8_t* pixel_data = reinterpret_cast<uint8_t*>(malloc(kElementCount)); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15510 | i::Handle<i::ExternalPixelArray> pixels = |
| 15511 | i::Handle<i::ExternalPixelArray>::cast( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 15512 | factory->NewExternalArray(kElementCount, |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 15513 | v8::kExternalPixelArray, |
| 15514 | pixel_data)); |
fschneider@chromium.org | 3a5fd78 | 2011-02-24 10:10:44 +0000 | [diff] [blame] | 15515 | for (int i = 0; i < kElementCount; i++) { |
| 15516 | pixels->set(i, i % 256); |
| 15517 | } |
| 15518 | v8::Handle<v8::ObjectTemplate> templ = v8::ObjectTemplate::New(); |
| 15519 | templ->SetIndexedPropertyHandler(NotHandledIndexedPropertyGetter, |
| 15520 | NotHandledIndexedPropertySetter); |
| 15521 | v8::Handle<v8::Object> obj = templ->NewInstance(); |
| 15522 | obj->SetIndexedPropertiesToPixelData(pixel_data, kElementCount); |
| 15523 | context->Global()->Set(v8_str("pixels"), obj); |
| 15524 | v8::Handle<v8::Value> result = CompileRun("pixels[1]"); |
| 15525 | CHECK_EQ(1, result->Int32Value()); |
| 15526 | result = CompileRun("var sum = 0;" |
| 15527 | "for (var i = 0; i < 8; i++) {" |
| 15528 | " sum += pixels[i] = pixels[i] = -i;" |
| 15529 | "}" |
| 15530 | "sum;"); |
| 15531 | CHECK_EQ(-28, result->Int32Value()); |
| 15532 | result = CompileRun("pixels.hasOwnProperty('1')"); |
| 15533 | CHECK(result->BooleanValue()); |
| 15534 | free(pixel_data); |
| 15535 | } |
| 15536 | |
| 15537 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15538 | static int ExternalArrayElementSize(v8::ExternalArrayType array_type) { |
| 15539 | switch (array_type) { |
| 15540 | case v8::kExternalByteArray: |
| 15541 | case v8::kExternalUnsignedByteArray: |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15542 | case v8::kExternalPixelArray: |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15543 | return 1; |
| 15544 | break; |
| 15545 | case v8::kExternalShortArray: |
| 15546 | case v8::kExternalUnsignedShortArray: |
| 15547 | return 2; |
| 15548 | break; |
| 15549 | case v8::kExternalIntArray: |
| 15550 | case v8::kExternalUnsignedIntArray: |
| 15551 | case v8::kExternalFloatArray: |
| 15552 | return 4; |
| 15553 | break; |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 15554 | case v8::kExternalDoubleArray: |
| 15555 | return 8; |
| 15556 | break; |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 15557 | default: |
| 15558 | UNREACHABLE(); |
| 15559 | return -1; |
| 15560 | } |
| 15561 | UNREACHABLE(); |
| 15562 | return -1; |
| 15563 | } |
| 15564 | |
| 15565 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15566 | template <class ExternalArrayClass, class ElementType> |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15567 | static void ObjectWithExternalArrayTestHelper( |
| 15568 | Handle<Context> context, |
| 15569 | v8::Handle<Object> obj, |
| 15570 | int element_count, |
| 15571 | v8::ExternalArrayType array_type, |
| 15572 | int64_t low, int64_t high) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15573 | i::Handle<i::JSObject> jsobj = v8::Utils::OpenHandle(*obj); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15574 | i::Isolate* isolate = jsobj->GetIsolate(); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15575 | obj->Set(v8_str("field"), v8::Int32::New(1503)); |
| 15576 | context->Global()->Set(v8_str("ext_array"), obj); |
| 15577 | v8::Handle<v8::Value> result = CompileRun("ext_array.field"); |
| 15578 | CHECK_EQ(1503, result->Int32Value()); |
| 15579 | result = CompileRun("ext_array[1]"); |
| 15580 | CHECK_EQ(1, result->Int32Value()); |
| 15581 | |
| 15582 | // Check pass through of assigned smis |
| 15583 | result = CompileRun("var sum = 0;" |
| 15584 | "for (var i = 0; i < 8; i++) {" |
| 15585 | " sum += ext_array[i] = ext_array[i] = -i;" |
| 15586 | "}" |
| 15587 | "sum;"); |
| 15588 | CHECK_EQ(-28, result->Int32Value()); |
| 15589 | |
| 15590 | // Check assigned smis |
| 15591 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15592 | " ext_array[i] = i;" |
| 15593 | "}" |
| 15594 | "var sum = 0;" |
| 15595 | "for (var i = 0; i < 8; i++) {" |
| 15596 | " sum += ext_array[i];" |
| 15597 | "}" |
| 15598 | "sum;"); |
| 15599 | CHECK_EQ(28, result->Int32Value()); |
| 15600 | |
| 15601 | // Check assigned smis in reverse order |
| 15602 | result = CompileRun("for (var i = 8; --i >= 0; ) {" |
| 15603 | " ext_array[i] = i;" |
| 15604 | "}" |
| 15605 | "var sum = 0;" |
| 15606 | "for (var i = 0; i < 8; i++) {" |
| 15607 | " sum += ext_array[i];" |
| 15608 | "}" |
| 15609 | "sum;"); |
| 15610 | CHECK_EQ(28, result->Int32Value()); |
| 15611 | |
| 15612 | // Check pass through of assigned HeapNumbers |
| 15613 | result = CompileRun("var sum = 0;" |
| 15614 | "for (var i = 0; i < 16; i+=2) {" |
| 15615 | " sum += ext_array[i] = ext_array[i] = (-i * 0.5);" |
| 15616 | "}" |
| 15617 | "sum;"); |
| 15618 | CHECK_EQ(-28, result->Int32Value()); |
| 15619 | |
| 15620 | // Check assigned HeapNumbers |
| 15621 | result = CompileRun("for (var i = 0; i < 16; i+=2) {" |
| 15622 | " ext_array[i] = (i * 0.5);" |
| 15623 | "}" |
| 15624 | "var sum = 0;" |
| 15625 | "for (var i = 0; i < 16; i+=2) {" |
| 15626 | " sum += ext_array[i];" |
| 15627 | "}" |
| 15628 | "sum;"); |
| 15629 | CHECK_EQ(28, result->Int32Value()); |
| 15630 | |
| 15631 | // Check assigned HeapNumbers in reverse order |
| 15632 | result = CompileRun("for (var i = 14; i >= 0; i-=2) {" |
| 15633 | " ext_array[i] = (i * 0.5);" |
| 15634 | "}" |
| 15635 | "var sum = 0;" |
| 15636 | "for (var i = 0; i < 16; i+=2) {" |
| 15637 | " sum += ext_array[i];" |
| 15638 | "}" |
| 15639 | "sum;"); |
| 15640 | CHECK_EQ(28, result->Int32Value()); |
| 15641 | |
| 15642 | i::ScopedVector<char> test_buf(1024); |
| 15643 | |
| 15644 | // Check legal boundary conditions. |
| 15645 | // The repeated loads and stores ensure the ICs are exercised. |
| 15646 | const char* boundary_program = |
| 15647 | "var res = 0;" |
| 15648 | "for (var i = 0; i < 16; i++) {" |
| 15649 | " ext_array[i] = %lld;" |
| 15650 | " if (i > 8) {" |
| 15651 | " res = ext_array[i];" |
| 15652 | " }" |
| 15653 | "}" |
| 15654 | "res;"; |
| 15655 | i::OS::SNPrintF(test_buf, |
| 15656 | boundary_program, |
| 15657 | low); |
| 15658 | result = CompileRun(test_buf.start()); |
| 15659 | CHECK_EQ(low, result->IntegerValue()); |
| 15660 | |
| 15661 | i::OS::SNPrintF(test_buf, |
| 15662 | boundary_program, |
| 15663 | high); |
| 15664 | result = CompileRun(test_buf.start()); |
| 15665 | CHECK_EQ(high, result->IntegerValue()); |
| 15666 | |
| 15667 | // Check misprediction of type in IC. |
| 15668 | result = CompileRun("var tmp_array = ext_array;" |
| 15669 | "var sum = 0;" |
| 15670 | "for (var i = 0; i < 8; i++) {" |
| 15671 | " tmp_array[i] = i;" |
| 15672 | " sum += tmp_array[i];" |
| 15673 | " if (i == 4) {" |
| 15674 | " tmp_array = {};" |
| 15675 | " }" |
| 15676 | "}" |
| 15677 | "sum;"); |
erik.corry@gmail.com | c3b670f | 2011-10-05 21:44:48 +0000 | [diff] [blame] | 15678 | // Force GC to trigger verification. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15679 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15680 | CHECK_EQ(28, result->Int32Value()); |
| 15681 | |
| 15682 | // Make sure out-of-range loads do not throw. |
| 15683 | i::OS::SNPrintF(test_buf, |
| 15684 | "var caught_exception = false;" |
| 15685 | "try {" |
| 15686 | " ext_array[%d];" |
| 15687 | "} catch (e) {" |
| 15688 | " caught_exception = true;" |
| 15689 | "}" |
| 15690 | "caught_exception;", |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15691 | element_count); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15692 | result = CompileRun(test_buf.start()); |
| 15693 | CHECK_EQ(false, result->BooleanValue()); |
| 15694 | |
| 15695 | // Make sure out-of-range stores do not throw. |
| 15696 | i::OS::SNPrintF(test_buf, |
| 15697 | "var caught_exception = false;" |
| 15698 | "try {" |
| 15699 | " ext_array[%d] = 1;" |
| 15700 | "} catch (e) {" |
| 15701 | " caught_exception = true;" |
| 15702 | "}" |
| 15703 | "caught_exception;", |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15704 | element_count); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15705 | result = CompileRun(test_buf.start()); |
| 15706 | CHECK_EQ(false, result->BooleanValue()); |
| 15707 | |
| 15708 | // Check other boundary conditions, values and operations. |
| 15709 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15710 | " ext_array[7] = undefined;" |
| 15711 | "}" |
| 15712 | "ext_array[7];"); |
| 15713 | CHECK_EQ(0, result->Int32Value()); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 15714 | if (array_type == v8::kExternalDoubleArray || |
| 15715 | array_type == v8::kExternalFloatArray) { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15716 | CHECK_EQ(static_cast<int>(i::OS::nan_value()), |
| 15717 | static_cast<int>( |
| 15718 | jsobj->GetElement(isolate, 7)->ToObjectChecked()->Number())); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 15719 | } else { |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15720 | CheckElementValue(isolate, 0, jsobj, 7); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 15721 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15722 | |
| 15723 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15724 | " ext_array[6] = '2.3';" |
| 15725 | "}" |
| 15726 | "ext_array[6];"); |
| 15727 | CHECK_EQ(2, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15728 | CHECK_EQ(2, |
| 15729 | static_cast<int>( |
| 15730 | jsobj->GetElement(isolate, 6)->ToObjectChecked()->Number())); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15731 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 15732 | if (array_type != v8::kExternalFloatArray && |
| 15733 | array_type != v8::kExternalDoubleArray) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15734 | // Though the specification doesn't state it, be explicit about |
| 15735 | // converting NaNs and +/-Infinity to zero. |
| 15736 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15737 | " ext_array[i] = 5;" |
| 15738 | "}" |
| 15739 | "for (var i = 0; i < 8; i++) {" |
| 15740 | " ext_array[i] = NaN;" |
| 15741 | "}" |
| 15742 | "ext_array[5];"); |
| 15743 | CHECK_EQ(0, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15744 | CheckElementValue(isolate, 0, jsobj, 5); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15745 | |
| 15746 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15747 | " ext_array[i] = 5;" |
| 15748 | "}" |
| 15749 | "for (var i = 0; i < 8; i++) {" |
| 15750 | " ext_array[i] = Infinity;" |
| 15751 | "}" |
| 15752 | "ext_array[5];"); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15753 | int expected_value = |
| 15754 | (array_type == v8::kExternalPixelArray) ? 255 : 0; |
| 15755 | CHECK_EQ(expected_value, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15756 | CheckElementValue(isolate, expected_value, jsobj, 5); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15757 | |
| 15758 | result = CompileRun("for (var i = 0; i < 8; i++) {" |
| 15759 | " ext_array[i] = 5;" |
| 15760 | "}" |
| 15761 | "for (var i = 0; i < 8; i++) {" |
| 15762 | " ext_array[i] = -Infinity;" |
| 15763 | "}" |
| 15764 | "ext_array[5];"); |
| 15765 | CHECK_EQ(0, result->Int32Value()); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15766 | CheckElementValue(isolate, 0, jsobj, 5); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 15767 | |
| 15768 | // Check truncation behavior of integral arrays. |
| 15769 | const char* unsigned_data = |
| 15770 | "var source_data = [0.6, 10.6];" |
| 15771 | "var expected_results = [0, 10];"; |
| 15772 | const char* signed_data = |
| 15773 | "var source_data = [0.6, 10.6, -0.6, -10.6];" |
| 15774 | "var expected_results = [0, 10, 0, -10];"; |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15775 | const char* pixel_data = |
| 15776 | "var source_data = [0.6, 10.6];" |
| 15777 | "var expected_results = [1, 11];"; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 15778 | bool is_unsigned = |
| 15779 | (array_type == v8::kExternalUnsignedByteArray || |
| 15780 | array_type == v8::kExternalUnsignedShortArray || |
| 15781 | array_type == v8::kExternalUnsignedIntArray); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15782 | bool is_pixel_data = array_type == v8::kExternalPixelArray; |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 15783 | |
| 15784 | i::OS::SNPrintF(test_buf, |
| 15785 | "%s" |
| 15786 | "var all_passed = true;" |
| 15787 | "for (var i = 0; i < source_data.length; i++) {" |
| 15788 | " for (var j = 0; j < 8; j++) {" |
| 15789 | " ext_array[j] = source_data[i];" |
| 15790 | " }" |
| 15791 | " all_passed = all_passed &&" |
| 15792 | " (ext_array[5] == expected_results[i]);" |
| 15793 | "}" |
| 15794 | "all_passed;", |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 15795 | (is_unsigned ? |
| 15796 | unsigned_data : |
| 15797 | (is_pixel_data ? pixel_data : signed_data))); |
vegorov@chromium.org | 0a4e901 | 2011-01-24 12:33:13 +0000 | [diff] [blame] | 15798 | result = CompileRun(test_buf.start()); |
| 15799 | CHECK_EQ(true, result->BooleanValue()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15800 | } |
| 15801 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15802 | i::Handle<ExternalArrayClass> array( |
| 15803 | ExternalArrayClass::cast(jsobj->elements())); |
| 15804 | for (int i = 0; i < element_count; i++) { |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 15805 | array->set(i, static_cast<ElementType>(i)); |
| 15806 | } |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15807 | |
karlklose@chromium.org | 44bc708 | 2011-04-11 12:33:05 +0000 | [diff] [blame] | 15808 | // Test complex assignments |
| 15809 | result = CompileRun("function ee_op_test_complex_func(sum) {" |
| 15810 | " for (var i = 0; i < 40; ++i) {" |
| 15811 | " sum += (ext_array[i] += 1);" |
| 15812 | " sum += (ext_array[i] -= 1);" |
| 15813 | " } " |
| 15814 | " return sum;" |
| 15815 | "}" |
| 15816 | "sum=0;" |
| 15817 | "for (var i=0;i<10000;++i) {" |
| 15818 | " sum=ee_op_test_complex_func(sum);" |
| 15819 | "}" |
| 15820 | "sum;"); |
| 15821 | CHECK_EQ(16000000, result->Int32Value()); |
| 15822 | |
| 15823 | // Test count operations |
| 15824 | result = CompileRun("function ee_op_test_count_func(sum) {" |
| 15825 | " for (var i = 0; i < 40; ++i) {" |
| 15826 | " sum += (++ext_array[i]);" |
| 15827 | " sum += (--ext_array[i]);" |
| 15828 | " } " |
| 15829 | " return sum;" |
| 15830 | "}" |
| 15831 | "sum=0;" |
| 15832 | "for (var i=0;i<10000;++i) {" |
| 15833 | " sum=ee_op_test_count_func(sum);" |
| 15834 | "}" |
| 15835 | "sum;"); |
| 15836 | CHECK_EQ(16000000, result->Int32Value()); |
| 15837 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15838 | result = CompileRun("ext_array[3] = 33;" |
| 15839 | "delete ext_array[3];" |
| 15840 | "ext_array[3];"); |
| 15841 | CHECK_EQ(33, result->Int32Value()); |
| 15842 | |
| 15843 | result = CompileRun("ext_array[0] = 10; ext_array[1] = 11;" |
| 15844 | "ext_array[2] = 12; ext_array[3] = 13;" |
| 15845 | "ext_array.__defineGetter__('2'," |
| 15846 | "function() { return 120; });" |
| 15847 | "ext_array[2];"); |
| 15848 | CHECK_EQ(12, result->Int32Value()); |
| 15849 | |
| 15850 | result = CompileRun("var js_array = new Array(40);" |
| 15851 | "js_array[0] = 77;" |
| 15852 | "js_array;"); |
| 15853 | CHECK_EQ(77, v8::Object::Cast(*result)->Get(v8_str("0"))->Int32Value()); |
| 15854 | |
| 15855 | result = CompileRun("ext_array[1] = 23;" |
| 15856 | "ext_array.__proto__ = [];" |
| 15857 | "js_array.__proto__ = ext_array;" |
| 15858 | "js_array.concat(ext_array);"); |
| 15859 | CHECK_EQ(77, v8::Object::Cast(*result)->Get(v8_str("0"))->Int32Value()); |
| 15860 | CHECK_EQ(23, v8::Object::Cast(*result)->Get(v8_str("1"))->Int32Value()); |
| 15861 | |
| 15862 | result = CompileRun("ext_array[1] = 23;"); |
| 15863 | CHECK_EQ(23, result->Int32Value()); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15864 | } |
| 15865 | |
| 15866 | |
| 15867 | template <class ExternalArrayClass, class ElementType> |
| 15868 | static void ExternalArrayTestHelper(v8::ExternalArrayType array_type, |
| 15869 | int64_t low, |
| 15870 | int64_t high) { |
| 15871 | LocalContext context; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15872 | i::Isolate* isolate = CcTest::i_isolate(); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15873 | i::Factory* factory = isolate->factory(); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15874 | v8::HandleScope scope(context->GetIsolate()); |
| 15875 | const int kElementCount = 40; |
| 15876 | int element_size = ExternalArrayElementSize(array_type); |
| 15877 | ElementType* array_data = |
| 15878 | static_cast<ElementType*>(malloc(kElementCount * element_size)); |
| 15879 | i::Handle<ExternalArrayClass> array = |
| 15880 | i::Handle<ExternalArrayClass>::cast( |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 15881 | factory->NewExternalArray(kElementCount, array_type, array_data)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15882 | // Force GC to trigger verification. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15883 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15884 | for (int i = 0; i < kElementCount; i++) { |
| 15885 | array->set(i, static_cast<ElementType>(i)); |
| 15886 | } |
| 15887 | // Force GC to trigger verification. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 15888 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15889 | for (int i = 0; i < kElementCount; i++) { |
| 15890 | CHECK_EQ(static_cast<int64_t>(i), |
| 15891 | static_cast<int64_t>(array->get_scalar(i))); |
| 15892 | CHECK_EQ(static_cast<int64_t>(i), static_cast<int64_t>(array_data[i])); |
| 15893 | } |
| 15894 | |
| 15895 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 15896 | i::Handle<i::JSObject> jsobj = v8::Utils::OpenHandle(*obj); |
| 15897 | // Set the elements to be the external array. |
| 15898 | obj->SetIndexedPropertiesToExternalArrayData(array_data, |
| 15899 | array_type, |
| 15900 | kElementCount); |
jkummerow@chromium.org | 3d00d0a | 2013-09-04 13:57:32 +0000 | [diff] [blame] | 15901 | CHECK_EQ(1, |
| 15902 | static_cast<int>( |
| 15903 | jsobj->GetElement(isolate, 1)->ToObjectChecked()->Number())); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 15904 | |
| 15905 | ObjectWithExternalArrayTestHelper<ExternalArrayClass, ElementType>( |
| 15906 | context.local(), obj, kElementCount, array_type, low, high); |
| 15907 | |
| 15908 | v8::Handle<v8::Value> result; |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 15909 | |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 15910 | // Test more complex manipulations which cause eax to contain values |
| 15911 | // that won't be completely overwritten by loads from the arrays. |
| 15912 | // This catches bugs in the instructions used for the KeyedLoadIC |
| 15913 | // for byte and word types. |
| 15914 | { |
| 15915 | const int kXSize = 300; |
| 15916 | const int kYSize = 300; |
| 15917 | const int kLargeElementCount = kXSize * kYSize * 4; |
| 15918 | ElementType* large_array_data = |
| 15919 | static_cast<ElementType*>(malloc(kLargeElementCount * element_size)); |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 15920 | v8::Handle<v8::Object> large_obj = v8::Object::New(); |
| 15921 | // Set the elements to be the external array. |
| 15922 | large_obj->SetIndexedPropertiesToExternalArrayData(large_array_data, |
| 15923 | array_type, |
| 15924 | kLargeElementCount); |
| 15925 | context->Global()->Set(v8_str("large_array"), large_obj); |
| 15926 | // Initialize contents of a few rows. |
| 15927 | for (int x = 0; x < 300; x++) { |
| 15928 | int row = 0; |
| 15929 | int offset = row * 300 * 4; |
| 15930 | large_array_data[offset + 4 * x + 0] = (ElementType) 127; |
| 15931 | large_array_data[offset + 4 * x + 1] = (ElementType) 0; |
| 15932 | large_array_data[offset + 4 * x + 2] = (ElementType) 0; |
| 15933 | large_array_data[offset + 4 * x + 3] = (ElementType) 127; |
| 15934 | row = 150; |
| 15935 | offset = row * 300 * 4; |
| 15936 | large_array_data[offset + 4 * x + 0] = (ElementType) 127; |
| 15937 | large_array_data[offset + 4 * x + 1] = (ElementType) 0; |
| 15938 | large_array_data[offset + 4 * x + 2] = (ElementType) 0; |
| 15939 | large_array_data[offset + 4 * x + 3] = (ElementType) 127; |
| 15940 | row = 298; |
| 15941 | offset = row * 300 * 4; |
| 15942 | large_array_data[offset + 4 * x + 0] = (ElementType) 127; |
| 15943 | large_array_data[offset + 4 * x + 1] = (ElementType) 0; |
| 15944 | large_array_data[offset + 4 * x + 2] = (ElementType) 0; |
| 15945 | large_array_data[offset + 4 * x + 3] = (ElementType) 127; |
| 15946 | } |
| 15947 | // The goal of the code below is to make "offset" large enough |
| 15948 | // that the computation of the index (which goes into eax) has |
| 15949 | // high bits set which will not be overwritten by a byte or short |
| 15950 | // load. |
| 15951 | result = CompileRun("var failed = false;" |
| 15952 | "var offset = 0;" |
| 15953 | "for (var i = 0; i < 300; i++) {" |
| 15954 | " if (large_array[4 * i] != 127 ||" |
| 15955 | " large_array[4 * i + 1] != 0 ||" |
| 15956 | " large_array[4 * i + 2] != 0 ||" |
| 15957 | " large_array[4 * i + 3] != 127) {" |
| 15958 | " failed = true;" |
| 15959 | " }" |
| 15960 | "}" |
| 15961 | "offset = 150 * 300 * 4;" |
| 15962 | "for (var i = 0; i < 300; i++) {" |
| 15963 | " if (large_array[offset + 4 * i] != 127 ||" |
| 15964 | " large_array[offset + 4 * i + 1] != 0 ||" |
| 15965 | " large_array[offset + 4 * i + 2] != 0 ||" |
| 15966 | " large_array[offset + 4 * i + 3] != 127) {" |
| 15967 | " failed = true;" |
| 15968 | " }" |
| 15969 | "}" |
| 15970 | "offset = 298 * 300 * 4;" |
| 15971 | "for (var i = 0; i < 300; i++) {" |
| 15972 | " if (large_array[offset + 4 * i] != 127 ||" |
| 15973 | " large_array[offset + 4 * i + 1] != 0 ||" |
| 15974 | " large_array[offset + 4 * i + 2] != 0 ||" |
| 15975 | " large_array[offset + 4 * i + 3] != 127) {" |
| 15976 | " failed = true;" |
| 15977 | " }" |
| 15978 | "}" |
| 15979 | "!failed;"); |
| 15980 | CHECK_EQ(true, result->BooleanValue()); |
| 15981 | free(large_array_data); |
| 15982 | } |
| 15983 | |
fschneider@chromium.org | 7979bbb | 2011-03-28 10:47:03 +0000 | [diff] [blame] | 15984 | // The "" property descriptor is overloaded to store information about |
| 15985 | // the external array. Ensure that setting and accessing the "" property |
| 15986 | // works (it should overwrite the information cached about the external |
| 15987 | // array in the DescriptorArray) in various situations. |
| 15988 | result = CompileRun("ext_array[''] = 23; ext_array['']"); |
| 15989 | CHECK_EQ(23, result->Int32Value()); |
| 15990 | |
| 15991 | // Property "" set after the external array is associated with the object. |
| 15992 | { |
| 15993 | v8::Handle<v8::Object> obj2 = v8::Object::New(); |
| 15994 | obj2->Set(v8_str("ee_test_field"), v8::Int32::New(256)); |
| 15995 | obj2->Set(v8_str(""), v8::Int32::New(1503)); |
| 15996 | // Set the elements to be the external array. |
| 15997 | obj2->SetIndexedPropertiesToExternalArrayData(array_data, |
| 15998 | array_type, |
| 15999 | kElementCount); |
| 16000 | context->Global()->Set(v8_str("ext_array"), obj2); |
| 16001 | result = CompileRun("ext_array['']"); |
| 16002 | CHECK_EQ(1503, result->Int32Value()); |
| 16003 | } |
| 16004 | |
| 16005 | // Property "" set after the external array is associated with the object. |
| 16006 | { |
| 16007 | v8::Handle<v8::Object> obj2 = v8::Object::New(); |
| 16008 | obj2->Set(v8_str("ee_test_field_2"), v8::Int32::New(256)); |
| 16009 | // Set the elements to be the external array. |
| 16010 | obj2->SetIndexedPropertiesToExternalArrayData(array_data, |
| 16011 | array_type, |
| 16012 | kElementCount); |
| 16013 | obj2->Set(v8_str(""), v8::Int32::New(1503)); |
| 16014 | context->Global()->Set(v8_str("ext_array"), obj2); |
| 16015 | result = CompileRun("ext_array['']"); |
| 16016 | CHECK_EQ(1503, result->Int32Value()); |
| 16017 | } |
| 16018 | |
| 16019 | // Should reuse the map from previous test. |
| 16020 | { |
| 16021 | v8::Handle<v8::Object> obj2 = v8::Object::New(); |
| 16022 | obj2->Set(v8_str("ee_test_field_2"), v8::Int32::New(256)); |
| 16023 | // Set the elements to be the external array. Should re-use the map |
| 16024 | // from previous test. |
| 16025 | obj2->SetIndexedPropertiesToExternalArrayData(array_data, |
| 16026 | array_type, |
| 16027 | kElementCount); |
| 16028 | context->Global()->Set(v8_str("ext_array"), obj2); |
| 16029 | result = CompileRun("ext_array['']"); |
| 16030 | } |
| 16031 | |
| 16032 | // Property "" is a constant function that shouldn't not be interfered with |
| 16033 | // when an external array is set. |
| 16034 | { |
| 16035 | v8::Handle<v8::Object> obj2 = v8::Object::New(); |
| 16036 | // Start |
| 16037 | obj2->Set(v8_str("ee_test_field3"), v8::Int32::New(256)); |
| 16038 | |
| 16039 | // Add a constant function to an object. |
| 16040 | context->Global()->Set(v8_str("ext_array"), obj2); |
| 16041 | result = CompileRun("ext_array[''] = function() {return 1503;};" |
| 16042 | "ext_array['']();"); |
| 16043 | |
| 16044 | // Add an external array transition to the same map that |
| 16045 | // has the constant transition. |
| 16046 | v8::Handle<v8::Object> obj3 = v8::Object::New(); |
| 16047 | obj3->Set(v8_str("ee_test_field3"), v8::Int32::New(256)); |
| 16048 | obj3->SetIndexedPropertiesToExternalArrayData(array_data, |
| 16049 | array_type, |
| 16050 | kElementCount); |
| 16051 | context->Global()->Set(v8_str("ext_array"), obj3); |
| 16052 | } |
| 16053 | |
| 16054 | // If a external array transition is in the map, it should get clobbered |
| 16055 | // by a constant function. |
| 16056 | { |
| 16057 | // Add an external array transition. |
| 16058 | v8::Handle<v8::Object> obj3 = v8::Object::New(); |
| 16059 | obj3->Set(v8_str("ee_test_field4"), v8::Int32::New(256)); |
| 16060 | obj3->SetIndexedPropertiesToExternalArrayData(array_data, |
| 16061 | array_type, |
| 16062 | kElementCount); |
| 16063 | |
| 16064 | // Add a constant function to the same map that just got an external array |
| 16065 | // transition. |
| 16066 | v8::Handle<v8::Object> obj2 = v8::Object::New(); |
| 16067 | obj2->Set(v8_str("ee_test_field4"), v8::Int32::New(256)); |
| 16068 | context->Global()->Set(v8_str("ext_array"), obj2); |
| 16069 | result = CompileRun("ext_array[''] = function() {return 1503;};" |
| 16070 | "ext_array['']();"); |
| 16071 | } |
| 16072 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16073 | free(array_data); |
| 16074 | } |
| 16075 | |
| 16076 | |
| 16077 | THREADED_TEST(ExternalByteArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16078 | ExternalArrayTestHelper<i::ExternalByteArray, int8_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16079 | v8::kExternalByteArray, |
| 16080 | -128, |
| 16081 | 127); |
| 16082 | } |
| 16083 | |
| 16084 | |
| 16085 | THREADED_TEST(ExternalUnsignedByteArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16086 | ExternalArrayTestHelper<i::ExternalUnsignedByteArray, uint8_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16087 | v8::kExternalUnsignedByteArray, |
| 16088 | 0, |
| 16089 | 255); |
| 16090 | } |
| 16091 | |
| 16092 | |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 16093 | THREADED_TEST(ExternalPixelArray) { |
| 16094 | ExternalArrayTestHelper<i::ExternalPixelArray, uint8_t>( |
| 16095 | v8::kExternalPixelArray, |
| 16096 | 0, |
| 16097 | 255); |
| 16098 | } |
| 16099 | |
| 16100 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16101 | THREADED_TEST(ExternalShortArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16102 | ExternalArrayTestHelper<i::ExternalShortArray, int16_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16103 | v8::kExternalShortArray, |
| 16104 | -32768, |
| 16105 | 32767); |
| 16106 | } |
| 16107 | |
| 16108 | |
| 16109 | THREADED_TEST(ExternalUnsignedShortArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16110 | ExternalArrayTestHelper<i::ExternalUnsignedShortArray, uint16_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16111 | v8::kExternalUnsignedShortArray, |
| 16112 | 0, |
| 16113 | 65535); |
| 16114 | } |
| 16115 | |
| 16116 | |
| 16117 | THREADED_TEST(ExternalIntArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16118 | ExternalArrayTestHelper<i::ExternalIntArray, int32_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16119 | v8::kExternalIntArray, |
| 16120 | INT_MIN, // -2147483648 |
| 16121 | INT_MAX); // 2147483647 |
| 16122 | } |
| 16123 | |
| 16124 | |
| 16125 | THREADED_TEST(ExternalUnsignedIntArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16126 | ExternalArrayTestHelper<i::ExternalUnsignedIntArray, uint32_t>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16127 | v8::kExternalUnsignedIntArray, |
| 16128 | 0, |
| 16129 | UINT_MAX); // 4294967295 |
| 16130 | } |
| 16131 | |
| 16132 | |
| 16133 | THREADED_TEST(ExternalFloatArray) { |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 16134 | ExternalArrayTestHelper<i::ExternalFloatArray, float>( |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16135 | v8::kExternalFloatArray, |
| 16136 | -500, |
| 16137 | 500); |
| 16138 | } |
| 16139 | |
| 16140 | |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 16141 | THREADED_TEST(ExternalDoubleArray) { |
| 16142 | ExternalArrayTestHelper<i::ExternalDoubleArray, double>( |
| 16143 | v8::kExternalDoubleArray, |
| 16144 | -500, |
| 16145 | 500); |
| 16146 | } |
| 16147 | |
| 16148 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 16149 | THREADED_TEST(ExternalArrays) { |
| 16150 | TestExternalByteArray(); |
| 16151 | TestExternalUnsignedByteArray(); |
| 16152 | TestExternalShortArray(); |
| 16153 | TestExternalUnsignedShortArray(); |
| 16154 | TestExternalIntArray(); |
| 16155 | TestExternalUnsignedIntArray(); |
| 16156 | TestExternalFloatArray(); |
| 16157 | } |
| 16158 | |
| 16159 | |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 16160 | void ExternalArrayInfoTestHelper(v8::ExternalArrayType array_type) { |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 16161 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16162 | v8::HandleScope scope(context->GetIsolate()); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 16163 | for (int size = 0; size < 100; size += 10) { |
| 16164 | int element_size = ExternalArrayElementSize(array_type); |
| 16165 | void* external_data = malloc(size * element_size); |
| 16166 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 16167 | obj->SetIndexedPropertiesToExternalArrayData( |
| 16168 | external_data, array_type, size); |
| 16169 | CHECK(obj->HasIndexedPropertiesInExternalArrayData()); |
| 16170 | CHECK_EQ(external_data, obj->GetIndexedPropertiesExternalArrayData()); |
| 16171 | CHECK_EQ(array_type, obj->GetIndexedPropertiesExternalArrayDataType()); |
| 16172 | CHECK_EQ(size, obj->GetIndexedPropertiesExternalArrayDataLength()); |
| 16173 | free(external_data); |
| 16174 | } |
| 16175 | } |
| 16176 | |
| 16177 | |
| 16178 | THREADED_TEST(ExternalArrayInfo) { |
| 16179 | ExternalArrayInfoTestHelper(v8::kExternalByteArray); |
| 16180 | ExternalArrayInfoTestHelper(v8::kExternalUnsignedByteArray); |
| 16181 | ExternalArrayInfoTestHelper(v8::kExternalShortArray); |
| 16182 | ExternalArrayInfoTestHelper(v8::kExternalUnsignedShortArray); |
| 16183 | ExternalArrayInfoTestHelper(v8::kExternalIntArray); |
| 16184 | ExternalArrayInfoTestHelper(v8::kExternalUnsignedIntArray); |
| 16185 | ExternalArrayInfoTestHelper(v8::kExternalFloatArray); |
erik.corry@gmail.com | 3847bd5 | 2011-04-27 10:38:56 +0000 | [diff] [blame] | 16186 | ExternalArrayInfoTestHelper(v8::kExternalDoubleArray); |
danno@chromium.org | 4d3fe4e | 2011-03-10 10:14:28 +0000 | [diff] [blame] | 16187 | ExternalArrayInfoTestHelper(v8::kExternalPixelArray); |
whesse@chromium.org | 2c186ca | 2010-06-16 11:32:39 +0000 | [diff] [blame] | 16188 | } |
| 16189 | |
| 16190 | |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 16191 | void ExternalArrayLimitTestHelper(v8::ExternalArrayType array_type, int size) { |
| 16192 | v8::Handle<v8::Object> obj = v8::Object::New(); |
| 16193 | v8::V8::SetFatalErrorHandler(StoringErrorCallback); |
| 16194 | last_location = last_message = NULL; |
| 16195 | obj->SetIndexedPropertiesToExternalArrayData(NULL, array_type, size); |
| 16196 | CHECK(!obj->HasIndexedPropertiesInExternalArrayData()); |
| 16197 | CHECK_NE(NULL, last_location); |
| 16198 | CHECK_NE(NULL, last_message); |
| 16199 | } |
| 16200 | |
| 16201 | |
| 16202 | TEST(ExternalArrayLimits) { |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 16203 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16204 | v8::HandleScope scope(context->GetIsolate()); |
danno@chromium.org | 412fa51 | 2012-09-14 13:28:26 +0000 | [diff] [blame] | 16205 | ExternalArrayLimitTestHelper(v8::kExternalByteArray, 0x40000000); |
| 16206 | ExternalArrayLimitTestHelper(v8::kExternalByteArray, 0xffffffff); |
| 16207 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedByteArray, 0x40000000); |
| 16208 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedByteArray, 0xffffffff); |
| 16209 | ExternalArrayLimitTestHelper(v8::kExternalShortArray, 0x40000000); |
| 16210 | ExternalArrayLimitTestHelper(v8::kExternalShortArray, 0xffffffff); |
| 16211 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedShortArray, 0x40000000); |
| 16212 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedShortArray, 0xffffffff); |
| 16213 | ExternalArrayLimitTestHelper(v8::kExternalIntArray, 0x40000000); |
| 16214 | ExternalArrayLimitTestHelper(v8::kExternalIntArray, 0xffffffff); |
| 16215 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedIntArray, 0x40000000); |
| 16216 | ExternalArrayLimitTestHelper(v8::kExternalUnsignedIntArray, 0xffffffff); |
| 16217 | ExternalArrayLimitTestHelper(v8::kExternalFloatArray, 0x40000000); |
| 16218 | ExternalArrayLimitTestHelper(v8::kExternalFloatArray, 0xffffffff); |
| 16219 | ExternalArrayLimitTestHelper(v8::kExternalDoubleArray, 0x40000000); |
| 16220 | ExternalArrayLimitTestHelper(v8::kExternalDoubleArray, 0xffffffff); |
| 16221 | ExternalArrayLimitTestHelper(v8::kExternalPixelArray, 0x40000000); |
| 16222 | ExternalArrayLimitTestHelper(v8::kExternalPixelArray, 0xffffffff); |
| 16223 | } |
| 16224 | |
| 16225 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16226 | template <typename ElementType, typename TypedArray, |
| 16227 | class ExternalArrayClass> |
| 16228 | void TypedArrayTestHelper(v8::ExternalArrayType array_type, |
| 16229 | int64_t low, int64_t high) { |
| 16230 | const int kElementCount = 50; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16231 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 16232 | i::ScopedVector<ElementType> backing_store(kElementCount+2); |
| 16233 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16234 | LocalContext env; |
| 16235 | v8::Isolate* isolate = env->GetIsolate(); |
| 16236 | v8::HandleScope handle_scope(isolate); |
| 16237 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 16238 | Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New( |
| 16239 | backing_store.start(), (kElementCount+2)*sizeof(ElementType)); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16240 | Local<TypedArray> ta = |
| 16241 | TypedArray::New(ab, 2*sizeof(ElementType), kElementCount); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16242 | CheckInternalFieldsAreZero<v8::ArrayBufferView>(ta); |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16243 | CHECK_EQ(kElementCount, static_cast<int>(ta->Length())); |
| 16244 | CHECK_EQ(2*sizeof(ElementType), static_cast<int>(ta->ByteOffset())); |
| 16245 | CHECK_EQ(kElementCount*sizeof(ElementType), |
| 16246 | static_cast<int>(ta->ByteLength())); |
| 16247 | CHECK_EQ(ab, ta->Buffer()); |
| 16248 | |
svenpanne@chromium.org | a53e8e0 | 2013-05-24 12:35:50 +0000 | [diff] [blame] | 16249 | ElementType* data = backing_store.start() + 2; |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 16250 | for (int i = 0; i < kElementCount; i++) { |
| 16251 | data[i] = static_cast<ElementType>(i); |
| 16252 | } |
| 16253 | |
| 16254 | ObjectWithExternalArrayTestHelper<ExternalArrayClass, ElementType>( |
| 16255 | env.local(), ta, kElementCount, array_type, low, high); |
| 16256 | } |
| 16257 | |
| 16258 | |
| 16259 | THREADED_TEST(Uint8Array) { |
| 16260 | TypedArrayTestHelper<uint8_t, v8::Uint8Array, i::ExternalUnsignedByteArray>( |
| 16261 | v8::kExternalUnsignedByteArray, 0, 0xFF); |
| 16262 | } |
| 16263 | |
| 16264 | |
| 16265 | THREADED_TEST(Int8Array) { |
| 16266 | TypedArrayTestHelper<int8_t, v8::Int8Array, i::ExternalByteArray>( |
| 16267 | v8::kExternalByteArray, -0x80, 0x7F); |
| 16268 | } |
| 16269 | |
| 16270 | |
| 16271 | THREADED_TEST(Uint16Array) { |
| 16272 | TypedArrayTestHelper<uint16_t, |
| 16273 | v8::Uint16Array, |
| 16274 | i::ExternalUnsignedShortArray>( |
| 16275 | v8::kExternalUnsignedShortArray, 0, 0xFFFF); |
| 16276 | } |
| 16277 | |
| 16278 | |
| 16279 | THREADED_TEST(Int16Array) { |
| 16280 | TypedArrayTestHelper<int16_t, v8::Int16Array, i::ExternalShortArray>( |
| 16281 | v8::kExternalShortArray, -0x8000, 0x7FFF); |
| 16282 | } |
| 16283 | |
| 16284 | |
| 16285 | THREADED_TEST(Uint32Array) { |
| 16286 | TypedArrayTestHelper<uint32_t, v8::Uint32Array, i::ExternalUnsignedIntArray>( |
| 16287 | v8::kExternalUnsignedIntArray, 0, UINT_MAX); |
| 16288 | } |
| 16289 | |
| 16290 | |
| 16291 | THREADED_TEST(Int32Array) { |
| 16292 | TypedArrayTestHelper<int32_t, v8::Int32Array, i::ExternalIntArray>( |
| 16293 | v8::kExternalIntArray, INT_MIN, INT_MAX); |
| 16294 | } |
| 16295 | |
| 16296 | |
| 16297 | THREADED_TEST(Float32Array) { |
| 16298 | TypedArrayTestHelper<float, v8::Float32Array, i::ExternalFloatArray>( |
| 16299 | v8::kExternalFloatArray, -500, 500); |
| 16300 | } |
| 16301 | |
| 16302 | |
| 16303 | THREADED_TEST(Float64Array) { |
| 16304 | TypedArrayTestHelper<double, v8::Float64Array, i::ExternalDoubleArray>( |
| 16305 | v8::kExternalDoubleArray, -500, 500); |
| 16306 | } |
| 16307 | |
| 16308 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16309 | THREADED_TEST(Uint8ClampedArray) { |
| 16310 | TypedArrayTestHelper<uint8_t, v8::Uint8ClampedArray, i::ExternalPixelArray>( |
| 16311 | v8::kExternalPixelArray, 0, 0xFF); |
| 16312 | } |
| 16313 | |
| 16314 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16315 | THREADED_TEST(DataView) { |
| 16316 | const int kSize = 50; |
| 16317 | |
| 16318 | i::ScopedVector<uint8_t> backing_store(kSize+2); |
| 16319 | |
| 16320 | LocalContext env; |
| 16321 | v8::Isolate* isolate = env->GetIsolate(); |
| 16322 | v8::HandleScope handle_scope(isolate); |
| 16323 | |
| 16324 | Local<v8::ArrayBuffer> ab = v8::ArrayBuffer::New( |
| 16325 | backing_store.start(), 2 + kSize); |
| 16326 | Local<v8::DataView> dv = |
| 16327 | v8::DataView::New(ab, 2, kSize); |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16328 | CheckInternalFieldsAreZero<v8::ArrayBufferView>(dv); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16329 | CHECK_EQ(2, static_cast<int>(dv->ByteOffset())); |
| 16330 | CHECK_EQ(kSize, static_cast<int>(dv->ByteLength())); |
| 16331 | CHECK_EQ(ab, dv->Buffer()); |
| 16332 | } |
| 16333 | |
| 16334 | |
| 16335 | #define IS_ARRAY_BUFFER_VIEW_TEST(View) \ |
| 16336 | THREADED_TEST(Is##View) { \ |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16337 | LocalContext env; \ |
| 16338 | v8::Isolate* isolate = env->GetIsolate(); \ |
| 16339 | v8::HandleScope handle_scope(isolate); \ |
| 16340 | \ |
| 16341 | Handle<Value> result = CompileRun( \ |
| 16342 | "var ab = new ArrayBuffer(128);" \ |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16343 | "new " #View "(ab)"); \ |
| 16344 | CHECK(result->IsArrayBufferView()); \ |
| 16345 | CHECK(result->Is##View()); \ |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16346 | CheckInternalFieldsAreZero<v8::ArrayBufferView>(result.As<v8::View>()); \ |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16347 | } |
| 16348 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16349 | IS_ARRAY_BUFFER_VIEW_TEST(Uint8Array) |
| 16350 | IS_ARRAY_BUFFER_VIEW_TEST(Int8Array) |
| 16351 | IS_ARRAY_BUFFER_VIEW_TEST(Uint16Array) |
| 16352 | IS_ARRAY_BUFFER_VIEW_TEST(Int16Array) |
| 16353 | IS_ARRAY_BUFFER_VIEW_TEST(Uint32Array) |
| 16354 | IS_ARRAY_BUFFER_VIEW_TEST(Int32Array) |
| 16355 | IS_ARRAY_BUFFER_VIEW_TEST(Float32Array) |
| 16356 | IS_ARRAY_BUFFER_VIEW_TEST(Float64Array) |
| 16357 | IS_ARRAY_BUFFER_VIEW_TEST(Uint8ClampedArray) |
| 16358 | IS_ARRAY_BUFFER_VIEW_TEST(DataView) |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16359 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16360 | #undef IS_ARRAY_BUFFER_VIEW_TEST |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16361 | |
| 16362 | |
| 16363 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16364 | THREADED_TEST(ScriptContextDependence) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16365 | LocalContext c1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16366 | v8::HandleScope scope(c1->GetIsolate()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16367 | const char *source = "foo"; |
| 16368 | v8::Handle<v8::Script> dep = v8::Script::Compile(v8::String::New(source)); |
| 16369 | v8::Handle<v8::Script> indep = v8::Script::New(v8::String::New(source)); |
| 16370 | c1->Global()->Set(v8::String::New("foo"), v8::Integer::New(100)); |
| 16371 | CHECK_EQ(dep->Run()->Int32Value(), 100); |
| 16372 | CHECK_EQ(indep->Run()->Int32Value(), 100); |
| 16373 | LocalContext c2; |
| 16374 | c2->Global()->Set(v8::String::New("foo"), v8::Integer::New(101)); |
| 16375 | CHECK_EQ(dep->Run()->Int32Value(), 100); |
| 16376 | CHECK_EQ(indep->Run()->Int32Value(), 101); |
| 16377 | } |
| 16378 | |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 16379 | |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16380 | THREADED_TEST(StackTrace) { |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16381 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16382 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | 911335c | 2009-08-19 12:59:44 +0000 | [diff] [blame] | 16383 | v8::TryCatch try_catch; |
| 16384 | const char *source = "function foo() { FAIL.FAIL; }; foo();"; |
| 16385 | v8::Handle<v8::String> src = v8::String::New(source); |
| 16386 | v8::Handle<v8::String> origin = v8::String::New("stack-trace-test"); |
| 16387 | v8::Script::New(src, origin)->Run(); |
| 16388 | CHECK(try_catch.HasCaught()); |
| 16389 | v8::String::Utf8Value stack(try_catch.StackTrace()); |
| 16390 | CHECK(strstr(*stack, "at foo (stack-trace-test") != NULL); |
| 16391 | } |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 16392 | |
| 16393 | |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16394 | // Checks that a StackFrame has certain expected values. |
| 16395 | void checkStackFrame(const char* expected_script_name, |
| 16396 | const char* expected_func_name, int expected_line_number, |
| 16397 | int expected_column, bool is_eval, bool is_constructor, |
| 16398 | v8::Handle<v8::StackFrame> frame) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16399 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16400 | v8::String::Utf8Value func_name(frame->GetFunctionName()); |
| 16401 | v8::String::Utf8Value script_name(frame->GetScriptName()); |
| 16402 | if (*script_name == NULL) { |
| 16403 | // The situation where there is no associated script, like for evals. |
| 16404 | CHECK(expected_script_name == NULL); |
| 16405 | } else { |
| 16406 | CHECK(strstr(*script_name, expected_script_name) != NULL); |
| 16407 | } |
| 16408 | CHECK(strstr(*func_name, expected_func_name) != NULL); |
| 16409 | CHECK_EQ(expected_line_number, frame->GetLineNumber()); |
| 16410 | CHECK_EQ(expected_column, frame->GetColumn()); |
| 16411 | CHECK_EQ(is_eval, frame->IsEval()); |
| 16412 | CHECK_EQ(is_constructor, frame->IsConstructor()); |
| 16413 | } |
| 16414 | |
| 16415 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16416 | void AnalyzeStackInNativeCode(const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16417 | v8::HandleScope scope(args.GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16418 | const char* origin = "capture-stack-trace-test"; |
| 16419 | const int kOverviewTest = 1; |
| 16420 | const int kDetailedTest = 2; |
| 16421 | |
| 16422 | ASSERT(args.Length() == 1); |
| 16423 | |
| 16424 | int testGroup = args[0]->Int32Value(); |
| 16425 | if (testGroup == kOverviewTest) { |
| 16426 | v8::Handle<v8::StackTrace> stackTrace = |
| 16427 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kOverview); |
| 16428 | CHECK_EQ(4, stackTrace->GetFrameCount()); |
| 16429 | checkStackFrame(origin, "bar", 2, 10, false, false, |
| 16430 | stackTrace->GetFrame(0)); |
| 16431 | checkStackFrame(origin, "foo", 6, 3, false, false, |
| 16432 | stackTrace->GetFrame(1)); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 16433 | // This is the source string inside the eval which has the call to foo. |
| 16434 | checkStackFrame(NULL, "", 1, 5, false, false, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16435 | stackTrace->GetFrame(2)); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 16436 | // The last frame is an anonymous function which has the initial eval call. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16437 | checkStackFrame(origin, "", 8, 7, false, false, |
| 16438 | stackTrace->GetFrame(3)); |
| 16439 | |
| 16440 | CHECK(stackTrace->AsArray()->IsArray()); |
| 16441 | } else if (testGroup == kDetailedTest) { |
| 16442 | v8::Handle<v8::StackTrace> stackTrace = |
| 16443 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); |
| 16444 | CHECK_EQ(4, stackTrace->GetFrameCount()); |
| 16445 | checkStackFrame(origin, "bat", 4, 22, false, false, |
| 16446 | stackTrace->GetFrame(0)); |
| 16447 | checkStackFrame(origin, "baz", 8, 3, false, true, |
| 16448 | stackTrace->GetFrame(1)); |
ricow@chromium.org | 5ad5ace | 2010-06-23 09:06:43 +0000 | [diff] [blame] | 16449 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 16450 | bool is_eval = true; |
| 16451 | #else // ENABLE_DEBUGGER_SUPPORT |
| 16452 | bool is_eval = false; |
| 16453 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 16454 | |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 16455 | // This is the source string inside the eval which has the call to baz. |
| 16456 | checkStackFrame(NULL, "", 1, 5, is_eval, false, |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16457 | stackTrace->GetFrame(2)); |
whesse@chromium.org | 030d38e | 2011-07-13 13:23:34 +0000 | [diff] [blame] | 16458 | // The last frame is an anonymous function which has the initial eval call. |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16459 | checkStackFrame(origin, "", 10, 1, false, false, |
| 16460 | stackTrace->GetFrame(3)); |
| 16461 | |
| 16462 | CHECK(stackTrace->AsArray()->IsArray()); |
| 16463 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16464 | } |
| 16465 | |
| 16466 | |
| 16467 | // Tests the C++ StackTrace API. |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 16468 | // TODO(3074796): Reenable this as a THREADED_TEST once it passes. |
| 16469 | // THREADED_TEST(CaptureStackTrace) { |
| 16470 | TEST(CaptureStackTrace) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16471 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16472 | v8::Handle<v8::String> origin = v8::String::New("capture-stack-trace-test"); |
| 16473 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 16474 | templ->Set(v8_str("AnalyzeStackInNativeCode"), |
| 16475 | v8::FunctionTemplate::New(AnalyzeStackInNativeCode)); |
| 16476 | LocalContext context(0, templ); |
| 16477 | |
| 16478 | // Test getting OVERVIEW information. Should ignore information that is not |
| 16479 | // script name, function name, line number, and column offset. |
| 16480 | const char *overview_source = |
| 16481 | "function bar() {\n" |
| 16482 | " var y; AnalyzeStackInNativeCode(1);\n" |
| 16483 | "}\n" |
| 16484 | "function foo() {\n" |
| 16485 | "\n" |
| 16486 | " bar();\n" |
| 16487 | "}\n" |
| 16488 | "var x;eval('new foo();');"; |
| 16489 | v8::Handle<v8::String> overview_src = v8::String::New(overview_source); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 16490 | v8::Handle<Value> overview_result( |
| 16491 | v8::Script::New(overview_src, origin)->Run()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 16492 | CHECK(!overview_result.IsEmpty()); |
| 16493 | CHECK(overview_result->IsObject()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16494 | |
| 16495 | // Test getting DETAILED information. |
| 16496 | const char *detailed_source = |
| 16497 | "function bat() {AnalyzeStackInNativeCode(2);\n" |
| 16498 | "}\n" |
| 16499 | "\n" |
| 16500 | "function baz() {\n" |
| 16501 | " bat();\n" |
| 16502 | "}\n" |
| 16503 | "eval('new baz();');"; |
| 16504 | v8::Handle<v8::String> detailed_src = v8::String::New(detailed_source); |
| 16505 | // Make the script using a non-zero line and column offset. |
| 16506 | v8::Handle<v8::Integer> line_offset = v8::Integer::New(3); |
| 16507 | v8::Handle<v8::Integer> column_offset = v8::Integer::New(5); |
| 16508 | v8::ScriptOrigin detailed_origin(origin, line_offset, column_offset); |
| 16509 | v8::Handle<v8::Script> detailed_script( |
| 16510 | v8::Script::New(detailed_src, &detailed_origin)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 16511 | v8::Handle<Value> detailed_result(detailed_script->Run()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 16512 | CHECK(!detailed_result.IsEmpty()); |
| 16513 | CHECK(detailed_result->IsObject()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 16514 | } |
| 16515 | |
| 16516 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 16517 | static void StackTraceForUncaughtExceptionListener( |
| 16518 | v8::Handle<v8::Message> message, |
| 16519 | v8::Handle<Value>) { |
| 16520 | v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace(); |
| 16521 | CHECK_EQ(2, stack_trace->GetFrameCount()); |
| 16522 | checkStackFrame("origin", "foo", 2, 3, false, false, |
| 16523 | stack_trace->GetFrame(0)); |
| 16524 | checkStackFrame("origin", "bar", 5, 3, false, false, |
| 16525 | stack_trace->GetFrame(1)); |
| 16526 | } |
| 16527 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16528 | |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 16529 | TEST(CaptureStackTraceForUncaughtException) { |
| 16530 | report_count = 0; |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 16531 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16532 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 6a2b0aa | 2010-07-13 20:58:03 +0000 | [diff] [blame] | 16533 | v8::V8::AddMessageListener(StackTraceForUncaughtExceptionListener); |
| 16534 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 16535 | |
| 16536 | Script::Compile(v8_str("function foo() {\n" |
| 16537 | " throw 1;\n" |
| 16538 | "};\n" |
| 16539 | "function bar() {\n" |
| 16540 | " foo();\n" |
| 16541 | "};"), |
| 16542 | v8_str("origin"))->Run(); |
| 16543 | v8::Local<v8::Object> global = env->Global(); |
| 16544 | Local<Value> trouble = global->Get(v8_str("bar")); |
| 16545 | CHECK(trouble->IsFunction()); |
| 16546 | Function::Cast(*trouble)->Call(global, 0, NULL); |
| 16547 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16548 | v8::V8::RemoveMessageListeners(StackTraceForUncaughtExceptionListener); |
| 16549 | } |
| 16550 | |
| 16551 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 16552 | TEST(CaptureStackTraceForUncaughtExceptionAndSetters) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 16553 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16554 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 16555 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true, |
| 16556 | 1024, |
| 16557 | v8::StackTrace::kDetailed); |
| 16558 | |
| 16559 | CompileRun( |
| 16560 | "var setters = ['column', 'lineNumber', 'scriptName',\n" |
| 16561 | " 'scriptNameOrSourceURL', 'functionName', 'isEval',\n" |
| 16562 | " 'isConstructor'];\n" |
| 16563 | "for (var i = 0; i < setters.length; i++) {\n" |
| 16564 | " var prop = setters[i];\n" |
| 16565 | " Object.prototype.__defineSetter__(prop, function() { throw prop; });\n" |
| 16566 | "}\n"); |
| 16567 | CompileRun("throw 'exception';"); |
| 16568 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16569 | } |
| 16570 | |
| 16571 | |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16572 | static void RethrowStackTraceHandler(v8::Handle<v8::Message> message, |
| 16573 | v8::Handle<v8::Value> data) { |
| 16574 | // Use the frame where JavaScript is called from. |
| 16575 | v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace(); |
| 16576 | CHECK(!stack_trace.IsEmpty()); |
| 16577 | int frame_count = stack_trace->GetFrameCount(); |
| 16578 | CHECK_EQ(3, frame_count); |
| 16579 | int line_number[] = {1, 2, 5}; |
| 16580 | for (int i = 0; i < frame_count; i++) { |
| 16581 | CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber()); |
| 16582 | } |
| 16583 | } |
| 16584 | |
| 16585 | |
| 16586 | // Test that we only return the stack trace at the site where the exception |
| 16587 | // is first thrown (not where it is rethrown). |
| 16588 | TEST(RethrowStackTrace) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16589 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16590 | v8::HandleScope scope(env->GetIsolate()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16591 | // We make sure that |
| 16592 | // - the stack trace of the ReferenceError in g() is reported. |
| 16593 | // - the stack trace is not overwritten when e1 is rethrown by t(). |
| 16594 | // - the stack trace of e2 does not overwrite that of e1. |
| 16595 | const char* source = |
| 16596 | "function g() { error; } \n" |
| 16597 | "function f() { g(); } \n" |
| 16598 | "function t(e) { throw e; } \n" |
| 16599 | "try { \n" |
| 16600 | " f(); \n" |
| 16601 | "} catch (e1) { \n" |
| 16602 | " try { \n" |
| 16603 | " error; \n" |
| 16604 | " } catch (e2) { \n" |
| 16605 | " t(e1); \n" |
| 16606 | " } \n" |
| 16607 | "} \n"; |
| 16608 | v8::V8::AddMessageListener(RethrowStackTraceHandler); |
| 16609 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 16610 | CompileRun(source); |
| 16611 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16612 | v8::V8::RemoveMessageListeners(RethrowStackTraceHandler); |
| 16613 | } |
| 16614 | |
| 16615 | |
| 16616 | static void RethrowPrimitiveStackTraceHandler(v8::Handle<v8::Message> message, |
| 16617 | v8::Handle<v8::Value> data) { |
| 16618 | v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace(); |
| 16619 | CHECK(!stack_trace.IsEmpty()); |
| 16620 | int frame_count = stack_trace->GetFrameCount(); |
| 16621 | CHECK_EQ(2, frame_count); |
| 16622 | int line_number[] = {3, 7}; |
| 16623 | for (int i = 0; i < frame_count; i++) { |
| 16624 | CHECK_EQ(line_number[i], stack_trace->GetFrame(i)->GetLineNumber()); |
| 16625 | } |
| 16626 | } |
| 16627 | |
| 16628 | |
| 16629 | // Test that we do not recognize identity for primitive exceptions. |
| 16630 | TEST(RethrowPrimitiveStackTrace) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16631 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16632 | v8::HandleScope scope(env->GetIsolate()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16633 | // We do not capture stack trace for non Error objects on creation time. |
| 16634 | // Instead, we capture the stack trace on last throw. |
| 16635 | const char* source = |
| 16636 | "function g() { throw 404; } \n" |
| 16637 | "function f() { g(); } \n" |
| 16638 | "function t(e) { throw e; } \n" |
| 16639 | "try { \n" |
| 16640 | " f(); \n" |
| 16641 | "} catch (e1) { \n" |
| 16642 | " t(e1) \n" |
| 16643 | "} \n"; |
| 16644 | v8::V8::AddMessageListener(RethrowPrimitiveStackTraceHandler); |
| 16645 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 16646 | CompileRun(source); |
| 16647 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16648 | v8::V8::RemoveMessageListeners(RethrowPrimitiveStackTraceHandler); |
| 16649 | } |
| 16650 | |
| 16651 | |
| 16652 | static void RethrowExistingStackTraceHandler(v8::Handle<v8::Message> message, |
| 16653 | v8::Handle<v8::Value> data) { |
| 16654 | // Use the frame where JavaScript is called from. |
| 16655 | v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace(); |
| 16656 | CHECK(!stack_trace.IsEmpty()); |
| 16657 | CHECK_EQ(1, stack_trace->GetFrameCount()); |
| 16658 | CHECK_EQ(1, stack_trace->GetFrame(0)->GetLineNumber()); |
| 16659 | } |
| 16660 | |
| 16661 | |
| 16662 | // Test that the stack trace is captured when the error object is created and |
| 16663 | // not where it is thrown. |
| 16664 | TEST(RethrowExistingStackTrace) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16665 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16666 | v8::HandleScope scope(env->GetIsolate()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16667 | const char* source = |
| 16668 | "var e = new Error(); \n" |
| 16669 | "throw e; \n"; |
| 16670 | v8::V8::AddMessageListener(RethrowExistingStackTraceHandler); |
| 16671 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 16672 | CompileRun(source); |
| 16673 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16674 | v8::V8::RemoveMessageListeners(RethrowExistingStackTraceHandler); |
| 16675 | } |
| 16676 | |
| 16677 | |
| 16678 | static void RethrowBogusErrorStackTraceHandler(v8::Handle<v8::Message> message, |
| 16679 | v8::Handle<v8::Value> data) { |
| 16680 | // Use the frame where JavaScript is called from. |
| 16681 | v8::Handle<v8::StackTrace> stack_trace = message->GetStackTrace(); |
| 16682 | CHECK(!stack_trace.IsEmpty()); |
| 16683 | CHECK_EQ(1, stack_trace->GetFrameCount()); |
| 16684 | CHECK_EQ(2, stack_trace->GetFrame(0)->GetLineNumber()); |
| 16685 | } |
| 16686 | |
| 16687 | |
| 16688 | // Test that the stack trace is captured where the bogus Error object is thrown. |
| 16689 | TEST(RethrowBogusErrorStackTrace) { |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16690 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16691 | v8::HandleScope scope(env->GetIsolate()); |
jkummerow@chromium.org | ab7dad4 | 2012-02-07 12:07:34 +0000 | [diff] [blame] | 16692 | const char* source = |
| 16693 | "var e = {__proto__: new Error()} \n" |
| 16694 | "throw e; \n"; |
| 16695 | v8::V8::AddMessageListener(RethrowBogusErrorStackTraceHandler); |
| 16696 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(true); |
| 16697 | CompileRun(source); |
| 16698 | v8::V8::SetCaptureStackTraceForUncaughtExceptions(false); |
| 16699 | v8::V8::RemoveMessageListeners(RethrowBogusErrorStackTraceHandler); |
| 16700 | } |
| 16701 | |
| 16702 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16703 | void AnalyzeStackOfEvalWithSourceURL( |
| 16704 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16705 | v8::HandleScope scope(args.GetIsolate()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 16706 | v8::Handle<v8::StackTrace> stackTrace = |
| 16707 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); |
| 16708 | CHECK_EQ(5, stackTrace->GetFrameCount()); |
| 16709 | v8::Handle<v8::String> url = v8_str("eval_url"); |
| 16710 | for (int i = 0; i < 3; i++) { |
| 16711 | v8::Handle<v8::String> name = |
| 16712 | stackTrace->GetFrame(i)->GetScriptNameOrSourceURL(); |
| 16713 | CHECK(!name.IsEmpty()); |
| 16714 | CHECK_EQ(url, name); |
| 16715 | } |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 16716 | } |
| 16717 | |
| 16718 | |
| 16719 | TEST(SourceURLInStackTrace) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16720 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 16721 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 16722 | templ->Set(v8_str("AnalyzeStackOfEvalWithSourceURL"), |
| 16723 | v8::FunctionTemplate::New(AnalyzeStackOfEvalWithSourceURL)); |
| 16724 | LocalContext context(0, templ); |
| 16725 | |
| 16726 | const char *source = |
| 16727 | "function outer() {\n" |
| 16728 | "function bar() {\n" |
| 16729 | " AnalyzeStackOfEvalWithSourceURL();\n" |
| 16730 | "}\n" |
| 16731 | "function foo() {\n" |
| 16732 | "\n" |
| 16733 | " bar();\n" |
| 16734 | "}\n" |
| 16735 | "foo();\n" |
| 16736 | "}\n" |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 16737 | "eval('(' + outer +')()%s');"; |
| 16738 | |
| 16739 | i::ScopedVector<char> code(1024); |
| 16740 | i::OS::SNPrintF(code, source, "//# sourceURL=eval_url"); |
| 16741 | CHECK(CompileRun(code.start())->IsUndefined()); |
| 16742 | i::OS::SNPrintF(code, source, "//@ sourceURL=eval_url"); |
| 16743 | CHECK(CompileRun(code.start())->IsUndefined()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 16744 | } |
| 16745 | |
| 16746 | |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 16747 | static int scriptIdInStack[2]; |
| 16748 | |
| 16749 | void AnalyzeScriptIdInStack( |
| 16750 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 16751 | v8::HandleScope scope(args.GetIsolate()); |
| 16752 | v8::Handle<v8::StackTrace> stackTrace = |
| 16753 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kScriptId); |
| 16754 | CHECK_EQ(2, stackTrace->GetFrameCount()); |
| 16755 | for (int i = 0; i < 2; i++) { |
| 16756 | scriptIdInStack[i] = stackTrace->GetFrame(i)->GetScriptId(); |
| 16757 | } |
| 16758 | } |
| 16759 | |
| 16760 | |
| 16761 | TEST(ScriptIdInStackTrace) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16762 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 8fa5bd9 | 2013-09-02 11:45:09 +0000 | [diff] [blame] | 16763 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 16764 | templ->Set(v8_str("AnalyzeScriptIdInStack"), |
| 16765 | v8::FunctionTemplate::New(AnalyzeScriptIdInStack)); |
| 16766 | LocalContext context(0, templ); |
| 16767 | |
| 16768 | v8::Handle<v8::String> scriptSource = v8::String::New( |
| 16769 | "function foo() {\n" |
| 16770 | " AnalyzeScriptIdInStack();" |
| 16771 | "}\n" |
| 16772 | "foo();\n"); |
| 16773 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 16774 | v8::Local<v8::Script> script(v8::Script::Compile(scriptSource, &origin)); |
| 16775 | script->Run(); |
| 16776 | for (int i = 0; i < 2; i++) { |
| 16777 | CHECK(scriptIdInStack[i] != v8::Message::kNoScriptIdInfo); |
| 16778 | CHECK_EQ(scriptIdInStack[i], script->GetId()); |
| 16779 | } |
| 16780 | } |
| 16781 | |
| 16782 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16783 | void AnalyzeStackOfInlineScriptWithSourceURL( |
| 16784 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16785 | v8::HandleScope scope(args.GetIsolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16786 | v8::Handle<v8::StackTrace> stackTrace = |
| 16787 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); |
| 16788 | CHECK_EQ(4, stackTrace->GetFrameCount()); |
| 16789 | v8::Handle<v8::String> url = v8_str("url"); |
| 16790 | for (int i = 0; i < 3; i++) { |
| 16791 | v8::Handle<v8::String> name = |
| 16792 | stackTrace->GetFrame(i)->GetScriptNameOrSourceURL(); |
| 16793 | CHECK(!name.IsEmpty()); |
| 16794 | CHECK_EQ(url, name); |
| 16795 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16796 | } |
| 16797 | |
| 16798 | |
| 16799 | TEST(InlineScriptWithSourceURLInStackTrace) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16800 | v8::HandleScope scope(CcTest::isolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16801 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 16802 | templ->Set(v8_str("AnalyzeStackOfInlineScriptWithSourceURL"), |
| 16803 | v8::FunctionTemplate::New( |
| 16804 | AnalyzeStackOfInlineScriptWithSourceURL)); |
| 16805 | LocalContext context(0, templ); |
| 16806 | |
| 16807 | const char *source = |
| 16808 | "function outer() {\n" |
| 16809 | "function bar() {\n" |
| 16810 | " AnalyzeStackOfInlineScriptWithSourceURL();\n" |
| 16811 | "}\n" |
| 16812 | "function foo() {\n" |
| 16813 | "\n" |
| 16814 | " bar();\n" |
| 16815 | "}\n" |
| 16816 | "foo();\n" |
| 16817 | "}\n" |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 16818 | "outer()\n%s"; |
| 16819 | |
| 16820 | i::ScopedVector<char> code(1024); |
| 16821 | i::OS::SNPrintF(code, source, "//# sourceURL=source_url"); |
| 16822 | CHECK(CompileRunWithOrigin(code.start(), "url", 0, 1)->IsUndefined()); |
| 16823 | i::OS::SNPrintF(code, source, "//@ sourceURL=source_url"); |
| 16824 | CHECK(CompileRunWithOrigin(code.start(), "url", 0, 1)->IsUndefined()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16825 | } |
| 16826 | |
| 16827 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16828 | void AnalyzeStackOfDynamicScriptWithSourceURL( |
| 16829 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16830 | v8::HandleScope scope(args.GetIsolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16831 | v8::Handle<v8::StackTrace> stackTrace = |
| 16832 | v8::StackTrace::CurrentStackTrace(10, v8::StackTrace::kDetailed); |
| 16833 | CHECK_EQ(4, stackTrace->GetFrameCount()); |
| 16834 | v8::Handle<v8::String> url = v8_str("source_url"); |
| 16835 | for (int i = 0; i < 3; i++) { |
| 16836 | v8::Handle<v8::String> name = |
| 16837 | stackTrace->GetFrame(i)->GetScriptNameOrSourceURL(); |
| 16838 | CHECK(!name.IsEmpty()); |
| 16839 | CHECK_EQ(url, name); |
| 16840 | } |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16841 | } |
| 16842 | |
| 16843 | |
| 16844 | TEST(DynamicWithSourceURLInStackTrace) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16845 | v8::HandleScope scope(CcTest::isolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16846 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 16847 | templ->Set(v8_str("AnalyzeStackOfDynamicScriptWithSourceURL"), |
| 16848 | v8::FunctionTemplate::New( |
| 16849 | AnalyzeStackOfDynamicScriptWithSourceURL)); |
| 16850 | LocalContext context(0, templ); |
| 16851 | |
| 16852 | const char *source = |
| 16853 | "function outer() {\n" |
| 16854 | "function bar() {\n" |
| 16855 | " AnalyzeStackOfDynamicScriptWithSourceURL();\n" |
| 16856 | "}\n" |
| 16857 | "function foo() {\n" |
| 16858 | "\n" |
| 16859 | " bar();\n" |
| 16860 | "}\n" |
| 16861 | "foo();\n" |
| 16862 | "}\n" |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 16863 | "outer()\n%s"; |
| 16864 | |
| 16865 | i::ScopedVector<char> code(1024); |
| 16866 | i::OS::SNPrintF(code, source, "//# sourceURL=source_url"); |
| 16867 | CHECK(CompileRunWithOrigin(code.start(), "url", 0, 0)->IsUndefined()); |
| 16868 | i::OS::SNPrintF(code, source, "//@ sourceURL=source_url"); |
| 16869 | CHECK(CompileRunWithOrigin(code.start(), "url", 0, 0)->IsUndefined()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 16870 | } |
| 16871 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16872 | |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16873 | static void CreateGarbageInOldSpace() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16874 | i::Factory* factory = CcTest::i_isolate()->factory(); |
| 16875 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16876 | i::AlwaysAllocateScope always_allocate; |
| 16877 | for (int i = 0; i < 1000; i++) { |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 16878 | factory->NewFixedArray(1000, i::TENURED); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 16879 | } |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 16880 | } |
| 16881 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 16882 | |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 16883 | // Test that idle notification can be handled and eventually returns true. |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16884 | TEST(IdleNotification) { |
| 16885 | const intptr_t MB = 1024 * 1024; |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16886 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16887 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16888 | intptr_t initial_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16889 | CreateGarbageInOldSpace(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16890 | intptr_t size_with_garbage = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16891 | CHECK_GT(size_with_garbage, initial_size + MB); |
| 16892 | bool finished = false; |
| 16893 | for (int i = 0; i < 200 && !finished; i++) { |
| 16894 | finished = v8::V8::IdleNotification(); |
| 16895 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16896 | intptr_t final_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16897 | CHECK(finished); |
| 16898 | CHECK_LT(final_size, initial_size + 1); |
| 16899 | } |
| 16900 | |
| 16901 | |
| 16902 | // Test that idle notification can be handled and eventually collects garbage. |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 16903 | TEST(IdleNotificationWithSmallHint) { |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16904 | const intptr_t MB = 1024 * 1024; |
| 16905 | const int IdlePauseInMs = 900; |
svenpanne@chromium.org | ecb9dd6 | 2011-12-01 08:22:35 +0000 | [diff] [blame] | 16906 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16907 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16908 | intptr_t initial_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16909 | CreateGarbageInOldSpace(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16910 | intptr_t size_with_garbage = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16911 | CHECK_GT(size_with_garbage, initial_size + MB); |
| 16912 | bool finished = false; |
| 16913 | for (int i = 0; i < 200 && !finished; i++) { |
| 16914 | finished = v8::V8::IdleNotification(IdlePauseInMs); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 16915 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16916 | intptr_t final_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16917 | CHECK(finished); |
| 16918 | CHECK_LT(final_size, initial_size + 1); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 16919 | } |
| 16920 | |
| 16921 | |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16922 | // Test that idle notification can be handled and eventually collects garbage. |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 16923 | TEST(IdleNotificationWithLargeHint) { |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16924 | const intptr_t MB = 1024 * 1024; |
| 16925 | const int IdlePauseInMs = 900; |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 16926 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16927 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16928 | intptr_t initial_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16929 | CreateGarbageInOldSpace(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16930 | intptr_t size_with_garbage = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16931 | CHECK_GT(size_with_garbage, initial_size + MB); |
| 16932 | bool finished = false; |
| 16933 | for (int i = 0; i < 200 && !finished; i++) { |
| 16934 | finished = v8::V8::IdleNotification(IdlePauseInMs); |
yangguo@chromium.org | 5645471 | 2012-02-16 15:33:53 +0000 | [diff] [blame] | 16935 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16936 | intptr_t final_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16937 | CHECK(finished); |
| 16938 | CHECK_LT(final_size, initial_size + 1); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 16939 | } |
| 16940 | |
| 16941 | |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16942 | TEST(Regress2107) { |
| 16943 | const intptr_t MB = 1024 * 1024; |
| 16944 | const int kShortIdlePauseInMs = 100; |
| 16945 | const int kLongIdlePauseInMs = 1000; |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16946 | LocalContext env; |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16947 | v8::Isolate* isolate = env->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 16948 | v8::HandleScope scope(env->GetIsolate()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16949 | intptr_t initial_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16950 | // Send idle notification to start a round of incremental GCs. |
| 16951 | v8::V8::IdleNotification(kShortIdlePauseInMs); |
| 16952 | // Emulate 7 page reloads. |
| 16953 | for (int i = 0; i < 7; i++) { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 16954 | { |
| 16955 | v8::HandleScope inner_scope(env->GetIsolate()); |
| 16956 | v8::Local<v8::Context> ctx = v8::Context::New(isolate); |
| 16957 | ctx->Enter(); |
| 16958 | CreateGarbageInOldSpace(); |
| 16959 | ctx->Exit(); |
| 16960 | } |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16961 | v8::V8::ContextDisposedNotification(); |
| 16962 | v8::V8::IdleNotification(kLongIdlePauseInMs); |
| 16963 | } |
| 16964 | // Create garbage and check that idle notification still collects it. |
| 16965 | CreateGarbageInOldSpace(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16966 | intptr_t size_with_garbage = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16967 | CHECK_GT(size_with_garbage, initial_size + MB); |
| 16968 | bool finished = false; |
| 16969 | for (int i = 0; i < 200 && !finished; i++) { |
| 16970 | finished = v8::V8::IdleNotification(kShortIdlePauseInMs); |
| 16971 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16972 | intptr_t final_size = CcTest::heap()->SizeOfObjects(); |
danno@chromium.org | 2c26cb1 | 2012-05-03 09:06:43 +0000 | [diff] [blame] | 16973 | CHECK_LT(final_size, initial_size + 1); |
| 16974 | } |
| 16975 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 16976 | static uint32_t* stack_limit; |
| 16977 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 16978 | static void GetStackLimitCallback( |
| 16979 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 16980 | stack_limit = reinterpret_cast<uint32_t*>( |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 16981 | CcTest::i_isolate()->stack_guard()->real_climit()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 16982 | } |
| 16983 | |
| 16984 | |
| 16985 | // Uses the address of a local variable to determine the stack top now. |
| 16986 | // Given a size, returns an address that is that far from the current |
| 16987 | // top of stack. |
| 16988 | static uint32_t* ComputeStackLimit(uint32_t size) { |
| 16989 | uint32_t* answer = &size - (size / sizeof(size)); |
| 16990 | // If the size is very large and the stack is very near the bottom of |
| 16991 | // memory then the calculation above may wrap around and give an address |
| 16992 | // that is above the (downwards-growing) stack. In that case we return |
| 16993 | // a very low address. |
| 16994 | if (answer > &size) return reinterpret_cast<uint32_t*>(sizeof(size)); |
| 16995 | return answer; |
| 16996 | } |
| 16997 | |
| 16998 | |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 16999 | // We need at least 165kB for an x64 debug build with clang and ASAN. |
| 17000 | static const int stack_breathing_room = 256 * i::KB; |
| 17001 | |
| 17002 | |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17003 | TEST(SetResourceConstraints) { |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 17004 | uint32_t* set_limit = ComputeStackLimit(stack_breathing_room); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17005 | |
| 17006 | // Set stack limit. |
| 17007 | v8::ResourceConstraints constraints; |
| 17008 | constraints.set_stack_limit(set_limit); |
| 17009 | CHECK(v8::SetResourceConstraints(&constraints)); |
| 17010 | |
| 17011 | // Execute a script. |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17012 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17013 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17014 | Local<v8::FunctionTemplate> fun_templ = |
| 17015 | v8::FunctionTemplate::New(GetStackLimitCallback); |
| 17016 | Local<Function> fun = fun_templ->GetFunction(); |
| 17017 | env->Global()->Set(v8_str("get_stack_limit"), fun); |
| 17018 | CompileRun("get_stack_limit();"); |
| 17019 | |
| 17020 | CHECK(stack_limit == set_limit); |
| 17021 | } |
| 17022 | |
| 17023 | |
| 17024 | TEST(SetResourceConstraintsInThread) { |
| 17025 | uint32_t* set_limit; |
| 17026 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17027 | v8::Locker locker(CcTest::isolate()); |
jkummerow@chromium.org | 4e308cf | 2013-05-17 13:39:16 +0000 | [diff] [blame] | 17028 | set_limit = ComputeStackLimit(stack_breathing_room); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17029 | |
| 17030 | // Set stack limit. |
| 17031 | v8::ResourceConstraints constraints; |
| 17032 | constraints.set_stack_limit(set_limit); |
| 17033 | CHECK(v8::SetResourceConstraints(&constraints)); |
| 17034 | |
| 17035 | // Execute a script. |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17036 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17037 | LocalContext env; |
| 17038 | Local<v8::FunctionTemplate> fun_templ = |
| 17039 | v8::FunctionTemplate::New(GetStackLimitCallback); |
| 17040 | Local<Function> fun = fun_templ->GetFunction(); |
| 17041 | env->Global()->Set(v8_str("get_stack_limit"), fun); |
| 17042 | CompileRun("get_stack_limit();"); |
| 17043 | |
| 17044 | CHECK(stack_limit == set_limit); |
| 17045 | } |
| 17046 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17047 | v8::Locker locker(CcTest::isolate()); |
sgjesse@chromium.org | c514574 | 2009-10-07 09:00:33 +0000 | [diff] [blame] | 17048 | CHECK(stack_limit == set_limit); |
| 17049 | } |
ager@chromium.org | 96c75b5 | 2009-08-26 09:13:16 +0000 | [diff] [blame] | 17050 | } |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17051 | |
| 17052 | |
| 17053 | THREADED_TEST(GetHeapStatistics) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17054 | LocalContext c1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17055 | v8::HandleScope scope(c1->GetIsolate()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17056 | v8::HeapStatistics heap_statistics; |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 17057 | CHECK_EQ(static_cast<int>(heap_statistics.total_heap_size()), 0); |
| 17058 | CHECK_EQ(static_cast<int>(heap_statistics.used_heap_size()), 0); |
hpayer@chromium.org | 7c3372b | 2013-02-13 17:26:04 +0000 | [diff] [blame] | 17059 | c1->GetIsolate()->GetHeapStatistics(&heap_statistics); |
ager@chromium.org | c4c9272 | 2009-11-18 14:12:51 +0000 | [diff] [blame] | 17060 | CHECK_NE(static_cast<int>(heap_statistics.total_heap_size()), 0); |
| 17061 | CHECK_NE(static_cast<int>(heap_statistics.used_heap_size()), 0); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17062 | } |
| 17063 | |
| 17064 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17065 | class VisitorImpl : public v8::ExternalResourceVisitor { |
| 17066 | public: |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17067 | explicit VisitorImpl(TestResource** resource) { |
| 17068 | for (int i = 0; i < 4; i++) { |
| 17069 | resource_[i] = resource[i]; |
| 17070 | found_resource_[i] = false; |
| 17071 | } |
| 17072 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17073 | virtual ~VisitorImpl() {} |
| 17074 | virtual void VisitExternalString(v8::Handle<v8::String> string) { |
| 17075 | if (!string->IsExternal()) { |
| 17076 | CHECK(string->IsExternalAscii()); |
| 17077 | return; |
| 17078 | } |
| 17079 | v8::String::ExternalStringResource* resource = |
| 17080 | string->GetExternalStringResource(); |
| 17081 | CHECK(resource); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17082 | for (int i = 0; i < 4; i++) { |
| 17083 | if (resource_[i] == resource) { |
| 17084 | CHECK(!found_resource_[i]); |
| 17085 | found_resource_[i] = true; |
| 17086 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17087 | } |
| 17088 | } |
| 17089 | void CheckVisitedResources() { |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17090 | for (int i = 0; i < 4; i++) { |
| 17091 | CHECK(found_resource_[i]); |
| 17092 | } |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17093 | } |
| 17094 | |
| 17095 | private: |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17096 | v8::String::ExternalStringResource* resource_[4]; |
| 17097 | bool found_resource_[4]; |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17098 | }; |
| 17099 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17100 | |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17101 | TEST(VisitExternalStrings) { |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17102 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17103 | v8::HandleScope scope(env->GetIsolate()); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17104 | const char* string = "Some string"; |
| 17105 | uint16_t* two_byte_string = AsciiToTwoByteString(string); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17106 | TestResource* resource[4]; |
| 17107 | resource[0] = new TestResource(two_byte_string); |
| 17108 | v8::Local<v8::String> string0 = v8::String::NewExternal(resource[0]); |
| 17109 | resource[1] = new TestResource(two_byte_string); |
| 17110 | v8::Local<v8::String> string1 = v8::String::NewExternal(resource[1]); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17111 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17112 | // Externalized symbol. |
| 17113 | resource[2] = new TestResource(two_byte_string); |
| 17114 | v8::Local<v8::String> string2 = v8::String::NewSymbol(string); |
| 17115 | CHECK(string2->MakeExternal(resource[2])); |
| 17116 | |
| 17117 | // Symbolized External. |
| 17118 | resource[3] = new TestResource(AsciiToTwoByteString("Some other string")); |
| 17119 | v8::Local<v8::String> string3 = v8::String::NewExternal(resource[3]); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17120 | CcTest::heap()->CollectAllAvailableGarbage(); // Tenure string. |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17121 | // Turn into a symbol. |
| 17122 | i::Handle<i::String> string3_i = v8::Utils::OpenHandle(*string3); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17123 | CHECK(!CcTest::heap()->InternalizeString(*string3_i)->IsFailure()); |
yangguo@chromium.org | 4a9f655 | 2013-03-04 14:46:33 +0000 | [diff] [blame] | 17124 | CHECK(string3_i->IsInternalizedString()); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17125 | |
| 17126 | // We need to add usages for string* to avoid warnings in GCC 4.7 |
| 17127 | CHECK(string0->IsExternal()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 17128 | CHECK(string1->IsExternal()); |
| 17129 | CHECK(string2->IsExternal()); |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17130 | CHECK(string3->IsExternal()); |
ulan@chromium.org | 2efb900 | 2012-01-19 15:36:35 +0000 | [diff] [blame] | 17131 | |
verwaest@chromium.org | e4ee6de | 2012-11-06 12:13:00 +0000 | [diff] [blame] | 17132 | VisitorImpl visitor(resource); |
erik.corry@gmail.com | f2038fb | 2012-01-16 11:42:08 +0000 | [diff] [blame] | 17133 | v8::V8::VisitExternalResources(&visitor); |
| 17134 | visitor.CheckVisitedResources(); |
| 17135 | } |
| 17136 | |
| 17137 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17138 | static double DoubleFromBits(uint64_t value) { |
| 17139 | double target; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 17140 | i::OS::MemCopy(&target, &value, sizeof(target)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17141 | return target; |
| 17142 | } |
| 17143 | |
| 17144 | |
| 17145 | static uint64_t DoubleToBits(double value) { |
| 17146 | uint64_t target; |
mstarzinger@chromium.org | e27d617 | 2013-04-17 11:51:44 +0000 | [diff] [blame] | 17147 | i::OS::MemCopy(&target, &value, sizeof(target)); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17148 | return target; |
| 17149 | } |
| 17150 | |
| 17151 | |
| 17152 | static double DoubleToDateTime(double input) { |
| 17153 | double date_limit = 864e13; |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 17154 | if (std::isnan(input) || input < -date_limit || input > date_limit) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17155 | return i::OS::nan_value(); |
| 17156 | } |
| 17157 | return (input < 0) ? -(floor(-input)) : floor(input); |
| 17158 | } |
| 17159 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17160 | |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17161 | // We don't have a consistent way to write 64-bit constants syntactically, so we |
| 17162 | // split them into two 32-bit constants and combine them programmatically. |
| 17163 | static double DoubleFromBits(uint32_t high_bits, uint32_t low_bits) { |
| 17164 | return DoubleFromBits((static_cast<uint64_t>(high_bits) << 32) | low_bits); |
| 17165 | } |
| 17166 | |
| 17167 | |
| 17168 | THREADED_TEST(QuietSignalingNaNs) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17169 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17170 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17171 | v8::TryCatch try_catch; |
| 17172 | |
| 17173 | // Special double values. |
| 17174 | double snan = DoubleFromBits(0x7ff00000, 0x00000001); |
| 17175 | double qnan = DoubleFromBits(0x7ff80000, 0x00000000); |
| 17176 | double infinity = DoubleFromBits(0x7ff00000, 0x00000000); |
| 17177 | double max_normal = DoubleFromBits(0x7fefffff, 0xffffffffu); |
| 17178 | double min_normal = DoubleFromBits(0x00100000, 0x00000000); |
| 17179 | double max_denormal = DoubleFromBits(0x000fffff, 0xffffffffu); |
| 17180 | double min_denormal = DoubleFromBits(0x00000000, 0x00000001); |
| 17181 | |
| 17182 | // Date values are capped at +/-100000000 days (times 864e5 ms per day) |
| 17183 | // on either side of the epoch. |
| 17184 | double date_limit = 864e13; |
| 17185 | |
| 17186 | double test_values[] = { |
| 17187 | snan, |
| 17188 | qnan, |
| 17189 | infinity, |
| 17190 | max_normal, |
| 17191 | date_limit + 1, |
| 17192 | date_limit, |
| 17193 | min_normal, |
| 17194 | max_denormal, |
| 17195 | min_denormal, |
| 17196 | 0, |
| 17197 | -0, |
| 17198 | -min_denormal, |
| 17199 | -max_denormal, |
| 17200 | -min_normal, |
| 17201 | -date_limit, |
| 17202 | -date_limit - 1, |
| 17203 | -max_normal, |
| 17204 | -infinity, |
| 17205 | -qnan, |
| 17206 | -snan |
| 17207 | }; |
| 17208 | int num_test_values = 20; |
| 17209 | |
| 17210 | for (int i = 0; i < num_test_values; i++) { |
| 17211 | double test_value = test_values[i]; |
| 17212 | |
| 17213 | // Check that Number::New preserves non-NaNs and quiets SNaNs. |
| 17214 | v8::Handle<v8::Value> number = v8::Number::New(test_value); |
| 17215 | double stored_number = number->NumberValue(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 17216 | if (!std::isnan(test_value)) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17217 | CHECK_EQ(test_value, stored_number); |
| 17218 | } else { |
| 17219 | uint64_t stored_bits = DoubleToBits(stored_number); |
| 17220 | // Check if quiet nan (bits 51..62 all set). |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17221 | #if defined(V8_TARGET_ARCH_MIPS) && !defined(USE_SIMULATOR) |
| 17222 | // Most significant fraction bit for quiet nan is set to 0 |
| 17223 | // on MIPS architecture. Allowed by IEEE-754. |
| 17224 | CHECK_EQ(0xffe, static_cast<int>((stored_bits >> 51) & 0xfff)); |
| 17225 | #else |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17226 | CHECK_EQ(0xfff, static_cast<int>((stored_bits >> 51) & 0xfff)); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17227 | #endif |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17228 | } |
| 17229 | |
| 17230 | // Check that Date::New preserves non-NaNs in the date range and |
| 17231 | // quiets SNaNs. |
| 17232 | v8::Handle<v8::Value> date = v8::Date::New(test_value); |
| 17233 | double expected_stored_date = DoubleToDateTime(test_value); |
| 17234 | double stored_date = date->NumberValue(); |
ulan@chromium.org | 77ca49a | 2013-04-22 09:43:56 +0000 | [diff] [blame] | 17235 | if (!std::isnan(expected_stored_date)) { |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17236 | CHECK_EQ(expected_stored_date, stored_date); |
| 17237 | } else { |
| 17238 | uint64_t stored_bits = DoubleToBits(stored_date); |
| 17239 | // Check if quiet nan (bits 51..62 all set). |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17240 | #if defined(V8_TARGET_ARCH_MIPS) && !defined(USE_SIMULATOR) |
| 17241 | // Most significant fraction bit for quiet nan is set to 0 |
| 17242 | // on MIPS architecture. Allowed by IEEE-754. |
| 17243 | CHECK_EQ(0xffe, static_cast<int>((stored_bits >> 51) & 0xfff)); |
| 17244 | #else |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17245 | CHECK_EQ(0xfff, static_cast<int>((stored_bits >> 51) & 0xfff)); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17246 | #endif |
ager@chromium.org | 3811b43 | 2009-10-28 14:53:37 +0000 | [diff] [blame] | 17247 | } |
| 17248 | } |
| 17249 | } |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 17250 | |
| 17251 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17252 | static void SpaghettiIncident( |
| 17253 | const v8::FunctionCallbackInfo<v8::Value>& args) { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17254 | v8::HandleScope scope(args.GetIsolate()); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 17255 | v8::TryCatch tc; |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 17256 | v8::Handle<v8::String> str(args[0]->ToString()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 17257 | USE(str); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 17258 | if (tc.HasCaught()) |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17259 | tc.ReThrow(); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 17260 | } |
| 17261 | |
| 17262 | |
| 17263 | // Test that an exception can be propagated down through a spaghetti |
| 17264 | // stack using ReThrow. |
| 17265 | THREADED_TEST(SpaghettiStackReThrow) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17266 | v8::HandleScope scope(CcTest::isolate()); |
christian.plesner.hansen@gmail.com | b9ce637 | 2009-11-03 11:38:18 +0000 | [diff] [blame] | 17267 | LocalContext context; |
| 17268 | context->Global()->Set( |
| 17269 | v8::String::New("s"), |
| 17270 | v8::FunctionTemplate::New(SpaghettiIncident)->GetFunction()); |
| 17271 | v8::TryCatch try_catch; |
| 17272 | CompileRun( |
| 17273 | "var i = 0;" |
| 17274 | "var o = {" |
| 17275 | " toString: function () {" |
| 17276 | " if (i == 10) {" |
| 17277 | " throw 'Hey!';" |
| 17278 | " } else {" |
| 17279 | " i++;" |
| 17280 | " return s(o);" |
| 17281 | " }" |
| 17282 | " }" |
| 17283 | "};" |
| 17284 | "s(o);"); |
| 17285 | CHECK(try_catch.HasCaught()); |
| 17286 | v8::String::Utf8Value value(try_catch.Exception()); |
| 17287 | CHECK_EQ(0, strcmp(*value, "Hey!")); |
| 17288 | } |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17289 | |
| 17290 | |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 17291 | TEST(Regress528) { |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17292 | v8::V8::Initialize(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17293 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 17294 | v8::HandleScope scope(isolate); |
| 17295 | v8::Local<Context> other_context; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17296 | int gc_count; |
| 17297 | |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17298 | // Create a context used to keep the code from aging in the compilation |
| 17299 | // cache. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 17300 | other_context = Context::New(isolate); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17301 | |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17302 | // Context-dependent context data creates reference from the compilation |
| 17303 | // cache to the global object. |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17304 | const char* source_simple = "1"; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17305 | { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 17306 | v8::HandleScope scope(isolate); |
| 17307 | v8::Local<Context> context = Context::New(isolate); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17308 | |
| 17309 | context->Enter(); |
| 17310 | Local<v8::String> obj = v8::String::New(""); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 17311 | context->SetEmbedderData(0, obj); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17312 | CompileRun(source_simple); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17313 | context->Exit(); |
| 17314 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 17315 | v8::V8::ContextDisposedNotification(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17316 | for (gc_count = 1; gc_count < 10; gc_count++) { |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17317 | other_context->Enter(); |
| 17318 | CompileRun(source_simple); |
| 17319 | other_context->Exit(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17320 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17321 | if (GetGlobalObjectsCount() == 1) break; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17322 | } |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17323 | CHECK_GE(2, gc_count); |
| 17324 | CHECK_EQ(1, GetGlobalObjectsCount()); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17325 | |
| 17326 | // Eval in a function creates reference from the compilation cache to the |
| 17327 | // global object. |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17328 | const char* source_eval = "function f(){eval('1')}; f()"; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17329 | { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 17330 | v8::HandleScope scope(isolate); |
| 17331 | v8::Local<Context> context = Context::New(isolate); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17332 | |
| 17333 | context->Enter(); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17334 | CompileRun(source_eval); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17335 | context->Exit(); |
| 17336 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 17337 | v8::V8::ContextDisposedNotification(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17338 | for (gc_count = 1; gc_count < 10; gc_count++) { |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17339 | other_context->Enter(); |
| 17340 | CompileRun(source_eval); |
| 17341 | other_context->Exit(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17342 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17343 | if (GetGlobalObjectsCount() == 1) break; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17344 | } |
sgjesse@chromium.org | ac6aa17 | 2009-12-04 12:29:05 +0000 | [diff] [blame] | 17345 | CHECK_GE(2, gc_count); |
sgjesse@chromium.org | 9818059 | 2009-12-02 08:17:28 +0000 | [diff] [blame] | 17346 | CHECK_EQ(1, GetGlobalObjectsCount()); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17347 | |
| 17348 | // Looking up the line number for an exception creates reference from the |
| 17349 | // compilation cache to the global object. |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17350 | const char* source_exception = "function f(){throw 1;} f()"; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17351 | { |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 17352 | v8::HandleScope scope(isolate); |
| 17353 | v8::Local<Context> context = Context::New(isolate); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17354 | |
| 17355 | context->Enter(); |
| 17356 | v8::TryCatch try_catch; |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17357 | CompileRun(source_exception); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17358 | CHECK(try_catch.HasCaught()); |
| 17359 | v8::Handle<v8::Message> message = try_catch.Message(); |
| 17360 | CHECK(!message.IsEmpty()); |
| 17361 | CHECK_EQ(1, message->GetLineNumber()); |
| 17362 | context->Exit(); |
| 17363 | } |
svenpanne@chromium.org | c859c4f | 2012-10-15 11:51:39 +0000 | [diff] [blame] | 17364 | v8::V8::ContextDisposedNotification(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17365 | for (gc_count = 1; gc_count < 10; gc_count++) { |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17366 | other_context->Enter(); |
| 17367 | CompileRun(source_exception); |
| 17368 | other_context->Exit(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17369 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17370 | if (GetGlobalObjectsCount() == 1) break; |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17371 | } |
ager@chromium.org | 6012123 | 2009-12-03 11:25:37 +0000 | [diff] [blame] | 17372 | CHECK_GE(2, gc_count); |
| 17373 | CHECK_EQ(1, GetGlobalObjectsCount()); |
| 17374 | |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 17375 | v8::V8::ContextDisposedNotification(); |
sgjesse@chromium.org | 499aaa5 | 2009-11-30 08:07:20 +0000 | [diff] [blame] | 17376 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17377 | |
| 17378 | |
| 17379 | THREADED_TEST(ScriptOrigin) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17380 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17381 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17382 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 17383 | v8::Handle<v8::String> script = v8::String::New( |
| 17384 | "function f() {}\n\nfunction g() {}"); |
| 17385 | v8::Script::Compile(script, &origin)->Run(); |
| 17386 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 17387 | env->Global()->Get(v8::String::New("f"))); |
| 17388 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
| 17389 | env->Global()->Get(v8::String::New("g"))); |
| 17390 | |
| 17391 | v8::ScriptOrigin script_origin_f = f->GetScriptOrigin(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 17392 | CHECK_EQ("test", *v8::String::Utf8Value(script_origin_f.ResourceName())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17393 | CHECK_EQ(0, script_origin_f.ResourceLineOffset()->Int32Value()); |
| 17394 | |
| 17395 | v8::ScriptOrigin script_origin_g = g->GetScriptOrigin(); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 17396 | CHECK_EQ("test", *v8::String::Utf8Value(script_origin_g.ResourceName())); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17397 | CHECK_EQ(0, script_origin_g.ResourceLineOffset()->Int32Value()); |
| 17398 | } |
| 17399 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17400 | |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 17401 | THREADED_TEST(FunctionGetInferredName) { |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 17402 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17403 | v8::HandleScope scope(env->GetIsolate()); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 17404 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 17405 | v8::Handle<v8::String> script = v8::String::New( |
| 17406 | "var foo = { bar : { baz : function() {}}}; var f = foo.bar.baz;"); |
| 17407 | v8::Script::Compile(script, &origin)->Run(); |
| 17408 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 17409 | env->Global()->Get(v8::String::New("f"))); |
ulan@chromium.org | 906e2fb | 2013-05-14 08:14:38 +0000 | [diff] [blame] | 17410 | CHECK_EQ("foo.bar.baz", *v8::String::Utf8Value(f->GetInferredName())); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 17411 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17412 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17413 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17414 | THREADED_TEST(ScriptLineNumber) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17415 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17416 | v8::HandleScope scope(env->GetIsolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17417 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test")); |
| 17418 | v8::Handle<v8::String> script = v8::String::New( |
| 17419 | "function f() {}\n\nfunction g() {}"); |
| 17420 | v8::Script::Compile(script, &origin)->Run(); |
| 17421 | v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast( |
| 17422 | env->Global()->Get(v8::String::New("f"))); |
| 17423 | v8::Local<v8::Function> g = v8::Local<v8::Function>::Cast( |
| 17424 | env->Global()->Get(v8::String::New("g"))); |
| 17425 | CHECK_EQ(0, f->GetScriptLineNumber()); |
| 17426 | CHECK_EQ(2, g->GetScriptLineNumber()); |
| 17427 | } |
| 17428 | |
| 17429 | |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17430 | THREADED_TEST(ScriptColumnNumber) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17431 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17432 | v8::HandleScope scope(env->GetIsolate()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17433 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"), |
| 17434 | v8::Integer::New(3), v8::Integer::New(2)); |
| 17435 | v8::Handle<v8::String> script = v8::String::New( |
| 17436 | "function foo() {}\n\n function bar() {}"); |
| 17437 | v8::Script::Compile(script, &origin)->Run(); |
| 17438 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 17439 | env->Global()->Get(v8::String::New("foo"))); |
| 17440 | v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast( |
| 17441 | env->Global()->Get(v8::String::New("bar"))); |
| 17442 | CHECK_EQ(14, foo->GetScriptColumnNumber()); |
| 17443 | CHECK_EQ(17, bar->GetScriptColumnNumber()); |
| 17444 | } |
| 17445 | |
| 17446 | |
| 17447 | THREADED_TEST(FunctionGetScriptId) { |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17448 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17449 | v8::HandleScope scope(env->GetIsolate()); |
danno@chromium.org | c612e02 | 2011-11-10 11:38:15 +0000 | [diff] [blame] | 17450 | v8::ScriptOrigin origin = v8::ScriptOrigin(v8::String::New("test"), |
| 17451 | v8::Integer::New(3), v8::Integer::New(2)); |
| 17452 | v8::Handle<v8::String> scriptSource = v8::String::New( |
| 17453 | "function foo() {}\n\n function bar() {}"); |
| 17454 | v8::Local<v8::Script> script(v8::Script::Compile(scriptSource, &origin)); |
| 17455 | script->Run(); |
| 17456 | v8::Local<v8::Function> foo = v8::Local<v8::Function>::Cast( |
| 17457 | env->Global()->Get(v8::String::New("foo"))); |
| 17458 | v8::Local<v8::Function> bar = v8::Local<v8::Function>::Cast( |
| 17459 | env->Global()->Get(v8::String::New("bar"))); |
| 17460 | CHECK_EQ(script->Id(), foo->GetScriptId()); |
| 17461 | CHECK_EQ(script->Id(), bar->GetScriptId()); |
| 17462 | } |
| 17463 | |
| 17464 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17465 | static void GetterWhichReturns42( |
| 17466 | Local<String> name, |
| 17467 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 17468 | CHECK(v8::Utils::OpenHandle(*info.This())->IsJSObject()); |
| 17469 | CHECK(v8::Utils::OpenHandle(*info.Holder())->IsJSObject()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17470 | info.GetReturnValue().Set(v8_num(42)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17471 | } |
| 17472 | |
| 17473 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17474 | static void SetterWhichSetsYOnThisTo23( |
| 17475 | Local<String> name, |
| 17476 | Local<Value> value, |
| 17477 | const v8::PropertyCallbackInfo<void>& info) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 17478 | CHECK(v8::Utils::OpenHandle(*info.This())->IsJSObject()); |
| 17479 | CHECK(v8::Utils::OpenHandle(*info.Holder())->IsJSObject()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17480 | info.This()->Set(v8_str("y"), v8_num(23)); |
| 17481 | } |
| 17482 | |
| 17483 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17484 | void FooGetInterceptor(Local<String> name, |
| 17485 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 17486 | CHECK(v8::Utils::OpenHandle(*info.This())->IsJSObject()); |
| 17487 | CHECK(v8::Utils::OpenHandle(*info.Holder())->IsJSObject()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17488 | if (!name->Equals(v8_str("foo"))) return; |
| 17489 | info.GetReturnValue().Set(v8_num(42)); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 17490 | } |
| 17491 | |
| 17492 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17493 | void FooSetInterceptor(Local<String> name, |
| 17494 | Local<Value> value, |
| 17495 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 17496 | CHECK(v8::Utils::OpenHandle(*info.This())->IsJSObject()); |
| 17497 | CHECK(v8::Utils::OpenHandle(*info.Holder())->IsJSObject()); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17498 | if (!name->Equals(v8_str("foo"))) return; |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 17499 | info.This()->Set(v8_str("y"), v8_num(23)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17500 | info.GetReturnValue().Set(v8_num(23)); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 17501 | } |
| 17502 | |
| 17503 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17504 | TEST(SetterOnConstructorPrototype) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17505 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17506 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 17507 | templ->SetAccessor(v8_str("x"), |
| 17508 | GetterWhichReturns42, |
| 17509 | SetterWhichSetsYOnThisTo23); |
| 17510 | LocalContext context; |
| 17511 | context->Global()->Set(v8_str("P"), templ->NewInstance()); |
| 17512 | CompileRun("function C1() {" |
| 17513 | " this.x = 23;" |
| 17514 | "};" |
| 17515 | "C1.prototype = P;" |
| 17516 | "function C2() {" |
| 17517 | " this.x = 23" |
| 17518 | "};" |
| 17519 | "C2.prototype = { };" |
| 17520 | "C2.prototype.__proto__ = P;"); |
| 17521 | |
| 17522 | v8::Local<v8::Script> script; |
| 17523 | script = v8::Script::Compile(v8_str("new C1();")); |
| 17524 | for (int i = 0; i < 10; i++) { |
| 17525 | v8::Handle<v8::Object> c1 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17526 | CHECK_EQ(42, c1->Get(v8_str("x"))->Int32Value()); |
| 17527 | CHECK_EQ(23, c1->Get(v8_str("y"))->Int32Value()); |
| 17528 | } |
| 17529 | |
| 17530 | script = v8::Script::Compile(v8_str("new C2();")); |
| 17531 | for (int i = 0; i < 10; i++) { |
| 17532 | v8::Handle<v8::Object> c2 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17533 | CHECK_EQ(42, c2->Get(v8_str("x"))->Int32Value()); |
| 17534 | CHECK_EQ(23, c2->Get(v8_str("y"))->Int32Value()); |
| 17535 | } |
| 17536 | } |
| 17537 | |
| 17538 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17539 | static void NamedPropertyGetterWhichReturns42( |
| 17540 | Local<String> name, |
| 17541 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 17542 | info.GetReturnValue().Set(v8_num(42)); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17543 | } |
| 17544 | |
| 17545 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17546 | static void NamedPropertySetterWhichSetsYOnThisTo23( |
| 17547 | Local<String> name, |
| 17548 | Local<Value> value, |
| 17549 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17550 | if (name->Equals(v8_str("x"))) { |
| 17551 | info.This()->Set(v8_str("y"), v8_num(23)); |
| 17552 | } |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17553 | } |
| 17554 | |
| 17555 | |
| 17556 | THREADED_TEST(InterceptorOnConstructorPrototype) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17557 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 17558 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 17559 | templ->SetNamedPropertyHandler(NamedPropertyGetterWhichReturns42, |
| 17560 | NamedPropertySetterWhichSetsYOnThisTo23); |
| 17561 | LocalContext context; |
| 17562 | context->Global()->Set(v8_str("P"), templ->NewInstance()); |
| 17563 | CompileRun("function C1() {" |
| 17564 | " this.x = 23;" |
| 17565 | "};" |
| 17566 | "C1.prototype = P;" |
| 17567 | "function C2() {" |
| 17568 | " this.x = 23" |
| 17569 | "};" |
| 17570 | "C2.prototype = { };" |
| 17571 | "C2.prototype.__proto__ = P;"); |
| 17572 | |
| 17573 | v8::Local<v8::Script> script; |
| 17574 | script = v8::Script::Compile(v8_str("new C1();")); |
| 17575 | for (int i = 0; i < 10; i++) { |
| 17576 | v8::Handle<v8::Object> c1 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17577 | CHECK_EQ(23, c1->Get(v8_str("x"))->Int32Value()); |
| 17578 | CHECK_EQ(42, c1->Get(v8_str("y"))->Int32Value()); |
| 17579 | } |
| 17580 | |
| 17581 | script = v8::Script::Compile(v8_str("new C2();")); |
| 17582 | for (int i = 0; i < 10; i++) { |
| 17583 | v8::Handle<v8::Object> c2 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17584 | CHECK_EQ(23, c2->Get(v8_str("x"))->Int32Value()); |
| 17585 | CHECK_EQ(42, c2->Get(v8_str("y"))->Int32Value()); |
| 17586 | } |
| 17587 | } |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17588 | |
| 17589 | |
danno@chromium.org | f005df6 | 2013-04-30 16:36:45 +0000 | [diff] [blame] | 17590 | TEST(Regress618) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17591 | const char* source = "function C1() {" |
| 17592 | " this.x = 23;" |
| 17593 | "};" |
| 17594 | "C1.prototype = P;"; |
| 17595 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17596 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17597 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17598 | v8::Local<v8::Script> script; |
| 17599 | |
| 17600 | // Use a simple object as prototype. |
| 17601 | v8::Local<v8::Object> prototype = v8::Object::New(); |
| 17602 | prototype->Set(v8_str("y"), v8_num(42)); |
| 17603 | context->Global()->Set(v8_str("P"), prototype); |
| 17604 | |
| 17605 | // This compile will add the code to the compilation cache. |
| 17606 | CompileRun(source); |
| 17607 | |
| 17608 | script = v8::Script::Compile(v8_str("new C1();")); |
whesse@chromium.org | 4a1fe7d | 2010-09-27 12:32:04 +0000 | [diff] [blame] | 17609 | // Allow enough iterations for the inobject slack tracking logic |
| 17610 | // to finalize instance size and install the fast construct stub. |
| 17611 | for (int i = 0; i < 256; i++) { |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 17612 | v8::Handle<v8::Object> c1 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17613 | CHECK_EQ(23, c1->Get(v8_str("x"))->Int32Value()); |
| 17614 | CHECK_EQ(42, c1->Get(v8_str("y"))->Int32Value()); |
| 17615 | } |
| 17616 | |
| 17617 | // Use an API object with accessors as prototype. |
| 17618 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 17619 | templ->SetAccessor(v8_str("x"), |
| 17620 | GetterWhichReturns42, |
| 17621 | SetterWhichSetsYOnThisTo23); |
| 17622 | context->Global()->Set(v8_str("P"), templ->NewInstance()); |
| 17623 | |
| 17624 | // This compile will get the code from the compilation cache. |
| 17625 | CompileRun(source); |
| 17626 | |
| 17627 | script = v8::Script::Compile(v8_str("new C1();")); |
| 17628 | for (int i = 0; i < 10; i++) { |
| 17629 | v8::Handle<v8::Object> c1 = v8::Handle<v8::Object>::Cast(script->Run()); |
| 17630 | CHECK_EQ(42, c1->Get(v8_str("x"))->Int32Value()); |
| 17631 | CHECK_EQ(23, c1->Get(v8_str("y"))->Int32Value()); |
| 17632 | } |
| 17633 | } |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17634 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17635 | v8::Isolate* gc_callbacks_isolate = NULL; |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17636 | int prologue_call_count = 0; |
| 17637 | int epilogue_call_count = 0; |
| 17638 | int prologue_call_count_second = 0; |
| 17639 | int epilogue_call_count_second = 0; |
| 17640 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17641 | void PrologueCallback(v8::GCType, v8::GCCallbackFlags flags) { |
| 17642 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17643 | ++prologue_call_count; |
| 17644 | } |
| 17645 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17646 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17647 | void PrologueCallback(v8::Isolate* isolate, |
| 17648 | v8::GCType, |
| 17649 | v8::GCCallbackFlags flags) { |
| 17650 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
| 17651 | CHECK_EQ(gc_callbacks_isolate, isolate); |
| 17652 | ++prologue_call_count; |
| 17653 | } |
| 17654 | |
| 17655 | |
| 17656 | void EpilogueCallback(v8::GCType, v8::GCCallbackFlags flags) { |
| 17657 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17658 | ++epilogue_call_count; |
| 17659 | } |
| 17660 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17661 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17662 | void EpilogueCallback(v8::Isolate* isolate, |
| 17663 | v8::GCType, |
| 17664 | v8::GCCallbackFlags flags) { |
| 17665 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
| 17666 | CHECK_EQ(gc_callbacks_isolate, isolate); |
| 17667 | ++epilogue_call_count; |
| 17668 | } |
| 17669 | |
| 17670 | |
| 17671 | void PrologueCallbackSecond(v8::GCType, v8::GCCallbackFlags flags) { |
| 17672 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17673 | ++prologue_call_count_second; |
| 17674 | } |
| 17675 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17676 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17677 | void PrologueCallbackSecond(v8::Isolate* isolate, |
| 17678 | v8::GCType, |
| 17679 | v8::GCCallbackFlags flags) { |
| 17680 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
| 17681 | CHECK_EQ(gc_callbacks_isolate, isolate); |
| 17682 | ++prologue_call_count_second; |
| 17683 | } |
| 17684 | |
| 17685 | |
| 17686 | void EpilogueCallbackSecond(v8::GCType, v8::GCCallbackFlags flags) { |
| 17687 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17688 | ++epilogue_call_count_second; |
| 17689 | } |
| 17690 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 17691 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17692 | void EpilogueCallbackSecond(v8::Isolate* isolate, |
| 17693 | v8::GCType, |
| 17694 | v8::GCCallbackFlags flags) { |
| 17695 | CHECK_EQ(flags, v8::kNoGCCallbackFlags); |
| 17696 | CHECK_EQ(gc_callbacks_isolate, isolate); |
| 17697 | ++epilogue_call_count_second; |
| 17698 | } |
| 17699 | |
| 17700 | |
| 17701 | TEST(GCCallbacksOld) { |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17702 | LocalContext context; |
| 17703 | |
| 17704 | v8::V8::AddGCPrologueCallback(PrologueCallback); |
| 17705 | v8::V8::AddGCEpilogueCallback(EpilogueCallback); |
| 17706 | CHECK_EQ(0, prologue_call_count); |
| 17707 | CHECK_EQ(0, epilogue_call_count); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17708 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17709 | CHECK_EQ(1, prologue_call_count); |
| 17710 | CHECK_EQ(1, epilogue_call_count); |
| 17711 | v8::V8::AddGCPrologueCallback(PrologueCallbackSecond); |
| 17712 | v8::V8::AddGCEpilogueCallback(EpilogueCallbackSecond); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17713 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17714 | CHECK_EQ(2, prologue_call_count); |
| 17715 | CHECK_EQ(2, epilogue_call_count); |
| 17716 | CHECK_EQ(1, prologue_call_count_second); |
| 17717 | CHECK_EQ(1, epilogue_call_count_second); |
| 17718 | v8::V8::RemoveGCPrologueCallback(PrologueCallback); |
| 17719 | v8::V8::RemoveGCEpilogueCallback(EpilogueCallback); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17720 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17721 | CHECK_EQ(2, prologue_call_count); |
| 17722 | CHECK_EQ(2, epilogue_call_count); |
| 17723 | CHECK_EQ(2, prologue_call_count_second); |
| 17724 | CHECK_EQ(2, epilogue_call_count_second); |
| 17725 | v8::V8::RemoveGCPrologueCallback(PrologueCallbackSecond); |
| 17726 | v8::V8::RemoveGCEpilogueCallback(EpilogueCallbackSecond); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17727 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 17728 | CHECK_EQ(2, prologue_call_count); |
| 17729 | CHECK_EQ(2, epilogue_call_count); |
| 17730 | CHECK_EQ(2, prologue_call_count_second); |
| 17731 | CHECK_EQ(2, epilogue_call_count_second); |
| 17732 | } |
| 17733 | |
| 17734 | |
| 17735 | TEST(GCCallbacks) { |
| 17736 | LocalContext context; |
| 17737 | v8::Isolate* isolate = context->GetIsolate(); |
| 17738 | gc_callbacks_isolate = isolate; |
| 17739 | isolate->AddGCPrologueCallback(PrologueCallback); |
| 17740 | isolate->AddGCEpilogueCallback(EpilogueCallback); |
| 17741 | CHECK_EQ(0, prologue_call_count); |
| 17742 | CHECK_EQ(0, epilogue_call_count); |
| 17743 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 17744 | CHECK_EQ(1, prologue_call_count); |
| 17745 | CHECK_EQ(1, epilogue_call_count); |
| 17746 | isolate->AddGCPrologueCallback(PrologueCallbackSecond); |
| 17747 | isolate->AddGCEpilogueCallback(EpilogueCallbackSecond); |
| 17748 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 17749 | CHECK_EQ(2, prologue_call_count); |
| 17750 | CHECK_EQ(2, epilogue_call_count); |
| 17751 | CHECK_EQ(1, prologue_call_count_second); |
| 17752 | CHECK_EQ(1, epilogue_call_count_second); |
| 17753 | isolate->RemoveGCPrologueCallback(PrologueCallback); |
| 17754 | isolate->RemoveGCEpilogueCallback(EpilogueCallback); |
| 17755 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
| 17756 | CHECK_EQ(2, prologue_call_count); |
| 17757 | CHECK_EQ(2, epilogue_call_count); |
| 17758 | CHECK_EQ(2, prologue_call_count_second); |
| 17759 | CHECK_EQ(2, epilogue_call_count_second); |
| 17760 | isolate->RemoveGCPrologueCallback(PrologueCallbackSecond); |
| 17761 | isolate->RemoveGCEpilogueCallback(EpilogueCallbackSecond); |
| 17762 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 17763 | CHECK_EQ(2, prologue_call_count); |
| 17764 | CHECK_EQ(2, epilogue_call_count); |
| 17765 | CHECK_EQ(2, prologue_call_count_second); |
| 17766 | CHECK_EQ(2, epilogue_call_count_second); |
| 17767 | } |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17768 | |
| 17769 | |
| 17770 | THREADED_TEST(AddToJSFunctionResultCache) { |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 17771 | i::FLAG_stress_compaction = false; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17772 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17773 | v8::HandleScope scope(CcTest::isolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17774 | |
| 17775 | LocalContext context; |
| 17776 | |
| 17777 | const char* code = |
| 17778 | "(function() {" |
| 17779 | " var key0 = 'a';" |
| 17780 | " var key1 = 'b';" |
| 17781 | " var r0 = %_GetFromCache(0, key0);" |
| 17782 | " var r1 = %_GetFromCache(0, key1);" |
| 17783 | " var r0_ = %_GetFromCache(0, key0);" |
| 17784 | " if (r0 !== r0_)" |
| 17785 | " return 'Different results for ' + key0 + ': ' + r0 + ' vs. ' + r0_;" |
| 17786 | " var r1_ = %_GetFromCache(0, key1);" |
| 17787 | " if (r1 !== r1_)" |
| 17788 | " return 'Different results for ' + key1 + ': ' + r1 + ' vs. ' + r1_;" |
| 17789 | " return 'PASSED';" |
| 17790 | "})()"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17791 | CcTest::heap()->ClearJSFunctionResultCaches(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17792 | ExpectString(code, "PASSED"); |
| 17793 | } |
| 17794 | |
| 17795 | |
| 17796 | static const int k0CacheSize = 16; |
| 17797 | |
| 17798 | THREADED_TEST(FillJSFunctionResultCache) { |
| 17799 | i::FLAG_allow_natives_syntax = true; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17800 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17801 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17802 | |
| 17803 | const char* code = |
| 17804 | "(function() {" |
| 17805 | " var k = 'a';" |
| 17806 | " var r = %_GetFromCache(0, k);" |
| 17807 | " for (var i = 0; i < 16; i++) {" |
| 17808 | " %_GetFromCache(0, 'a' + i);" |
| 17809 | " };" |
| 17810 | " if (r === %_GetFromCache(0, k))" |
| 17811 | " return 'FAILED: k0CacheSize is too small';" |
| 17812 | " return 'PASSED';" |
| 17813 | "})()"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17814 | CcTest::heap()->ClearJSFunctionResultCaches(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17815 | ExpectString(code, "PASSED"); |
| 17816 | } |
| 17817 | |
| 17818 | |
| 17819 | THREADED_TEST(RoundRobinGetFromCache) { |
| 17820 | i::FLAG_allow_natives_syntax = true; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17821 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17822 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17823 | |
| 17824 | const char* code = |
| 17825 | "(function() {" |
| 17826 | " var keys = [];" |
| 17827 | " for (var i = 0; i < 16; i++) keys.push(i);" |
| 17828 | " var values = [];" |
| 17829 | " for (var i = 0; i < 16; i++) values[i] = %_GetFromCache(0, keys[i]);" |
| 17830 | " for (var i = 0; i < 16; i++) {" |
| 17831 | " var v = %_GetFromCache(0, keys[i]);" |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 17832 | " if (v.toString() !== values[i].toString())" |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17833 | " return 'Wrong value for ' + " |
| 17834 | " keys[i] + ': ' + v + ' vs. ' + values[i];" |
| 17835 | " };" |
| 17836 | " return 'PASSED';" |
| 17837 | "})()"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17838 | CcTest::heap()->ClearJSFunctionResultCaches(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17839 | ExpectString(code, "PASSED"); |
| 17840 | } |
| 17841 | |
| 17842 | |
| 17843 | THREADED_TEST(ReverseGetFromCache) { |
| 17844 | i::FLAG_allow_natives_syntax = true; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17845 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17846 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17847 | |
| 17848 | const char* code = |
| 17849 | "(function() {" |
| 17850 | " var keys = [];" |
| 17851 | " for (var i = 0; i < 16; i++) keys.push(i);" |
| 17852 | " var values = [];" |
| 17853 | " for (var i = 0; i < 16; i++) values[i] = %_GetFromCache(0, keys[i]);" |
| 17854 | " for (var i = 15; i >= 16; i--) {" |
| 17855 | " var v = %_GetFromCache(0, keys[i]);" |
| 17856 | " if (v !== values[i])" |
| 17857 | " return 'Wrong value for ' + " |
| 17858 | " keys[i] + ': ' + v + ' vs. ' + values[i];" |
| 17859 | " };" |
| 17860 | " return 'PASSED';" |
| 17861 | "})()"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17862 | CcTest::heap()->ClearJSFunctionResultCaches(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17863 | ExpectString(code, "PASSED"); |
| 17864 | } |
| 17865 | |
| 17866 | |
| 17867 | THREADED_TEST(TestEviction) { |
| 17868 | i::FLAG_allow_natives_syntax = true; |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17869 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17870 | v8::HandleScope scope(context->GetIsolate()); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17871 | |
| 17872 | const char* code = |
| 17873 | "(function() {" |
| 17874 | " for (var i = 0; i < 2*16; i++) {" |
| 17875 | " %_GetFromCache(0, 'a' + i);" |
| 17876 | " };" |
| 17877 | " return 'PASSED';" |
| 17878 | "})()"; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17879 | CcTest::heap()->ClearJSFunctionResultCaches(); |
sgjesse@chromium.org | 720dc0b | 2010-05-10 09:25:39 +0000 | [diff] [blame] | 17880 | ExpectString(code, "PASSED"); |
| 17881 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17882 | |
| 17883 | |
| 17884 | THREADED_TEST(TwoByteStringInAsciiCons) { |
| 17885 | // See Chromium issue 47824. |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17886 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 17887 | v8::HandleScope scope(context->GetIsolate()); |
| 17888 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17889 | const char* init_code = |
| 17890 | "var str1 = 'abelspendabel';" |
| 17891 | "var str2 = str1 + str1 + str1;" |
| 17892 | "str2;"; |
| 17893 | Local<Value> result = CompileRun(init_code); |
| 17894 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 17895 | Local<Value> indexof = CompileRun("str2.indexOf('els')"); |
| 17896 | Local<Value> lastindexof = CompileRun("str2.lastIndexOf('dab')"); |
| 17897 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17898 | CHECK(result->IsString()); |
| 17899 | i::Handle<i::String> string = v8::Utils::OpenHandle(String::Cast(*result)); |
| 17900 | int length = string->length(); |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 17901 | CHECK(string->IsOneByteRepresentation()); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17902 | |
| 17903 | FlattenString(string); |
| 17904 | i::Handle<i::String> flat_string = FlattenGetString(string); |
| 17905 | |
ulan@chromium.org | 8e8d882 | 2012-11-23 14:36:46 +0000 | [diff] [blame] | 17906 | CHECK(string->IsOneByteRepresentation()); |
| 17907 | CHECK(flat_string->IsOneByteRepresentation()); |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17908 | |
| 17909 | // Create external resource. |
| 17910 | uint16_t* uc16_buffer = new uint16_t[length + 1]; |
| 17911 | |
| 17912 | i::String::WriteToFlat(*flat_string, uc16_buffer, 0, length); |
| 17913 | uc16_buffer[length] = 0; |
| 17914 | |
| 17915 | TestResource resource(uc16_buffer); |
| 17916 | |
| 17917 | flat_string->MakeExternal(&resource); |
| 17918 | |
| 17919 | CHECK(flat_string->IsTwoByteRepresentation()); |
| 17920 | |
jkummerow@chromium.org | 7bd87f0 | 2013-03-20 18:06:29 +0000 | [diff] [blame] | 17921 | // If the cons string has been short-circuited, skip the following checks. |
| 17922 | if (!string.is_identical_to(flat_string)) { |
| 17923 | // At this point, we should have a Cons string which is flat and ASCII, |
| 17924 | // with a first half that is a two-byte string (although it only contains |
| 17925 | // ASCII characters). This is a valid sequence of steps, and it can happen |
| 17926 | // in real pages. |
| 17927 | CHECK(string->IsOneByteRepresentation()); |
| 17928 | i::ConsString* cons = i::ConsString::cast(*string); |
| 17929 | CHECK_EQ(0, cons->second()->length()); |
| 17930 | CHECK(cons->first()->IsTwoByteRepresentation()); |
| 17931 | } |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17932 | |
| 17933 | // Check that some string operations work. |
| 17934 | |
| 17935 | // Atom RegExp. |
| 17936 | Local<Value> reresult = CompileRun("str2.match(/abel/g).length;"); |
| 17937 | CHECK_EQ(6, reresult->Int32Value()); |
| 17938 | |
| 17939 | // Nonatom RegExp. |
| 17940 | reresult = CompileRun("str2.match(/abe./g).length;"); |
| 17941 | CHECK_EQ(6, reresult->Int32Value()); |
| 17942 | |
| 17943 | reresult = CompileRun("str2.search(/bel/g);"); |
| 17944 | CHECK_EQ(1, reresult->Int32Value()); |
| 17945 | |
| 17946 | reresult = CompileRun("str2.search(/be./g);"); |
| 17947 | CHECK_EQ(1, reresult->Int32Value()); |
| 17948 | |
| 17949 | ExpectTrue("/bel/g.test(str2);"); |
| 17950 | |
| 17951 | ExpectTrue("/be./g.test(str2);"); |
| 17952 | |
| 17953 | reresult = CompileRun("/bel/g.exec(str2);"); |
| 17954 | CHECK(!reresult->IsNull()); |
| 17955 | |
| 17956 | reresult = CompileRun("/be./g.exec(str2);"); |
| 17957 | CHECK(!reresult->IsNull()); |
| 17958 | |
| 17959 | ExpectString("str2.substring(2, 10);", "elspenda"); |
| 17960 | |
| 17961 | ExpectString("str2.substring(2, 20);", "elspendabelabelspe"); |
| 17962 | |
| 17963 | ExpectString("str2.charAt(2);", "e"); |
| 17964 | |
ricow@chromium.org | ddd545c | 2011-08-24 12:02:41 +0000 | [diff] [blame] | 17965 | ExpectObject("str2.indexOf('els');", indexof); |
| 17966 | |
| 17967 | ExpectObject("str2.lastIndexOf('dab');", lastindexof); |
| 17968 | |
lrn@chromium.org | 32d961d | 2010-06-30 09:09:34 +0000 | [diff] [blame] | 17969 | reresult = CompileRun("str2.charCodeAt(2);"); |
| 17970 | CHECK_EQ(static_cast<int32_t>('e'), reresult->Int32Value()); |
| 17971 | } |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 17972 | |
| 17973 | |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 17974 | TEST(ContainsOnlyOneByte) { |
| 17975 | v8::V8::Initialize(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 17976 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 17977 | v8::HandleScope scope(isolate); |
| 17978 | // Make a buffer long enough that it won't automatically be converted. |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 17979 | const int length = 512; |
| 17980 | // Ensure word aligned assignment. |
| 17981 | const int aligned_length = length*sizeof(uintptr_t)/sizeof(uint16_t); |
| 17982 | i::SmartArrayPointer<uintptr_t> |
| 17983 | aligned_contents(new uintptr_t[aligned_length]); |
| 17984 | uint16_t* string_contents = reinterpret_cast<uint16_t*>(*aligned_contents); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 17985 | // Set to contain only one byte. |
| 17986 | for (int i = 0; i < length-1; i++) { |
| 17987 | string_contents[i] = 0x41; |
| 17988 | } |
| 17989 | string_contents[length-1] = 0; |
| 17990 | // Simple case. |
| 17991 | Handle<String> string; |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 17992 | string = String::NewExternal(new TestResource(string_contents)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 17993 | CHECK(!string->IsOneByte() && string->ContainsOnlyOneByte()); |
| 17994 | // Counter example. |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 17995 | string = String::NewFromTwoByte(isolate, string_contents); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 17996 | CHECK(string->IsOneByte() && string->ContainsOnlyOneByte()); |
| 17997 | // Test left right and balanced cons strings. |
| 17998 | Handle<String> base = String::NewFromUtf8(isolate, "a"); |
| 17999 | Handle<String> left = base; |
| 18000 | Handle<String> right = base; |
| 18001 | for (int i = 0; i < 1000; i++) { |
| 18002 | left = String::Concat(base, left); |
| 18003 | right = String::Concat(right, base); |
| 18004 | } |
| 18005 | Handle<String> balanced = String::Concat(left, base); |
| 18006 | balanced = String::Concat(balanced, right); |
| 18007 | Handle<String> cons_strings[] = {left, balanced, right}; |
| 18008 | Handle<String> two_byte = |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 18009 | String::NewExternal(new TestResource(string_contents)); |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 18010 | for (size_t i = 0; i < ARRAY_SIZE(cons_strings); i++) { |
| 18011 | // Base assumptions. |
| 18012 | string = cons_strings[i]; |
| 18013 | CHECK(string->IsOneByte() && string->ContainsOnlyOneByte()); |
| 18014 | // Test left and right concatentation. |
| 18015 | string = String::Concat(two_byte, cons_strings[i]); |
| 18016 | CHECK(!string->IsOneByte() && string->ContainsOnlyOneByte()); |
| 18017 | string = String::Concat(cons_strings[i], two_byte); |
| 18018 | CHECK(!string->IsOneByte() && string->ContainsOnlyOneByte()); |
| 18019 | } |
verwaest@chromium.org | 8a00e82 | 2013-06-10 15:11:22 +0000 | [diff] [blame] | 18020 | // Set bits in different positions |
| 18021 | // for strings of different lengths and alignments. |
| 18022 | for (int alignment = 0; alignment < 7; alignment++) { |
| 18023 | for (int size = 2; alignment + size < length; size *= 2) { |
| 18024 | int zero_offset = size + alignment; |
| 18025 | string_contents[zero_offset] = 0; |
| 18026 | for (int i = 0; i < size; i++) { |
| 18027 | int shift = 8 + (i % 7); |
| 18028 | string_contents[alignment + i] = 1 << shift; |
| 18029 | string = |
| 18030 | String::NewExternal(new TestResource(string_contents + alignment)); |
| 18031 | CHECK_EQ(size, string->Length()); |
| 18032 | CHECK(!string->ContainsOnlyOneByte()); |
| 18033 | string_contents[alignment + i] = 0x41; |
| 18034 | } |
| 18035 | string_contents[zero_offset] = 0x41; |
| 18036 | } |
| 18037 | } |
ulan@chromium.org | dfe5307 | 2013-06-06 14:14:51 +0000 | [diff] [blame] | 18038 | } |
| 18039 | |
| 18040 | |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 18041 | // Failed access check callback that performs a GC on each invocation. |
| 18042 | void FailedAccessCheckCallbackGC(Local<v8::Object> target, |
| 18043 | v8::AccessType type, |
| 18044 | Local<v8::Value> data) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18045 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 18046 | } |
| 18047 | |
| 18048 | |
| 18049 | TEST(GCInFailedAccessCheckCallback) { |
| 18050 | // Install a failed access check callback that performs a GC on each |
| 18051 | // invocation. Then force the callback to be called from va |
| 18052 | |
| 18053 | v8::V8::Initialize(); |
| 18054 | v8::V8::SetFailedAccessCheckCallbackFunction(&FailedAccessCheckCallbackGC); |
| 18055 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18056 | v8::HandleScope scope(CcTest::isolate()); |
ager@chromium.org | ea4f62e | 2010-08-16 16:28:43 +0000 | [diff] [blame] | 18057 | |
| 18058 | // Create an ObjectTemplate for global objects and install access |
| 18059 | // check callbacks that will block access. |
| 18060 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 18061 | global_template->SetAccessCheckCallbacks(NamedGetAccessBlocker, |
| 18062 | IndexedGetAccessBlocker, |
| 18063 | v8::Handle<v8::Value>(), |
| 18064 | false); |
| 18065 | |
| 18066 | // Create a context and set an x property on it's global object. |
| 18067 | LocalContext context0(NULL, global_template); |
| 18068 | context0->Global()->Set(v8_str("x"), v8_num(42)); |
| 18069 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 18070 | |
| 18071 | // Create a context with a different security token so that the |
| 18072 | // failed access check callback will be called on each access. |
| 18073 | LocalContext context1(NULL, global_template); |
| 18074 | context1->Global()->Set(v8_str("other"), global0); |
| 18075 | |
| 18076 | // Get property with failed access check. |
| 18077 | ExpectUndefined("other.x"); |
| 18078 | |
| 18079 | // Get element with failed access check. |
| 18080 | ExpectUndefined("other[0]"); |
| 18081 | |
| 18082 | // Set property with failed access check. |
| 18083 | v8::Handle<v8::Value> result = CompileRun("other.x = new Object()"); |
| 18084 | CHECK(result->IsObject()); |
| 18085 | |
| 18086 | // Set element with failed access check. |
| 18087 | result = CompileRun("other[0] = new Object()"); |
| 18088 | CHECK(result->IsObject()); |
| 18089 | |
| 18090 | // Get property attribute with failed access check. |
| 18091 | ExpectFalse("\'x\' in other"); |
| 18092 | |
| 18093 | // Get property attribute for element with failed access check. |
| 18094 | ExpectFalse("0 in other"); |
| 18095 | |
| 18096 | // Delete property. |
| 18097 | ExpectFalse("delete other.x"); |
| 18098 | |
| 18099 | // Delete element. |
| 18100 | CHECK_EQ(false, global0->Delete(0)); |
| 18101 | |
| 18102 | // DefineAccessor. |
| 18103 | CHECK_EQ(false, |
| 18104 | global0->SetAccessor(v8_str("x"), GetXValue, NULL, v8_str("x"))); |
| 18105 | |
| 18106 | // Define JavaScript accessor. |
| 18107 | ExpectUndefined("Object.prototype.__defineGetter__.call(" |
| 18108 | " other, \'x\', function() { return 42; })"); |
| 18109 | |
| 18110 | // LookupAccessor. |
| 18111 | ExpectUndefined("Object.prototype.__lookupGetter__.call(" |
| 18112 | " other, \'x\')"); |
| 18113 | |
| 18114 | // HasLocalElement. |
| 18115 | ExpectFalse("Object.prototype.hasOwnProperty.call(other, \'0\')"); |
| 18116 | |
| 18117 | CHECK_EQ(false, global0->HasRealIndexedProperty(0)); |
| 18118 | CHECK_EQ(false, global0->HasRealNamedProperty(v8_str("x"))); |
| 18119 | CHECK_EQ(false, global0->HasRealNamedCallbackProperty(v8_str("x"))); |
| 18120 | |
| 18121 | // Reset the failed access check callback so it does not influence |
| 18122 | // the other tests. |
| 18123 | v8::V8::SetFailedAccessCheckCallbackFunction(NULL); |
| 18124 | } |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18125 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18126 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18127 | TEST(IsolateNewDispose) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18128 | v8::Isolate* current_isolate = CcTest::isolate(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18129 | v8::Isolate* isolate = v8::Isolate::New(); |
| 18130 | CHECK(isolate != NULL); |
| 18131 | CHECK(!reinterpret_cast<i::Isolate*>(isolate)->IsDefaultIsolate()); |
| 18132 | CHECK(current_isolate != isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18133 | CHECK(current_isolate == CcTest::isolate()); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18134 | |
| 18135 | v8::V8::SetFatalErrorHandler(StoringErrorCallback); |
| 18136 | last_location = last_message = NULL; |
| 18137 | isolate->Dispose(); |
| 18138 | CHECK_EQ(last_location, NULL); |
| 18139 | CHECK_EQ(last_message, NULL); |
| 18140 | } |
| 18141 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18142 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18143 | UNINITIALIZED_TEST(DisposeIsolateWhenInUse) { |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18144 | v8::Isolate* isolate = v8::Isolate::New(); |
| 18145 | CHECK(isolate); |
| 18146 | isolate->Enter(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18147 | v8::HandleScope scope(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18148 | LocalContext context(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18149 | // Run something in this isolate. |
| 18150 | ExpectTrue("true"); |
| 18151 | v8::V8::SetFatalErrorHandler(StoringErrorCallback); |
| 18152 | last_location = last_message = NULL; |
| 18153 | // Still entered, should fail. |
| 18154 | isolate->Dispose(); |
| 18155 | CHECK_NE(last_location, NULL); |
| 18156 | CHECK_NE(last_message, NULL); |
| 18157 | } |
| 18158 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18159 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18160 | TEST(RunTwoIsolatesOnSingleThread) { |
| 18161 | // Run isolate 1. |
| 18162 | v8::Isolate* isolate1 = v8::Isolate::New(); |
| 18163 | isolate1->Enter(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18164 | v8::Persistent<v8::Context> context1; |
| 18165 | { |
| 18166 | v8::HandleScope scope(isolate1); |
| 18167 | context1.Reset(isolate1, Context::New(isolate1)); |
| 18168 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18169 | |
| 18170 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18171 | v8::HandleScope scope(isolate1); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18172 | v8::Local<v8::Context> context = |
| 18173 | v8::Local<v8::Context>::New(isolate1, context1); |
| 18174 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18175 | // Run something in new isolate. |
| 18176 | CompileRun("var foo = 'isolate 1';"); |
| 18177 | ExpectString("function f() { return foo; }; f()", "isolate 1"); |
| 18178 | } |
| 18179 | |
| 18180 | // Run isolate 2. |
| 18181 | v8::Isolate* isolate2 = v8::Isolate::New(); |
| 18182 | v8::Persistent<v8::Context> context2; |
| 18183 | |
| 18184 | { |
| 18185 | v8::Isolate::Scope iscope(isolate2); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18186 | v8::HandleScope scope(isolate2); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18187 | context2.Reset(isolate2, Context::New(isolate2)); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18188 | v8::Local<v8::Context> context = |
| 18189 | v8::Local<v8::Context>::New(isolate2, context2); |
| 18190 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18191 | |
| 18192 | // Run something in new isolate. |
| 18193 | CompileRun("var foo = 'isolate 2';"); |
| 18194 | ExpectString("function f() { return foo; }; f()", "isolate 2"); |
| 18195 | } |
| 18196 | |
| 18197 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18198 | v8::HandleScope scope(isolate1); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18199 | v8::Local<v8::Context> context = |
| 18200 | v8::Local<v8::Context>::New(isolate1, context1); |
| 18201 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18202 | // Now again in isolate 1 |
| 18203 | ExpectString("function f() { return foo; }; f()", "isolate 1"); |
| 18204 | } |
| 18205 | |
| 18206 | isolate1->Exit(); |
| 18207 | |
| 18208 | // Run some stuff in default isolate. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18209 | v8::Persistent<v8::Context> context_default; |
| 18210 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18211 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18212 | v8::Isolate::Scope iscope(isolate); |
| 18213 | v8::HandleScope scope(isolate); |
| 18214 | context_default.Reset(isolate, Context::New(isolate)); |
| 18215 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18216 | |
| 18217 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18218 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18219 | v8::Local<v8::Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18220 | v8::Local<v8::Context>::New(CcTest::isolate(), context_default); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18221 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18222 | // Variables in other isolates should be not available, verify there |
| 18223 | // is an exception. |
| 18224 | ExpectTrue("function f() {" |
| 18225 | " try {" |
| 18226 | " foo;" |
| 18227 | " return false;" |
| 18228 | " } catch(e) {" |
| 18229 | " return true;" |
| 18230 | " }" |
| 18231 | "};" |
| 18232 | "var isDefaultIsolate = true;" |
| 18233 | "f()"); |
| 18234 | } |
| 18235 | |
| 18236 | isolate1->Enter(); |
| 18237 | |
| 18238 | { |
| 18239 | v8::Isolate::Scope iscope(isolate2); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18240 | v8::HandleScope scope(isolate2); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18241 | v8::Local<v8::Context> context = |
| 18242 | v8::Local<v8::Context>::New(isolate2, context2); |
| 18243 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18244 | ExpectString("function f() { return foo; }; f()", "isolate 2"); |
| 18245 | } |
| 18246 | |
| 18247 | { |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18248 | v8::HandleScope scope(v8::Isolate::GetCurrent()); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18249 | v8::Local<v8::Context> context = |
| 18250 | v8::Local<v8::Context>::New(v8::Isolate::GetCurrent(), context1); |
| 18251 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18252 | ExpectString("function f() { return foo; }; f()", "isolate 1"); |
| 18253 | } |
| 18254 | |
| 18255 | { |
| 18256 | v8::Isolate::Scope iscope(isolate2); |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 18257 | context2.Dispose(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18258 | } |
| 18259 | |
rossberg@chromium.org | 79e7902 | 2013-06-03 15:43:46 +0000 | [diff] [blame] | 18260 | context1.Dispose(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18261 | isolate1->Exit(); |
| 18262 | |
| 18263 | v8::V8::SetFatalErrorHandler(StoringErrorCallback); |
| 18264 | last_location = last_message = NULL; |
| 18265 | |
| 18266 | isolate1->Dispose(); |
| 18267 | CHECK_EQ(last_location, NULL); |
| 18268 | CHECK_EQ(last_message, NULL); |
| 18269 | |
| 18270 | isolate2->Dispose(); |
| 18271 | CHECK_EQ(last_location, NULL); |
| 18272 | CHECK_EQ(last_message, NULL); |
| 18273 | |
| 18274 | // Check that default isolate still runs. |
| 18275 | { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18276 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18277 | v8::Local<v8::Context> context = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18278 | v8::Local<v8::Context>::New(CcTest::isolate(), context_default); |
jkummerow@chromium.org | c118402 | 2013-05-28 16:58:15 +0000 | [diff] [blame] | 18279 | v8::Context::Scope context_scope(context); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18280 | ExpectTrue("function f() { return isDefaultIsolate; }; f()"); |
| 18281 | } |
| 18282 | } |
| 18283 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18284 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18285 | static int CalcFibonacci(v8::Isolate* isolate, int limit) { |
| 18286 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18287 | v8::HandleScope scope(isolate); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18288 | LocalContext context(isolate); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18289 | i::ScopedVector<char> code(1024); |
| 18290 | i::OS::SNPrintF(code, "function fib(n) {" |
| 18291 | " if (n <= 2) return 1;" |
| 18292 | " return fib(n-1) + fib(n-2);" |
| 18293 | "}" |
| 18294 | "fib(%d)", limit); |
| 18295 | Local<Value> value = CompileRun(code.start()); |
| 18296 | CHECK(value->IsNumber()); |
| 18297 | return static_cast<int>(value->NumberValue()); |
| 18298 | } |
| 18299 | |
| 18300 | class IsolateThread : public v8::internal::Thread { |
| 18301 | public: |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 18302 | IsolateThread(v8::Isolate* isolate, int fib_limit) |
| 18303 | : Thread("IsolateThread"), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18304 | isolate_(isolate), |
| 18305 | fib_limit_(fib_limit), |
| 18306 | result_(0) { } |
| 18307 | |
| 18308 | void Run() { |
| 18309 | result_ = CalcFibonacci(isolate_, fib_limit_); |
| 18310 | } |
| 18311 | |
| 18312 | int result() { return result_; } |
| 18313 | |
| 18314 | private: |
| 18315 | v8::Isolate* isolate_; |
| 18316 | int fib_limit_; |
| 18317 | int result_; |
| 18318 | }; |
| 18319 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18320 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18321 | TEST(MultipleIsolatesOnIndividualThreads) { |
| 18322 | v8::Isolate* isolate1 = v8::Isolate::New(); |
| 18323 | v8::Isolate* isolate2 = v8::Isolate::New(); |
| 18324 | |
| 18325 | IsolateThread thread1(isolate1, 21); |
| 18326 | IsolateThread thread2(isolate2, 12); |
| 18327 | |
| 18328 | // Compute some fibonacci numbers on 3 threads in 3 isolates. |
| 18329 | thread1.Start(); |
| 18330 | thread2.Start(); |
| 18331 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18332 | int result1 = CalcFibonacci(CcTest::isolate(), 21); |
| 18333 | int result2 = CalcFibonacci(CcTest::isolate(), 12); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18334 | |
| 18335 | thread1.Join(); |
| 18336 | thread2.Join(); |
| 18337 | |
| 18338 | // Compare results. The actual fibonacci numbers for 12 and 21 are taken |
| 18339 | // (I'm lazy!) from http://en.wikipedia.org/wiki/Fibonacci_number |
| 18340 | CHECK_EQ(result1, 10946); |
| 18341 | CHECK_EQ(result2, 144); |
| 18342 | CHECK_EQ(result1, thread1.result()); |
| 18343 | CHECK_EQ(result2, thread2.result()); |
| 18344 | |
| 18345 | isolate1->Dispose(); |
| 18346 | isolate2->Dispose(); |
| 18347 | } |
| 18348 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18349 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 18350 | TEST(IsolateDifferentContexts) { |
| 18351 | v8::Isolate* isolate = v8::Isolate::New(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18352 | Local<v8::Context> context; |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 18353 | { |
| 18354 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18355 | v8::HandleScope handle_scope(isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18356 | context = v8::Context::New(isolate); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 18357 | v8::Context::Scope context_scope(context); |
| 18358 | Local<Value> v = CompileRun("2"); |
| 18359 | CHECK(v->IsNumber()); |
| 18360 | CHECK_EQ(2, static_cast<int>(v->NumberValue())); |
| 18361 | } |
| 18362 | { |
| 18363 | v8::Isolate::Scope isolate_scope(isolate); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18364 | v8::HandleScope handle_scope(isolate); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 18365 | context = v8::Context::New(isolate); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 18366 | v8::Context::Scope context_scope(context); |
| 18367 | Local<Value> v = CompileRun("22"); |
| 18368 | CHECK(v->IsNumber()); |
| 18369 | CHECK_EQ(22, static_cast<int>(v->NumberValue())); |
| 18370 | } |
| 18371 | } |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18372 | |
| 18373 | class InitDefaultIsolateThread : public v8::internal::Thread { |
| 18374 | public: |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 18375 | enum TestCase { |
| 18376 | IgnoreOOM, |
| 18377 | SetResourceConstraints, |
| 18378 | SetFatalHandler, |
| 18379 | SetCounterFunction, |
| 18380 | SetCreateHistogramFunction, |
| 18381 | SetAddHistogramSampleFunction |
| 18382 | }; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18383 | |
| 18384 | explicit InitDefaultIsolateThread(TestCase testCase) |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 18385 | : Thread("InitDefaultIsolateThread"), |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18386 | testCase_(testCase), |
| 18387 | result_(false) { } |
| 18388 | |
| 18389 | void Run() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18390 | v8::Isolate* isolate = v8::Isolate::New(); |
| 18391 | isolate->Enter(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18392 | switch (testCase_) { |
| 18393 | case IgnoreOOM: |
| 18394 | v8::V8::IgnoreOutOfMemoryException(); |
| 18395 | break; |
| 18396 | |
| 18397 | case SetResourceConstraints: { |
| 18398 | static const int K = 1024; |
| 18399 | v8::ResourceConstraints constraints; |
| 18400 | constraints.set_max_young_space_size(256 * K); |
| 18401 | constraints.set_max_old_space_size(4 * K * K); |
| 18402 | v8::SetResourceConstraints(&constraints); |
| 18403 | break; |
| 18404 | } |
| 18405 | |
| 18406 | case SetFatalHandler: |
| 18407 | v8::V8::SetFatalErrorHandler(NULL); |
| 18408 | break; |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 18409 | |
| 18410 | case SetCounterFunction: |
| 18411 | v8::V8::SetCounterFunction(NULL); |
| 18412 | break; |
| 18413 | |
| 18414 | case SetCreateHistogramFunction: |
| 18415 | v8::V8::SetCreateHistogramFunction(NULL); |
| 18416 | break; |
| 18417 | |
| 18418 | case SetAddHistogramSampleFunction: |
| 18419 | v8::V8::SetAddHistogramSampleFunction(NULL); |
| 18420 | break; |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18421 | } |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18422 | isolate->Exit(); |
| 18423 | isolate->Dispose(); |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18424 | result_ = true; |
| 18425 | } |
| 18426 | |
| 18427 | bool result() { return result_; } |
| 18428 | |
| 18429 | private: |
| 18430 | TestCase testCase_; |
| 18431 | bool result_; |
| 18432 | }; |
| 18433 | |
| 18434 | |
| 18435 | static void InitializeTestHelper(InitDefaultIsolateThread::TestCase testCase) { |
| 18436 | InitDefaultIsolateThread thread(testCase); |
| 18437 | thread.Start(); |
| 18438 | thread.Join(); |
| 18439 | CHECK_EQ(thread.result(), true); |
| 18440 | } |
| 18441 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18442 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18443 | TEST(InitializeDefaultIsolateOnSecondaryThread1) { |
| 18444 | InitializeTestHelper(InitDefaultIsolateThread::IgnoreOOM); |
| 18445 | } |
| 18446 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18447 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18448 | TEST(InitializeDefaultIsolateOnSecondaryThread2) { |
| 18449 | InitializeTestHelper(InitDefaultIsolateThread::SetResourceConstraints); |
| 18450 | } |
| 18451 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18452 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 18453 | TEST(InitializeDefaultIsolateOnSecondaryThread3) { |
| 18454 | InitializeTestHelper(InitDefaultIsolateThread::SetFatalHandler); |
| 18455 | } |
| 18456 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18457 | |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 18458 | TEST(InitializeDefaultIsolateOnSecondaryThread4) { |
| 18459 | InitializeTestHelper(InitDefaultIsolateThread::SetCounterFunction); |
| 18460 | } |
| 18461 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18462 | |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 18463 | TEST(InitializeDefaultIsolateOnSecondaryThread5) { |
| 18464 | InitializeTestHelper(InitDefaultIsolateThread::SetCreateHistogramFunction); |
| 18465 | } |
| 18466 | |
mstarzinger@chromium.org | e0e1b0d | 2013-07-08 08:38:06 +0000 | [diff] [blame] | 18467 | |
sgjesse@chromium.org | e059905 | 2011-03-25 07:34:35 +0000 | [diff] [blame] | 18468 | TEST(InitializeDefaultIsolateOnSecondaryThread6) { |
| 18469 | InitializeTestHelper(InitDefaultIsolateThread::SetAddHistogramSampleFunction); |
| 18470 | } |
| 18471 | |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18472 | |
| 18473 | TEST(StringCheckMultipleContexts) { |
| 18474 | const char* code = |
| 18475 | "(function() { return \"a\".charAt(0); })()"; |
| 18476 | |
| 18477 | { |
| 18478 | // Run the code twice in the first context to initialize the call IC. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18479 | LocalContext context1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18480 | v8::HandleScope scope(context1->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18481 | ExpectString(code, "a"); |
| 18482 | ExpectString(code, "a"); |
| 18483 | } |
| 18484 | |
| 18485 | { |
| 18486 | // Change the String.prototype in the second context and check |
| 18487 | // that the right function gets called. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18488 | LocalContext context2; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18489 | v8::HandleScope scope(context2->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18490 | CompileRun("String.prototype.charAt = function() { return \"not a\"; }"); |
| 18491 | ExpectString(code, "not a"); |
| 18492 | } |
| 18493 | } |
| 18494 | |
| 18495 | |
| 18496 | TEST(NumberCheckMultipleContexts) { |
| 18497 | const char* code = |
| 18498 | "(function() { return (42).toString(); })()"; |
| 18499 | |
| 18500 | { |
| 18501 | // Run the code twice in the first context to initialize the call IC. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18502 | LocalContext context1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18503 | v8::HandleScope scope(context1->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18504 | ExpectString(code, "42"); |
| 18505 | ExpectString(code, "42"); |
| 18506 | } |
| 18507 | |
| 18508 | { |
| 18509 | // Change the Number.prototype in the second context and check |
| 18510 | // that the right function gets called. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18511 | LocalContext context2; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18512 | v8::HandleScope scope(context2->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18513 | CompileRun("Number.prototype.toString = function() { return \"not 42\"; }"); |
| 18514 | ExpectString(code, "not 42"); |
| 18515 | } |
| 18516 | } |
| 18517 | |
| 18518 | |
| 18519 | TEST(BooleanCheckMultipleContexts) { |
| 18520 | const char* code = |
| 18521 | "(function() { return true.toString(); })()"; |
| 18522 | |
| 18523 | { |
| 18524 | // Run the code twice in the first context to initialize the call IC. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18525 | LocalContext context1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18526 | v8::HandleScope scope(context1->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18527 | ExpectString(code, "true"); |
| 18528 | ExpectString(code, "true"); |
| 18529 | } |
| 18530 | |
| 18531 | { |
| 18532 | // Change the Boolean.prototype in the second context and check |
| 18533 | // that the right function gets called. |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18534 | LocalContext context2; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18535 | v8::HandleScope scope(context2->GetIsolate()); |
fschneider@chromium.org | c20610a | 2010-09-22 09:44:58 +0000 | [diff] [blame] | 18536 | CompileRun("Boolean.prototype.toString = function() { return \"\"; }"); |
| 18537 | ExpectString(code, ""); |
| 18538 | } |
| 18539 | } |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18540 | |
| 18541 | |
| 18542 | TEST(DontDeleteCellLoadIC) { |
| 18543 | const char* function_code = |
| 18544 | "function readCell() { while (true) { return cell; } }"; |
| 18545 | |
| 18546 | { |
| 18547 | // Run the code twice in the first context to initialize the load |
| 18548 | // IC for a don't delete cell. |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18549 | LocalContext context1; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18550 | v8::HandleScope scope(context1->GetIsolate()); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18551 | CompileRun("var cell = \"first\";"); |
| 18552 | ExpectBoolean("delete cell", false); |
| 18553 | CompileRun(function_code); |
| 18554 | ExpectString("readCell()", "first"); |
| 18555 | ExpectString("readCell()", "first"); |
| 18556 | } |
| 18557 | |
| 18558 | { |
| 18559 | // Use a deletable cell in the second context. |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18560 | LocalContext context2; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18561 | v8::HandleScope scope(context2->GetIsolate()); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18562 | CompileRun("cell = \"second\";"); |
| 18563 | CompileRun(function_code); |
| 18564 | ExpectString("readCell()", "second"); |
| 18565 | ExpectBoolean("delete cell", true); |
| 18566 | ExpectString("(function() {" |
| 18567 | " try {" |
| 18568 | " return readCell();" |
| 18569 | " } catch(e) {" |
| 18570 | " return e.toString();" |
| 18571 | " }" |
| 18572 | "})()", |
| 18573 | "ReferenceError: cell is not defined"); |
| 18574 | CompileRun("cell = \"new_second\";"); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18575 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18576 | ExpectString("readCell()", "new_second"); |
| 18577 | ExpectString("readCell()", "new_second"); |
| 18578 | } |
| 18579 | } |
| 18580 | |
| 18581 | |
| 18582 | TEST(DontDeleteCellLoadICForceDelete) { |
| 18583 | const char* function_code = |
| 18584 | "function readCell() { while (true) { return cell; } }"; |
| 18585 | |
| 18586 | // Run the code twice to initialize the load IC for a don't delete |
| 18587 | // cell. |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18588 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18589 | v8::HandleScope scope(context->GetIsolate()); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18590 | CompileRun("var cell = \"value\";"); |
| 18591 | ExpectBoolean("delete cell", false); |
| 18592 | CompileRun(function_code); |
| 18593 | ExpectString("readCell()", "value"); |
| 18594 | ExpectString("readCell()", "value"); |
| 18595 | |
| 18596 | // Delete the cell using the API and check the inlined code works |
| 18597 | // correctly. |
| 18598 | CHECK(context->Global()->ForceDelete(v8_str("cell"))); |
| 18599 | ExpectString("(function() {" |
| 18600 | " try {" |
| 18601 | " return readCell();" |
| 18602 | " } catch(e) {" |
| 18603 | " return e.toString();" |
| 18604 | " }" |
| 18605 | "})()", |
| 18606 | "ReferenceError: cell is not defined"); |
| 18607 | } |
| 18608 | |
| 18609 | |
| 18610 | TEST(DontDeleteCellLoadICAPI) { |
| 18611 | const char* function_code = |
| 18612 | "function readCell() { while (true) { return cell; } }"; |
| 18613 | |
| 18614 | // Run the code twice to initialize the load IC for a don't delete |
| 18615 | // cell created using the API. |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18616 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18617 | v8::HandleScope scope(context->GetIsolate()); |
ricow@chromium.org | eb7c144 | 2010-10-04 08:54:21 +0000 | [diff] [blame] | 18618 | context->Global()->Set(v8_str("cell"), v8_str("value"), v8::DontDelete); |
| 18619 | ExpectBoolean("delete cell", false); |
| 18620 | CompileRun(function_code); |
| 18621 | ExpectString("readCell()", "value"); |
| 18622 | ExpectString("readCell()", "value"); |
| 18623 | |
| 18624 | // Delete the cell using the API and check the inlined code works |
| 18625 | // correctly. |
| 18626 | CHECK(context->Global()->ForceDelete(v8_str("cell"))); |
| 18627 | ExpectString("(function() {" |
| 18628 | " try {" |
| 18629 | " return readCell();" |
| 18630 | " } catch(e) {" |
| 18631 | " return e.toString();" |
| 18632 | " }" |
| 18633 | "})()", |
| 18634 | "ReferenceError: cell is not defined"); |
| 18635 | } |
| 18636 | |
| 18637 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18638 | class Visitor42 : public v8::PersistentHandleVisitor { |
| 18639 | public: |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18640 | explicit Visitor42(v8::Persistent<v8::Object>* object) |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18641 | : counter_(0), object_(object) { } |
| 18642 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18643 | virtual void VisitPersistentHandle(Persistent<Value>* value, |
| 18644 | uint16_t class_id) { |
| 18645 | if (class_id != 42) return; |
| 18646 | CHECK_EQ(42, value->WrapperClassId()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18647 | v8::Isolate* isolate = CcTest::isolate(); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18648 | v8::HandleScope handle_scope(isolate); |
| 18649 | v8::Handle<v8::Value> handle = v8::Local<v8::Value>::New(isolate, *value); |
| 18650 | v8::Handle<v8::Value> object = |
| 18651 | v8::Local<v8::Object>::New(isolate, *object_); |
| 18652 | CHECK(handle->IsObject()); |
| 18653 | CHECK_EQ(Handle<Object>::Cast(handle), object); |
| 18654 | ++counter_; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18655 | } |
| 18656 | |
| 18657 | int counter_; |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18658 | v8::Persistent<v8::Object>* object_; |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18659 | }; |
| 18660 | |
| 18661 | |
| 18662 | TEST(PersistentHandleVisitor) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18663 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 18664 | v8::Isolate* isolate = context->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18665 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18666 | v8::Persistent<v8::Object> object(isolate, v8::Object::New()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18667 | CHECK_EQ(0, object.WrapperClassId()); |
| 18668 | object.SetWrapperClassId(42); |
| 18669 | CHECK_EQ(42, object.WrapperClassId()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18670 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18671 | Visitor42 visitor(&object); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18672 | v8::V8::VisitHandlesWithClassIds(&visitor); |
| 18673 | CHECK_EQ(1, visitor.counter_); |
| 18674 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18675 | object.Dispose(); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 18676 | } |
| 18677 | |
| 18678 | |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 18679 | TEST(WrapperClassId) { |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 18680 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 18681 | v8::Isolate* isolate = context->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18682 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18683 | v8::Persistent<v8::Object> object(isolate, v8::Object::New()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18684 | CHECK_EQ(0, object.WrapperClassId()); |
| 18685 | object.SetWrapperClassId(65535); |
| 18686 | CHECK_EQ(65535, object.WrapperClassId()); |
| 18687 | object.Dispose(); |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 18688 | } |
| 18689 | |
| 18690 | |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18691 | TEST(PersistentHandleInNewSpaceVisitor) { |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18692 | LocalContext context; |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 18693 | v8::Isolate* isolate = context->GetIsolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18694 | v8::HandleScope scope(isolate); |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18695 | v8::Persistent<v8::Object> object1(isolate, v8::Object::New()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18696 | CHECK_EQ(0, object1.WrapperClassId()); |
| 18697 | object1.SetWrapperClassId(42); |
| 18698 | CHECK_EQ(42, object1.WrapperClassId()); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18699 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18700 | CcTest::heap()->CollectAllGarbage(i::Heap::kNoGCFlags); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18701 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18702 | v8::Persistent<v8::Object> object2(isolate, v8::Object::New()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18703 | CHECK_EQ(0, object2.WrapperClassId()); |
| 18704 | object2.SetWrapperClassId(42); |
| 18705 | CHECK_EQ(42, object2.WrapperClassId()); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18706 | |
rossberg@chromium.org | b99c754 | 2013-05-31 11:40:45 +0000 | [diff] [blame] | 18707 | Visitor42 visitor(&object2); |
mvstanton@chromium.org | d16d853 | 2013-01-25 13:29:10 +0000 | [diff] [blame] | 18708 | v8::V8::VisitHandlesForPartialDependence(isolate, &visitor); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18709 | CHECK_EQ(1, visitor.counter_); |
| 18710 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 18711 | object1.Dispose(); |
| 18712 | object2.Dispose(); |
yangguo@chromium.org | 003650e | 2013-01-24 16:31:08 +0000 | [diff] [blame] | 18713 | } |
| 18714 | |
| 18715 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18716 | TEST(RegExp) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18717 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18718 | v8::HandleScope scope(context->GetIsolate()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18719 | |
| 18720 | v8::Handle<v8::RegExp> re = v8::RegExp::New(v8_str("foo"), v8::RegExp::kNone); |
| 18721 | CHECK(re->IsRegExp()); |
| 18722 | CHECK(re->GetSource()->Equals(v8_str("foo"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18723 | CHECK_EQ(v8::RegExp::kNone, re->GetFlags()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18724 | |
| 18725 | re = v8::RegExp::New(v8_str("bar"), |
| 18726 | static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | |
| 18727 | v8::RegExp::kGlobal)); |
| 18728 | CHECK(re->IsRegExp()); |
| 18729 | CHECK(re->GetSource()->Equals(v8_str("bar"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18730 | CHECK_EQ(v8::RegExp::kIgnoreCase | v8::RegExp::kGlobal, |
| 18731 | static_cast<int>(re->GetFlags())); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18732 | |
| 18733 | re = v8::RegExp::New(v8_str("baz"), |
| 18734 | static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | |
| 18735 | v8::RegExp::kMultiline)); |
| 18736 | CHECK(re->IsRegExp()); |
| 18737 | CHECK(re->GetSource()->Equals(v8_str("baz"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18738 | CHECK_EQ(v8::RegExp::kIgnoreCase | v8::RegExp::kMultiline, |
| 18739 | static_cast<int>(re->GetFlags())); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18740 | |
| 18741 | re = CompileRun("/quux/").As<v8::RegExp>(); |
| 18742 | CHECK(re->IsRegExp()); |
| 18743 | CHECK(re->GetSource()->Equals(v8_str("quux"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18744 | CHECK_EQ(v8::RegExp::kNone, re->GetFlags()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18745 | |
| 18746 | re = CompileRun("/quux/gm").As<v8::RegExp>(); |
| 18747 | CHECK(re->IsRegExp()); |
| 18748 | CHECK(re->GetSource()->Equals(v8_str("quux"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18749 | CHECK_EQ(v8::RegExp::kGlobal | v8::RegExp::kMultiline, |
| 18750 | static_cast<int>(re->GetFlags())); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18751 | |
| 18752 | // Override the RegExp constructor and check the API constructor |
| 18753 | // still works. |
| 18754 | CompileRun("RegExp = function() {}"); |
| 18755 | |
| 18756 | re = v8::RegExp::New(v8_str("foobar"), v8::RegExp::kNone); |
| 18757 | CHECK(re->IsRegExp()); |
| 18758 | CHECK(re->GetSource()->Equals(v8_str("foobar"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18759 | CHECK_EQ(v8::RegExp::kNone, re->GetFlags()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18760 | |
| 18761 | re = v8::RegExp::New(v8_str("foobarbaz"), |
| 18762 | static_cast<v8::RegExp::Flags>(v8::RegExp::kIgnoreCase | |
| 18763 | v8::RegExp::kMultiline)); |
| 18764 | CHECK(re->IsRegExp()); |
| 18765 | CHECK(re->GetSource()->Equals(v8_str("foobarbaz"))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 18766 | CHECK_EQ(v8::RegExp::kIgnoreCase | v8::RegExp::kMultiline, |
| 18767 | static_cast<int>(re->GetFlags())); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18768 | |
| 18769 | context->Global()->Set(v8_str("re"), re); |
| 18770 | ExpectTrue("re.test('FoobarbaZ')"); |
| 18771 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 18772 | // RegExps are objects on which you can set properties. |
| 18773 | re->Set(v8_str("property"), v8::Integer::New(32)); |
jkummerow@chromium.org | c3b3712 | 2011-11-07 10:14:12 +0000 | [diff] [blame] | 18774 | v8::Handle<v8::Value> value(CompileRun("re.property")); |
yangguo@chromium.org | 659ceec | 2012-01-26 07:37:54 +0000 | [diff] [blame] | 18775 | CHECK_EQ(32, value->Int32Value()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 18776 | |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18777 | v8::TryCatch try_catch; |
| 18778 | re = v8::RegExp::New(v8_str("foo["), v8::RegExp::kNone); |
| 18779 | CHECK(re.IsEmpty()); |
| 18780 | CHECK(try_catch.HasCaught()); |
| 18781 | context->Global()->Set(v8_str("ex"), try_catch.Exception()); |
| 18782 | ExpectTrue("ex instanceof SyntaxError"); |
| 18783 | } |
| 18784 | |
| 18785 | |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 18786 | THREADED_TEST(Equals) { |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 18787 | LocalContext localContext; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18788 | v8::HandleScope handleScope(localContext->GetIsolate()); |
sgjesse@chromium.org | 496c03a | 2011-02-14 12:05:43 +0000 | [diff] [blame] | 18789 | |
| 18790 | v8::Handle<v8::Object> globalProxy = localContext->Global(); |
| 18791 | v8::Handle<Value> global = globalProxy->GetPrototype(); |
| 18792 | |
| 18793 | CHECK(global->StrictEquals(global)); |
| 18794 | CHECK(!global->StrictEquals(globalProxy)); |
| 18795 | CHECK(!globalProxy->StrictEquals(global)); |
| 18796 | CHECK(globalProxy->StrictEquals(globalProxy)); |
| 18797 | |
| 18798 | CHECK(global->Equals(global)); |
| 18799 | CHECK(!global->Equals(globalProxy)); |
| 18800 | CHECK(!globalProxy->Equals(global)); |
| 18801 | CHECK(globalProxy->Equals(globalProxy)); |
| 18802 | } |
| 18803 | |
| 18804 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18805 | static void Getter(v8::Local<v8::String> property, |
| 18806 | const v8::PropertyCallbackInfo<v8::Value>& info ) { |
| 18807 | info.GetReturnValue().Set(v8_str("42!")); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18808 | } |
| 18809 | |
| 18810 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18811 | static void Enumerator(const v8::PropertyCallbackInfo<v8::Array>& info) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18812 | v8::Handle<v8::Array> result = v8::Array::New(); |
| 18813 | result->Set(0, v8_str("universalAnswer")); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18814 | info.GetReturnValue().Set(result); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18815 | } |
| 18816 | |
| 18817 | |
| 18818 | TEST(NamedEnumeratorAndForIn) { |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18819 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18820 | v8::HandleScope handle_scope(context->GetIsolate()); |
ager@chromium.org | b61a0d1 | 2010-10-13 08:35:23 +0000 | [diff] [blame] | 18821 | v8::Context::Scope context_scope(context.local()); |
| 18822 | |
| 18823 | v8::Handle<v8::ObjectTemplate> tmpl = v8::ObjectTemplate::New(); |
| 18824 | tmpl->SetNamedPropertyHandler(Getter, NULL, NULL, NULL, Enumerator); |
| 18825 | context->Global()->Set(v8_str("o"), tmpl->NewInstance()); |
| 18826 | v8::Handle<v8::Array> result = v8::Handle<v8::Array>::Cast(CompileRun( |
| 18827 | "var result = []; for (var k in o) result.push(k); result")); |
| 18828 | CHECK_EQ(1, result->Length()); |
| 18829 | CHECK_EQ(v8_str("universalAnswer"), result->Get(0)); |
| 18830 | } |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 18831 | |
| 18832 | |
| 18833 | TEST(DefinePropertyPostDetach) { |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 18834 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 18835 | v8::HandleScope scope(context->GetIsolate()); |
erik.corry@gmail.com | d91075f | 2011-02-10 07:45:38 +0000 | [diff] [blame] | 18836 | v8::Handle<v8::Object> proxy = context->Global(); |
| 18837 | v8::Handle<v8::Function> define_property = |
| 18838 | CompileRun("(function() {" |
| 18839 | " Object.defineProperty(" |
| 18840 | " this," |
| 18841 | " 1," |
| 18842 | " { configurable: true, enumerable: true, value: 3 });" |
| 18843 | "})").As<Function>(); |
| 18844 | context->DetachGlobal(); |
| 18845 | define_property->Call(proxy, 0, NULL); |
| 18846 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 18847 | |
| 18848 | |
| 18849 | static void InstallContextId(v8::Handle<Context> context, int id) { |
| 18850 | Context::Scope scope(context); |
| 18851 | CompileRun("Object.prototype").As<Object>()-> |
| 18852 | Set(v8_str("context_id"), v8::Integer::New(id)); |
| 18853 | } |
| 18854 | |
| 18855 | |
| 18856 | static void CheckContextId(v8::Handle<Object> object, int expected) { |
| 18857 | CHECK_EQ(expected, object->Get(v8_str("context_id"))->Int32Value()); |
| 18858 | } |
| 18859 | |
| 18860 | |
| 18861 | THREADED_TEST(CreationContext) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18862 | HandleScope handle_scope(CcTest::isolate()); |
| 18863 | Handle<Context> context1 = Context::New(CcTest::isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 18864 | InstallContextId(context1, 1); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18865 | Handle<Context> context2 = Context::New(CcTest::isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 18866 | InstallContextId(context2, 2); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18867 | Handle<Context> context3 = Context::New(CcTest::isolate()); |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 18868 | InstallContextId(context3, 3); |
| 18869 | |
| 18870 | Local<v8::FunctionTemplate> tmpl = v8::FunctionTemplate::New(); |
| 18871 | |
| 18872 | Local<Object> object1; |
| 18873 | Local<Function> func1; |
| 18874 | { |
| 18875 | Context::Scope scope(context1); |
| 18876 | object1 = Object::New(); |
| 18877 | func1 = tmpl->GetFunction(); |
| 18878 | } |
| 18879 | |
| 18880 | Local<Object> object2; |
| 18881 | Local<Function> func2; |
| 18882 | { |
| 18883 | Context::Scope scope(context2); |
| 18884 | object2 = Object::New(); |
| 18885 | func2 = tmpl->GetFunction(); |
| 18886 | } |
| 18887 | |
| 18888 | Local<Object> instance1; |
| 18889 | Local<Object> instance2; |
| 18890 | |
| 18891 | { |
| 18892 | Context::Scope scope(context3); |
| 18893 | instance1 = func1->NewInstance(); |
| 18894 | instance2 = func2->NewInstance(); |
| 18895 | } |
| 18896 | |
| 18897 | CHECK(object1->CreationContext() == context1); |
| 18898 | CheckContextId(object1, 1); |
| 18899 | CHECK(func1->CreationContext() == context1); |
| 18900 | CheckContextId(func1, 1); |
| 18901 | CHECK(instance1->CreationContext() == context1); |
| 18902 | CheckContextId(instance1, 1); |
| 18903 | CHECK(object2->CreationContext() == context2); |
| 18904 | CheckContextId(object2, 2); |
| 18905 | CHECK(func2->CreationContext() == context2); |
| 18906 | CheckContextId(func2, 2); |
| 18907 | CHECK(instance2->CreationContext() == context2); |
| 18908 | CheckContextId(instance2, 2); |
| 18909 | |
| 18910 | { |
| 18911 | Context::Scope scope(context1); |
| 18912 | CHECK(object1->CreationContext() == context1); |
| 18913 | CheckContextId(object1, 1); |
| 18914 | CHECK(func1->CreationContext() == context1); |
| 18915 | CheckContextId(func1, 1); |
| 18916 | CHECK(instance1->CreationContext() == context1); |
| 18917 | CheckContextId(instance1, 1); |
| 18918 | CHECK(object2->CreationContext() == context2); |
| 18919 | CheckContextId(object2, 2); |
| 18920 | CHECK(func2->CreationContext() == context2); |
| 18921 | CheckContextId(func2, 2); |
| 18922 | CHECK(instance2->CreationContext() == context2); |
| 18923 | CheckContextId(instance2, 2); |
| 18924 | } |
| 18925 | |
| 18926 | { |
| 18927 | Context::Scope scope(context2); |
| 18928 | CHECK(object1->CreationContext() == context1); |
| 18929 | CheckContextId(object1, 1); |
| 18930 | CHECK(func1->CreationContext() == context1); |
| 18931 | CheckContextId(func1, 1); |
| 18932 | CHECK(instance1->CreationContext() == context1); |
| 18933 | CheckContextId(instance1, 1); |
| 18934 | CHECK(object2->CreationContext() == context2); |
| 18935 | CheckContextId(object2, 2); |
| 18936 | CHECK(func2->CreationContext() == context2); |
| 18937 | CheckContextId(func2, 2); |
| 18938 | CHECK(instance2->CreationContext() == context2); |
| 18939 | CheckContextId(instance2, 2); |
| 18940 | } |
kmillikin@chromium.org | c36ce6e | 2011-04-04 08:25:31 +0000 | [diff] [blame] | 18941 | } |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18942 | |
| 18943 | |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 18944 | THREADED_TEST(CreationContextOfJsFunction) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 18945 | HandleScope handle_scope(CcTest::isolate()); |
| 18946 | Handle<Context> context = Context::New(CcTest::isolate()); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 18947 | InstallContextId(context, 1); |
| 18948 | |
| 18949 | Local<Object> function; |
| 18950 | { |
| 18951 | Context::Scope scope(context); |
| 18952 | function = CompileRun("function foo() {}; foo").As<Object>(); |
| 18953 | } |
| 18954 | |
| 18955 | CHECK(function->CreationContext() == context); |
| 18956 | CheckContextId(function, 1); |
whesse@chromium.org | 4acdc2c | 2011-08-15 13:01:23 +0000 | [diff] [blame] | 18957 | } |
| 18958 | |
| 18959 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18960 | void HasOwnPropertyIndexedPropertyGetter( |
| 18961 | uint32_t index, |
| 18962 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 18963 | if (index == 42) info.GetReturnValue().Set(v8_str("yes")); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18964 | } |
| 18965 | |
| 18966 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18967 | void HasOwnPropertyNamedPropertyGetter( |
| 18968 | Local<String> property, |
| 18969 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 18970 | if (property->Equals(v8_str("foo"))) info.GetReturnValue().Set(v8_str("yes")); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18971 | } |
| 18972 | |
| 18973 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18974 | void HasOwnPropertyIndexedPropertyQuery( |
| 18975 | uint32_t index, const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 18976 | if (index == 42) info.GetReturnValue().Set(1); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18977 | } |
| 18978 | |
| 18979 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18980 | void HasOwnPropertyNamedPropertyQuery( |
| 18981 | Local<String> property, |
| 18982 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 18983 | if (property->Equals(v8_str("foo"))) info.GetReturnValue().Set(1); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18984 | } |
| 18985 | |
| 18986 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18987 | void HasOwnPropertyNamedPropertyQuery2( |
| 18988 | Local<String> property, |
| 18989 | const v8::PropertyCallbackInfo<v8::Integer>& info) { |
| 18990 | if (property->Equals(v8_str("bar"))) info.GetReturnValue().Set(1); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18991 | } |
| 18992 | |
| 18993 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 18994 | void HasOwnPropertyAccessorGetter( |
| 18995 | Local<String> property, |
| 18996 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 18997 | info.GetReturnValue().Set(v8_str("yes")); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 18998 | } |
| 18999 | |
| 19000 | |
| 19001 | TEST(HasOwnProperty) { |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 19002 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19003 | v8::HandleScope scope(env->GetIsolate()); |
sgjesse@chromium.org | 8e8294a | 2011-05-02 14:30:53 +0000 | [diff] [blame] | 19004 | { // Check normal properties and defined getters. |
| 19005 | Handle<Value> value = CompileRun( |
| 19006 | "function Foo() {" |
| 19007 | " this.foo = 11;" |
| 19008 | " this.__defineGetter__('baz', function() { return 1; });" |
| 19009 | "};" |
| 19010 | "function Bar() { " |
| 19011 | " this.bar = 13;" |
| 19012 | " this.__defineGetter__('bla', function() { return 2; });" |
| 19013 | "};" |
| 19014 | "Bar.prototype = new Foo();" |
| 19015 | "new Bar();"); |
| 19016 | CHECK(value->IsObject()); |
| 19017 | Handle<Object> object = value->ToObject(); |
| 19018 | CHECK(object->Has(v8_str("foo"))); |
| 19019 | CHECK(!object->HasOwnProperty(v8_str("foo"))); |
| 19020 | CHECK(object->HasOwnProperty(v8_str("bar"))); |
| 19021 | CHECK(object->Has(v8_str("baz"))); |
| 19022 | CHECK(!object->HasOwnProperty(v8_str("baz"))); |
| 19023 | CHECK(object->HasOwnProperty(v8_str("bla"))); |
| 19024 | } |
| 19025 | { // Check named getter interceptors. |
| 19026 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19027 | templ->SetNamedPropertyHandler(HasOwnPropertyNamedPropertyGetter); |
| 19028 | Handle<Object> instance = templ->NewInstance(); |
| 19029 | CHECK(!instance->HasOwnProperty(v8_str("42"))); |
| 19030 | CHECK(instance->HasOwnProperty(v8_str("foo"))); |
| 19031 | CHECK(!instance->HasOwnProperty(v8_str("bar"))); |
| 19032 | } |
| 19033 | { // Check indexed getter interceptors. |
| 19034 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19035 | templ->SetIndexedPropertyHandler(HasOwnPropertyIndexedPropertyGetter); |
| 19036 | Handle<Object> instance = templ->NewInstance(); |
| 19037 | CHECK(instance->HasOwnProperty(v8_str("42"))); |
| 19038 | CHECK(!instance->HasOwnProperty(v8_str("43"))); |
| 19039 | CHECK(!instance->HasOwnProperty(v8_str("foo"))); |
| 19040 | } |
| 19041 | { // Check named query interceptors. |
| 19042 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19043 | templ->SetNamedPropertyHandler(0, 0, HasOwnPropertyNamedPropertyQuery); |
| 19044 | Handle<Object> instance = templ->NewInstance(); |
| 19045 | CHECK(instance->HasOwnProperty(v8_str("foo"))); |
| 19046 | CHECK(!instance->HasOwnProperty(v8_str("bar"))); |
| 19047 | } |
| 19048 | { // Check indexed query interceptors. |
| 19049 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19050 | templ->SetIndexedPropertyHandler(0, 0, HasOwnPropertyIndexedPropertyQuery); |
| 19051 | Handle<Object> instance = templ->NewInstance(); |
| 19052 | CHECK(instance->HasOwnProperty(v8_str("42"))); |
| 19053 | CHECK(!instance->HasOwnProperty(v8_str("41"))); |
| 19054 | } |
| 19055 | { // Check callbacks. |
| 19056 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19057 | templ->SetAccessor(v8_str("foo"), HasOwnPropertyAccessorGetter); |
| 19058 | Handle<Object> instance = templ->NewInstance(); |
| 19059 | CHECK(instance->HasOwnProperty(v8_str("foo"))); |
| 19060 | CHECK(!instance->HasOwnProperty(v8_str("bar"))); |
| 19061 | } |
| 19062 | { // Check that query wins on disagreement. |
| 19063 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19064 | templ->SetNamedPropertyHandler(HasOwnPropertyNamedPropertyGetter, |
| 19065 | 0, |
| 19066 | HasOwnPropertyNamedPropertyQuery2); |
| 19067 | Handle<Object> instance = templ->NewInstance(); |
| 19068 | CHECK(!instance->HasOwnProperty(v8_str("foo"))); |
| 19069 | CHECK(instance->HasOwnProperty(v8_str("bar"))); |
| 19070 | } |
| 19071 | } |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19072 | |
| 19073 | |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 19074 | TEST(IndexedInterceptorWithStringProto) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19075 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | 71fc346 | 2013-02-27 09:34:27 +0000 | [diff] [blame] | 19076 | Handle<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19077 | templ->SetIndexedPropertyHandler(NULL, |
| 19078 | NULL, |
| 19079 | HasOwnPropertyIndexedPropertyQuery); |
| 19080 | LocalContext context; |
| 19081 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 19082 | CompileRun("var s = new String('foobar'); obj.__proto__ = s;"); |
| 19083 | // These should be intercepted. |
| 19084 | CHECK(CompileRun("42 in obj")->BooleanValue()); |
| 19085 | CHECK(CompileRun("'42' in obj")->BooleanValue()); |
| 19086 | // These should fall through to the String prototype. |
| 19087 | CHECK(CompileRun("0 in obj")->BooleanValue()); |
| 19088 | CHECK(CompileRun("'0' in obj")->BooleanValue()); |
| 19089 | // And these should both fail. |
| 19090 | CHECK(!CompileRun("32 in obj")->BooleanValue()); |
| 19091 | CHECK(!CompileRun("'32' in obj")->BooleanValue()); |
| 19092 | } |
| 19093 | |
| 19094 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19095 | void CheckCodeGenerationAllowed() { |
| 19096 | Handle<Value> result = CompileRun("eval('42')"); |
| 19097 | CHECK_EQ(42, result->Int32Value()); |
| 19098 | result = CompileRun("(function(e) { return e('42'); })(eval)"); |
| 19099 | CHECK_EQ(42, result->Int32Value()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19100 | result = CompileRun("var f = new Function('return 42'); f()"); |
| 19101 | CHECK_EQ(42, result->Int32Value()); |
| 19102 | } |
| 19103 | |
| 19104 | |
| 19105 | void CheckCodeGenerationDisallowed() { |
| 19106 | TryCatch try_catch; |
| 19107 | |
| 19108 | Handle<Value> result = CompileRun("eval('42')"); |
| 19109 | CHECK(result.IsEmpty()); |
| 19110 | CHECK(try_catch.HasCaught()); |
| 19111 | try_catch.Reset(); |
| 19112 | |
| 19113 | result = CompileRun("(function(e) { return e('42'); })(eval)"); |
| 19114 | CHECK(result.IsEmpty()); |
| 19115 | CHECK(try_catch.HasCaught()); |
| 19116 | try_catch.Reset(); |
| 19117 | |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19118 | result = CompileRun("var f = new Function('return 42'); f()"); |
| 19119 | CHECK(result.IsEmpty()); |
| 19120 | CHECK(try_catch.HasCaught()); |
| 19121 | } |
| 19122 | |
| 19123 | |
| 19124 | bool CodeGenerationAllowed(Local<Context> context) { |
| 19125 | ApiTestFuzzer::Fuzz(); |
| 19126 | return true; |
| 19127 | } |
| 19128 | |
| 19129 | |
| 19130 | bool CodeGenerationDisallowed(Local<Context> context) { |
| 19131 | ApiTestFuzzer::Fuzz(); |
| 19132 | return false; |
| 19133 | } |
| 19134 | |
| 19135 | |
| 19136 | THREADED_TEST(AllowCodeGenFromStrings) { |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19137 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19138 | v8::HandleScope scope(context->GetIsolate()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19139 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 19140 | // eval and the Function constructor allowed by default. |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 19141 | CHECK(context->IsCodeGenerationFromStringsAllowed()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19142 | CheckCodeGenerationAllowed(); |
| 19143 | |
ager@chromium.org | ea91cc5 | 2011-05-23 06:06:11 +0000 | [diff] [blame] | 19144 | // Disallow eval and the Function constructor. |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19145 | context->AllowCodeGenerationFromStrings(false); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 19146 | CHECK(!context->IsCodeGenerationFromStringsAllowed()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19147 | CheckCodeGenerationDisallowed(); |
| 19148 | |
| 19149 | // Allow again. |
| 19150 | context->AllowCodeGenerationFromStrings(true); |
| 19151 | CheckCodeGenerationAllowed(); |
| 19152 | |
| 19153 | // Disallow but setting a global callback that will allow the calls. |
| 19154 | context->AllowCodeGenerationFromStrings(false); |
| 19155 | V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationAllowed); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 19156 | CHECK(!context->IsCodeGenerationFromStringsAllowed()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19157 | CheckCodeGenerationAllowed(); |
| 19158 | |
| 19159 | // Set a callback that disallows the code generation. |
| 19160 | V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); |
jkummerow@chromium.org | 1145ef8 | 2012-02-02 16:21:15 +0000 | [diff] [blame] | 19161 | CHECK(!context->IsCodeGenerationFromStringsAllowed()); |
fschneider@chromium.org | fb144a0 | 2011-05-04 12:43:48 +0000 | [diff] [blame] | 19162 | CheckCodeGenerationDisallowed(); |
| 19163 | } |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 19164 | |
| 19165 | |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 19166 | TEST(SetErrorMessageForCodeGenFromStrings) { |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 19167 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19168 | v8::HandleScope scope(context->GetIsolate()); |
ulan@chromium.org | 56c14af | 2012-09-20 12:51:09 +0000 | [diff] [blame] | 19169 | TryCatch try_catch; |
| 19170 | |
| 19171 | Handle<String> message = v8_str("Message") ; |
| 19172 | Handle<String> expected_message = v8_str("Uncaught EvalError: Message"); |
| 19173 | V8::SetAllowCodeGenerationFromStringsCallback(&CodeGenerationDisallowed); |
| 19174 | context->AllowCodeGenerationFromStrings(false); |
| 19175 | context->SetErrorMessageForCodeGenerationFromStrings(message); |
| 19176 | Handle<Value> result = CompileRun("eval('42')"); |
| 19177 | CHECK(result.IsEmpty()); |
| 19178 | CHECK(try_catch.HasCaught()); |
| 19179 | Handle<String> actual_message = try_catch.Message()->Get(); |
| 19180 | CHECK(expected_message->Equals(actual_message)); |
| 19181 | } |
| 19182 | |
| 19183 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 19184 | static void NonObjectThis(const v8::FunctionCallbackInfo<v8::Value>& args) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 19185 | } |
| 19186 | |
| 19187 | |
| 19188 | THREADED_TEST(CallAPIFunctionOnNonObject) { |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 19189 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19190 | v8::HandleScope scope(context->GetIsolate()); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 19191 | Handle<FunctionTemplate> templ = v8::FunctionTemplate::New(NonObjectThis); |
| 19192 | Handle<Function> function = templ->GetFunction(); |
| 19193 | context->Global()->Set(v8_str("f"), function); |
| 19194 | TryCatch try_catch; |
| 19195 | CompileRun("f.call(2)"); |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 19196 | } |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 19197 | |
| 19198 | |
| 19199 | // Regression test for issue 1470. |
| 19200 | THREADED_TEST(ReadOnlyIndexedProperties) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19201 | v8::HandleScope scope(CcTest::isolate()); |
svenpanne@chromium.org | 6d786c9 | 2011-06-15 10:58:27 +0000 | [diff] [blame] | 19202 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19203 | |
| 19204 | LocalContext context; |
| 19205 | Local<v8::Object> obj = templ->NewInstance(); |
| 19206 | context->Global()->Set(v8_str("obj"), obj); |
| 19207 | obj->Set(v8_str("1"), v8_str("DONT_CHANGE"), v8::ReadOnly); |
| 19208 | obj->Set(v8_str("1"), v8_str("foobar")); |
| 19209 | CHECK_EQ(v8_str("DONT_CHANGE"), obj->Get(v8_str("1"))); |
| 19210 | obj->Set(v8_num(2), v8_str("DONT_CHANGE"), v8::ReadOnly); |
| 19211 | obj->Set(v8_num(2), v8_str("foobar")); |
| 19212 | CHECK_EQ(v8_str("DONT_CHANGE"), obj->Get(v8_num(2))); |
| 19213 | |
| 19214 | // Test non-smi case. |
| 19215 | obj->Set(v8_str("2000000000"), v8_str("DONT_CHANGE"), v8::ReadOnly); |
| 19216 | obj->Set(v8_str("2000000000"), v8_str("foobar")); |
| 19217 | CHECK_EQ(v8_str("DONT_CHANGE"), obj->Get(v8_str("2000000000"))); |
| 19218 | } |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 19219 | |
| 19220 | |
| 19221 | THREADED_TEST(Regress1516) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 19222 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19223 | v8::HandleScope scope(context->GetIsolate()); |
| 19224 | |
| 19225 | { v8::HandleScope temp_scope(context->GetIsolate()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 19226 | CompileRun("({'a': 0})"); |
| 19227 | } |
| 19228 | |
| 19229 | int elements; |
| 19230 | { i::MapCache* map_cache = |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19231 | i::MapCache::cast(CcTest::i_isolate()->context()->map_cache()); |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 19232 | elements = map_cache->NumberOfElements(); |
| 19233 | CHECK_LE(1, elements); |
| 19234 | } |
| 19235 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19236 | CcTest::heap()->CollectAllGarbage( |
yangguo@chromium.org | d2899aa | 2012-06-21 11:16:20 +0000 | [diff] [blame] | 19237 | i::Heap::kAbortIncrementalMarkingMask); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19238 | { i::Object* raw_map_cache = CcTest::i_isolate()->context()->map_cache(); |
| 19239 | if (raw_map_cache != CcTest::heap()->undefined_value()) { |
ricow@chromium.org | 4f693d6 | 2011-07-04 14:01:31 +0000 | [diff] [blame] | 19240 | i::MapCache* map_cache = i::MapCache::cast(raw_map_cache); |
| 19241 | CHECK_GT(elements, map_cache->NumberOfElements()); |
| 19242 | } |
| 19243 | } |
| 19244 | } |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19245 | |
| 19246 | |
| 19247 | static bool BlockProtoNamedSecurityTestCallback(Local<v8::Object> global, |
| 19248 | Local<Value> name, |
| 19249 | v8::AccessType type, |
| 19250 | Local<Value> data) { |
| 19251 | // Only block read access to __proto__. |
| 19252 | if (type == v8::ACCESS_GET && |
| 19253 | name->IsString() && |
| 19254 | name->ToString()->Length() == 9 && |
| 19255 | name->ToString()->Utf8Length() == 9) { |
| 19256 | char buffer[10]; |
| 19257 | CHECK_EQ(10, name->ToString()->WriteUtf8(buffer)); |
| 19258 | return strncmp(buffer, "__proto__", 9) != 0; |
| 19259 | } |
| 19260 | |
| 19261 | return true; |
| 19262 | } |
| 19263 | |
| 19264 | |
| 19265 | THREADED_TEST(Regress93759) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19266 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 19267 | HandleScope scope(isolate); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19268 | |
| 19269 | // Template for object with security check. |
| 19270 | Local<ObjectTemplate> no_proto_template = v8::ObjectTemplate::New(); |
| 19271 | // We don't do indexing, so any callback can be used for that. |
| 19272 | no_proto_template->SetAccessCheckCallbacks( |
| 19273 | BlockProtoNamedSecurityTestCallback, |
| 19274 | IndexedSecurityTestCallback); |
| 19275 | |
| 19276 | // Templates for objects with hidden prototypes and possibly security check. |
| 19277 | Local<FunctionTemplate> hidden_proto_template = v8::FunctionTemplate::New(); |
| 19278 | hidden_proto_template->SetHiddenPrototype(true); |
| 19279 | |
| 19280 | Local<FunctionTemplate> protected_hidden_proto_template = |
| 19281 | v8::FunctionTemplate::New(); |
| 19282 | protected_hidden_proto_template->InstanceTemplate()->SetAccessCheckCallbacks( |
| 19283 | BlockProtoNamedSecurityTestCallback, |
| 19284 | IndexedSecurityTestCallback); |
| 19285 | protected_hidden_proto_template->SetHiddenPrototype(true); |
| 19286 | |
| 19287 | // Context for "foreign" objects used in test. |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 19288 | Local<Context> context = v8::Context::New(isolate); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19289 | context->Enter(); |
| 19290 | |
| 19291 | // Plain object, no security check. |
| 19292 | Local<Object> simple_object = Object::New(); |
| 19293 | |
| 19294 | // Object with explicit security check. |
| 19295 | Local<Object> protected_object = |
| 19296 | no_proto_template->NewInstance(); |
| 19297 | |
| 19298 | // JSGlobalProxy object, always have security check. |
| 19299 | Local<Object> proxy_object = |
| 19300 | context->Global(); |
| 19301 | |
| 19302 | // Global object, the prototype of proxy_object. No security checks. |
| 19303 | Local<Object> global_object = |
| 19304 | proxy_object->GetPrototype()->ToObject(); |
| 19305 | |
| 19306 | // Hidden prototype without security check. |
| 19307 | Local<Object> hidden_prototype = |
| 19308 | hidden_proto_template->GetFunction()->NewInstance(); |
| 19309 | Local<Object> object_with_hidden = |
| 19310 | Object::New(); |
| 19311 | object_with_hidden->SetPrototype(hidden_prototype); |
| 19312 | |
| 19313 | // Hidden prototype with security check on the hidden prototype. |
| 19314 | Local<Object> protected_hidden_prototype = |
| 19315 | protected_hidden_proto_template->GetFunction()->NewInstance(); |
| 19316 | Local<Object> object_with_protected_hidden = |
| 19317 | Object::New(); |
| 19318 | object_with_protected_hidden->SetPrototype(protected_hidden_prototype); |
| 19319 | |
| 19320 | context->Exit(); |
| 19321 | |
| 19322 | // Template for object for second context. Values to test are put on it as |
| 19323 | // properties. |
| 19324 | Local<ObjectTemplate> global_template = ObjectTemplate::New(); |
| 19325 | global_template->Set(v8_str("simple"), simple_object); |
| 19326 | global_template->Set(v8_str("protected"), protected_object); |
| 19327 | global_template->Set(v8_str("global"), global_object); |
| 19328 | global_template->Set(v8_str("proxy"), proxy_object); |
| 19329 | global_template->Set(v8_str("hidden"), object_with_hidden); |
| 19330 | global_template->Set(v8_str("phidden"), object_with_protected_hidden); |
| 19331 | |
| 19332 | LocalContext context2(NULL, global_template); |
| 19333 | |
| 19334 | Local<Value> result1 = CompileRun("Object.getPrototypeOf(simple)"); |
| 19335 | CHECK(result1->Equals(simple_object->GetPrototype())); |
| 19336 | |
| 19337 | Local<Value> result2 = CompileRun("Object.getPrototypeOf(protected)"); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 19338 | CHECK(result2->Equals(Undefined(isolate))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19339 | |
| 19340 | Local<Value> result3 = CompileRun("Object.getPrototypeOf(global)"); |
| 19341 | CHECK(result3->Equals(global_object->GetPrototype())); |
| 19342 | |
| 19343 | Local<Value> result4 = CompileRun("Object.getPrototypeOf(proxy)"); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 19344 | CHECK(result4->Equals(Undefined(isolate))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19345 | |
| 19346 | Local<Value> result5 = CompileRun("Object.getPrototypeOf(hidden)"); |
| 19347 | CHECK(result5->Equals( |
| 19348 | object_with_hidden->GetPrototype()->ToObject()->GetPrototype())); |
| 19349 | |
| 19350 | Local<Value> result6 = CompileRun("Object.getPrototypeOf(phidden)"); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 19351 | CHECK(result6->Equals(Undefined(isolate))); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19352 | } |
| 19353 | |
| 19354 | |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 19355 | THREADED_TEST(Regress125988) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19356 | v8::HandleScope scope(CcTest::isolate()); |
jkummerow@chromium.org | 212d964 | 2012-05-11 15:02:09 +0000 | [diff] [blame] | 19357 | Handle<FunctionTemplate> intercept = FunctionTemplate::New(); |
| 19358 | AddInterceptor(intercept, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 19359 | LocalContext env; |
| 19360 | env->Global()->Set(v8_str("Intercept"), intercept->GetFunction()); |
| 19361 | CompileRun("var a = new Object();" |
| 19362 | "var b = new Intercept();" |
| 19363 | "var c = new Object();" |
| 19364 | "c.__proto__ = b;" |
| 19365 | "b.__proto__ = a;" |
| 19366 | "a.x = 23;" |
| 19367 | "for (var i = 0; i < 3; i++) c.x;"); |
| 19368 | ExpectBoolean("c.hasOwnProperty('x')", false); |
| 19369 | ExpectInt32("c.x", 23); |
| 19370 | CompileRun("a.y = 42;" |
| 19371 | "for (var i = 0; i < 3; i++) c.x;"); |
| 19372 | ExpectBoolean("c.hasOwnProperty('x')", false); |
| 19373 | ExpectInt32("c.x", 23); |
| 19374 | ExpectBoolean("c.hasOwnProperty('y')", false); |
| 19375 | ExpectInt32("c.y", 42); |
| 19376 | } |
| 19377 | |
| 19378 | |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19379 | static void TestReceiver(Local<Value> expected_result, |
| 19380 | Local<Value> expected_receiver, |
| 19381 | const char* code) { |
| 19382 | Local<Value> result = CompileRun(code); |
| 19383 | CHECK(result->IsObject()); |
| 19384 | CHECK(expected_receiver->Equals(result->ToObject()->Get(1))); |
| 19385 | CHECK(expected_result->Equals(result->ToObject()->Get(0))); |
| 19386 | } |
| 19387 | |
| 19388 | |
| 19389 | THREADED_TEST(ForeignFunctionReceiver) { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19390 | v8::Isolate* isolate = CcTest::isolate(); |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 19391 | HandleScope scope(isolate); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19392 | |
| 19393 | // Create two contexts with different "id" properties ('i' and 'o'). |
| 19394 | // Call a function both from its own context and from a the foreign |
| 19395 | // context, and see what "this" is bound to (returning both "this" |
| 19396 | // and "this.id" for comparison). |
| 19397 | |
ulan@chromium.org | 57ff881 | 2013-05-10 08:16:55 +0000 | [diff] [blame] | 19398 | Local<Context> foreign_context = v8::Context::New(isolate); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19399 | foreign_context->Enter(); |
| 19400 | Local<Value> foreign_function = |
| 19401 | CompileRun("function func() { return { 0: this.id, " |
| 19402 | " 1: this, " |
| 19403 | " toString: function() { " |
| 19404 | " return this[0];" |
| 19405 | " }" |
| 19406 | " };" |
| 19407 | "}" |
| 19408 | "var id = 'i';" |
| 19409 | "func;"); |
| 19410 | CHECK(foreign_function->IsFunction()); |
| 19411 | foreign_context->Exit(); |
| 19412 | |
| 19413 | LocalContext context; |
| 19414 | |
| 19415 | Local<String> password = v8_str("Password"); |
| 19416 | // Don't get hit by security checks when accessing foreign_context's |
| 19417 | // global receiver (aka. global proxy). |
| 19418 | context->SetSecurityToken(password); |
| 19419 | foreign_context->SetSecurityToken(password); |
| 19420 | |
| 19421 | Local<String> i = v8_str("i"); |
| 19422 | Local<String> o = v8_str("o"); |
| 19423 | Local<String> id = v8_str("id"); |
| 19424 | |
| 19425 | CompileRun("function ownfunc() { return { 0: this.id, " |
| 19426 | " 1: this, " |
| 19427 | " toString: function() { " |
| 19428 | " return this[0];" |
| 19429 | " }" |
| 19430 | " };" |
| 19431 | "}" |
| 19432 | "var id = 'o';" |
| 19433 | "ownfunc"); |
| 19434 | context->Global()->Set(v8_str("func"), foreign_function); |
| 19435 | |
| 19436 | // Sanity check the contexts. |
| 19437 | CHECK(i->Equals(foreign_context->Global()->Get(id))); |
| 19438 | CHECK(o->Equals(context->Global()->Get(id))); |
| 19439 | |
| 19440 | // Checking local function's receiver. |
| 19441 | // Calling function using its call/apply methods. |
| 19442 | TestReceiver(o, context->Global(), "ownfunc.call()"); |
| 19443 | TestReceiver(o, context->Global(), "ownfunc.apply()"); |
| 19444 | // Making calls through built-in functions. |
| 19445 | TestReceiver(o, context->Global(), "[1].map(ownfunc)[0]"); |
| 19446 | CHECK(o->Equals(CompileRun("'abcbd'.replace(/b/,ownfunc)[1]"))); |
| 19447 | CHECK(o->Equals(CompileRun("'abcbd'.replace(/b/g,ownfunc)[1]"))); |
| 19448 | CHECK(o->Equals(CompileRun("'abcbd'.replace(/b/g,ownfunc)[3]"))); |
| 19449 | // Calling with environment record as base. |
| 19450 | TestReceiver(o, context->Global(), "ownfunc()"); |
| 19451 | // Calling with no base. |
| 19452 | TestReceiver(o, context->Global(), "(1,ownfunc)()"); |
| 19453 | |
| 19454 | // Checking foreign function return value. |
| 19455 | // Calling function using its call/apply methods. |
| 19456 | TestReceiver(i, foreign_context->Global(), "func.call()"); |
| 19457 | TestReceiver(i, foreign_context->Global(), "func.apply()"); |
| 19458 | // Calling function using another context's call/apply methods. |
| 19459 | TestReceiver(i, foreign_context->Global(), |
| 19460 | "Function.prototype.call.call(func)"); |
| 19461 | TestReceiver(i, foreign_context->Global(), |
| 19462 | "Function.prototype.call.apply(func)"); |
| 19463 | TestReceiver(i, foreign_context->Global(), |
| 19464 | "Function.prototype.apply.call(func)"); |
| 19465 | TestReceiver(i, foreign_context->Global(), |
| 19466 | "Function.prototype.apply.apply(func)"); |
| 19467 | // Making calls through built-in functions. |
| 19468 | TestReceiver(i, foreign_context->Global(), "[1].map(func)[0]"); |
| 19469 | // ToString(func()) is func()[0], i.e., the returned this.id. |
| 19470 | CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/,func)[1]"))); |
| 19471 | CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[1]"))); |
| 19472 | CHECK(i->Equals(CompileRun("'abcbd'.replace(/b/g,func)[3]"))); |
| 19473 | |
| 19474 | // TODO(1547): Make the following also return "i". |
| 19475 | // Calling with environment record as base. |
| 19476 | TestReceiver(o, context->Global(), "func()"); |
| 19477 | // Calling with no base. |
| 19478 | TestReceiver(o, context->Global(), "(1,func)()"); |
ricow@chromium.org | 4668a2c | 2011-08-29 10:41:00 +0000 | [diff] [blame] | 19479 | } |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19480 | |
| 19481 | |
| 19482 | uint8_t callback_fired = 0; |
| 19483 | |
| 19484 | |
| 19485 | void CallCompletedCallback1() { |
| 19486 | i::OS::Print("Firing callback 1.\n"); |
| 19487 | callback_fired ^= 1; // Toggle first bit. |
| 19488 | } |
| 19489 | |
| 19490 | |
| 19491 | void CallCompletedCallback2() { |
| 19492 | i::OS::Print("Firing callback 2.\n"); |
| 19493 | callback_fired ^= 2; // Toggle second bit. |
| 19494 | } |
| 19495 | |
| 19496 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 19497 | void RecursiveCall(const v8::FunctionCallbackInfo<v8::Value>& args) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19498 | int32_t level = args[0]->Int32Value(); |
| 19499 | if (level < 3) { |
| 19500 | level++; |
| 19501 | i::OS::Print("Entering recursion level %d.\n", level); |
| 19502 | char script[64]; |
| 19503 | i::Vector<char> script_vector(script, sizeof(script)); |
| 19504 | i::OS::SNPrintF(script_vector, "recursion(%d)", level); |
| 19505 | CompileRun(script_vector.start()); |
| 19506 | i::OS::Print("Leaving recursion level %d.\n", level); |
| 19507 | CHECK_EQ(0, callback_fired); |
| 19508 | } else { |
| 19509 | i::OS::Print("Recursion ends.\n"); |
| 19510 | CHECK_EQ(0, callback_fired); |
| 19511 | } |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19512 | } |
| 19513 | |
| 19514 | |
| 19515 | TEST(CallCompletedCallback) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19516 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19517 | v8::HandleScope scope(env->GetIsolate()); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19518 | v8::Handle<v8::FunctionTemplate> recursive_runtime = |
| 19519 | v8::FunctionTemplate::New(RecursiveCall); |
| 19520 | env->Global()->Set(v8_str("recursion"), |
| 19521 | recursive_runtime->GetFunction()); |
| 19522 | // Adding the same callback a second time has no effect. |
| 19523 | v8::V8::AddCallCompletedCallback(CallCompletedCallback1); |
| 19524 | v8::V8::AddCallCompletedCallback(CallCompletedCallback1); |
| 19525 | v8::V8::AddCallCompletedCallback(CallCompletedCallback2); |
| 19526 | i::OS::Print("--- Script (1) ---\n"); |
| 19527 | Local<Script> script = |
| 19528 | v8::Script::Compile(v8::String::New("recursion(0)")); |
| 19529 | script->Run(); |
| 19530 | CHECK_EQ(3, callback_fired); |
| 19531 | |
| 19532 | i::OS::Print("\n--- Script (2) ---\n"); |
| 19533 | callback_fired = 0; |
| 19534 | v8::V8::RemoveCallCompletedCallback(CallCompletedCallback1); |
| 19535 | script->Run(); |
| 19536 | CHECK_EQ(2, callback_fired); |
| 19537 | |
| 19538 | i::OS::Print("\n--- Function ---\n"); |
| 19539 | callback_fired = 0; |
| 19540 | Local<Function> recursive_function = |
| 19541 | Local<Function>::Cast(env->Global()->Get(v8_str("recursion"))); |
| 19542 | v8::Handle<Value> args[] = { v8_num(0) }; |
| 19543 | recursive_function->Call(env->Global(), 1, args); |
| 19544 | CHECK_EQ(2, callback_fired); |
| 19545 | } |
| 19546 | |
| 19547 | |
| 19548 | void CallCompletedCallbackNoException() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19549 | v8::HandleScope scope(CcTest::isolate()); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19550 | CompileRun("1+1;"); |
| 19551 | } |
| 19552 | |
| 19553 | |
| 19554 | void CallCompletedCallbackException() { |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19555 | v8::HandleScope scope(CcTest::isolate()); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19556 | CompileRun("throw 'second exception';"); |
| 19557 | } |
| 19558 | |
| 19559 | |
| 19560 | TEST(CallCompletedCallbackOneException) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19561 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19562 | v8::HandleScope scope(env->GetIsolate()); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19563 | v8::V8::AddCallCompletedCallback(CallCompletedCallbackNoException); |
| 19564 | CompileRun("throw 'exception';"); |
| 19565 | } |
| 19566 | |
| 19567 | |
| 19568 | TEST(CallCompletedCallbackTwoExceptions) { |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19569 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19570 | v8::HandleScope scope(env->GetIsolate()); |
rossberg@chromium.org | fab1498 | 2012-01-05 15:02:15 +0000 | [diff] [blame] | 19571 | v8::V8::AddCallCompletedCallback(CallCompletedCallbackException); |
| 19572 | CompileRun("throw 'first exception';"); |
| 19573 | } |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 19574 | |
| 19575 | |
| 19576 | static int probes_counter = 0; |
| 19577 | static int misses_counter = 0; |
| 19578 | static int updates_counter = 0; |
| 19579 | |
| 19580 | |
| 19581 | static int* LookupCounter(const char* name) { |
| 19582 | if (strcmp(name, "c:V8.MegamorphicStubCacheProbes") == 0) { |
| 19583 | return &probes_counter; |
| 19584 | } else if (strcmp(name, "c:V8.MegamorphicStubCacheMisses") == 0) { |
| 19585 | return &misses_counter; |
| 19586 | } else if (strcmp(name, "c:V8.MegamorphicStubCacheUpdates") == 0) { |
| 19587 | return &updates_counter; |
| 19588 | } |
| 19589 | return NULL; |
| 19590 | } |
| 19591 | |
| 19592 | |
| 19593 | static const char* kMegamorphicTestProgram = |
| 19594 | "function ClassA() { };" |
| 19595 | "function ClassB() { };" |
| 19596 | "ClassA.prototype.foo = function() { };" |
| 19597 | "ClassB.prototype.foo = function() { };" |
| 19598 | "function fooify(obj) { obj.foo(); };" |
| 19599 | "var a = new ClassA();" |
| 19600 | "var b = new ClassB();" |
| 19601 | "for (var i = 0; i < 10000; i++) {" |
| 19602 | " fooify(a);" |
| 19603 | " fooify(b);" |
| 19604 | "}"; |
| 19605 | |
| 19606 | |
| 19607 | static void StubCacheHelper(bool primary) { |
| 19608 | V8::SetCounterFunction(LookupCounter); |
| 19609 | USE(kMegamorphicTestProgram); |
| 19610 | #ifdef DEBUG |
| 19611 | i::FLAG_native_code_counters = true; |
| 19612 | if (primary) { |
| 19613 | i::FLAG_test_primary_stub_cache = true; |
| 19614 | } else { |
| 19615 | i::FLAG_test_secondary_stub_cache = true; |
| 19616 | } |
| 19617 | i::FLAG_crankshaft = false; |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 19618 | LocalContext env; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19619 | v8::HandleScope scope(env->GetIsolate()); |
ulan@chromium.org | 812308e | 2012-02-29 15:58:45 +0000 | [diff] [blame] | 19620 | int initial_probes = probes_counter; |
| 19621 | int initial_misses = misses_counter; |
| 19622 | int initial_updates = updates_counter; |
| 19623 | CompileRun(kMegamorphicTestProgram); |
| 19624 | int probes = probes_counter - initial_probes; |
| 19625 | int misses = misses_counter - initial_misses; |
| 19626 | int updates = updates_counter - initial_updates; |
| 19627 | CHECK_LT(updates, 10); |
| 19628 | CHECK_LT(misses, 10); |
| 19629 | CHECK_GE(probes, 10000); |
| 19630 | #endif |
| 19631 | } |
| 19632 | |
| 19633 | |
| 19634 | TEST(SecondaryStubCache) { |
| 19635 | StubCacheHelper(true); |
| 19636 | } |
| 19637 | |
| 19638 | |
| 19639 | TEST(PrimaryStubCache) { |
| 19640 | StubCacheHelper(false); |
| 19641 | } |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19642 | |
| 19643 | |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19644 | TEST(StaticGetters) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19645 | LocalContext context; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19646 | i::Factory* factory = CcTest::i_isolate()->factory(); |
| 19647 | v8::Isolate* isolate = CcTest::isolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19648 | v8::HandleScope scope(isolate); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 19649 | i::Handle<i::Object> undefined_value = factory->undefined_value(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19650 | CHECK(*v8::Utils::OpenHandle(*v8::Undefined(isolate)) == *undefined_value); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 19651 | i::Handle<i::Object> null_value = factory->null_value(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19652 | CHECK(*v8::Utils::OpenHandle(*v8::Null(isolate)) == *null_value); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 19653 | i::Handle<i::Object> true_value = factory->true_value(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19654 | CHECK(*v8::Utils::OpenHandle(*v8::True(isolate)) == *true_value); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 19655 | i::Handle<i::Object> false_value = factory->false_value(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19656 | CHECK(*v8::Utils::OpenHandle(*v8::False(isolate)) == *false_value); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19657 | } |
| 19658 | |
| 19659 | |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19660 | UNINITIALIZED_TEST(IsolateEmbedderData) { |
| 19661 | CcTest::DisableAutomaticDispose(); |
| 19662 | v8::Isolate* isolate = v8::Isolate::New(); |
| 19663 | isolate->Enter(); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 19664 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19665 | CHECK_EQ(NULL, isolate->GetData()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 19666 | CHECK_EQ(NULL, i_isolate->GetData()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19667 | static void* data1 = reinterpret_cast<void*>(0xacce55ed); |
| 19668 | isolate->SetData(data1); |
| 19669 | CHECK_EQ(data1, isolate->GetData()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 19670 | CHECK_EQ(data1, i_isolate->GetData()); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19671 | static void* data2 = reinterpret_cast<void*>(0xdecea5ed); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 19672 | i_isolate->SetData(data2); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19673 | CHECK_EQ(data2, isolate->GetData()); |
hpayer@chromium.org | c5d4971 | 2013-09-11 08:25:48 +0000 | [diff] [blame] | 19674 | CHECK_EQ(data2, i_isolate->GetData()); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19675 | isolate->Exit(); |
| 19676 | isolate->Dispose(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19677 | } |
| 19678 | |
| 19679 | |
| 19680 | TEST(StringEmpty) { |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19681 | LocalContext context; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19682 | i::Factory* factory = CcTest::i_isolate()->factory(); |
| 19683 | v8::Isolate* isolate = CcTest::isolate(); |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19684 | v8::HandleScope scope(isolate); |
verwaest@chromium.org | d4be0f0 | 2013-06-05 13:39:03 +0000 | [diff] [blame] | 19685 | i::Handle<i::Object> empty_string = factory->empty_string(); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19686 | CHECK(*v8::Utils::OpenHandle(*v8::String::Empty()) == *empty_string); |
| 19687 | CHECK(*v8::Utils::OpenHandle(*v8::String::Empty(isolate)) == *empty_string); |
yangguo@chromium.org | efdb9d7 | 2012-04-26 08:21:05 +0000 | [diff] [blame] | 19688 | } |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19689 | |
| 19690 | |
| 19691 | static int instance_checked_getter_count = 0; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 19692 | static void InstanceCheckedGetter( |
| 19693 | Local<String> name, |
| 19694 | const v8::PropertyCallbackInfo<v8::Value>& info) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19695 | CHECK_EQ(name, v8_str("foo")); |
| 19696 | instance_checked_getter_count++; |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 19697 | info.GetReturnValue().Set(v8_num(11)); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19698 | } |
| 19699 | |
| 19700 | |
| 19701 | static int instance_checked_setter_count = 0; |
| 19702 | static void InstanceCheckedSetter(Local<String> name, |
| 19703 | Local<Value> value, |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 19704 | const v8::PropertyCallbackInfo<void>& info) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19705 | CHECK_EQ(name, v8_str("foo")); |
| 19706 | CHECK_EQ(value, v8_num(23)); |
| 19707 | instance_checked_setter_count++; |
| 19708 | } |
| 19709 | |
| 19710 | |
| 19711 | static void CheckInstanceCheckedResult(int getters, |
| 19712 | int setters, |
| 19713 | bool expects_callbacks, |
| 19714 | TryCatch* try_catch) { |
| 19715 | if (expects_callbacks) { |
| 19716 | CHECK(!try_catch->HasCaught()); |
| 19717 | CHECK_EQ(getters, instance_checked_getter_count); |
| 19718 | CHECK_EQ(setters, instance_checked_setter_count); |
| 19719 | } else { |
| 19720 | CHECK(try_catch->HasCaught()); |
| 19721 | CHECK_EQ(0, instance_checked_getter_count); |
| 19722 | CHECK_EQ(0, instance_checked_setter_count); |
| 19723 | } |
| 19724 | try_catch->Reset(); |
| 19725 | } |
| 19726 | |
| 19727 | |
| 19728 | static void CheckInstanceCheckedAccessors(bool expects_callbacks) { |
| 19729 | instance_checked_getter_count = 0; |
| 19730 | instance_checked_setter_count = 0; |
| 19731 | TryCatch try_catch; |
| 19732 | |
| 19733 | // Test path through generic runtime code. |
| 19734 | CompileRun("obj.foo"); |
| 19735 | CheckInstanceCheckedResult(1, 0, expects_callbacks, &try_catch); |
| 19736 | CompileRun("obj.foo = 23"); |
| 19737 | CheckInstanceCheckedResult(1, 1, expects_callbacks, &try_catch); |
| 19738 | |
| 19739 | // Test path through generated LoadIC and StoredIC. |
| 19740 | CompileRun("function test_get(o) { o.foo; }" |
| 19741 | "test_get(obj);"); |
| 19742 | CheckInstanceCheckedResult(2, 1, expects_callbacks, &try_catch); |
| 19743 | CompileRun("test_get(obj);"); |
| 19744 | CheckInstanceCheckedResult(3, 1, expects_callbacks, &try_catch); |
| 19745 | CompileRun("test_get(obj);"); |
| 19746 | CheckInstanceCheckedResult(4, 1, expects_callbacks, &try_catch); |
| 19747 | CompileRun("function test_set(o) { o.foo = 23; }" |
| 19748 | "test_set(obj);"); |
| 19749 | CheckInstanceCheckedResult(4, 2, expects_callbacks, &try_catch); |
| 19750 | CompileRun("test_set(obj);"); |
| 19751 | CheckInstanceCheckedResult(4, 3, expects_callbacks, &try_catch); |
| 19752 | CompileRun("test_set(obj);"); |
| 19753 | CheckInstanceCheckedResult(4, 4, expects_callbacks, &try_catch); |
| 19754 | |
| 19755 | // Test path through optimized code. |
| 19756 | CompileRun("%OptimizeFunctionOnNextCall(test_get);" |
| 19757 | "test_get(obj);"); |
| 19758 | CheckInstanceCheckedResult(5, 4, expects_callbacks, &try_catch); |
| 19759 | CompileRun("%OptimizeFunctionOnNextCall(test_set);" |
| 19760 | "test_set(obj);"); |
| 19761 | CheckInstanceCheckedResult(5, 5, expects_callbacks, &try_catch); |
| 19762 | |
| 19763 | // Cleanup so that closures start out fresh in next check. |
| 19764 | CompileRun("%DeoptimizeFunction(test_get);" |
| 19765 | "%ClearFunctionTypeFeedback(test_get);" |
| 19766 | "%DeoptimizeFunction(test_set);" |
| 19767 | "%ClearFunctionTypeFeedback(test_set);"); |
| 19768 | } |
| 19769 | |
| 19770 | |
| 19771 | THREADED_TEST(InstanceCheckOnInstanceAccessor) { |
| 19772 | v8::internal::FLAG_allow_natives_syntax = true; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19773 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19774 | v8::HandleScope scope(context->GetIsolate()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19775 | |
| 19776 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 19777 | Local<ObjectTemplate> inst = templ->InstanceTemplate(); |
| 19778 | inst->SetAccessor(v8_str("foo"), |
| 19779 | InstanceCheckedGetter, InstanceCheckedSetter, |
| 19780 | Handle<Value>(), |
| 19781 | v8::DEFAULT, |
| 19782 | v8::None, |
| 19783 | v8::AccessorSignature::New(templ)); |
| 19784 | context->Global()->Set(v8_str("f"), templ->GetFunction()); |
| 19785 | |
| 19786 | printf("Testing positive ...\n"); |
| 19787 | CompileRun("var obj = new f();"); |
| 19788 | CHECK(templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19789 | CheckInstanceCheckedAccessors(true); |
| 19790 | |
| 19791 | printf("Testing negative ...\n"); |
| 19792 | CompileRun("var obj = {};" |
| 19793 | "obj.__proto__ = new f();"); |
| 19794 | CHECK(!templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19795 | CheckInstanceCheckedAccessors(false); |
| 19796 | } |
| 19797 | |
| 19798 | |
| 19799 | THREADED_TEST(InstanceCheckOnInstanceAccessorWithInterceptor) { |
| 19800 | v8::internal::FLAG_allow_natives_syntax = true; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19801 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19802 | v8::HandleScope scope(context->GetIsolate()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19803 | |
| 19804 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 19805 | Local<ObjectTemplate> inst = templ->InstanceTemplate(); |
| 19806 | AddInterceptor(templ, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 19807 | inst->SetAccessor(v8_str("foo"), |
| 19808 | InstanceCheckedGetter, InstanceCheckedSetter, |
| 19809 | Handle<Value>(), |
| 19810 | v8::DEFAULT, |
| 19811 | v8::None, |
| 19812 | v8::AccessorSignature::New(templ)); |
| 19813 | context->Global()->Set(v8_str("f"), templ->GetFunction()); |
| 19814 | |
| 19815 | printf("Testing positive ...\n"); |
| 19816 | CompileRun("var obj = new f();"); |
| 19817 | CHECK(templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19818 | CheckInstanceCheckedAccessors(true); |
| 19819 | |
| 19820 | printf("Testing negative ...\n"); |
| 19821 | CompileRun("var obj = {};" |
| 19822 | "obj.__proto__ = new f();"); |
| 19823 | CHECK(!templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19824 | CheckInstanceCheckedAccessors(false); |
| 19825 | } |
| 19826 | |
| 19827 | |
| 19828 | THREADED_TEST(InstanceCheckOnPrototypeAccessor) { |
| 19829 | v8::internal::FLAG_allow_natives_syntax = true; |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19830 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19831 | v8::HandleScope scope(context->GetIsolate()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19832 | |
| 19833 | Local<FunctionTemplate> templ = FunctionTemplate::New(); |
| 19834 | Local<ObjectTemplate> proto = templ->PrototypeTemplate(); |
| 19835 | proto->SetAccessor(v8_str("foo"), |
| 19836 | InstanceCheckedGetter, InstanceCheckedSetter, |
| 19837 | Handle<Value>(), |
| 19838 | v8::DEFAULT, |
| 19839 | v8::None, |
| 19840 | v8::AccessorSignature::New(templ)); |
| 19841 | context->Global()->Set(v8_str("f"), templ->GetFunction()); |
| 19842 | |
| 19843 | printf("Testing positive ...\n"); |
| 19844 | CompileRun("var obj = new f();"); |
| 19845 | CHECK(templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19846 | CheckInstanceCheckedAccessors(true); |
| 19847 | |
| 19848 | printf("Testing negative ...\n"); |
| 19849 | CompileRun("var obj = {};" |
| 19850 | "obj.__proto__ = new f();"); |
| 19851 | CHECK(!templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19852 | CheckInstanceCheckedAccessors(false); |
| 19853 | |
| 19854 | printf("Testing positive with modified prototype chain ...\n"); |
| 19855 | CompileRun("var obj = new f();" |
| 19856 | "var pro = {};" |
| 19857 | "pro.__proto__ = obj.__proto__;" |
| 19858 | "obj.__proto__ = pro;"); |
| 19859 | CHECK(templ->HasInstance(context->Global()->Get(v8_str("obj")))); |
| 19860 | CheckInstanceCheckedAccessors(true); |
| 19861 | } |
| 19862 | |
| 19863 | |
| 19864 | TEST(TryFinallyMessage) { |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19865 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19866 | v8::HandleScope scope(context->GetIsolate()); |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 19867 | { |
| 19868 | // Test that the original error message is not lost if there is a |
| 19869 | // recursive call into Javascript is done in the finally block, e.g. to |
| 19870 | // initialize an IC. (crbug.com/129171) |
| 19871 | TryCatch try_catch; |
| 19872 | const char* trigger_ic = |
| 19873 | "try { \n" |
| 19874 | " throw new Error('test'); \n" |
| 19875 | "} finally { \n" |
| 19876 | " var x = 0; \n" |
| 19877 | " x++; \n" // Trigger an IC initialization here. |
| 19878 | "} \n"; |
| 19879 | CompileRun(trigger_ic); |
| 19880 | CHECK(try_catch.HasCaught()); |
| 19881 | Local<Message> message = try_catch.Message(); |
| 19882 | CHECK(!message.IsEmpty()); |
| 19883 | CHECK_EQ(2, message->GetLineNumber()); |
| 19884 | } |
| 19885 | |
| 19886 | { |
| 19887 | // Test that the original exception message is indeed overwritten if |
| 19888 | // a new error is thrown in the finally block. |
| 19889 | TryCatch try_catch; |
| 19890 | const char* throw_again = |
| 19891 | "try { \n" |
| 19892 | " throw new Error('test'); \n" |
| 19893 | "} finally { \n" |
| 19894 | " var x = 0; \n" |
| 19895 | " x++; \n" |
| 19896 | " throw new Error('again'); \n" // This is the new uncaught error. |
| 19897 | "} \n"; |
| 19898 | CompileRun(throw_again); |
| 19899 | CHECK(try_catch.HasCaught()); |
| 19900 | Local<Message> message = try_catch.Message(); |
| 19901 | CHECK(!message.IsEmpty()); |
| 19902 | CHECK_EQ(6, message->GetLineNumber()); |
| 19903 | } |
| 19904 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19905 | |
| 19906 | |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19907 | static void Helper137002(bool do_store, |
| 19908 | bool polymorphic, |
| 19909 | bool remove_accessor, |
| 19910 | bool interceptor) { |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19911 | LocalContext context; |
| 19912 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19913 | if (interceptor) { |
| 19914 | templ->SetNamedPropertyHandler(FooGetInterceptor, FooSetInterceptor); |
| 19915 | } else { |
| 19916 | templ->SetAccessor(v8_str("foo"), |
| 19917 | GetterWhichReturns42, |
| 19918 | SetterWhichSetsYOnThisTo23); |
| 19919 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19920 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 19921 | |
| 19922 | // Turn monomorphic on slow object with native accessor, then turn |
| 19923 | // polymorphic, finally optimize to create negative lookup and fail. |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19924 | CompileRun(do_store ? |
| 19925 | "function f(x) { x.foo = void 0; }" : |
| 19926 | "function f(x) { return x.foo; }"); |
| 19927 | CompileRun("obj.y = void 0;"); |
| 19928 | if (!interceptor) { |
| 19929 | CompileRun("%OptimizeObjectForAddingMultipleProperties(obj, 1);"); |
| 19930 | } |
| 19931 | CompileRun("obj.__proto__ = null;" |
| 19932 | "f(obj); f(obj); f(obj);"); |
| 19933 | if (polymorphic) { |
| 19934 | CompileRun("f({});"); |
| 19935 | } |
| 19936 | CompileRun("obj.y = void 0;" |
| 19937 | "%OptimizeFunctionOnNextCall(f);"); |
| 19938 | if (remove_accessor) { |
| 19939 | CompileRun("delete obj.foo;"); |
| 19940 | } |
| 19941 | CompileRun("var result = f(obj);"); |
| 19942 | if (do_store) { |
| 19943 | CompileRun("result = obj.y;"); |
| 19944 | } |
| 19945 | if (remove_accessor && !interceptor) { |
| 19946 | CHECK(context->Global()->Get(v8_str("result"))->IsUndefined()); |
| 19947 | } else { |
| 19948 | CHECK_EQ(do_store ? 23 : 42, |
| 19949 | context->Global()->Get(v8_str("result"))->Int32Value()); |
| 19950 | } |
| 19951 | } |
| 19952 | |
| 19953 | |
| 19954 | THREADED_TEST(Regress137002a) { |
| 19955 | i::FLAG_allow_natives_syntax = true; |
| 19956 | i::FLAG_compilation_cache = false; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 19957 | v8::HandleScope scope(CcTest::isolate()); |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19958 | for (int i = 0; i < 16; i++) { |
| 19959 | Helper137002(i & 8, i & 4, i & 2, i & 1); |
| 19960 | } |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19961 | } |
| 19962 | |
| 19963 | |
| 19964 | THREADED_TEST(Regress137002b) { |
| 19965 | i::FLAG_allow_natives_syntax = true; |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19966 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 19967 | v8::HandleScope scope(context->GetIsolate()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19968 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 19969 | templ->SetAccessor(v8_str("foo"), |
| 19970 | GetterWhichReturns42, |
| 19971 | SetterWhichSetsYOnThisTo23); |
| 19972 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 19973 | |
| 19974 | // Turn monomorphic on slow object with native accessor, then just |
| 19975 | // delete the property and fail. |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19976 | CompileRun("function load(x) { return x.foo; }" |
| 19977 | "function store(x) { x.foo = void 0; }" |
| 19978 | "function keyed_load(x, key) { return x[key]; }" |
| 19979 | // Second version of function has a different source (add void 0) |
| 19980 | // so that it does not share code with the first version. This |
| 19981 | // ensures that the ICs are monomorphic. |
| 19982 | "function load2(x) { void 0; return x.foo; }" |
| 19983 | "function store2(x) { void 0; x.foo = void 0; }" |
| 19984 | "function keyed_load2(x, key) { void 0; return x[key]; }" |
| 19985 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 19986 | "obj.y = void 0;" |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 19987 | "obj.__proto__ = null;" |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19988 | "var subobj = {};" |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 19989 | "subobj.y = void 0;" |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19990 | "subobj.__proto__ = obj;" |
| 19991 | "%OptimizeObjectForAddingMultipleProperties(obj, 1);" |
| 19992 | |
| 19993 | // Make the ICs monomorphic. |
| 19994 | "load(obj); load(obj);" |
| 19995 | "load2(subobj); load2(subobj);" |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 19996 | "store(obj); store(obj);" |
| 19997 | "store2(subobj); store2(subobj);" |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 19998 | "keyed_load(obj, 'foo'); keyed_load(obj, 'foo');" |
| 19999 | "keyed_load2(subobj, 'foo'); keyed_load2(subobj, 'foo');" |
| 20000 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 20001 | // Actually test the shiny new ICs and better not crash. This |
| 20002 | // serves as a regression test for issue 142088 as well. |
| 20003 | "load(obj);" |
| 20004 | "load2(subobj);" |
| 20005 | "store(obj);" |
| 20006 | "store2(subobj);" |
| 20007 | "keyed_load(obj, 'foo');" |
| 20008 | "keyed_load2(subobj, 'foo');" |
| 20009 | |
erik.corry@gmail.com | 8876724 | 2012-08-08 14:43:45 +0000 | [diff] [blame] | 20010 | // Delete the accessor. It better not be called any more now. |
| 20011 | "delete obj.foo;" |
| 20012 | "obj.y = void 0;" |
| 20013 | "subobj.y = void 0;" |
| 20014 | |
| 20015 | "var load_result = load(obj);" |
| 20016 | "var load_result2 = load2(subobj);" |
| 20017 | "var keyed_load_result = keyed_load(obj, 'foo');" |
| 20018 | "var keyed_load_result2 = keyed_load2(subobj, 'foo');" |
| 20019 | "store(obj);" |
| 20020 | "store2(subobj);" |
| 20021 | "var y_from_obj = obj.y;" |
| 20022 | "var y_from_subobj = subobj.y;"); |
| 20023 | CHECK(context->Global()->Get(v8_str("load_result"))->IsUndefined()); |
| 20024 | CHECK(context->Global()->Get(v8_str("load_result2"))->IsUndefined()); |
| 20025 | CHECK(context->Global()->Get(v8_str("keyed_load_result"))->IsUndefined()); |
| 20026 | CHECK(context->Global()->Get(v8_str("keyed_load_result2"))->IsUndefined()); |
| 20027 | CHECK(context->Global()->Get(v8_str("y_from_obj"))->IsUndefined()); |
| 20028 | CHECK(context->Global()->Get(v8_str("y_from_subobj"))->IsUndefined()); |
jkummerow@chromium.org | 28583c9 | 2012-07-16 11:31:55 +0000 | [diff] [blame] | 20029 | } |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 20030 | |
| 20031 | |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 20032 | THREADED_TEST(Regress142088) { |
| 20033 | i::FLAG_allow_natives_syntax = true; |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 20034 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 20035 | v8::HandleScope scope(context->GetIsolate()); |
verwaest@chromium.org | de64f72 | 2012-08-16 15:44:54 +0000 | [diff] [blame] | 20036 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 20037 | templ->SetAccessor(v8_str("foo"), |
| 20038 | GetterWhichReturns42, |
| 20039 | SetterWhichSetsYOnThisTo23); |
| 20040 | context->Global()->Set(v8_str("obj"), templ->NewInstance()); |
| 20041 | |
| 20042 | CompileRun("function load(x) { return x.foo; }" |
| 20043 | "var o = Object.create(obj);" |
| 20044 | "%OptimizeObjectForAddingMultipleProperties(obj, 1);" |
| 20045 | "load(o); load(o); load(o); load(o);"); |
| 20046 | } |
| 20047 | |
| 20048 | |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 20049 | THREADED_TEST(Regress137496) { |
| 20050 | i::FLAG_expose_gc = true; |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 20051 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 20052 | v8::HandleScope scope(context->GetIsolate()); |
verwaest@chromium.org | 753aee4 | 2012-07-17 16:15:42 +0000 | [diff] [blame] | 20053 | |
| 20054 | // Compile a try-finally clause where the finally block causes a GC |
| 20055 | // while there still is a message pending for external reporting. |
| 20056 | TryCatch try_catch; |
| 20057 | try_catch.SetVerbose(true); |
| 20058 | CompileRun("try { throw new Error(); } finally { gc(); }"); |
| 20059 | CHECK(try_catch.HasCaught()); |
| 20060 | } |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20061 | |
| 20062 | |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 20063 | THREADED_TEST(Regress149912) { |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 20064 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 20065 | v8::HandleScope scope(context->GetIsolate()); |
verwaest@chromium.org | 33e09c8 | 2012-10-10 17:07:22 +0000 | [diff] [blame] | 20066 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20067 | AddInterceptor(templ, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 20068 | context->Global()->Set(v8_str("Bug"), templ->GetFunction()); |
| 20069 | CompileRun("Number.prototype.__proto__ = new Bug; var x = 0; x.foo();"); |
| 20070 | } |
| 20071 | |
| 20072 | |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 20073 | THREADED_TEST(Regress157124) { |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 20074 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 20075 | v8::HandleScope scope(context->GetIsolate()); |
rossberg@chromium.org | 89e18f5 | 2012-10-22 13:09:53 +0000 | [diff] [blame] | 20076 | Local<ObjectTemplate> templ = ObjectTemplate::New(); |
| 20077 | Local<Object> obj = templ->NewInstance(); |
| 20078 | obj->GetIdentityHash(); |
| 20079 | obj->DeleteHiddenValue(v8_str("Bug")); |
| 20080 | } |
| 20081 | |
| 20082 | |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 20083 | THREADED_TEST(Regress2535) { |
| 20084 | i::FLAG_harmony_collections = true; |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 20085 | LocalContext context; |
svenpanne@chromium.org | 2bda543 | 2013-03-15 12:39:50 +0000 | [diff] [blame] | 20086 | v8::HandleScope scope(context->GetIsolate()); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 20087 | Local<Value> set_value = CompileRun("new Set();"); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 20088 | Local<Object> set_object(Local<Object>::Cast(set_value)); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 20089 | CHECK_EQ(0, set_object->InternalFieldCount()); |
| 20090 | Local<Value> map_value = CompileRun("new Map();"); |
danno@chromium.org | f95d4b9 | 2013-06-13 14:40:17 +0000 | [diff] [blame] | 20091 | Local<Object> map_object(Local<Object>::Cast(map_value)); |
ulan@chromium.org | 6e196bf | 2013-03-13 09:38:22 +0000 | [diff] [blame] | 20092 | CHECK_EQ(0, map_object->InternalFieldCount()); |
| 20093 | } |
| 20094 | |
| 20095 | |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 20096 | THREADED_TEST(Regress2746) { |
| 20097 | LocalContext context; |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 20098 | v8::Isolate* isolate = context->GetIsolate(); |
| 20099 | v8::HandleScope scope(isolate); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 20100 | Local<Object> obj = Object::New(); |
| 20101 | Local<String> key = String::New("key"); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 20102 | obj->SetHiddenValue(key, v8::Undefined(isolate)); |
mstarzinger@chromium.org | 1510d58 | 2013-06-28 14:00:48 +0000 | [diff] [blame] | 20103 | Local<Value> value = obj->GetHiddenValue(key); |
| 20104 | CHECK(!value.IsEmpty()); |
| 20105 | CHECK(value->IsUndefined()); |
| 20106 | } |
| 20107 | |
| 20108 | |
jkummerow@chromium.org | 1048047 | 2013-07-17 08:22:15 +0000 | [diff] [blame] | 20109 | THREADED_TEST(Regress260106) { |
| 20110 | LocalContext context; |
| 20111 | v8::HandleScope scope(context->GetIsolate()); |
| 20112 | Local<FunctionTemplate> templ = FunctionTemplate::New(DummyCallHandler); |
| 20113 | CompileRun("for (var i = 0; i < 128; i++) Object.prototype[i] = 0;"); |
| 20114 | Local<Function> function = templ->GetFunction(); |
| 20115 | CHECK(!function.IsEmpty()); |
| 20116 | CHECK(function->IsFunction()); |
| 20117 | } |
| 20118 | |
| 20119 | |
danno@chromium.org | 5940060 | 2013-08-13 17:09:37 +0000 | [diff] [blame] | 20120 | THREADED_TEST(JSONParseObject) { |
| 20121 | LocalContext context; |
| 20122 | HandleScope scope(context->GetIsolate()); |
| 20123 | Local<Value> obj = v8::JSON::Parse(v8_str("{\"x\":42}")); |
| 20124 | Handle<Object> global = context->Global(); |
| 20125 | global->Set(v8_str("obj"), obj); |
| 20126 | ExpectString("JSON.stringify(obj)", "{\"x\":42}"); |
| 20127 | } |
| 20128 | |
| 20129 | |
| 20130 | THREADED_TEST(JSONParseNumber) { |
| 20131 | LocalContext context; |
| 20132 | HandleScope scope(context->GetIsolate()); |
| 20133 | Local<Value> obj = v8::JSON::Parse(v8_str("42")); |
| 20134 | Handle<Object> global = context->Global(); |
| 20135 | global->Set(v8_str("obj"), obj); |
| 20136 | ExpectString("JSON.stringify(obj)", "42"); |
| 20137 | } |
| 20138 | |
| 20139 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 20140 | #if V8_OS_POSIX |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20141 | class ThreadInterruptTest { |
| 20142 | public: |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20143 | ThreadInterruptTest() : sem_(0), sem_value_(0) { } |
| 20144 | ~ThreadInterruptTest() {} |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20145 | |
| 20146 | void RunTest() { |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20147 | InterruptThread i_thread(this); |
| 20148 | i_thread.Start(); |
| 20149 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20150 | sem_.Wait(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20151 | CHECK_EQ(kExpectedValue, sem_value_); |
| 20152 | } |
| 20153 | |
| 20154 | private: |
| 20155 | static const int kExpectedValue = 1; |
| 20156 | |
| 20157 | class InterruptThread : public i::Thread { |
| 20158 | public: |
| 20159 | explicit InterruptThread(ThreadInterruptTest* test) |
| 20160 | : Thread("InterruptThread"), test_(test) {} |
| 20161 | |
| 20162 | virtual void Run() { |
| 20163 | struct sigaction action; |
| 20164 | |
| 20165 | // Ensure that we'll enter waiting condition |
| 20166 | i::OS::Sleep(100); |
| 20167 | |
| 20168 | // Setup signal handler |
| 20169 | memset(&action, 0, sizeof(action)); |
| 20170 | action.sa_handler = SignalHandler; |
| 20171 | sigaction(SIGCHLD, &action, NULL); |
| 20172 | |
| 20173 | // Send signal |
| 20174 | kill(getpid(), SIGCHLD); |
| 20175 | |
| 20176 | // Ensure that if wait has returned because of error |
| 20177 | i::OS::Sleep(100); |
| 20178 | |
| 20179 | // Set value and signal semaphore |
| 20180 | test_->sem_value_ = 1; |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20181 | test_->sem_.Signal(); |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20182 | } |
| 20183 | |
| 20184 | static void SignalHandler(int signal) { |
| 20185 | } |
| 20186 | |
| 20187 | private: |
| 20188 | ThreadInterruptTest* test_; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20189 | }; |
| 20190 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20191 | i::Semaphore sem_; |
yangguo@chromium.org | 46839fb | 2012-08-28 09:06:19 +0000 | [diff] [blame] | 20192 | volatile int sem_value_; |
| 20193 | }; |
| 20194 | |
| 20195 | |
| 20196 | THREADED_TEST(SemaphoreInterruption) { |
| 20197 | ThreadInterruptTest().RunTest(); |
| 20198 | } |
yangguo@chromium.org | 46a2a51 | 2013-01-18 16:29:40 +0000 | [diff] [blame] | 20199 | |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 20200 | |
| 20201 | static bool NamedAccessAlwaysBlocked(Local<v8::Object> global, |
| 20202 | Local<Value> name, |
| 20203 | v8::AccessType type, |
| 20204 | Local<Value> data) { |
| 20205 | i::PrintF("Named access blocked.\n"); |
| 20206 | return false; |
| 20207 | } |
| 20208 | |
| 20209 | |
| 20210 | static bool IndexAccessAlwaysBlocked(Local<v8::Object> global, |
| 20211 | uint32_t key, |
| 20212 | v8::AccessType type, |
| 20213 | Local<Value> data) { |
| 20214 | i::PrintF("Indexed access blocked.\n"); |
| 20215 | return false; |
| 20216 | } |
| 20217 | |
| 20218 | |
| 20219 | void UnreachableCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 20220 | CHECK(false); |
| 20221 | } |
| 20222 | |
| 20223 | |
| 20224 | TEST(JSONStringifyAccessCheck) { |
| 20225 | v8::V8::Initialize(); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20226 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 20227 | |
| 20228 | // Create an ObjectTemplate for global objects and install access |
| 20229 | // check callbacks that will block access. |
| 20230 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 20231 | global_template->SetAccessCheckCallbacks(NamedAccessAlwaysBlocked, |
| 20232 | IndexAccessAlwaysBlocked); |
| 20233 | |
| 20234 | // Create a context and set an x property on it's global object. |
| 20235 | LocalContext context0(NULL, global_template); |
| 20236 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 20237 | global0->Set(v8_str("x"), v8_num(42)); |
| 20238 | ExpectString("JSON.stringify(this)", "{\"x\":42}"); |
| 20239 | |
| 20240 | for (int i = 0; i < 2; i++) { |
| 20241 | if (i == 1) { |
| 20242 | // Install a toJSON function on the second run. |
| 20243 | v8::Handle<v8::FunctionTemplate> toJSON = |
| 20244 | v8::FunctionTemplate::New(UnreachableCallback); |
| 20245 | |
| 20246 | global0->Set(v8_str("toJSON"), toJSON->GetFunction()); |
| 20247 | } |
| 20248 | // Create a context with a different security token so that the |
| 20249 | // failed access check callback will be called on each access. |
| 20250 | LocalContext context1(NULL, global_template); |
| 20251 | context1->Global()->Set(v8_str("other"), global0); |
| 20252 | |
| 20253 | ExpectString("JSON.stringify(other)", "{}"); |
| 20254 | ExpectString("JSON.stringify({ 'a' : other, 'b' : ['c'] })", |
| 20255 | "{\"a\":{},\"b\":[\"c\"]}"); |
| 20256 | ExpectString("JSON.stringify([other, 'b', 'c'])", |
| 20257 | "[{},\"b\",\"c\"]"); |
| 20258 | |
| 20259 | v8::Handle<v8::Array> array = v8::Array::New(2); |
| 20260 | array->Set(0, v8_str("a")); |
| 20261 | array->Set(1, v8_str("b")); |
| 20262 | context1->Global()->Set(v8_str("array"), array); |
| 20263 | ExpectString("JSON.stringify(array)", "[\"a\",\"b\"]"); |
| 20264 | array->TurnOnAccessCheck(); |
| 20265 | ExpectString("JSON.stringify(array)", "[]"); |
| 20266 | ExpectString("JSON.stringify([array])", "[[]]"); |
| 20267 | ExpectString("JSON.stringify({'a' : array})", "{\"a\":[]}"); |
| 20268 | } |
| 20269 | } |
| 20270 | |
| 20271 | |
| 20272 | bool access_check_fail_thrown = false; |
| 20273 | bool catch_callback_called = false; |
| 20274 | |
| 20275 | |
| 20276 | // Failed access check callback that performs a GC on each invocation. |
| 20277 | void FailedAccessCheckThrows(Local<v8::Object> target, |
| 20278 | v8::AccessType type, |
| 20279 | Local<v8::Value> data) { |
| 20280 | access_check_fail_thrown = true; |
| 20281 | i::PrintF("Access check failed. Error thrown.\n"); |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 20282 | CcTest::isolate()->ThrowException( |
| 20283 | v8::Exception::Error(v8_str("cross context"))); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 20284 | } |
| 20285 | |
| 20286 | |
| 20287 | void CatcherCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 20288 | for (int i = 0; i < args.Length(); i++) { |
| 20289 | i::PrintF("%s\n", *String::Utf8Value(args[i])); |
| 20290 | } |
| 20291 | catch_callback_called = true; |
| 20292 | } |
| 20293 | |
| 20294 | |
| 20295 | void HasOwnPropertyCallback(const v8::FunctionCallbackInfo<v8::Value>& args) { |
| 20296 | args[0]->ToObject()->HasOwnProperty(args[1]->ToString()); |
| 20297 | } |
| 20298 | |
| 20299 | |
| 20300 | void CheckCorrectThrow(const char* script) { |
| 20301 | // Test that the script, when wrapped into a try-catch, triggers the catch |
| 20302 | // clause due to failed access check throwing an exception. |
| 20303 | // The subsequent try-catch should run without any exception. |
| 20304 | access_check_fail_thrown = false; |
| 20305 | catch_callback_called = false; |
| 20306 | i::ScopedVector<char> source(1024); |
| 20307 | i::OS::SNPrintF(source, "try { %s; } catch (e) { catcher(e); }", script); |
| 20308 | CompileRun(source.start()); |
| 20309 | CHECK(access_check_fail_thrown); |
| 20310 | CHECK(catch_callback_called); |
| 20311 | |
| 20312 | access_check_fail_thrown = false; |
| 20313 | catch_callback_called = false; |
| 20314 | CompileRun("try { [1, 2, 3].sort(); } catch (e) { catcher(e) };"); |
| 20315 | CHECK(!access_check_fail_thrown); |
| 20316 | CHECK(!catch_callback_called); |
| 20317 | } |
| 20318 | |
| 20319 | |
| 20320 | TEST(AccessCheckThrows) { |
| 20321 | i::FLAG_allow_natives_syntax = true; |
| 20322 | v8::V8::Initialize(); |
| 20323 | v8::V8::SetFailedAccessCheckCallbackFunction(&FailedAccessCheckThrows); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20324 | v8::HandleScope scope(CcTest::isolate()); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 20325 | |
| 20326 | // Create an ObjectTemplate for global objects and install access |
| 20327 | // check callbacks that will block access. |
| 20328 | v8::Handle<v8::ObjectTemplate> global_template = v8::ObjectTemplate::New(); |
| 20329 | global_template->SetAccessCheckCallbacks(NamedAccessAlwaysBlocked, |
| 20330 | IndexAccessAlwaysBlocked); |
| 20331 | |
| 20332 | // Create a context and set an x property on it's global object. |
| 20333 | LocalContext context0(NULL, global_template); |
| 20334 | context0->Global()->Set(v8_str("x"), v8_num(42)); |
| 20335 | v8::Handle<v8::Object> global0 = context0->Global(); |
| 20336 | |
| 20337 | // Create a context with a different security token so that the |
| 20338 | // failed access check callback will be called on each access. |
| 20339 | LocalContext context1(NULL, global_template); |
| 20340 | context1->Global()->Set(v8_str("other"), global0); |
| 20341 | |
| 20342 | v8::Handle<v8::FunctionTemplate> catcher_fun = |
| 20343 | v8::FunctionTemplate::New(CatcherCallback); |
| 20344 | context1->Global()->Set(v8_str("catcher"), catcher_fun->GetFunction()); |
| 20345 | |
| 20346 | v8::Handle<v8::FunctionTemplate> has_own_property_fun = |
| 20347 | v8::FunctionTemplate::New(HasOwnPropertyCallback); |
| 20348 | context1->Global()->Set(v8_str("has_own_property"), |
| 20349 | has_own_property_fun->GetFunction()); |
| 20350 | |
| 20351 | { v8::TryCatch try_catch; |
| 20352 | access_check_fail_thrown = false; |
| 20353 | CompileRun("other.x;"); |
| 20354 | CHECK(access_check_fail_thrown); |
| 20355 | CHECK(try_catch.HasCaught()); |
| 20356 | } |
| 20357 | |
| 20358 | CheckCorrectThrow("other.x"); |
| 20359 | CheckCorrectThrow("other[1]"); |
| 20360 | CheckCorrectThrow("JSON.stringify(other)"); |
| 20361 | CheckCorrectThrow("has_own_property(other, 'x')"); |
| 20362 | CheckCorrectThrow("%GetProperty(other, 'x')"); |
| 20363 | CheckCorrectThrow("%SetProperty(other, 'x', 'foo', 1, 0)"); |
| 20364 | CheckCorrectThrow("%IgnoreAttributesAndSetProperty(other, 'x', 'foo')"); |
| 20365 | CheckCorrectThrow("%DeleteProperty(other, 'x', 0)"); |
| 20366 | CheckCorrectThrow("%DeleteProperty(other, '1', 0)"); |
| 20367 | CheckCorrectThrow("%HasLocalProperty(other, 'x')"); |
| 20368 | CheckCorrectThrow("%HasProperty(other, 'x')"); |
| 20369 | CheckCorrectThrow("%HasElement(other, 1)"); |
| 20370 | CheckCorrectThrow("%IsPropertyEnumerable(other, 'x')"); |
| 20371 | CheckCorrectThrow("%GetPropertyNames(other)"); |
| 20372 | CheckCorrectThrow("%GetLocalPropertyNames(other, true)"); |
| 20373 | CheckCorrectThrow("%DefineOrRedefineAccessorProperty(" |
| 20374 | "other, 'x', null, null, 1)"); |
verwaest@chromium.org | 32cb9b2 | 2013-08-21 11:18:12 +0000 | [diff] [blame] | 20375 | |
| 20376 | // Reset the failed access check callback so it does not influence |
| 20377 | // the other tests. |
| 20378 | v8::V8::SetFailedAccessCheckCallbackFunction(NULL); |
danno@chromium.org | 169691d | 2013-07-15 08:01:13 +0000 | [diff] [blame] | 20379 | } |
| 20380 | |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20381 | |
| 20382 | THREADED_TEST(Regress256330) { |
| 20383 | i::FLAG_allow_natives_syntax = true; |
| 20384 | LocalContext context; |
| 20385 | v8::HandleScope scope(context->GetIsolate()); |
| 20386 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20387 | AddInterceptor(templ, EmptyInterceptorGetter, EmptyInterceptorSetter); |
| 20388 | context->Global()->Set(v8_str("Bug"), templ->GetFunction()); |
| 20389 | CompileRun("\"use strict\"; var o = new Bug;" |
| 20390 | "function f(o) { o.x = 10; };" |
| 20391 | "f(o); f(o); f(o);" |
| 20392 | "%OptimizeFunctionOnNextCall(f);" |
| 20393 | "f(o);"); |
| 20394 | ExpectBoolean("%GetOptimizationStatus(f) != 2", true); |
| 20395 | } |
| 20396 | |
| 20397 | |
| 20398 | THREADED_TEST(CrankshaftInterceptorSetter) { |
| 20399 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20400 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20401 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20402 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 20403 | LocalContext env; |
| 20404 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 20405 | CompileRun("var obj = new Obj;" |
| 20406 | // Initialize fields to avoid transitions later. |
| 20407 | "obj.age = 0;" |
| 20408 | "obj.accessor_age = 42;" |
| 20409 | "function setter(i) { this.accessor_age = i; };" |
| 20410 | "function getter() { return this.accessor_age; };" |
| 20411 | "function setAge(i) { obj.age = i; };" |
| 20412 | "Object.defineProperty(obj, 'age', { get:getter, set:setter });" |
| 20413 | "setAge(1);" |
| 20414 | "setAge(2);" |
| 20415 | "setAge(3);" |
| 20416 | "%OptimizeFunctionOnNextCall(setAge);" |
| 20417 | "setAge(4);"); |
| 20418 | // All stores went through the interceptor. |
| 20419 | ExpectInt32("obj.interceptor_age", 4); |
| 20420 | ExpectInt32("obj.accessor_age", 42); |
| 20421 | } |
| 20422 | |
| 20423 | |
| 20424 | THREADED_TEST(CrankshaftInterceptorGetter) { |
| 20425 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20426 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20427 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20428 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 20429 | LocalContext env; |
| 20430 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 20431 | CompileRun("var obj = new Obj;" |
| 20432 | // Initialize fields to avoid transitions later. |
| 20433 | "obj.age = 1;" |
| 20434 | "obj.accessor_age = 42;" |
| 20435 | "function getter() { return this.accessor_age; };" |
| 20436 | "function getAge() { return obj.interceptor_age; };" |
| 20437 | "Object.defineProperty(obj, 'interceptor_age', { get:getter });" |
| 20438 | "getAge();" |
| 20439 | "getAge();" |
| 20440 | "getAge();" |
| 20441 | "%OptimizeFunctionOnNextCall(getAge);"); |
| 20442 | // Access through interceptor. |
| 20443 | ExpectInt32("getAge()", 1); |
| 20444 | } |
| 20445 | |
| 20446 | |
| 20447 | THREADED_TEST(CrankshaftInterceptorFieldRead) { |
| 20448 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20449 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20450 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20451 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 20452 | LocalContext env; |
| 20453 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 20454 | CompileRun("var obj = new Obj;" |
| 20455 | "obj.__proto__.interceptor_age = 42;" |
| 20456 | "obj.age = 100;" |
| 20457 | "function getAge() { return obj.interceptor_age; };"); |
| 20458 | ExpectInt32("getAge();", 100); |
| 20459 | ExpectInt32("getAge();", 100); |
| 20460 | ExpectInt32("getAge();", 100); |
| 20461 | CompileRun("%OptimizeFunctionOnNextCall(getAge);"); |
| 20462 | // Access through interceptor. |
| 20463 | ExpectInt32("getAge();", 100); |
| 20464 | } |
| 20465 | |
| 20466 | |
| 20467 | THREADED_TEST(CrankshaftInterceptorFieldWrite) { |
| 20468 | i::FLAG_allow_natives_syntax = true; |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20469 | v8::HandleScope scope(CcTest::isolate()); |
mstarzinger@chromium.org | e900018 | 2013-09-03 11:25:39 +0000 | [diff] [blame] | 20470 | Handle<FunctionTemplate> templ = FunctionTemplate::New(); |
| 20471 | AddInterceptor(templ, InterceptorGetter, InterceptorSetter); |
| 20472 | LocalContext env; |
| 20473 | env->Global()->Set(v8_str("Obj"), templ->GetFunction()); |
| 20474 | CompileRun("var obj = new Obj;" |
| 20475 | "obj.age = 100000;" |
| 20476 | "function setAge(i) { obj.age = i };" |
| 20477 | "setAge(100);" |
| 20478 | "setAge(101);" |
| 20479 | "setAge(102);" |
| 20480 | "%OptimizeFunctionOnNextCall(setAge);" |
| 20481 | "setAge(103);"); |
| 20482 | ExpectInt32("obj.age", 100000); |
| 20483 | ExpectInt32("obj.interceptor_age", 103); |
| 20484 | } |
| 20485 | |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 20486 | |
verwaest@chromium.org | 662436e | 2013-08-28 08:41:27 +0000 | [diff] [blame] | 20487 | #endif // V8_OS_POSIX |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 20488 | |
| 20489 | |
| 20490 | static Local<Value> function_new_expected_env; |
| 20491 | static void FunctionNewCallback(const v8::FunctionCallbackInfo<Value>& info) { |
| 20492 | CHECK_EQ(function_new_expected_env, info.Data()); |
| 20493 | info.GetReturnValue().Set(17); |
| 20494 | } |
| 20495 | |
| 20496 | |
| 20497 | THREADED_TEST(FunctionNew) { |
| 20498 | LocalContext env; |
| 20499 | v8::Isolate* isolate = env->GetIsolate(); |
| 20500 | v8::HandleScope scope(isolate); |
| 20501 | Local<Object> data = v8::Object::New(); |
| 20502 | function_new_expected_env = data; |
| 20503 | Local<Function> func = Function::New(isolate, FunctionNewCallback, data); |
| 20504 | env->Global()->Set(v8_str("func"), func); |
| 20505 | Local<Value> result = CompileRun("func();"); |
| 20506 | CHECK_EQ(v8::Integer::New(17, isolate), result); |
| 20507 | // Verify function not cached |
| 20508 | int serial_number = |
| 20509 | i::Smi::cast(v8::Utils::OpenHandle(*func) |
| 20510 | ->shared()->get_api_func_data()->serial_number())->value(); |
| 20511 | i::Isolate* i_isolate = reinterpret_cast<i::Isolate*>(isolate); |
| 20512 | i::Object* elm = i_isolate->native_context()->function_cache() |
| 20513 | ->GetElementNoExceptionThrown(i_isolate, serial_number); |
machenbach@chromium.org | 528ce02 | 2013-09-23 14:09:36 +0000 | [diff] [blame] | 20514 | CHECK(elm->IsUndefined()); |
| 20515 | // Verify that each Function::New creates a new function instance |
| 20516 | Local<Object> data2 = v8::Object::New(); |
| 20517 | function_new_expected_env = data2; |
| 20518 | Local<Function> func2 = Function::New(isolate, FunctionNewCallback, data2); |
| 20519 | CHECK(!func2->IsNull()); |
| 20520 | CHECK_NE(func, func2); |
| 20521 | env->Global()->Set(v8_str("func2"), func2); |
| 20522 | Local<Value> result2 = CompileRun("func2();"); |
| 20523 | CHECK_EQ(v8::Integer::New(17, isolate), result2); |
dslomov@chromium.org | 639bac0 | 2013-09-09 11:58:54 +0000 | [diff] [blame] | 20524 | } |
| 20525 | |
machenbach@chromium.org | cfdf67d | 2013-09-27 07:27:26 +0000 | [diff] [blame] | 20526 | |
| 20527 | TEST(EscapeableHandleScope) { |
| 20528 | HandleScope outer_scope(CcTest::isolate()); |
| 20529 | LocalContext context; |
| 20530 | const int runs = 10; |
| 20531 | Local<String> values[runs]; |
| 20532 | for (int i = 0; i < runs; i++) { |
| 20533 | v8::EscapableHandleScope inner_scope(CcTest::isolate()); |
| 20534 | Local<String> value; |
| 20535 | if (i != 0) value = v8_str("escape value"); |
| 20536 | values[i] = inner_scope.Escape(value); |
| 20537 | } |
| 20538 | for (int i = 0; i < runs; i++) { |
| 20539 | Local<String> expected; |
| 20540 | if (i != 0) { |
| 20541 | CHECK_EQ(v8_str("escape value"), values[i]); |
| 20542 | } else { |
| 20543 | CHECK(values[i].IsEmpty()); |
| 20544 | } |
| 20545 | } |
| 20546 | } |