Line data Source code
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/runtime/runtime-utils.h"
6 :
7 : #include "src/accessors.h"
8 : #include "src/arguments.h"
9 : #include "src/compiler.h"
10 : #include "src/isolate-inl.h"
11 : #include "src/messages.h"
12 :
13 : namespace v8 {
14 : namespace internal {
15 :
16 780 : RUNTIME_FUNCTION(Runtime_FunctionGetName) {
17 390 : HandleScope scope(isolate);
18 : DCHECK_EQ(1, args.length());
19 :
20 780 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
21 390 : if (function->IsJSBoundFunction()) {
22 0 : RETURN_RESULT_OR_FAILURE(
23 : isolate, JSBoundFunction::GetName(
24 : isolate, Handle<JSBoundFunction>::cast(function)));
25 : } else {
26 780 : return *JSFunction::GetName(isolate, Handle<JSFunction>::cast(function));
27 390 : }
28 : }
29 :
30 : // TODO(5530): Remove once uses in debug.js are gone.
31 2350 : RUNTIME_FUNCTION(Runtime_FunctionGetScript) {
32 1175 : HandleScope scope(isolate);
33 : DCHECK_EQ(1, args.length());
34 2350 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
35 :
36 1175 : if (function->IsJSFunction()) {
37 : Handle<Object> script(
38 3495 : Handle<JSFunction>::cast(function)->shared()->script(), isolate);
39 1165 : if (script->IsScript()) {
40 2330 : return *Script::GetWrapper(Handle<Script>::cast(script));
41 : }
42 : }
43 10 : return isolate->heap()->undefined_value();
44 : }
45 :
46 7052 : RUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
47 3526 : HandleScope scope(isolate);
48 : DCHECK_EQ(1, args.length());
49 7052 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
50 :
51 3526 : if (function->IsJSFunction()) {
52 : Handle<Object> script(
53 10578 : Handle<JSFunction>::cast(function)->shared()->script(), isolate);
54 3526 : if (script->IsScript()) {
55 7052 : return Smi::FromInt(Handle<Script>::cast(script)->id());
56 : }
57 : }
58 0 : return Smi::FromInt(-1);
59 : }
60 :
61 524 : RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
62 262 : HandleScope scope(isolate);
63 : DCHECK_EQ(1, args.length());
64 524 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
65 262 : if (function->IsJSFunction()) {
66 756 : return *Handle<JSFunction>::cast(function)->shared()->GetSourceCode();
67 : }
68 10 : return isolate->heap()->undefined_value();
69 : }
70 :
71 :
72 3480 : RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
73 : SealHandleScope shs(isolate);
74 : DCHECK_EQ(1, args.length());
75 :
76 3480 : CONVERT_ARG_CHECKED(JSFunction, fun, 0);
77 1740 : int pos = fun->shared()->start_position();
78 1740 : return Smi::FromInt(pos);
79 : }
80 :
81 0 : RUNTIME_FUNCTION(Runtime_FunctionGetContextData) {
82 : SealHandleScope shs(isolate);
83 : DCHECK_EQ(1, args.length());
84 :
85 0 : CONVERT_ARG_CHECKED(JSFunction, fun, 0);
86 0 : return fun->native_context()->debug_context_id();
87 : }
88 :
89 244 : RUNTIME_FUNCTION(Runtime_FunctionSetLength) {
90 : SealHandleScope shs(isolate);
91 : DCHECK_EQ(2, args.length());
92 :
93 244 : CONVERT_ARG_CHECKED(JSFunction, fun, 0);
94 244 : CONVERT_SMI_ARG_CHECKED(length, 1);
95 122 : fun->shared()->set_length(length);
96 122 : return isolate->heap()->undefined_value();
97 : }
98 :
99 :
100 1098 : RUNTIME_FUNCTION(Runtime_FunctionSetPrototype) {
101 549 : HandleScope scope(isolate);
102 : DCHECK_EQ(2, args.length());
103 :
104 1098 : CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0);
105 549 : CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
106 549 : CHECK(fun->IsConstructor());
107 549 : JSFunction::SetPrototype(fun, value);
108 549 : return args[0]; // return TOS
109 : }
110 :
111 :
112 2310 : RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
113 : SealHandleScope shs(isolate);
114 : DCHECK_EQ(1, args.length());
115 :
116 2310 : CONVERT_ARG_CHECKED(JSFunction, f, 0);
117 1155 : return isolate->heap()->ToBoolean(f->shared()->IsApiFunction());
118 : }
119 :
120 :
121 4148 : RUNTIME_FUNCTION(Runtime_SetCode) {
122 1037 : HandleScope scope(isolate);
123 : DCHECK_EQ(2, args.length());
124 :
125 2074 : CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
126 2074 : CONVERT_ARG_HANDLE_CHECKED(JSFunction, source, 1);
127 :
128 1037 : Handle<SharedFunctionInfo> target_shared(target->shared());
129 1037 : Handle<SharedFunctionInfo> source_shared(source->shared());
130 :
131 2074 : if (!source->is_compiled() &&
132 1037 : !Compiler::Compile(source, Compiler::KEEP_EXCEPTION)) {
133 0 : return isolate->heap()->exception();
134 : }
135 :
136 : // Set the code, scope info, formal parameter count, and the length
137 : // of the target shared function info.
138 2074 : target_shared->set_code(source_shared->code());
139 1037 : if (source_shared->HasBytecodeArray()) {
140 2074 : target_shared->set_bytecode_array(source_shared->bytecode_array());
141 : }
142 2074 : target_shared->set_scope_info(source_shared->scope_info());
143 2074 : target_shared->set_outer_scope_info(source_shared->outer_scope_info());
144 2074 : target_shared->set_length(source_shared->GetLength());
145 2074 : target_shared->set_feedback_metadata(source_shared->feedback_metadata());
146 : target_shared->set_internal_formal_parameter_count(
147 2074 : source_shared->internal_formal_parameter_count());
148 : target_shared->set_start_position_and_type(
149 2074 : source_shared->start_position_and_type());
150 2074 : target_shared->set_end_position(source_shared->end_position());
151 1037 : bool was_native = target_shared->native();
152 1037 : target_shared->set_compiler_hints(source_shared->compiler_hints());
153 2074 : target_shared->set_native(was_native);
154 2074 : target_shared->set_function_literal_id(source_shared->function_literal_id());
155 :
156 1037 : Handle<Object> source_script(source_shared->script(), isolate);
157 1037 : if (source_script->IsScript()) {
158 : SharedFunctionInfo::SetScript(source_shared,
159 2074 : isolate->factory()->undefined_value());
160 : }
161 1037 : SharedFunctionInfo::SetScript(target_shared, source_script);
162 :
163 : // Set the code of the target function.
164 2074 : target->set_code(source_shared->code());
165 :
166 1037 : Handle<Context> context(source->context());
167 1037 : target->set_context(*context);
168 :
169 : // Make sure we get a fresh copy of the literal vector to avoid cross
170 : // context contamination, and that the literal vector makes it's way into
171 : // the target_shared optimized code map.
172 1037 : JSFunction::EnsureLiterals(target);
173 :
174 2074 : if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) {
175 : isolate->logger()->LogExistingFunction(
176 0 : source_shared, Handle<AbstractCode>(source_shared->abstract_code()));
177 : }
178 :
179 1037 : return *target;
180 : }
181 :
182 :
183 : // Set the native flag on the function.
184 : // This is used to decide if we should transform null and undefined
185 : // into the global object when doing call and apply.
186 3112 : RUNTIME_FUNCTION(Runtime_SetNativeFlag) {
187 : SealHandleScope shs(isolate);
188 : DCHECK_EQ(1, args.length());
189 :
190 1556 : CONVERT_ARG_CHECKED(Object, object, 0);
191 :
192 1556 : if (object->IsJSFunction()) {
193 : JSFunction* func = JSFunction::cast(object);
194 1556 : func->shared()->set_native(true);
195 : }
196 1556 : return isolate->heap()->undefined_value();
197 : }
198 :
199 :
200 37476 : RUNTIME_FUNCTION(Runtime_IsConstructor) {
201 : SealHandleScope shs(isolate);
202 : DCHECK_EQ(1, args.length());
203 18738 : CONVERT_ARG_CHECKED(Object, object, 0);
204 18738 : return isolate->heap()->ToBoolean(object->IsConstructor());
205 : }
206 :
207 :
208 1028 : RUNTIME_FUNCTION(Runtime_Call) {
209 514 : HandleScope scope(isolate);
210 : DCHECK_LE(2, args.length());
211 514 : int const argc = args.length() - 2;
212 514 : CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
213 514 : CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
214 1028 : ScopedVector<Handle<Object>> argv(argc);
215 702 : for (int i = 0; i < argc; ++i) {
216 702 : argv[i] = args.at(2 + i);
217 : }
218 1028 : RETURN_RESULT_OR_FAILURE(
219 514 : isolate, Execution::Call(isolate, target, receiver, argc, argv.start()));
220 : }
221 :
222 :
223 : // ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
224 0 : RUNTIME_FUNCTION(Runtime_ConvertReceiver) {
225 0 : HandleScope scope(isolate);
226 : DCHECK_EQ(1, args.length());
227 0 : CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 0);
228 0 : return *Object::ConvertReceiver(isolate, receiver).ToHandleChecked();
229 : }
230 :
231 :
232 13770 : RUNTIME_FUNCTION(Runtime_IsFunction) {
233 : SealHandleScope shs(isolate);
234 : DCHECK_EQ(1, args.length());
235 6885 : CONVERT_ARG_CHECKED(Object, object, 0);
236 6885 : return isolate->heap()->ToBoolean(object->IsFunction());
237 : }
238 :
239 :
240 0 : RUNTIME_FUNCTION(Runtime_FunctionToString) {
241 0 : HandleScope scope(isolate);
242 : DCHECK_EQ(1, args.length());
243 0 : CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
244 : return function->IsJSBoundFunction()
245 : ? *JSBoundFunction::ToString(
246 0 : Handle<JSBoundFunction>::cast(function))
247 0 : : *JSFunction::ToString(Handle<JSFunction>::cast(function));
248 : }
249 :
250 : } // namespace internal
251 : } // namespace v8
|