| 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_Truncate, 2) \ | |
| 44 V(File_Length, 1) \ | |
| 45 V(File_Flush, 1) \ | |
| 46 V(File_Create, 1) \ | |
| 47 V(File_Delete, 1) \ | |
| 48 V(File_FullPath, 1) \ | |
| 49 V(Logger_PrintString, 1) \ | |
| 50 V(Platform_NumberOfProcessors, 0) \ | |
| 51 V(Platform_OperatingSystem, 0) \ | |
| 52 V(Platform_PathSeparator, 0) \ | |
| 53 V(Process_Start, 8) \ | |
| 54 V(Process_Kill, 2) \ | |
| 55 V(Process_Exit, 2) \ | |
| 56 V(ServerSocket_CreateBindListen, 4) \ | |
| 57 V(ServerSocket_Accept, 2) \ | |
| 58 V(Socket_CreateConnect, 3) \ | |
| 59 V(Socket_Available, 1) \ | |
| 60 V(Socket_ReadList, 4) \ | |
| 61 V(Socket_WriteList, 4) \ | |
| 62 V(Socket_GetPort, 1) \ | |
| 63 V(Socket_GetStdioHandle, 2) | |
| 64 | |
| 65 | |
| 66 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); | |
| 67 | |
| 68 static struct NativeEntries { | |
| 69 const char* name_; | |
| 70 Dart_NativeFunction function_; | |
| 71 int argument_count_; | |
| 72 } BuiltinEntries[] = { | |
| 73 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) | |
| 74 }; | |
| 75 | |
| 76 | |
| 77 static Dart_NativeFunction native_lookup(Dart_Handle name, | |
| 78 int argument_count) { | |
| 79 const char* function_name = NULL; | |
| 80 Dart_Handle result = Dart_StringToCString(name, &function_name); | |
| 81 DART_CHECK_VALID(result); | |
| 82 ASSERT(function_name != NULL); | |
| 83 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); | |
| 84 for (int i = 0; i < num_entries; i++) { | |
| 85 struct NativeEntries* entry = &(BuiltinEntries[i]); | |
| 86 if (!strcmp(function_name, entry->name_)) { | |
| 87 if (entry->argument_count_ == argument_count) { | |
| 88 return reinterpret_cast<Dart_NativeFunction>(entry->function_); | |
| 89 } else { | |
| 90 // Wrong number of arguments. | |
| 91 // TODO(regis): Should we pass a buffer for error reporting? | |
| 92 return NULL; | |
| 93 } | |
| 94 } | |
| 95 } | |
| 96 return NULL; | |
| 97 } | |
| 98 | |
| 99 | |
| 100 static void SetupCorelibImports(Dart_Handle builtin_lib) { | |
| 101 // Lookup the core libraries and import the builtin library into them. | |
| 102 Dart_Handle url = Dart_NewString(DartUtils::kCoreLibURL); | |
| 103 Dart_Handle core_lib = Dart_LookupLibrary(url); | |
| 104 DART_CHECK_VALID(core_lib); | |
| 105 Dart_Handle result = Dart_LibraryImportLibrary(core_lib, builtin_lib); | |
| 106 DART_CHECK_VALID(result); | |
| 107 | |
| 108 url = Dart_NewString(DartUtils::kCoreImplLibURL); | |
| 109 Dart_Handle coreimpl_lib = Dart_LookupLibrary(url); | |
| 110 DART_CHECK_VALID(coreimpl_lib); | |
| 111 result = Dart_LibraryImportLibrary(coreimpl_lib, builtin_lib); | |
| 112 DART_CHECK_VALID(result); | |
| 113 } | |
| 114 | |
| 115 | |
| 116 Dart_Handle Builtin_Source() { | |
| 117 Dart_Handle str = Dart_NewString(Builtin_source_); | |
| 118 return str; | |
| 119 } | |
| 120 | |
| 121 | |
| 122 void Builtin_SetupLibrary(Dart_Handle builtin_lib) { | |
| 123 // Setup core lib, builtin import structure. | |
| 124 SetupCorelibImports(builtin_lib); | |
| 125 // Setup the native resolver for built in library functions. | |
| 126 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); | |
| 127 DART_CHECK_VALID(result); | |
| 128 } | |
| 129 | |
| 130 | |
| 131 void Builtin_ImportLibrary(Dart_Handle library) { | |
| 132 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | |
| 133 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | |
| 134 if (Dart_IsError(builtin_lib)) { | |
| 135 Dart_Handle source = Dart_NewString(Builtin_source_); | |
| 136 builtin_lib = Dart_LoadLibrary(url, source); | |
| 137 if (!Dart_IsError(builtin_lib)) { | |
| 138 Builtin_SetupLibrary(builtin_lib); | |
| 139 } | |
| 140 } | |
| 141 // Import the builtin library into current library. | |
| 142 DART_CHECK_VALID(builtin_lib); | |
| 143 Dart_Handle result = Dart_LibraryImportLibrary(library, builtin_lib); | |
| 144 DART_CHECK_VALID(result); | |
| 145 } | |
| 146 | |
| 147 | |
| 148 void Builtin_SetNativeResolver() { | |
| 149 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); | |
| 150 Dart_Handle builtin_lib = Dart_LookupLibrary(url); | |
| 151 DART_CHECK_VALID(builtin_lib); | |
| 152 // Setup the native resolver for built in library functions. | |
| 153 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, native_lookup); | |
| 154 DART_CHECK_VALID(result); | |
| 155 } | |
| OLD | NEW |