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