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/builtins/accessors.h"
6 #include "src/codegen/compiler.h"
7 #include "src/execution/arguments-inl.h"
8 #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 namespace v8 {
14 namespace internal {
15
16 // TODO(5530): Remove once uses in debug.js are gone.
RUNTIME_FUNCTION(Runtime_FunctionGetScriptSource)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 Handle<Object> script(Handle<JSFunction>::cast(function)->shared().script(),
24 isolate);
25 if (script->IsScript()) return Handle<Script>::cast(script)->source();
26 }
27 return ReadOnlyRoots(isolate).undefined_value();
28 }
29
RUNTIME_FUNCTION(Runtime_FunctionGetScriptId)30 RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
31 HandleScope scope(isolate);
32 DCHECK_EQ(1, args.length());
33 Handle<JSReceiver> function = args.at<JSReceiver>(0);
34
35 if (function->IsJSFunction()) {
36 Handle<Object> script(Handle<JSFunction>::cast(function)->shared().script(),
37 isolate);
38 if (script->IsScript()) {
39 return Smi::FromInt(Handle<Script>::cast(script)->id());
40 }
41 }
42 return Smi::FromInt(-1);
43 }
44
RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode)45 RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
46 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
RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition)58 RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
59 SealHandleScope shs(isolate);
60 DCHECK_EQ(1, args.length());
61
62 auto fun = JSFunction::cast(args[0]);
63 int pos = fun.shared().StartPosition();
64 return Smi::FromInt(pos);
65 }
66
67
RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction)68 RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
69 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
RUNTIME_FUNCTION(Runtime_Call)77 RUNTIME_FUNCTION(Runtime_Call) {
78 HandleScope scope(isolate);
79 DCHECK_LE(2, args.length());
80 int const argc = args.length() - 2;
81 Handle<Object> target = args.at(0);
82 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
RUNTIME_FUNCTION(Runtime_IsFunction)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 } // namespace internal
101 } // namespace v8
102