Repository navigation
Create Jint engines through a configuration service #142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
FlorianRappl
merged 7 commits into
AngleSharp:devel
from
EvotecIT:feature/script-execution-options
Oct 4, 2026
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4f3c1da
feat: configure Jint before page scripts run
PrzemyslawKlys f76c912
fix: prepare DOM proxies before native engine initialization
PrzemyslawKlys ccbc3d0
Use an engine factory and retain readonly instance state
PrzemyslawKlys b67e463
Merge remote-tracking branch 'refs/remotes/upstream/devel' into featu…
PrzemyslawKlys c415cb0
Register engine creation as a browsing context service
PrzemyslawKlys 76f27e3
Preserve engine creator registrations in worker contexts
PrzemyslawKlys 25dc67b
Pass the current window to the engine creator
PrzemyslawKlys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,150 @@ | ||
| namespace AngleSharp.Js.Tests | ||
| { | ||
| using AngleSharp.Dom; | ||
| using AngleSharp.Io; | ||
| using AngleSharp.Js.Tests.Mocks; | ||
| using AngleSharp.Scripting; | ||
| using Jint; | ||
| using Jint.Runtime; | ||
| using NUnit.Framework; | ||
| using System; | ||
| using System.Threading; | ||
| using System.Threading.Tasks; | ||
|
|
||
| public class EngineConfigurationTests | ||
| { | ||
| [Test] | ||
| public async Task ConstraintsAreInstalledBeforeInlineScriptsRun() | ||
| { | ||
| var config = Configuration.Default | ||
| .With(new EngineCreator(options => new Engine(options.MaxStatements(64)))) | ||
| .WithJs() | ||
| .WithEventLoop(); | ||
|
|
||
| using (var context = BrowsingContext.New(config)) | ||
| { | ||
| var document = await context.OpenAsync(request => request.Content( | ||
| "<div id='result'>before</div><script>for (var i = 0; i < 1000; i++) {} " + | ||
| "document.getElementById('result').textContent = 'after';</script>")).ConfigureAwait(false); | ||
|
|
||
| Assert.AreEqual("before", document.QuerySelector("#result").TextContent); | ||
| Assert.Throws<StatementsCountOverflowException>(() => document.ExecuteScript( | ||
| "for (var i = 0; i < 1000; i++) {}")); | ||
| Assert.AreEqual("before", document.ExecuteScript("document.getElementById('result').textContent")); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task CreatorServiceCanBeCreatedForEachBrowsingContext() | ||
| { | ||
| var services = 0; | ||
| Func<IBrowsingContext, EngineCreator> createService = _ => | ||
| { | ||
| services++; | ||
| var limit = services == 1 ? 64 : 2000; | ||
| return options => new Engine(options.MaxStatements(limit)); | ||
| }; | ||
| var config = Configuration.Default.With(createService).WithJs().WithEventLoop(); | ||
|
|
||
| using (var first = BrowsingContext.New(config)) | ||
| using (var second = BrowsingContext.New(config)) | ||
| { | ||
| var firstDocument = await first.OpenNewAsync().ConfigureAwait(false); | ||
| var secondDocument = await second.OpenNewAsync().ConfigureAwait(false); | ||
| Assert.Throws<StatementsCountOverflowException>(() => firstDocument.ExecuteScript( | ||
| "for (var i = 0; i < 1000; i++) {}")); | ||
| Assert.AreEqual(100, secondDocument.ExecuteScript("var i = 0; for (; i < 100; i++) {} i;")); | ||
| Assert.AreEqual(2, services); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task CreatorRunsOncePerWindowAndRetainsDomWrapping() | ||
| { | ||
| var calls = 0; | ||
| var config = Configuration.Default.With(new EngineCreator(options => | ||
| { | ||
| calls++; | ||
| return new Engine(options.MaxStatements(1000)); | ||
| })).WithJs().WithEventLoop(); | ||
|
|
||
| using (var firstContext = BrowsingContext.New(config)) | ||
| using (var secondContext = BrowsingContext.New(config)) | ||
| { | ||
| var first = await firstContext.OpenAsync(request => request.Content("<p>first</p>")).ConfigureAwait(false); | ||
| var second = await secondContext.OpenAsync(request => request.Content("<p>second</p>")).ConfigureAwait(false); | ||
|
|
||
| Assert.AreEqual("first", first.ExecuteScript("document.querySelector('p').textContent")); | ||
| Assert.AreEqual("first", first.ExecuteScript("document.querySelector('p').textContent")); | ||
| Assert.AreEqual("second", second.ExecuteScript("document.querySelector('p').textContent")); | ||
| Assert.AreEqual(2, calls); | ||
| } | ||
| } | ||
|
|
||
| [Test] | ||
| public async Task CreatorReturnsTheEngineUsedForInlineScriptsAndDomBindings() | ||
| { | ||
| Engine created = null; | ||
| var config = Configuration.Default.With(new EngineCreator(options => | ||
| { | ||
| created = new Engine(options.Configure(engine => engine.SetValue("hostValue", "configured"))); | ||
| return created; | ||
| })).WithJs().WithEventLoop(); | ||
|
|
||
| using (var context = BrowsingContext.New(config)) | ||
| { | ||
| var document = await context.OpenAsync(request => request.Content( | ||
| "<p>before</p><script>document.querySelector('p').textContent = hostValue;</script>")).ConfigureAwait(false); | ||
| Assert.AreEqual("configured", document.QuerySelector("p").TextContent); | ||
| Assert.AreSame(created, context.GetService<JsScriptingService>().GetOrCreateJint(document)); | ||
| created.SetValue("hostNode", document.QuerySelector("p")); | ||
| Assert.AreEqual(true, document.ExecuteScript("hostNode === document.querySelector('p')")); | ||
| } | ||
| } | ||
|
|
||
| [TestCase(false)] | ||
| [TestCase(true)] | ||
| public async Task WorkersInheritTheEngineCreatorService(Boolean contextFactory) | ||
| { | ||
| var services = 0; | ||
| EngineCreator CreateEngine(String hostValue) => options => new Engine(options.MaxStatements(256) | ||
| .Configure(engine => engine.SetValue("hostValue", hostValue))); | ||
| Object registration = contextFactory | ||
| ? (Object)new Func<IBrowsingContext, EngineCreator>(_ => CreateEngine("context-" + Interlocked.Increment(ref services))) | ||
| : CreateEngine("configured"); | ||
| var config = Configuration.Default | ||
| .With(registration) | ||
| .WithJs() | ||
| .WithEventLoop() | ||
| .With(new DelayedRequester(0, "self.__host = hostValue; for (var i = 0; i < 10000; i++) {} self.__after = true;")) | ||
| .WithDefaultLoader(new LoaderOptions { IsResourceLoadingEnabled = true }); | ||
|
|
||
| using (var context = BrowsingContext.New(config)) | ||
| { | ||
| var document = await context.OpenNewAsync().ConfigureAwait(false); | ||
| Assert.AreEqual(contextFactory ? "context-1" : "configured", document.ExecuteScript("hostValue")); | ||
| var worker = new Dom.Worker(document.DefaultView, "https://example.com/worker.js"); | ||
|
|
||
| try | ||
| { | ||
| for (var retries = 200; retries > 0 && !worker.IsInitialized && worker.StartupError is null; retries--) | ||
| { | ||
| await Task.Delay(10).ConfigureAwait(false); | ||
| } | ||
|
|
||
| Assert.IsInstanceOf<StatementsCountOverflowException>(worker.StartupError); | ||
| Assert.AreEqual(contextFactory ? "context-2" : "configured", worker.EvaluateInWorker("self.__host")); | ||
| Assert.AreEqual("undefined", worker.EvaluateInWorker("typeof self.__after")); | ||
| if (contextFactory) | ||
| { | ||
| Assert.AreEqual(2, services); | ||
| } | ||
| } | ||
| finally | ||
| { | ||
| worker.Terminate(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,14 @@ | ||
| namespace AngleSharp.Js | ||
| { | ||
| /// <summary> | ||
| /// Creates a window's Jint engine. Register this service with the configuration | ||
| /// to customize engine creation before page scripts run. | ||
| /// </summary> | ||
| /// <param name="options">Options containing the DOM wrapper, module loader, and stack guard.</param> | ||
| /// <returns>A fresh engine constructed with the supplied options.</returns> | ||
| /// <remarks> | ||
| /// DOM bindings are installed after this delegate returns. Configure the engine | ||
| /// and host globals here; access DOM objects and run page scripts afterwards. | ||
| /// </remarks> | ||
| public delegate Jint.Engine EngineCreator(Jint.Options options); | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.