V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
runtime-function.cc
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/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 namespace v8 {
13 namespace internal {
14 
15 // TODO(5530): Remove once uses in debug.js are gone.
16 RUNTIME_FUNCTION(Runtime_FunctionGetScriptSource) {
17  HandleScope scope(isolate);
18  DCHECK_EQ(1, args.length());
19  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
20 
21  if (function->IsJSFunction()) {
22  Handle<Object> script(
23  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 RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
30  HandleScope scope(isolate);
31  DCHECK_EQ(1, args.length());
32  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
33 
34  if (function->IsJSFunction()) {
35  Handle<Object> script(
36  Handle<JSFunction>::cast(function)->shared()->script(), isolate);
37  if (script->IsScript()) {
38  return Smi::FromInt(Handle<Script>::cast(script)->id());
39  }
40  }
41  return Smi::FromInt(-1);
42 }
43 
44 RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
45  HandleScope scope(isolate);
46  DCHECK_EQ(1, args.length());
47  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 RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
58  SealHandleScope shs(isolate);
59  DCHECK_EQ(1, args.length());
60 
61  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
62  int pos = fun->shared()->StartPosition();
63  return Smi::FromInt(pos);
64 }
65 
66 
67 RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
68  SealHandleScope shs(isolate);
69  DCHECK_EQ(1, args.length());
70 
71  CONVERT_ARG_CHECKED(JSFunction, f, 0);
72  return isolate->heap()->ToBoolean(f->shared()->IsApiFunction());
73 }
74 
75 
76 // Set the native flag on the function.
77 // This is used to decide if we should transform null and undefined
78 // into the global object when doing call and apply.
79 RUNTIME_FUNCTION(Runtime_SetNativeFlag) {
80  SealHandleScope shs(isolate);
81  DCHECK_EQ(1, args.length());
82 
83  CONVERT_ARG_CHECKED(Object, object, 0);
84 
85  if (object->IsJSFunction()) {
86  JSFunction* func = JSFunction::cast(object);
87  func->shared()->set_native(true);
88  }
89  return ReadOnlyRoots(isolate).undefined_value();
90 }
91 
92 
93 RUNTIME_FUNCTION(Runtime_Call) {
94  HandleScope scope(isolate);
95  DCHECK_LE(2, args.length());
96  int const argc = args.length() - 2;
97  CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
98  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
99  ScopedVector<Handle<Object>> argv(argc);
100  for (int i = 0; i < argc; ++i) {
101  argv[i] = args.at(2 + i);
102  }
103  RETURN_RESULT_OR_FAILURE(
104  isolate, Execution::Call(isolate, target, receiver, argc, argv.start()));
105 }
106 
107 
108 RUNTIME_FUNCTION(Runtime_IsFunction) {
109  SealHandleScope shs(isolate);
110  DCHECK_EQ(1, args.length());
111  CONVERT_ARG_CHECKED(Object, object, 0);
112  return isolate->heap()->ToBoolean(object->IsFunction());
113 }
114 
115 
116 } // namespace internal
117 } // namespace v8
Definition: libplatform.h:13