50b61921f59ed8916841e8a4edfd404a3b66208d
[platform/upstream/nodejs.git] / deps / v8 / src / runtime / runtime-internal.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/v8.h"
6
7 #include "src/arguments.h"
8 #include "src/bootstrapper.h"
9 #include "src/debug.h"
10 #include "src/messages.h"
11 #include "src/parser.h"
12 #include "src/prettyprinter.h"
13 #include "src/runtime/runtime-utils.h"
14
15 namespace v8 {
16 namespace internal {
17
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();
23 }
24
25
26 RUNTIME_FUNCTION(Runtime_Throw) {
27   HandleScope scope(isolate);
28   DCHECK(args.length() == 1);
29
30   return isolate->Throw(args[0]);
31 }
32
33
34 RUNTIME_FUNCTION(Runtime_ReThrow) {
35   HandleScope scope(isolate);
36   DCHECK(args.length() == 1);
37
38   return isolate->ReThrow(args[0]);
39 }
40
41
42 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
43   SealHandleScope shs(isolate);
44   DCHECK(args.length() == 0);
45   return isolate->PromoteScheduledException();
46 }
47
48
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)));
55 }
56
57
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);
70   }
71   return isolate->heap()->undefined_value();
72 }
73
74
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();
85 }
86
87
88 RUNTIME_FUNCTION(Runtime_PromiseHasHandlerSymbol) {
89   DCHECK(args.length() == 0);
90   return isolate->heap()->promise_has_handler_symbol();
91 }
92
93
94 RUNTIME_FUNCTION(Runtime_StackGuard) {
95   SealHandleScope shs(isolate);
96   DCHECK(args.length() == 0);
97
98   // First check if this is a real stack overflow.
99   StackLimitCheck check(isolate);
100   if (check.JsHasOverflowed()) {
101     return isolate->StackOverflow();
102   }
103
104   return isolate->stack_guard()->HandleInterrupts();
105 }
106
107
108 RUNTIME_FUNCTION(Runtime_Interrupt) {
109   SealHandleScope shs(isolate);
110   DCHECK(args.length() == 0);
111   return isolate->stack_guard()->HandleInterrupts();
112 }
113
114
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);
123 }
124
125
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);
137 }
138
139
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);
148
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);
154   }
155   return isolate->heap()->undefined_value();
156 }
157
158
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();
165
166   Zone zone;
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();
172     }
173     CallPrinter printer(isolate, &zone);
174     const char* string = printer.Print(info.function(), location.start_pos());
175     return *isolate->factory()->NewStringFromAsciiChecked(string);
176   }
177
178   CompilationInfo info(location.script(), &zone);
179   if (!Parser::ParseStatic(&info)) {
180     isolate->clear_pending_exception();
181     return isolate->heap()->empty_string();
182   }
183   CallPrinter printer(isolate, &zone);
184   const char* string = printer.Print(info.function(), location.start_pos());
185   return *isolate->factory()->NewStringFromAsciiChecked(string);
186 }
187
188
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);
194
195   {
196     DisallowHeapAllocation no_alloc;
197
198     int finger_index = cache->finger_index();
199     Object* o = cache->get(finger_index);
200     if (o == key) {
201       // The fastest case: hit the same place again.
202       return cache->get(finger_index + 1);
203     }
204
205     for (int i = finger_index - 2; i >= JSFunctionResultCache::kEntriesIndex;
206          i -= 2) {
207       o = cache->get(i);
208       if (o == key) {
209         cache->set_finger_index(i);
210         return cache->get(i + 1);
211       }
212     }
213
214     int size = cache->size();
215     DCHECK(size <= cache->length());
216
217     for (int i = size - 2; i > finger_index; i -= 2) {
218       o = cache->get(i);
219       if (o == key) {
220         cache->set_finger_index(i);
221         return cache->get(i + 1);
222       }
223     }
224   }
225
226   // There is no value in the cache.  Invoke the function and cache result.
227   HandleScope scope(isolate);
228
229   Handle<JSFunctionResultCache> cache_handle(cache);
230   Handle<Object> key_handle(key, isolate);
231   Handle<Object> value;
232   {
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(
240         isolate, value,
241         Execution::Call(isolate, factory, receiver, arraysize(argv), argv));
242   }
243
244 #ifdef VERIFY_HEAP
245   if (FLAG_verify_heap) {
246     cache_handle->JSFunctionResultCacheVerify();
247   }
248 #endif
249
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();
253
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.
256   int index = -1;
257   if (size < cache_handle->length()) {
258     cache_handle->set_size(size + JSFunctionResultCache::kEntrySize);
259     index = size;
260   } else {
261     index = finger_index + JSFunctionResultCache::kEntrySize;
262     if (index == cache_handle->length()) {
263       index = JSFunctionResultCache::kEntriesIndex;
264     }
265   }
266
267   DCHECK(index % 2 == 0);
268   DCHECK(index >= JSFunctionResultCache::kEntriesIndex);
269   DCHECK(index < cache_handle->length());
270
271   cache_handle->set(index, *key_handle);
272   cache_handle->set(index + 1, *value);
273   cache_handle->set_finger_index(index);
274
275 #ifdef VERIFY_HEAP
276   if (FLAG_verify_heap) {
277     cache_handle->JSFunctionResultCacheVerify();
278   }
279 #endif
280
281   return *value;
282 }
283
284
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());
290 }
291
292
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();
298 }
299
300
301 RUNTIME_FUNCTION(Runtime_IS_VAR) {
302   UNREACHABLE();  // implemented as macro in the parser
303   return NULL;
304 }
305
306
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);
313 }
314 }
315 }  // namespace v8::internal