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