runtime-function.cc 9.08 KB
Newer Older
1 2 3 4
// Copyright 2014 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5
#include "src/runtime/runtime-utils.h"
6 7 8 9

#include "src/accessors.h"
#include "src/arguments.h"
#include "src/compiler.h"
10
#include "src/frames-inl.h"
11
#include "src/isolate-inl.h"
12
#include "src/messages.h"
13
#include "src/wasm/wasm-module.h"
14 15 16 17 18

namespace v8 {
namespace internal {

RUNTIME_FUNCTION(Runtime_FunctionGetName) {
19
  HandleScope scope(isolate);
20 21
  DCHECK(args.length() == 1);

22 23
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
  if (function->IsJSBoundFunction()) {
24 25 26
    RETURN_RESULT_OR_FAILURE(
        isolate, JSBoundFunction::GetName(
                     isolate, Handle<JSBoundFunction>::cast(function)));
27
  } else {
28
    return *JSFunction::GetName(isolate, Handle<JSFunction>::cast(function));
29
  }
30 31 32 33
}


RUNTIME_FUNCTION(Runtime_FunctionSetName) {
34
  HandleScope scope(isolate);
35 36
  DCHECK(args.length() == 2);

37
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, f, 0);
38
  CONVERT_ARG_HANDLE_CHECKED(String, name, 1);
39

40 41
  name = String::Flatten(name);
  f->shared()->set_name(*name);
42 43 44 45 46 47 48 49 50
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_FunctionRemovePrototype) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 1);

  CONVERT_ARG_CHECKED(JSFunction, f, 0);
51
  CHECK(f->RemovePrototype());
52
  f->shared()->SetConstructStub(
53
      *isolate->builtins()->ConstructedNonConstructable());
54 55 56 57 58 59 60

  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_FunctionGetScript) {
  HandleScope scope(isolate);
61 62
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
63

64 65 66 67 68 69 70 71
  if (function->IsJSFunction()) {
    Handle<Object> script(
        Handle<JSFunction>::cast(function)->shared()->script(), isolate);
    if (script->IsScript()) {
      return *Script::GetWrapper(Handle<Script>::cast(script));
    }
  }
  return isolate->heap()->undefined_value();
72 73 74 75 76
}


RUNTIME_FUNCTION(Runtime_FunctionGetSourceCode) {
  HandleScope scope(isolate);
77 78
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
79 80 81 82
  if (function->IsJSFunction()) {
    return *Handle<JSFunction>::cast(function)->shared()->GetSourceCode();
  }
  return isolate->heap()->undefined_value();
83 84 85 86 87 88 89 90 91 92 93 94
}


RUNTIME_FUNCTION(Runtime_FunctionGetScriptSourcePosition) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 1);

  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
  int pos = fun->shared()->start_position();
  return Smi::FromInt(pos);
}

95 96 97 98 99 100 101 102
RUNTIME_FUNCTION(Runtime_FunctionGetContextData) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 1);

  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
  FixedArray* array = fun->native_context()->embedder_data();
  return array->get(v8::Context::kDebugIdIndex);
}
103 104 105 106 107 108 109

RUNTIME_FUNCTION(Runtime_FunctionSetInstanceClassName) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 2);

  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
  CONVERT_ARG_CHECKED(String, name, 1);
110
  fun->shared()->set_instance_class_name(name);
111 112 113 114 115 116 117 118 119 120
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_FunctionSetLength) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 2);

  CONVERT_ARG_CHECKED(JSFunction, fun, 0);
  CONVERT_SMI_ARG_CHECKED(length, 1);
121
  CHECK((length & 0xC0000000) == 0xC0000000 || (length & 0xC0000000) == 0x0);
122 123 124 125 126 127 128 129 130 131 132
  fun->shared()->set_length(length);
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_FunctionSetPrototype) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 2);

  CONVERT_ARG_HANDLE_CHECKED(JSFunction, fun, 0);
  CONVERT_ARG_HANDLE_CHECKED(Object, value, 1);
133
  CHECK(fun->IsConstructor());
134 135
  RETURN_FAILURE_ON_EXCEPTION(isolate,
                              Accessors::FunctionSetPrototype(fun, value));
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
  return args[0];  // return TOS
}


RUNTIME_FUNCTION(Runtime_FunctionIsAPIFunction) {
  SealHandleScope shs(isolate);
  DCHECK(args.length() == 1);

  CONVERT_ARG_CHECKED(JSFunction, f, 0);
  return isolate->heap()->ToBoolean(f->shared()->IsApiFunction());
}


RUNTIME_FUNCTION(Runtime_SetCode) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 2);

  CONVERT_ARG_HANDLE_CHECKED(JSFunction, target, 0);
  CONVERT_ARG_HANDLE_CHECKED(JSFunction, source, 1);

  Handle<SharedFunctionInfo> target_shared(target->shared());
  Handle<SharedFunctionInfo> source_shared(source->shared());

159
  if (!Compiler::Compile(source, Compiler::KEEP_EXCEPTION)) {
160 161 162 163 164 165 166 167 168 169 170 171 172
    return isolate->heap()->exception();
  }

  // Mark both, the source and the target, as un-flushable because the
  // shared unoptimized code makes them impossible to enqueue in a list.
  DCHECK(target_shared->code()->gc_metadata() == NULL);
  DCHECK(source_shared->code()->gc_metadata() == NULL);
  target_shared->set_dont_flush(true);
  source_shared->set_dont_flush(true);

  // Set the code, scope info, formal parameter count, and the length
  // of the target shared function info.
  target_shared->ReplaceCode(source_shared->code());
173
  if (source_shared->HasBytecodeArray()) {
174
    target_shared->set_bytecode_array(source_shared->bytecode_array());
175
  }
176
  target_shared->set_scope_info(source_shared->scope_info());
177
  target_shared->set_outer_scope_info(source_shared->outer_scope_info());
178
  target_shared->set_length(source_shared->length());
179
  target_shared->set_num_literals(source_shared->num_literals());
180
  target_shared->set_feedback_metadata(source_shared->feedback_metadata());
181 182
  target_shared->set_internal_formal_parameter_count(
      source_shared->internal_formal_parameter_count());
183 184 185 186 187
  target_shared->set_start_position_and_type(
      source_shared->start_position_and_type());
  target_shared->set_end_position(source_shared->end_position());
  bool was_native = target_shared->native();
  target_shared->set_compiler_hints(source_shared->compiler_hints());
188 189
  target_shared->set_opt_count_and_bailout_reason(
      source_shared->opt_count_and_bailout_reason());
190 191
  target_shared->set_native(was_native);
  target_shared->set_profiler_ticks(source_shared->profiler_ticks());
192 193
  SharedFunctionInfo::SetScript(
      target_shared, Handle<Object>(source_shared->script(), isolate));
194 195 196

  // Set the code of the target function.
  target->ReplaceCode(source_shared->code());
197
  DCHECK(target->next_function_link()->IsUndefined(isolate));
198 199 200

  Handle<Context> context(source->context());
  target->set_context(*context);
201

202 203 204 205
  // Make sure we get a fresh copy of the literal vector to avoid cross
  // context contamination, and that the literal vector makes it's way into
  // the target_shared optimized code map.
  JSFunction::EnsureLiterals(target);
206

207
  if (isolate->logger()->is_logging_code_events() || isolate->is_profiling()) {
208 209
    isolate->logger()->LogExistingFunction(
        source_shared, Handle<AbstractCode>(source_shared->abstract_code()));
210 211 212 213 214 215 216 217 218 219 220
  }

  return *target;
}


// Set the native flag on the function.
// This is used to decide if we should transform null and undefined
// into the global object when doing call and apply.
RUNTIME_FUNCTION(Runtime_SetNativeFlag) {
  SealHandleScope shs(isolate);
221
  DCHECK_EQ(1, args.length());
222 223 224 225 226 227 228 229 230 231 232

  CONVERT_ARG_CHECKED(Object, object, 0);

  if (object->IsJSFunction()) {
    JSFunction* func = JSFunction::cast(object);
    func->shared()->set_native(true);
  }
  return isolate->heap()->undefined_value();
}


233
RUNTIME_FUNCTION(Runtime_IsConstructor) {
234 235 236 237
  SealHandleScope shs(isolate);
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_CHECKED(Object, object, 0);
  return isolate->heap()->ToBoolean(object->IsConstructor());
238 239
}

240
RUNTIME_FUNCTION(Runtime_SetForceInlineFlag) {
241
  SealHandleScope shs(isolate);
242
  DCHECK_EQ(1, args.length());
243 244 245 246
  CONVERT_ARG_HANDLE_CHECKED(Object, object, 0);

  if (object->IsJSFunction()) {
    JSFunction* func = JSFunction::cast(*object);
247
    func->shared()->set_force_inline(true);
248 249 250 251 252 253 254
  }
  return isolate->heap()->undefined_value();
}


RUNTIME_FUNCTION(Runtime_Call) {
  HandleScope scope(isolate);
255 256
  DCHECK_LE(2, args.length());
  int const argc = args.length() - 2;
257
  CONVERT_ARG_HANDLE_CHECKED(Object, target, 0);
258 259
  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 1);
  ScopedVector<Handle<Object>> argv(argc);
260
  for (int i = 0; i < argc; ++i) {
261
    argv[i] = args.at<Object>(2 + i);
262
  }
263 264
  RETURN_RESULT_OR_FAILURE(
      isolate, Execution::Call(isolate, target, receiver, argc, argv.start()));
265 266 267
}


268 269 270 271 272
// ES6 section 9.2.1.2, OrdinaryCallBindThis for sloppy callee.
RUNTIME_FUNCTION(Runtime_ConvertReceiver) {
  HandleScope scope(isolate);
  DCHECK(args.length() == 1);
  CONVERT_ARG_HANDLE_CHECKED(Object, receiver, 0);
273
  return *Object::ConvertReceiver(isolate, receiver).ToHandleChecked();
274 275 276
}


277
RUNTIME_FUNCTION(Runtime_IsFunction) {
278
  SealHandleScope shs(isolate);
279 280 281
  DCHECK_EQ(1, args.length());
  CONVERT_ARG_CHECKED(Object, object, 0);
  return isolate->heap()->ToBoolean(object->IsFunction());
282
}
283 284


285 286 287
RUNTIME_FUNCTION(Runtime_FunctionToString) {
  HandleScope scope(isolate);
  DCHECK_EQ(1, args.length());
288 289 290 291 292
  CONVERT_ARG_HANDLE_CHECKED(JSReceiver, function, 0);
  return function->IsJSBoundFunction()
             ? *JSBoundFunction::ToString(
                   Handle<JSBoundFunction>::cast(function))
             : *JSFunction::ToString(Handle<JSFunction>::cast(function));
293 294
}

295 296
}  // namespace internal
}  // namespace v8