| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 #include <string.h> | 6 #include <string.h> |
| 7 | 7 |
| 8 #include "include/dart_api.h" | 8 #include "include/dart_api.h" |
| 9 | 9 |
| 10 #include "bin/builtin.h" | 10 #include "bin/builtin.h" |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 66 int argument_count_; | 66 int argument_count_; |
| 67 } BuiltinEntries[] = { | 67 } BuiltinEntries[] = { |
| 68 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) | 68 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) |
| 69 }; | 69 }; |
| 70 | 70 |
| 71 | 71 |
| 72 static Dart_NativeFunction native_lookup(Dart_Handle name, | 72 static Dart_NativeFunction native_lookup(Dart_Handle name, |
| 73 int argument_count) { | 73 int argument_count) { |
| 74 const char* function_name = NULL; | 74 const char* function_name = NULL; |
| 75 Dart_Handle result = Dart_StringToCString(name, &function_name); | 75 Dart_Handle result = Dart_StringToCString(name, &function_name); |
| 76 ASSERT(Dart_IsValid(result)); | 76 ASSERT(!Dart_IsError(result)); |
| 77 ASSERT(function_name != NULL); | 77 ASSERT(function_name != NULL); |
| 78 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | 78 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
| 79 for (int i = 0; i < num_entries; i++) { | 79 for (int i = 0; i < num_entries; i++) { |
| 80 struct NativeEntries* entry = &(BuiltinEntries[i]); | 80 struct NativeEntries* entry = &(BuiltinEntries[i]); |
| 81 if (!strcmp(function_name, entry->name_)) { | 81 if (!strcmp(function_name, entry->name_)) { |
| 82 if (entry->argument_count_ == argument_count) { | 82 if (entry->argument_count_ == argument_count) { |
| 83 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | 83 return reinterpret_cast<Dart_NativeFunction>(entry->function_); |
| 84 } else { | 84 } else { |
| 85 // Wrong number of arguments. | 85 // Wrong number of arguments. |
| 86 // TODO(regis): Should we pass a buffer for error reporting? | 86 // TODO(regis): Should we pass a buffer for error reporting? |
| 87 return NULL; | 87 return NULL; |
| 88 } | 88 } |
| 89 } | 89 } |
| 90 } | 90 } |
| 91 return NULL; | 91 return NULL; |
| 92 } | 92 } |
| 93 | 93 |
| 94 | 94 |
| 95 void Builtin_LoadLibrary() { | 95 void Builtin_LoadLibrary() { |
| 96 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | 96 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
| 97 Dart_Handle result = Dart_LookupLibrary(url); | 97 Dart_Handle result = Dart_LookupLibrary(url); |
| 98 if (Dart_IsValid(result)) { | 98 if (!Dart_IsError(result)) { |
| 99 // Builtin library already loaded. | 99 // Builtin library already loaded. |
| 100 return; | 100 return; |
| 101 } | 101 } |
| 102 | 102 |
| 103 // Load the library. | 103 // Load the library. |
| 104 Dart_Handle source = Dart_NewString(Builtin_source_); | 104 Dart_Handle source = Dart_NewString(Builtin_source_); |
| 105 Dart_Handle builtin_lib = Dart_LoadLibrary(url, source); | 105 Dart_Handle builtin_lib = Dart_LoadLibrary(url, source); |
| 106 ASSERT(Dart_IsValid(builtin_lib)); | 106 ASSERT(!Dart_IsError(builtin_lib)); |
| 107 | 107 |
| 108 // Lookup the core libraries and inject the builtin library into them. | 108 // Lookup the core libraries and inject the builtin library into them. |
| 109 Dart_Handle core_lib = Dart_LookupLibrary(Dart_NewString("dart:core")); | 109 Dart_Handle core_lib = Dart_LookupLibrary(Dart_NewString("dart:core")); |
| 110 ASSERT(Dart_IsValid(core_lib)); | 110 ASSERT(!Dart_IsError(core_lib)); |
| 111 result = Dart_LibraryImportLibrary(core_lib, builtin_lib); | 111 result = Dart_LibraryImportLibrary(core_lib, builtin_lib); |
| 112 ASSERT(Dart_IsValid(result)); | 112 ASSERT(!Dart_IsError(result)); |
| 113 | 113 |
| 114 Dart_Handle coreimpl_lib = | 114 Dart_Handle coreimpl_lib = |
| 115 Dart_LookupLibrary(Dart_NewString("dart:coreimpl")); | 115 Dart_LookupLibrary(Dart_NewString("dart:coreimpl")); |
| 116 ASSERT(Dart_IsValid(coreimpl_lib)); | 116 ASSERT(!Dart_IsError(coreimpl_lib)); |
| 117 result = Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib); | 117 result = Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib); |
| 118 ASSERT(Dart_IsValid(result)); | 118 ASSERT(!Dart_IsError(result)); |
| 119 result = Dart_LibraryImportLibrary(builtin_lib, coreimpl_lib); | 119 result = Dart_LibraryImportLibrary(builtin_lib, coreimpl_lib); |
| 120 ASSERT(Dart_IsValid(result)); | 120 ASSERT(!Dart_IsError(result)); |
| 121 | 121 |
| 122 // Create a native wrapper "EventHandlerNativeWrapper" so that we can add a | 122 // Create a native wrapper "EventHandlerNativeWrapper" so that we can add a |
| 123 // native field to store the event handle for implementing all | 123 // native field to store the event handle for implementing all |
| 124 // event operations. | 124 // event operations. |
| 125 Dart_Handle name = Dart_NewString("EventHandlerNativeWrapper"); | 125 Dart_Handle name = Dart_NewString("EventHandlerNativeWrapper"); |
| 126 const int kNumEventHandlerFields = 1; | 126 const int kNumEventHandlerFields = 1; |
| 127 result = Dart_CreateNativeWrapperClass(builtin_lib, | 127 result = Dart_CreateNativeWrapperClass(builtin_lib, |
| 128 name, | 128 name, |
| 129 kNumEventHandlerFields); | 129 kNumEventHandlerFields); |
| 130 ASSERT(Dart_IsValid(result)); | 130 ASSERT(!Dart_IsError(result)); |
| 131 } | 131 } |
| 132 | 132 |
| 133 | 133 |
| 134 void Builtin_ImportLibrary(Dart_Handle library) { | 134 void Builtin_ImportLibrary(Dart_Handle library) { |
| 135 Builtin_LoadLibrary(); | 135 Builtin_LoadLibrary(); |
| 136 | 136 |
| 137 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | 137 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
| 138 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | 138 Dart_Handle builtin_lib = Dart_LookupLibrary(url); |
| 139 ASSERT(Dart_IsValid(builtin_lib)); | 139 ASSERT(!Dart_IsError(builtin_lib)); |
| 140 Dart_Handle result = Dart_LibraryImportLibrary(library, builtin_lib); | 140 Dart_Handle result = Dart_LibraryImportLibrary(library, builtin_lib); |
| 141 ASSERT(Dart_IsValid(result)); | 141 ASSERT(!Dart_IsError(result)); |
| 142 } | 142 } |
| 143 | 143 |
| 144 | 144 |
| 145 void Builtin_SetNativeResolver() { | 145 void Builtin_SetNativeResolver() { |
| 146 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | 146 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
| 147 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | 147 Dart_Handle builtin_lib = Dart_LookupLibrary(url); |
| 148 ASSERT(Dart_IsValid(builtin_lib)); | 148 ASSERT(!Dart_IsError(builtin_lib)); |
| 149 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); | 149 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); |
| 150 ASSERT(Dart_IsValid(result)); | 150 ASSERT(!Dart_IsError(result)); |
| 151 } | 151 } |
| OLD | NEW |