Skip to content

Commit

Permalink
Resolve dependency on JavaScript in compilation of Interactive enviro…
Browse files Browse the repository at this point in the history
…nments

Switch the compilation of the parsed Elm modules for Interactive session environments from the JavaScript-based to the Pine-based compiler.
  • Loading branch information
Viir committed Oct 13, 2024
1 parent 9f205aa commit 1ef08c3
Show file tree
Hide file tree
Showing 7 changed files with 253 additions and 98 deletions.
24 changes: 21 additions & 3 deletions implement/pine/Elm/ElmCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,20 @@ public static Result<string, IImmutableDictionary<IReadOnlyList<string>, ReadOnl

public ElmInteractiveEnvironment.FunctionRecord CompileParsedInteractiveSubmission { get; }

public ElmInteractiveEnvironment.FunctionRecord ExpandElmInteractiveEnvironmentWithModules { get; }

private static readonly PineVM.PineVMParseCache parseCache = new();

private ElmCompiler(
TreeNodeWithStringPath compilerSourceFiles,
PineValue compilerEnvironment,
ElmInteractiveEnvironment.FunctionRecord compileParsedInteractiveSubmission)
ElmInteractiveEnvironment.FunctionRecord compileParsedInteractiveSubmission,
ElmInteractiveEnvironment.FunctionRecord expandElmInteractiveEnvironmentWithModules)
{
CompilerSourceFiles = compilerSourceFiles;
CompilerEnvironment = compilerEnvironment;
CompileParsedInteractiveSubmission = compileParsedInteractiveSubmission;
ExpandElmInteractiveEnvironmentWithModules = expandElmInteractiveEnvironmentWithModules;
}

public static Task<Result<string, ElmCompiler>> GetElmCompilerAsync(
Expand Down Expand Up @@ -202,6 +206,10 @@ public static Result<string, PineValue> LoadOrCompileInteractiveEnvironment(
return Result<string, PineValue>.ok(fromBundle);
}

/*
* TODO: Load from cache
* */

return CompileInteractiveEnvironment(compilerSourceFiles);
}

Expand Down Expand Up @@ -239,10 +247,20 @@ public static Result<string, ElmCompiler> ElmCompilerFromEnvValue(
moduleName: "ElmCompiler",
declarationName: "compileParsedInteractiveSubmission",
parseCache)
.Map(parseModuleResponse =>
.MapError(err => "Failed parsing function to compile interactive submission")
.AndThen(compileParsedInteractiveSubmission =>
ElmInteractiveEnvironment.ParseFunctionFromElmModule(
interactiveEnvironment: compiledEnv,
moduleName: "ElmCompiler",
declarationName: "expandElmInteractiveEnvironmentWithModules",
parseCache)
.Map(expandElmInteractiveEnvironmentWithModules =>
new ElmCompiler(
compilerSourceFiles,
compiledEnv,
parseModuleResponse.functionRecord));
compileParsedInteractiveSubmission:
compileParsedInteractiveSubmission.functionRecord,
expandElmInteractiveEnvironmentWithModules:
expandElmInteractiveEnvironmentWithModules.functionRecord)));
}
}
32 changes: 30 additions & 2 deletions implement/pine/ElmInteractive/ElmInteractive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -420,11 +420,39 @@ void logDuration(string label) =>
fromJson,
parentDictionary: null));

logDuration("Deserialize (from " + CommandLineInterface.FormatIntegerForDisplay(responseJson.Length) + " chars) and " + nameof(ParsePineValueFromJson));
logDuration(
"Deserialize (from " + CommandLineInterface.FormatIntegerForDisplay(responseJson.Length) +
" chars) and " + nameof(ParsePineValueFromJson));

return response;
}

public static Result<string, PineValue> ParseElmModuleTextToPineValue(
string elmModuleText,
IJavaScriptEngine compilerJavaScriptEngine)
{
var jsonEncodedElmModuleText =
System.Text.Json.JsonSerializer.Serialize(elmModuleText);

var responseJson =
compilerJavaScriptEngine.CallFunction(
"parseElmModuleTextToPineValue",
jsonEncodedElmModuleText)
.ToString()!;

var responseResultStructure =
System.Text.Json.JsonSerializer.Deserialize<Result<string, ElmInteractive.PineValueJson>>(
responseJson,
compilerInterfaceJsonSerializerOptions)!;

return
responseResultStructure
.Map(responseStructure =>
ParsePineValueFromJson(
responseStructure,
parentDictionary: null));
}

public record CompilationCache(
IImmutableDictionary<PineValue, PineValueMappedForTransport> ValueMappedForTransportCache,
IImmutableDictionary<PineValue, (PineValueJson, IReadOnlyDictionary<string, PineValue>)> ValueJsonCache,
Expand Down Expand Up @@ -792,7 +820,7 @@ public static PineValue ParsePineValueFromJson(
return dictionary;
}

private static IReadOnlyList<string>? ModulesTextsFromAppCodeTree(TreeNodeWithStringPath? appCodeTree) =>
public static IReadOnlyList<string>? ModulesTextsFromAppCodeTree(TreeNodeWithStringPath? appCodeTree) =>
appCodeTree == null ?
null
:
Expand Down
2 changes: 1 addition & 1 deletion implement/pine/ElmInteractive/InteractiveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static IInteractiveSession Create(

ElmEngineType.Pine pineConfig =>
new InteractiveSessionPine(
compileElmProgramCodeFiles: compileElmProgramCodeFiles,
compilerSourceFiles: compileElmProgramCodeFiles,
initialState: null,
appCodeTree: appCodeTree,
caching: pineConfig.Caching,
Expand Down
Loading

0 comments on commit 1ef08c3

Please sign in to comment.