Skip to content

Commit 0355f1a

Browse files
committed
Reduced a number of delegate-wrappers
1 parent 2df6ec9 commit 0355f1a

File tree

5 files changed

+438
-283
lines changed

5 files changed

+438
-283
lines changed

NuGet/MsieJavaScriptEngine.nuspec

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
<requireLicenseAcceptance>false</requireLicenseAcceptance>
1313
<description>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine). Project was based on the code of SassAndCoffee.JavaScript (http://github.com/paulcbetts/SassAndCoffee), Chakra Sample Hosts (http://github.com/panopticoncentral/chakra-host) and jsrt-dotnet (http://github.com/robpaveza/jsrt-dotnet).</description>
1414
<summary>This library is a .NET wrapper for working with the JavaScript engines of Internet Explorer and Edge (JsRT versions of Chakra, ActiveScript version of Chakra and Classic JavaScript Engine).</summary>
15-
<releaseNotes>Now during the rethrowing of exceptions are preserved the full call stack trace.</releaseNotes>
15+
<releaseNotes>1. Now during the rethrowing of exceptions are preserved the full call stack trace;
16+
2. Reduced a number of delegate-wrappers.</releaseNotes>
1617
<copyright>Copyright (c) 2012-2017 Andrey Taritsyn - http://www.taritsyn.ru</copyright>
1718
<language>en-US</language>
1819
<tags>JavaScript ECMAScript MSIE IE Edge Chakra</tags>

NuGet/readme.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
=============
2222
RELEASE NOTES
2323
=============
24-
Now during the rethrowing of exceptions are preserved the full call stack trace.
24+
1. Now during the rethrowing of exceptions are preserved the full call stack
25+
trace;
26+
2. Reduced a number of delegate-wrappers.
2527

2628
============
2729
PROJECT SITE

src/MsieJavaScriptEngine/ActiveScript/ActiveScriptJsEngineBase.cs

Lines changed: 103 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -330,56 +330,6 @@ private void ThrowError()
330330
}
331331
}
332332

333-
private void InvokeScript(Action action)
334-
{
335-
_dispatcher.Invoke(() =>
336-
{
337-
try
338-
{
339-
action();
340-
}
341-
catch (ActiveScriptException e)
342-
{
343-
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
344-
}
345-
catch (TargetInvocationException e)
346-
{
347-
var activeScriptException = e.InnerException as ActiveScriptException;
348-
if (activeScriptException != null)
349-
{
350-
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
351-
}
352-
353-
throw;
354-
}
355-
});
356-
}
357-
358-
private T InvokeScript<T>(Func<T> func)
359-
{
360-
return _dispatcher.Invoke(() =>
361-
{
362-
try
363-
{
364-
return func();
365-
}
366-
catch (ActiveScriptException e)
367-
{
368-
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
369-
}
370-
catch (TargetInvocationException e)
371-
{
372-
var activeScriptException = e.InnerException as ActiveScriptException;
373-
if (activeScriptException != null)
374-
{
375-
throw ConvertActiveScriptExceptionToJsRuntimeException(activeScriptException);
376-
}
377-
378-
throw;
379-
}
380-
});
381-
}
382-
383333
/// <summary>
384334
/// Executes a script text
385335
/// </summary>
@@ -516,35 +466,33 @@ private void InnerSetVariableValue(string variableName, object value)
516466
}
517467
}
518468

519-
private void EmbedHostItem(string itemName, object value)
469+
private void InnerEmbedHostItem(string itemName, object value)
520470
{
521-
InvokeScript(() =>
471+
object oldValue = null;
472+
if (_hostItems.ContainsKey(itemName))
522473
{
523-
object oldValue = null;
524-
if (_hostItems.ContainsKey(itemName))
525-
{
526-
oldValue = _hostItems[itemName];
527-
}
528-
_hostItems[itemName] = value;
474+
oldValue = _hostItems[itemName];
475+
}
476+
_hostItems[itemName] = value;
529477

530-
try
478+
try
479+
{
480+
_activeScriptWrapper.AddNamedItem(itemName, ScriptItemFlags.IsVisible | ScriptItemFlags.GlobalMembers);
481+
}
482+
catch
483+
{
484+
if (oldValue != null)
531485
{
532-
_activeScriptWrapper.AddNamedItem(itemName, ScriptItemFlags.IsVisible | ScriptItemFlags.GlobalMembers);
486+
_hostItems[itemName] = oldValue;
533487
}
534-
catch (Exception)
488+
else
535489
{
536-
if (oldValue != null)
537-
{
538-
_hostItems[itemName] = oldValue;
539-
}
540-
else
541-
{
542-
_hostItems.Remove(itemName);
543-
}
544-
545-
throw;
490+
_hostItems.Remove(itemName);
546491
}
547-
});
492+
493+
ThrowError();
494+
throw;
495+
}
548496
}
549497

550498
/// <summary>
@@ -614,30 +562,52 @@ public override string Mode
614562

615563
public override object Evaluate(string expression, string documentName)
616564
{
617-
object result = InvokeScript(() => InnerExecute(expression, documentName, true));
565+
object result = _dispatcher.Invoke(() =>
566+
{
567+
try
568+
{
569+
return InnerExecute(expression, documentName, true);
570+
}
571+
catch (ActiveScriptException e)
572+
{
573+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
574+
}
575+
});
576+
618577
result = MapToHostType(result);
619578

620579
return result;
621580
}
622581

623582
public override void Execute(string code, string documentName)
624583
{
625-
InvokeScript(() =>
584+
_dispatcher.Invoke(() =>
626585
{
627-
InnerExecute(code, documentName, false);
586+
try
587+
{
588+
InnerExecute(code, documentName, false);
589+
}
590+
catch (ActiveScriptException e)
591+
{
592+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
593+
}
628594
});
629595
}
630596

631597
public override object CallFunction(string functionName, params object[] args)
632598
{
633599
object[] processedArgs = MapToScriptType(args);
634600

635-
object result = InvokeScript(() =>
601+
object result = _dispatcher.Invoke(() =>
636602
{
637603
try
638604
{
639605
return InnerCallFunction(functionName, processedArgs);
640606
}
607+
catch (ActiveScriptException e)
608+
{
609+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
610+
}
641611
catch (MissingMemberException)
642612
{
643613
throw new JsRuntimeException(
@@ -652,7 +622,7 @@ public override object CallFunction(string functionName, params object[] args)
652622

653623
public override bool HasVariable(string variableName)
654624
{
655-
bool result = InvokeScript(() =>
625+
bool result = _dispatcher.Invoke(() =>
656626
{
657627
bool variableExist;
658628

@@ -661,6 +631,10 @@ public override bool HasVariable(string variableName)
661631
object variableValue = InnerGetVariableValue(variableName);
662632
variableExist = variableValue != null;
663633
}
634+
catch (ActiveScriptException e)
635+
{
636+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
637+
}
664638
catch (MissingMemberException)
665639
{
666640
variableExist = false;
@@ -674,12 +648,16 @@ public override bool HasVariable(string variableName)
674648

675649
public override object GetVariableValue(string variableName)
676650
{
677-
object result = InvokeScript(() =>
651+
object result = _dispatcher.Invoke(() =>
678652
{
679653
try
680654
{
681655
return InnerGetVariableValue(variableName);
682656
}
657+
catch (ActiveScriptException e)
658+
{
659+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
660+
}
683661
catch (MissingMemberException)
684662
{
685663
throw new JsRuntimeException(
@@ -695,32 +673,72 @@ public override object GetVariableValue(string variableName)
695673
public override void SetVariableValue(string variableName, object value)
696674
{
697675
object processedValue = MapToScriptType(value);
698-
InvokeScript(() => InnerSetVariableValue(variableName, processedValue));
676+
677+
_dispatcher.Invoke(() =>
678+
{
679+
try
680+
{
681+
InnerSetVariableValue(variableName, processedValue);
682+
}
683+
catch (ActiveScriptException e)
684+
{
685+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
686+
}
687+
});
699688
}
700689

701690
public override void RemoveVariable(string variableName)
702691
{
703-
InvokeScript(() =>
692+
_dispatcher.Invoke(() =>
704693
{
705-
InnerSetVariableValue(variableName, null);
694+
try
695+
{
696+
InnerSetVariableValue(variableName, null);
706697

707-
if (_hostItems.ContainsKey(variableName))
698+
if (_hostItems.ContainsKey(variableName))
699+
{
700+
_hostItems.Remove(variableName);
701+
}
702+
}
703+
catch (ActiveScriptException e)
708704
{
709-
_hostItems.Remove(variableName);
705+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
710706
}
711707
});
712708
}
713709

714710
public override void EmbedHostObject(string itemName, object value)
715711
{
716712
object processedValue = MapToScriptType(value);
717-
EmbedHostItem(itemName, processedValue);
713+
714+
_dispatcher.Invoke(() =>
715+
{
716+
try
717+
{
718+
InnerEmbedHostItem(itemName, processedValue);
719+
}
720+
catch (ActiveScriptException e)
721+
{
722+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
723+
}
724+
});
718725
}
719726

720727
public override void EmbedHostType(string itemName, Type type)
721728
{
722729
var typeValue = new HostType(type, _engineMode);
723-
EmbedHostItem(itemName, typeValue);
730+
731+
_dispatcher.Invoke(() =>
732+
{
733+
try
734+
{
735+
InnerEmbedHostItem(itemName, typeValue);
736+
}
737+
catch (ActiveScriptException e)
738+
{
739+
throw ConvertActiveScriptExceptionToJsRuntimeException(e);
740+
}
741+
});
724742
}
725743

726744
public override void CollectGarbage()

0 commit comments

Comments
 (0)