|
@@ -5,9 +5,19 @@ namespace Jint.Runtime.Modules;
|
|
|
|
|
|
public sealed class DefaultModuleLoader : IModuleLoader
|
|
|
{
|
|
|
+ public delegate void ModuleLoadedEventHandler(object sender, string source, Module module);
|
|
|
+
|
|
|
private readonly Uri _basePath;
|
|
|
private readonly bool _restrictToBasePath;
|
|
|
|
|
|
+ /// <summary>
|
|
|
+ /// The Loaded event is triggered after a module is loaded and parsed.
|
|
|
+ /// </summary>
|
|
|
+ /// <remarks>
|
|
|
+ /// The event is not triggered if a module was loaded but failed to parse.
|
|
|
+ /// </remarks>
|
|
|
+ public event ModuleLoadedEventHandler? Loaded;
|
|
|
+
|
|
|
public DefaultModuleLoader(string basePath) : this(basePath, true)
|
|
|
{
|
|
|
|
|
@@ -125,22 +135,25 @@ public sealed class DefaultModuleLoader : IModuleLoader
|
|
|
|
|
|
var code = File.ReadAllText(fileName);
|
|
|
|
|
|
+ var source = resolved.Uri.LocalPath;
|
|
|
Module module;
|
|
|
try
|
|
|
{
|
|
|
- module = new JavaScriptParser().ParseModule(code, source: resolved.Uri.LocalPath);
|
|
|
+ module = new JavaScriptParser().ParseModule(code, source);
|
|
|
}
|
|
|
catch (ParserException ex)
|
|
|
{
|
|
|
- ExceptionHelper.ThrowSyntaxError(engine.Realm, $"Error while loading module: error in module '{resolved.Uri.LocalPath}': {ex.Error}");
|
|
|
+ ExceptionHelper.ThrowSyntaxError(engine.Realm, $"Error while loading module: error in module '{source}': {ex.Error}");
|
|
|
module = null;
|
|
|
}
|
|
|
catch (Exception)
|
|
|
{
|
|
|
- ExceptionHelper.ThrowJavaScriptException(engine, $"Could not load module {resolved.Uri?.LocalPath}", (Location) default);
|
|
|
+ ExceptionHelper.ThrowJavaScriptException(engine, $"Could not load module {source}", (Location) default);
|
|
|
module = null;
|
|
|
}
|
|
|
|
|
|
+ Loaded?.Invoke(this, source, module);
|
|
|
+
|
|
|
return module;
|
|
|
}
|
|
|
|