OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 #include <stdlib.h> |
| 6 #include <string.h> |
| 7 |
| 8 #include "include/dart_api.h" |
| 9 |
| 10 #include "bin/builtin.h" |
| 11 #include "bin/dartutils.h" |
| 12 |
| 13 |
| 14 // List all native functions implemented in standalone dart that is used |
| 15 // to inject additional functionality e.g: Logger, file I/O, socket I/O etc. |
| 16 #define BUILTIN_NATIVE_LIST(V) \ |
| 17 V(Directory_List, 7) \ |
| 18 V(Directory_Exists, 2) \ |
| 19 V(Directory_Create, 2) \ |
| 20 V(Directory_CreateTemp, 3) \ |
| 21 V(Directory_Delete, 2) \ |
| 22 V(EventHandler_Start, 1) \ |
| 23 V(EventHandler_SendData, 4) \ |
| 24 V(Exit, 1) \ |
| 25 V(File_Open, 2) \ |
| 26 V(File_Exists, 1) \ |
| 27 V(File_Close, 1) \ |
| 28 V(File_ReadByte, 1) \ |
| 29 V(File_WriteByte, 2) \ |
| 30 V(File_WriteString, 2) \ |
| 31 V(File_ReadList, 4) \ |
| 32 V(File_WriteList, 4) \ |
| 33 V(File_Position, 1) \ |
| 34 V(File_SetPosition, 2) \ |
| 35 V(File_Length, 1) \ |
| 36 V(File_Flush, 1) \ |
| 37 V(File_Create, 1) \ |
| 38 V(File_Delete, 1) \ |
| 39 V(File_FullPath, 1) \ |
| 40 V(Logger_PrintString, 1) \ |
| 41 V(Platform_NumberOfProcessors, 0) \ |
| 42 V(Platform_OperatingSystem, 0) \ |
| 43 V(Platform_PathSeparator, 0) \ |
| 44 V(Process_Start, 8) \ |
| 45 V(Process_Kill, 2) \ |
| 46 V(Process_Exit, 2) \ |
| 47 V(ServerSocket_CreateBindListen, 4) \ |
| 48 V(ServerSocket_Accept, 2) \ |
| 49 V(Socket_CreateConnect, 3) \ |
| 50 V(Socket_Available, 1) \ |
| 51 V(Socket_ReadList, 4) \ |
| 52 V(Socket_WriteList, 4) \ |
| 53 V(Socket_GetPort, 1) \ |
| 54 V(Socket_GetStdioHandle, 2) |
| 55 |
| 56 |
| 57 BUILTIN_NATIVE_LIST(DECLARE_FUNCTION); |
| 58 |
| 59 static struct NativeEntries { |
| 60 const char* name_; |
| 61 Dart_NativeFunction function_; |
| 62 int argument_count_; |
| 63 } BuiltinEntries[] = { |
| 64 BUILTIN_NATIVE_LIST(REGISTER_FUNCTION) |
| 65 }; |
| 66 |
| 67 |
| 68 Dart_NativeFunction Builtin::NativeLookup(Dart_Handle name, |
| 69 int argument_count) { |
| 70 const char* function_name = NULL; |
| 71 Dart_Handle result = Dart_StringToCString(name, &function_name); |
| 72 DART_CHECK_VALID(result); |
| 73 ASSERT(function_name != NULL); |
| 74 int num_entries = sizeof(BuiltinEntries) / sizeof(struct NativeEntries); |
| 75 for (int i = 0; i < num_entries; i++) { |
| 76 struct NativeEntries* entry = &(BuiltinEntries[i]); |
| 77 if (!strcmp(function_name, entry->name_)) { |
| 78 if (entry->argument_count_ == argument_count) { |
| 79 return reinterpret_cast<Dart_NativeFunction>(entry->function_); |
| 80 } else { |
| 81 // Wrong number of arguments. |
| 82 // TODO(regis): Should we pass a buffer for error reporting? |
| 83 return NULL; |
| 84 } |
| 85 } |
| 86 } |
| 87 return NULL; |
| 88 } |
| 89 |
| 90 |
| 91 // Implementation of native functions which are used for some |
| 92 // test/debug functionality in standalone dart mode. |
| 93 |
| 94 void Builtin::PrintString(FILE* out, Dart_Handle str) { |
| 95 const char* cstring = NULL; |
| 96 Dart_Handle result = Dart_StringToCString(str, &cstring); |
| 97 if (Dart_IsError(result)) { |
| 98 cstring = Dart_GetError(result); |
| 99 } |
| 100 fprintf(out, "%s\n", cstring); |
| 101 fflush(out); |
| 102 } |
| 103 |
| 104 |
| 105 void FUNCTION_NAME(Logger_PrintString)(Dart_NativeArguments args) { |
| 106 Dart_EnterScope(); |
| 107 Builtin::PrintString(stdout, Dart_GetNativeArgument(args, 0)); |
| 108 Dart_ExitScope(); |
| 109 } |
| 110 |
| 111 void FUNCTION_NAME(Exit)(Dart_NativeArguments args) { |
| 112 Dart_EnterScope(); |
| 113 int64_t status = DartUtils::GetIntegerValue(Dart_GetNativeArgument(args, 0)); |
| 114 Dart_ExitScope(); |
| 115 exit(status); |
| 116 } |
| 117 |
| 118 |
| 119 void Builtin::SetNativeResolver() { |
| 120 Dart_Handle url = Dart_NewString(DartUtils::kBuiltinLibURL); |
| 121 Dart_Handle builtin_lib = Dart_LookupLibrary(url); |
| 122 DART_CHECK_VALID(builtin_lib); |
| 123 // Setup the native resolver for built in library functions. |
| 124 Dart_Handle result = Dart_SetNativeResolver(builtin_lib, NativeLookup); |
| 125 DART_CHECK_VALID(result); |
| 126 } |
OLD | NEW |