| 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/assembler.h" | 6 #include "vm/assembler.h" |
| 7 #include "vm/class_finalizer.h" | 7 #include "vm/class_finalizer.h" |
| 8 #include "vm/compiler.h" | 8 #include "vm/compiler.h" |
| 9 #include "vm/dart_entry.h" | 9 #include "vm/dart_entry.h" |
| 10 #include "vm/object.h" | 10 #include "vm/object.h" |
| 11 #include "vm/resolver.h" | 11 #include "vm/resolver.h" |
| 12 #include "vm/symbols.h" | 12 #include "vm/symbols.h" |
| 13 #include "vm/unit_test.h" | 13 #include "vm/unit_test.h" |
| 14 | 14 |
| 15 namespace dart { | 15 namespace dart { |
| 16 | 16 |
| 17 TEST_CASE(DartEntry) { | 17 TEST_CASE(DartEntry) { |
| 18 const char* kScriptChars = | 18 const char* kScriptChars = |
| 19 "class A {\n" | 19 "class A {\n" |
| 20 " static foo() { return 42; }\n" | 20 " static foo() { return 42; }\n" |
| 21 "}\n"; | 21 "}\n"; |
| 22 String& url = String::Handle(String::New("dart-test:DartEntry")); | 22 String& url = String::Handle(String::New("dart-test:DartEntry")); |
| 23 String& source = String::Handle(String::New(kScriptChars)); | 23 String& source = String::Handle(String::New(kScriptChars)); |
| 24 Script& script = Script::Handle(Script::New(url, | 24 Script& script = Script::Handle(Script::New(url, |
| 25 source, | 25 source, |
| 26 RawScript::kScriptTag)); | 26 RawScript::kScriptTag)); |
| 27 Library& lib = Library::Handle(Library::CoreLibrary()); | 27 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 28 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 28 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 29 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 29 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 30 String& ambiguity_error_msg = String::Handle(); |
| 30 Class& cls = Class::Handle( | 31 Class& cls = Class::Handle( |
| 31 lib.LookupClass(String::Handle(Symbols::New("A")))); | 32 lib.LookupClass(String::Handle(Symbols::New("A")), &ambiguity_error_msg)); |
| 32 EXPECT(!cls.IsNull()); | 33 EXPECT(!cls.IsNull()); // No ambiguity error expected. |
| 33 String& name = String::Handle(String::New("foo")); | 34 String& name = String::Handle(String::New("foo")); |
| 34 Function& function = Function::Handle(cls.LookupStaticFunction(name)); | 35 Function& function = Function::Handle(cls.LookupStaticFunction(name)); |
| 35 EXPECT(!function.IsNull()); | 36 EXPECT(!function.IsNull()); |
| 36 | 37 |
| 37 EXPECT(CompilerTest::TestCompileFunction(function)); | 38 EXPECT(CompilerTest::TestCompileFunction(function)); |
| 38 EXPECT(function.HasCode()); | 39 EXPECT(function.HasCode()); |
| 39 const Smi& retval = Smi::Handle(reinterpret_cast<RawSmi*>( | 40 const Smi& retval = Smi::Handle(reinterpret_cast<RawSmi*>( |
| 40 DartEntry::InvokeFunction(function, Object::empty_array()))); | 41 DartEntry::InvokeFunction(function, Object::empty_array()))); |
| 41 EXPECT_EQ(Smi::New(42), retval.raw()); | 42 EXPECT_EQ(Smi::New(42), retval.raw()); |
| 42 } | 43 } |
| 43 | 44 |
| 44 | 45 |
| 45 TEST_CASE(InvokeStatic_CompileError) { | 46 TEST_CASE(InvokeStatic_CompileError) { |
| 46 const char* kScriptChars = | 47 const char* kScriptChars = |
| 47 "class A {\n" | 48 "class A {\n" |
| 48 " static foo() { return ++++; }\n" | 49 " static foo() { return ++++; }\n" |
| 49 "}\n"; | 50 "}\n"; |
| 50 String& url = String::Handle(String::New("dart-test:DartEntry")); | 51 String& url = String::Handle(String::New("dart-test:DartEntry")); |
| 51 String& source = String::Handle(String::New(kScriptChars)); | 52 String& source = String::Handle(String::New(kScriptChars)); |
| 52 Script& script = Script::Handle(Script::New(url, | 53 Script& script = Script::Handle(Script::New(url, |
| 53 source, | 54 source, |
| 54 RawScript::kScriptTag)); | 55 RawScript::kScriptTag)); |
| 55 Library& lib = Library::Handle(Library::CoreLibrary()); | 56 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 56 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 57 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 57 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 58 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 59 String& ambiguity_error_msg = String::Handle(); |
| 58 Class& cls = Class::Handle( | 60 Class& cls = Class::Handle( |
| 59 lib.LookupClass(String::Handle(Symbols::New("A")))); | 61 lib.LookupClass(String::Handle(Symbols::New("A")), &ambiguity_error_msg)); |
| 60 EXPECT(!cls.IsNull()); | 62 EXPECT(!cls.IsNull()); // No ambiguity error expected. |
| 61 String& name = String::Handle(String::New("foo")); | 63 String& name = String::Handle(String::New("foo")); |
| 62 Function& function = Function::Handle(cls.LookupStaticFunction(name)); | 64 Function& function = Function::Handle(cls.LookupStaticFunction(name)); |
| 63 EXPECT(!function.IsNull()); | 65 EXPECT(!function.IsNull()); |
| 64 GrowableArray<const Object*> arguments; | 66 GrowableArray<const Object*> arguments; |
| 65 const Object& retval = Object::Handle( | 67 const Object& retval = Object::Handle( |
| 66 DartEntry::InvokeFunction(function, Object::empty_array())); | 68 DartEntry::InvokeFunction(function, Object::empty_array())); |
| 67 EXPECT(retval.IsError()); | 69 EXPECT(retval.IsError()); |
| 68 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 70 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
| 69 } | 71 } |
| 70 | 72 |
| 71 | 73 |
| 72 TEST_CASE(InvokeDynamic_CompileError) { | 74 TEST_CASE(InvokeDynamic_CompileError) { |
| 73 const char* kScriptChars = | 75 const char* kScriptChars = |
| 74 "class A {\n" | 76 "class A {\n" |
| 75 " foo() { return ++++; }\n" | 77 " foo() { return ++++; }\n" |
| 76 "}\n"; | 78 "}\n"; |
| 77 String& url = String::Handle(String::New("dart-test:DartEntry")); | 79 String& url = String::Handle(String::New("dart-test:DartEntry")); |
| 78 String& source = String::Handle(String::New(kScriptChars)); | 80 String& source = String::Handle(String::New(kScriptChars)); |
| 79 Script& script = Script::Handle(Script::New(url, | 81 Script& script = Script::Handle(Script::New(url, |
| 80 source, | 82 source, |
| 81 RawScript::kScriptTag)); | 83 RawScript::kScriptTag)); |
| 82 Library& lib = Library::Handle(Library::CoreLibrary()); | 84 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 83 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 85 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 84 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 86 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 87 String& ambiguity_error_msg = String::Handle(); |
| 85 Class& cls = Class::Handle( | 88 Class& cls = Class::Handle( |
| 86 lib.LookupClass(String::Handle(Symbols::New("A")))); | 89 lib.LookupClass(String::Handle(Symbols::New("A")), &ambiguity_error_msg)); |
| 87 EXPECT(!cls.IsNull()); | 90 EXPECT(!cls.IsNull()); // No ambiguity error expected. |
| 88 | 91 |
| 89 // Invoke the constructor. | 92 // Invoke the constructor. |
| 90 const Instance& instance = Instance::Handle(Instance::New(cls)); | 93 const Instance& instance = Instance::Handle(Instance::New(cls)); |
| 91 const Array& constructor_arguments = Array::Handle(Array::New(2)); | 94 const Array& constructor_arguments = Array::Handle(Array::New(2)); |
| 92 constructor_arguments.SetAt(0, instance); | 95 constructor_arguments.SetAt(0, instance); |
| 93 constructor_arguments.SetAt( | 96 constructor_arguments.SetAt( |
| 94 1, Smi::Handle(Smi::New(Function::kCtorPhaseAll))); | 97 1, Smi::Handle(Smi::New(Function::kCtorPhaseAll))); |
| 95 String& constructor_name = String::Handle(Symbols::New("A.")); | 98 String& constructor_name = String::Handle(Symbols::New("A.")); |
| 96 Function& constructor = | 99 Function& constructor = |
| 97 Function::Handle(cls.LookupConstructor(constructor_name)); | 100 Function::Handle(cls.LookupConstructor(constructor_name)); |
| 98 ASSERT(!constructor.IsNull()); | 101 ASSERT(!constructor.IsNull()); |
| 99 DartEntry::InvokeFunction(constructor, constructor_arguments); | 102 DartEntry::InvokeFunction(constructor, constructor_arguments); |
| 100 | 103 |
| 101 // Call foo. | 104 // Call foo. |
| 102 String& name = String::Handle(String::New("foo")); | 105 String& name = String::Handle(String::New("foo")); |
| 103 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); | 106 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); |
| 104 EXPECT(!function.IsNull()); | 107 EXPECT(!function.IsNull()); |
| 105 const Array& args = Array::Handle(Array::New(1)); | 108 const Array& args = Array::Handle(Array::New(1)); |
| 106 args.SetAt(0, instance); | 109 args.SetAt(0, instance); |
| 107 const Object& retval = Object::Handle(DartEntry::InvokeFunction(function, | 110 const Object& retval = Object::Handle(DartEntry::InvokeFunction(function, |
| 108 args)); | 111 args)); |
| 109 EXPECT(retval.IsError()); | 112 EXPECT(retval.IsError()); |
| 110 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 113 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
| 111 } | 114 } |
| 112 | 115 |
| 113 } // namespace dart | 116 } // namespace dart |
| OLD | NEW |