OLD | NEW |
---|---|
1 // Copyright 2015 the V8 project authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/v8.h" | 5 #include "src/v8.h" |
6 | 6 |
7 #include "src/execution.h" | 7 #include "src/execution.h" |
8 #include "src/handles.h" | 8 #include "src/handles.h" |
9 #include "src/interpreter/bytecode-array-builder.h" | 9 #include "src/interpreter/bytecode-array-builder.h" |
10 #include "src/interpreter/interpreter.h" | 10 #include "src/interpreter/interpreter.h" |
(...skipping 1981 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1992 | 1992 |
1993 // TODO(rmcilroy): modify tests when we have real try finally support. | 1993 // TODO(rmcilroy): modify tests when we have real try finally support. |
1994 std::string source(InterpreterTester::SourceForBody( | 1994 std::string source(InterpreterTester::SourceForBody( |
1995 "var a = 1; try { a = a + 1; } finally { a = a + 2; }; return a;")); | 1995 "var a = 1; try { a = a + 1; } finally { a = a + 2; }; return a;")); |
1996 InterpreterTester tester(handles.main_isolate(), source.c_str()); | 1996 InterpreterTester tester(handles.main_isolate(), source.c_str()); |
1997 auto callable = tester.GetCallable<>(); | 1997 auto callable = tester.GetCallable<>(); |
1998 | 1998 |
1999 Handle<Object> return_val = callable().ToHandleChecked(); | 1999 Handle<Object> return_val = callable().ToHandleChecked(); |
2000 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(4)); | 2000 CHECK_EQ(Smi::cast(*return_val), Smi::FromInt(4)); |
2001 } | 2001 } |
2002 | |
2003 | |
2004 TEST(InterpreterCountOperators) { | |
2005 HandleAndZoneScope handles; | |
2006 i::Isolate* isolate = handles.main_isolate(); | |
2007 i::Factory* factory = isolate->factory(); | |
2008 | |
2009 std::pair<const char*, Handle<Object>> count_ops[16] = { | |
2010 std::make_pair("var a = 1; return ++a;", | |
2011 Handle<Object>(Smi::FromInt(2), isolate)), | |
Toon Verwaest
2015/10/20 13:42:28
you could just write handle(Smi::FromInt(2), isola
rmcilroy
2015/10/22 12:47:56
Good call, done this throughout, thanks!
| |
2012 std::make_pair("var a = 1; return a++;", | |
2013 Handle<Object>(Smi::FromInt(1), isolate)), | |
2014 std::make_pair("var a = 5; return --a;", | |
2015 Handle<Object>(Smi::FromInt(4), isolate)), | |
2016 std::make_pair("var a = 5; return a--;", | |
2017 Handle<Object>(Smi::FromInt(5), isolate)), | |
2018 std::make_pair("var a = 5.2; return --a;", | |
2019 factory->NewHeapNumber(4.2)), | |
2020 std::make_pair("var a = 'string'; return ++a;", | |
2021 factory->nan_value()), | |
2022 std::make_pair("var a = 'string'; return a--;", | |
2023 factory->nan_value()), | |
2024 std::make_pair("var a = true; return ++a;", | |
2025 Handle<Object>(Smi::FromInt(2), isolate)), | |
2026 std::make_pair("var a = false; return a--;", | |
2027 Handle<Object>(Smi::FromInt(0), isolate)), | |
2028 std::make_pair("var a = { val: 11 }; return ++a.val;", | |
2029 Handle<Object>(Smi::FromInt(12), isolate)), | |
2030 std::make_pair("var a = { val: 11 }; return a.val--;", | |
2031 Handle<Object>(Smi::FromInt(11), isolate)), | |
2032 std::make_pair("var a = { val: 11 }; return ++a.val;", | |
2033 Handle<Object>(Smi::FromInt(12), isolate)), | |
2034 std::make_pair("var name = 'val'; var a = { val: 22 }; return --a[name];", | |
2035 Handle<Object>(Smi::FromInt(21), isolate)), | |
2036 std::make_pair("var name = 'val'; var a = { val: 22 }; return a[name]++;", | |
2037 Handle<Object>(Smi::FromInt(22), isolate)), | |
2038 std::make_pair("var a = 1; (function() { a = 2 })(); return ++a;", | |
2039 Handle<Object>(Smi::FromInt(3), isolate)), | |
2040 std::make_pair("var a = 1; (function() { a = 2 })(); return a--;", | |
2041 Handle<Object>(Smi::FromInt(2), isolate)), | |
2042 }; | |
2043 | |
2044 for (size_t i = 0; i < arraysize(count_ops); i++) { | |
2045 std::string source(InterpreterTester::SourceForBody(count_ops[i].first)); | |
2046 InterpreterTester tester(handles.main_isolate(), source.c_str()); | |
2047 auto callable = tester.GetCallable<>(); | |
2048 | |
2049 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
2050 CHECK(return_value->SameValue(*count_ops[i].second)); | |
2051 } | |
2052 } | |
2053 | |
2054 | |
2055 TEST(InterpreterGlobalCountOperators) { | |
2056 HandleAndZoneScope handles; | |
2057 i::Isolate* isolate = handles.main_isolate(); | |
2058 | |
2059 std::pair<const char*, Handle<Object>> count_ops[6] = { | |
2060 std::make_pair("var global = 100;function f(){ return ++global; }", | |
2061 Handle<Object>(Smi::FromInt(101), isolate)), | |
2062 std::make_pair("var global = 100; function f(){ return --global; }", | |
2063 Handle<Object>(Smi::FromInt(99), isolate)), | |
2064 std::make_pair("var global = 100; function f(){ return global++; }", | |
2065 Handle<Object>(Smi::FromInt(100), isolate)), | |
2066 std::make_pair("unallocated = 200; function f(){ return ++unallocated; }", | |
2067 Handle<Object>(Smi::FromInt(201), isolate)), | |
2068 std::make_pair("unallocated = 200; function f(){ return --unallocated; }", | |
2069 Handle<Object>(Smi::FromInt(199), isolate)), | |
2070 std::make_pair("unallocated = 200; function f(){ return unallocated++; }", | |
2071 Handle<Object>(Smi::FromInt(200), isolate)), | |
2072 }; | |
2073 | |
2074 for (size_t i = 0; i < arraysize(count_ops); i++) { | |
2075 InterpreterTester tester(handles.main_isolate(), count_ops[i].first); | |
2076 auto callable = tester.GetCallable<>(); | |
2077 | |
2078 Handle<i::Object> return_value = callable().ToHandleChecked(); | |
2079 CHECK(return_value->SameValue(*count_ops[i].second)); | |
2080 } | |
2081 } | |
OLD | NEW |