runtime-function.cc 3.31 KB
Newer Older
1 2 3 4 5
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/accessors.h"
6
#include "src/arguments-inl.h"
7
#include "src/compiler.h"
8
#include "src/counters.h"
9
#include "src/isolate-inl.h"
10
#include "src/runtime/runtime-utils.h"
11 12 13 14

namespace v8 {
namespace internal {

15
// TODO(5530): Remove once uses in debug.js are gone.
16
RUNTIME_FUNCTION(Runtime_FunctionGetScriptSource) {
17
  HandleScope scope(isolate);
18 19
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
20

21 22 23
  if (function->IsJSFunction()) {
    Handle<Object> script(
        Handle<JSFunction>::cast(function)->shared()->script(), isolate);
24
    if (script->IsScript()) return Handle<Script>::cast(script)->source();
25
  }
26
  return ReadOnlyRoots(isolate).undefined_value();
27 28
}

29 30 31 32 33 34 35 36 37 38 39 40 41 42
RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);

  if (function->IsJSFunction()) {
    Handle<Object> script(
        Handle<JSFunction>::cast(function)->shared()->script(), isolate);
    if (script->IsScript()) {
      return Smi::FromInt(Handle<Script>::cast(script)->id());
    }
  }
  return Smi::FromInt(-1);
}
43 44 45

RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
  HandleScope scope(isolate);
46 47
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
48
  if (function->IsJSFunction()) {
49
    Handle<SharedFunctionInfo> shared(
50
        Handle<JSFunction>::cast(function)->shared(), isolate);
51
    return *SharedFunctionInfo::GetSourceCode(shared);
52
  }
53
  return ReadOnlyRoots(isolate).undefined_value();
54 55 56 57 58
}


RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
  SealHandleScope shs(isolate);
59
  DCHECK_EQ(1, args.length());
60 61

  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
62
  int pos = fun->shared()->StartPosition();
63 64 65 66 67 68
  return Smi::FromInt(pos);
}


RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
  SealHandleScope shs(isolate);
69
  DCHECK_EQ(1, args.length());
70 71 72 73 74 75 76 77 78 79 80

  CONVERT_ARG_CHECKED(JSFunction, f, 0);
  return isolate->heap()->ToBoolean(f->shared()->IsApiFunction());
}


// Set the native flag on the function.
// This is used to decide if we should transform null and undefined
// into the global object when doing call and apply.
RUNTIME_FUNCTION(Runtime_SetNativeFlag) {
  SealHandleScope shs(isolate);
81
  DCHECK_EQ(1, args.length());
82 83 84 85

  CONVERT_ARG_CHECKED(Object, object, 0);

  if (object->IsJSFunction()) {
86
    JSFunction func = JSFunction::cast(object);
87 88
    func->shared()->set_native(true);
  }
89
  return ReadOnlyRoots(isolate).undefined_value();
90 91 92 93 94
}


RUNTIME_FUNCTION(Runtime_Call) {
  HandleScope scope(isolate);
95 96
  DCHECK_LE(2, args.length());
  int const argc = args.length() - 2;
97
  CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
98 99
  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
  ScopedVector<Handle<Object>> argv(argc);
100
  for (int i = 0; i < argc; ++i) {
101
    argv[i] = args.at(2 + i);
102
  }
103 104
  RETURN_RESULT_OR_FAILURE(
      isolate, Execution::Call(isolate, target, receiver, argc, argv.start()));
105 106 107
}


108
RUNTIME_FUNCTION(Runtime_IsFunction) {
109
  SealHandleScope shs(isolate);
110 111 112
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_CHECKED(Object, object, 0);
  return isolate->heap()->ToBoolean(object->IsFunction());
113
}
114 115


116 117
}  // namespace internal
}  // namespace v8