Emily Bernier | d0a1eb7 | 2015-03-24 16:35:39 -0400 | [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. |
| 4 | |
| 5 | #include "src/v8.h" |
| 6 | |
| 7 | #include "src/accessors.h" |
| 8 | #include "src/arguments.h" |
| 9 | #include "src/compiler.h" |
| 10 | #include "src/deoptimizer.h" |
| 11 | #include "src/frames.h" |
| 12 | #include "src/runtime/runtime-utils.h" |
| 13 | |
| 14 | namespace v8 { |
| 15 | namespace internal { |
| 16 | |
| 17 | RUNTIME_FUNCTION(Runtime_IsSloppyModeFunction) { |
| 18 | SealHandleScope shs(isolate); |
| 19 | DCHECK(args.length() == 1); |
| 20 | CONVERT_ARG_CHECKED(JSReceiver, callable, 0); |
| 21 | if (!callable->IsJSFunction()) { |
| 22 | HandleScope scope(isolate); |
| 23 | Handle<Object> delegate; |
| 24 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 25 | isolate, delegate, Execution::TryGetFunctionDelegate( |
| 26 | isolate, Handle<JSReceiver>(callable))); |
| 27 | callable = JSFunction::cast(*delegate); |
| 28 | } |
| 29 | JSFunction* function = JSFunction::cast(callable); |
| 30 | SharedFunctionInfo* shared = function->shared(); |
| 31 | return isolate->heap()->ToBoolean(shared->strict_mode() == SLOPPY); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | RUNTIME_FUNCTION(Runtime_GetDefaultReceiver) { |
| 36 | SealHandleScope shs(isolate); |
| 37 | DCHECK(args.length() == 1); |
| 38 | CONVERT_ARG_CHECKED(JSReceiver, callable, 0); |
| 39 | |
| 40 | if (!callable->IsJSFunction()) { |
| 41 | HandleScope scope(isolate); |
| 42 | Handle<Object> delegate; |
| 43 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 44 | isolate, delegate, Execution::TryGetFunctionDelegate( |
| 45 | isolate, Handle<JSReceiver>(callable))); |
| 46 | callable = JSFunction::cast(*delegate); |
| 47 | } |
| 48 | JSFunction* function = JSFunction::cast(callable); |
| 49 | |
| 50 | SharedFunctionInfo* shared = function->shared(); |
| 51 | if (shared->native() || shared->strict_mode() == STRICT) { |
| 52 | return isolate->heap()->undefined_value(); |
| 53 | } |
| 54 | // Returns undefined for strict or native functions, or |
| 55 | // the associated global receiver for "normal" functions. |
| 56 | |
| 57 | return function->global_proxy(); |
| 58 | } |
| 59 | |
| 60 | |
| 61 | RUNTIME_FUNCTION(Runtime_FunctionGetName) { |
| 62 | SealHandleScope shs(isolate); |
| 63 | DCHECK(args.length() == 1); |
| 64 | |
| 65 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 66 | return f->shared()->name(); |
| 67 | } |
| 68 | |
| 69 | |
| 70 | static Handle<String> NameToFunctionName(Handle<Name> name) { |
| 71 | Handle<String> stringName(name->GetHeap()->empty_string()); |
| 72 | |
| 73 | // TODO(caitp): Follow proper rules in section 9.2.11 (SetFunctionName) |
| 74 | if (name->IsSymbol()) { |
| 75 | Handle<Object> description(Handle<Symbol>::cast(name)->name(), |
| 76 | name->GetIsolate()); |
| 77 | if (description->IsString()) { |
| 78 | stringName = Handle<String>::cast(description); |
| 79 | } |
| 80 | } else { |
| 81 | stringName = Handle<String>::cast(name); |
| 82 | } |
| 83 | |
| 84 | return stringName; |
| 85 | } |
| 86 | |
| 87 | |
| 88 | RUNTIME_FUNCTION(Runtime_FunctionSetName) { |
| 89 | HandleScope scope(isolate); |
| 90 | DCHECK(args.length() == 2); |
| 91 | |
| 92 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0); |
| 93 | CONVERT_ARG_HANDLE_CHECKED(Name, name, 1); |
| 94 | |
| 95 | f->shared()->set_name(*NameToFunctionName(name)); |
| 96 | return isolate->heap()->undefined_value(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | RUNTIME_FUNCTION(Runtime_FunctionNameShouldPrintAsAnonymous) { |
| 101 | SealHandleScope shs(isolate); |
| 102 | DCHECK(args.length() == 1); |
| 103 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 104 | return isolate->heap()->ToBoolean( |
| 105 | f->shared()->name_should_print_as_anonymous()); |
| 106 | } |
| 107 | |
| 108 | |
| 109 | RUNTIME_FUNCTION(Runtime_FunctionMarkNameShouldPrintAsAnonymous) { |
| 110 | SealHandleScope shs(isolate); |
| 111 | DCHECK(args.length() == 1); |
| 112 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 113 | f->shared()->set_name_should_print_as_anonymous(true); |
| 114 | return isolate->heap()->undefined_value(); |
| 115 | } |
| 116 | |
| 117 | |
| 118 | RUNTIME_FUNCTION(Runtime_FunctionIsArrow) { |
| 119 | SealHandleScope shs(isolate); |
| 120 | DCHECK(args.length() == 1); |
| 121 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 122 | return isolate->heap()->ToBoolean(f->shared()->is_arrow()); |
| 123 | } |
| 124 | |
| 125 | |
| 126 | RUNTIME_FUNCTION(Runtime_FunctionIsConciseMethod) { |
| 127 | SealHandleScope shs(isolate); |
| 128 | DCHECK(args.length() == 1); |
| 129 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 130 | return isolate->heap()->ToBoolean(f->shared()->is_concise_method()); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | RUNTIME_FUNCTION(Runtime_FunctionRemovePrototype) { |
| 135 | SealHandleScope shs(isolate); |
| 136 | DCHECK(args.length() == 1); |
| 137 | |
| 138 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 139 | RUNTIME_ASSERT(f->RemovePrototype()); |
| 140 | |
| 141 | return isolate->heap()->undefined_value(); |
| 142 | } |
| 143 | |
| 144 | |
| 145 | RUNTIME_FUNCTION(Runtime_FunctionGetScript) { |
| 146 | HandleScope scope(isolate); |
| 147 | DCHECK(args.length() == 1); |
| 148 | |
| 149 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 150 | Handle<Object> script = Handle<Object>(fun->shared()->script(), isolate); |
| 151 | if (!script->IsScript()) return isolate->heap()->undefined_value(); |
| 152 | |
| 153 | return *Script::GetWrapper(Handle<Script>::cast(script)); |
| 154 | } |
| 155 | |
| 156 | |
| 157 | RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) { |
| 158 | HandleScope scope(isolate); |
| 159 | DCHECK(args.length() == 1); |
| 160 | |
| 161 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0); |
| 162 | Handle<SharedFunctionInfo> shared(f->shared()); |
| 163 | return *shared->GetSourceCode(); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) { |
| 168 | SealHandleScope shs(isolate); |
| 169 | DCHECK(args.length() == 1); |
| 170 | |
| 171 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 172 | int pos = fun->shared()->start_position(); |
| 173 | return Smi::FromInt(pos); |
| 174 | } |
| 175 | |
| 176 | |
| 177 | RUNTIME_FUNCTION(Runtime_FunctionGetPositionForOffset) { |
| 178 | SealHandleScope shs(isolate); |
| 179 | DCHECK(args.length() == 2); |
| 180 | |
| 181 | CONVERT_ARG_CHECKED(Code, code, 0); |
| 182 | CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]); |
| 183 | |
| 184 | RUNTIME_ASSERT(0 <= offset && offset < code->Size()); |
| 185 | |
| 186 | Address pc = code->address() + offset; |
| 187 | return Smi::FromInt(code->SourcePosition(pc)); |
| 188 | } |
| 189 | |
| 190 | |
| 191 | RUNTIME_FUNCTION(Runtime_FunctionSetInstanceClassName) { |
| 192 | SealHandleScope shs(isolate); |
| 193 | DCHECK(args.length() == 2); |
| 194 | |
| 195 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 196 | CONVERT_ARG_CHECKED(String, name, 1); |
| 197 | fun->SetInstanceClassName(name); |
| 198 | return isolate->heap()->undefined_value(); |
| 199 | } |
| 200 | |
| 201 | |
| 202 | RUNTIME_FUNCTION(Runtime_FunctionSetLength) { |
| 203 | SealHandleScope shs(isolate); |
| 204 | DCHECK(args.length() == 2); |
| 205 | |
| 206 | CONVERT_ARG_CHECKED(JSFunction, fun, 0); |
| 207 | CONVERT_SMI_ARG_CHECKED(length, 1); |
| 208 | RUNTIME_ASSERT((length & 0xC0000000) == 0xC0000000 || |
| 209 | (length & 0xC0000000) == 0x0); |
| 210 | fun->shared()->set_length(length); |
| 211 | return isolate->heap()->undefined_value(); |
| 212 | } |
| 213 | |
| 214 | |
| 215 | RUNTIME_FUNCTION(Runtime_FunctionSetPrototype) { |
| 216 | HandleScope scope(isolate); |
| 217 | DCHECK(args.length() == 2); |
| 218 | |
| 219 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0); |
| 220 | CONVERT_ARG_HANDLE_CHECKED(Object, value, 1); |
| 221 | RUNTIME_ASSERT(fun->should_have_prototype()); |
| 222 | RETURN_FAILURE_ON_EXCEPTION(isolate, |
| 223 | Accessors::FunctionSetPrototype(fun, value)); |
| 224 | return args[0]; // return TOS |
| 225 | } |
| 226 | |
| 227 | |
| 228 | RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) { |
| 229 | SealHandleScope shs(isolate); |
| 230 | DCHECK(args.length() == 1); |
| 231 | |
| 232 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 233 | return isolate->heap()->ToBoolean(f->shared()->IsApiFunction()); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | RUNTIME_FUNCTION(Runtime_FunctionIsBuiltin) { |
| 238 | SealHandleScope shs(isolate); |
| 239 | DCHECK(args.length() == 1); |
| 240 | |
| 241 | CONVERT_ARG_CHECKED(JSFunction, f, 0); |
| 242 | return isolate->heap()->ToBoolean(f->IsBuiltin()); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | RUNTIME_FUNCTION(Runtime_SetCode) { |
| 247 | HandleScope scope(isolate); |
| 248 | DCHECK(args.length() == 2); |
| 249 | |
| 250 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0); |
| 251 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, source, 1); |
| 252 | |
| 253 | Handle<SharedFunctionInfo> target_shared(target->shared()); |
| 254 | Handle<SharedFunctionInfo> source_shared(source->shared()); |
| 255 | RUNTIME_ASSERT(!source_shared->bound()); |
| 256 | |
| 257 | if (!Compiler::EnsureCompiled(source, KEEP_EXCEPTION)) { |
| 258 | return isolate->heap()->exception(); |
| 259 | } |
| 260 | |
| 261 | // Mark both, the source and the target, as un-flushable because the |
| 262 | // shared unoptimized code makes them impossible to enqueue in a list. |
| 263 | DCHECK(target_shared->code()->gc_metadata() == NULL); |
| 264 | DCHECK(source_shared->code()->gc_metadata() == NULL); |
| 265 | target_shared->set_dont_flush(true); |
| 266 | source_shared->set_dont_flush(true); |
| 267 | |
| 268 | // Set the code, scope info, formal parameter count, and the length |
| 269 | // of the target shared function info. |
| 270 | target_shared->ReplaceCode(source_shared->code()); |
| 271 | target_shared->set_scope_info(source_shared->scope_info()); |
| 272 | target_shared->set_length(source_shared->length()); |
| 273 | target_shared->set_feedback_vector(source_shared->feedback_vector()); |
| 274 | target_shared->set_formal_parameter_count( |
| 275 | source_shared->formal_parameter_count()); |
| 276 | target_shared->set_script(source_shared->script()); |
| 277 | target_shared->set_start_position_and_type( |
| 278 | source_shared->start_position_and_type()); |
| 279 | target_shared->set_end_position(source_shared->end_position()); |
| 280 | bool was_native = target_shared->native(); |
| 281 | target_shared->set_compiler_hints(source_shared->compiler_hints()); |
| 282 | target_shared->set_native(was_native); |
| 283 | target_shared->set_profiler_ticks(source_shared->profiler_ticks()); |
| 284 | |
| 285 | // Set the code of the target function. |
| 286 | target->ReplaceCode(source_shared->code()); |
| 287 | DCHECK(target->next_function_link()->IsUndefined()); |
| 288 | |
| 289 | // Make sure we get a fresh copy of the literal vector to avoid cross |
| 290 | // context contamination. |
| 291 | Handle<Context> context(source->context()); |
| 292 | int number_of_literals = source->NumberOfLiterals(); |
| 293 | Handle<FixedArray> literals = |
| 294 | isolate->factory()->NewFixedArray(number_of_literals, TENURED); |
| 295 | if (number_of_literals > 0) { |
| 296 | literals->set(JSFunction::kLiteralNativeContextIndex, |
| 297 | context->native_context()); |
| 298 | } |
| 299 | target->set_context(*context); |
| 300 | target->set_literals(*literals); |
| 301 | |
| 302 | if (isolate->logger()->is_logging_code_events() || |
| 303 | isolate->cpu_profiler()->is_profiling()) { |
| 304 | isolate->logger()->LogExistingFunction(source_shared, |
| 305 | Handle<Code>(source_shared->code())); |
| 306 | } |
| 307 | |
| 308 | return *target; |
| 309 | } |
| 310 | |
| 311 | |
| 312 | // Set the native flag on the function. |
| 313 | // This is used to decide if we should transform null and undefined |
| 314 | // into the global object when doing call and apply. |
| 315 | RUNTIME_FUNCTION(Runtime_SetNativeFlag) { |
| 316 | SealHandleScope shs(isolate); |
| 317 | RUNTIME_ASSERT(args.length() == 1); |
| 318 | |
| 319 | CONVERT_ARG_CHECKED(Object, object, 0); |
| 320 | |
| 321 | if (object->IsJSFunction()) { |
| 322 | JSFunction* func = JSFunction::cast(object); |
| 323 | func->shared()->set_native(true); |
| 324 | } |
| 325 | return isolate->heap()->undefined_value(); |
| 326 | } |
| 327 | |
| 328 | |
| 329 | RUNTIME_FUNCTION(Runtime_SetInlineBuiltinFlag) { |
| 330 | SealHandleScope shs(isolate); |
| 331 | RUNTIME_ASSERT(args.length() == 1); |
| 332 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 333 | |
| 334 | if (object->IsJSFunction()) { |
| 335 | JSFunction* func = JSFunction::cast(*object); |
| 336 | func->shared()->set_inline_builtin(true); |
| 337 | } |
| 338 | return isolate->heap()->undefined_value(); |
| 339 | } |
| 340 | |
| 341 | |
| 342 | // Find the arguments of the JavaScript function invocation that called |
| 343 | // into C++ code. Collect these in a newly allocated array of handles (possibly |
| 344 | // prefixed by a number of empty handles). |
| 345 | static SmartArrayPointer<Handle<Object> > GetCallerArguments(Isolate* isolate, |
| 346 | int prefix_argc, |
| 347 | int* total_argc) { |
| 348 | // Find frame containing arguments passed to the caller. |
| 349 | JavaScriptFrameIterator it(isolate); |
| 350 | JavaScriptFrame* frame = it.frame(); |
| 351 | List<JSFunction*> functions(2); |
| 352 | frame->GetFunctions(&functions); |
| 353 | if (functions.length() > 1) { |
| 354 | int inlined_jsframe_index = functions.length() - 1; |
| 355 | JSFunction* inlined_function = functions[inlined_jsframe_index]; |
| 356 | SlotRefValueBuilder slot_refs( |
| 357 | frame, inlined_jsframe_index, |
| 358 | inlined_function->shared()->formal_parameter_count()); |
| 359 | |
| 360 | int args_count = slot_refs.args_length(); |
| 361 | |
| 362 | *total_argc = prefix_argc + args_count; |
| 363 | SmartArrayPointer<Handle<Object> > param_data( |
| 364 | NewArray<Handle<Object> >(*total_argc)); |
| 365 | slot_refs.Prepare(isolate); |
| 366 | for (int i = 0; i < args_count; i++) { |
| 367 | Handle<Object> val = slot_refs.GetNext(isolate, 0); |
| 368 | param_data[prefix_argc + i] = val; |
| 369 | } |
| 370 | slot_refs.Finish(isolate); |
| 371 | |
| 372 | return param_data; |
| 373 | } else { |
| 374 | it.AdvanceToArgumentsFrame(); |
| 375 | frame = it.frame(); |
| 376 | int args_count = frame->ComputeParametersCount(); |
| 377 | |
| 378 | *total_argc = prefix_argc + args_count; |
| 379 | SmartArrayPointer<Handle<Object> > param_data( |
| 380 | NewArray<Handle<Object> >(*total_argc)); |
| 381 | for (int i = 0; i < args_count; i++) { |
| 382 | Handle<Object> val = Handle<Object>(frame->GetParameter(i), isolate); |
| 383 | param_data[prefix_argc + i] = val; |
| 384 | } |
| 385 | return param_data; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | |
| 390 | RUNTIME_FUNCTION(Runtime_FunctionBindArguments) { |
| 391 | HandleScope scope(isolate); |
| 392 | DCHECK(args.length() == 4); |
| 393 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, bound_function, 0); |
| 394 | CONVERT_ARG_HANDLE_CHECKED(Object, bindee, 1); |
| 395 | CONVERT_ARG_HANDLE_CHECKED(Object, this_object, 2); |
| 396 | CONVERT_NUMBER_ARG_HANDLE_CHECKED(new_length, 3); |
| 397 | |
| 398 | // TODO(lrn): Create bound function in C++ code from premade shared info. |
| 399 | bound_function->shared()->set_bound(true); |
| 400 | // Get all arguments of calling function (Function.prototype.bind). |
| 401 | int argc = 0; |
| 402 | SmartArrayPointer<Handle<Object> > arguments = |
| 403 | GetCallerArguments(isolate, 0, &argc); |
| 404 | // Don't count the this-arg. |
| 405 | if (argc > 0) { |
| 406 | RUNTIME_ASSERT(arguments[0].is_identical_to(this_object)); |
| 407 | argc--; |
| 408 | } else { |
| 409 | RUNTIME_ASSERT(this_object->IsUndefined()); |
| 410 | } |
| 411 | // Initialize array of bindings (function, this, and any existing arguments |
| 412 | // if the function was already bound). |
| 413 | Handle<FixedArray> new_bindings; |
| 414 | int i; |
| 415 | if (bindee->IsJSFunction() && JSFunction::cast(*bindee)->shared()->bound()) { |
| 416 | Handle<FixedArray> old_bindings( |
| 417 | JSFunction::cast(*bindee)->function_bindings()); |
| 418 | RUNTIME_ASSERT(old_bindings->length() > JSFunction::kBoundFunctionIndex); |
| 419 | new_bindings = |
| 420 | isolate->factory()->NewFixedArray(old_bindings->length() + argc); |
| 421 | bindee = Handle<Object>(old_bindings->get(JSFunction::kBoundFunctionIndex), |
| 422 | isolate); |
| 423 | i = 0; |
| 424 | for (int n = old_bindings->length(); i < n; i++) { |
| 425 | new_bindings->set(i, old_bindings->get(i)); |
| 426 | } |
| 427 | } else { |
| 428 | int array_size = JSFunction::kBoundArgumentsStartIndex + argc; |
| 429 | new_bindings = isolate->factory()->NewFixedArray(array_size); |
| 430 | new_bindings->set(JSFunction::kBoundFunctionIndex, *bindee); |
| 431 | new_bindings->set(JSFunction::kBoundThisIndex, *this_object); |
| 432 | i = 2; |
| 433 | } |
| 434 | // Copy arguments, skipping the first which is "this_arg". |
| 435 | for (int j = 0; j < argc; j++, i++) { |
| 436 | new_bindings->set(i, *arguments[j + 1]); |
| 437 | } |
| 438 | new_bindings->set_map_no_write_barrier( |
| 439 | isolate->heap()->fixed_cow_array_map()); |
| 440 | bound_function->set_function_bindings(*new_bindings); |
| 441 | |
| 442 | // Update length. Have to remove the prototype first so that map migration |
| 443 | // is happy about the number of fields. |
| 444 | RUNTIME_ASSERT(bound_function->RemovePrototype()); |
| 445 | Handle<Map> bound_function_map( |
| 446 | isolate->native_context()->bound_function_map()); |
| 447 | JSObject::MigrateToMap(bound_function, bound_function_map); |
| 448 | Handle<String> length_string = isolate->factory()->length_string(); |
| 449 | PropertyAttributes attr = |
| 450 | static_cast<PropertyAttributes>(DONT_DELETE | DONT_ENUM | READ_ONLY); |
| 451 | RETURN_FAILURE_ON_EXCEPTION( |
| 452 | isolate, JSObject::SetOwnPropertyIgnoreAttributes( |
| 453 | bound_function, length_string, new_length, attr)); |
| 454 | return *bound_function; |
| 455 | } |
| 456 | |
| 457 | |
| 458 | RUNTIME_FUNCTION(Runtime_BoundFunctionGetBindings) { |
| 459 | HandleScope handles(isolate); |
| 460 | DCHECK(args.length() == 1); |
| 461 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, callable, 0); |
| 462 | if (callable->IsJSFunction()) { |
| 463 | Handle<JSFunction> function = Handle<JSFunction>::cast(callable); |
| 464 | if (function->shared()->bound()) { |
| 465 | Handle<FixedArray> bindings(function->function_bindings()); |
| 466 | RUNTIME_ASSERT(bindings->map() == isolate->heap()->fixed_cow_array_map()); |
| 467 | return *isolate->factory()->NewJSArrayWithElements(bindings); |
| 468 | } |
| 469 | } |
| 470 | return isolate->heap()->undefined_value(); |
| 471 | } |
| 472 | |
| 473 | |
| 474 | RUNTIME_FUNCTION(Runtime_NewObjectFromBound) { |
| 475 | HandleScope scope(isolate); |
| 476 | DCHECK(args.length() == 1); |
| 477 | // First argument is a function to use as a constructor. |
| 478 | CONVERT_ARG_HANDLE_CHECKED(JSFunction, function, 0); |
| 479 | RUNTIME_ASSERT(function->shared()->bound()); |
| 480 | |
| 481 | // The argument is a bound function. Extract its bound arguments |
| 482 | // and callable. |
| 483 | Handle<FixedArray> bound_args = |
| 484 | Handle<FixedArray>(FixedArray::cast(function->function_bindings())); |
| 485 | int bound_argc = bound_args->length() - JSFunction::kBoundArgumentsStartIndex; |
| 486 | Handle<Object> bound_function( |
| 487 | JSReceiver::cast(bound_args->get(JSFunction::kBoundFunctionIndex)), |
| 488 | isolate); |
| 489 | DCHECK(!bound_function->IsJSFunction() || |
| 490 | !Handle<JSFunction>::cast(bound_function)->shared()->bound()); |
| 491 | |
| 492 | int total_argc = 0; |
| 493 | SmartArrayPointer<Handle<Object> > param_data = |
| 494 | GetCallerArguments(isolate, bound_argc, &total_argc); |
| 495 | for (int i = 0; i < bound_argc; i++) { |
| 496 | param_data[i] = Handle<Object>( |
| 497 | bound_args->get(JSFunction::kBoundArgumentsStartIndex + i), isolate); |
| 498 | } |
| 499 | |
| 500 | if (!bound_function->IsJSFunction()) { |
| 501 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 502 | isolate, bound_function, |
| 503 | Execution::TryGetConstructorDelegate(isolate, bound_function)); |
| 504 | } |
| 505 | DCHECK(bound_function->IsJSFunction()); |
| 506 | |
| 507 | Handle<Object> result; |
| 508 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 509 | isolate, result, Execution::New(Handle<JSFunction>::cast(bound_function), |
| 510 | total_argc, param_data.get())); |
| 511 | return *result; |
| 512 | } |
| 513 | |
| 514 | |
| 515 | RUNTIME_FUNCTION(Runtime_Call) { |
| 516 | HandleScope scope(isolate); |
| 517 | DCHECK(args.length() >= 2); |
| 518 | int argc = args.length() - 2; |
| 519 | CONVERT_ARG_CHECKED(JSReceiver, fun, argc + 1); |
| 520 | Object* receiver = args[0]; |
| 521 | |
| 522 | // If there are too many arguments, allocate argv via malloc. |
| 523 | const int argv_small_size = 10; |
| 524 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 525 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 526 | Handle<Object>* argv = argv_small_buffer; |
| 527 | if (argc > argv_small_size) { |
| 528 | argv = new Handle<Object>[argc]; |
| 529 | if (argv == NULL) return isolate->StackOverflow(); |
| 530 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 531 | } |
| 532 | |
| 533 | for (int i = 0; i < argc; ++i) { |
| 534 | argv[i] = Handle<Object>(args[1 + i], isolate); |
| 535 | } |
| 536 | |
| 537 | Handle<JSReceiver> hfun(fun); |
| 538 | Handle<Object> hreceiver(receiver, isolate); |
| 539 | Handle<Object> result; |
| 540 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 541 | isolate, result, |
| 542 | Execution::Call(isolate, hfun, hreceiver, argc, argv, true)); |
| 543 | return *result; |
| 544 | } |
| 545 | |
| 546 | |
| 547 | RUNTIME_FUNCTION(Runtime_Apply) { |
| 548 | HandleScope scope(isolate); |
| 549 | DCHECK(args.length() == 5); |
| 550 | CONVERT_ARG_HANDLE_CHECKED(JSReceiver, fun, 0); |
| 551 | CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1); |
| 552 | CONVERT_ARG_HANDLE_CHECKED(JSObject, arguments, 2); |
| 553 | CONVERT_INT32_ARG_CHECKED(offset, 3); |
| 554 | CONVERT_INT32_ARG_CHECKED(argc, 4); |
| 555 | RUNTIME_ASSERT(offset >= 0); |
| 556 | // Loose upper bound to allow fuzzing. We'll most likely run out of |
| 557 | // stack space before hitting this limit. |
| 558 | static int kMaxArgc = 1000000; |
| 559 | RUNTIME_ASSERT(argc >= 0 && argc <= kMaxArgc); |
| 560 | |
| 561 | // If there are too many arguments, allocate argv via malloc. |
| 562 | const int argv_small_size = 10; |
| 563 | Handle<Object> argv_small_buffer[argv_small_size]; |
| 564 | SmartArrayPointer<Handle<Object> > argv_large_buffer; |
| 565 | Handle<Object>* argv = argv_small_buffer; |
| 566 | if (argc > argv_small_size) { |
| 567 | argv = new Handle<Object>[argc]; |
| 568 | if (argv == NULL) return isolate->StackOverflow(); |
| 569 | argv_large_buffer = SmartArrayPointer<Handle<Object> >(argv); |
| 570 | } |
| 571 | |
| 572 | for (int i = 0; i < argc; ++i) { |
| 573 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 574 | isolate, argv[i], Object::GetElement(isolate, arguments, offset + i)); |
| 575 | } |
| 576 | |
| 577 | Handle<Object> result; |
| 578 | ASSIGN_RETURN_FAILURE_ON_EXCEPTION( |
| 579 | isolate, result, |
| 580 | Execution::Call(isolate, fun, receiver, argc, argv, true)); |
| 581 | return *result; |
| 582 | } |
| 583 | |
| 584 | |
| 585 | RUNTIME_FUNCTION(Runtime_GetFunctionDelegate) { |
| 586 | HandleScope scope(isolate); |
| 587 | DCHECK(args.length() == 1); |
| 588 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 589 | RUNTIME_ASSERT(!object->IsJSFunction()); |
| 590 | return *Execution::GetFunctionDelegate(isolate, object); |
| 591 | } |
| 592 | |
| 593 | |
| 594 | RUNTIME_FUNCTION(Runtime_GetConstructorDelegate) { |
| 595 | HandleScope scope(isolate); |
| 596 | DCHECK(args.length() == 1); |
| 597 | CONVERT_ARG_HANDLE_CHECKED(Object, object, 0); |
| 598 | RUNTIME_ASSERT(!object->IsJSFunction()); |
| 599 | return *Execution::GetConstructorDelegate(isolate, object); |
| 600 | } |
| 601 | |
| 602 | |
| 603 | RUNTIME_FUNCTION(RuntimeReference_CallFunction) { |
| 604 | SealHandleScope shs(isolate); |
| 605 | return __RT_impl_Runtime_Call(args, isolate); |
| 606 | } |
| 607 | |
| 608 | |
| 609 | RUNTIME_FUNCTION(RuntimeReference_IsConstructCall) { |
| 610 | SealHandleScope shs(isolate); |
| 611 | DCHECK(args.length() == 0); |
| 612 | JavaScriptFrameIterator it(isolate); |
| 613 | JavaScriptFrame* frame = it.frame(); |
| 614 | return isolate->heap()->ToBoolean(frame->IsConstructor()); |
| 615 | } |
| 616 | |
| 617 | |
| 618 | RUNTIME_FUNCTION(RuntimeReference_IsFunction) { |
| 619 | SealHandleScope shs(isolate); |
| 620 | DCHECK(args.length() == 1); |
| 621 | CONVERT_ARG_CHECKED(Object, obj, 0); |
| 622 | return isolate->heap()->ToBoolean(obj->IsJSFunction()); |
| 623 | } |
| 624 | } |
| 625 | } // namespace v8::internal |