Skip to content

Commit 1891c57

Browse files
author
Eric Swann
committed
Updates to allow loading of external scripts and added tests
1 parent d2c413f commit 1891c57

13 files changed

Lines changed: 393 additions & 40 deletions

README.md

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,13 @@ These settings can be set manually or added to the AppSettings of your applicati
3535
//V8 Max Executable Size in bytes:
3636
int MaxExecutableBytes { get; }
3737

38-
//V8 Max Young Space in bytes:
39-
int MaxYoungSpaceBytes { get; }
40-
41-
//V8 Max Old Space in bytes:
38+
//V8 Max New Space in bytes:
39+
int MaxNewSpaceBytes { get; }
40+
41+
//V8 Max Old Space in bytes:
4242
int MaxOldSpaceBytes { get; }
4343
44-
//Default script timeout in ms:
44+
//Default script timeout in ms. If the script timeout is set to 0, the script will not execute within a Task with a timeout.
4545
int ScriptTimeoutMilliSeconds { get; }
4646

4747
//Max number of simultaneous V8 Runtimes:
@@ -61,15 +61,32 @@ These settings can be set manually or added to the AppSettings of your applicati
6161
var manager = new RuntimeManager(new ManagerSettings());
6262
await manager.ExecuteAsync("test", "var i = 0; i++;");
6363

64-
### Passing in Host Objects
64+
### Execution Options
65+
Execution options are the new way of passing options to the ClearScript runtime manager.
66+
67+
/// Objects to inject into the JavaScript runtime.
68+
public IEnumerable<HostObject> HostObjects
69+
70+
/// Types to make available to the JavaScript runtime.
71+
public IEnumerable<HostType> HostTypes
72+
73+
/// Indicates that this script should be added to the script cache once compiled. Default is True.
74+
public bool AddToCache
75+
76+
/// External JavaScripts to import before executing the current script.
77+
public IList<IncludeScript> Scripts
78+
79+
#### Passing in Host Objects
80+
Host objects are a way of passing an object instance to JavaScript from .Net.
6581

6682
var subject = new TestObject{Name = "Name", Count = 0};
6783
var manager = new RuntimeManager(new ManualManagerSettings());
6884

6985
await manager.ExecuteAsync("testscript", "subject.Count = 10;",
70-
new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, null);
86+
new ExecutionOptions{HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}}});
7187

72-
### Passing in Host Types
88+
#### Passing in Host Types
89+
Host types allow you to instantiate a .Net type or types in the JavaScript runtime.
7390

7491
var manager = new RuntimeManager(new ManagerSettings());
7592
var hostType = new HostType
@@ -80,8 +97,32 @@ These settings can be set manually or added to the AppSettings of your applicati
8097
var subject = new TestObject();
8198

8299
await manager.ExecuteAsync("testscript", "subject.Result = MathStuff.Pow(10,2);",
83-
new List<HostObject> { new HostObject { Name = "subject", Target = subject } },
84-
new List<HostType> { hostType });
100+
new ExecutionOptions{
101+
HostObjects = new List<HostObject> { new HostObject { Name = "subject", Target = subject } },
102+
HostTypes = new List<HostType> { hostType }});
103+
104+
#### Passing in External Scripts
105+
External scripts can also be run by setting the IncludeScripts property on the ExecutionOptions. These scripts are
106+
intended to set up reused libraries and will be run before the execution of the main script.
107+
108+
A script can be set up in a couple of ways:
109+
* Set a file path or url in the Uri property.
110+
* Set the code property to the code to execute.
111+
112+
The script will be compiled and cached in the same way as a normal script.
113+
114+
public class IncludeScript
115+
{
116+
/// Unique name of the script to execute.
117+
public string Name { get; set; }
118+
119+
/// Uri (file or Url) of the script to execute. Need to include script code or script Url.
120+
public string Uri { get; set; }
121+
122+
/// Code of the script to include. Need to include script code or script Url.
123+
public string Code { get; set; }
124+
}
125+
85126

86127
### Using the Manager Pool
87128

src/ClearScript.Manager.Test/ClearScript.Manager.Test.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
<Compile Include="V8ScriptingTests.cs" />
5555
<Compile Include="WhenCachingScripts.cs" />
5656
<Compile Include="WhenExecutingBasicJavaScript.cs" />
57+
<Compile Include="WhenExecutingScriptWithIncludeScripts.cs" />
5758
<Compile Include="WhenExecutingScriptWithTimeout.cs" />
5859
<Compile Include="WhenExecutingWithHostObjects.cs" />
5960
<Compile Include="WhenExecutingWithHostTypes.cs">
@@ -97,6 +98,9 @@
9798
<Content Include="Lib\v8-x64.pdb">
9899
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
99100
</Content>
101+
<Content Include="TestIncludeScript.js">
102+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
103+
</Content>
100104
</ItemGroup>
101105
<ItemGroup>
102106
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
var x = 'test string1';
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using System.Collections.Generic;
2+
using NUnit.Framework;
3+
using Should;
4+
5+
namespace ClearScript.Manager.Test
6+
{
7+
[TestFixture]
8+
public class WhenExecutingScriptWithIncludeScripts
9+
{
10+
[Test]
11+
public async void Script_With_String_Code_Include_Is_Included()
12+
{
13+
var preScript = "var x = 'test string1';";
14+
15+
var subject = new TestObject();
16+
var manager = new RuntimeManager(new ManualManagerSettings());
17+
18+
await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;",
19+
new ExecutionOptions
20+
{
21+
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
22+
Scripts = new List<IncludeScript> {new IncludeScript {Code = preScript, Name = "testScript"}}
23+
});
24+
25+
26+
subject.Name.ShouldEqual("Name");
27+
subject.Count.ShouldEqual(10);
28+
subject.TestString.ShouldEqual("test string1");
29+
}
30+
31+
[Test]
32+
public async void Script_With_File_Include_Is_Included()
33+
{
34+
var subject = new TestObject();
35+
var manager = new RuntimeManager(new ManualManagerSettings());
36+
37+
await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;",
38+
new ExecutionOptions
39+
{
40+
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
41+
Scripts = new List<IncludeScript> {new IncludeScript {Uri = ".\\TestIncludeScript.js", Name = "testScript"}}
42+
});
43+
44+
subject.Name.ShouldEqual("Name");
45+
subject.Count.ShouldEqual(10);
46+
subject.TestString.ShouldEqual("test string1");
47+
}
48+
49+
[Test]
50+
public async void Script_With_Http_Include_Is_Included()
51+
{
52+
var subject = new TestObject();
53+
var manager = new RuntimeManager(new ManualManagerSettings());
54+
55+
await manager.ExecuteAsync("testscript", "subject.Count = 10; subject.TestString = x;",
56+
new ExecutionOptions
57+
{
58+
HostObjects = new List<HostObject> {new HostObject {Name = "subject", Target = subject}},
59+
Scripts = new List<IncludeScript> {new IncludeScript {Uri = "http://localhost:9510/TestIncludeScript.js", Name = "testScript"}}
60+
});
61+
62+
subject.Name.ShouldEqual("Name");
63+
subject.Count.ShouldEqual(10);
64+
subject.TestString.ShouldEqual("test string1");
65+
}
66+
67+
public class TestObject
68+
{
69+
private string _name = "Name";
70+
71+
public string Name
72+
{
73+
get { return _name; }
74+
set { _name = value; }
75+
}
76+
77+
public string TestString { get; set; }
78+
79+
public int Count { get; set; }
80+
}
81+
}
82+
}

src/ClearScript.Manager.Test/WhenExecutingScriptWithTimeout.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,40 @@ public void Script_Exceeding_Timeout_Errs()
2323

2424
}
2525

26+
27+
[Test]
28+
public async void Script_Not_Exceeding_Timeout_Works()
29+
{
30+
var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 500 })
31+
{
32+
AddConsoleReference = true
33+
};
34+
35+
var script = "Console.WriteLine('Started:' + new Date().toJSON()); " +
36+
"var now = new Date().getTime(); " +
37+
"Console.WriteLine('Finished:' + new Date().toJSON());";
38+
39+
40+
await manager.ExecuteAsync("test", script);
41+
42+
}
43+
44+
[Test]
45+
public async void Script_With_0_Timeout_Works()
46+
{
47+
var manager = new RuntimeManager(new ManualManagerSettings { ScriptTimeoutMilliSeconds = 0 })
48+
{
49+
AddConsoleReference = true
50+
};
51+
52+
var script = "Console.WriteLine('Started:' + new Date().toJSON()); " +
53+
"var now = new Date().getTime(); " +
54+
"Console.WriteLine('Finished:' + new Date().toJSON());";
55+
56+
57+
await manager.ExecuteAsync("test", script);
58+
59+
}
60+
2661
}
2762
}

src/ClearScript.Manager.Test/WhenExecutingWithHostObjects.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public async void Script_With_Typed_Subject_Can_Be_Executed()
1616
var manager = new RuntimeManager(new ManualManagerSettings());
1717

1818
await manager.ExecuteAsync("testscript", "subject.Count = 10;",
19-
new List<HostObject> {new HostObject {Name = "subject", Target = subject}}, null);
19+
new List<HostObject> {new HostObject {Name = "subject", Target = subject}});
2020

2121
subject.Name.ShouldEqual("Name");
2222
subject.Count.ShouldEqual(10);
@@ -32,7 +32,7 @@ public async void Script_With_Dynamic_Subject_Can_Be_Executed()
3232
var manager = new RuntimeManager(new ManualManagerSettings());
3333

3434
await manager.ExecuteAsync("test", "subject.Count = 10;",
35-
new List<HostObject> { new HostObject { Name = "subject", Target = subject } }, null);
35+
new List<HostObject> { new HostObject { Name = "subject", Target = subject } });
3636

3737
//Should craps out on dynamics
3838
Assert.AreEqual(subject.Name, "Name");

src/ClearScript.Manager/ClearScript.Manager.csproj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
<Reference Include="System" />
4141
<Reference Include="System.Configuration" />
4242
<Reference Include="System.Core" />
43+
<Reference Include="System.Net.Http" />
44+
<Reference Include="System.Net.Http.Extensions">
45+
<HintPath>..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Extensions.dll</HintPath>
46+
</Reference>
47+
<Reference Include="System.Net.Http.Primitives">
48+
<HintPath>..\packages\Microsoft.Net.Http.2.2.28\lib\net45\System.Net.Http.Primitives.dll</HintPath>
49+
</Reference>
50+
<Reference Include="System.Net.Http.WebRequest" />
4351
<Reference Include="System.Xml.Linq" />
4452
<Reference Include="System.Data.DataSetExtensions" />
4553
<Reference Include="Microsoft.CSharp" />
@@ -53,6 +61,8 @@
5361
<Compile Include="Caching\IDictionaryEx.cs" />
5462
<Compile Include="Caching\LruCache.cs" />
5563
<Compile Include="Caching\LruCacheCorruptionException.cs" />
64+
<Compile Include="ExecutionOptions.cs" />
65+
<Compile Include="IncludeScript.cs" />
5666
<Compile Include="HostObject.cs" />
5767
<Compile Include="HostType.cs" />
5868
<Compile Include="Properties\AssemblyInfo.cs" />
@@ -70,6 +80,11 @@
7080
<PostBuildEvent>
7181
</PostBuildEvent>
7282
</PropertyGroup>
83+
<Import Project="..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets" Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" />
84+
<Target Name="EnsureBclBuildImported" BeforeTargets="BeforeBuild" Condition="'$(BclBuildImported)' == ''">
85+
<Error Condition="!Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=317567." HelpKeyword="BCLBUILD2001" />
86+
<Error Condition="Exists('..\packages\Microsoft.Bcl.Build.1.0.14\tools\Microsoft.Bcl.Build.targets')" Text="The build restored NuGet packages. Build the project again to include these packages in the build. For more information, see http://go.microsoft.com/fwlink/?LinkID=317568." HelpKeyword="BCLBUILD2002" />
87+
</Target>
7388
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
7489
Other similar extension points exist, see Microsoft.Common.targets.
7590
<Target Name="BeforeBuild">

src/ClearScript.Manager/ClearScript.Manager.nuspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
<tags>ClearScript V8 JavaScript</tags>
1616
<dependencies>
1717
<dependency id="ClearScript.Installer" version="1.0.0" />
18+
<dependency id="Microsoft.Net.Http" version="2.2.28" />
1819
</dependencies>
1920
</metadata>
2021
<files>
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
3+
namespace ClearScript.Manager
4+
{
5+
/// <summary>
6+
/// Options for execution of of scripts by the Runtime Manager
7+
/// </summary>
8+
public class ExecutionOptions
9+
{
10+
private bool _addToCache = true;
11+
12+
/// <summary>
13+
/// Objects to inject into the JavaScript runtime.
14+
/// </summary>
15+
public IEnumerable<HostObject> HostObjects { get; set; }
16+
17+
/// <summary>
18+
/// Types to make available to the JavaScript runtime.
19+
/// </summary>
20+
public IEnumerable<HostType> HostTypes { get; set; }
21+
22+
/// <summary>
23+
/// Indicates that this script should be added to the script cache once compiled. Default is True.
24+
/// </summary>
25+
public bool AddToCache
26+
{
27+
get { return _addToCache; }
28+
set { _addToCache = value; }
29+
}
30+
31+
/// <summary>
32+
/// External JavaScripts to import before executing the current script.
33+
/// </summary>
34+
public IList<IncludeScript> Scripts { get; set; }
35+
}
36+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
namespace ClearScript.Manager
2+
{
3+
/// <summary>
4+
/// Script to include in the execution of another script. This script is executed first. Can contain functions, libraries etc...
5+
/// </summary>
6+
public class IncludeScript
7+
{
8+
/// <summary>
9+
/// Unique name of the script to execute.
10+
/// </summary>
11+
public string Name { get; set; }
12+
13+
/// <summary>
14+
/// Uri (file or Url) of the script to execute. Need to include script code or script Url.
15+
/// </summary>
16+
public string Uri { get; set; }
17+
18+
/// <summary>
19+
/// Code of the script to include. Need to include script code or script Url.
20+
/// </summary>
21+
public string Code { get; set; }
22+
}
23+
}

0 commit comments

Comments
 (0)