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