11cb0ef41Sopenharmony_ci// Copyright 2014 the V8 project authors. All rights reserved.
21cb0ef41Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be
31cb0ef41Sopenharmony_ci// found in the LICENSE file.
41cb0ef41Sopenharmony_ci
51cb0ef41Sopenharmony_ci#include "src/builtins/accessors.h"
61cb0ef41Sopenharmony_ci#include "src/codegen/compiler.h"
71cb0ef41Sopenharmony_ci#include "src/execution/arguments-inl.h"
81cb0ef41Sopenharmony_ci#include "src/execution/isolate-inl.h"
91cb0ef41Sopenharmony_ci#include "src/heap/heap-inl.h"  // For ToBoolean. TODO(jkummerow): Drop.
101cb0ef41Sopenharmony_ci#include "src/logging/counters.h"
111cb0ef41Sopenharmony_ci#include "src/runtime/runtime-utils.h"
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_cinamespace v8 {
141cb0ef41Sopenharmony_cinamespace internal {
151cb0ef41Sopenharmony_ci
161cb0ef41Sopenharmony_ci// TODO(5530): Remove once uses in debug.js are gone.
171cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_FunctionGetScriptSource) {
181cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
191cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
201cb0ef41Sopenharmony_ci  Handle<JSReceiver> function = args.at<JSReceiver>(0);
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci  if (function->IsJSFunction()) {
231cb0ef41Sopenharmony_ci    Handle<Object> script(Handle<JSFunction>::cast(function)->shared().script(),
241cb0ef41Sopenharmony_ci                          isolate);
251cb0ef41Sopenharmony_ci    if (script->IsScript()) return Handle<Script>::cast(script)->source();
261cb0ef41Sopenharmony_ci  }
271cb0ef41Sopenharmony_ci  return ReadOnlyRoots(isolate).undefined_value();
281cb0ef41Sopenharmony_ci}
291cb0ef41Sopenharmony_ci
301cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_FunctionGetScriptId) {
311cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
321cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
331cb0ef41Sopenharmony_ci  Handle<JSReceiver> function = args.at<JSReceiver>(0);
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci  if (function->IsJSFunction()) {
361cb0ef41Sopenharmony_ci    Handle<Object> script(Handle<JSFunction>::cast(function)->shared().script(),
371cb0ef41Sopenharmony_ci                          isolate);
381cb0ef41Sopenharmony_ci    if (script->IsScript()) {
391cb0ef41Sopenharmony_ci      return Smi::FromInt(Handle<Script>::cast(script)->id());
401cb0ef41Sopenharmony_ci    }
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  return Smi::FromInt(-1);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
461cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
471cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
481cb0ef41Sopenharmony_ci  Handle<JSReceiver> function = args.at<JSReceiver>(0);
491cb0ef41Sopenharmony_ci  if (function->IsJSFunction()) {
501cb0ef41Sopenharmony_ci    Handle<SharedFunctionInfo> shared(
511cb0ef41Sopenharmony_ci        Handle<JSFunction>::cast(function)->shared(), isolate);
521cb0ef41Sopenharmony_ci    return *SharedFunctionInfo::GetSourceCode(shared);
531cb0ef41Sopenharmony_ci  }
541cb0ef41Sopenharmony_ci  return ReadOnlyRoots(isolate).undefined_value();
551cb0ef41Sopenharmony_ci}
561cb0ef41Sopenharmony_ci
571cb0ef41Sopenharmony_ci
581cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
591cb0ef41Sopenharmony_ci  SealHandleScope shs(isolate);
601cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
611cb0ef41Sopenharmony_ci
621cb0ef41Sopenharmony_ci  auto fun = JSFunction::cast(args[0]);
631cb0ef41Sopenharmony_ci  int pos = fun.shared().StartPosition();
641cb0ef41Sopenharmony_ci  return Smi::FromInt(pos);
651cb0ef41Sopenharmony_ci}
661cb0ef41Sopenharmony_ci
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
691cb0ef41Sopenharmony_ci  SealHandleScope shs(isolate);
701cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
711cb0ef41Sopenharmony_ci
721cb0ef41Sopenharmony_ci  auto f = JSFunction::cast(args[0]);
731cb0ef41Sopenharmony_ci  return isolate->heap()->ToBoolean(f.shared().IsApiFunction());
741cb0ef41Sopenharmony_ci}
751cb0ef41Sopenharmony_ci
761cb0ef41Sopenharmony_ci
771cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_Call) {
781cb0ef41Sopenharmony_ci  HandleScope scope(isolate);
791cb0ef41Sopenharmony_ci  DCHECK_LE(2, args.length());
801cb0ef41Sopenharmony_ci  int const argc = args.length() - 2;
811cb0ef41Sopenharmony_ci  Handle<Object> target = args.at(0);
821cb0ef41Sopenharmony_ci  Handle<Object> receiver = args.at(1);
831cb0ef41Sopenharmony_ci  base::ScopedVector<Handle<Object>> argv(argc);
841cb0ef41Sopenharmony_ci  for (int i = 0; i < argc; ++i) {
851cb0ef41Sopenharmony_ci    argv[i] = args.at(2 + i);
861cb0ef41Sopenharmony_ci  }
871cb0ef41Sopenharmony_ci  RETURN_RESULT_OR_FAILURE(
881cb0ef41Sopenharmony_ci      isolate, Execution::Call(isolate, target, receiver, argc, argv.begin()));
891cb0ef41Sopenharmony_ci}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci
921cb0ef41Sopenharmony_ciRUNTIME_FUNCTION(Runtime_IsFunction) {
931cb0ef41Sopenharmony_ci  SealHandleScope shs(isolate);
941cb0ef41Sopenharmony_ci  DCHECK_EQ(1, args.length());
951cb0ef41Sopenharmony_ci  Object object = args[0];
961cb0ef41Sopenharmony_ci  return isolate->heap()->ToBoolean(object.IsFunction());
971cb0ef41Sopenharmony_ci}
981cb0ef41Sopenharmony_ci
991cb0ef41Sopenharmony_ci
1001cb0ef41Sopenharmony_ci}  // namespace internal
1011cb0ef41Sopenharmony_ci}  // namespace v8
102