| OLD | NEW |
| 1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 #include "frames-inl.h" | 34 #include "frames-inl.h" |
| 35 #include "string-stream.h" | 35 #include "string-stream.h" |
| 36 | 36 |
| 37 using ::v8::ObjectTemplate; | 37 using ::v8::ObjectTemplate; |
| 38 using ::v8::Value; | 38 using ::v8::Value; |
| 39 using ::v8::Context; | 39 using ::v8::Context; |
| 40 using ::v8::Local; | 40 using ::v8::Local; |
| 41 using ::v8::String; | 41 using ::v8::String; |
| 42 using ::v8::Script; | 42 using ::v8::Script; |
| 43 using ::v8::Function; | 43 using ::v8::Function; |
| 44 using ::v8::AccessorInfo; | |
| 45 using ::v8::Extension; | 44 using ::v8::Extension; |
| 46 | 45 |
| 47 static void handle_property(Local<String> name, | 46 static void handle_property(Local<String> name, |
| 48 const v8::PropertyCallbackInfo<v8::Value>& info) { | 47 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 49 ApiTestFuzzer::Fuzz(); | 48 ApiTestFuzzer::Fuzz(); |
| 50 info.GetReturnValue().Set(v8_num(900)); | 49 info.GetReturnValue().Set(v8_num(900)); |
| 51 } | 50 } |
| 52 | 51 |
| 53 | 52 |
| 53 static void handle_property(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 54 ApiTestFuzzer::Fuzz(); |
| 55 CHECK_EQ(0, info.Length()); |
| 56 info.GetReturnValue().Set(v8_num(907)); |
| 57 } |
| 58 |
| 59 |
| 54 THREADED_TEST(PropertyHandler) { | 60 THREADED_TEST(PropertyHandler) { |
| 55 LocalContext env; | 61 LocalContext env; |
| 56 v8::HandleScope scope(env->GetIsolate()); | 62 v8::HandleScope scope(env->GetIsolate()); |
| 57 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); | 63 Local<v8::FunctionTemplate> fun_templ = v8::FunctionTemplate::New(); |
| 58 fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property); | 64 fun_templ->InstanceTemplate()->SetAccessor(v8_str("foo"), handle_property); |
| 65 Local<v8::FunctionTemplate> getter_templ = |
| 66 v8::FunctionTemplate::New(handle_property); |
| 67 getter_templ->SetLength(0); |
| 68 fun_templ-> |
| 69 InstanceTemplate()->SetAccessorProperty(v8_str("bar"), getter_templ); |
| 59 Local<Function> fun = fun_templ->GetFunction(); | 70 Local<Function> fun = fun_templ->GetFunction(); |
| 60 env->Global()->Set(v8_str("Fun"), fun); | 71 env->Global()->Set(v8_str("Fun"), fun); |
| 61 Local<Script> getter = v8_compile("var obj = new Fun(); obj.foo;"); | 72 Local<Script> getter = v8_compile("var obj = new Fun(); obj.foo;"); |
| 62 CHECK_EQ(900, getter->Run()->Int32Value()); | 73 CHECK_EQ(900, getter->Run()->Int32Value()); |
| 63 Local<Script> setter = v8_compile("obj.foo = 901;"); | 74 Local<Script> setter = v8_compile("obj.foo = 901;"); |
| 64 CHECK_EQ(901, setter->Run()->Int32Value()); | 75 CHECK_EQ(901, setter->Run()->Int32Value()); |
| 76 getter = v8_compile("obj.bar;"); |
| 77 CHECK_EQ(907, getter->Run()->Int32Value()); |
| 78 setter = v8_compile("obj.bar = 908;"); |
| 79 CHECK_EQ(908, setter->Run()->Int32Value()); |
| 65 } | 80 } |
| 66 | 81 |
| 67 | 82 |
| 68 static void GetIntValue(Local<String> property, | 83 static void GetIntValue(Local<String> property, |
| 69 const v8::PropertyCallbackInfo<v8::Value>& info) { | 84 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 70 ApiTestFuzzer::Fuzz(); | 85 ApiTestFuzzer::Fuzz(); |
| 71 int* value = | 86 int* value = |
| 72 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value()); | 87 static_cast<int*>(v8::Handle<v8::External>::Cast(info.Data())->Value()); |
| 73 info.GetReturnValue().Set(v8_num(*value)); | 88 info.GetReturnValue().Set(v8_num(*value)); |
| 74 } | 89 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 102 GetIntValue, | 117 GetIntValue, |
| 103 SetIntValue, | 118 SetIntValue, |
| 104 v8::External::New(&baz)); | 119 v8::External::New(&baz)); |
| 105 LocalContext env(0, templ->InstanceTemplate()); | 120 LocalContext env(0, templ->InstanceTemplate()); |
| 106 v8_compile("foo = (++bar) + baz")->Run(); | 121 v8_compile("foo = (++bar) + baz")->Run(); |
| 107 CHECK_EQ(bar, -3); | 122 CHECK_EQ(bar, -3); |
| 108 CHECK_EQ(foo, 7); | 123 CHECK_EQ(foo, 7); |
| 109 } | 124 } |
| 110 | 125 |
| 111 | 126 |
| 112 static int x_register = 0; | 127 static int x_register[2] = {0, 0}; |
| 113 static v8::Handle<v8::Object> x_receiver; | 128 static v8::Handle<v8::Object> x_receiver; |
| 114 static v8::Handle<v8::Object> x_holder; | 129 static v8::Handle<v8::Object> x_holder; |
| 115 | 130 |
| 131 template<class Info> |
| 132 static void XGetter(const Info& info, int offset) { |
| 133 ApiTestFuzzer::Fuzz(); |
| 134 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 135 CHECK_EQ(isolate, info.GetIsolate()); |
| 136 CHECK_EQ(x_receiver, info.This()); |
| 137 info.GetReturnValue().Set(v8_num(x_register[offset])); |
| 138 } |
| 139 |
| 116 | 140 |
| 117 static void XGetter(Local<String> name, | 141 static void XGetter(Local<String> name, |
| 118 const v8::PropertyCallbackInfo<v8::Value>& info) { | 142 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 119 ApiTestFuzzer::Fuzz(); | 143 CHECK_EQ(x_holder, info.Holder()); |
| 144 XGetter(info, 0); |
| 145 } |
| 146 |
| 147 |
| 148 static void XGetter(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 149 XGetter(info, 1); |
| 150 } |
| 151 |
| 152 |
| 153 template<class Info> |
| 154 static void XSetter(Local<Value> value, const Info& info, int offset) { |
| 120 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | 155 v8::Isolate* isolate = v8::Isolate::GetCurrent(); |
| 121 CHECK_EQ(isolate, info.GetIsolate()); | 156 CHECK_EQ(isolate, info.GetIsolate()); |
| 122 CHECK_EQ(x_receiver, info.This()); | 157 CHECK_EQ(x_holder, info.This()); |
| 123 CHECK_EQ(x_holder, info.Holder()); | 158 x_register[offset] = value->Int32Value(); |
| 124 info.GetReturnValue().Set(v8_num(x_register)); | |
| 125 } | 159 } |
| 126 | 160 |
| 127 | 161 |
| 128 static void XSetter(Local<String> name, | 162 static void XSetter(Local<String> name, |
| 129 Local<Value> value, | 163 Local<Value> value, |
| 130 const v8::PropertyCallbackInfo<void>& info) { | 164 const v8::PropertyCallbackInfo<void>& info) { |
| 131 v8::Isolate* isolate = v8::Isolate::GetCurrent(); | |
| 132 CHECK_EQ(isolate, info.GetIsolate()); | |
| 133 CHECK_EQ(x_holder, info.This()); | |
| 134 CHECK_EQ(x_holder, info.Holder()); | 165 CHECK_EQ(x_holder, info.Holder()); |
| 135 x_register = value->Int32Value(); | 166 XSetter(value, info, 0); |
| 136 } | 167 } |
| 137 | 168 |
| 138 | 169 |
| 170 static void XSetter(const v8::FunctionCallbackInfo<v8::Value>& info) { |
| 171 CHECK_EQ(1, info.Length()); |
| 172 XSetter(info[0], info, 1); |
| 173 } |
| 174 |
| 175 |
| 139 THREADED_TEST(AccessorIC) { | 176 THREADED_TEST(AccessorIC) { |
| 140 LocalContext context; | 177 LocalContext context; |
| 141 v8::HandleScope scope(context->GetIsolate()); | 178 v8::HandleScope scope(context->GetIsolate()); |
| 142 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); | 179 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 143 obj->SetAccessor(v8_str("x"), XGetter, XSetter); | 180 obj->SetAccessor(v8_str("x0"), XGetter, XSetter); |
| 181 obj->SetAccessorProperty(v8_str("x1"), |
| 182 v8::FunctionTemplate::New(XGetter), |
| 183 v8::FunctionTemplate::New(XSetter)); |
| 144 x_holder = obj->NewInstance(); | 184 x_holder = obj->NewInstance(); |
| 145 context->Global()->Set(v8_str("holder"), x_holder); | 185 context->Global()->Set(v8_str("holder"), x_holder); |
| 146 x_receiver = v8::Object::New(); | 186 x_receiver = v8::Object::New(); |
| 147 context->Global()->Set(v8_str("obj"), x_receiver); | 187 context->Global()->Set(v8_str("obj"), x_receiver); |
| 148 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun( | 188 v8::Handle<v8::Array> array = v8::Handle<v8::Array>::Cast(CompileRun( |
| 149 "obj.__proto__ = holder;" | 189 "obj.__proto__ = holder;" |
| 150 "var result = [];" | 190 "var result = [];" |
| 151 "for (var i = 0; i < 10; i++) {" | 191 "for (var i = 0; i < 10; i++) {" |
| 152 " holder.x = i;" | 192 " holder.x0 = i;" |
| 153 " result.push(obj.x);" | 193 " holder.x1 = i;" |
| 194 " result.push(obj.x0);" |
| 195 " result.push(obj.x1);" |
| 154 "}" | 196 "}" |
| 155 "result")); | 197 "result")); |
| 156 CHECK_EQ(10, array->Length()); | 198 CHECK_EQ(20, array->Length()); |
| 157 for (int i = 0; i < 10; i++) { | 199 for (int i = 0; i < 20; i++) { |
| 158 v8::Handle<Value> entry = array->Get(v8::Integer::New(i)); | 200 v8::Handle<Value> entry = array->Get(v8::Integer::New(i)); |
| 159 CHECK_EQ(v8::Integer::New(i), entry); | 201 CHECK_EQ(v8::Integer::New(i/2), entry); |
| 160 } | 202 } |
| 161 } | 203 } |
| 162 | 204 |
| 163 | 205 |
| 164 static void AccessorProhibitsOverwritingGetter( | 206 static void AccessorProhibitsOverwritingGetter( |
| 165 Local<String> name, | 207 Local<String> name, |
| 166 const v8::PropertyCallbackInfo<v8::Value>& info) { | 208 const v8::PropertyCallbackInfo<v8::Value>& info) { |
| 167 ApiTestFuzzer::Fuzz(); | 209 ApiTestFuzzer::Fuzz(); |
| 168 info.GetReturnValue().Set(true); | 210 info.GetReturnValue().Set(true); |
| 169 } | 211 } |
| (...skipping 308 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 478 LocalContext env; | 520 LocalContext env; |
| 479 v8::HandleScope scope(env->GetIsolate()); | 521 v8::HandleScope scope(env->GetIsolate()); |
| 480 | 522 |
| 481 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); | 523 v8::Handle<v8::ObjectTemplate> obj = ObjectTemplate::New(); |
| 482 obj->SetNamedPropertyHandler( | 524 obj->SetNamedPropertyHandler( |
| 483 JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator); | 525 JSONStringifyGetter, NULL, NULL, NULL, JSONStringifyEnumerator); |
| 484 env->Global()->Set(v8_str("obj"), obj->NewInstance()); | 526 env->Global()->Set(v8_str("obj"), obj->NewInstance()); |
| 485 v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}"); | 527 v8::Handle<v8::String> expected = v8_str("{\"regress\":\"crbug-161028\"}"); |
| 486 CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected)); | 528 CHECK(CompileRun("JSON.stringify(obj)")->Equals(expected)); |
| 487 } | 529 } |
| OLD | NEW |