[builtins] Remove the weird STACK_OVERFLOW builtin.
[platform/upstream/v8.git] / 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/runtime/runtime-utils.h"
6
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"
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_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,
32                                 "ExportFromRuntime");
33   Bootstrapper::ExportFromRuntime(isolate, container);
34   JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime");
35   return *container;
36 }
37
38
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");
48   return *container;
49 }
50
51
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);
69     }
70     RUNTIME_ASSERT(index != Context::kNotFound);
71     native_context->set(index, *object);
72   }
73   return isolate->heap()->undefined_value();
74 }
75
76
77 RUNTIME_FUNCTION(Runtime_Throw) {
78   HandleScope scope(isolate);
79   DCHECK(args.length() == 1);
80   return isolate->Throw(args[0]);
81 }
82
83
84 RUNTIME_FUNCTION(Runtime_ReThrow) {
85   HandleScope scope(isolate);
86   DCHECK(args.length() == 1);
87   return isolate->ReThrow(args[0]);
88 }
89
90
91 RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
92   SealHandleScope shs(isolate);
93   DCHECK_EQ(0, args.length());
94   return isolate->StackOverflow();
95 }
96
97
98 RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
99   SealHandleScope shs(isolate);
100   DCHECK(args.length() == 0);
101   return isolate->UnwindAndFindHandler();
102 }
103
104
105 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
106   SealHandleScope shs(isolate);
107   DCHECK(args.length() == 0);
108   return isolate->PromoteScheduledException();
109 }
110
111
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));
118 }
119
120
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);
129 }
130
131
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);
140 }
141
142
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);
151 }
152
153
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(
159       isolate,
160       NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
161 }
162
163
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));
169 }
170
171
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);
184   }
185   return isolate->heap()->undefined_value();
186 }
187
188
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();
199 }
200
201
202 RUNTIME_FUNCTION(Runtime_StackGuard) {
203   SealHandleScope shs(isolate);
204   DCHECK(args.length() == 0);
205
206   // First check if this is a real stack overflow.
207   StackLimitCheck check(isolate);
208   if (check.JsHasOverflowed()) {
209     return isolate->StackOverflow();
210   }
211
212   return isolate->stack_guard()->HandleInterrupts();
213 }
214
215
216 RUNTIME_FUNCTION(Runtime_Interrupt) {
217   SealHandleScope shs(isolate);
218   DCHECK(args.length() == 0);
219   return isolate->stack_guard()->HandleInterrupts();
220 }
221
222
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);
231 }
232
233
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);
245 }
246
247
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);
256
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));
264   }
265   return isolate->heap()->undefined_value();
266 }
267
268
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());
274 }
275
276
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();
282 }
283
284
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(
291       isolate, result,
292       isolate->error_tostring_helper()->Stringify(isolate, error));
293   return *result;
294 }
295
296
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(
306       isolate, result,
307       MessageTemplate::FormatMessage(template_index, arg0, arg1, arg2));
308   return *result;
309 }
310
311
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);               \
320   }
321
322 static inline Object* ReturnDereferencedHandle(Handle<Object> obj,
323                                                Isolate* isolate) {
324   return *obj;
325 }
326
327
328 static inline Object* ReturnPositiveSmiOrNull(int value, Isolate* isolate) {
329   if (value >= 0) return Smi::FromInt(value);
330   return isolate->heap()->null_value();
331 }
332
333
334 static inline Object* ReturnBoolean(bool value, Isolate* isolate) {
335   return isolate->heap()->ToBoolean(value);
336 }
337
338
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)
349
350 #undef CALLSITE_GET
351
352
353 RUNTIME_FUNCTION(Runtime_IS_VAR) {
354   UNREACHABLE();  // implemented as macro in the parser
355   return NULL;
356 }
357
358
359 RUNTIME_FUNCTION(Runtime_IncrementStatsCounter) {
360   SealHandleScope shs(isolate);
361   DCHECK(args.length() == 1);
362   CONVERT_ARG_CHECKED(String, name, 0);
363
364   if (FLAG_native_code_counters) {
365     StatsCounter(isolate, name->ToCString().get()).Increment();
366   }
367   return isolate->heap()->undefined_value();
368 }
369
370
371 RUNTIME_FUNCTION(Runtime_Likely) {
372   DCHECK(args.length() == 1);
373   return args[0];
374 }
375
376
377 RUNTIME_FUNCTION(Runtime_Unlikely) {
378   DCHECK(args.length() == 1);
379   return args[0];
380 }
381
382
383 RUNTIME_FUNCTION(Runtime_HarmonyToString) {
384   // TODO(caitp): Delete this runtime method when removing --harmony-tostring
385   return isolate->heap()->ToBoolean(FLAG_harmony_tostring);
386 }
387
388
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();
394 }
395
396
397 RUNTIME_FUNCTION(Runtime_GetCallerJSFunction) {
398   SealHandleScope shs(isolate);
399   StackFrameIterator it(isolate);
400   RUNTIME_ASSERT(it.frame()->type() == StackFrame::STUB);
401   it.Advance();
402   RUNTIME_ASSERT(it.frame()->type() == StackFrame::JAVA_SCRIPT);
403   return JavaScriptFrame::cast(it.frame())->function();
404 }
405
406
407 RUNTIME_FUNCTION(Runtime_GetCodeStubExportsObject) {
408   HandleScope shs(isolate);
409   return isolate->heap()->code_stub_exports_object();
410 }
411
412 }  // namespace internal
413 }  // namespace v8