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.
5 #include "src/runtime/runtime-utils.h"
7 #include "src/arguments.h"
8 #include "src/bootstrapper.h"
9 #include "src/conversions.h"
10 #include "src/debug/debug.h"
11 #include "src/frames-inl.h"
12 #include "src/isolate-inl.h"
13 #include "src/messages.h"
18 RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) {
19 SealHandleScope shs(isolate);
20 DCHECK(args.length() == 0);
21 RUNTIME_ASSERT(isolate->bootstrapper()->IsActive());
22 return isolate->heap()->undefined_value();
26 RUNTIME_FUNCTION(Runtime_ExportFromRuntime) {
27 HandleScope scope(isolate);
28 DCHECK(args.length() == 1);
29 CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
30 RUNTIME_ASSERT(isolate->bootstrapper()->IsActive());
31 JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
33 Bootstrapper::ExportFromRuntime(isolate, container);
34 JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime");
39 RUNTIME_FUNCTION(Runtime_ExportExperimentalFromRuntime) {
40 HandleScope scope(isolate);
41 DCHECK(args.length() == 1);
42 CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
43 RUNTIME_ASSERT(isolate->bootstrapper()->IsActive());
44 JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
45 "ExportExperimentalFromRuntime");
46 Bootstrapper::ExportExperimentalFromRuntime(isolate, container);
47 JSObject::MigrateSlowToFast(container, 0, "ExportExperimentalFromRuntime");
52 RUNTIME_FUNCTION(Runtime_InstallToContext) {
53 HandleScope scope(isolate);
54 DCHECK(args.length() == 1);
55 CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
56 RUNTIME_ASSERT(array->HasFastElements());
57 RUNTIME_ASSERT(isolate->bootstrapper()->IsActive());
58 Handle<Context> native_context = isolate->native_context();
59 Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()));
60 int length = Smi::cast(array->length())->value();
61 for (int i = 0; i < length; i += 2) {
62 RUNTIME_ASSERT(fixed_array->get(i)->IsString());
63 Handle<String> name(String::cast(fixed_array->get(i)));
64 RUNTIME_ASSERT(fixed_array->get(i + 1)->IsJSObject());
65 Handle<JSObject> object(JSObject::cast(fixed_array->get(i + 1)));
66 int index = Context::ImportedFieldIndexForName(name);
67 if (index == Context::kNotFound) {
68 index = Context::IntrinsicIndexForName(name);
70 RUNTIME_ASSERT(index != Context::kNotFound);
71 native_context->set(index, *object);
73 return isolate->heap()->undefined_value();
77 RUNTIME_FUNCTION(Runtime_Throw) {
78 HandleScope scope(isolate);
79 DCHECK(args.length() == 1);
80 return isolate->Throw(args[0]);
84 RUNTIME_FUNCTION(Runtime_ReThrow) {
85 HandleScope scope(isolate);
86 DCHECK(args.length() == 1);
87 return isolate->ReThrow(args[0]);
91 RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
92 SealHandleScope shs(isolate);
93 DCHECK_EQ(0, args.length());
94 return isolate->StackOverflow();
98 RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
99 SealHandleScope shs(isolate);
100 DCHECK(args.length() == 0);
101 return isolate->UnwindAndFindHandler();
105 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
106 SealHandleScope shs(isolate);
107 DCHECK(args.length() == 0);
108 return isolate->PromoteScheduledException();
112 RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
113 HandleScope scope(isolate);
114 DCHECK(args.length() == 1);
115 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
116 THROW_NEW_ERROR_RETURN_FAILURE(
117 isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
121 RUNTIME_FUNCTION(Runtime_NewTypeError) {
122 HandleScope scope(isolate);
123 DCHECK(args.length() == 2);
124 CONVERT_INT32_ARG_CHECKED(template_index, 0);
125 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
126 auto message_template =
127 static_cast<MessageTemplate::Template>(template_index);
128 return *isolate->factory()->NewTypeError(message_template, arg0);
132 RUNTIME_FUNCTION(Runtime_NewReferenceError) {
133 HandleScope scope(isolate);
134 DCHECK(args.length() == 2);
135 CONVERT_INT32_ARG_CHECKED(template_index, 0);
136 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
137 auto message_template =
138 static_cast<MessageTemplate::Template>(template_index);
139 return *isolate->factory()->NewReferenceError(message_template, arg0);
143 RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
144 HandleScope scope(isolate);
145 DCHECK(args.length() == 2);
146 CONVERT_INT32_ARG_CHECKED(template_index, 0);
147 CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
148 auto message_template =
149 static_cast<MessageTemplate::Template>(template_index);
150 return *isolate->factory()->NewSyntaxError(message_template, arg0);
154 RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
155 HandleScope scope(isolate);
156 DCHECK(args.length() == 1);
157 CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
158 THROW_NEW_ERROR_RETURN_FAILURE(
160 NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
164 RUNTIME_FUNCTION(Runtime_ThrowStrongModeImplicitConversion) {
165 HandleScope scope(isolate);
166 DCHECK(args.length() == 0);
167 THROW_NEW_ERROR_RETURN_FAILURE(
168 isolate, NewTypeError(MessageTemplate::kStrongImplicitConversion));
172 RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) {
173 DCHECK(args.length() == 3);
174 HandleScope scope(isolate);
175 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
176 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
177 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2);
178 if (debug_event) isolate->debug()->OnPromiseReject(promise, value);
179 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
180 // Do not report if we actually have a handler.
181 if (JSReceiver::GetDataProperty(promise, key)->IsUndefined()) {
182 isolate->ReportPromiseReject(promise, value,
183 v8::kPromiseRejectWithNoHandler);
185 return isolate->heap()->undefined_value();
189 RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) {
190 DCHECK(args.length() == 1);
191 HandleScope scope(isolate);
192 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
193 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
194 // At this point, no revocation has been issued before
195 RUNTIME_ASSERT(JSReceiver::GetDataProperty(promise, key)->IsUndefined());
196 isolate->ReportPromiseReject(promise, Handle<Object>(),
197 v8::kPromiseHandlerAddedAfterReject);
198 return isolate->heap()->undefined_value();
202 RUNTIME_FUNCTION(Runtime_StackGuard) {
203 SealHandleScope shs(isolate);
204 DCHECK(args.length() == 0);
206 // First check if this is a real stack overflow.
207 StackLimitCheck check(isolate);
208 if (check.JsHasOverflowed()) {
209 return isolate->StackOverflow();
212 return isolate->stack_guard()->HandleInterrupts();
216 RUNTIME_FUNCTION(Runtime_Interrupt) {
217 SealHandleScope shs(isolate);
218 DCHECK(args.length() == 0);
219 return isolate->stack_guard()->HandleInterrupts();
223 RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
224 HandleScope scope(isolate);
225 DCHECK(args.length() == 1);
226 CONVERT_SMI_ARG_CHECKED(size, 0);
227 RUNTIME_ASSERT(IsAligned(size, kPointerSize));
228 RUNTIME_ASSERT(size > 0);
229 RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize);
230 return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE);
234 RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
235 HandleScope scope(isolate);
236 DCHECK(args.length() == 2);
237 CONVERT_SMI_ARG_CHECKED(size, 0);
238 CONVERT_SMI_ARG_CHECKED(flags, 1);
239 RUNTIME_ASSERT(IsAligned(size, kPointerSize));
240 RUNTIME_ASSERT(size > 0);
241 RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize);
242 bool double_align = AllocateDoubleAlignFlag::decode(flags);
243 AllocationSpace space = AllocateTargetSpace::decode(flags);
244 return *isolate->factory()->NewFillerObject(size, double_align, space);
248 // Collect the raw data for a stack trace. Returns an array of 4
249 // element segments each containing a receiver, function, code and
250 // native code offset.
251 RUNTIME_FUNCTION(Runtime_CollectStackTrace) {
252 HandleScope scope(isolate);
253 DCHECK(args.length() == 2);
254 CONVERT_ARG_HANDLE_CHECKED(JSObject, error_object, 0);
255 CONVERT_ARG_HANDLE_CHECKED(Object, caller, 1);
257 if (!isolate->bootstrapper()->IsActive()) {
258 // Optionally capture a more detailed stack trace for the message.
259 RETURN_FAILURE_ON_EXCEPTION(
260 isolate, isolate->CaptureAndSetDetailedStackTrace(error_object));
261 // Capture a simple stack trace for the stack property.
262 RETURN_FAILURE_ON_EXCEPTION(
263 isolate, isolate->CaptureAndSetSimpleStackTrace(error_object, caller));
265 return isolate->heap()->undefined_value();
269 RUNTIME_FUNCTION(Runtime_MessageGetStartPosition) {
270 SealHandleScope shs(isolate);
271 DCHECK(args.length() == 1);
272 CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
273 return Smi::FromInt(message->start_position());
277 RUNTIME_FUNCTION(Runtime_MessageGetScript) {
278 SealHandleScope shs(isolate);
279 DCHECK(args.length() == 1);
280 CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
281 return message->script();
285 RUNTIME_FUNCTION(Runtime_ErrorToStringRT) {
286 HandleScope scope(isolate);
287 DCHECK(args.length() == 1);
288 CONVERT_ARG_HANDLE_CHECKED(JSObject, error, 0);
289 Handle<String> result;
290 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
292 isolate->error_tostring_helper()->Stringify(isolate, error));
297 RUNTIME_FUNCTION(Runtime_FormatMessageString) {
298 HandleScope scope(isolate);
299 DCHECK(args.length() == 4);
300 CONVERT_INT32_ARG_CHECKED(template_index, 0);
301 CONVERT_ARG_HANDLE_CHECKED(String, arg0, 1);
302 CONVERT_ARG_HANDLE_CHECKED(String, arg1, 2);
303 CONVERT_ARG_HANDLE_CHECKED(String, arg2, 3);
304 Handle<String> result;
305 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
307 MessageTemplate::FormatMessage(template_index, arg0, arg1, arg2));
312 #define CALLSITE_GET(NAME, RETURN) \
313 RUNTIME_FUNCTION(Runtime_CallSite##NAME##RT) { \
314 HandleScope scope(isolate); \
315 DCHECK(args.length() == 1); \
316 CONVERT_ARG_HANDLE_CHECKED(JSObject, call_site_obj, 0); \
317 Handle<String> result; \
318 CallSite call_site(isolate, call_site_obj); \
319 return RETURN(call_site.NAME(), isolate); \
322 static inline Object* ReturnDereferencedHandle(Handle<Object> obj,
328 static inline Object* ReturnPositiveSmiOrNull(int value, Isolate* isolate) {
329 if (value >= 0) return Smi::FromInt(value);
330 return isolate->heap()->null_value();
334 static inline Object* ReturnBoolean(bool value, Isolate* isolate) {
335 return isolate->heap()->ToBoolean(value);
339 CALLSITE_GET(GetFileName, ReturnDereferencedHandle)
340 CALLSITE_GET(GetFunctionName, ReturnDereferencedHandle)
341 CALLSITE_GET(GetScriptNameOrSourceUrl, ReturnDereferencedHandle)
342 CALLSITE_GET(GetMethodName, ReturnDereferencedHandle)
343 CALLSITE_GET(GetLineNumber, ReturnPositiveSmiOrNull)
344 CALLSITE_GET(GetColumnNumber, ReturnPositiveSmiOrNull)
345 CALLSITE_GET(IsNative, ReturnBoolean)
346 CALLSITE_GET(IsToplevel, ReturnBoolean)
347 CALLSITE_GET(IsEval, ReturnBoolean)
348 CALLSITE_GET(IsConstructor, ReturnBoolean)
353 RUNTIME_FUNCTION(Runtime_IS_VAR) {
354 UNREACHABLE(); // implemented as macro in the parser
359 RUNTIME_FUNCTION(Runtime_IncrementStatsCounter) {
360 SealHandleScope shs(isolate);
361 DCHECK(args.length() == 1);
362 CONVERT_ARG_CHECKED(String, name, 0);
364 if (FLAG_native_code_counters) {
365 StatsCounter(isolate, name->ToCString().get()).Increment();
367 return isolate->heap()->undefined_value();
371 RUNTIME_FUNCTION(Runtime_Likely) {
372 DCHECK(args.length() == 1);
377 RUNTIME_FUNCTION(Runtime_Unlikely) {
378 DCHECK(args.length() == 1);
383 RUNTIME_FUNCTION(Runtime_HarmonyToString) {
384 // TODO(caitp): Delete this runtime method when removing --harmony-tostring
385 return isolate->heap()->ToBoolean(FLAG_harmony_tostring);
389 RUNTIME_FUNCTION(Runtime_GetTypeFeedbackVector) {
390 SealHandleScope shs(isolate);
391 DCHECK(args.length() == 1);
392 CONVERT_ARG_CHECKED(JSFunction, function, 0);
393 return function->shared()->feedback_vector();
397 RUNTIME_FUNCTION(Runtime_GetCallerJSFunction) {
398 SealHandleScope shs(isolate);
399 StackFrameIterator it(isolate);
400 RUNTIME_ASSERT(it.frame()->type() == StackFrame::STUB);
402 RUNTIME_ASSERT(it.frame()->type() == StackFrame::JAVA_SCRIPT);
403 return JavaScriptFrame::cast(it.frame())->function();
407 RUNTIME_FUNCTION(Runtime_GetCodeStubExportsObject) {
408 HandleScope shs(isolate);
409 return isolate->heap()->code_stub_exports_object();
412 } // namespace internal