| 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 "platform/assert.h" | 5 #include "platform/assert.h" |
| 6 #include "vm/class_finalizer.h" | 6 #include "vm/class_finalizer.h" |
| 7 #include "vm/code_patcher.h" | 7 #include "vm/code_patcher.h" |
| 8 #include "vm/compiler.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/symbols.h" | 12 #include "vm/symbols.h" |
| 12 #include "vm/thread_pool.h" | 13 #include "vm/thread_pool.h" |
| 13 #include "vm/thread_registry.h" | |
| 14 #include "vm/unit_test.h" | 14 #include "vm/unit_test.h" |
| 15 | 15 |
| 16 namespace dart { | 16 namespace dart { |
| 17 | 17 |
| 18 DECLARE_FLAG(bool, background_compilation); | 18 DECLARE_FLAG(bool, background_compilation); |
| 19 | 19 |
| 20 TEST_CASE(CompileScript) { | 20 VM_TEST_CASE(CompileScript) { |
| 21 const char* kScriptChars = | 21 const char* kScriptChars = |
| 22 "class A {\n" | 22 "class A {\n" |
| 23 " static foo() { return 42; }\n" | 23 " static foo() { return 42; }\n" |
| 24 "}\n"; | 24 "}\n"; |
| 25 String& url = String::Handle(String::New("dart-test:CompileScript")); | 25 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 26 String& source = String::Handle(String::New(kScriptChars)); | 26 String& source = String::Handle(String::New(kScriptChars)); |
| 27 Script& script = Script::Handle(Script::New(url, | 27 Script& script = Script::Handle(Script::New(url, |
| 28 source, | 28 source, |
| 29 RawScript::kScriptTag)); | 29 RawScript::kScriptTag)); |
| 30 Library& lib = Library::Handle(Library::CoreLibrary()); | 30 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 31 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 31 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 32 } | 32 } |
| 33 | 33 |
| 34 | 34 |
| 35 TEST_CASE(CompileFunction) { | 35 VM_TEST_CASE(CompileFunction) { |
| 36 const char* kScriptChars = | 36 const char* kScriptChars = |
| 37 "class A {\n" | 37 "class A {\n" |
| 38 " static foo() { return 42; }\n" | 38 " static foo() { return 42; }\n" |
| 39 " static moo() {\n" | 39 " static moo() {\n" |
| 40 " // A.foo();\n" | 40 " // A.foo();\n" |
| 41 " }\n" | 41 " }\n" |
| 42 "}\n"; | 42 "}\n"; |
| 43 String& url = String::Handle(String::New("dart-test:CompileFunction")); | 43 String& url = String::Handle(String::New("dart-test:CompileFunction")); |
| 44 String& source = String::Handle(String::New(kScriptChars)); | 44 String& source = String::Handle(String::New(kScriptChars)); |
| 45 Script& script = Script::Handle(Script::New(url, | 45 Script& script = Script::Handle(Script::New(url, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 66 EXPECT(!function_moo.IsNull()); | 66 EXPECT(!function_moo.IsNull()); |
| 67 | 67 |
| 68 EXPECT(CompilerTest::TestCompileFunction(function_moo)); | 68 EXPECT(CompilerTest::TestCompileFunction(function_moo)); |
| 69 EXPECT(function_moo.HasCode()); | 69 EXPECT(function_moo.HasCode()); |
| 70 function_source = function_moo.GetSource(); | 70 function_source = function_moo.GetSource(); |
| 71 EXPECT_STREQ("static moo() {\n // A.foo();\n }", | 71 EXPECT_STREQ("static moo() {\n // A.foo();\n }", |
| 72 function_source.ToCString()); | 72 function_source.ToCString()); |
| 73 } | 73 } |
| 74 | 74 |
| 75 | 75 |
| 76 TEST_CASE(CompileFunctionOnHelperThread) { | 76 VM_TEST_CASE(CompileFunctionOnHelperThread) { |
| 77 // Create a simple function and compile it without optimization. | 77 // Create a simple function and compile it without optimization. |
| 78 const char* kScriptChars = | 78 const char* kScriptChars = |
| 79 "class A {\n" | 79 "class A {\n" |
| 80 " static foo() { return 42; }\n" | 80 " static foo() { return 42; }\n" |
| 81 "}\n"; | 81 "}\n"; |
| 82 String& url = | 82 String& url = |
| 83 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); | 83 String::Handle(String::New("dart-test:CompileFunctionOnHelperThread")); |
| 84 String& source = String::Handle(String::New(kScriptChars)); | 84 String& source = String::Handle(String::New(kScriptChars)); |
| 85 Script& script = Script::Handle(Script::New(url, | 85 Script& script = Script::Handle(Script::New(url, |
| 86 source, | 86 source, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 99 EXPECT(func.HasCode()); | 99 EXPECT(func.HasCode()); |
| 100 EXPECT(!func.HasOptimizedCode()); | 100 EXPECT(!func.HasOptimizedCode()); |
| 101 FLAG_background_compilation = true; | 101 FLAG_background_compilation = true; |
| 102 BackgroundCompiler::EnsureInit(thread); | 102 BackgroundCompiler::EnsureInit(thread); |
| 103 Isolate* isolate = thread->isolate(); | 103 Isolate* isolate = thread->isolate(); |
| 104 ASSERT(isolate->background_compiler() != NULL); | 104 ASSERT(isolate->background_compiler() != NULL); |
| 105 isolate->background_compiler()->CompileOptimized(func); | 105 isolate->background_compiler()->CompileOptimized(func); |
| 106 Monitor* m = new Monitor(); | 106 Monitor* m = new Monitor(); |
| 107 MonitorLocker ml(m); | 107 MonitorLocker ml(m); |
| 108 while (!func.HasOptimizedCode()) { | 108 while (!func.HasOptimizedCode()) { |
| 109 Isolate::Current()->thread_registry()->CheckSafepoint(); | 109 ml.WaitWithSafepointCheck(thread, 1); |
| 110 ml.Wait(1); | |
| 111 } | 110 } |
| 112 BackgroundCompiler::Stop(isolate->background_compiler()); | 111 BackgroundCompiler::Stop(isolate->background_compiler()); |
| 113 } | 112 } |
| 114 | 113 |
| 115 | 114 |
| 116 TEST_CASE(RegenerateAllocStubs) { | 115 TEST_CASE(RegenerateAllocStubs) { |
| 117 const char* kScriptChars = | 116 const char* kScriptChars = |
| 118 "class A {\n" | 117 "class A {\n" |
| 119 "}\n" | 118 "}\n" |
| 120 "unOpt() => new A(); \n" | 119 "unOpt() => new A(); \n" |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 " var apa = 'Herr Nilsson'; \n" | 159 " var apa = 'Herr Nilsson'; \n" |
| 161 " calc(x) => '${x*ten}'; \n" | 160 " calc(x) => '${x*ten}'; \n" |
| 162 "} \n" | 161 "} \n" |
| 163 "makeObj() => new A(); \n"; | 162 "makeObj() => new A(); \n"; |
| 164 | 163 |
| 165 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); | 164 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars, NULL); |
| 166 Dart_Handle obj_handle = | 165 Dart_Handle obj_handle = |
| 167 Dart_Invoke(lib, Dart_NewStringFromCString("makeObj"), 0, NULL); | 166 Dart_Invoke(lib, Dart_NewStringFromCString("makeObj"), 0, NULL); |
| 168 EXPECT(!Dart_IsNull(obj_handle)); | 167 EXPECT(!Dart_IsNull(obj_handle)); |
| 169 EXPECT(!Dart_IsError(obj_handle)); | 168 EXPECT(!Dart_IsError(obj_handle)); |
| 169 TransitionNativeToVM transition(thread); |
| 170 const Object& obj = Object::Handle(Api::UnwrapHandle(obj_handle)); | 170 const Object& obj = Object::Handle(Api::UnwrapHandle(obj_handle)); |
| 171 EXPECT(!obj.IsNull()); | 171 EXPECT(!obj.IsNull()); |
| 172 EXPECT(obj.IsInstance()); | 172 EXPECT(obj.IsInstance()); |
| 173 | 173 |
| 174 String& expr_text = String::Handle(); | 174 String& expr_text = String::Handle(); |
| 175 expr_text = String::New("apa + ' ${calc(10)}' + dot"); | 175 expr_text = String::New("apa + ' ${calc(10)}' + dot"); |
| 176 Object& val = Object::Handle(); | 176 Object& val = Object::Handle(); |
| 177 val = Instance::Cast(obj).Evaluate(expr_text, | 177 val = Instance::Cast(obj).Evaluate(expr_text, |
| 178 Array::empty_array(), | 178 Array::empty_array(), |
| 179 Array::empty_array()); | 179 Array::empty_array()); |
| 180 EXPECT(!val.IsNull()); | 180 EXPECT(!val.IsNull()); |
| 181 EXPECT(!val.IsError()); | 181 EXPECT(!val.IsError()); |
| 182 EXPECT(val.IsString()); | 182 EXPECT(val.IsString()); |
| 183 EXPECT_STREQ("Herr Nilsson 100.", val.ToCString()); | 183 EXPECT_STREQ("Herr Nilsson 100.", val.ToCString()); |
| 184 } | 184 } |
| 185 | 185 |
| 186 | 186 |
| 187 TEST_CASE(EvalExpressionWithLazyCompile) { | 187 VM_TEST_CASE(EvalExpressionWithLazyCompile) { |
| 188 Library& lib = Library::Handle(Library::CoreLibrary()); | 188 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 189 | 189 |
| 190 const String& expression = String::Handle(String::New( | 190 const String& expression = String::Handle(String::New( |
| 191 "(){ return (){ return (){ return 3 + 4; }(); }(); }()")); | 191 "(){ return (){ return (){ return 3 + 4; }(); }(); }()")); |
| 192 Object& val = Object::Handle(); | 192 Object& val = Object::Handle(); |
| 193 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 193 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 194 | 194 |
| 195 EXPECT(!val.IsNull()); | 195 EXPECT(!val.IsNull()); |
| 196 EXPECT(!val.IsError()); | 196 EXPECT(!val.IsError()); |
| 197 EXPECT(val.IsInteger()); | 197 EXPECT(val.IsInteger()); |
| 198 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 198 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 199 } | 199 } |
| 200 | 200 |
| 201 | 201 |
| 202 TEST_CASE(EvalExpressionExhaustCIDs) { | 202 VM_TEST_CASE(EvalExpressionExhaustCIDs) { |
| 203 Library& lib = Library::Handle(Library::CoreLibrary()); | 203 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 204 | 204 |
| 205 const String& expression = String::Handle(String::New("3 + 4")); | 205 const String& expression = String::Handle(String::New("3 + 4")); |
| 206 Object& val = Object::Handle(); | 206 Object& val = Object::Handle(); |
| 207 | 207 |
| 208 // Run once to ensure everything we touch is compiled. | 208 // Run once to ensure everything we touch is compiled. |
| 209 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 209 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 210 EXPECT(!val.IsNull()); | 210 EXPECT(!val.IsNull()); |
| 211 EXPECT(!val.IsError()); | 211 EXPECT(!val.IsError()); |
| 212 EXPECT(val.IsInteger()); | 212 EXPECT(val.IsInteger()); |
| 213 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 213 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 214 | 214 |
| 215 intptr_t initial_class_table_size = | 215 intptr_t initial_class_table_size = |
| 216 Isolate::Current()->class_table()->NumCids(); | 216 Isolate::Current()->class_table()->NumCids(); |
| 217 | 217 |
| 218 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); | 218 val = lib.Evaluate(expression, Array::empty_array(), Array::empty_array()); |
| 219 EXPECT(!val.IsNull()); | 219 EXPECT(!val.IsNull()); |
| 220 EXPECT(!val.IsError()); | 220 EXPECT(!val.IsError()); |
| 221 EXPECT(val.IsInteger()); | 221 EXPECT(val.IsInteger()); |
| 222 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 222 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 223 | 223 |
| 224 intptr_t final_class_table_size = | 224 intptr_t final_class_table_size = |
| 225 Isolate::Current()->class_table()->NumCids(); | 225 Isolate::Current()->class_table()->NumCids(); |
| 226 // Eval should not eat into this non-renewable resource. | 226 // Eval should not eat into this non-renewable resource. |
| 227 EXPECT_EQ(initial_class_table_size, final_class_table_size); | 227 EXPECT_EQ(initial_class_table_size, final_class_table_size); |
| 228 } | 228 } |
| 229 | 229 |
| 230 } // namespace dart | 230 } // namespace dart |
| OLD | NEW |