runtime-function.cc 2.94 KB
Newer Older
1 2 3 4
// 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.

5
#include "src/builtins/accessors.h"
6
#include "src/codegen/compiler.h"
7 8
#include "src/execution/arguments-inl.h"
#include "src/execution/isolate-inl.h"
9
#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
10
#include "src/logging/counters.h"
11
#include "src/runtime/runtime-utils.h"
12 13 14 15

namespace v8 {
namespace internal {

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

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

30 31 32
RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
33
  Handle<JSReceiver> function = args.at<JSReceiver>(0);
34 35

  if (function->IsJSFunction()) {
36 37
    Handle<Object> script(Handle<JSFunction>::cast(function)->shared().script(),
                          isolate);
38 39 40 41 42 43
    if (script->IsScript()) {
      return Smi::FromInt(Handle<Script>::cast(script)->id());
    }
  }
  return Smi::FromInt(-1);
}
44 45 46

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


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

62
  auto fun = JSFunction::cast(args[0]);
63
  int pos = fun.shared().StartPosition();
64 65 66 67 68 69
  return Smi::FromInt(pos);
}


RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
  SealHandleScope shs(isolate);
70
  DCHECK_EQ(1, args.length());
71

72
  auto f = JSFunction::cast(args[0]);
73
  return isolate->heap()->ToBoolean(f.shared().IsApiFunction());
74 75 76 77 78
}


RUNTIME_FUNCTION(Runtime_Call) {
  HandleScope scope(isolate);
79 80
  DCHECK_LE(2, args.length());
  int const argc = args.length() - 2;
81 82
  Handle<Object> target = args.at(0);
  Handle<Object> receiver = args.at(1);
83
  base::ScopedVector<Handle<Object>> argv(argc);
84
  for (int i = 0; i < argc; ++i) {
85
    argv[i] = args.at(2 + i);
86
  }
87
  RETURN_RESULT_OR_FAILURE(
88
      isolate, Execution::Call(isolate, target, receiver, argc, argv.begin()));
89 90 91
}


92
RUNTIME_FUNCTION(Runtime_IsFunction) {
93
  SealHandleScope shs(isolate);
94
  DCHECK_EQ(1, args.length());
95
  Object object = args[0];
96
  return isolate->heap()->ToBoolean(object.IsFunction());
97
}
98 99


100 101
}  // namespace internal
}  // namespace v8