| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "vm/compiler.h" |
| 5 #include "platform/assert.h" | 6 #include "platform/assert.h" |
| 6 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
| 7 #include "vm/code_patcher.h" | 8 #include "vm/code_patcher.h" |
| 8 #include "vm/compiler.h" | |
| 9 #include "vm/dart_api_impl.h" | 9 #include "vm/dart_api_impl.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| 11 #include "vm/safepoint.h" | 11 #include "vm/safepoint.h" |
| 12 #include "vm/symbols.h" | 12 #include "vm/symbols.h" |
| 13 #include "vm/thread_pool.h" | 13 #include "vm/thread_pool.h" |
| 14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 ISOLATE_UNIT_TEST_CASE(CompileScript) { | 18 ISOLATE_UNIT_TEST_CASE(CompileScript) { |
| 19 const char* kScriptChars = | 19 const char* kScriptChars = |
| 20 "class A {\n" | 20 "class A {\n" |
| 21 " static foo() { return 42; }\n" | 21 " static foo() { return 42; }\n" |
| 22 "}\n"; | 22 "}\n"; |
| 23 String& url = String::Handle(String::New("dart-test:CompileScript")); | 23 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 24 String& source = String::Handle(String::New(kScriptChars)); | 24 String& source = String::Handle(String::New(kScriptChars)); |
| 25 Script& script = | 25 Script& script = |
| 26 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); | 26 Script::Handle(Script::New(url, source, RawScript::kScriptTag)); |
| 27 Library& lib = Library::Handle(Library::CoreLibrary()); | 27 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 28 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 28 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 29 } | 29 } |
| 30 | 30 |
| 31 | |
| 32 ISOLATE_UNIT_TEST_CASE(CompileFunction) { | 31 ISOLATE_UNIT_TEST_CASE(CompileFunction) { |
| 33 const char* kScriptChars = | 32 const char* kScriptChars = |
| 34 "class A {\n" | 33 "class A {\n" |
| 35 " static foo() { return 42; }\n" | 34 " static foo() { return 42; }\n" |
| 36 " static moo() {\n" | 35 " static moo() {\n" |
| 37 " // A.foo();\n" | 36 " // A.foo();\n" |
| 38 " }\n" | 37 " }\n" |
| 39 "}\n"; | 38 "}\n"; |
| 40 String& url = String::Handle(String::New("dart-test:CompileFunction")); | 39 String& url = String::Handle(String::New("dart-test:CompileFunction")); |
| 41 String& source = String::Handle(String::New(kScriptChars)); | 40 String& source = String::Handle(String::New(kScriptChars)); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 61 Function::Handle(cls.LookupStaticFunction(function_moo_name)); | 60 Function::Handle(cls.LookupStaticFunction(function_moo_name)); |
| 62 EXPECT(!function_moo.IsNull()); | 61 EXPECT(!function_moo.IsNull()); |
| 63 | 62 |
| 64 EXPECT(CompilerTest::TestCompileFunction(function_moo)); | 63 EXPECT(CompilerTest::TestCompileFunction(function_moo)); |
| 65 EXPECT(function_moo.HasCode()); | 64 EXPECT(function_moo.HasCode()); |
| 66 function_source = function_moo.GetSource(); | 65 function_source = function_moo.GetSource(); |
| 67 EXPECT_STREQ("static moo() {\n // A.foo();\n }", | 66 EXPECT_STREQ("static moo() {\n // A.foo();\n }", |
| 68 function_source.ToCString()); | 67 function_source.ToCString()); |
| 69 } | 68 } |
| 70 | 69 |
| 71 | |
| 72 ISOLATE_UNIT_TEST_CASE(CompileFunctionOnHelperThread) { | 70 ISOLATE_UNIT_TEST_CASE(CompileFunctionOnHelperThread) { |
| 73 // Create a simple function and compile it without optimization. | 71 // Create a simple function and compile it without optimization. |
| 74 const char* kScriptChars = | 72 const char* kScriptChars = |
| 75 "class A {\n" | 73 "class A {\n" |
| 76 " static foo() { return 42; }\n" | 74 " static foo() { return 42; }\n" |
| 77 "}\n"; | 75 "}\n"; |
| 78 String& url = | 76 String& url = |
| 79 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); | 77 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); |
| 80 String& source = String::Handle(String::New(kScriptChars)); | 78 String& source = String::Handle(String::New(kScriptChars)); |
| 81 Script& script = | 79 Script& script = |
| (...skipping 23 matching lines...) Expand all Loading... |
| 105 { | 103 { |
| 106 MonitorLocker ml(m); | 104 MonitorLocker ml(m); |
| 107 while (!func.HasOptimizedCode()) { | 105 while (!func.HasOptimizedCode()) { |
| 108 ml.WaitWithSafepointCheck(thread, 1); | 106 ml.WaitWithSafepointCheck(thread, 1); |
| 109 } | 107 } |
| 110 } | 108 } |
| 111 delete m; | 109 delete m; |
| 112 BackgroundCompiler::Stop(isolate); | 110 BackgroundCompiler::Stop(isolate); |
| 113 } | 111 } |
| 114 | 112 |
| 115 | |
| 116 TEST_CASE(RegenerateAllocStubs) { | 113 TEST_CASE(RegenerateAllocStubs) { |
| 117 const char* kScriptChars = | 114 const char* kScriptChars = |
| 118 "class A {\n" | 115 "class A {\n" |
| 119 "}\n" | 116 "}\n" |
| 120 "unOpt() => new A(); \n" | 117 "unOpt() => new A(); \n" |
| 121 "optIt() => new A(); \n" | 118 "optIt() => new A(); \n" |
| 122 "A main() {\n" | 119 "A main() {\n" |
| 123 " return unOpt();\n" | 120 " return unOpt();\n" |
| 124 "}\n"; | 121 "}\n"; |
| 125 | 122 |
| (...skipping 17 matching lines...) Expand all Loading... |
| 143 | 140 |
| 144 owner.DisableAllocationStub(); | 141 owner.DisableAllocationStub(); |
| 145 result = Dart_Invoke(lib, NewString("main"), 0, NULL); | 142 result = Dart_Invoke(lib, NewString("main"), 0, NULL); |
| 146 EXPECT_VALID(result); | 143 EXPECT_VALID(result); |
| 147 | 144 |
| 148 owner.DisableAllocationStub(); | 145 owner.DisableAllocationStub(); |
| 149 result = Dart_Invoke(lib, NewString("main"), 0, NULL); | 146 result = Dart_Invoke(lib, NewString("main"), 0, NULL); |
| 150 EXPECT_VALID(result); | 147 EXPECT_VALID(result); |
| 151 } | 148 } |
| 152 | 149 |
| 153 | |
| 154 TEST_CASE(EvalExpression) { | 150 TEST_CASE(EvalExpression) { |
| 155 const char* kScriptChars = | 151 const char* kScriptChars = |
| 156 "int ten = 2 * 5; \n" | 152 "int ten = 2 * 5; \n" |
| 157 "get dot => '.'; \n" | 153 "get dot => '.'; \n" |
| 158 "class A { \n" | 154 "class A { \n" |
| 159 " var apa = 'Herr Nilsson'; \n" | 155 " var apa = 'Herr Nilsson'; \n" |
| 160 " calc(x) => '${x*ten}'; \n" | 156 " calc(x) => '${x*ten}'; \n" |
| 161 "} \n" | 157 "} \n" |
| 162 "makeObj() => new A(); \n"; | 158 "makeObj() => new A(); \n"; |
| 163 | 159 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 176 Object& val = Object::Handle(); | 172 Object& val = Object::Handle(); |
| 177 const Class& receiver_cls = Class::Handle(obj.clazz()); | 173 const Class& receiver_cls = Class::Handle(obj.clazz()); |
| 178 val = Instance::Cast(obj).Evaluate( | 174 val = Instance::Cast(obj).Evaluate( |
| 179 receiver_cls, expr_text, Array::empty_array(), Array::empty_array()); | 175 receiver_cls, expr_text, Array::empty_array(), Array::empty_array()); |
| 180 EXPECT(!val.IsNull()); | 176 EXPECT(!val.IsNull()); |
| 181 EXPECT(!val.IsError()); | 177 EXPECT(!val.IsError()); |
| 182 EXPECT(val.IsString()); | 178 EXPECT(val.IsString()); |
| 183 EXPECT_STREQ("Herr Nilsson 100.", val.ToCString()); | 179 EXPECT_STREQ("Herr Nilsson 100.", val.ToCString()); |
| 184 } | 180 } |
| 185 | 181 |
| 186 | |
| 187 ISOLATE_UNIT_TEST_CASE(EvalExpressionWithLazyCompile) { | 182 ISOLATE_UNIT_TEST_CASE(EvalExpressionWithLazyCompile) { |
| 188 Library& lib = Library::Handle(Library::CoreLibrary()); | 183 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 189 | 184 |
| 190 const String& expression = String::Handle( | 185 const String& expression = String::Handle( |
| 191 String::New("(){ return (){ return (){ return 3 + 4; }(); }(); }()")); | 186 String::New("(){ return (){ return (){ return 3 + 4; }(); }(); }()")); |
| 192 Object& val = Object::Handle(); | 187 Object& val = Object::Handle(); |
| 193 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 188 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 194 | 189 |
| 195 EXPECT(!val.IsNull()); | 190 EXPECT(!val.IsNull()); |
| 196 EXPECT(!val.IsError()); | 191 EXPECT(!val.IsError()); |
| 197 EXPECT(val.IsInteger()); | 192 EXPECT(val.IsInteger()); |
| 198 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 193 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 199 } | 194 } |
| 200 | 195 |
| 201 | |
| 202 ISOLATE_UNIT_TEST_CASE(EvalExpressionExhaustCIDs) { | 196 ISOLATE_UNIT_TEST_CASE(EvalExpressionExhaustCIDs) { |
| 203 Library& lib = Library::Handle(Library::CoreLibrary()); | 197 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 204 | 198 |
| 205 const String& expression = String::Handle(String::New("3 + 4")); | 199 const String& expression = String::Handle(String::New("3 + 4")); |
| 206 Object& val = Object::Handle(); | 200 Object& val = Object::Handle(); |
| 207 | 201 |
| 208 // Run once to ensure everything we touch is compiled. | 202 // Run once to ensure everything we touch is compiled. |
| 209 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 203 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 210 EXPECT(!val.IsNull()); | 204 EXPECT(!val.IsNull()); |
| 211 EXPECT(!val.IsError()); | 205 EXPECT(!val.IsError()); |
| 212 EXPECT(val.IsInteger()); | 206 EXPECT(val.IsInteger()); |
| 213 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 207 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 214 | 208 |
| 215 intptr_t initial_class_table_size = | 209 intptr_t initial_class_table_size = |
| 216 Isolate::Current()->class_table()->NumCids(); | 210 Isolate::Current()->class_table()->NumCids(); |
| 217 | 211 |
| 218 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 212 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 219 EXPECT(!val.IsNull()); | 213 EXPECT(!val.IsNull()); |
| 220 EXPECT(!val.IsError()); | 214 EXPECT(!val.IsError()); |
| 221 EXPECT(val.IsInteger()); | 215 EXPECT(val.IsInteger()); |
| 222 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 216 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 223 | 217 |
| 224 intptr_t final_class_table_size = | 218 intptr_t final_class_table_size = |
| 225 Isolate::Current()->class_table()->NumCids(); | 219 Isolate::Current()->class_table()->NumCids(); |
| 226 // Eval should not eat into this non-renewable resource. | 220 // Eval should not eat into this non-renewable resource. |
| 227 EXPECT_EQ(initial_class_table_size, final_class_table_size); | 221 EXPECT_EQ(initial_class_table_size, final_class_table_size); |
| 228 } | 222 } |
| 229 | 223 |
| 230 } // namespace dart | 224 } // namespace dart |
| OLD | NEW |