Line data Source code
1 : // Copyright 2015 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 : #include "src/heap/heap.h"
11 : #include "src/objects-inl.h"
12 : #include "src/objects.h"
13 :
14 : namespace v8 {
15 : namespace internal {
16 :
17 15 : void TestArrayBufferViewContents(LocalContext& env, bool should_use_buffer) {
18 : v8::Local<v8::Object> obj_a = v8::Local<v8::Object>::Cast(
19 30 : env->Global()
20 60 : ->Get(v8::Isolate::GetCurrent()->GetCurrentContext(), v8_str("a"))
21 : .ToLocalChecked());
22 15 : CHECK(obj_a->IsArrayBufferView());
23 : v8::Local<v8::ArrayBufferView> array_buffer_view =
24 : v8::Local<v8::ArrayBufferView>::Cast(obj_a);
25 15 : CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
26 15 : unsigned char contents[4] = {23, 23, 23, 23};
27 15 : CHECK_EQ(sizeof(contents),
28 : array_buffer_view->CopyContents(contents, sizeof(contents)));
29 15 : CHECK_EQ(array_buffer_view->HasBuffer(), should_use_buffer);
30 135 : for (size_t i = 0; i < sizeof(contents); ++i) {
31 120 : CHECK_EQ(i, contents[i]);
32 : }
33 15 : }
34 :
35 :
36 26644 : TEST(CopyContentsTypedArray) {
37 5 : LocalContext env;
38 10 : v8::HandleScope scope(env->GetIsolate());
39 : CompileRun(
40 : "var a = new Uint8Array(4);"
41 : "a[0] = 0;"
42 : "a[1] = 1;"
43 : "a[2] = 2;"
44 : "a[3] = 3;");
45 5 : TestArrayBufferViewContents(env, false);
46 5 : }
47 :
48 :
49 26644 : TEST(CopyContentsArray) {
50 5 : LocalContext env;
51 10 : v8::HandleScope scope(env->GetIsolate());
52 : CompileRun("var a = new Uint8Array([0, 1, 2, 3]);");
53 5 : TestArrayBufferViewContents(env, false);
54 5 : }
55 :
56 :
57 26644 : TEST(CopyContentsView) {
58 5 : LocalContext env;
59 10 : v8::HandleScope scope(env->GetIsolate());
60 : CompileRun(
61 : "var b = new ArrayBuffer(6);"
62 : "var c = new Uint8Array(b);"
63 : "c[0] = -1;"
64 : "c[1] = -1;"
65 : "c[2] = 0;"
66 : "c[3] = 1;"
67 : "c[4] = 2;"
68 : "c[5] = 3;"
69 : "var a = new DataView(b, 2);");
70 5 : TestArrayBufferViewContents(env, true);
71 5 : }
72 :
73 :
74 26644 : TEST(AllocateNotExternal) {
75 5 : LocalContext env;
76 10 : v8::HandleScope scope(env->GetIsolate());
77 5 : void* memory = reinterpret_cast<Isolate*>(env->GetIsolate())
78 : ->array_buffer_allocator()
79 5 : ->Allocate(1024);
80 : v8::Local<v8::ArrayBuffer> buffer =
81 : v8::ArrayBuffer::New(env->GetIsolate(), memory, 1024,
82 5 : v8::ArrayBufferCreationMode::kInternalized);
83 5 : CHECK(!buffer->IsExternal());
84 5 : CHECK_EQ(memory, buffer->GetContents().Data());
85 5 : }
86 :
87 25 : void TestSpeciesProtector(char* code,
88 : bool invalidates_species_protector = true) {
89 : v8::Isolate::CreateParams create_params;
90 25 : create_params.array_buffer_allocator = CcTest::array_buffer_allocator();
91 : std::string typed_array_constructors[] = {
92 : #define TYPED_ARRAY_CTOR(Type, type, TYPE, ctype) #Type "Array",
93 :
94 : TYPED_ARRAYS(TYPED_ARRAY_CTOR)
95 : #undef TYPED_ARRAY_CTOR
96 325 : };
97 :
98 575 : for (auto& constructor : typed_array_constructors) {
99 275 : v8::Isolate* isolate = v8::Isolate::New(create_params);
100 275 : isolate->Enter();
101 : {
102 275 : LocalContext context(isolate);
103 550 : v8::HandleScope scope(isolate);
104 550 : v8::TryCatch try_catch(isolate);
105 :
106 825 : CompileRun(("let x = new " + constructor + "();").c_str());
107 825 : CompileRun(("let constructor = " + constructor + ";").c_str());
108 : v8::Local<v8::Value> constructor_obj = CompileRun(constructor.c_str());
109 275 : CHECK_EQ(constructor_obj, CompileRun("x.slice().constructor"));
110 275 : CHECK_EQ(constructor_obj, CompileRun("x.subarray().constructor"));
111 275 : CHECK_EQ(constructor_obj, CompileRun("x.map(()=>{}).constructor"));
112 550 : std::string decl = "class MyTypedArray extends " + constructor + " { }";
113 : CompileRun(decl.c_str());
114 :
115 : v8::internal::Isolate* i_isolate =
116 : reinterpret_cast<v8::internal::Isolate*>(isolate);
117 275 : CHECK(i_isolate->IsTypedArraySpeciesLookupChainIntact());
118 : CompileRun(code);
119 275 : if (invalidates_species_protector) {
120 220 : CHECK(!i_isolate->IsTypedArraySpeciesLookupChainIntact());
121 : } else {
122 55 : CHECK(i_isolate->IsTypedArraySpeciesLookupChainIntact());
123 : }
124 :
125 : v8::Local<v8::Value> my_typed_array = CompileRun("MyTypedArray");
126 275 : CHECK_EQ(my_typed_array, CompileRun("x.slice().constructor"));
127 275 : CHECK_EQ(my_typed_array, CompileRun("x.subarray().constructor"));
128 275 : CHECK_EQ(my_typed_array, CompileRun("x.map(()=>{}).constructor"));
129 : }
130 275 : isolate->Exit();
131 275 : isolate->Dispose();
132 : }
133 25 : }
134 :
135 26644 : UNINITIALIZED_TEST(SpeciesConstructor) {
136 5 : char code[] = "x.constructor = MyTypedArray";
137 5 : TestSpeciesProtector(code);
138 5 : }
139 :
140 26644 : UNINITIALIZED_TEST(SpeciesConstructorAccessor) {
141 : char code[] =
142 5 : "Object.defineProperty(x, 'constructor',{get() {return MyTypedArray;}})";
143 5 : TestSpeciesProtector(code);
144 5 : }
145 :
146 26644 : UNINITIALIZED_TEST(SpeciesModified) {
147 : char code[] =
148 : "Object.defineProperty(constructor, Symbol.species, "
149 5 : "{value:MyTypedArray})";
150 5 : TestSpeciesProtector(code);
151 5 : }
152 :
153 26644 : UNINITIALIZED_TEST(SpeciesParentConstructor) {
154 5 : char code[] = "constructor.prototype.constructor = MyTypedArray";
155 5 : TestSpeciesProtector(code);
156 5 : }
157 :
158 26644 : UNINITIALIZED_TEST(SpeciesProto) {
159 5 : char code[] = "x.__proto__ = MyTypedArray.prototype";
160 5 : TestSpeciesProtector(code, false);
161 5 : }
162 :
163 : } // namespace internal
164 79917 : } // namespace v8
|