blob: 56cf3b6dcbd8c371364bb256cf411277afae868d [file] [log] [blame]
Emily Bernierd0a1eb72015-03-24 16:35:39 -04001// 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
Ben Murdoch4a90d5f2016-03-22 12:00:34 +00005#include "src/runtime/runtime-utils.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -04006
7#include "src/accessors.h"
8#include "src/arguments.h"
9#include "src/compiler.h"
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000010#include "src/frames-inl.h"
11#include "src/isolate-inl.h"
12#include "src/messages.h"
13#include "src/profiler/cpu-profiler.h"
Ben Murdochc5610432016-08-08 18:44:38 +010014#include "src/wasm/wasm-module.h"
Emily Bernierd0a1eb72015-03-24 16:35:39 -040015
16namespace v8 {
17namespace internal {
18
Emily Bernierd0a1eb72015-03-24 16:35:39 -040019RUNTIME_FUNCTION(Runtime_FunctionGetName) {
Ben Murdochda12d292016-06-02 14:46:10 +010020 HandleScope scope(isolate);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040021 DCHECK(args.length() == 1);
22
Ben Murdochda12d292016-06-02 14:46:10 +010023 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
Ben Murdochda12d292016-06-02 14:46:10 +010024 Handle<Object> result;
Ben Murdochc5610432016-08-08 18:44:38 +010025 if (function->IsJSBoundFunction()) {
Ben Murdochda12d292016-06-02 14:46:10 +010026 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
27 isolate, result, JSBoundFunction::GetName(
28 isolate, Handle<JSBoundFunction>::cast(function)));
Ben Murdochda12d292016-06-02 14:46:10 +010029 } else {
Ben Murdochc5610432016-08-08 18:44:38 +010030 result = JSFunction::GetName(isolate, Handle<JSFunction>::cast(function));
Ben Murdochda12d292016-06-02 14:46:10 +010031 }
Ben Murdochc5610432016-08-08 18:44:38 +010032 return *result;
Emily Bernierd0a1eb72015-03-24 16:35:39 -040033}
34
35
Emily Bernierd0a1eb72015-03-24 16:35:39 -040036RUNTIME_FUNCTION(Runtime_FunctionSetName) {
37 HandleScope scope(isolate);
38 DCHECK(args.length() == 2);
39
40 CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000041 CONVERT_ARG_HANDLE_CHECKED(String, name, 1);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040042
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000043 name = String::Flatten(name);
44 f->shared()->set_name(*name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040045 return isolate->heap()->undefined_value();
46}
47
48
Emily Bernierd0a1eb72015-03-24 16:35:39 -040049RUNTIME_FUNCTION(Runtime_FunctionRemovePrototype) {
50 SealHandleScope shs(isolate);
51 DCHECK(args.length() == 1);
52
53 CONVERT_ARG_CHECKED(JSFunction, f, 0);
54 RUNTIME_ASSERT(f->RemovePrototype());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000055 f->shared()->set_construct_stub(
56 *isolate->builtins()->ConstructedNonConstructable());
Emily Bernierd0a1eb72015-03-24 16:35:39 -040057
58 return isolate->heap()->undefined_value();
59}
60
61
62RUNTIME_FUNCTION(Runtime_FunctionGetScript) {
63 HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000064 DCHECK_EQ(1, args.length());
65 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -040066
Ben Murdoch097c5b22016-05-18 11:27:45 +010067 if (function->IsJSFunction()) {
68 Handle<Object> script(
69 Handle<JSFunction>::cast(function)->shared()->script(), isolate);
70 if (script->IsScript()) {
71 return *Script::GetWrapper(Handle<Script>::cast(script));
72 }
73 }
74 return isolate->heap()->undefined_value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040075}
76
77
78RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
79 HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +000080 DCHECK_EQ(1, args.length());
81 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
Ben Murdoch097c5b22016-05-18 11:27:45 +010082 if (function->IsJSFunction()) {
83 return *Handle<JSFunction>::cast(function)->shared()->GetSourceCode();
84 }
85 return isolate->heap()->undefined_value();
Emily Bernierd0a1eb72015-03-24 16:35:39 -040086}
87
88
89RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
90 SealHandleScope shs(isolate);
91 DCHECK(args.length() == 1);
92
93 CONVERT_ARG_CHECKED(JSFunction, fun, 0);
94 int pos = fun->shared()->start_position();
95 return Smi::FromInt(pos);
96}
97
98
99RUNTIME_FUNCTION(Runtime_FunctionGetPositionForOffset) {
100 SealHandleScope shs(isolate);
101 DCHECK(args.length() == 2);
102
Ben Murdoch097c5b22016-05-18 11:27:45 +0100103 CONVERT_ARG_CHECKED(AbstractCode, abstract_code, 0);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400104 CONVERT_NUMBER_CHECKED(int, offset, Int32, args[1]);
Ben Murdoch097c5b22016-05-18 11:27:45 +0100105 return Smi::FromInt(abstract_code->SourcePosition(offset));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400106}
107
Ben Murdochda12d292016-06-02 14:46:10 +0100108RUNTIME_FUNCTION(Runtime_FunctionGetContextData) {
109 SealHandleScope shs(isolate);
110 DCHECK(args.length() == 1);
111
112 CONVERT_ARG_CHECKED(JSFunction, fun, 0);
113 FixedArray* array = fun->native_context()->embedder_data();
114 return array->get(v8::Context::kDebugIdIndex);
115}
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400116
117RUNTIME_FUNCTION(Runtime_FunctionSetInstanceClassName) {
118 SealHandleScope shs(isolate);
119 DCHECK(args.length() == 2);
120
121 CONVERT_ARG_CHECKED(JSFunction, fun, 0);
122 CONVERT_ARG_CHECKED(String, name, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000123 fun->shared()->set_instance_class_name(name);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400124 return isolate->heap()->undefined_value();
125}
126
127
128RUNTIME_FUNCTION(Runtime_FunctionSetLength) {
129 SealHandleScope shs(isolate);
130 DCHECK(args.length() == 2);
131
132 CONVERT_ARG_CHECKED(JSFunction, fun, 0);
133 CONVERT_SMI_ARG_CHECKED(length, 1);
134 RUNTIME_ASSERT((length & 0xC0000000) == 0xC0000000 ||
135 (length & 0xC0000000) == 0x0);
136 fun->shared()->set_length(length);
137 return isolate->heap()->undefined_value();
138}
139
140
141RUNTIME_FUNCTION(Runtime_FunctionSetPrototype) {
142 HandleScope scope(isolate);
143 DCHECK(args.length() == 2);
144
145 CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0);
146 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000147 RUNTIME_ASSERT(fun->IsConstructor());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400148 RETURN_FAILURE_ON_EXCEPTION(isolate,
149 Accessors::FunctionSetPrototype(fun, value));
150 return args[0]; // return TOS
151}
152
153
154RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
155 SealHandleScope shs(isolate);
156 DCHECK(args.length() == 1);
157
158 CONVERT_ARG_CHECKED(JSFunction, f, 0);
159 return isolate->heap()->ToBoolean(f->shared()->IsApiFunction());
160}
161
162
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400163RUNTIME_FUNCTION(Runtime_SetCode) {
164 HandleScope scope(isolate);
165 DCHECK(args.length() == 2);
166
167 CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
168 CONVERT_ARG_HANDLE_CHECKED(JSFunction, source, 1);
169
170 Handle<SharedFunctionInfo> target_shared(target->shared());
171 Handle<SharedFunctionInfo> source_shared(source->shared());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400172
Ben Murdochda12d292016-06-02 14:46:10 +0100173 if (!Compiler::Compile(source, Compiler::KEEP_EXCEPTION)) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400174 return isolate->heap()->exception();
175 }
176
177 // Mark both, the source and the target, as un-flushable because the
178 // shared unoptimized code makes them impossible to enqueue in a list.
179 DCHECK(target_shared->code()->gc_metadata() == NULL);
180 DCHECK(source_shared->code()->gc_metadata() == NULL);
181 target_shared->set_dont_flush(true);
182 source_shared->set_dont_flush(true);
183
184 // Set the code, scope info, formal parameter count, and the length
185 // of the target shared function info.
186 target_shared->ReplaceCode(source_shared->code());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100187 if (source_shared->HasBytecodeArray()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100188 target_shared->set_bytecode_array(source_shared->bytecode_array());
Ben Murdoch097c5b22016-05-18 11:27:45 +0100189 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400190 target_shared->set_scope_info(source_shared->scope_info());
191 target_shared->set_length(source_shared->length());
192 target_shared->set_feedback_vector(source_shared->feedback_vector());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000193 target_shared->set_internal_formal_parameter_count(
194 source_shared->internal_formal_parameter_count());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400195 target_shared->set_start_position_and_type(
196 source_shared->start_position_and_type());
197 target_shared->set_end_position(source_shared->end_position());
198 bool was_native = target_shared->native();
199 target_shared->set_compiler_hints(source_shared->compiler_hints());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000200 target_shared->set_opt_count_and_bailout_reason(
201 source_shared->opt_count_and_bailout_reason());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400202 target_shared->set_native(was_native);
203 target_shared->set_profiler_ticks(source_shared->profiler_ticks());
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000204 SharedFunctionInfo::SetScript(
205 target_shared, Handle<Object>(source_shared->script(), isolate));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400206
207 // Set the code of the target function.
208 target->ReplaceCode(source_shared->code());
209 DCHECK(target->next_function_link()->IsUndefined());
210
211 // Make sure we get a fresh copy of the literal vector to avoid cross
212 // context contamination.
213 Handle<Context> context(source->context());
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400214 target->set_context(*context);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000215
216 int number_of_literals = source->NumberOfLiterals();
217 Handle<LiteralsArray> literals =
218 LiteralsArray::New(isolate, handle(target_shared->feedback_vector()),
219 number_of_literals, TENURED);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400220 target->set_literals(*literals);
221
222 if (isolate->logger()->is_logging_code_events() ||
223 isolate->cpu_profiler()->is_profiling()) {
Ben Murdochda12d292016-06-02 14:46:10 +0100224 isolate->logger()->LogExistingFunction(
225 source_shared, Handle<AbstractCode>(source_shared->abstract_code()));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400226 }
227
228 return *target;
229}
230
231
232// Set the native flag on the function.
233// This is used to decide if we should transform null and undefined
234// into the global object when doing call and apply.
235RUNTIME_FUNCTION(Runtime_SetNativeFlag) {
236 SealHandleScope shs(isolate);
237 RUNTIME_ASSERT(args.length() == 1);
238
239 CONVERT_ARG_CHECKED(Object, object, 0);
240
241 if (object->IsJSFunction()) {
242 JSFunction* func = JSFunction::cast(object);
243 func->shared()->set_native(true);
244 }
245 return isolate->heap()->undefined_value();
246}
247
248
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000249RUNTIME_FUNCTION(Runtime_IsConstructor) {
250 SealHandleScope shs(isolate);
251 DCHECK_EQ(1, args.length());
252 CONVERT_ARG_CHECKED(Object, object, 0);
253 return isolate->heap()->ToBoolean(object->IsConstructor());
254}
255
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000256RUNTIME_FUNCTION(Runtime_SetForceInlineFlag) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400257 SealHandleScope shs(isolate);
258 RUNTIME_ASSERT(args.length() == 1);
259 CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
260
261 if (object->IsJSFunction()) {
262 JSFunction* func = JSFunction::cast(*object);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000263 func->shared()->set_force_inline(true);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400264 }
265 return isolate->heap()->undefined_value();
266}
267
268
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400269RUNTIME_FUNCTION(Runtime_Call) {
270 HandleScope scope(isolate);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000271 DCHECK_LE(2, args.length());
272 int const argc = args.length() - 2;
Ben Murdochda12d292016-06-02 14:46:10 +0100273 CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000274 CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
275 ScopedVector<Handle<Object>> argv(argc);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400276 for (int i = 0; i < argc; ++i) {
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000277 argv[i] = args.at<Object>(2 + i);
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400278 }
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400279 Handle<Object> result;
280 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
281 isolate, result,
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000282 Execution::Call(isolate, target, receiver, argc, argv.start()));
283 return *result;
284}
285
286
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000287// ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
288RUNTIME_FUNCTION(Runtime_ConvertReceiver) {
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400289 HandleScope scope(isolate);
290 DCHECK(args.length() == 1);
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000291 CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 0);
Ben Murdochc5610432016-08-08 18:44:38 +0100292 return *Object::ConvertReceiver(isolate, receiver).ToHandleChecked();
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400293}
294
295
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000296RUNTIME_FUNCTION(Runtime_IsFunction) {
297 SealHandleScope shs(isolate);
298 DCHECK_EQ(1, args.length());
299 CONVERT_ARG_CHECKED(Object, object, 0);
300 return isolate->heap()->ToBoolean(object->IsFunction());
301}
302
303
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000304RUNTIME_FUNCTION(Runtime_FunctionToString) {
305 HandleScope scope(isolate);
306 DCHECK_EQ(1, args.length());
307 CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
308 return function->IsJSBoundFunction()
309 ? *JSBoundFunction::ToString(
310 Handle<JSBoundFunction>::cast(function))
311 : *JSFunction::ToString(Handle<JSFunction>::cast(function));
Emily Bernierd0a1eb72015-03-24 16:35:39 -0400312}
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000313
Ben Murdochc5610432016-08-08 18:44:38 +0100314RUNTIME_FUNCTION(Runtime_WasmGetFunctionName) {
315 HandleScope scope(isolate);
316 DCHECK_EQ(2, args.length());
317
318 CONVERT_ARG_HANDLE_CHECKED(JSObject, wasm, 0);
319 CONVERT_SMI_ARG_CHECKED(func_index, 1);
320
321 return *wasm::GetWasmFunctionName(wasm, func_index);
322}
323
Ben Murdoch4a90d5f2016-03-22 12:00:34 +0000324} // namespace internal
325} // namespace v8