runtime-function.cc 2.97 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 20
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 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 33 34 35
RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);

  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 48
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 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

  CONVERT_ARG_CHECKED(JSFunction, fun, 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

  CONVERT_ARG_CHECKED(JSFunction, f, 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
  CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
82 83
  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
  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 95
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_CHECKED(Object, object, 0);
96
  return isolate->heap()->ToBoolean(object.IsFunction());
97
}
98 99


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