Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(180)

Side by Side Diff: runtime/bin/fuchsia_test.cc

Issue 2244413005: [fuchsia] Remove no longer needed fuchsia_test (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/BUILD.gn ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, 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 <dart_api.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9
10 #include "bin/log.h"
11 #include "platform/assert.h"
12
13 const char* kBuiltinScript =
14 "_printString(String line) native \"Builtin_PrintString\";\n"
15 "_getPrintClosure() => _printString;\n";
16
17 const char* kHelloWorldScript = "main() { print(\"Hello, Fuchsia!\"); }";
18
19 namespace dart {
20 namespace bin {
21
22 // vm_isolate_snapshot_buffer points to a snapshot for the vm isolate if we
23 // link in a snapshot otherwise it is initialized to NULL.
24 extern const uint8_t* vm_isolate_snapshot_buffer;
25
26 // isolate_snapshot_buffer points to a snapshot for an isolate if we link in a
27 // snapshot otherwise it is initialized to NULL.
28 extern const uint8_t* isolate_snapshot_buffer;
29
30 static void Builtin_PrintString(Dart_NativeArguments args) {
31 intptr_t length = 0;
32 uint8_t* chars = NULL;
33 Dart_Handle str = Dart_GetNativeArgument(args, 0);
34 Dart_Handle result = Dart_StringToUTF8(str, &chars, &length);
35 if (Dart_IsError(result)) {
36 Dart_PropagateError(result);
37 }
38 // Uses fwrite to support printing NUL bytes.
39 intptr_t res = fwrite(chars, 1, length, stdout);
40 ASSERT(res == length);
41 fputs("\n", stdout);
42 fflush(stdout);
43 }
44
45 static Dart_NativeFunction NativeLookup(Dart_Handle name,
46 int argument_count,
47 bool* auto_setup_scope) {
48 const char* function_name = NULL;
49 Dart_Handle err = Dart_StringToCString(name, &function_name);
50 DART_CHECK_VALID(err);
51 *auto_setup_scope = true;
52 if (strcmp(function_name, "Builtin_PrintString") == 0) {
53 return reinterpret_cast<Dart_NativeFunction>(Builtin_PrintString);
54 }
55 return NULL;
56 }
57
58 static const uint8_t* NativeSymbol(Dart_NativeFunction nf) {
59 if (reinterpret_cast<Dart_NativeFunction>(Builtin_PrintString) == nf) {
60 return reinterpret_cast<const uint8_t*>("Builtin_PrintString");
61 }
62 return NULL;
63 }
64
65 static Dart_Handle PrepareBuiltinLibrary(const char* script) {
66 Log::Print("Creating builtin library uri\n");
67 Dart_Handle builtin_uri = Dart_NewStringFromCString("builtin_uri");
68 DART_CHECK_VALID(builtin_uri);
69
70 Log::Print("Creating builtin library script string\n");
71 Dart_Handle builtin_script = Dart_NewStringFromCString(script);
72 DART_CHECK_VALID(builtin_script);
73
74 Log::Print("Loading builtin library\n");
75 Dart_Handle status =
76 Dart_LoadLibrary(builtin_uri, Dart_Null(), builtin_script, 0, 0);
77 DART_CHECK_VALID(status);
78
79 Log::Print("Looking up builtin library\n");
80 Dart_Handle builtin_library = Dart_LookupLibrary(builtin_uri);
81 DART_CHECK_VALID(builtin_library);
82
83 Log::Print("Setting up native resolver for builtin library\n");
84 status = Dart_SetNativeResolver(builtin_library, NativeLookup, NativeSymbol);
85 DART_CHECK_VALID(status);
86
87 return builtin_library;
88 }
89
90 static Dart_Handle PrepareScriptLibrary(const char* script) {
91 Log::Print("Creating script URI string\n");
92 Dart_Handle script_uri = Dart_NewStringFromCString("script_uri");
93 DART_CHECK_VALID(script_uri);
94
95 Log::Print("Creating script string\n");
96 Dart_Handle script_string = Dart_NewStringFromCString(script);
97 DART_CHECK_VALID(script_string);
98
99 Log::Print("Loading script into new library\n");
100 Dart_Handle status =
101 Dart_LoadLibrary(script_uri, Dart_Null(), script_string, 0, 0);
102 DART_CHECK_VALID(status);
103
104 Log::Print("Looking up script library\n");
105 Dart_Handle library = Dart_LookupLibrary(script_uri);
106 DART_CHECK_VALID(library);
107
108 return library;
109 }
110
111 static Dart_Handle LoadInternalLibrary() {
112 Log::Print("Creating internal library uri string\n");
113 Dart_Handle url = Dart_NewStringFromCString("dart:_internal");
114 DART_CHECK_VALID(url);
115
116 Log::Print("Looking up internal library\n");
117 Dart_Handle internal_library = Dart_LookupLibrary(url);
118 DART_CHECK_VALID(internal_library);
119
120 return internal_library;
121 }
122
123 static void PreparePrintClosure(Dart_Handle builtin_library,
124 Dart_Handle internal_library) {
125 Log::Print("Creating _getPrintClosure name string\n");
126 Dart_Handle get_print_closure_name =
127 Dart_NewStringFromCString("_getPrintClosure");
128 DART_CHECK_VALID(get_print_closure_name);
129
130 Log::Print("Invoking _getPrintClosure\n");
131 Dart_Handle print_closure = Dart_Invoke(
132 builtin_library, get_print_closure_name, 0, NULL);
133 DART_CHECK_VALID(print_closure);
134
135 Log::Print("Creating _printClosure name string\n");
136 Dart_Handle print_closure_name = Dart_NewStringFromCString("_printClosure");
137 DART_CHECK_VALID(print_closure_name);
138
139 Log::Print("Setting _printClosure to result of _getPrintClosure\n");
140 Dart_Handle status = Dart_SetField(
141 internal_library, print_closure_name, print_closure);
142 DART_CHECK_VALID(status);
143 }
144
145 int Main() {
146 Log::Print("Calling Dart_SetVMFlags\n");
147 if (!Dart_SetVMFlags(0, NULL)) {
148 FATAL("Failed to set flags\n");
149 }
150 Log::Print("Calling Dart_Initialize\n");
151 Dart_InitializeParams init_params;
152 memset(&init_params, 0, sizeof(init_params));
153 init_params.version = DART_INITIALIZE_PARAMS_CURRENT_VERSION;
154 init_params.vm_isolate_snapshot = vm_isolate_snapshot_buffer;
155 char* error = Dart_Initialize(&init_params);
156 if (error != NULL) {
157 FATAL1("VM initialization failed: %s\n", error);
158 }
159
160 Log::Print("Creating Isolate\n");
161 Dart_Isolate isolate = Dart_CreateIsolate(
162 "script_uri",
163 "main",
164 isolate_snapshot_buffer,
165 NULL,
166 NULL,
167 &error);
168 if (isolate == NULL) {
169 FATAL1("Dart_CreateIsolate failed: %s\n", error);
170 }
171
172 Log::Print("Entering Scope\n");
173 Dart_EnterScope();
174
175 Dart_Handle library = PrepareScriptLibrary(kHelloWorldScript);
176
177 Dart_Handle builtin_library = PrepareBuiltinLibrary(kBuiltinScript);
178
179 Log::Print("Finalizing loading\n");
180 Dart_Handle status = Dart_FinalizeLoading(false);
181 DART_CHECK_VALID(status);
182
183 Dart_Handle internal_library = LoadInternalLibrary();
184
185 PreparePrintClosure(builtin_library, internal_library);
186
187 Log::Print("Creating main string\n");
188 Dart_Handle main_name = Dart_NewStringFromCString("main");
189 DART_CHECK_VALID(main_name);
190
191 Log::Print("---- Invoking main() ----\n");
192 status = Dart_Invoke(library, main_name, 0, NULL);
193 DART_CHECK_VALID(status);
194 Log::Print("---- main() returned ----\n");
195
196 Log::Print("Exiting Scope\n");
197 Dart_ExitScope();
198 Log::Print("Shutting down the isolate\n");
199 Dart_ShutdownIsolate();
200
201 Log::Print("Calling Dart_Cleanup\n");
202 error = Dart_Cleanup();
203 if (error != NULL) {
204 FATAL1("VM Cleanup failed: %s\n", error);
205 }
206
207 Log::Print("Success!\n");
208 return 0;
209 }
210
211 } // namespace bin
212 } // namespace dart
213
214 int main(void) {
215 return dart::bin::Main();
216 }
OLDNEW
« no previous file with comments | « runtime/bin/BUILD.gn ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698