| 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" | |
| 9 | |
| 10 #include "bin/builtin.h" | 8 #include "bin/builtin.h" |
| 11 #include "bin/dartutils.h" | |
| 12 | 9 |
| 13 // The string on the next line will be filled in with the contents of the | 10 // The string on the next line will be filled in with the contents of the |
| 14 // builtin.dart file. | 11 // builtin.dart file. |
| 15 // This string forms the content of builtin functionality which is injected | 12 // This string forms the content of builtin functionality which is injected |
| 16 // into standalone dart to provide some test/debug functionality. | 13 // into standalone dart to provide some test/debug functionality. |
| 17 static const char Builtin_source_[] = { | 14 const char Builtin::Builtin_source_[] = { |
| 18 {{DART_SOURCE}} | 15 {{DART_SOURCE}} |
| 19 }; | 16 }; |
| 20 | |
| 21 | |
| 22 // List all native functions implemented in standalone dart that is used | |
| 23 // to inject additional functionality e.g: Logger, file I/O, socket I/O etc. | |
| 24 #define BUILTIN_NATIVE_LIST(V) \ | |
| 25 V(Directory_List, 7) \ | |
| 26 V(Directory_Exists, 2) \ | |
| 27 V(Directory_Create, 2) \ | |
| 28 V(Directory_CreateTemp, 3) \ | |
| 29 V(Directory_Delete, 2) \ | |
| 30 V(EventHandler_Start, 1) \ | |
| 31 V(EventHandler_SendData, 4) \ | |
| 32 V(Exit, 1) \ | |
| 33 V(File_Open, 2) \ | |
| 34 V(File_Exists, 1) \ | |
| 35 V(File_Close, 1) \ | |
| 36 V(File_ReadByte, 1) \ | |
| 37 V(File_WriteByte, 2) \ | |
| 38 V(File_WriteString, 2) \ | |
| 39 V(File_ReadList, 4) \ | |
| 40 V(File_WriteList, 4) \ | |
| 41 V(File_Position, 1) \ | |
| 42 V(File_SetPosition, 2) \ | |
| 43 V(File_Length, 1) \ | |
| 44 V(File_Flush, 1) \ | |
| 45 V(File_Create, 1) \ | |
| 46 V(File_Delete, 1) \ | |
| 47 V(File_FullPath, 1) \ | |
| 48 V(Logger_PrintString, 1) \ | |
| 49 V(Platform_NumberOfProcessors, 0) \ | |
| 50 V(Platform_OperatingSystem, 0) \ | |
| 51 V(Platform_PathSeparator, 0) \ | |
| 52 V(Process_Start, 8) \ | |
| 53 V(Process_Kill, 2) \ | |
| 54 V(Process_Exit, 2) \ | |
| 55 V(ServerSocket_CreateBindListen, 4) \ | |
| 56 V(ServerSocket_Accept, 2) \ | |
| 57 V(Socket_CreateConnect, 3) \ | |
| 58 V(Socket_Available, 1) \ | |
| 59 V(Socket_ReadList, 4) \ | |
| 60 V(Socket_WriteList, 4) \ | |
| 61 V(Socket_GetPort, 1) \ | |
| 62 V(Socket_GetStdioHandle, 2) | |
| 63 | |
| 64 | |
| 65 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); | |
| 66 | |
| 67 static struct NativeEntries { | |
| 68 const char* name_; | |
| 69 Dart_NativeFunction function_; | |
| 70 int argument_count_; | |
| 71 } BuiltinEntries[] = { | |
| 72 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) | |
| 73 }; | |
| 74 | |
| 75 | |
| 76 static Dart_NativeFunction native_lookup(Dart_Handle name, | |
| 77 int argument_count) { | |
| 78 const char* function_name = NULL; | |
| 79 Dart_Handle result = Dart_StringToCString(name, &function_name); | |
| 80 DART_CHECK_VALID(result); | |
| 81 ASSERT(function_name != NULL); | |
| 82 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | |
| 83 for (int i = 0; i < num_entries; i++) { | |
| 84 struct NativeEntries* entry = &(BuiltinEntries[i]); | |
| 85 if (!strcmp(function_name, entry->name_)) { | |
| 86 if (entry->argument_count_ == argument_count) { | |
| 87 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | |
| 88 } else { | |
| 89 // Wrong number of arguments. | |
| 90 // TODO(regis): Should we pass a buffer for error reporting? | |
| 91 return NULL; | |
| 92 } | |
| 93 } | |
| 94 } | |
| 95 return NULL; | |
| 96 } | |
| 97 | |
| 98 | |
| 99 static void SetupCorelibImports(Dart_Handle builtin_lib) { | |
| 100 // Lookup the core libraries and import the builtin library into them. | |
| 101 Dart_Handle url = Dart_NewString(DartUtils::kCoreLibURL); | |
| 102 Dart_Handle core_lib = Dart_LookupLibrary(url); | |
| 103 DART_CHECK_VALID(core_lib); | |
| 104 Dart_Handle result = Dart_LibraryImportLibrary(core_lib, builtin_lib); | |
| 105 DART_CHECK_VALID(result); | |
| 106 | |
| 107 url = Dart_NewString(DartUtils::kCoreImplLibURL); | |
| 108 Dart_Handle coreimpl_lib = Dart_LookupLibrary(url); | |
| 109 DART_CHECK_VALID(coreimpl_lib); | |
| 110 result = Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib); | |
| 111 DART_CHECK_VALID(result); | |
| 112 } | |
| 113 | |
| 114 | |
| 115 Dart_Handle Builtin_Source() { | |
| 116 Dart_Handle str = Dart_NewString(Builtin_source_); | |
| 117 return str; | |
| 118 } | |
| 119 | |
| 120 | |
| 121 void Builtin_SetupLibrary(Dart_Handle builtin_lib) { | |
| 122 // Setup core lib, builtin import structure. | |
| 123 SetupCorelibImports(builtin_lib); | |
| 124 // Setup the native resolver for built in library functions. | |
| 125 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); | |
| 126 DART_CHECK_VALID(result); | |
| 127 } | |
| 128 | |
| 129 | |
| 130 void Builtin_ImportLibrary(Dart_Handle library) { | |
| 131 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | |
| 132 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | |
| 133 if (Dart_IsError(builtin_lib)) { | |
| 134 Dart_Handle source = Dart_NewString(Builtin_source_); | |
| 135 builtin_lib = Dart_LoadLibrary(url, source); | |
| 136 if (!Dart_IsError(builtin_lib)) { | |
| 137 Builtin_SetupLibrary(builtin_lib); | |
| 138 } | |
| 139 } | |
| 140 // Import the builtin library into current library. | |
| 141 DART_CHECK_VALID(builtin_lib); | |
| 142 Dart_Handle result = Dart_LibraryImportLibrary(library, builtin_lib); | |
| 143 DART_CHECK_VALID(result); | |
| 144 } | |
| 145 | |
| 146 | |
| 147 void Builtin_SetNativeResolver() { | |
| 148 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | |
| 149 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | |
| 150 DART_CHECK_VALID(builtin_lib); | |
| 151 // Setup the native resolver for built in library functions. | |
| 152 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); | |
| 153 DART_CHECK_VALID(result); | |
| 154 } | |
| OLD | NEW |