Skip to content

Commit 33c230f

Browse files
committed
Add assembly registration for VroomJs. Throw exception if error occurs while initialising on a supported environment (Linux or Mac)
1 parent 5a83d33 commit 33c230f

File tree

5 files changed

+146
-3
lines changed

5 files changed

+146
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright (c) 2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using React.TinyIoC;
11+
12+
namespace React.JavaScriptEngine.VroomJs
13+
{
14+
/// <summary>
15+
/// Handles registration of VroomJS for ReactJS.NET.
16+
/// </summary>
17+
public class AssemblyRegistration : IAssemblyRegistration
18+
{
19+
/// <summary>
20+
/// Registers components in the React IoC container
21+
/// </summary>
22+
/// <param name="container">Container to register components in</param>
23+
public void Register(TinyIoCContainer container)
24+
{
25+
// Only supported on Linux or Mac
26+
if (!VroomJsUtils.IsEnvironmentSupported())
27+
{
28+
return;
29+
}
30+
31+
JavaScriptEngineFactory.AddFactoryWithPriority(() => new VroomJsEngine(), priority: 10);
32+
VroomJsUtils.EnsureEngineFunctional();
33+
}
34+
}
35+
}

src/React.JavaScriptEngine.VroomJs/React.JavaScriptEngine.VroomJs.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
<OutputType>Library</OutputType>
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>React.JavaScriptEngine.VroomJs</RootNamespace>
11-
<AssemblyName>React.JavaScriptEngine.VroomJs.original</AssemblyName>
11+
<AssemblyName>React.JavaScriptEngine.VroomJs</AssemblyName>
1212
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
</PropertyGroup>
@@ -20,7 +20,7 @@
2020
<DefineConstants>DEBUG;TRACE</DefineConstants>
2121
<ErrorReport>prompt</ErrorReport>
2222
<WarningLevel>4</WarningLevel>
23-
<DocumentationFile>..\..\bin\Debug\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.original.xml</DocumentationFile>
23+
<DocumentationFile>..\..\bin\Debug\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.xml</DocumentationFile>
2424
<NoWarn>1607</NoWarn>
2525
</PropertyGroup>
2626
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
@@ -30,7 +30,7 @@
3030
<DefineConstants>TRACE</DefineConstants>
3131
<ErrorReport>prompt</ErrorReport>
3232
<WarningLevel>4</WarningLevel>
33-
<DocumentationFile>..\..\bin\Release\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.original.xml</DocumentationFile>
33+
<DocumentationFile>..\..\bin\Release\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.xml</DocumentationFile>
3434
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
3535
<NoWarn>1607</NoWarn>
3636
</PropertyGroup>
@@ -62,8 +62,11 @@
6262
<Compile Include="..\SharedAssemblyVersionInfo.cs">
6363
<Link>Properties\SharedAssemblyVersionInfo.cs</Link>
6464
</Compile>
65+
<Compile Include="AssemblyRegistration.cs" />
6566
<Compile Include="Properties\AssemblyInfo.cs" />
6667
<Compile Include="VroomJsEngine.cs" />
68+
<Compile Include="VroomJsInitialisationException.cs" />
69+
<Compile Include="VroomJsUtils.cs" />
6770
</ItemGroup>
6871
<ItemGroup>
6972
<None Include="packages.config" />
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using System.Runtime.Serialization;
12+
using React.Exceptions;
13+
14+
namespace React.JavaScriptEngine.VroomJs
15+
{
16+
/// <summary>
17+
/// Thrown when the JavaScript engine does not support JSX transformation
18+
/// </summary>
19+
[Serializable]
20+
public class VroomJsInitialisationException : ReactException
21+
{
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="VroomJsInitialisationException"/> class.
24+
/// </summary>
25+
public VroomJsInitialisationException(string innerMessage) :
26+
base(GetMessage(innerMessage)) { }
27+
28+
/// <summary>
29+
/// Used by deserialization
30+
/// </summary>
31+
protected VroomJsInitialisationException(SerializationInfo info, StreamingContext context)
32+
: base(info, context) { }
33+
34+
/// <summary>
35+
/// Gets a message that describes the current exception.
36+
/// </summary>
37+
private static string GetMessage(string innerMessage)
38+
{
39+
return
40+
"Failed to initialise VroomJs. This is most likely caused by the native library " +
41+
"(libvroomjs.so) being out of date or your system lacking a compatible version of " +
42+
"V8. Please run Mono with the `MONO_LOG_LEVEL=debug` environment variable for " +
43+
"more debugging information. \n\n " +
44+
"More details: " + innerMessage;
45+
}
46+
}
47+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2014, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
10+
using System;
11+
using React.Exceptions;
12+
13+
namespace React.JavaScriptEngine.VroomJs
14+
{
15+
/// <summary>
16+
/// Utility methods for VroomJs JavaScript engine
17+
/// </summary>
18+
public static class VroomJsUtils
19+
{
20+
/// <summary>
21+
/// Determines if the current environment supports the VroomJs engine
22+
/// </summary>
23+
/// <returns></returns>
24+
public static bool IsEnvironmentSupported()
25+
{
26+
return Environment.OSVersion.Platform == PlatformID.Unix;
27+
}
28+
29+
/// <summary>
30+
/// If the user is explicitly referencing this assembly, they probably want to use it.
31+
/// Attempt to use the engine and throw an exception if it doesn't work.
32+
/// </summary>
33+
public static void EnsureEngineFunctional()
34+
{
35+
int result = 0;
36+
try
37+
{
38+
using (var engine = new VroomJsEngine())
39+
{
40+
result = engine.Evaluate<int>("1 + 1");
41+
}
42+
}
43+
catch (Exception ex)
44+
{
45+
throw new VroomJsInitialisationException(ex.Message);
46+
}
47+
48+
if (result != 2)
49+
{
50+
throw new ReactException("Mathematics is broken. 1 + 1 = " + result);
51+
}
52+
}
53+
}
54+
}

src/React.Sample.Mvc4/React.Sample.Mvc4.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@
136136
<Content Include="Views\Web.config" />
137137
</ItemGroup>
138138
<ItemGroup>
139+
<ProjectReference Include="..\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.csproj">
140+
<Project>{b4a5902a-70e2-4fa4-817d-dcc78d31e9b9}</Project>
141+
<Name>React.JavaScriptEngine.VroomJs</Name>
142+
</ProjectReference>
139143
<ProjectReference Include="..\React.Web.Mvc4\React.Web.Mvc4.csproj">
140144
<Project>{662d52ac-1ee9-4372-bd74-379f9ac56451}</Project>
141145
<Name>React.Web.Mvc4</Name>

0 commit comments

Comments
 (0)