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/unit_test.h" | 13 #include "vm/unit_test.h" |
13 | 14 |
14 namespace dart { | 15 namespace dart { |
15 | 16 |
16 // Only ia32 and x64 can run execution tests. | 17 // Only ia32 and x64 can run execution tests. |
17 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) | 18 #if defined(TARGET_ARCH_IA32) || defined(TARGET_ARCH_X64) |
18 | 19 |
19 TEST_CASE(DartEntry) { | 20 TEST_CASE(DartEntry) { |
20 const char* kScriptChars = | 21 const char* kScriptChars = |
21 "class A {\n" | 22 "class A {\n" |
22 " static foo() { return 42; }\n" | 23 " static foo() { return 42; }\n" |
23 "}\n"; | 24 "}\n"; |
24 String& url = String::Handle(String::New("dart-test:DartEntry")); | 25 String& url = String::Handle(String::New("dart-test:DartEntry")); |
25 String& source = String::Handle(String::New(kScriptChars)); | 26 String& source = String::Handle(String::New(kScriptChars)); |
26 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); | 27 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); |
27 Library& lib = Library::Handle(Library::CoreLibrary()); | 28 Library& lib = Library::Handle(Library::CoreLibrary()); |
28 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 29 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
29 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 30 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
30 Class& cls = Class::Handle( | 31 Class& cls = Class::Handle( |
31 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 32 lib.LookupClass(String::Handle(Symbols::New("A")))); |
32 EXPECT(!cls.IsNull()); | 33 EXPECT(!cls.IsNull()); |
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 GrowableArray<const Object*> arguments; | 40 GrowableArray<const Object*> arguments; |
40 const Array& kNoArgumentNames = Array::Handle(); | 41 const Array& kNoArgumentNames = Array::Handle(); |
41 const Smi& retval = Smi::Handle( | 42 const Smi& retval = Smi::Handle( |
42 reinterpret_cast<RawSmi*>(DartEntry::InvokeStatic(function, | 43 reinterpret_cast<RawSmi*>(DartEntry::InvokeStatic(function, |
43 arguments, | 44 arguments, |
44 kNoArgumentNames))); | 45 kNoArgumentNames))); |
45 EXPECT_EQ(Smi::New(42), retval.raw()); | 46 EXPECT_EQ(Smi::New(42), retval.raw()); |
46 } | 47 } |
47 | 48 |
48 | 49 |
49 TEST_CASE(InvokeStatic_CompileError) { | 50 TEST_CASE(InvokeStatic_CompileError) { |
50 const char* kScriptChars = | 51 const char* kScriptChars = |
51 "class A {\n" | 52 "class A {\n" |
52 " static foo() { return ++++; }\n" | 53 " static foo() { return ++++; }\n" |
53 "}\n"; | 54 "}\n"; |
54 String& url = String::Handle(String::New("dart-test:DartEntry")); | 55 String& url = String::Handle(String::New("dart-test:DartEntry")); |
55 String& source = String::Handle(String::New(kScriptChars)); | 56 String& source = String::Handle(String::New(kScriptChars)); |
56 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); | 57 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); |
57 Library& lib = Library::Handle(Library::CoreLibrary()); | 58 Library& lib = Library::Handle(Library::CoreLibrary()); |
58 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 59 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
59 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 60 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
60 Class& cls = Class::Handle( | 61 Class& cls = Class::Handle( |
61 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 62 lib.LookupClass(String::Handle(Symbols::New("A")))); |
62 EXPECT(!cls.IsNull()); | 63 EXPECT(!cls.IsNull()); |
63 String& name = String::Handle(String::New("foo")); | 64 String& name = String::Handle(String::New("foo")); |
64 Function& function = Function::Handle(cls.LookupStaticFunction(name)); | 65 Function& function = Function::Handle(cls.LookupStaticFunction(name)); |
65 EXPECT(!function.IsNull()); | 66 EXPECT(!function.IsNull()); |
66 GrowableArray<const Object*> arguments; | 67 GrowableArray<const Object*> arguments; |
67 const Array& kNoArgumentNames = Array::Handle(); | 68 const Array& kNoArgumentNames = Array::Handle(); |
68 const Object& retval = Object::Handle( | 69 const Object& retval = Object::Handle( |
69 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); | 70 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); |
70 EXPECT(retval.IsError()); | 71 EXPECT(retval.IsError()); |
71 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 72 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
72 } | 73 } |
73 | 74 |
74 | 75 |
75 TEST_CASE(InvokeDynamic_CompileError) { | 76 TEST_CASE(InvokeDynamic_CompileError) { |
76 const char* kScriptChars = | 77 const char* kScriptChars = |
77 "class A {\n" | 78 "class A {\n" |
78 " foo() { return ++++; }\n" | 79 " foo() { return ++++; }\n" |
79 "}\n"; | 80 "}\n"; |
80 String& url = String::Handle(String::New("dart-test:DartEntry")); | 81 String& url = String::Handle(String::New("dart-test:DartEntry")); |
81 String& source = String::Handle(String::New(kScriptChars)); | 82 String& source = String::Handle(String::New(kScriptChars)); |
82 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); | 83 Script& script = Script::Handle(Script::New(url, source, RawScript::kScript)); |
83 Library& lib = Library::Handle(Library::CoreLibrary()); | 84 Library& lib = Library::Handle(Library::CoreLibrary()); |
84 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 85 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
85 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 86 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
86 Class& cls = Class::Handle( | 87 Class& cls = Class::Handle( |
87 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 88 lib.LookupClass(String::Handle(Symbols::New("A")))); |
88 EXPECT(!cls.IsNull()); | 89 EXPECT(!cls.IsNull()); |
89 | 90 |
90 // Invoke the constructor. | 91 // Invoke the constructor. |
91 const Instance& instance = Instance::Handle(Instance::New(cls)); | 92 const Instance& instance = Instance::Handle(Instance::New(cls)); |
92 GrowableArray<const Object*> constructor_arguments(2); | 93 GrowableArray<const Object*> constructor_arguments(2); |
93 constructor_arguments.Add(&instance); | 94 constructor_arguments.Add(&instance); |
94 constructor_arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll))); | 95 constructor_arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll))); |
95 String& constructor_name = String::Handle(String::NewSymbol("A.")); | 96 String& constructor_name = String::Handle(Symbols::New("A.")); |
96 Function& constructor = | 97 Function& constructor = |
97 Function::Handle(cls.LookupConstructor(constructor_name)); | 98 Function::Handle(cls.LookupConstructor(constructor_name)); |
98 ASSERT(!constructor.IsNull()); | 99 ASSERT(!constructor.IsNull()); |
99 const Array& kNoArgumentNames = Array::Handle(); | 100 const Array& kNoArgumentNames = Array::Handle(); |
100 DartEntry::InvokeStatic(constructor, constructor_arguments, kNoArgumentNames); | 101 DartEntry::InvokeStatic(constructor, constructor_arguments, kNoArgumentNames); |
101 | 102 |
102 // Call foo. | 103 // Call foo. |
103 String& name = String::Handle(String::New("foo")); | 104 String& name = String::Handle(String::New("foo")); |
104 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); | 105 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); |
105 EXPECT(!function.IsNull()); | 106 EXPECT(!function.IsNull()); |
106 GrowableArray<const Object*> arguments; | 107 GrowableArray<const Object*> arguments; |
107 const Object& retval = Object::Handle( | 108 const Object& retval = Object::Handle( |
108 DartEntry::InvokeDynamic( | 109 DartEntry::InvokeDynamic( |
109 instance, function, arguments, kNoArgumentNames)); | 110 instance, function, arguments, kNoArgumentNames)); |
110 EXPECT(retval.IsError()); | 111 EXPECT(retval.IsError()); |
111 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 112 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
112 } | 113 } |
113 | 114 |
114 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64. | 115 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64. |
115 | 116 |
116 } // namespace dart | 117 } // namespace dart |
OLD | NEW |