Skip to content

Commit

Permalink
Add a moduleView runtime compilation test
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthaTi committed Sep 26, 2024
1 parent 3c6915a commit f501822
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 6 deletions.
24 changes: 21 additions & 3 deletions dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,19 @@
},
{
"name": "dynamic",
"targetType": "dynamicLibrary"
"targetType": "dynamicLibrary",
"dflags-posix-dmd": [
"-defaultlib=libphobos2.so"
],
"dflags-windows-dmd": [
"-defaultlib=phobos2.dll"
],
"dflags-ldc": [
"--link-defaultlib-shared"
],
"libs-windows": [
"LIBCMT"
]
},
{
"name": "source",
Expand Down Expand Up @@ -132,15 +144,21 @@
],
"sourcePaths": [],
"subConfigurations": {
"fluid": "default"
"fluid": "dynamic"
},
"configurations": [
{
"name": "linux-dynamic",
"platforms": [
"linux"
],
"targetType": "dynamicLibrary"
"targetType": "staticLibrary",
"dflags-posix-dmd": [
"-defaultlib=libphobos2.so"
],
"dflags-ldc": [
"--link-defaultlib-shared"
]
},
{
"name": "static",
Expand Down
54 changes: 51 additions & 3 deletions source/fluid/module_view.d
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,7 @@ private struct ModuleView {
const prefix = source[0 .. start] ~ injectSource ~ source[start .. exampleStart];
const value = source[exampleStart .. exampleEnd];
const suffix = Rope(source[exampleEnd .. $]);

// Append code editor to the result
documentation.children ~= exampleView(compiler, prefix, value, suffix, contentTheme);
return documentation;
Expand Down Expand Up @@ -493,6 +493,9 @@ Frame exampleView(DlangCompiler compiler, CodeInput input, Theme contentTheme) {
bindbc.SharedLib library;
alias library this;

// BUG this crashes the tour on exit
// without this, memory is leaked
version (none)
~this() {
clear();
}
Expand All @@ -509,7 +512,7 @@ Frame exampleView(DlangCompiler compiler, CodeInput input, Theme contentTheme) {
auto originalValue = input.value;

/// Compile the program
void compileAndRun() {
void compileAndRun() @trusted {

string outputPath;

Expand Down Expand Up @@ -627,7 +630,7 @@ Frame exampleView(DlangCompiler compiler, Rope prefix, string value, Rope suffix
}

/// Run a Fluid snippet from a shared library.
private bindbc.SharedLib runSharedLibrary(string path) @trusted {
private bindbc.SharedLib runSharedLibrary(string path) @system {

// Load the resulting library
auto library = bindbc.load(path.toStringz);
Expand All @@ -652,6 +655,51 @@ private bindbc.SharedLib runSharedLibrary(string path) @trusted {

}

@system unittest {

import std.path;

auto source = q{
import fluid;

pragma(mangle, "fluid_moduleView_entrypoint")
void entrypoint() {
run(label("Hello, World!"));
}
};

// Configure the compiler
auto compiler = DlangCompiler.findAny();
compiler.importPaths ~= [
"source",
"../source",
expandTilde("~/.dub/packages/bindbc-freetype/1.1.1/bindbc-freetype/source"),
expandTilde("~/.dub/packages/bindbc-loader/1.1.5/bindbc-loader/source"),
];

// Compile the pogram
string outputPath;
auto program = compiler.compileSharedLibrary(source, outputPath);
assert(program.status == 0, program.output);

// Prepare the environment
Node output;
mockRun = delegate (node) {
output = node;
};
scope (exit) mockRun = null;

// Make sure it could be loaded
auto library = runSharedLibrary(outputPath);
assert(library != bindbc.invalidHandle);
scope (exit) bindbc.unload(library);

// And test the output
auto result = cast(Label) output;
assert(result.text == "Hello, World!");

}

/// Creates a `CodeInput` with D syntax highlighting.
CodeInput dlangInput(void delegate() @safe submitted = null) @trusted {

Expand Down

0 comments on commit f501822

Please sign in to comment.