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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « runtime/bin/BUILD.gn ('k') | runtime/platform/utils_fuchsia.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/bin/fuchsia_test.cc
diff --git a/runtime/bin/fuchsia_test.cc b/runtime/bin/fuchsia_test.cc
index e92014089d56e40cc25de3ba8d8f80e4c610f576..c6670b4dd2653ef2b538b12e971f490086fe8a52 100644
--- a/runtime/bin/fuchsia_test.cc
+++ b/runtime/bin/fuchsia_test.cc
@@ -2,21 +2,140 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+#include <dart_api.h>
#include <stdio.h>
#include <stdlib.h>
-
-#include <dart_api.h>
+#include <string.h>
#include "bin/log.h"
+#include "platform/assert.h"
+
+const char* kBuiltinScript =
+ "_printString(String line) native \"Builtin_PrintString\";\n"
+ "_getPrintClosure() => _printString;\n";
+
+const char* kHelloWorldScript = "main() { print(\"Hello, Fuchsia!\"); }";
namespace dart {
namespace bin {
+static void Builtin_PrintString(Dart_NativeArguments args) {
+ intptr_t length = 0;
+ uint8_t* chars = NULL;
+ Dart_Handle str = Dart_GetNativeArgument(args, 0);
+ Dart_Handle result = Dart_StringToUTF8(str, &chars, &length);
+ if (Dart_IsError(result)) {
+ Dart_PropagateError(result);
+ }
+ // Uses fwrite to support printing NUL bytes.
+ intptr_t res = fwrite(chars, 1, length, stdout);
+ ASSERT(res == length);
+ fputs("\n", stdout);
+ fflush(stdout);
+}
+
+static Dart_NativeFunction NativeLookup(Dart_Handle name,
+ int argument_count,
+ bool* auto_setup_scope) {
+ const char* function_name = NULL;
+ Dart_Handle err = Dart_StringToCString(name, &function_name);
+ DART_CHECK_VALID(err);
+ *auto_setup_scope = true;
+ if (strcmp(function_name, "Builtin_PrintString") == 0) {
+ return reinterpret_cast<Dart_NativeFunction>(Builtin_PrintString);
+ }
+ return NULL;
+}
+
+static const uint8_t* NativeSymbol(Dart_NativeFunction nf) {
+ if (reinterpret_cast<Dart_NativeFunction>(Builtin_PrintString) == nf) {
+ return reinterpret_cast<const uint8_t*>("Builtin_PrintString");
+ }
+ return NULL;
+}
+
+static Dart_Handle PrepareBuiltinLibrary(const char* script) {
+ Log::Print("Creating builtin library uri\n");
+ Dart_Handle builtin_uri = Dart_NewStringFromCString("builtin_uri");
+ DART_CHECK_VALID(builtin_uri);
+
+ Log::Print("Creating builtin library script string\n");
+ Dart_Handle builtin_script = Dart_NewStringFromCString(script);
+ DART_CHECK_VALID(builtin_script);
+
+ Log::Print("Loading builtin library\n");
+ Dart_Handle status = Dart_LoadLibrary(builtin_uri, builtin_script, 0, 0);
+ DART_CHECK_VALID(status);
+
+ Log::Print("Looking up builtin library\n");
+ Dart_Handle builtin_library = Dart_LookupLibrary(builtin_uri);
+ DART_CHECK_VALID(builtin_library);
+
+ Log::Print("Setting up native resolver for builtin library\n");
+ status = Dart_SetNativeResolver(builtin_library, NativeLookup, NativeSymbol);
+ DART_CHECK_VALID(status);
+
+ return builtin_library;
+}
+
+static Dart_Handle PrepareScriptLibrary(const char* script) {
+ Log::Print("Creating script URI string\n");
+ Dart_Handle script_uri = Dart_NewStringFromCString("script_uri");
+ DART_CHECK_VALID(script_uri);
+
+ Log::Print("Creating script string\n");
+ Dart_Handle script_string = Dart_NewStringFromCString(script);
+ DART_CHECK_VALID(script_string);
+
+ Log::Print("Loading script into new library\n");
+ Dart_Handle status = Dart_LoadLibrary(script_uri, script_string, 0, 0);
+ DART_CHECK_VALID(status);
+
+ Log::Print("Looking up script library\n");
+ Dart_Handle library = Dart_LookupLibrary(script_uri);
+ DART_CHECK_VALID(library);
+
+ return library;
+}
+
+static Dart_Handle LoadInternalLibrary() {
+ Log::Print("Creating internal library uri string\n");
+ Dart_Handle url = Dart_NewStringFromCString("dart:_internal");
+ DART_CHECK_VALID(url);
+
+ Log::Print("Looking up internal library\n");
+ Dart_Handle internal_library = Dart_LookupLibrary(url);
+ DART_CHECK_VALID(internal_library);
+
+ return internal_library;
+}
+
+static void PreparePrintClosure(Dart_Handle builtin_library,
+ Dart_Handle internal_library) {
+ Log::Print("Creating _getPrintClosure name string\n");
+ Dart_Handle get_print_closure_name =
+ Dart_NewStringFromCString("_getPrintClosure");
+ DART_CHECK_VALID(get_print_closure_name);
+
+ Log::Print("Invoking _getPrintClosure\n");
+ Dart_Handle print_closure = Dart_Invoke(
+ builtin_library, get_print_closure_name, 0, NULL);
+ DART_CHECK_VALID(print_closure);
+
+ Log::Print("Creating _printClosure name string\n");
+ Dart_Handle print_closure_name = Dart_NewStringFromCString("_printClosure");
+ DART_CHECK_VALID(print_closure_name);
+
+ Log::Print("Setting _printClosure to result of _getPrintClosure\n");
+ Dart_Handle status = Dart_SetField(
+ internal_library, print_closure_name, print_closure);
+ DART_CHECK_VALID(status);
+}
+
int Main() {
Log::Print("Calling Dart_SetVMFlags\n");
if (!Dart_SetVMFlags(0, NULL)) {
- Log::PrintErr("Failed to set flags\n");
- return -1;
+ FATAL("Failed to set flags\n");
}
Log::Print("Calling Dart_Initialize\n");
char* error = Dart_Initialize(
@@ -30,17 +149,54 @@ int Main() {
NULL,
NULL);
if (error != NULL) {
- Log::PrintErr("VM initialization failed: %s\n", error);
- free(error);
- return -1;
+ FATAL1("VM initialization failed: %s\n", error);
}
+ Log::Print("Creating Isolate\n");
+ Dart_Isolate isolate = Dart_CreateIsolate(
+ "script_uri",
+ "main",
+ NULL,
+ NULL,
+ NULL,
+ &error);
+ if (isolate == NULL) {
+ FATAL1("Dart_CreateIsolate failed: %s\n", error);
+ }
+
+ Log::Print("Entering Scope\n");
+ Dart_EnterScope();
+
+ Dart_Handle library = PrepareScriptLibrary(kHelloWorldScript);
+
+ Dart_Handle builtin_library = PrepareBuiltinLibrary(kBuiltinScript);
+
+ Log::Print("Finalizing loading\n");
+ Dart_Handle status = Dart_FinalizeLoading(false);
+ DART_CHECK_VALID(status);
+
+ Dart_Handle internal_library = LoadInternalLibrary();
+
+ PreparePrintClosure(builtin_library, internal_library);
+
+ Log::Print("Creating main string\n");
+ Dart_Handle main_name = Dart_NewStringFromCString("main");
+ DART_CHECK_VALID(main_name);
+
+ Log::Print("---- Invoking main() ----\n");
+ status = Dart_Invoke(library, main_name, 0, NULL);
+ DART_CHECK_VALID(status);
+ Log::Print("---- main() returned ----\n");
+
+ Log::Print("Exiting Scope\n");
+ Dart_ExitScope();
+ Log::Print("Shutting down the isolate\n");
+ Dart_ShutdownIsolate();
+
Log::Print("Calling Dart_Cleanup\n");
error = Dart_Cleanup();
if (error != NULL) {
- Log::PrintErr("VM Cleanup failed: %s\n", error);
- free(error);
- return -1;
+ FATAL1("VM Cleanup failed: %s\n", error);
}
Log::Print("Success!\n");
« 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