-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Setup the IO library, which needs to be setup to have Isolate.spawn work as it initializes the `VMService.script` callback (through `_Platform._nativeScript`). Call `SetupCoreLibraries` from `OnIsolateInitialize` so that async functions in Isolates work Add an example / test for isolate spawning.
- Loading branch information
1 parent
19d33df
commit 6f28908
Showing
8 changed files
with
189 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
|
||
add_subdirectory(isolate_spawn) | ||
add_subdirectory(simple_example) | ||
add_subdirectory(simple_example_ffi) | ||
add_subdirectory(realtime_example) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
cmake_minimum_required(VERSION 3.21) | ||
|
||
project(isolate_spawn) | ||
|
||
add_executable(isolate_spawn main.cpp) | ||
|
||
target_include_directories(isolate_spawn PRIVATE | ||
"${DART_DLL_DIR}" | ||
"${DART_DIR}/runtime/include" | ||
) | ||
|
||
if(WIN32) | ||
add_custom_command(TARGET isolate_spawn POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:isolate_spawn> $<TARGET_FILE_DIR:isolate_spawn> | ||
COMMAND_EXPAND_LISTS | ||
) | ||
endif() | ||
|
||
add_custom_command(TARGET isolate_spawn POST_BUILD | ||
COMMAND ${CMAKE_COMMAND} -E copy ${PROJECT_SOURCE_DIR}/hello_world.dart $<TARGET_FILE_DIR:isolate_spawn> | ||
COMMAND_EXPAND_LISTS | ||
) | ||
|
||
target_link_libraries(isolate_spawn PUBLIC dart_dll) | ||
|
||
if (MSVC) | ||
set_property(TARGET isolate_spawn PROPERTY VS_DEBUGGER_WORKING_DIRECTORY $<TARGET_FILE_DIR:isolate_spawn>) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import 'dart:isolate'; | ||
|
||
@pragma('vm:external-name', 'SimplePrint') | ||
external void simplePrint(String s); | ||
|
||
Future<String> _isolateWorker() async { | ||
simplePrint("Isolate work 1..."); | ||
await Future.delayed(Duration(seconds: 10)); | ||
simplePrint("Isolate work 2..."); | ||
return "done"; | ||
} | ||
|
||
void main() async { | ||
simplePrint("Hello From Dart!\n"); | ||
final isolateValue = await Isolate.run(_isolateWorker); | ||
simplePrint("Isolate complete, returned $isolateValue"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <string.h> | ||
#include <iostream> | ||
|
||
#include "dart_dll.h" | ||
|
||
#include <dart_api.h> | ||
|
||
Dart_Handle HandleError(Dart_Handle handle) { | ||
if (Dart_IsError(handle)) { | ||
Dart_PropagateError(handle); | ||
} | ||
return handle; | ||
} | ||
|
||
void SimplePrint(Dart_NativeArguments arguments) { | ||
Dart_Handle string = HandleError(Dart_GetNativeArgument(arguments, 0)); | ||
if (Dart_IsString(string)) { | ||
const char* cstring; | ||
Dart_StringToCString(string, &cstring); | ||
std::cout << "Hello from C++. Dart says:\n"; | ||
std::cout << cstring << std::endl; | ||
} | ||
} | ||
|
||
Dart_NativeFunction ResolveNativeFunction(Dart_Handle name, | ||
int /* argc */, | ||
bool* /* auto_setup_scope */) { | ||
if (!Dart_IsString(name)) { | ||
return nullptr; | ||
} | ||
|
||
Dart_NativeFunction result = nullptr; | ||
|
||
const char* cname; | ||
HandleError(Dart_StringToCString(name, &cname)); | ||
|
||
if (strcmp("SimplePrint", cname) == 0) { | ||
result = SimplePrint; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
int main() { | ||
// Initialize Dart | ||
DartDllConfig config; | ||
config.service_port = 6222; | ||
|
||
if (!DartDll_Initialize(config)) { | ||
return -1; | ||
} | ||
|
||
// Load your main isolate file, also providing the path to a package config if one exists. | ||
// The build makes sure these are coppied to the output directory for running the example | ||
Dart_Isolate isolate = DartDll_LoadScript("hello_world.dart", nullptr); | ||
if (isolate == nullptr) { | ||
return -1; | ||
} | ||
|
||
// With the library loaded, you can now use the dart_api.h functions | ||
// This includes setting the native function resolver: | ||
Dart_EnterIsolate(isolate); | ||
Dart_EnterScope(); | ||
|
||
Dart_Handle library = Dart_RootLibrary(); | ||
Dart_SetNativeResolver(library, ResolveNativeFunction, nullptr); | ||
|
||
// And run "main" | ||
Dart_Handle result = DartDll_RunMain(library); | ||
if (Dart_IsError(result)) { | ||
std::cerr << "Failed to invoke main: " << Dart_GetError(result); | ||
} | ||
|
||
Dart_ExitScope(); | ||
Dart_ShutdownIsolate(); | ||
|
||
// Don't forget to shutdown | ||
DartDll_Shutdown(); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters