• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/ast/prettyprinter.h"
9 #include "src/bootstrapper.h"
10 #include "src/conversions.h"
11 #include "src/debug/debug.h"
12 #include "src/frames-inl.h"
13 #include "src/isolate-inl.h"
14 #include "src/messages.h"
15 #include "src/parsing/parser.h"
16 #include "src/wasm/wasm-module.h"
17 
18 namespace v8 {
19 namespace internal {
20 
RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping)21 RUNTIME_FUNCTION(Runtime_CheckIsBootstrapping) {
22   SealHandleScope shs(isolate);
23   DCHECK(args.length() == 0);
24   CHECK(isolate->bootstrapper()->IsActive());
25   return isolate->heap()->undefined_value();
26 }
27 
28 
RUNTIME_FUNCTION(Runtime_ExportFromRuntime)29 RUNTIME_FUNCTION(Runtime_ExportFromRuntime) {
30   HandleScope scope(isolate);
31   DCHECK(args.length() == 1);
32   CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
33   CHECK(isolate->bootstrapper()->IsActive());
34   JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
35                                 "ExportFromRuntime");
36   Bootstrapper::ExportFromRuntime(isolate, container);
37   JSObject::MigrateSlowToFast(container, 0, "ExportFromRuntime");
38   return *container;
39 }
40 
41 
RUNTIME_FUNCTION(Runtime_ExportExperimentalFromRuntime)42 RUNTIME_FUNCTION(Runtime_ExportExperimentalFromRuntime) {
43   HandleScope scope(isolate);
44   DCHECK(args.length() == 1);
45   CONVERT_ARG_HANDLE_CHECKED(JSObject, container, 0);
46   CHECK(isolate->bootstrapper()->IsActive());
47   JSObject::NormalizeProperties(container, KEEP_INOBJECT_PROPERTIES, 10,
48                                 "ExportExperimentalFromRuntime");
49   Bootstrapper::ExportExperimentalFromRuntime(isolate, container);
50   JSObject::MigrateSlowToFast(container, 0, "ExportExperimentalFromRuntime");
51   return *container;
52 }
53 
54 
RUNTIME_FUNCTION(Runtime_InstallToContext)55 RUNTIME_FUNCTION(Runtime_InstallToContext) {
56   HandleScope scope(isolate);
57   DCHECK(args.length() == 1);
58   CONVERT_ARG_HANDLE_CHECKED(JSArray, array, 0);
59   CHECK(array->HasFastElements());
60   CHECK(isolate->bootstrapper()->IsActive());
61   Handle<Context> native_context = isolate->native_context();
62   Handle<FixedArray> fixed_array(FixedArray::cast(array->elements()));
63   int length = Smi::cast(array->length())->value();
64   for (int i = 0; i < length; i += 2) {
65     CHECK(fixed_array->get(i)->IsString());
66     Handle<String> name(String::cast(fixed_array->get(i)));
67     CHECK(fixed_array->get(i + 1)->IsJSObject());
68     Handle<JSObject> object(JSObject::cast(fixed_array->get(i + 1)));
69     int index = Context::ImportedFieldIndexForName(name);
70     if (index == Context::kNotFound) {
71       index = Context::IntrinsicIndexForName(name);
72     }
73     CHECK(index != Context::kNotFound);
74     native_context->set(index, *object);
75   }
76   return isolate->heap()->undefined_value();
77 }
78 
79 
RUNTIME_FUNCTION(Runtime_Throw)80 RUNTIME_FUNCTION(Runtime_Throw) {
81   HandleScope scope(isolate);
82   DCHECK(args.length() == 1);
83   return isolate->Throw(args[0]);
84 }
85 
86 
RUNTIME_FUNCTION(Runtime_ReThrow)87 RUNTIME_FUNCTION(Runtime_ReThrow) {
88   HandleScope scope(isolate);
89   DCHECK(args.length() == 1);
90   return isolate->ReThrow(args[0]);
91 }
92 
93 
RUNTIME_FUNCTION(Runtime_ThrowStackOverflow)94 RUNTIME_FUNCTION(Runtime_ThrowStackOverflow) {
95   SealHandleScope shs(isolate);
96   DCHECK_LE(0, args.length());
97   return isolate->StackOverflow();
98 }
99 
RUNTIME_FUNCTION(Runtime_ThrowWasmError)100 RUNTIME_FUNCTION(Runtime_ThrowWasmError) {
101   HandleScope scope(isolate);
102   DCHECK_EQ(2, args.length());
103   CONVERT_SMI_ARG_CHECKED(message_id, 0);
104   CONVERT_SMI_ARG_CHECKED(byte_offset, 1);
105   Handle<Object> error_obj = isolate->factory()->NewError(
106       static_cast<MessageTemplate::Template>(message_id));
107 
108   // For wasm traps, the byte offset (a.k.a source position) can not be
109   // determined from relocation info, since the explicit checks for traps
110   // converge in one singe block which calls this runtime function.
111   // We hence pass the byte offset explicitely, and patch it into the top-most
112   // frame (a wasm frame) on the collected stack trace.
113   // TODO(wasm): This implementation is temporary, see bug #5007:
114   // https://bugs.chromium.org/p/v8/issues/detail?id=5007
115   Handle<JSObject> error = Handle<JSObject>::cast(error_obj);
116   Handle<Object> stack_trace_obj = JSReceiver::GetDataProperty(
117       error, isolate->factory()->stack_trace_symbol());
118   // Patch the stack trace (array of <receiver, function, code, position>).
119   if (stack_trace_obj->IsJSArray()) {
120     Handle<FixedArray> stack_elements(
121         FixedArray::cast(JSArray::cast(*stack_trace_obj)->elements()));
122     DCHECK_EQ(1, stack_elements->length() % 4);
123     DCHECK(Code::cast(stack_elements->get(3))->kind() == Code::WASM_FUNCTION);
124     DCHECK(stack_elements->get(4)->IsSmi() &&
125            Smi::cast(stack_elements->get(4))->value() >= 0);
126     stack_elements->set(4, Smi::FromInt(-1 - byte_offset));
127   }
128   Handle<Object> detailed_stack_trace_obj = JSReceiver::GetDataProperty(
129       error, isolate->factory()->detailed_stack_trace_symbol());
130   // Patch the detailed stack trace (array of JSObjects with various
131   // properties).
132   if (detailed_stack_trace_obj->IsJSArray()) {
133     Handle<FixedArray> stack_elements(
134         FixedArray::cast(JSArray::cast(*detailed_stack_trace_obj)->elements()));
135     DCHECK_GE(stack_elements->length(), 1);
136     Handle<JSObject> top_frame(JSObject::cast(stack_elements->get(0)));
137     Handle<String> wasm_offset_key =
138         isolate->factory()->InternalizeOneByteString(
139             STATIC_CHAR_VECTOR("column"));
140     LookupIterator it(top_frame, wasm_offset_key, top_frame,
141                       LookupIterator::PROTOTYPE_CHAIN_SKIP_INTERCEPTOR);
142     if (it.IsFound()) {
143       DCHECK(JSReceiver::GetDataProperty(&it)->IsSmi());
144       // Make column number 1-based here.
145       Maybe<bool> data_set = JSReceiver::SetDataProperty(
146           &it, handle(Smi::FromInt(byte_offset + 1), isolate));
147       DCHECK(data_set.IsJust() && data_set.FromJust() == true);
148       USE(data_set);
149     }
150   }
151 
152   return isolate->Throw(*error_obj);
153 }
154 
RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler)155 RUNTIME_FUNCTION(Runtime_UnwindAndFindExceptionHandler) {
156   SealHandleScope shs(isolate);
157   DCHECK(args.length() == 0);
158   return isolate->UnwindAndFindHandler();
159 }
160 
161 
RUNTIME_FUNCTION(Runtime_PromoteScheduledException)162 RUNTIME_FUNCTION(Runtime_PromoteScheduledException) {
163   SealHandleScope shs(isolate);
164   DCHECK(args.length() == 0);
165   return isolate->PromoteScheduledException();
166 }
167 
168 
RUNTIME_FUNCTION(Runtime_ThrowReferenceError)169 RUNTIME_FUNCTION(Runtime_ThrowReferenceError) {
170   HandleScope scope(isolate);
171   DCHECK(args.length() == 1);
172   CONVERT_ARG_HANDLE_CHECKED(Object, name, 0);
173   THROW_NEW_ERROR_RETURN_FAILURE(
174       isolate, NewReferenceError(MessageTemplate::kNotDefined, name));
175 }
176 
177 
RUNTIME_FUNCTION(Runtime_NewTypeError)178 RUNTIME_FUNCTION(Runtime_NewTypeError) {
179   HandleScope scope(isolate);
180   DCHECK(args.length() == 2);
181   CONVERT_INT32_ARG_CHECKED(template_index, 0);
182   CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
183   auto message_template =
184       static_cast<MessageTemplate::Template>(template_index);
185   return *isolate->factory()->NewTypeError(message_template, arg0);
186 }
187 
188 
RUNTIME_FUNCTION(Runtime_NewReferenceError)189 RUNTIME_FUNCTION(Runtime_NewReferenceError) {
190   HandleScope scope(isolate);
191   DCHECK(args.length() == 2);
192   CONVERT_INT32_ARG_CHECKED(template_index, 0);
193   CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
194   auto message_template =
195       static_cast<MessageTemplate::Template>(template_index);
196   return *isolate->factory()->NewReferenceError(message_template, arg0);
197 }
198 
199 
RUNTIME_FUNCTION(Runtime_NewSyntaxError)200 RUNTIME_FUNCTION(Runtime_NewSyntaxError) {
201   HandleScope scope(isolate);
202   DCHECK(args.length() == 2);
203   CONVERT_INT32_ARG_CHECKED(template_index, 0);
204   CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 1);
205   auto message_template =
206       static_cast<MessageTemplate::Template>(template_index);
207   return *isolate->factory()->NewSyntaxError(message_template, arg0);
208 }
209 
210 
RUNTIME_FUNCTION(Runtime_ThrowIllegalInvocation)211 RUNTIME_FUNCTION(Runtime_ThrowIllegalInvocation) {
212   HandleScope scope(isolate);
213   DCHECK(args.length() == 0);
214   THROW_NEW_ERROR_RETURN_FAILURE(
215       isolate, NewTypeError(MessageTemplate::kIllegalInvocation));
216 }
217 
RUNTIME_FUNCTION(Runtime_ThrowIncompatibleMethodReceiver)218 RUNTIME_FUNCTION(Runtime_ThrowIncompatibleMethodReceiver) {
219   HandleScope scope(isolate);
220   DCHECK_EQ(2, args.length());
221   CONVERT_ARG_HANDLE_CHECKED(Object, arg0, 0);
222   CONVERT_ARG_HANDLE_CHECKED(Object, arg1, 1);
223   THROW_NEW_ERROR_RETURN_FAILURE(
224       isolate,
225       NewTypeError(MessageTemplate::kIncompatibleMethodReceiver, arg0, arg1));
226 }
227 
RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject)228 RUNTIME_FUNCTION(Runtime_ThrowIteratorResultNotAnObject) {
229   HandleScope scope(isolate);
230   DCHECK(args.length() == 1);
231   CONVERT_ARG_HANDLE_CHECKED(Object, value, 0);
232   THROW_NEW_ERROR_RETURN_FAILURE(
233       isolate,
234       NewTypeError(MessageTemplate::kIteratorResultNotAnObject, value));
235 }
236 
RUNTIME_FUNCTION(Runtime_ThrowGeneratorRunning)237 RUNTIME_FUNCTION(Runtime_ThrowGeneratorRunning) {
238   HandleScope scope(isolate);
239   DCHECK_EQ(0, args.length());
240   THROW_NEW_ERROR_RETURN_FAILURE(
241       isolate, NewTypeError(MessageTemplate::kGeneratorRunning));
242 }
243 
RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction)244 RUNTIME_FUNCTION(Runtime_ThrowApplyNonFunction) {
245   HandleScope scope(isolate);
246   DCHECK_EQ(1, args.length());
247   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
248   Handle<String> type = Object::TypeOf(isolate, object);
249   THROW_NEW_ERROR_RETURN_FAILURE(
250       isolate, NewTypeError(MessageTemplate::kApplyNonFunction, object, type));
251 }
252 
253 
RUNTIME_FUNCTION(Runtime_PromiseRejectEvent)254 RUNTIME_FUNCTION(Runtime_PromiseRejectEvent) {
255   DCHECK(args.length() == 3);
256   HandleScope scope(isolate);
257   CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
258   CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
259   CONVERT_BOOLEAN_ARG_CHECKED(debug_event, 2);
260   if (debug_event) isolate->debug()->OnPromiseReject(promise, value);
261   Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
262   // Do not report if we actually have a handler.
263   if (JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate)) {
264     isolate->ReportPromiseReject(promise, value,
265                                  v8::kPromiseRejectWithNoHandler);
266   }
267   return isolate->heap()->undefined_value();
268 }
269 
270 
RUNTIME_FUNCTION(Runtime_PromiseRevokeReject)271 RUNTIME_FUNCTION(Runtime_PromiseRevokeReject) {
272   DCHECK(args.length() == 1);
273   HandleScope scope(isolate);
274   CONVERT_ARG_HANDLE_CHECKED(JSObject, promise, 0);
275   Handle<Symbol> key = isolate->factory()->promise_has_handler_symbol();
276   // At this point, no revocation has been issued before
277   CHECK(JSReceiver::GetDataProperty(promise, key)->IsUndefined(isolate));
278   isolate->ReportPromiseReject(promise, Handle<Object>(),
279                                v8::kPromiseHandlerAddedAfterReject);
280   return isolate->heap()->undefined_value();
281 }
282 
283 
RUNTIME_FUNCTION(Runtime_StackGuard)284 RUNTIME_FUNCTION(Runtime_StackGuard) {
285   SealHandleScope shs(isolate);
286   DCHECK(args.length() == 0);
287 
288   // First check if this is a real stack overflow.
289   StackLimitCheck check(isolate);
290   if (check.JsHasOverflowed()) {
291     return isolate->StackOverflow();
292   }
293 
294   return isolate->stack_guard()->HandleInterrupts();
295 }
296 
297 
RUNTIME_FUNCTION(Runtime_Interrupt)298 RUNTIME_FUNCTION(Runtime_Interrupt) {
299   SealHandleScope shs(isolate);
300   DCHECK(args.length() == 0);
301   return isolate->stack_guard()->HandleInterrupts();
302 }
303 
304 
RUNTIME_FUNCTION(Runtime_AllocateInNewSpace)305 RUNTIME_FUNCTION(Runtime_AllocateInNewSpace) {
306   HandleScope scope(isolate);
307   DCHECK(args.length() == 1);
308   CONVERT_SMI_ARG_CHECKED(size, 0);
309   CHECK(IsAligned(size, kPointerSize));
310   CHECK(size > 0);
311   CHECK(size <= Page::kMaxRegularHeapObjectSize);
312   return *isolate->factory()->NewFillerObject(size, false, NEW_SPACE);
313 }
314 
315 
RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace)316 RUNTIME_FUNCTION(Runtime_AllocateInTargetSpace) {
317   HandleScope scope(isolate);
318   DCHECK(args.length() == 2);
319   CONVERT_SMI_ARG_CHECKED(size, 0);
320   CONVERT_SMI_ARG_CHECKED(flags, 1);
321   CHECK(IsAligned(size, kPointerSize));
322   CHECK(size > 0);
323   CHECK(size <= Page::kMaxRegularHeapObjectSize);
324   bool double_align = AllocateDoubleAlignFlag::decode(flags);
325   AllocationSpace space = AllocateTargetSpace::decode(flags);
326   return *isolate->factory()->NewFillerObject(size, double_align, space);
327 }
328 
RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString)329 RUNTIME_FUNCTION(Runtime_AllocateSeqOneByteString) {
330   HandleScope scope(isolate);
331   DCHECK_EQ(1, args.length());
332   CONVERT_SMI_ARG_CHECKED(length, 0);
333   Handle<SeqOneByteString> result;
334   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
335       isolate, result, isolate->factory()->NewRawOneByteString(length));
336   return *result;
337 }
338 
RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString)339 RUNTIME_FUNCTION(Runtime_AllocateSeqTwoByteString) {
340   HandleScope scope(isolate);
341   DCHECK_EQ(1, args.length());
342   CONVERT_SMI_ARG_CHECKED(length, 0);
343   Handle<SeqTwoByteString> result;
344   ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
345       isolate, result, isolate->factory()->NewRawTwoByteString(length));
346   return *result;
347 }
348 
349 // Collect the raw data for a stack trace.  Returns an array of 4
350 // element segments each containing a receiver, function, code and
351 // native code offset.
RUNTIME_FUNCTION(Runtime_CollectStackTrace)352 RUNTIME_FUNCTION(Runtime_CollectStackTrace) {
353   HandleScope scope(isolate);
354   DCHECK(args.length() == 2);
355   CONVERT_ARG_HANDLE_CHECKED(JSReceiver, error_object, 0);
356   CONVERT_ARG_HANDLE_CHECKED(Object, caller, 1);
357 
358   if (!isolate->bootstrapper()->IsActive()) {
359     // Optionally capture a more detailed stack trace for the message.
360     RETURN_FAILURE_ON_EXCEPTION(
361         isolate, isolate->CaptureAndSetDetailedStackTrace(error_object));
362     // Capture a simple stack trace for the stack property.
363     RETURN_FAILURE_ON_EXCEPTION(
364         isolate, isolate->CaptureAndSetSimpleStackTrace(error_object, caller));
365   }
366   return isolate->heap()->undefined_value();
367 }
368 
369 
RUNTIME_FUNCTION(Runtime_MessageGetStartPosition)370 RUNTIME_FUNCTION(Runtime_MessageGetStartPosition) {
371   SealHandleScope shs(isolate);
372   DCHECK(args.length() == 1);
373   CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
374   return Smi::FromInt(message->start_position());
375 }
376 
377 
RUNTIME_FUNCTION(Runtime_MessageGetScript)378 RUNTIME_FUNCTION(Runtime_MessageGetScript) {
379   SealHandleScope shs(isolate);
380   DCHECK(args.length() == 1);
381   CONVERT_ARG_CHECKED(JSMessageObject, message, 0);
382   return message->script();
383 }
384 
385 
RUNTIME_FUNCTION(Runtime_FormatMessageString)386 RUNTIME_FUNCTION(Runtime_FormatMessageString) {
387   HandleScope scope(isolate);
388   DCHECK(args.length() == 4);
389   CONVERT_INT32_ARG_CHECKED(template_index, 0);
390   CONVERT_ARG_HANDLE_CHECKED(String, arg0, 1);
391   CONVERT_ARG_HANDLE_CHECKED(String, arg1, 2);
392   CONVERT_ARG_HANDLE_CHECKED(String, arg2, 3);
393   isolate->native_context()->IncrementErrorsThrown();
394   RETURN_RESULT_OR_FAILURE(isolate, MessageTemplate::FormatMessage(
395                                         template_index, arg0, arg1, arg2));
396 }
397 
398 #define CALLSITE_GET(NAME, RETURN)                          \
399   RUNTIME_FUNCTION(Runtime_CallSite##NAME##RT) {            \
400     HandleScope scope(isolate);                             \
401     DCHECK(args.length() == 1);                             \
402     CONVERT_ARG_HANDLE_CHECKED(JSObject, call_site_obj, 0); \
403     Handle<String> result;                                  \
404     CallSite call_site(isolate, call_site_obj);             \
405     CHECK(call_site.IsJavaScript() || call_site.IsWasm());  \
406     return RETURN(call_site.NAME(), isolate);               \
407   }
408 
ReturnDereferencedHandle(Handle<Object> obj,Isolate * isolate)409 static inline Object* ReturnDereferencedHandle(Handle<Object> obj,
410                                                Isolate* isolate) {
411   return *obj;
412 }
413 
414 
ReturnPositiveNumberOrNull(int value,Isolate * isolate)415 static inline Object* ReturnPositiveNumberOrNull(int value, Isolate* isolate) {
416   if (value >= 0) return *isolate->factory()->NewNumberFromInt(value);
417   return isolate->heap()->null_value();
418 }
419 
420 
ReturnBoolean(bool value,Isolate * isolate)421 static inline Object* ReturnBoolean(bool value, Isolate* isolate) {
422   return isolate->heap()->ToBoolean(value);
423 }
424 
425 
CALLSITE_GET(GetFileName,ReturnDereferencedHandle)426 CALLSITE_GET(GetFileName, ReturnDereferencedHandle)
427 CALLSITE_GET(GetFunctionName, ReturnDereferencedHandle)
428 CALLSITE_GET(GetScriptNameOrSourceUrl, ReturnDereferencedHandle)
429 CALLSITE_GET(GetMethodName, ReturnDereferencedHandle)
430 CALLSITE_GET(GetLineNumber, ReturnPositiveNumberOrNull)
431 CALLSITE_GET(GetColumnNumber, ReturnPositiveNumberOrNull)
432 CALLSITE_GET(IsNative, ReturnBoolean)
433 CALLSITE_GET(IsToplevel, ReturnBoolean)
434 CALLSITE_GET(IsEval, ReturnBoolean)
435 CALLSITE_GET(IsConstructor, ReturnBoolean)
436 
437 #undef CALLSITE_GET
438 
439 
440 RUNTIME_FUNCTION(Runtime_IS_VAR) {
441   UNREACHABLE();  // implemented as macro in the parser
442   return NULL;
443 }
444 
445 
446 namespace {
447 
ComputeLocation(Isolate * isolate,MessageLocation * target)448 bool ComputeLocation(Isolate* isolate, MessageLocation* target) {
449   JavaScriptFrameIterator it(isolate);
450   if (!it.done()) {
451     JavaScriptFrame* frame = it.frame();
452     JSFunction* fun = frame->function();
453     Object* script = fun->shared()->script();
454     if (script->IsScript() &&
455         !(Script::cast(script)->source()->IsUndefined(isolate))) {
456       Handle<Script> casted_script(Script::cast(script), isolate);
457       // Compute the location from the function and the relocation info of the
458       // baseline code. For optimized code this will use the deoptimization
459       // information to get canonical location information.
460       List<FrameSummary> frames(FLAG_max_inlining_levels + 1);
461       it.frame()->Summarize(&frames);
462       FrameSummary& summary = frames.last();
463       int pos = summary.abstract_code()->SourcePosition(summary.code_offset());
464       *target = MessageLocation(casted_script, pos, pos + 1, handle(fun));
465       return true;
466     }
467   }
468   return false;
469 }
470 
471 
RenderCallSite(Isolate * isolate,Handle<Object> object)472 Handle<String> RenderCallSite(Isolate* isolate, Handle<Object> object) {
473   MessageLocation location;
474   if (ComputeLocation(isolate, &location)) {
475     Zone zone(isolate->allocator());
476     base::SmartPointer<ParseInfo> info(
477         location.function()->shared()->is_function()
478             ? new ParseInfo(&zone, location.function())
479             : new ParseInfo(&zone, location.script()));
480     if (Parser::ParseStatic(info.get())) {
481       CallPrinter printer(isolate, location.function()->shared()->IsBuiltin());
482       const char* string = printer.Print(info->literal(), location.start_pos());
483       if (strlen(string) > 0) {
484         return isolate->factory()->NewStringFromAsciiChecked(string);
485       }
486     } else {
487       isolate->clear_pending_exception();
488     }
489   }
490   return Object::TypeOf(isolate, object);
491 }
492 
493 }  // namespace
494 
495 
RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable)496 RUNTIME_FUNCTION(Runtime_ThrowCalledNonCallable) {
497   HandleScope scope(isolate);
498   DCHECK_EQ(1, args.length());
499   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
500   Handle<String> callsite = RenderCallSite(isolate, object);
501   THROW_NEW_ERROR_RETURN_FAILURE(
502       isolate, NewTypeError(MessageTemplate::kCalledNonCallable, callsite));
503 }
504 
RUNTIME_FUNCTION(Runtime_ThrowCalledOnNullOrUndefined)505 RUNTIME_FUNCTION(Runtime_ThrowCalledOnNullOrUndefined) {
506   HandleScope scope(isolate);
507   DCHECK_EQ(1, args.length());
508   CONVERT_ARG_HANDLE_CHECKED(String, name, 0);
509   THROW_NEW_ERROR_RETURN_FAILURE(
510       isolate, NewTypeError(MessageTemplate::kCalledOnNullOrUndefined, name));
511 }
512 
RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable)513 RUNTIME_FUNCTION(Runtime_ThrowConstructedNonConstructable) {
514   HandleScope scope(isolate);
515   DCHECK_EQ(1, args.length());
516   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
517   Handle<String> callsite = RenderCallSite(isolate, object);
518   THROW_NEW_ERROR_RETURN_FAILURE(
519       isolate, NewTypeError(MessageTemplate::kNotConstructor, callsite));
520 }
521 
522 
RUNTIME_FUNCTION(Runtime_ThrowDerivedConstructorReturnedNonObject)523 RUNTIME_FUNCTION(Runtime_ThrowDerivedConstructorReturnedNonObject) {
524   HandleScope scope(isolate);
525   DCHECK_EQ(0, args.length());
526   THROW_NEW_ERROR_RETURN_FAILURE(
527       isolate, NewTypeError(MessageTemplate::kDerivedConstructorReturn));
528 }
529 
530 
531 // ES6 section 7.3.17 CreateListFromArrayLike (obj)
RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike)532 RUNTIME_FUNCTION(Runtime_CreateListFromArrayLike) {
533   HandleScope scope(isolate);
534   DCHECK_EQ(1, args.length());
535   CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);
536   RETURN_RESULT_OR_FAILURE(isolate, Object::CreateListFromArrayLike(
537                                         isolate, object, ElementTypes::kAll));
538 }
539 
540 
RUNTIME_FUNCTION(Runtime_IncrementUseCounter)541 RUNTIME_FUNCTION(Runtime_IncrementUseCounter) {
542   HandleScope scope(isolate);
543   DCHECK_EQ(1, args.length());
544   CONVERT_SMI_ARG_CHECKED(counter, 0);
545   isolate->CountUsage(static_cast<v8::Isolate::UseCounterFeature>(counter));
546   return isolate->heap()->undefined_value();
547 }
548 
RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats)549 RUNTIME_FUNCTION(Runtime_GetAndResetRuntimeCallStats) {
550   HandleScope scope(isolate);
551   if (args.length() == 0) {
552     // Without arguments, the result is returned as a string.
553     DCHECK_EQ(0, args.length());
554     std::stringstream stats_stream;
555     isolate->counters()->runtime_call_stats()->Print(stats_stream);
556     Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(
557         stats_stream.str().c_str());
558     isolate->counters()->runtime_call_stats()->Reset();
559     return *result;
560   } else {
561     DCHECK_LE(args.length(), 2);
562     std::FILE* f;
563     if (args[0]->IsString()) {
564       // With a string argument, the results are appended to that file.
565       CONVERT_ARG_HANDLE_CHECKED(String, arg0, 0);
566       String::FlatContent flat = arg0->GetFlatContent();
567       const char* filename =
568           reinterpret_cast<const char*>(&(flat.ToOneByteVector()[0]));
569       f = std::fopen(filename, "a");
570       DCHECK_NOT_NULL(f);
571     } else {
572       // With an integer argument, the results are written to stdout/stderr.
573       CONVERT_SMI_ARG_CHECKED(fd, 0);
574       DCHECK(fd == 1 || fd == 2);
575       f = fd == 1 ? stdout : stderr;
576     }
577     // The second argument (if any) is a message header to be printed.
578     if (args.length() >= 2) {
579       CONVERT_ARG_HANDLE_CHECKED(String, arg1, 1);
580       arg1->PrintOn(f);
581       std::fputc('\n', f);
582       std::fflush(f);
583     }
584     OFStream stats_stream(f);
585     isolate->counters()->runtime_call_stats()->Print(stats_stream);
586     isolate->counters()->runtime_call_stats()->Reset();
587     if (args[0]->IsString())
588       std::fclose(f);
589     else
590       std::fflush(f);
591     return isolate->heap()->undefined_value();
592   }
593 }
594 
RUNTIME_FUNCTION(Runtime_EnqueueMicrotask)595 RUNTIME_FUNCTION(Runtime_EnqueueMicrotask) {
596   HandleScope scope(isolate);
597   DCHECK(args.length() == 1);
598   CONVERT_ARG_HANDLE_CHECKED(JSFunction, microtask, 0);
599   isolate->EnqueueMicrotask(microtask);
600   return isolate->heap()->undefined_value();
601 }
602 
RUNTIME_FUNCTION(Runtime_RunMicrotasks)603 RUNTIME_FUNCTION(Runtime_RunMicrotasks) {
604   HandleScope scope(isolate);
605   DCHECK(args.length() == 0);
606   isolate->RunMicrotasks();
607   return isolate->heap()->undefined_value();
608 }
609 
RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance)610 RUNTIME_FUNCTION(Runtime_OrdinaryHasInstance) {
611   HandleScope scope(isolate);
612   DCHECK_EQ(2, args.length());
613   CONVERT_ARG_HANDLE_CHECKED(Object, callable, 0);
614   CONVERT_ARG_HANDLE_CHECKED(Object, object, 1);
615   RETURN_RESULT_OR_FAILURE(
616       isolate, Object::OrdinaryHasInstance(isolate, callable, object));
617 }
618 
RUNTIME_FUNCTION(Runtime_IsWasmObject)619 RUNTIME_FUNCTION(Runtime_IsWasmObject) {
620   HandleScope scope(isolate);
621   DCHECK_EQ(1, args.length());
622   CONVERT_ARG_CHECKED(Object, object, 0);
623   bool is_wasm_object =
624       object->IsJSObject() && wasm::IsWasmObject(JSObject::cast(object));
625   return *isolate->factory()->ToBoolean(is_wasm_object);
626 }
627 
628 }  // namespace internal
629 }  // namespace v8
630