OLD | NEW |
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2007-2009 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 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
43 static const bool kLogThreading = true; | 43 static const bool kLogThreading = true; |
44 | 44 |
45 static bool IsNaN(double x) { | 45 static bool IsNaN(double x) { |
46 #ifdef WIN32 | 46 #ifdef WIN32 |
47 return _isnan(x); | 47 return _isnan(x); |
48 #else | 48 #else |
49 return isnan(x); | 49 return isnan(x); |
50 #endif | 50 #endif |
51 } | 51 } |
52 | 52 |
| 53 using ::v8::AccessorInfo; |
| 54 using ::v8::Context; |
| 55 using ::v8::Extension; |
| 56 using ::v8::Function; |
| 57 using ::v8::HandleScope; |
| 58 using ::v8::Local; |
| 59 using ::v8::Object; |
53 using ::v8::ObjectTemplate; | 60 using ::v8::ObjectTemplate; |
| 61 using ::v8::Persistent; |
| 62 using ::v8::Script; |
| 63 using ::v8::String; |
54 using ::v8::Value; | 64 using ::v8::Value; |
55 using ::v8::Context; | 65 using ::v8::V8; |
56 using ::v8::Local; | |
57 using ::v8::String; | |
58 using ::v8::Script; | |
59 using ::v8::Function; | |
60 using ::v8::AccessorInfo; | |
61 using ::v8::Extension; | |
62 | 66 |
63 namespace i = ::i; | 67 namespace i = ::i; |
64 | 68 |
65 | 69 |
66 static void ExpectString(const char* code, const char* expected) { | 70 static void ExpectString(const char* code, const char* expected) { |
67 Local<Value> result = CompileRun(code); | 71 Local<Value> result = CompileRun(code); |
68 CHECK(result->IsString()); | 72 CHECK(result->IsString()); |
69 String::AsciiValue ascii(result); | 73 String::AsciiValue ascii(result); |
70 CHECK_EQ(expected, *ascii); | 74 CHECK_EQ(expected, *ascii); |
71 } | 75 } |
(...skipping 1710 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1782 { | 1786 { |
1783 v8::HandleScope scope; | 1787 v8::HandleScope scope; |
1784 Local<String> str = v8_str("str"); | 1788 Local<String> str = v8_str("str"); |
1785 global = v8::Persistent<String>::New(str); | 1789 global = v8::Persistent<String>::New(str); |
1786 } | 1790 } |
1787 CHECK_EQ(global->Length(), 3); | 1791 CHECK_EQ(global->Length(), 3); |
1788 global.Dispose(); | 1792 global.Dispose(); |
1789 } | 1793 } |
1790 | 1794 |
1791 | 1795 |
| 1796 static int NumberOfWeakCalls = 0; |
| 1797 static void WeakPointerCallback(Persistent<Value> handle, void* id) { |
| 1798 CHECK_EQ(reinterpret_cast<void*>(1234), id); |
| 1799 NumberOfWeakCalls++; |
| 1800 handle.Dispose(); |
| 1801 } |
| 1802 |
| 1803 THREADED_TEST(ApiObjectGroups) { |
| 1804 HandleScope scope; |
| 1805 LocalContext env; |
| 1806 |
| 1807 NumberOfWeakCalls = 0; |
| 1808 |
| 1809 Persistent<Object> g1s1; |
| 1810 Persistent<Object> g1s2; |
| 1811 Persistent<Object> g1c1; |
| 1812 Persistent<Object> g2s1; |
| 1813 Persistent<Object> g2s2; |
| 1814 Persistent<Object> g2c1; |
| 1815 |
| 1816 { |
| 1817 HandleScope scope; |
| 1818 g1s1 = Persistent<Object>::New(Object::New()); |
| 1819 g1s2 = Persistent<Object>::New(Object::New()); |
| 1820 g1c1 = Persistent<Object>::New(Object::New()); |
| 1821 g1s1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1822 g1s2.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1823 g1c1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1824 |
| 1825 g2s1 = Persistent<Object>::New(Object::New()); |
| 1826 g2s2 = Persistent<Object>::New(Object::New()); |
| 1827 g2c1 = Persistent<Object>::New(Object::New()); |
| 1828 g2s1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1829 g2s2.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1830 g2c1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1831 } |
| 1832 |
| 1833 Persistent<Object> root = Persistent<Object>::New(g1s1); // make a root. |
| 1834 |
| 1835 // Connect group 1 and 2, make a cycle. |
| 1836 CHECK(g1s2->Set(0, g2s2)); |
| 1837 CHECK(g2s1->Set(0, g1s1)); |
| 1838 |
| 1839 { |
| 1840 Persistent<Value> g1_objects[] = { g1s1, g1s2 }; |
| 1841 Persistent<Value> g1_children[] = { g1c1 }; |
| 1842 Persistent<Value> g2_objects[] = { g2s1, g2s2 }; |
| 1843 Persistent<Value> g2_children[] = { g2c1 }; |
| 1844 V8::AddObjectGroup(g1_objects, 2); |
| 1845 V8::AddImplicitReferences(g1s1, g1_children, 1); |
| 1846 V8::AddObjectGroup(g2_objects, 2); |
| 1847 V8::AddImplicitReferences(g2s2, g2_children, 1); |
| 1848 } |
| 1849 // Do a full GC |
| 1850 i::Heap::CollectGarbage(i::OLD_POINTER_SPACE); |
| 1851 |
| 1852 // All object should be alive. |
| 1853 CHECK_EQ(0, NumberOfWeakCalls); |
| 1854 |
| 1855 // Weaken the root. |
| 1856 root.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1857 // But make children strong roots---all the objects (except for children) |
| 1858 // should be collectable now. |
| 1859 g1c1.ClearWeak(); |
| 1860 g2c1.ClearWeak(); |
| 1861 |
| 1862 // Groups are deleted, rebuild groups. |
| 1863 { |
| 1864 Persistent<Value> g1_objects[] = { g1s1, g1s2 }; |
| 1865 Persistent<Value> g1_children[] = { g1c1 }; |
| 1866 Persistent<Value> g2_objects[] = { g2s1, g2s2 }; |
| 1867 Persistent<Value> g2_children[] = { g2c1 }; |
| 1868 V8::AddObjectGroup(g1_objects, 2); |
| 1869 V8::AddImplicitReferences(g1s1, g1_children, 1); |
| 1870 V8::AddObjectGroup(g2_objects, 2); |
| 1871 V8::AddImplicitReferences(g2s2, g2_children, 1); |
| 1872 } |
| 1873 |
| 1874 i::Heap::CollectGarbage(i::OLD_POINTER_SPACE); |
| 1875 |
| 1876 // All objects should be gone. 5 global handles in total. |
| 1877 CHECK_EQ(5, NumberOfWeakCalls); |
| 1878 |
| 1879 // And now make children weak again and collect them. |
| 1880 g1c1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1881 g2c1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1882 |
| 1883 i::Heap::CollectGarbage(i::OLD_POINTER_SPACE); |
| 1884 CHECK_EQ(7, NumberOfWeakCalls); |
| 1885 } |
| 1886 |
| 1887 |
| 1888 THREADED_TEST(ApiObjectGroupsCycle) { |
| 1889 HandleScope scope; |
| 1890 LocalContext env; |
| 1891 |
| 1892 NumberOfWeakCalls = 0; |
| 1893 |
| 1894 Persistent<Object> g1s1; |
| 1895 Persistent<Object> g1s2; |
| 1896 Persistent<Object> g2s1; |
| 1897 Persistent<Object> g2s2; |
| 1898 Persistent<Object> g3s1; |
| 1899 Persistent<Object> g3s2; |
| 1900 |
| 1901 { |
| 1902 HandleScope scope; |
| 1903 g1s1 = Persistent<Object>::New(Object::New()); |
| 1904 g1s2 = Persistent<Object>::New(Object::New()); |
| 1905 g1s1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1906 g1s2.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1907 |
| 1908 g2s1 = Persistent<Object>::New(Object::New()); |
| 1909 g2s2 = Persistent<Object>::New(Object::New()); |
| 1910 g2s1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1911 g2s2.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1912 |
| 1913 g3s1 = Persistent<Object>::New(Object::New()); |
| 1914 g3s2 = Persistent<Object>::New(Object::New()); |
| 1915 g3s1.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1916 g3s2.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1917 } |
| 1918 |
| 1919 Persistent<Object> root = Persistent<Object>::New(g1s1); // make a root. |
| 1920 |
| 1921 // Connect groups. We're building the following cycle: |
| 1922 // G1: { g1s1, g2s1 }, g1s1 implicitly references g2s1, ditto for other |
| 1923 // groups. |
| 1924 { |
| 1925 Persistent<Value> g1_objects[] = { g1s1, g1s2 }; |
| 1926 Persistent<Value> g1_children[] = { g2s1 }; |
| 1927 Persistent<Value> g2_objects[] = { g2s1, g2s2 }; |
| 1928 Persistent<Value> g2_children[] = { g3s1 }; |
| 1929 Persistent<Value> g3_objects[] = { g3s1, g3s2 }; |
| 1930 Persistent<Value> g3_children[] = { g1s1 }; |
| 1931 V8::AddObjectGroup(g1_objects, 2); |
| 1932 V8::AddImplicitReferences(g1s1, g1_children, 1); |
| 1933 V8::AddObjectGroup(g2_objects, 2); |
| 1934 V8::AddImplicitReferences(g2s1, g2_children, 1); |
| 1935 V8::AddObjectGroup(g3_objects, 2); |
| 1936 V8::AddImplicitReferences(g3s1, g3_children, 1); |
| 1937 } |
| 1938 // Do a full GC |
| 1939 i::Heap::CollectGarbage(i::OLD_POINTER_SPACE); |
| 1940 |
| 1941 // All object should be alive. |
| 1942 CHECK_EQ(0, NumberOfWeakCalls); |
| 1943 |
| 1944 // Weaken the root. |
| 1945 root.MakeWeak(reinterpret_cast<void*>(1234), &WeakPointerCallback); |
| 1946 |
| 1947 // Groups are deleted, rebuild groups. |
| 1948 { |
| 1949 Persistent<Value> g1_objects[] = { g1s1, g1s2 }; |
| 1950 Persistent<Value> g1_children[] = { g2s1 }; |
| 1951 Persistent<Value> g2_objects[] = { g2s1, g2s2 }; |
| 1952 Persistent<Value> g2_children[] = { g3s1 }; |
| 1953 Persistent<Value> g3_objects[] = { g3s1, g3s2 }; |
| 1954 Persistent<Value> g3_children[] = { g1s1 }; |
| 1955 V8::AddObjectGroup(g1_objects, 2); |
| 1956 V8::AddImplicitReferences(g1s1, g1_children, 1); |
| 1957 V8::AddObjectGroup(g2_objects, 2); |
| 1958 V8::AddImplicitReferences(g2s1, g2_children, 1); |
| 1959 V8::AddObjectGroup(g3_objects, 2); |
| 1960 V8::AddImplicitReferences(g3s1, g3_children, 1); |
| 1961 } |
| 1962 |
| 1963 i::Heap::CollectGarbage(i::OLD_POINTER_SPACE); |
| 1964 |
| 1965 // All objects should be gone. 7 global handles in total. |
| 1966 CHECK_EQ(7, NumberOfWeakCalls); |
| 1967 } |
| 1968 |
| 1969 |
1792 THREADED_TEST(ScriptException) { | 1970 THREADED_TEST(ScriptException) { |
1793 v8::HandleScope scope; | 1971 v8::HandleScope scope; |
1794 LocalContext env; | 1972 LocalContext env; |
1795 Local<Script> script = Script::Compile(v8_str("throw 'panama!';")); | 1973 Local<Script> script = Script::Compile(v8_str("throw 'panama!';")); |
1796 v8::TryCatch try_catch; | 1974 v8::TryCatch try_catch; |
1797 Local<Value> result = script->Run(); | 1975 Local<Value> result = script->Run(); |
1798 CHECK(result.IsEmpty()); | 1976 CHECK(result.IsEmpty()); |
1799 CHECK(try_catch.HasCaught()); | 1977 CHECK(try_catch.HasCaught()); |
1800 String::AsciiValue exception_value(try_catch.Exception()); | 1978 String::AsciiValue exception_value(try_catch.Exception()); |
1801 CHECK_EQ(*exception_value, "panama!"); | 1979 CHECK_EQ(*exception_value, "panama!"); |
(...skipping 11126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12928 v8::Handle<v8::Function> define_property = | 13106 v8::Handle<v8::Function> define_property = |
12929 CompileRun("(function() {" | 13107 CompileRun("(function() {" |
12930 " Object.defineProperty(" | 13108 " Object.defineProperty(" |
12931 " this," | 13109 " this," |
12932 " 1," | 13110 " 1," |
12933 " { configurable: true, enumerable: true, value: 3 });" | 13111 " { configurable: true, enumerable: true, value: 3 });" |
12934 "})").As<Function>(); | 13112 "})").As<Function>(); |
12935 context->DetachGlobal(); | 13113 context->DetachGlobal(); |
12936 define_property->Call(proxy, 0, NULL); | 13114 define_property->Call(proxy, 0, NULL); |
12937 } | 13115 } |
OLD | NEW |