Skip to content

Commit ae335fb

Browse files
committed
Add VroomJS JavaScript engine bindings (V8 for Mono)
1 parent 2021527 commit ae335fb

File tree

9 files changed

+378
-0
lines changed

9 files changed

+378
-0
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "site/packages"]
22
path = site/packages
33
url = https://github.com/Daniel15/simple-nuget-server.git
4+
[submodule "lib/VroomJs"]
5+
path = lib/VroomJs
6+
url = https://github.com/fogzot/vroomjs.git

lib/VroomJs

Submodule VroomJs added at 86f8558

lib/VroomJs.dll

16.5 KB
Binary file not shown.

lib/build-vroomjs.bat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@echo off
2+
"%ProgramFiles(x86)%\MSBuild\12.0\Bin\MSBuild.exe" VroomJs\VroomJs\VroomJs.csproj /p:BuildProjectReferences=false /p:Configuration=Release /p:OutputPath=..\..\ /p:SignAssembly=true /p:AssemblyOriginatorKeyFile=..\..\..\src\Key.snk
3+
pause
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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.Reflection;
11+
using System.Runtime.InteropServices;
12+
13+
[assembly: AssemblyTitle("React.JavaScriptEngine.VroomJs")]
14+
[assembly: AssemblyDescription("VroomJs JavaScript library for ReactJS.NET")]
15+
[assembly: ComVisible(false)]
16+
[assembly: Guid("3c246f9a-d9ae-4a43-86fb-00d4591e2a38")]
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{B4A5902A-70E2-4FA4-817D-DCC78D31E9B9}</ProjectGuid>
8+
<OutputType>Library</OutputType>
9+
<AppDesignerFolder>Properties</AppDesignerFolder>
10+
<RootNamespace>React.JavaScriptEngine.VroomJs</RootNamespace>
11+
<AssemblyName>React.JavaScriptEngine.VroomJs.original</AssemblyName>
12+
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
13+
<FileAlignment>512</FileAlignment>
14+
</PropertyGroup>
15+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
16+
<DebugSymbols>true</DebugSymbols>
17+
<DebugType>full</DebugType>
18+
<Optimize>false</Optimize>
19+
<OutputPath>..\..\bin\Debug\React.JavaScriptEngine.VroomJs\</OutputPath>
20+
<DefineConstants>DEBUG;TRACE</DefineConstants>
21+
<ErrorReport>prompt</ErrorReport>
22+
<WarningLevel>4</WarningLevel>
23+
<DocumentationFile>..\..\bin\Debug\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.original.xml</DocumentationFile>
24+
<NoWarn>1607</NoWarn>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<DebugType>pdbonly</DebugType>
28+
<Optimize>true</Optimize>
29+
<OutputPath>..\..\bin\Release\React.JavaScriptEngine.VroomJs\</OutputPath>
30+
<DefineConstants>TRACE</DefineConstants>
31+
<ErrorReport>prompt</ErrorReport>
32+
<WarningLevel>4</WarningLevel>
33+
<DocumentationFile>..\..\bin\Release\React.JavaScriptEngine.VroomJs\React.JavaScriptEngine.VroomJs.original.xml</DocumentationFile>
34+
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
35+
<NoWarn>1607</NoWarn>
36+
</PropertyGroup>
37+
<PropertyGroup>
38+
<SignAssembly>true</SignAssembly>
39+
</PropertyGroup>
40+
<PropertyGroup>
41+
<AssemblyOriginatorKeyFile>..\Key.snk</AssemblyOriginatorKeyFile>
42+
</PropertyGroup>
43+
<ItemGroup>
44+
<Reference Include="JavaScriptEngineSwitcher.Core">
45+
<HintPath>..\packages\JavaScriptEngineSwitcher.Core.1.1.3\lib\net40\JavaScriptEngineSwitcher.Core.dll</HintPath>
46+
</Reference>
47+
<Reference Include="Newtonsoft.Json, Version=4.5.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
48+
<HintPath>..\packages\Newtonsoft.Json.5.0.4\lib\net40\Newtonsoft.Json.dll</HintPath>
49+
</Reference>
50+
<Reference Include="System" />
51+
<Reference Include="System.Core" />
52+
<Reference Include="Microsoft.CSharp" />
53+
<Reference Include="VroomJs">
54+
<HintPath>..\..\lib\VroomJs.dll</HintPath>
55+
<Private>True</Private>
56+
</Reference>
57+
</ItemGroup>
58+
<ItemGroup>
59+
<Compile Include="..\SharedAssemblyInfo.cs">
60+
<Link>Properties\SharedAssemblyInfo.cs</Link>
61+
</Compile>
62+
<Compile Include="..\SharedAssemblyVersionInfo.cs">
63+
<Link>Properties\SharedAssemblyVersionInfo.cs</Link>
64+
</Compile>
65+
<Compile Include="Properties\AssemblyInfo.cs" />
66+
<Compile Include="VroomJsEngine.cs" />
67+
</ItemGroup>
68+
<ItemGroup>
69+
<None Include="packages.config" />
70+
</ItemGroup>
71+
<ItemGroup>
72+
<ProjectReference Include="..\React\React.csproj">
73+
<Project>{d0cc8a22-cee6-485c-924b-1f94426fea59}</Project>
74+
<Name>React</Name>
75+
</ProjectReference>
76+
</ItemGroup>
77+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
78+
<PropertyGroup>
79+
<PostBuildEvent>
80+
</PostBuildEvent>
81+
</PropertyGroup>
82+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
83+
Other similar extension points exist, see Microsoft.Common.targets.
84+
<Target Name="BeforeBuild">
85+
</Target>
86+
<Target Name="AfterBuild">
87+
</Target>
88+
-->
89+
</Project>
Lines changed: 254 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,254 @@
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.Linq;
12+
using JavaScriptEngineSwitcher.Core;
13+
using Newtonsoft.Json;
14+
15+
using OriginalJsEngine = VroomJs.JsEngine;
16+
using OriginalJsException = VroomJs.JsException;
17+
18+
namespace React.JavaScriptEngine.VroomJs
19+
{
20+
/// <summary>
21+
/// Basic implementation of a VroomJs JavaScript engine for JavaScriptEngineSwitcher.
22+
/// Connects to the V8 JavaScript engine on Linux and Mac OS X.
23+
/// </summary>
24+
public class VroomJsEngine : JsEngineBase
25+
{
26+
/// <summary>
27+
/// The VroomJs engine
28+
/// </summary>
29+
private readonly OriginalJsEngine _jsEngine;
30+
31+
/// <summary>
32+
/// Name of JavaScript engine
33+
/// </summary>
34+
public override string Name
35+
{
36+
get { return "VroomJs JavaScript engine"; }
37+
}
38+
39+
/// <summary>
40+
/// Version of original JavaScript engine
41+
/// </summary>
42+
public override string Version
43+
{
44+
get { return "86f8558d (Aug 17, 2013)"; }
45+
}
46+
47+
/// <summary>
48+
/// Constructs instance of adapter for VroomJs
49+
/// </summary>
50+
public VroomJsEngine()
51+
{
52+
try
53+
{
54+
_jsEngine = new OriginalJsEngine();
55+
}
56+
catch (Exception e)
57+
{
58+
59+
throw new JsEngineLoadException(
60+
string.Format(
61+
JavaScriptEngineSwitcher.Core.Resources.Strings.Runtime_JsEngineNotLoaded,
62+
Name,
63+
e.Message
64+
),
65+
Name,
66+
Version,
67+
e
68+
);
69+
}
70+
}
71+
72+
/// <summary>
73+
/// Converts a VroomJs exception into a <see cref="JsRuntimeException" />.
74+
/// </summary>
75+
/// <param name="jsException">VroomJs exception</param>
76+
/// <returns>JavaScriptEngineSwitcher exception</returns>
77+
private JsRuntimeException ConvertJavaScriptExceptionToJsRuntimeException(
78+
OriginalJsException jsException
79+
)
80+
{
81+
dynamic nativeEx = jsException.NativeException;
82+
return new JsRuntimeException(nativeEx.stack, Name, Version)
83+
{
84+
Category = nativeEx.name,
85+
};
86+
}
87+
88+
/// <summary>
89+
/// Evaluates the specified JavaScript expression
90+
/// </summary>
91+
/// <param name="expression">Expression to evaluate</param>
92+
/// <returns>Result of the expression</returns>
93+
protected override object InnerEvaluate(string expression)
94+
{
95+
try
96+
{
97+
return _jsEngine.Execute(expression);
98+
}
99+
catch (OriginalJsException ex)
100+
{
101+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Evaluates the specified JavaScript expression
107+
/// </summary>
108+
/// <param name="expression">Expression to evaluate</param>
109+
/// <typeparam name="T">Return type</typeparam>
110+
/// <returns>Result of the expression</returns>
111+
protected override T InnerEvaluate<T>(string expression)
112+
{
113+
return (T) InnerEvaluate(expression);
114+
}
115+
116+
/// <summary>
117+
/// Executes the specified JavaScript code
118+
/// </summary>
119+
/// <param name="code">Code to execute</param>
120+
protected override void InnerExecute(string code)
121+
{
122+
try
123+
{
124+
_jsEngine.Execute(code);
125+
}
126+
catch (OriginalJsException ex)
127+
{
128+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
129+
}
130+
}
131+
132+
/// <summary>
133+
/// Calls the specified JavaScript function
134+
/// </summary>
135+
/// <param name="functionName">Function to call</param>
136+
/// <param name="args">Arguments to pass to function</param>
137+
/// <returns>Result of the function</returns>
138+
protected override object InnerCallFunction(string functionName, params object[] args)
139+
{
140+
var code = string.Format(
141+
"{0}({1})",
142+
functionName,
143+
string.Join(", ", args.Select(JsonConvert.SerializeObject))
144+
);
145+
146+
try
147+
{
148+
return _jsEngine.Execute(code);
149+
}
150+
catch (OriginalJsException ex)
151+
{
152+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
153+
}
154+
}
155+
156+
/// <summary>
157+
/// Calls the specified JavaScript function
158+
/// </summary>
159+
/// <param name="functionName">Function to call</param>
160+
/// <param name="args">Arguments to pass to function</param>
161+
/// <typeparam name="T">Return type of the function</typeparam>
162+
/// <returns>Result of the function</returns>
163+
protected override T InnerCallFunction<T>(string functionName, params object[] args)
164+
{
165+
return (T) InnerCallFunction(functionName, args);
166+
}
167+
168+
/// <summary>
169+
/// Determines if the specified variable has been set
170+
/// </summary>
171+
/// <param name="variableName">Name of the variable</param>
172+
/// <returns><c>true</c> if the variable is defined</returns>
173+
protected override bool InnerHasVariable(string variableName)
174+
{
175+
var code = string.Format("typeof {0} !== 'undefined'", variableName);
176+
return InnerEvaluate<bool>(code);
177+
}
178+
179+
/// <summary>
180+
/// Gets the value of the specified variable
181+
/// </summary>
182+
/// <param name="variableName">Variable to get value of</param>
183+
/// <returns>Value</returns>
184+
protected override object InnerGetVariableValue(string variableName)
185+
{
186+
try
187+
{
188+
return _jsEngine.GetVariable(variableName);
189+
}
190+
catch (OriginalJsException ex)
191+
{
192+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
193+
}
194+
}
195+
196+
/// <summary>
197+
/// Gets the value of the specified variable
198+
/// </summary>
199+
/// <param name="variableName">Variable to get value of</param>
200+
/// <typeparam name="T">Type of the variable</typeparam>
201+
/// <returns>Value</returns>
202+
protected override T InnerGetVariableValue<T>(string variableName)
203+
{
204+
return (T)InnerGetVariableValue(variableName);
205+
}
206+
207+
/// <summary>
208+
/// Sets the value of the specified variable
209+
/// </summary>
210+
/// <param name="variableName">Variable to get value of</param>
211+
/// <param name="value">New value to set</param>
212+
/// <returns>Value</returns>
213+
protected override void InnerSetVariableValue(string variableName, object value)
214+
{
215+
try
216+
{
217+
_jsEngine.SetVariable(variableName, value);
218+
}
219+
catch (OriginalJsException ex)
220+
{
221+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
222+
}
223+
}
224+
225+
/// <summary>
226+
/// Deletes a variable
227+
/// </summary>
228+
/// <param name="variableName">Variable to delete</param>
229+
protected override void InnerRemoveVariable(string variableName)
230+
{
231+
var code = string.Format("{0} = undefined", variableName);
232+
try
233+
{
234+
_jsEngine.Execute(code);
235+
}
236+
catch (OriginalJsException ex)
237+
{
238+
throw ConvertJavaScriptExceptionToJsRuntimeException(ex);
239+
}
240+
}
241+
242+
/// <summary>
243+
/// Releases resources used by this engine.
244+
/// </summary>
245+
public override void Dispose()
246+
{
247+
if (!_disposed)
248+
{
249+
_jsEngine.Dispose();
250+
_disposed = true;
251+
}
252+
}
253+
}
254+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="JavaScriptEngineSwitcher.Core" version="1.1.3" targetFramework="net40" />
4+
<package id="Newtonsoft.Json" version="5.0.4" targetFramework="net40" />
5+
</packages>

0 commit comments

Comments
 (0)