Skip to content

Commit

Permalink
Unload ModuleBuilder from memory after loading, ModuleBuilder.ExportF…
Browse files Browse the repository at this point in the history
…unction
  • Loading branch information
christianrondeau committed Jan 21, 2022
1 parent 839ce7d commit 9580f04
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
16 changes: 14 additions & 2 deletions Jint.Tests/Runtime/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public void ShouldDefineModuleFromClrType()
Assert.Equal("hello world", ns.Get("exported").AsString());
}

private class ImportedClass
{
public string Value { get; set; } = "hello world";
}

[Fact]
public void ShouldAllowExportMultipleImports()
{
Expand Down Expand Up @@ -173,9 +178,16 @@ public void ShouldAllowChaining()
Assert.Equal(-1, ns.Get("num").AsInteger());
}

private class ImportedClass
[Fact]
public void ShouldAllowLoadingMoreThanOnce()
{
public string Value { get; set; } = "hello world";
var called = 0;
_engine.AddModule("imported-module", builder => builder.ExportFunction("count", args => called++));
_engine.AddModule("my-module", @"import { count } from 'imported-module'; count();");
_engine.ImportModule("my-module");
_engine.ImportModule("my-module");

Assert.Equal(called, 1);
}

#if(NET6_0_OR_GREATER)
Expand Down
1 change: 1 addition & 0 deletions Jint/Engine.Modules.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ internal JsModule LoadModule(string? referencingModuleLocation, string specifier
// Early link is required because we need to bind values before returning
module.Link();
moduleBuilder.BindExportedValues(module);
_builders.Remove(specifier);
}
else
{
Expand Down
6 changes: 6 additions & 0 deletions Jint/ModuleBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public ModuleBuilder ExportType(string name, Type type)
return this;
}

public ModuleBuilder ExportFunction(string name, Func<JsValue[], JsValue> fn)
{
_exports.Add(name, new ClrFunctionInstance(_engine, name, (@this, args) => fn(args)));
return this;
}

internal Module Parse()
{
if (_sourceRaw.Count > 0)
Expand Down

0 comments on commit 9580f04

Please sign in to comment.