Skip to content

Commit ae003e6

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore an attempt was made to fix a error #40 “Support For Native Promises”
1 parent ab48393 commit ae003e6

File tree

8 files changed

+115
-6
lines changed

8 files changed

+115
-6
lines changed

NuGet/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.nuspec

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2020
* JavaScriptEngineSwitcher.ChakraCore.Native.debian-x64
2121
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</description>
2222
<summary>JavaScriptEngineSwitcher.ChakraCore contains adapter `ChakraCoreJsEngine` (wrapper for the ChakraCore).</summary>
23-
<releaseNotes>1. ChakraCore was updated to version 1.7.2;
24-
2. Compilation error messages now contains a information about the error location.</releaseNotes>
23+
<releaseNotes>An attempt was made to fix a error #40 “Support For Native Promises”.</releaseNotes>
2524
<copyright>Copyright (c) 2013-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
2625
<language>en-US</language>
2726
<tags>JavaScriptEngineSwitcher JavaScript ECMAScript ChakraCore</tags>

NuGet/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
1. ChakraCore was updated to version 1.7.2;
34-
2. Compilation error messages now contains a information about the error
35-
location.
33+
An attempt was made to fix a error #40 “Support For Native Promises”.
3634

3735
=============
3836
DOCUMENTATION

src/JavaScriptEngineSwitcher.ChakraCore/ChakraCoreJsEngine.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,23 @@ private JsScope CreateJsScope()
207207
_jsRuntime.Disabled = false;
208208
}
209209

210-
return new JsScope(_jsContext);
210+
var jsScope = new JsScope(_jsContext);
211+
212+
JsRuntime.SetPromiseContinuationCallback(PromiseContinuationCallback, IntPtr.Zero);
213+
214+
return jsScope;
215+
}
216+
217+
/// <summary>
218+
/// The promise continuation callback
219+
/// </summary>
220+
/// <param name="task">The task, represented as a JavaScript function</param>
221+
/// <param name="callbackState">The data argument to be passed to the callback</param>
222+
private static void PromiseContinuationCallback(JsValue task, IntPtr callbackState)
223+
{
224+
task.AddRef();
225+
task.CallFunction(JsValue.GlobalObject);
226+
task.Release();
211227
}
212228

213229
#region Mapping

src/JavaScriptEngineSwitcher.ChakraCore/JsRt/JsRuntime.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,24 @@ public static JsRuntime Create(JsRuntimeAttributes attributes, JsThreadServiceCa
131131
return handle;
132132
}
133133

134+
/// <summary>
135+
/// Sets a promise continuation callback function that is called by the context when a task
136+
/// needs to be queued for future execution
137+
/// </summary>
138+
/// <remarks>
139+
/// <para>
140+
/// Requires an active script context.
141+
/// </para>
142+
/// </remarks>
143+
/// <param name="promiseContinuationCallback">The callback function being set</param>
144+
/// <param name="callbackState">User provided state that will be passed back to the callback</param>
145+
public static void SetPromiseContinuationCallback(JsPromiseContinuationCallback promiseContinuationCallback,
146+
IntPtr callbackState)
147+
{
148+
JsErrorHelpers.ThrowIfError(NativeMethods.JsSetPromiseContinuationCallback(promiseContinuationCallback,
149+
callbackState));
150+
}
151+
134152
/// <summary>
135153
/// Performs a full garbage collection
136154
/// </summary>

test/JavaScriptEngineSwitcher.Tests.Net4/JavaScriptEngineSwitcher.Tests.Net40.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@
9999
<Compile Include="..\JavaScriptEngineSwitcher.Tests\ChakraCore\CommonTests.cs">
100100
<Link>ChakraCore\CommonTests.cs</Link>
101101
</Compile>
102+
<Compile Include="..\JavaScriptEngineSwitcher.Tests\ChakraCore\Es2015Tests.cs">
103+
<Link>ChakraCore\Es2015Tests.cs</Link>
104+
</Compile>
102105
<Compile Include="..\JavaScriptEngineSwitcher.Tests\ChakraCore\Es5Tests.cs">
103106
<Link>ChakraCore\Es5Tests.cs</Link>
104107
</Compile>
@@ -108,6 +111,9 @@
108111
<Compile Include="..\JavaScriptEngineSwitcher.Tests\CommonTestsBase.cs">
109112
<Link>CommonTestsBase.cs</Link>
110113
</Compile>
114+
<Compile Include="..\JavaScriptEngineSwitcher.Tests\Es2015TestsBase.cs">
115+
<Link>Es2015TestsBase.cs</Link>
116+
</Compile>
111117
<Compile Include="..\JavaScriptEngineSwitcher.Tests\Es5TestsBase.cs">
112118
<Link>Es5TestsBase.cs</Link>
113119
</Compile>
@@ -186,6 +192,9 @@
186192
<Compile Include="..\JavaScriptEngineSwitcher.Tests\V8\CommonTests.cs">
187193
<Link>V8\CommonTests.cs</Link>
188194
</Compile>
195+
<Compile Include="..\JavaScriptEngineSwitcher.Tests\V8\Es2015Tests.cs">
196+
<Link>V8\Es2015Tests.cs</Link>
197+
</Compile>
189198
<Compile Include="..\JavaScriptEngineSwitcher.Tests\V8\Es5Tests.cs">
190199
<Link>V8\Es5Tests.cs</Link>
191200
</Compile>
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace JavaScriptEngineSwitcher.Tests.ChakraCore
2+
{
3+
public class Es2015Tests : Es2015TestsBase
4+
{
5+
protected override string EngineName
6+
{
7+
get { return "ChakraCoreJsEngine"; }
8+
}
9+
}
10+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System.Text;
2+
3+
using Xunit;
4+
5+
namespace JavaScriptEngineSwitcher.Tests
6+
{
7+
public abstract class Es2015TestsBase : TestsBase
8+
{
9+
#region Promises
10+
11+
[Fact]
12+
public virtual void ExecutionOfPromisesIsCorrect()
13+
{
14+
// Arrange
15+
var sb = new StringBuilder();
16+
const string input = @"var successfulWork = new Promise(function(resolve, reject) {
17+
resolve(""Resolved promise from JavaScript"");
18+
});
19+
20+
var unsuccessfulWork = new Promise(function (resolve, reject) {
21+
reject(""Rejected promise from JavaScript"");
22+
});
23+
24+
function resolveCallback(result) {
25+
console.AppendLine('Resolved: ' + result);
26+
}
27+
28+
function rejectCallback(reason) {
29+
console.AppendLine('Rejected: ' + reason);
30+
}
31+
32+
successfulWork.then(resolveCallback, rejectCallback);
33+
unsuccessfulWork.then(resolveCallback, rejectCallback);";
34+
35+
// Act
36+
using (var jsEngine = CreateJsEngine())
37+
{
38+
jsEngine.EmbedHostObject("sb", sb);
39+
jsEngine.Execute(input);
40+
}
41+
42+
// Assert
43+
}
44+
45+
#endregion
46+
}
47+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#if !NETCOREAPP1_0
2+
namespace JavaScriptEngineSwitcher.Tests.V8
3+
{
4+
public class Es2015Tests : Es2015TestsBase
5+
{
6+
protected override string EngineName
7+
{
8+
get { return "V8JsEngine"; }
9+
}
10+
}
11+
}
12+
#endif

0 commit comments

Comments
 (0)