Skip to content

Commit

Permalink
Now possible to pass a variables to ExportTable
Browse files Browse the repository at this point in the history
  • Loading branch information
ZverGuy committed Mar 27, 2023
1 parent f1a67e5 commit de9cad3
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 6 deletions.
20 changes: 20 additions & 0 deletions NiL.JS/ExportTable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,27 @@ internal set
_items[key] = value;
}
}
public void DefineConstructor(Type type, string name)
{
var ctor = GlobalContext.DefaultGlobalContext.GetConstructor(type);
_items.Add(name, ctor);
ctor._attributes |= JSValueAttributesInternal.DoNotEnumerate;
}

public JSValue DefineVariable(string name, bool deletable)
{
var defineVariable = GlobalContext.DefaultGlobalContext.DefineVariable(name, deletable);
_items.Add(name,defineVariable);
return defineVariable;
}
public JSValue DefineConstant(string name, JSValue value, bool deletable = false)
{
var v = DefineVariable(name, deletable);
v.Assign(value);
v._attributes |= JSValueAttributesInternal.ReadOnly;
return v;
}

public int Count => _items.Count;

public JSValue Default
Expand Down
94 changes: 88 additions & 6 deletions Tests/ModuleTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NiL.JS;
using NiL.JS.Core;
using NiL.JS.Core.Interop;
using NiL.JS.Extensions;

namespace Tests
{
Expand All @@ -19,6 +21,85 @@ public void ModuleWithEmptyCodeShouldCreateContext()
Assert.IsNotNull(module.Context);
}

private class TestClass
{
private readonly string _lol;

public TestClass(string lol)
{
_lol = lol;
}

public TestClass(int number)
{
_lol = number.ToString();
}

public string Get() => _lol;
}

[TestMethod]
public void DefineConstructorShouldAddConstructorAndItsCallable()
{
var module = new Module("testclass","");

module.Exports.DefineConstructor(typeof(TestClass), "TestClass");
var mainModule = new Module("main",@"import {TestClass} from 'testclass'
var test = new TestClass('lol')
var result = test.Get()");
mainModule.ModuleResolversChain.Add(new DelegateModuleResolver((
(ModuleRequest request, out Module result) =>
{
if (request.AbsolutePath.Contains("testclass"))
{
result = module;
return true;
}
else
{
result = null;
return false;
}
})));
mainModule.Run();
Assert.AreEqual("lol", mainModule.Context.GetVariable("result").As<string>());
}


[TestMethod]
public void DefineVariableShouldAddAndItsCallable()
{
var module = new Module("testclass","");

module.Exports.DefineVariable("testFunc", true)
.Assign(GlobalContext.CurrentGlobalContext.ProxyValue(new Func<string>(() => "lol")));
module.Exports.DefineConstant("testFuncConst", new Func<string>(() => "const"));

var mainModule = new Module("main",@"import {testFunc, testFuncConst} from 'testclass'
var result = testFunc()
var result2 = testFuncConst()");

mainModule.ModuleResolversChain.Add(new DelegateModuleResolver((
(ModuleRequest request, out Module result) =>
{
if (request.AbsolutePath.Contains("testclass"))
{
result = module;
return true;
}
else
{
result = null;
return false;
}
})));
mainModule.Run();
Assert.AreEqual("lol", mainModule.Context.GetVariable("result").As<string>());
Assert.AreEqual("const", mainModule.Context.GetVariable("result2").As<string>());
}
[TestMethod]
public void ExportOperatorShouldAddItemToExportTable()
{
Expand All @@ -38,7 +119,8 @@ private sealed class DelegateModuleResolver : IModuleResolver

public DelegateModuleResolver(ModuleResolverDelegate moduleResolverDelegate)
{
_moduleResolverDelegate = moduleResolverDelegate ?? throw new ArgumentNullException(nameof(moduleResolverDelegate));
_moduleResolverDelegate = moduleResolverDelegate ??
throw new ArgumentNullException(nameof(moduleResolverDelegate));
}

public bool TryGetModule(ModuleRequest moduleRequest, out Module result)
Expand Down Expand Up @@ -120,7 +202,7 @@ public void DynamicImportOperatorShouldImportItem()

module2.Run();

for (; ; )
for (;;)
{
Thread.Sleep(1);
var imported = module2.Context.GetVariable("m");
Expand Down Expand Up @@ -155,7 +237,7 @@ public void DynamicImportOperatorShouldImportDefaultItem()

module2.Run();

for (; ; )
for (;;)
{
Thread.Sleep(1);
var imported = module2.Context.GetVariable("m");
Expand All @@ -180,8 +262,8 @@ public void ExecutionWithTimeout()
}
catch (TimeoutException)
{

}

stopWatch.Stop();

Assert.AreEqual(1, Math.Round(stopWatch.Elapsed.TotalSeconds));
Expand All @@ -200,11 +282,11 @@ public void ExecutionWithTimeout_ExceptionShouldNotBeCaughtByTryCatch()
}
catch (TimeoutException)
{

}

stopWatch.Stop();

Assert.AreEqual(1, Math.Round(stopWatch.Elapsed.TotalSeconds));
}
}
}
}

0 comments on commit de9cad3

Please sign in to comment.