Skip to content

Commit 538ff04

Browse files
committed
выделена функция создания внешних данных; одинаковая обработка исключений
1 parent 342a790 commit 538ff04

1 file changed

Lines changed: 32 additions & 35 deletions

File tree

src/OneScript.StandardLibrary/DynamicLoadingFunctions.cs

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,29 +72,19 @@ public UserScriptContextInstance LoadScriptFromString(IBslProcess process,
7272
string code,
7373
StructureImpl externalContext = null)
7474
{
75-
var compiler = _engine.GetCompilerService();
76-
if (externalContext == null)
77-
{
78-
return _engine.AttachedScriptsFactory.LoadFromString(compiler, code, process);
79-
}
80-
else
81-
{
82-
var extData = new ExternalContextData();
83-
84-
foreach (var item in externalContext)
85-
{
86-
extData.Add(item.Key.ToString()!, item.Value);
87-
}
75+
var compiler = _engine.GetCompilerService();
76+
try
77+
{
78+
if (externalContext == null)
79+
return _engine.AttachedScriptsFactory.LoadFromString(compiler, code, process);
8880

89-
try
90-
{
91-
return _engine.AttachedScriptsFactory.LoadFromString(compiler, code, process, extData);
92-
}
93-
catch (Exception e) when (e is SyntaxErrorException || e is CompilerException)
94-
{
95-
throw ScriptCompilingException(e);
96-
}
97-
}
81+
var extData = CreateContextData(externalContext);
82+
return _engine.AttachedScriptsFactory.LoadFromString(compiler, code, process, extData);
83+
}
84+
catch (Exception e) when (e is SyntaxErrorException || e is CompilerException)
85+
{
86+
throw ScriptCompilingException(e);
87+
}
9888
}
9989

10090
/// <summary>
@@ -117,24 +107,31 @@ public UserScriptContextInstance LoadScript(IBslProcess process, string path, St
117107
{
118108
if(externalContext == null)
119109
return _engine.AttachedScriptsFactory.LoadFromPath(compiler, path, process);
120-
else
121-
{
122-
ExternalContextData extData = new ExternalContextData();
123-
124-
foreach (var item in externalContext)
125-
{
126-
extData.Add(item.Key.ToString()!, item.Value);
127-
}
128110

129-
return _engine.AttachedScriptsFactory.LoadFromPath(compiler, path, extData, process);
130-
}
111+
var extData = CreateContextData(externalContext);
112+
return _engine.AttachedScriptsFactory.LoadFromPath(compiler, path, extData, process);
131113
}
132114
catch (Exception e) when (e is SyntaxErrorException || e is CompilerException)
133115
{
134116
throw ScriptCompilingException(e);
135117
}
136-
}
137-
118+
}
119+
120+
private static ExternalContextData CreateContextData(StructureImpl externalContext)
121+
{
122+
if (externalContext == null)
123+
return null;
124+
125+
ExternalContextData extData = new ExternalContextData();
126+
127+
foreach (var item in externalContext)
128+
{
129+
extData.Add(item.Key.ToString()!, item.Value);
130+
}
131+
132+
return extData;
133+
}
134+
138135
/// <summary>
139136
/// Подключает внешнюю сборку среды .NET (*.dll) и регистрирует классы 1Script, объявленные в этой сборке.
140137
/// Публичные классы, отмеченные в dll атрибутом ContextClass, будут импортированы аналогично встроенным классам 1Script.
@@ -182,7 +179,7 @@ private static RuntimeException ScriptCompilingException(Exception e)
182179
{
183180
return new RuntimeException(BilingualString.Localize(
184181
"Ошибка компиляции подключаемого сценария:\n",
185-
"Error compiling attached script:\n") + e.Message);
182+
"Error compiling attached script:\n") + e.Message, e);
186183
}
187184
}
188185
}

0 commit comments

Comments
 (0)