| 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/symbols.h" | 11 #include "vm/symbols.h" |
| 12 #include "vm/thread_pool.h" |
| 12 #include "vm/unit_test.h" | 13 #include "vm/unit_test.h" |
| 13 | 14 |
| 14 namespace dart { | 15 namespace dart { |
| 15 | 16 |
| 16 TEST_CASE(CompileScript) { | 17 TEST_CASE(CompileScript) { |
| 17 const char* kScriptChars = | 18 const char* kScriptChars = |
| 18 "class A {\n" | 19 "class A {\n" |
| 19 " static foo() { return 42; }\n" | 20 " static foo() { return 42; }\n" |
| 20 "}\n"; | 21 "}\n"; |
| 21 String& url = String::Handle(String::New("dart-test:CompileScript")); | 22 String& url = String::Handle(String::New("dart-test:CompileScript")); |
| 22 String& source = String::Handle(String::New(kScriptChars)); | 23 String& source = String::Handle(String::New(kScriptChars)); |
| 23 Script& script = Script::Handle(Script::New(url, | 24 Script& script = Script::Handle(Script::New(url, |
| 24 source, | 25 source, |
| 25 RawScript::kScriptTag)); | 26 RawScript::kScriptTag)); |
| 26 Library& lib = Library::Handle(Library::CoreLibrary()); | 27 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 27 EXPECT(CompilerTest::TestCompileScript(lib, script)); | 28 EXPECT(CompilerTest::TestCompileScript(lib, script)); |
| 28 } | 29 } |
| 29 | 30 |
| 30 | 31 |
| 31 TEST_CASE(CompileFunction) { | 32 static void CompileFunctionImpl() { |
| 32 const char* kScriptChars = | 33 const char* kScriptChars = |
| 33 "class A {\n" | 34 "class A {\n" |
| 34 " static foo() { return 42; }\n" | 35 " static foo() { return 42; }\n" |
| 35 " static moo() {\n" | 36 " static moo() {\n" |
| 36 " // A.foo();\n" | 37 " // A.foo();\n" |
| 37 " }\n" | 38 " }\n" |
| 38 "}\n"; | 39 "}\n"; |
| 39 String& url = String::Handle(String::New("dart-test:CompileFunction")); | 40 String& url = String::Handle(String::New("dart-test:CompileFunction")); |
| 40 String& source = String::Handle(String::New(kScriptChars)); | 41 String& source = String::Handle(String::New(kScriptChars)); |
| 41 Script& script = Script::Handle(Script::New(url, | 42 Script& script = Script::Handle(Script::New(url, |
| (...skipping 20 matching lines...) Expand all Loading... |
| 62 EXPECT(!function_moo.IsNull()); | 63 EXPECT(!function_moo.IsNull()); |
| 63 | 64 |
| 64 EXPECT(CompilerTest::TestCompileFunction(function_moo)); | 65 EXPECT(CompilerTest::TestCompileFunction(function_moo)); |
| 65 EXPECT(function_moo.HasCode()); | 66 EXPECT(function_moo.HasCode()); |
| 66 function_source = function_moo.GetSource(); | 67 function_source = function_moo.GetSource(); |
| 67 EXPECT_STREQ("static moo() {\n // A.foo();\n }", | 68 EXPECT_STREQ("static moo() {\n // A.foo();\n }", |
| 68 function_source.ToCString()); | 69 function_source.ToCString()); |
| 69 } | 70 } |
| 70 | 71 |
| 71 | 72 |
| 73 TEST_CASE(CompileFunction) { |
| 74 CompileFunctionImpl(); |
| 75 } |
| 76 |
| 77 |
| 78 // Runs 'CompileFunctionImpl' on a helper thread. |
| 79 class CompileFunctionTask : public ThreadPool::Task { |
| 80 public: |
| 81 CompileFunctionTask(Isolate* isolate, |
| 82 Monitor* done_monitor, |
| 83 bool* done) |
| 84 : isolate_(isolate), |
| 85 done_monitor_(done_monitor), |
| 86 done_(done) { |
| 87 } |
| 88 |
| 89 virtual void Run() { |
| 90 Thread::EnterIsolateAsHelper(isolate_); |
| 91 { |
| 92 Thread* thread = Thread::Current(); |
| 93 StackZone stack_zone(thread); |
| 94 HANDLESCOPE(thread); |
| 95 CompileFunctionImpl(); |
| 96 } |
| 97 Thread::ExitIsolateAsHelper(); |
| 98 // Tell main thread that we are done. |
| 99 { |
| 100 MonitorLocker ml(done_monitor_); |
| 101 ASSERT(!*done_); |
| 102 *done_ = true; |
| 103 ml.Notify(); |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 Isolate* isolate_; |
| 109 Monitor* done_monitor_; |
| 110 bool* done_; |
| 111 }; |
| 112 |
| 113 |
| 114 TEST_CASE(CompileFunctionOnHelperThread) { |
| 115 Monitor done_monitor; |
| 116 bool done = false; |
| 117 Isolate* isolate = Thread::Current()->isolate(); |
| 118 // Flush store buffers, etc. |
| 119 // TODO(koda): Currently, the GC only does this for the current thread, (i.e, |
| 120 // the helper, in this test), but it should be done for all *threads* |
| 121 // after/at safepointing. |
| 122 Thread::PrepareForGC(); |
| 123 Dart::thread_pool()->Run( |
| 124 new CompileFunctionTask(isolate, &done_monitor, &done)); |
| 125 { |
| 126 // Manually wait. |
| 127 // TODO(koda): Replace with execution of Dart and/or VM code when GC |
| 128 // actually safepoints everything. |
| 129 MonitorLocker ml(&done_monitor); |
| 130 while (!done) { |
| 131 ml.Wait(); |
| 132 } |
| 133 } |
| 134 } |
| 135 |
| 136 |
| 72 TEST_CASE(RegenerateAllocStubs) { | 137 TEST_CASE(RegenerateAllocStubs) { |
| 73 const char* kScriptChars = | 138 const char* kScriptChars = |
| 74 "class A {\n" | 139 "class A {\n" |
| 75 "}\n" | 140 "}\n" |
| 76 "unOpt() => new A(); \n" | 141 "unOpt() => new A(); \n" |
| 77 "optIt() => new A(); \n" | 142 "optIt() => new A(); \n" |
| 78 "A main() {\n" | 143 "A main() {\n" |
| 79 " return unOpt();\n" | 144 " return unOpt();\n" |
| 80 "}\n"; | 145 "}\n"; |
| 81 | 146 |
| (...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 177 EXPECT(val.IsInteger()); | 242 EXPECT(val.IsInteger()); |
| 178 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); | 243 EXPECT_EQ(7, Integer::Cast(val).AsInt64Value()); |
| 179 | 244 |
| 180 intptr_t final_class_table_size = | 245 intptr_t final_class_table_size = |
| 181 Isolate::Current()->class_table()->NumCids(); | 246 Isolate::Current()->class_table()->NumCids(); |
| 182 // Eval should not eat into this non-renewable resource. | 247 // Eval should not eat into this non-renewable resource. |
| 183 EXPECT_EQ(initial_class_table_size, final_class_table_size); | 248 EXPECT_EQ(initial_class_table_size, final_class_table_size); |
| 184 } | 249 } |
| 185 | 250 |
| 186 } // namespace dart | 251 } // namespace dart |
| OLD | NEW |