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

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

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

Powered by Google App Engine
This is Rietveld 408576698