test-api.h 1.94 KB
Newer Older
1 2 3 4
// Copyright 2015 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 6 7
#ifndef V8_TEST_CCTEST_TEST_API_H_
#define V8_TEST_CCTEST_TEST_API_H_

8
#include "src/init/v8.h"
9

10
#include "src/api/api.h"
11 12
#include "src/execution/isolate.h"
#include "src/execution/vm-state.h"
13 14 15 16 17
#include "test/cctest/cctest.h"

template <typename T>
static void CheckReturnValue(const T& t, i::Address callback) {
  v8::ReturnValue<v8::Value> rv = t.GetReturnValue();
18
  i::FullObjectSlot o(*reinterpret_cast<i::Address*>(&rv));
19
  CHECK_EQ(CcTest::isolate(), t.GetIsolate());
20
  i::Isolate* isolate = reinterpret_cast<i::Isolate*>(t.GetIsolate());
21
  CHECK_EQ(t.GetIsolate(), rv.GetIsolate());
22
  CHECK((*o).IsTheHole(isolate) || (*o).IsUndefined(isolate));
23
  // Verify reset
24
  bool is_runtime = (*o).IsTheHole(isolate);
25 26 27 28 29 30
  if (is_runtime) {
    CHECK(rv.Get()->IsUndefined());
  } else {
    i::Handle<i::Object> v = v8::Utils::OpenHandle(*rv.Get());
    CHECK_EQ(*v, *o);
  }
31
  rv.Set(true);
32
  CHECK(!(*o).IsTheHole(isolate) && !(*o).IsUndefined(isolate));
vogelheim's avatar
vogelheim committed
33
  rv.Set(v8::Local<v8::Object>());
34 35
  CHECK((*o).IsTheHole(isolate) || (*o).IsUndefined(isolate));
  CHECK_EQ(is_runtime, (*o).IsTheHole(isolate));
36 37
  // If CPU profiler is active check that when API callback is invoked
  // VMState is set to EXTERNAL.
38
  if (isolate->is_profiling()) {
39 40 41 42 43
    CHECK_EQ(v8::EXTERNAL, isolate->current_vm_state());
    CHECK(isolate->external_callback_scope());
    CHECK_EQ(callback, isolate->external_callback_scope()->callback());
  }
}
44

45 46 47 48 49 50 51 52 53 54
template <typename T>
static void CheckInternalFieldsAreZero(v8::Local<T> value) {
  CHECK_EQ(T::kInternalFieldCount, value->InternalFieldCount());
  for (int i = 0; i < value->InternalFieldCount(); i++) {
    CHECK_EQ(0, value->GetInternalField(i)
                    ->Int32Value(CcTest::isolate()->GetCurrentContext())
                    .FromJust());
  }
}

55
#endif  // V8_TEST_CCTEST_TEST_API_H_