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