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