test-unscopables-hidden-prototype.cc 4.07 KB
Newer Older
1 2 3 4 5 6
// 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.

#include <stdlib.h>

7
#include "include/v8-function.h"
8
#include "src/init/v8.h"
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include "test/cctest/cctest.h"

namespace {


static void Cleanup() {
  CompileRun(
      "delete object.x;"
      "delete hidden_prototype.x;"
      "delete object[Symbol.unscopables];"
      "delete hidden_prototype[Symbol.unscopables];");
}


TEST(Unscopables) {
  LocalContext context;
  v8::Isolate* isolate = context->GetIsolate();
  v8::HandleScope handle_scope(isolate);
27
  v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
28 29 30 31

  v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate);
  v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate);

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  v8::Local<v8::Object> object = t0->GetFunction(current_context)
                                     .ToLocalChecked()
                                     ->NewInstance(current_context)
                                     .ToLocalChecked();
  v8::Local<v8::Object> hidden_prototype = t1->GetFunction(current_context)
                                               .ToLocalChecked()
                                               ->NewInstance(current_context)
                                               .ToLocalChecked();

  CHECK(object->SetPrototype(current_context, hidden_prototype).FromJust());

  context->Global()
      ->Set(current_context, v8_str("object"), object)
      .FromMaybe(false);
  context->Global()
      ->Set(current_context, v8_str("hidden_prototype"), hidden_prototype)
      .FromMaybe(false);

  CHECK_EQ(1, CompileRun("var result;"
                         "var x = 0;"
                         "object.x = 1;"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result")
                  ->Int32Value(current_context)
                  .FromJust());
59 60

  Cleanup();
61 62 63 64 65 66 67 68 69
  CHECK_EQ(2, CompileRun("var result;"
                         "var x = 0;"
                         "hidden_prototype.x = 2;"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result")
                  ->Int32Value(current_context)
                  .FromJust());
70 71

  Cleanup();
72 73 74 75 76 77 78 79 80 81
  CHECK_EQ(0, CompileRun("var result;"
                         "var x = 0;"
                         "object.x = 3;"
                         "object[Symbol.unscopables] = {x: true};"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result")
                  ->Int32Value(current_context)
                  .FromJust());
82 83

  Cleanup();
84 85 86 87 88 89 90 91 92 93
  CHECK_EQ(0, CompileRun("var result;"
                         "var x = 0;"
                         "hidden_prototype.x = 4;"
                         "hidden_prototype[Symbol.unscopables] = {x: true};"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result")
                  ->Int32Value(current_context)
                  .FromJust());
94 95

  Cleanup();
96 97 98 99 100 101 102 103 104 105
  CHECK_EQ(0, CompileRun("var result;"
                         "var x = 0;"
                         "object.x = 5;"
                         "hidden_prototype[Symbol.unscopables] = {x: true};"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result;")
                  ->Int32Value(current_context)
                  .FromJust());
106 107

  Cleanup();
108 109 110 111 112 113 114 115 116 117
  CHECK_EQ(0, CompileRun("var result;"
                         "var x = 0;"
                         "hidden_prototype.x = 6;"
                         "object[Symbol.unscopables] = {x: true};"
                         "with (object) {"
                         "  result = x;"
                         "}"
                         "result")
                  ->Int32Value(current_context)
                  .FromJust());
118
}
119 120

}  // namespace