Home | History | Annotate | Download | only in cctest
      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 <stdlib.h>
      6 
      7 #include "src/v8.h"
      8 #include "test/cctest/cctest.h"
      9 
     10 namespace {
     11 
     12 
     13 static void Cleanup() {
     14   CompileRun(
     15       "delete object.x;"
     16       "delete hidden_prototype.x;"
     17       "delete object[Symbol.unscopables];"
     18       "delete hidden_prototype[Symbol.unscopables];");
     19 }
     20 
     21 
     22 TEST(Unscopables) {
     23   LocalContext context;
     24   v8::Isolate* isolate = context->GetIsolate();
     25   v8::HandleScope handle_scope(isolate);
     26   v8::Local<v8::Context> current_context = isolate->GetCurrentContext();
     27 
     28   v8::Local<v8::FunctionTemplate> t0 = v8::FunctionTemplate::New(isolate);
     29   v8::Local<v8::FunctionTemplate> t1 = v8::FunctionTemplate::New(isolate);
     30 
     31   t1->SetHiddenPrototype(true);
     32 
     33   v8::Local<v8::Object> object = t0->GetFunction(current_context)
     34                                      .ToLocalChecked()
     35                                      ->NewInstance(current_context)
     36                                      .ToLocalChecked();
     37   v8::Local<v8::Object> hidden_prototype = t1->GetFunction(current_context)
     38                                                .ToLocalChecked()
     39                                                ->NewInstance(current_context)
     40                                                .ToLocalChecked();
     41 
     42   CHECK(object->SetPrototype(current_context, hidden_prototype).FromJust());
     43 
     44   context->Global()
     45       ->Set(current_context, v8_str("object"), object)
     46       .FromMaybe(false);
     47   context->Global()
     48       ->Set(current_context, v8_str("hidden_prototype"), hidden_prototype)
     49       .FromMaybe(false);
     50 
     51   CHECK_EQ(1, CompileRun("var result;"
     52                          "var x = 0;"
     53                          "object.x = 1;"
     54                          "with (object) {"
     55                          "  result = x;"
     56                          "}"
     57                          "result")
     58                   ->Int32Value(current_context)
     59                   .FromJust());
     60 
     61   Cleanup();
     62   CHECK_EQ(2, CompileRun("var result;"
     63                          "var x = 0;"
     64                          "hidden_prototype.x = 2;"
     65                          "with (object) {"
     66                          "  result = x;"
     67                          "}"
     68                          "result")
     69                   ->Int32Value(current_context)
     70                   .FromJust());
     71 
     72   Cleanup();
     73   CHECK_EQ(0, CompileRun("var result;"
     74                          "var x = 0;"
     75                          "object.x = 3;"
     76                          "object[Symbol.unscopables] = {x: true};"
     77                          "with (object) {"
     78                          "  result = x;"
     79                          "}"
     80                          "result")
     81                   ->Int32Value(current_context)
     82                   .FromJust());
     83 
     84   Cleanup();
     85   CHECK_EQ(0, CompileRun("var result;"
     86                          "var x = 0;"
     87                          "hidden_prototype.x = 4;"
     88                          "hidden_prototype[Symbol.unscopables] = {x: true};"
     89                          "with (object) {"
     90                          "  result = x;"
     91                          "}"
     92                          "result")
     93                   ->Int32Value(current_context)
     94                   .FromJust());
     95 
     96   Cleanup();
     97   CHECK_EQ(0, CompileRun("var result;"
     98                          "var x = 0;"
     99                          "object.x = 5;"
    100                          "hidden_prototype[Symbol.unscopables] = {x: true};"
    101                          "with (object) {"
    102                          "  result = x;"
    103                          "}"
    104                          "result;")
    105                   ->Int32Value(current_context)
    106                   .FromJust());
    107 
    108   Cleanup();
    109   CHECK_EQ(0, CompileRun("var result;"
    110                          "var x = 0;"
    111                          "hidden_prototype.x = 6;"
    112                          "object[Symbol.unscopables] = {x: true};"
    113                          "with (object) {"
    114                          "  result = x;"
    115                          "}"
    116                          "result")
    117                   ->Int32Value(current_context)
    118                   .FromJust());
    119 }
    120 
    121 }  // namespace
    122