| 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, | 27 Script& script = Script::Handle(Script::New(url, |
| 27 source, | 28 source, |
| 28 RawScript::kScriptTag)); | 29 RawScript::kScriptTag)); |
| 29 Library& lib = Library::Handle(Library::CoreLibrary()); | 30 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 30 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 31 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 31 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 32 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 32 Class& cls = Class::Handle( | 33 Class& cls = Class::Handle( |
| 33 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 34 lib.LookupClass(String::Handle(Symbols::New("A")))); |
| 34 EXPECT(!cls.IsNull()); | 35 EXPECT(!cls.IsNull()); |
| 35 String& name = String::Handle(String::New("foo")); | 36 String& name = String::Handle(String::New("foo")); |
| 36 Function& function = Function::Handle(cls.LookupStaticFunction(name)); | 37 Function& function = Function::Handle(cls.LookupStaticFunction(name)); |
| 37 EXPECT(!function.IsNull()); | 38 EXPECT(!function.IsNull()); |
| 38 | 39 |
| 39 EXPECT(CompilerTest::TestCompileFunction(function)); | 40 EXPECT(CompilerTest::TestCompileFunction(function)); |
| 40 EXPECT(function.HasCode()); | 41 EXPECT(function.HasCode()); |
| 41 GrowableArray<const Object*> arguments; | 42 GrowableArray<const Object*> arguments; |
| 42 const Array& kNoArgumentNames = Array::Handle(); | 43 const Array& kNoArgumentNames = Array::Handle(); |
| 43 const Smi& retval = Smi::Handle( | 44 const Smi& retval = Smi::Handle( |
| (...skipping 11 matching lines...) Expand all Loading... |
| 55 "}\n"; | 56 "}\n"; |
| 56 String& url = String::Handle(String::New("dart-test:DartEntry")); | 57 String& url = String::Handle(String::New("dart-test:DartEntry")); |
| 57 String& source = String::Handle(String::New(kScriptChars)); | 58 String& source = String::Handle(String::New(kScriptChars)); |
| 58 Script& script = Script::Handle(Script::New(url, | 59 Script& script = Script::Handle(Script::New(url, |
| 59 source, | 60 source, |
| 60 RawScript::kScriptTag)); | 61 RawScript::kScriptTag)); |
| 61 Library& lib = Library::Handle(Library::CoreLibrary()); | 62 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 62 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 63 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 63 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 64 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 64 Class& cls = Class::Handle( | 65 Class& cls = Class::Handle( |
| 65 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 66 lib.LookupClass(String::Handle(Symbols::New("A")))); |
| 66 EXPECT(!cls.IsNull()); | 67 EXPECT(!cls.IsNull()); |
| 67 String& name = String::Handle(String::New("foo")); | 68 String& name = String::Handle(String::New("foo")); |
| 68 Function& function = Function::Handle(cls.LookupStaticFunction(name)); | 69 Function& function = Function::Handle(cls.LookupStaticFunction(name)); |
| 69 EXPECT(!function.IsNull()); | 70 EXPECT(!function.IsNull()); |
| 70 GrowableArray<const Object*> arguments; | 71 GrowableArray<const Object*> arguments; |
| 71 const Array& kNoArgumentNames = Array::Handle(); | 72 const Array& kNoArgumentNames = Array::Handle(); |
| 72 const Object& retval = Object::Handle( | 73 const Object& retval = Object::Handle( |
| 73 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); | 74 DartEntry::InvokeStatic(function, arguments, kNoArgumentNames)); |
| 74 EXPECT(retval.IsError()); | 75 EXPECT(retval.IsError()); |
| 75 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 76 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
| 76 } | 77 } |
| 77 | 78 |
| 78 | 79 |
| 79 TEST_CASE(InvokeDynamic_CompileError) { | 80 TEST_CASE(InvokeDynamic_CompileError) { |
| 80 const char* kScriptChars = | 81 const char* kScriptChars = |
| 81 "class A {\n" | 82 "class A {\n" |
| 82 " foo() { return ++++; }\n" | 83 " foo() { return ++++; }\n" |
| 83 "}\n"; | 84 "}\n"; |
| 84 String& url = String::Handle(String::New("dart-test:DartEntry")); | 85 String& url = String::Handle(String::New("dart-test:DartEntry")); |
| 85 String& source = String::Handle(String::New(kScriptChars)); | 86 String& source = String::Handle(String::New(kScriptChars)); |
| 86 Script& script = Script::Handle(Script::New(url, | 87 Script& script = Script::Handle(Script::New(url, |
| 87 source, | 88 source, |
| 88 RawScript::kScriptTag)); | 89 RawScript::kScriptTag)); |
| 89 Library& lib = Library::Handle(Library::CoreLibrary()); | 90 Library& lib = Library::Handle(Library::CoreLibrary()); |
| 90 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); | 91 EXPECT_EQ(true, CompilerTest::TestCompileScript(lib, script)); |
| 91 EXPECT(ClassFinalizer::FinalizePendingClasses()); | 92 EXPECT(ClassFinalizer::FinalizePendingClasses()); |
| 92 Class& cls = Class::Handle( | 93 Class& cls = Class::Handle( |
| 93 lib.LookupClass(String::Handle(String::NewSymbol("A")))); | 94 lib.LookupClass(String::Handle(Symbols::New("A")))); |
| 94 EXPECT(!cls.IsNull()); | 95 EXPECT(!cls.IsNull()); |
| 95 | 96 |
| 96 // Invoke the constructor. | 97 // Invoke the constructor. |
| 97 const Instance& instance = Instance::Handle(Instance::New(cls)); | 98 const Instance& instance = Instance::Handle(Instance::New(cls)); |
| 98 GrowableArray<const Object*> constructor_arguments(2); | 99 GrowableArray<const Object*> constructor_arguments(2); |
| 99 constructor_arguments.Add(&instance); | 100 constructor_arguments.Add(&instance); |
| 100 constructor_arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll))); | 101 constructor_arguments.Add(&Smi::Handle(Smi::New(Function::kCtorPhaseAll))); |
| 101 String& constructor_name = String::Handle(String::NewSymbol("A.")); | 102 String& constructor_name = String::Handle(Symbols::New("A.")); |
| 102 Function& constructor = | 103 Function& constructor = |
| 103 Function::Handle(cls.LookupConstructor(constructor_name)); | 104 Function::Handle(cls.LookupConstructor(constructor_name)); |
| 104 ASSERT(!constructor.IsNull()); | 105 ASSERT(!constructor.IsNull()); |
| 105 const Array& kNoArgumentNames = Array::Handle(); | 106 const Array& kNoArgumentNames = Array::Handle(); |
| 106 DartEntry::InvokeStatic(constructor, constructor_arguments, kNoArgumentNames); | 107 DartEntry::InvokeStatic(constructor, constructor_arguments, kNoArgumentNames); |
| 107 | 108 |
| 108 // Call foo. | 109 // Call foo. |
| 109 String& name = String::Handle(String::New("foo")); | 110 String& name = String::Handle(String::New("foo")); |
| 110 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); | 111 Function& function = Function::Handle(cls.LookupDynamicFunction(name)); |
| 111 EXPECT(!function.IsNull()); | 112 EXPECT(!function.IsNull()); |
| 112 GrowableArray<const Object*> arguments; | 113 GrowableArray<const Object*> arguments; |
| 113 const Object& retval = Object::Handle( | 114 const Object& retval = Object::Handle( |
| 114 DartEntry::InvokeDynamic( | 115 DartEntry::InvokeDynamic( |
| 115 instance, function, arguments, kNoArgumentNames)); | 116 instance, function, arguments, kNoArgumentNames)); |
| 116 EXPECT(retval.IsError()); | 117 EXPECT(retval.IsError()); |
| 117 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); | 118 EXPECT_SUBSTRING("++++", Error::Cast(retval).ToErrorCString()); |
| 118 } | 119 } |
| 119 | 120 |
| 120 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64. | 121 #endif // TARGET_ARCH_IA32 || TARGET_ARCH_X64. |
| 121 | 122 |
| 122 } // namespace dart | 123 } // namespace dart |
| OLD | NEW |