Added null and decimal marshaling options to WindowsScriptEngine.

This commit is contained in:
ClearScript 2014-03-12 23:57:53 -04:00
Родитель aa16a8a84b
Коммит 7df5fa298b
4 изменённых файлов: 87 добавлений и 1 удалений

Просмотреть файл

@ -426,6 +426,11 @@ namespace Microsoft.ClearScript.Windows
{
if (obj == null)
{
if (engineFlags.HasFlag(WindowsScriptEngineFlags.MarshalNullAsDispatch))
{
return new DispatchWrapper(null);
}
return DBNull.Value;
}
@ -439,6 +444,11 @@ namespace Microsoft.ClearScript.Windows
return null;
}
if (engineFlags.HasFlag(WindowsScriptEngineFlags.MarshalDecimalAsCurrency) && (obj is decimal))
{
return new CurrencyWrapper(obj);
}
var hostItem = obj as HostItem;
if (hostItem != null)
{

Просмотреть файл

@ -92,6 +92,16 @@ namespace Microsoft.ClearScript.Windows
/// <summary>
/// Specifies that script language features that enhance standards compliance are to be enabled. This option only affects <see cref="JScriptEngine"/>.
/// </summary>
EnableStandardsMode = 0x00000008
EnableStandardsMode = 0x00000008,
/// <summary>
/// Specifies that <c>null</c> is to be marshaled as a variant of type <c>VT_DISPATCH</c>.
/// </summary>
MarshalNullAsDispatch = 0x00000010,
/// <summary>
/// Specifies that <see cref="decimal"/> values are to be marshaled as variants of type <c>VT_CY</c>.
/// </summary>
MarshalDecimalAsCurrency = 0x00000020
}
}

Просмотреть файл

@ -967,9 +967,44 @@ namespace Microsoft.ClearScript.Test
[TestMethod, TestCategory("JScriptEngine")]
public void JScriptEngine_StandardsMode()
{
// ReSharper disable AccessToDisposedClosure
TestUtil.AssertException<ScriptEngineException>(() => engine.Evaluate("JSON"));
engine.Dispose();
engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.EnableStandardsMode);
Assert.AreEqual("{\"foo\":123,\"bar\":456.789}", engine.Evaluate("JSON.stringify({ foo: 123, bar: 456.789 })"));
// ReSharper restore AccessToDisposedClosure
}
[TestMethod, TestCategory("JScriptEngine")]
public void JScriptEngine_MarshalNullAsDispatch()
{
engine.Script.func = new Func<object>(() => null);
Assert.IsTrue((bool)engine.Evaluate("func() === null"));
engine.Dispose();
engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalNullAsDispatch);
engine.Script.func = new Func<object>(() => null);
Assert.IsTrue((bool)engine.Evaluate("func() === null"));
}
[TestMethod, TestCategory("JScriptEngine")]
public void JScriptEngine_MarshalDecimalAsCurrency()
{
engine.Script.func = new Func<object>(() => 123.456M);
Assert.AreEqual("number", engine.Evaluate("typeof(func())"));
Assert.AreEqual(123.456 + 5, engine.Evaluate("func() + 5"));
engine.Dispose();
engine = new JScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalDecimalAsCurrency);
engine.Script.func = new Func<object>(() => 123.456M);
Assert.AreEqual("number", engine.Evaluate("typeof(func())"));
Assert.AreEqual(123.456 + 5, engine.Evaluate("func() + 5"));
}
// ReSharper restore InconsistentNaming

Просмотреть файл

@ -912,6 +912,37 @@ namespace Microsoft.ClearScript.Test
VBScriptEngine_Execute();
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_MarshalNullAsDispatch()
{
engine.Script.func = new Func<object>(() => null);
Assert.IsTrue((bool)engine.Evaluate("IsNull(func())"));
engine.Dispose();
engine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalNullAsDispatch);
engine.Script.func = new Func<object>(() => null);
Assert.IsTrue((bool)engine.Evaluate("func() is nothing"));
}
[TestMethod, TestCategory("VBScriptEngine")]
public void VBScriptEngine_MarshalDecimalAsCurrency()
{
// ReSharper disable AccessToDisposedClosure
engine.Script.func = new Func<object>(() => 123.456M);
TestUtil.AssertException<ScriptEngineException>(() => engine.Evaluate("TypeName(func())"));
engine.Dispose();
engine = new VBScriptEngine(WindowsScriptEngineFlags.EnableDebugging | WindowsScriptEngineFlags.MarshalDecimalAsCurrency);
engine.Script.func = new Func<object>(() => 123.456M);
Assert.AreEqual("Currency", engine.Evaluate("TypeName(func())"));
Assert.AreEqual(123.456M + 5, engine.Evaluate("func() + 5"));
// ReSharper restore AccessToDisposedClosure
}
// ReSharper restore InconsistentNaming
#endregion