Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 1 | // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 4 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 5 | #include "src/execution.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 6 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 7 | #include "src/bootstrapper.h" |
| 8 | #include "src/codegen.h" |
| 9 | #include "src/deoptimizer.h" |
| 10 | #include "src/isolate-inl.h" |
| 11 | #include "src/vm-state-inl.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 12 | |
| 13 | namespace v8 { |
| 14 | namespace internal { |
| 15 | |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 16 | StackGuard::StackGuard() |
| 17 | : isolate_(NULL) { |
| 18 | } |
| 19 | |
| 20 | |
| 21 | void StackGuard::set_interrupt_limits(const ExecutionAccess& lock) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 22 | DCHECK(isolate_ != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 23 | thread_local_.jslimit_ = kInterruptLimit; |
| 24 | thread_local_.climit_ = kInterruptLimit; |
| 25 | isolate_->heap()->SetStackLimits(); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | void StackGuard::reset_limits(const ExecutionAccess& lock) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 30 | DCHECK(isolate_ != NULL); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 31 | thread_local_.jslimit_ = thread_local_.real_jslimit_; |
| 32 | thread_local_.climit_ = thread_local_.real_climit_; |
| 33 | isolate_->heap()->SetStackLimits(); |
| 34 | } |
| 35 | |
| 36 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 37 | static void PrintDeserializedCodeInfo(Handle<JSFunction> function) { |
| 38 | if (function->code() == function->shared()->code() && |
| 39 | function->shared()->deserialized()) { |
| 40 | PrintF("Running deserialized script "); |
| 41 | Object* script = function->shared()->script(); |
| 42 | if (script->IsScript()) Script::cast(script)->name()->ShortPrint(); |
| 43 | PrintF("\n"); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 48 | MUST_USE_RESULT static MaybeHandle<Object> Invoke( |
| 49 | bool is_construct, |
| 50 | Handle<JSFunction> function, |
| 51 | Handle<Object> receiver, |
| 52 | int argc, |
| 53 | Handle<Object> args[]) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 54 | Isolate* isolate = function->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 55 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 56 | // Entering JavaScript. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 57 | VMState<JS> state(isolate); |
| 58 | CHECK(AllowJavascriptExecution::IsAllowed(isolate)); |
| 59 | if (!ThrowOnJavascriptExecution::IsAllowed(isolate)) { |
| 60 | isolate->ThrowIllegalOperation(); |
| 61 | isolate->ReportPendingMessages(); |
| 62 | return MaybeHandle<Object>(); |
| 63 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 64 | |
| 65 | // Placeholder for return value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 66 | Object* value = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 67 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 68 | typedef Object* (*JSEntryFunction)(byte* entry, |
| 69 | Object* function, |
| 70 | Object* receiver, |
| 71 | int argc, |
| 72 | Object*** args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 73 | |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 74 | Handle<Code> code = is_construct |
| 75 | ? isolate->factory()->js_construct_entry_code() |
| 76 | : isolate->factory()->js_entry_code(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 77 | |
| 78 | // Convert calls on global objects to be calls on the global |
| 79 | // receiver instead to avoid having a 'this' pointer which refers |
| 80 | // directly to a global object. |
| 81 | if (receiver->IsGlobalObject()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 82 | receiver = handle(Handle<GlobalObject>::cast(receiver)->global_proxy()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 85 | // Make sure that the global object of the context we're about to |
| 86 | // make the current one is indeed a global object. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 87 | DCHECK(function->context()->global_object()->IsGlobalObject()); |
Leon Clarke | e46be81 | 2010-01-19 14:06:41 +0000 | [diff] [blame] | 88 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 89 | { |
| 90 | // Save and restore context around invocation and block the |
| 91 | // allocation of handles without explicit handle scopes. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 92 | SaveContext save(isolate); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 93 | SealHandleScope shs(isolate); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 94 | JSEntryFunction stub_entry = FUNCTION_CAST<JSEntryFunction>(code->entry()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 95 | |
| 96 | // Call the function through the right JS entry stub. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 97 | byte* function_entry = function->code()->entry(); |
| 98 | JSFunction* func = *function; |
| 99 | Object* recv = *receiver; |
| 100 | Object*** argv = reinterpret_cast<Object***>(args); |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 101 | if (FLAG_profile_deserialization) PrintDeserializedCodeInfo(function); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 102 | value = |
| 103 | CALL_GENERATED_CODE(stub_entry, function_entry, func, recv, argc, argv); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 106 | #ifdef VERIFY_HEAP |
| 107 | value->ObjectVerify(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 108 | #endif |
| 109 | |
| 110 | // Update the pending exception flag and return the value. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 111 | bool has_exception = value->IsException(); |
| 112 | DCHECK(has_exception == isolate->has_pending_exception()); |
| 113 | if (has_exception) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 114 | isolate->ReportPendingMessages(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 115 | // Reset stepping state when script exits with uncaught exception. |
| 116 | if (isolate->debug()->is_active()) { |
| 117 | isolate->debug()->ClearStepping(); |
Ben Murdoch | e0cee9b | 2011-05-25 10:26:03 +0100 | [diff] [blame] | 118 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 119 | return MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 120 | } else { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 121 | isolate->clear_pending_message(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | } |
| 123 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 124 | return Handle<Object>(value, isolate); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 128 | MaybeHandle<Object> Execution::Call(Isolate* isolate, |
| 129 | Handle<Object> callable, |
| 130 | Handle<Object> receiver, |
| 131 | int argc, |
| 132 | Handle<Object> argv[], |
| 133 | bool convert_receiver) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 134 | if (!callable->IsJSFunction()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 135 | ASSIGN_RETURN_ON_EXCEPTION( |
| 136 | isolate, callable, TryGetFunctionDelegate(isolate, callable), Object); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 137 | } |
| 138 | Handle<JSFunction> func = Handle<JSFunction>::cast(callable); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 139 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 140 | // In sloppy mode, convert receiver. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 141 | if (convert_receiver && !receiver->IsJSReceiver() && |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 142 | !func->shared()->native() && |
| 143 | func->shared()->strict_mode() == SLOPPY) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 144 | if (receiver->IsUndefined() || receiver->IsNull()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 145 | receiver = handle(func->global_proxy()); |
| 146 | DCHECK(!receiver->IsJSBuiltinsObject()); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 147 | } else { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 148 | ASSIGN_RETURN_ON_EXCEPTION( |
| 149 | isolate, receiver, ToObject(isolate, receiver), Object); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 150 | } |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 153 | return Invoke(false, func, receiver, argc, argv); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 157 | MaybeHandle<Object> Execution::New(Handle<JSFunction> func, |
| 158 | int argc, |
| 159 | Handle<Object> argv[]) { |
| 160 | return Invoke(true, func, handle(func->global_proxy()), argc, argv); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 164 | MaybeHandle<Object> Execution::TryCall(Handle<JSFunction> func, |
| 165 | Handle<Object> receiver, int argc, |
| 166 | Handle<Object> args[], |
| 167 | MaybeHandle<Object>* exception_out) { |
| 168 | bool is_termination = false; |
| 169 | Isolate* isolate = func->GetIsolate(); |
| 170 | MaybeHandle<Object> maybe_result; |
| 171 | if (exception_out != NULL) *exception_out = MaybeHandle<Object>(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 172 | // Enter a try-block while executing the JavaScript code. To avoid |
| 173 | // duplicate error printing it must be non-verbose. Also, to avoid |
| 174 | // creating message objects during stack overflow we shouldn't |
| 175 | // capture messages. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 176 | { |
| 177 | v8::TryCatch catcher; |
| 178 | catcher.SetVerbose(false); |
| 179 | catcher.SetCaptureMessage(false); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 180 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 181 | maybe_result = Invoke(false, func, receiver, argc, args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 182 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 183 | if (maybe_result.is_null()) { |
| 184 | DCHECK(catcher.HasCaught()); |
| 185 | DCHECK(isolate->has_pending_exception()); |
| 186 | DCHECK(isolate->external_caught_exception()); |
| 187 | if (exception_out != NULL) { |
| 188 | if (isolate->pending_exception() == |
| 189 | isolate->heap()->termination_exception()) { |
| 190 | is_termination = true; |
| 191 | } else { |
| 192 | *exception_out = v8::Utils::OpenHandle(*catcher.Exception()); |
| 193 | } |
| 194 | } |
| 195 | isolate->OptionalRescheduleException(true); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 196 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 197 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 198 | DCHECK(!isolate->has_pending_exception()); |
| 199 | DCHECK(!isolate->external_caught_exception()); |
| 200 | } |
| 201 | if (is_termination) isolate->TerminateExecution(); |
| 202 | return maybe_result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 206 | Handle<Object> Execution::GetFunctionDelegate(Isolate* isolate, |
| 207 | Handle<Object> object) { |
| 208 | DCHECK(!object->IsJSFunction()); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 209 | Factory* factory = isolate->factory(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 210 | |
| 211 | // If you return a function from here, it will be called when an |
| 212 | // attempt is made to call the given object as a function. |
| 213 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 214 | // If object is a function proxy, get its handler. Iterate if necessary. |
| 215 | Object* fun = *object; |
| 216 | while (fun->IsJSFunctionProxy()) { |
| 217 | fun = JSFunctionProxy::cast(fun)->call_trap(); |
| 218 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 219 | if (fun->IsJSFunction()) return Handle<Object>(fun, isolate); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 220 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 221 | // Objects created through the API can have an instance-call handler |
| 222 | // that should be used when calling the object as a function. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 223 | if (fun->IsHeapObject() && |
| 224 | HeapObject::cast(fun)->map()->has_instance_call_handler()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | return Handle<JSFunction>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 226 | isolate->native_context()->call_as_function_delegate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 227 | } |
| 228 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 229 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 233 | MaybeHandle<Object> Execution::TryGetFunctionDelegate(Isolate* isolate, |
| 234 | Handle<Object> object) { |
| 235 | DCHECK(!object->IsJSFunction()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 236 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 237 | // If object is a function proxy, get its handler. Iterate if necessary. |
| 238 | Object* fun = *object; |
| 239 | while (fun->IsJSFunctionProxy()) { |
| 240 | fun = JSFunctionProxy::cast(fun)->call_trap(); |
| 241 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 242 | if (fun->IsJSFunction()) return Handle<Object>(fun, isolate); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 243 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 244 | // Objects created through the API can have an instance-call handler |
| 245 | // that should be used when calling the object as a function. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 246 | if (fun->IsHeapObject() && |
| 247 | HeapObject::cast(fun)->map()->has_instance_call_handler()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 248 | return Handle<JSFunction>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 249 | isolate->native_context()->call_as_function_delegate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // If the Object doesn't have an instance-call handler we should |
| 253 | // throw a non-callable exception. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 254 | THROW_NEW_ERROR(isolate, NewTypeError("called_non_callable", |
| 255 | i::HandleVector<i::Object>(&object, 1)), |
| 256 | Object); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 260 | Handle<Object> Execution::GetConstructorDelegate(Isolate* isolate, |
| 261 | Handle<Object> object) { |
| 262 | DCHECK(!object->IsJSFunction()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 263 | |
| 264 | // If you return a function from here, it will be called when an |
| 265 | // attempt is made to call the given object as a constructor. |
| 266 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 267 | // If object is a function proxies, get its handler. Iterate if necessary. |
| 268 | Object* fun = *object; |
| 269 | while (fun->IsJSFunctionProxy()) { |
| 270 | fun = JSFunctionProxy::cast(fun)->call_trap(); |
| 271 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 272 | if (fun->IsJSFunction()) return Handle<Object>(fun, isolate); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 273 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 274 | // Objects created through the API can have an instance-call handler |
| 275 | // that should be used when calling the object as a function. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 276 | if (fun->IsHeapObject() && |
| 277 | HeapObject::cast(fun)->map()->has_instance_call_handler()) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 278 | return Handle<JSFunction>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 279 | isolate->native_context()->call_as_constructor_delegate()); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 280 | } |
| 281 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 282 | return isolate->factory()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 286 | MaybeHandle<Object> Execution::TryGetConstructorDelegate( |
| 287 | Isolate* isolate, Handle<Object> object) { |
| 288 | DCHECK(!object->IsJSFunction()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 289 | |
| 290 | // If you return a function from here, it will be called when an |
| 291 | // attempt is made to call the given object as a constructor. |
| 292 | |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 293 | // If object is a function proxies, get its handler. Iterate if necessary. |
| 294 | Object* fun = *object; |
| 295 | while (fun->IsJSFunctionProxy()) { |
| 296 | fun = JSFunctionProxy::cast(fun)->call_trap(); |
| 297 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 298 | if (fun->IsJSFunction()) return Handle<Object>(fun, isolate); |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 299 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 300 | // Objects created through the API can have an instance-call handler |
| 301 | // that should be used when calling the object as a function. |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 302 | if (fun->IsHeapObject() && |
| 303 | HeapObject::cast(fun)->map()->has_instance_call_handler()) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 304 | return Handle<JSFunction>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 305 | isolate->native_context()->call_as_constructor_delegate()); |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | // If the Object doesn't have an instance-call handler we should |
| 309 | // throw a non-callable exception. |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 310 | THROW_NEW_ERROR(isolate, NewTypeError("called_non_callable", |
| 311 | i::HandleVector<i::Object>(&object, 1)), |
| 312 | Object); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | |
| 316 | void StackGuard::EnableInterrupts() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 317 | ExecutionAccess access(isolate_); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 318 | if (has_pending_interrupts(access)) { |
| 319 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | |
| 323 | |
| 324 | void StackGuard::SetStackLimit(uintptr_t limit) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 325 | ExecutionAccess access(isolate_); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 326 | // If the current limits are special (e.g. due to a pending interrupt) then |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 327 | // leave them alone. |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 328 | uintptr_t jslimit = SimulatorStack::JsLimitFromCLimit(isolate_, limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 329 | if (thread_local_.jslimit_ == thread_local_.real_jslimit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 330 | thread_local_.jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 331 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 332 | if (thread_local_.climit_ == thread_local_.real_climit_) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 333 | thread_local_.climit_ = limit; |
| 334 | } |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 335 | thread_local_.real_climit_ = limit; |
| 336 | thread_local_.real_jslimit_ = jslimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | |
| 340 | void StackGuard::DisableInterrupts() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 341 | ExecutionAccess access(isolate_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 342 | reset_limits(access); |
| 343 | } |
| 344 | |
| 345 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 346 | void StackGuard::PushPostponeInterruptsScope(PostponeInterruptsScope* scope) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 347 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 348 | // Intercept already requested interrupts. |
| 349 | int intercepted = thread_local_.interrupt_flags_ & scope->intercept_mask_; |
| 350 | scope->intercepted_flags_ = intercepted; |
| 351 | thread_local_.interrupt_flags_ &= ~intercepted; |
| 352 | if (!has_pending_interrupts(access)) reset_limits(access); |
| 353 | // Add scope to the chain. |
| 354 | scope->prev_ = thread_local_.postpone_interrupts_; |
| 355 | thread_local_.postpone_interrupts_ = scope; |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 356 | } |
| 357 | |
| 358 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 359 | void StackGuard::PopPostponeInterruptsScope() { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 360 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 361 | PostponeInterruptsScope* top = thread_local_.postpone_interrupts_; |
| 362 | // Make intercepted interrupts active. |
| 363 | DCHECK((thread_local_.interrupt_flags_ & top->intercept_mask_) == 0); |
| 364 | thread_local_.interrupt_flags_ |= top->intercepted_flags_; |
| 365 | if (has_pending_interrupts(access)) set_interrupt_limits(access); |
| 366 | // Remove scope from chain. |
| 367 | thread_local_.postpone_interrupts_ = top->prev_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 371 | bool StackGuard::CheckInterrupt(InterruptFlag flag) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 372 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 373 | return thread_local_.interrupt_flags_ & flag; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | void StackGuard::RequestInterrupt(InterruptFlag flag) { |
| 378 | ExecutionAccess access(isolate_); |
| 379 | // Check the chain of PostponeInterruptsScopes for interception. |
| 380 | if (thread_local_.postpone_interrupts_ && |
| 381 | thread_local_.postpone_interrupts_->Intercept(flag)) { |
| 382 | return; |
| 383 | } |
| 384 | |
| 385 | // Not intercepted. Set as active interrupt flag. |
| 386 | thread_local_.interrupt_flags_ |= flag; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 387 | set_interrupt_limits(access); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 391 | void StackGuard::ClearInterrupt(InterruptFlag flag) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 392 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 393 | // Clear the interrupt flag from the chain of PostponeInterruptsScopes. |
| 394 | for (PostponeInterruptsScope* current = thread_local_.postpone_interrupts_; |
| 395 | current != NULL; |
| 396 | current = current->prev_) { |
| 397 | current->intercepted_flags_ &= ~flag; |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 398 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 399 | |
| 400 | // Clear the interrupt flag from the active interrupt flags. |
| 401 | thread_local_.interrupt_flags_ &= ~flag; |
| 402 | if (!has_pending_interrupts(access)) reset_limits(access); |
Ben Murdoch | b0fe162 | 2011-05-05 13:52:32 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 406 | bool StackGuard::CheckAndClearInterrupt(InterruptFlag flag) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 407 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 408 | bool result = (thread_local_.interrupt_flags_ & flag); |
| 409 | thread_local_.interrupt_flags_ &= ~flag; |
| 410 | if (!has_pending_interrupts(access)) reset_limits(access); |
| 411 | return result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 415 | char* StackGuard::ArchiveStackGuard(char* to) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 416 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 417 | MemCopy(to, reinterpret_cast<char*>(&thread_local_), sizeof(ThreadLocal)); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 418 | ThreadLocal blank; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 419 | |
| 420 | // Set the stack limits using the old thread_local_. |
| 421 | // TODO(isolates): This was the old semantics of constructing a ThreadLocal |
| 422 | // (as the ctor called SetStackLimits, which looked at the |
| 423 | // current thread_local_ from StackGuard)-- but is this |
| 424 | // really what was intended? |
| 425 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 426 | thread_local_ = blank; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 427 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 428 | return to + sizeof(ThreadLocal); |
| 429 | } |
| 430 | |
| 431 | |
| 432 | char* StackGuard::RestoreStackGuard(char* from) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 433 | ExecutionAccess access(isolate_); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 434 | MemCopy(reinterpret_cast<char*>(&thread_local_), from, sizeof(ThreadLocal)); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 435 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 436 | return from + sizeof(ThreadLocal); |
| 437 | } |
| 438 | |
| 439 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 440 | void StackGuard::FreeThreadResources() { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 441 | Isolate::PerIsolateThreadData* per_thread = |
| 442 | isolate_->FindOrAllocatePerThreadDataForThisThread(); |
| 443 | per_thread->set_stack_limit(thread_local_.real_climit_); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | |
| 447 | void StackGuard::ThreadLocal::Clear() { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 448 | real_jslimit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 449 | jslimit_ = kIllegalLimit; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 450 | real_climit_ = kIllegalLimit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 451 | climit_ = kIllegalLimit; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 452 | postpone_interrupts_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 453 | interrupt_flags_ = 0; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 457 | bool StackGuard::ThreadLocal::Initialize(Isolate* isolate) { |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 458 | bool should_set_stack_limits = false; |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 459 | if (real_climit_ == kIllegalLimit) { |
Steve Block | 1e0659c | 2011-05-24 12:43:12 +0100 | [diff] [blame] | 460 | const uintptr_t kLimitSize = FLAG_stack_size * KB; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 461 | DCHECK(GetCurrentStackPosition() > kLimitSize); |
| 462 | uintptr_t limit = GetCurrentStackPosition() - kLimitSize; |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 463 | real_jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit); |
| 464 | jslimit_ = SimulatorStack::JsLimitFromCLimit(isolate, limit); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 465 | real_climit_ = limit; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 466 | climit_ = limit; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 467 | should_set_stack_limits = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 468 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 469 | postpone_interrupts_ = NULL; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 470 | interrupt_flags_ = 0; |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 471 | return should_set_stack_limits; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | |
| 475 | void StackGuard::ClearThread(const ExecutionAccess& lock) { |
| 476 | thread_local_.Clear(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 477 | isolate_->heap()->SetStackLimits(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | void StackGuard::InitThread(const ExecutionAccess& lock) { |
Ben Murdoch | 257744e | 2011-11-30 15:57:28 +0000 | [diff] [blame] | 482 | if (thread_local_.Initialize(isolate_)) isolate_->heap()->SetStackLimits(); |
| 483 | Isolate::PerIsolateThreadData* per_thread = |
| 484 | isolate_->FindOrAllocatePerThreadDataForThisThread(); |
| 485 | uintptr_t stored_limit = per_thread->stack_limit(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 486 | // You should hold the ExecutionAccess lock when you call this. |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 487 | if (stored_limit != 0) { |
Ben Murdoch | 3fb3ca8 | 2011-12-02 17:19:32 +0000 | [diff] [blame] | 488 | SetStackLimit(stored_limit); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | |
| 492 | |
| 493 | // --- C a l l s t o n a t i v e s --- |
| 494 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 495 | #define RETURN_NATIVE_CALL(name, args) \ |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 496 | do { \ |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 497 | Handle<Object> argv[] = args; \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 498 | return Call(isolate, \ |
| 499 | isolate->name##_fun(), \ |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 500 | isolate->js_builtins_object(), \ |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 501 | arraysize(argv), argv); \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 502 | } while (false) |
| 503 | |
| 504 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 505 | MaybeHandle<Object> Execution::ToNumber( |
| 506 | Isolate* isolate, Handle<Object> obj) { |
| 507 | RETURN_NATIVE_CALL(to_number, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 511 | MaybeHandle<Object> Execution::ToString( |
| 512 | Isolate* isolate, Handle<Object> obj) { |
| 513 | RETURN_NATIVE_CALL(to_string, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 517 | MaybeHandle<Object> Execution::ToDetailString( |
| 518 | Isolate* isolate, Handle<Object> obj) { |
| 519 | RETURN_NATIVE_CALL(to_detail_string, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 523 | MaybeHandle<Object> Execution::ToObject( |
| 524 | Isolate* isolate, Handle<Object> obj) { |
Ben Murdoch | 589d697 | 2011-11-30 16:04:58 +0000 | [diff] [blame] | 525 | if (obj->IsSpecObject()) return obj; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 526 | RETURN_NATIVE_CALL(to_object, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 530 | MaybeHandle<Object> Execution::ToInteger( |
| 531 | Isolate* isolate, Handle<Object> obj) { |
| 532 | RETURN_NATIVE_CALL(to_integer, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 536 | MaybeHandle<Object> Execution::ToUint32( |
| 537 | Isolate* isolate, Handle<Object> obj) { |
| 538 | RETURN_NATIVE_CALL(to_uint32, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 542 | MaybeHandle<Object> Execution::ToInt32( |
| 543 | Isolate* isolate, Handle<Object> obj) { |
| 544 | RETURN_NATIVE_CALL(to_int32, { obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 548 | MaybeHandle<Object> Execution::ToLength( |
| 549 | Isolate* isolate, Handle<Object> obj) { |
| 550 | RETURN_NATIVE_CALL(to_length, { obj }); |
| 551 | } |
| 552 | |
| 553 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 554 | MaybeHandle<Object> Execution::NewDate(Isolate* isolate, double time) { |
| 555 | Handle<Object> time_obj = isolate->factory()->NewNumber(time); |
| 556 | RETURN_NATIVE_CALL(create_date, { time_obj }); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | |
| 560 | #undef RETURN_NATIVE_CALL |
| 561 | |
| 562 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 563 | MaybeHandle<JSRegExp> Execution::NewJSRegExp(Handle<String> pattern, |
| 564 | Handle<String> flags) { |
| 565 | Isolate* isolate = pattern->GetIsolate(); |
Steve Block | 44f0eee | 2011-05-26 01:26:41 +0100 | [diff] [blame] | 566 | Handle<JSFunction> function = Handle<JSFunction>( |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 567 | isolate->native_context()->regexp_function()); |
| 568 | Handle<Object> re_obj; |
| 569 | ASSIGN_RETURN_ON_EXCEPTION( |
| 570 | isolate, re_obj, |
| 571 | RegExpImpl::CreateRegExpLiteral(function, pattern, flags), |
| 572 | JSRegExp); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 573 | return Handle<JSRegExp>::cast(re_obj); |
| 574 | } |
| 575 | |
| 576 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 577 | Handle<Object> Execution::CharAt(Handle<String> string, uint32_t index) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 578 | Isolate* isolate = string->GetIsolate(); |
| 579 | Factory* factory = isolate->factory(); |
| 580 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 581 | int int_index = static_cast<int>(index); |
| 582 | if (int_index < 0 || int_index >= string->length()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 583 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 586 | Handle<Object> char_at = Object::GetProperty( |
| 587 | isolate->js_builtins_object(), |
| 588 | factory->char_at_string()).ToHandleChecked(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 589 | if (!char_at->IsJSFunction()) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 590 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 593 | Handle<Object> index_object = factory->NewNumberFromInt(int_index); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 594 | Handle<Object> index_arg[] = { index_object }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 595 | Handle<Object> result; |
| 596 | if (!TryCall(Handle<JSFunction>::cast(char_at), |
| 597 | string, |
| 598 | arraysize(index_arg), |
| 599 | index_arg).ToHandle(&result)) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 600 | return factory->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 601 | } |
| 602 | return result; |
| 603 | } |
| 604 | |
| 605 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 606 | MaybeHandle<JSFunction> Execution::InstantiateFunction( |
| 607 | Handle<FunctionTemplateInfo> data) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 608 | Isolate* isolate = data->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 609 | if (!data->do_not_cache()) { |
| 610 | // Fast case: see if the function has already been instantiated |
| 611 | int serial_number = Smi::cast(data->serial_number())->value(); |
| 612 | Handle<JSObject> cache(isolate->native_context()->function_cache()); |
| 613 | Handle<Object> elm = |
| 614 | Object::GetElement(isolate, cache, serial_number).ToHandleChecked(); |
| 615 | if (elm->IsJSFunction()) return Handle<JSFunction>::cast(elm); |
| 616 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 617 | // The function has not yet been instantiated in this context; do it. |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 618 | Handle<Object> args[] = { data }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 619 | Handle<Object> result; |
| 620 | ASSIGN_RETURN_ON_EXCEPTION( |
| 621 | isolate, result, |
| 622 | Call(isolate, |
| 623 | isolate->instantiate_fun(), |
| 624 | isolate->js_builtins_object(), |
| 625 | arraysize(args), |
| 626 | args), |
| 627 | JSFunction); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 628 | return Handle<JSFunction>::cast(result); |
| 629 | } |
| 630 | |
| 631 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 632 | MaybeHandle<JSObject> Execution::InstantiateObject( |
| 633 | Handle<ObjectTemplateInfo> data) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 634 | Isolate* isolate = data->GetIsolate(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 635 | Handle<Object> result; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 636 | if (data->property_list()->IsUndefined() && |
| 637 | !data->constructor()->IsUndefined()) { |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 638 | Handle<FunctionTemplateInfo> cons_template = |
| 639 | Handle<FunctionTemplateInfo>( |
| 640 | FunctionTemplateInfo::cast(data->constructor())); |
| 641 | Handle<JSFunction> cons; |
| 642 | ASSIGN_RETURN_ON_EXCEPTION( |
| 643 | isolate, cons, InstantiateFunction(cons_template), JSObject); |
| 644 | ASSIGN_RETURN_ON_EXCEPTION(isolate, result, New(cons, 0, NULL), JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 645 | } else { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 646 | Handle<Object> args[] = { data }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 647 | ASSIGN_RETURN_ON_EXCEPTION( |
| 648 | isolate, result, |
| 649 | Call(isolate, |
| 650 | isolate->instantiate_fun(), |
| 651 | isolate->js_builtins_object(), |
| 652 | arraysize(args), |
| 653 | args), |
| 654 | JSObject); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 655 | } |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 656 | return Handle<JSObject>::cast(result); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 660 | MaybeHandle<Object> Execution::ConfigureInstance( |
| 661 | Isolate* isolate, |
| 662 | Handle<Object> instance, |
| 663 | Handle<Object> instance_template) { |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 664 | Handle<Object> args[] = { instance, instance_template }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 665 | return Execution::Call(isolate, |
| 666 | isolate->configure_instance_fun(), |
| 667 | isolate->js_builtins_object(), |
| 668 | arraysize(args), |
| 669 | args); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 670 | } |
| 671 | |
| 672 | |
| 673 | Handle<String> Execution::GetStackTraceLine(Handle<Object> recv, |
| 674 | Handle<JSFunction> fun, |
| 675 | Handle<Object> pos, |
| 676 | Handle<Object> is_global) { |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 677 | Isolate* isolate = fun->GetIsolate(); |
Ben Murdoch | 3ef787d | 2012-04-12 10:51:47 +0100 | [diff] [blame] | 678 | Handle<Object> args[] = { recv, fun, pos, is_global }; |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 679 | MaybeHandle<Object> maybe_result = |
| 680 | TryCall(isolate->get_stack_trace_line_fun(), |
| 681 | isolate->js_builtins_object(), |
| 682 | arraysize(args), |
| 683 | args); |
| 684 | Handle<Object> result; |
| 685 | if (!maybe_result.ToHandle(&result) || !result->IsString()) { |
| 686 | return isolate->factory()->empty_string(); |
Ben Murdoch | 8b112d2 | 2011-06-08 16:22:53 +0100 | [diff] [blame] | 687 | } |
| 688 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 689 | return Handle<String>::cast(result); |
| 690 | } |
| 691 | |
| 692 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 693 | Object* StackGuard::HandleInterrupts() { |
| 694 | if (CheckAndClearInterrupt(GC_REQUEST)) { |
| 695 | isolate_->heap()->CollectAllGarbage(Heap::kNoGCFlags, "GC interrupt"); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 696 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 697 | |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 698 | if (CheckDebugBreak() || CheckDebugCommand()) { |
| 699 | isolate_->debug()->HandleDebugBreak(); |
| 700 | } |
| 701 | |
| 702 | if (CheckAndClearInterrupt(TERMINATE_EXECUTION)) { |
| 703 | return isolate_->TerminateExecution(); |
| 704 | } |
| 705 | |
| 706 | if (CheckAndClearInterrupt(DEOPT_MARKED_ALLOCATION_SITES)) { |
| 707 | isolate_->heap()->DeoptMarkedAllocationSites(); |
| 708 | } |
| 709 | |
| 710 | if (CheckAndClearInterrupt(INSTALL_CODE)) { |
| 711 | DCHECK(isolate_->concurrent_recompilation_enabled()); |
| 712 | isolate_->optimizing_compiler_thread()->InstallOptimizedFunctions(); |
| 713 | } |
| 714 | |
| 715 | if (CheckAndClearInterrupt(API_INTERRUPT)) { |
Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [diff] [blame] | 716 | // Callbacks must be invoked outside of ExecusionAccess lock. |
| 717 | isolate_->InvokeApiInterruptCallbacks(); |
Ben Murdoch | b8a8cc1 | 2014-11-26 15:28:44 +0000 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | isolate_->counters()->stack_interrupts()->Increment(); |
| 721 | isolate_->counters()->runtime_profiler_ticks()->Increment(); |
| 722 | isolate_->runtime_profiler()->OptimizeNow(); |
| 723 | |
| 724 | return isolate_->heap()->undefined_value(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 725 | } |
| 726 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 727 | } } // namespace v8::internal |