11using System ;
22using System . IO ;
3+ using System . Reflection ;
34using System . Text ;
45
56using OriginalCompatibilityMode = Jurassic . CompatibilityMode ;
67using OriginalConcatenatedString = Jurassic . ConcatenatedString ;
8+ using OriginalErrorInstance = Jurassic . Library . ErrorInstance ;
9+ using OriginalFileScriptSource = Jurassic . FileScriptSource ;
710using OriginalJsEngine = Jurassic . ScriptEngine ;
811using OriginalJsException = Jurassic . JavaScriptException ;
912using OriginalNull = Jurassic . Null ;
13+ using OriginalStringScriptSource = Jurassic . StringScriptSource ;
1014using OriginalTypeConverter = Jurassic . TypeConverter ;
1115using OriginalUndefined = Jurassic . Undefined ;
1216
1317using JavaScriptEngineSwitcher . Core ;
18+ using JavaScriptEngineSwitcher . Core . Utilities ;
1419using CoreStrings = JavaScriptEngineSwitcher . Core . Resources . Strings ;
1520
1621namespace JavaScriptEngineSwitcher . Jurassic
@@ -147,7 +152,14 @@ private static object MapToHostType(object value)
147152 private JsRuntimeException ConvertJavascriptExceptionToJsRuntimeException (
148153 OriginalJsException jsException )
149154 {
150- var jsRuntimeException = new JsRuntimeException ( jsException . Message , EngineName , EngineVersion ,
155+ var jsError = jsException . ErrorObject as OriginalErrorInstance ;
156+ string message = jsException . Message ;
157+ if ( jsError != null )
158+ {
159+ message = ! string . IsNullOrEmpty ( jsError . Stack ) ? jsError . Stack : jsError . Message ;
160+ }
161+
162+ var jsRuntimeException = new JsRuntimeException ( message , EngineName , EngineVersion ,
151163 jsException )
152164 {
153165 Category = jsException . Name ,
@@ -162,14 +174,20 @@ private JsRuntimeException ConvertJavascriptExceptionToJsRuntimeException(
162174 #region JsEngineBase implementation
163175
164176 protected override object InnerEvaluate ( string expression )
177+ {
178+ return InnerEvaluate ( expression , null ) ;
179+ }
180+
181+ protected override object InnerEvaluate ( string expression , string documentName )
165182 {
166183 object result ;
167184
168185 lock ( _executionSynchronizer )
169186 {
170187 try
171188 {
172- result = _jsEngine . Evaluate ( expression ) ;
189+ var source = new OriginalStringScriptSource ( expression , documentName ) ;
190+ result = _jsEngine . Evaluate ( source ) ;
173191 }
174192 catch ( OriginalJsException e )
175193 {
@@ -188,18 +206,29 @@ protected override object InnerEvaluate(string expression)
188206
189207 protected override T InnerEvaluate < T > ( string expression )
190208 {
191- object result = InnerEvaluate ( expression ) ;
209+ return InnerEvaluate < T > ( expression , null ) ;
210+ }
211+
212+ protected override T InnerEvaluate < T > ( string expression , string documentName )
213+ {
214+ object result = InnerEvaluate ( expression , documentName ) ;
192215
193216 return OriginalTypeConverter . ConvertTo < T > ( _jsEngine , result ) ;
194217 }
195218
196219 protected override void InnerExecute ( string code )
220+ {
221+ InnerExecute ( code , null ) ;
222+ }
223+
224+ protected override void InnerExecute ( string code , string documentName )
197225 {
198226 lock ( _executionSynchronizer )
199227 {
200228 try
201229 {
202- _jsEngine . Execute ( code ) ;
230+ var source = new OriginalStringScriptSource ( code , documentName ) ;
231+ _jsEngine . Execute ( source ) ;
203232 }
204233 catch ( OriginalJsException e )
205234 {
@@ -382,7 +411,84 @@ public override void ExecuteFile(string path, Encoding encoding = null)
382411 {
383412 try
384413 {
385- _jsEngine . ExecuteFile ( path , encoding ) ;
414+ var source = new OriginalFileScriptSource ( path , encoding ) ;
415+ _jsEngine . Execute ( source ) ;
416+ }
417+ catch ( OriginalJsException e )
418+ {
419+ throw ConvertJavascriptExceptionToJsRuntimeException ( e ) ;
420+ }
421+ }
422+ }
423+
424+ public override void ExecuteResource ( string resourceName , Type type )
425+ {
426+ VerifyNotDisposed ( ) ;
427+
428+ if ( resourceName == null )
429+ {
430+ throw new ArgumentNullException (
431+ "resourceName" , string . Format ( CoreStrings . Common_ArgumentIsNull , "resourceName" ) ) ;
432+ }
433+
434+ if ( type == null )
435+ {
436+ throw new ArgumentNullException (
437+ "type" , string . Format ( CoreStrings . Common_ArgumentIsNull , "type" ) ) ;
438+ }
439+
440+ if ( string . IsNullOrWhiteSpace ( resourceName ) )
441+ {
442+ throw new ArgumentException (
443+ string . Format ( CoreStrings . Common_ArgumentIsEmpty , "resourceName" ) , "resourceName" ) ;
444+ }
445+
446+ Assembly assembly = type . GetTypeInfo ( ) . Assembly ;
447+ string nameSpace = type . Namespace ;
448+ string resourceFullName = nameSpace != null ? nameSpace + "." + resourceName : resourceName ;
449+
450+ lock ( _executionSynchronizer )
451+ {
452+ try
453+ {
454+ var source = new ResourceScriptSource ( resourceFullName , assembly ) ;
455+ _jsEngine . Execute ( source ) ;
456+ }
457+ catch ( OriginalJsException e )
458+ {
459+ throw ConvertJavascriptExceptionToJsRuntimeException ( e ) ;
460+ }
461+ }
462+ }
463+
464+ public override void ExecuteResource ( string resourceName , Assembly assembly )
465+ {
466+ VerifyNotDisposed ( ) ;
467+
468+ if ( resourceName == null )
469+ {
470+ throw new ArgumentNullException (
471+ "resourceName" , string . Format ( CoreStrings . Common_ArgumentIsNull , "resourceName" ) ) ;
472+ }
473+
474+ if ( assembly == null )
475+ {
476+ throw new ArgumentNullException (
477+ "assembly" , string . Format ( CoreStrings . Common_ArgumentIsNull , "assembly" ) ) ;
478+ }
479+
480+ if ( string . IsNullOrWhiteSpace ( resourceName ) )
481+ {
482+ throw new ArgumentException (
483+ string . Format ( CoreStrings . Common_ArgumentIsEmpty , "resourceName" ) , "resourceName" ) ;
484+ }
485+
486+ lock ( _executionSynchronizer )
487+ {
488+ try
489+ {
490+ var source = new ResourceScriptSource ( resourceName , assembly ) ;
491+ _jsEngine . Execute ( source ) ;
386492 }
387493 catch ( OriginalJsException e )
388494 {
0 commit comments