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.
7 #include "src/arguments.h"
8 #include "src/bootstrapper.h"
10 #include "src/messages.h"
11 #include "src/parser.h"
12 #include "src/prettyprinter.h"
13 #include "src/runtime/runtime-utils.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_Throw) {
27 HandleScope scope(isolate);
28 DCHECK(args.length() == 1);
30 return isolate->Throw(args[0]);
34 RUNTIME_FUNCTION(Runtime_ReThrow) {
35 HandleScope scope(isolate);
36 DCHECK(args.length() == 1);
38 return isolate->ReThrow(args[0]);
42 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
43 SealHandleScope shs(isolate);
44 DCHECK(args.length() == 0);
45 return isolate->PromoteScheduledException();
49 RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
50 HandleScope scope(isolate);
51 DCHECK(args.length() == 1);
52 CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
53 THROW_NEW_ERROR_RETURN_FAILURE(
54 isolate, NewReferenceError("not_defined", HandleVector(&name, 1)));
58 RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) {
59 DCHECK(args.length() == 3);
60 HandleScope scope(isolate);
61 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
62 CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
63 CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2);
64 if (debug_event) isolate->debug()->OnPromiseReject(promise, value);
65 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
66 // Do not report if we actually have a handler.
67 if (JSObject::GetDataProperty(promise, key)->IsUndefined()) {
68 isolate->ReportPromiseReject(promise, value,
69 v8::kPromiseRejectWithNoHandler);
71 return isolate->heap()->undefined_value();
75 RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) {
76 DCHECK(args.length() == 1);
77 HandleScope scope(isolate);
78 CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
79 Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
80 // At this point, no revocation has been issued before
81 RUNTIME_ASSERT(JSObject::GetDataProperty(promise, key)->IsUndefined());
82 isolate->ReportPromiseReject(promise, Handle<Object>(),
83 v8::kPromiseHandlerAddedAfterReject);
84 return isolate->heap()->undefined_value();
88 RUNTIME_FUNCTION(Runtime_PromiseHasHandlerSymbol) {
89 DCHECK(args.length() == 0);
90 return isolate->heap()->promise_has_handler_symbol();
94 RUNTIME_FUNCTION(Runtime_StackGuard) {
95 SealHandleScope shs(isolate);
96 DCHECK(args.length() == 0);
98 // First check if this is a real stack overflow.
99 StackLimitCheck check(isolate);
100 if (check.JsHasOverflowed()) {
101 return isolate->StackOverflow();
104 return isolate->stack_guard()->HandleInterrupts();
108 RUNTIME_FUNCTION(Runtime_Interrupt) {
109 SealHandleScope shs(isolate);
110 DCHECK(args.length() == 0);
111 return isolate->stack_guard()->HandleInterrupts();
115 RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
116 HandleScope scope(isolate);
117 DCHECK(args.length() == 1);
118 CONVERT_SMI_ARG_CHECKED(size, 0);
119 RUNTIME_ASSERT(IsAligned(size, kPointerSize));
120 RUNTIME_ASSERT(size > 0);
121 RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize);
122 return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE);
126 RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
127 HandleScope scope(isolate);
128 DCHECK(args.length() == 2);
129 CONVERT_SMI_ARG_CHECKED(size, 0);
130 CONVERT_SMI_ARG_CHECKED(flags, 1);
131 RUNTIME_ASSERT(IsAligned(size, kPointerSize));
132 RUNTIME_ASSERT(size > 0);
133 RUNTIME_ASSERT(size <= Page::kMaxRegularHeapObjectSize);
134 bool double_align = AllocateDoubleAlignFlag::decode(flags);
135 AllocationSpace space = AllocateTargetSpace::decode(flags);
136 return *isolate->factory()->NewFillerObject(size, double_align, space);
140 // Collect the raw data for a stack trace. Returns an array of 4
141 // element segments each containing a receiver, function, code and
142 // native code offset.
143 RUNTIME_FUNCTION(Runtime_CollectStackTrace) {
144 HandleScope scope(isolate);
145 DCHECK(args.length() == 2);
146 CONVERT_ARG_HANDLE_CHECKED(JSObject, error_object, 0);
147 CONVERT_ARG_HANDLE_CHECKED(Object, caller, 1);
149 if (!isolate->bootstrapper()->IsActive()) {
150 // Optionally capture a more detailed stack trace for the message.
151 isolate->CaptureAndSetDetailedStackTrace(error_object);
152 // Capture a simple stack trace for the stack property.
153 isolate->CaptureAndSetSimpleStackTrace(error_object, caller);
155 return isolate->heap()->undefined_value();
159 RUNTIME_FUNCTION(Runtime_RenderCallSite) {
160 HandleScope scope(isolate);
161 DCHECK(args.length() == 0);
162 MessageLocation location;
163 isolate->ComputeLocation(&location);
164 if (location.start_pos() == -1) return isolate->heap()->empty_string();
167 if (location.function()->shared()->is_function()) {
168 CompilationInfo info(location.function(), &zone);
169 if (!Parser::ParseStatic(&info)) {
170 isolate->clear_pending_exception();
171 return isolate->heap()->empty_string();
173 CallPrinter printer(isolate, &zone);
174 const char* string = printer.Print(info.function(), location.start_pos());
175 return *isolate->factory()->NewStringFromAsciiChecked(string);
178 CompilationInfo info(location.script(), &zone);
179 if (!Parser::ParseStatic(&info)) {
180 isolate->clear_pending_exception();
181 return isolate->heap()->empty_string();
183 CallPrinter printer(isolate, &zone);
184 const char* string = printer.Print(info.function(), location.start_pos());
185 return *isolate->factory()->NewStringFromAsciiChecked(string);
189 RUNTIME_FUNCTION(Runtime_GetFromCache) {
190 SealHandleScope shs(isolate);
191 // This is only called from codegen, so checks might be more lax.
192 CONVERT_ARG_CHECKED(JSFunctionResultCache, cache, 0);
193 CONVERT_ARG_CHECKED(Object, key, 1);
196 DisallowHeapAllocation no_alloc;
198 int finger_index = cache->finger_index();
199 Object* o = cache->get(finger_index);
201 // The fastest case: hit the same place again.
202 return cache->get(finger_index + 1);
205 for (int i = finger_index - 2; i >= JSFunctionResultCache::kEntriesIndex;
209 cache->set_finger_index(i);
210 return cache->get(i + 1);
214 int size = cache->size();
215 DCHECK(size <= cache->length());
217 for (int i = size - 2; i > finger_index; i -= 2) {
220 cache->set_finger_index(i);
221 return cache->get(i + 1);
226 // There is no value in the cache. Invoke the function and cache result.
227 HandleScope scope(isolate);
229 Handle<JSFunctionResultCache> cache_handle(cache);
230 Handle<Object> key_handle(key, isolate);
231 Handle<Object> value;
233 Handle<JSFunction> factory(JSFunction::cast(
234 cache_handle->get(JSFunctionResultCache::kFactoryIndex)));
235 // TODO(antonm): consider passing a receiver when constructing a cache.
236 Handle<JSObject> receiver(isolate->global_proxy());
237 // This handle is nor shared, nor used later, so it's safe.
238 Handle<Object> argv[] = {key_handle};
239 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
241 Execution::Call(isolate, factory, receiver, arraysize(argv), argv));
245 if (FLAG_verify_heap) {
246 cache_handle->JSFunctionResultCacheVerify();
250 // Function invocation may have cleared the cache. Reread all the data.
251 int finger_index = cache_handle->finger_index();
252 int size = cache_handle->size();
254 // If we have spare room, put new data into it, otherwise evict post finger
255 // entry which is likely to be the least recently used.
257 if (size < cache_handle->length()) {
258 cache_handle->set_size(size + JSFunctionResultCache::kEntrySize);
261 index = finger_index + JSFunctionResultCache::kEntrySize;
262 if (index == cache_handle->length()) {
263 index = JSFunctionResultCache::kEntriesIndex;
267 DCHECK(index % 2 == 0);
268 DCHECK(index >= JSFunctionResultCache::kEntriesIndex);
269 DCHECK(index < cache_handle->length());
271 cache_handle->set(index, *key_handle);
272 cache_handle->set(index + 1, *value);
273 cache_handle->set_finger_index(index);
276 if (FLAG_verify_heap) {
277 cache_handle->JSFunctionResultCacheVerify();
285 RUNTIME_FUNCTION(Runtime_MessageGetStartPosition) {
286 SealHandleScope shs(isolate);
287 DCHECK(args.length() == 1);
288 CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
289 return Smi::FromInt(message->start_position());
293 RUNTIME_FUNCTION(Runtime_MessageGetScript) {
294 SealHandleScope shs(isolate);
295 DCHECK(args.length() == 1);
296 CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
297 return message->script();
301 RUNTIME_FUNCTION(Runtime_IS_VAR) {
302 UNREACHABLE(); // implemented as macro in the parser
307 RUNTIME_FUNCTION(RuntimeReference_GetFromCache) {
308 HandleScope scope(isolate);
309 DCHECK(args.length() == 2);
310 CONVERT_SMI_ARG_CHECKED(id, 0);
311 args[0] = isolate->native_context()->jsfunction_result_caches()->get(id);
312 return __RT_impl_Runtime_GetFromCache(args, isolate);
315 } // namespace v8::internal