Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(299)

Side by Side Diff: runtime/vm/dart_api_impl_test.cc

Issue 174393004: Add Dart_GetNativeFieldsOfArgument to get all the native fields of a dart (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: Created 6 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "bin/builtin.h" 5 #include "bin/builtin.h"
6 #include "include/dart_api.h" 6 #include "include/dart_api.h"
7 #include "include/dart_debugger_api.h" 7 #include "include/dart_debugger_api.h"
8 #include "include/dart_mirrors_api.h" 8 #include "include/dart_mirrors_api.h"
9 #include "include/dart_native_api.h" 9 #include "include/dart_native_api.h"
10 #include "platform/assert.h" 10 #include "platform/assert.h"
(...skipping 3767 matching lines...) Expand 10 before | Expand all | Expand 10 after
3778 Dart_Handle expected_error = DartUtils::NewError( 3778 Dart_Handle expected_error = DartUtils::NewError(
3779 "'dart:test-lib': Error: line 1 pos 36: " 3779 "'dart:test-lib': Error: line 1 pos 36: "
3780 "class 'NativeFields' is trying to extend a native fields class, " 3780 "class 'NativeFields' is trying to extend a native fields class, "
3781 "but library '%s' has no native resolvers", 3781 "but library '%s' has no native resolvers",
3782 TestCase::url()); 3782 TestCase::url());
3783 EXPECT_SUBSTRING(Dart_GetError(expected_error), Dart_GetError(result)); 3783 EXPECT_SUBSTRING(Dart_GetError(expected_error), Dart_GetError(result));
3784 #endif 3784 #endif
3785 } 3785 }
3786 3786
3787 3787
3788 static const int kTestNumNativeFields = 2;
3789 static const intptr_t kNativeField1Value = 30;
3790 static const intptr_t kNativeField2Value = 40;
3791
3792 void TestNativeFieldsAccess_init(Dart_NativeArguments args) {
3793 Dart_Handle receiver = Dart_GetNativeArgument(args, 0);
3794 Dart_SetNativeInstanceField(receiver, 0, kNativeField1Value);
3795 Dart_SetNativeInstanceField(receiver, 1, kNativeField2Value);
3796 }
3797
3798
3799 void TestNativeFieldsAccess_access(Dart_NativeArguments args) {
3800 intptr_t field_values[kTestNumNativeFields];
3801 Dart_Handle result = Dart_GetNativeFieldsOfArgument(args,
3802 0,
3803 kTestNumNativeFields,
3804 field_values);
3805 EXPECT_VALID(result);
3806 EXPECT_EQ(kNativeField1Value, field_values[0]);
3807 EXPECT_EQ(kNativeField2Value, field_values[1]);
3808 result = Dart_GetNativeFieldsOfArgument(args,
3809 1,
3810 kTestNumNativeFields,
3811 field_values);
3812 EXPECT_VALID(result);
3813 EXPECT_EQ(0, field_values[0]);
3814 EXPECT_EQ(0, field_values[1]);
3815 }
3816
3817
3818 static Dart_NativeFunction TestNativeFieldsAccess_lookup(Dart_Handle name,
3819 int argument_count,
3820 bool* auto_scope) {
3821 ASSERT(auto_scope != NULL);
3822 *auto_scope = true;
3823 const Object& obj = Object::Handle(Api::UnwrapHandle(name));
3824 if (!obj.IsString()) {
3825 return NULL;
3826 }
3827 const char* function_name = obj.ToCString();
3828 ASSERT(function_name != NULL);
3829 if (!strcmp(function_name, "TestNativeFieldsAccess_init")) {
3830 return reinterpret_cast<Dart_NativeFunction>(&TestNativeFieldsAccess_init);
3831 } else if (!strcmp(function_name, "TestNativeFieldsAccess_access")) {
3832 return reinterpret_cast<Dart_NativeFunction>(
3833 &TestNativeFieldsAccess_access);
3834 } else {
3835 return NULL;
3836 }
3837 }
3838
3839
3840 TEST_CASE(TestNativeFieldsAccess) {
3841 const char* kScriptChars =
3842 "import 'dart:nativewrappers';"
3843 "class NativeFields extends NativeFieldWrapperClass2 {\n"
3844 " NativeFields(int i, int j) : fld1 = i, fld2 = j {}\n"
3845 " int fld1;\n"
3846 " final int fld2;\n"
3847 " static int fld3;\n"
3848 " static const int fld4 = 10;\n"
3849 " int initNativeFlds() native 'TestNativeFieldsAccess_init';\n"
3850 " int accessNativeFlds(int i) native 'TestNativeFieldsAccess_access';\n"
3851 "}\n"
3852 "NativeFields testMain() {\n"
3853 " NativeFields obj = new NativeFields(10, 20);\n"
3854 " obj.initNativeFlds();\n"
3855 " obj.accessNativeFlds(null);\n"
3856 " return obj;\n"
3857 "}\n";
3858
3859 // Load up a test script in the test library.
3860 Dart_Handle lib = TestCase::LoadTestScript(kScriptChars,
3861 TestNativeFieldsAccess_lookup);
3862
3863 // Invoke a function which returns an object of type NativeFields.
3864 Dart_Handle result = Dart_Invoke(lib, NewString("testMain"), 0, NULL);
3865 EXPECT_VALID(result);
3866 }
3867
3868
3788 TEST_CASE(InjectNativeFieldsSuperClass) { 3869 TEST_CASE(InjectNativeFieldsSuperClass) {
3789 const char* kScriptChars = 3870 const char* kScriptChars =
3790 "import 'dart:nativewrappers';" 3871 "import 'dart:nativewrappers';"
3791 "class NativeFieldsSuper extends NativeFieldWrapperClass1 {\n" 3872 "class NativeFieldsSuper extends NativeFieldWrapperClass1 {\n"
3792 " NativeFieldsSuper() : fld1 = 42 {}\n" 3873 " NativeFieldsSuper() : fld1 = 42 {}\n"
3793 " int fld1;\n" 3874 " int fld1;\n"
3794 "}\n" 3875 "}\n"
3795 "class NativeFields extends NativeFieldsSuper {\n" 3876 "class NativeFields extends NativeFieldsSuper {\n"
3796 " fld() => fld1;\n" 3877 " fld() => fld1;\n"
3797 "}\n" 3878 "}\n"
(...skipping 4032 matching lines...) Expand 10 before | Expand all | Expand 10 after
7830 NewString("main"), 7911 NewString("main"),
7831 1, 7912 1,
7832 dart_args); 7913 dart_args);
7833 int64_t value = 0; 7914 int64_t value = 0;
7834 result = Dart_IntegerToInt64(result, &value); 7915 result = Dart_IntegerToInt64(result, &value);
7835 EXPECT_VALID(result); 7916 EXPECT_VALID(result);
7836 EXPECT_EQ(6, value); 7917 EXPECT_EQ(6, value);
7837 } 7918 }
7838 7919
7839 } // namespace dart 7920 } // namespace dart
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698