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 <stdio.h> | 5 #include <stdio.h> |
6 | 6 |
7 #include "include/dart_api.h" | 7 #include "include/dart_api.h" |
8 | 8 |
9 #include "bin/builtin.h" | 9 #include "bin/builtin.h" |
10 #include "bin/dartutils.h" | 10 #include "bin/dartutils.h" |
11 | 11 |
12 static void SetupCorelibImports(Dart_Handle builtin_lib) { | 12 static void SetupCorelibImports(Dart_Handle builtin_lib) { |
13 // Lookup the core libraries and import the builtin library into them. | 13 // Lookup the core libraries and import the builtin library into them. |
14 Dart_Handle url = Dart_NewString(DartUtils::kCoreLibURL); | 14 Dart_Handle url = Dart_NewString(DartUtils::kCoreLibURL); |
15 Dart_Handle core_lib = Dart_LookupLibrary(url); | 15 Dart_Handle core_lib = Dart_LookupLibrary(url); |
16 DART_CHECK_VALID(core_lib); | 16 DART_CHECK_VALID(core_lib); |
17 DART_CHECK_VALID(Dart_LibraryImportLibrary(core_lib, builtin_lib)); | 17 DART_CHECK_VALID(Dart_LibraryImportLibrary(core_lib, builtin_lib)); |
18 | 18 |
19 url = Dart_NewString(DartUtils::kCoreImplLibURL); | 19 url = Dart_NewString(DartUtils::kCoreImplLibURL); |
20 Dart_Handle coreimpl_lib = Dart_LookupLibrary(url); | 20 Dart_Handle coreimpl_lib = Dart_LookupLibrary(url); |
21 DART_CHECK_VALID(coreimpl_lib); | 21 DART_CHECK_VALID(coreimpl_lib); |
22 DART_CHECK_VALID(Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib)); | 22 DART_CHECK_VALID(Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib)); |
23 } | 23 } |
24 | 24 |
25 | 25 |
26 Dart_Handle Builtin::Source(BuiltinLibraryId id) { | 26 Dart_Handle Builtin::Source(BuiltinLibraryId id) { |
27 Dart_Handle source; | 27 Dart_Handle source; |
28 if (id == kBuiltinLibrary) { | 28 switch (id) { |
29 source = Dart_NewString(Builtin::builtin_source_); | 29 case kBuiltinLibrary: |
30 } else { | 30 source = Dart_NewString(Builtin::builtin_source_); |
31 ASSERT(id == kIOLibrary); | 31 break; |
32 source = Dart_NewString(Builtin::io_source_); | 32 case kIOLibrary: |
33 source = Dart_NewString(Builtin::io_source_); | |
34 break; | |
35 case kJsonLibrary: | |
36 source = Dart_NewString(Builtin::json_source_); | |
37 break; | |
38 case kUriLibrary: | |
39 source = Dart_NewString(Builtin::uri_source_); | |
40 break; | |
41 case kUtf8Library: | |
42 source = Dart_NewString(Builtin::utf8_source_); | |
43 break; | |
44 default: | |
45 return Dart_Error("Unknown builtin source requested."); | |
33 } | 46 } |
34 return source; | 47 return source; |
35 } | 48 } |
36 | 49 |
37 | 50 |
38 void Builtin::SetupLibrary(Dart_Handle library, BuiltinLibraryId id) { | 51 void Builtin::SetupLibrary(Dart_Handle library, BuiltinLibraryId id) { |
39 if (id == kBuiltinLibrary) { | 52 if (id == kBuiltinLibrary) { |
40 // Setup core lib, builtin import structure. | 53 // Setup core lib, builtin import structure. |
41 SetupCorelibImports(library); | 54 SetupCorelibImports(library); |
42 } | 55 } |
43 // Setup the native resolver for built in library functions. | 56 // Setup the native resolver for built in library functions. |
44 DART_CHECK_VALID(Dart_SetNativeResolver(library, NativeLookup)); | 57 DART_CHECK_VALID(Dart_SetNativeResolver(library, NativeLookup)); |
siva
2012/02/16 18:44:17
Do all these libraries need this native hookup by
Ivan Posva
2012/02/16 19:58:50
Good point, I will only add the native resolve to
| |
45 } | 58 } |
46 | 59 |
47 | 60 |
48 Dart_Handle Builtin::LoadLibrary(BuiltinLibraryId id) { | 61 Dart_Handle Builtin::LoadLibrary(BuiltinLibraryId id) { |
49 Dart_Handle url; | 62 Dart_Handle url; |
50 if (id == kBuiltinLibrary) { | 63 switch (id) { |
51 url = Dart_NewString(DartUtils::kBuiltinLibURL); | 64 case kBuiltinLibrary: |
52 } else { | 65 url = Dart_NewString(DartUtils::kBuiltinLibURL); |
53 ASSERT(id == kIOLibrary); | 66 break; |
54 url = Dart_NewString(DartUtils::kIOLibURL); | 67 case kIOLibrary: |
68 url = Dart_NewString(DartUtils::kIOLibURL); | |
69 break; | |
70 case kJsonLibrary: | |
71 url = Dart_NewString(DartUtils::kJsonLibURL); | |
72 break; | |
73 case kUriLibrary: | |
74 url = Dart_NewString(DartUtils::kUriLibURL); | |
75 break; | |
76 case kUtf8Library: | |
77 url = Dart_NewString(DartUtils::kUtf8LibURL); | |
78 break; | |
79 default: | |
80 return Dart_Error("Unknown builtin library requested."); | |
turnidge
2012/02/16 17:42:02
This switch can go away if we get rid of ids. Ins
siva
2012/02/16 18:44:17
I would go for a table driven scheme for this, we
Ivan Posva
2012/02/16 19:58:50
I agree with you both that the way we are collecti
| |
55 } | 81 } |
56 Dart_Handle library = Dart_LookupLibrary(url); | 82 Dart_Handle library = Dart_LookupLibrary(url); |
57 if (Dart_IsError(library)) { | 83 if (Dart_IsError(library)) { |
58 Dart_Handle import_map = Dart_NewList(0); | 84 Dart_Handle import_map = Dart_NewList(0); |
59 library = Dart_LoadLibrary(url, Source(id), import_map); | 85 library = Dart_LoadLibrary(url, Source(id), import_map); |
60 if (!Dart_IsError(library)) { | 86 if (!Dart_IsError(library)) { |
61 SetupLibrary(library, id); | 87 SetupLibrary(library, id); |
62 } | 88 } |
63 } | 89 } |
64 DART_CHECK_VALID(library); | 90 DART_CHECK_VALID(library); |
65 return library; | 91 return library; |
66 } | 92 } |
67 | 93 |
68 | 94 |
69 void Builtin::ImportLibrary(Dart_Handle library, BuiltinLibraryId id) { | 95 void Builtin::ImportLibrary(Dart_Handle library, BuiltinLibraryId id) { |
70 Dart_Handle imported_library = LoadLibrary(id); | 96 Dart_Handle imported_library = LoadLibrary(id); |
71 // Import the library into current library. | 97 // Import the library into current library. |
72 DART_CHECK_VALID(Dart_LibraryImportLibrary(library, imported_library)); | 98 DART_CHECK_VALID(Dart_LibraryImportLibrary(library, imported_library)); |
73 } | 99 } |
OLD | NEW |