Skip to content

Commit 0ff0910

Browse files
committed
Регистрация свойства модуля с привязкой к библиотеке
1 parent 3581fdd commit 0ff0910

13 files changed

Lines changed: 250 additions & 84 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
namespace OneScript.Compilation.Binding
9+
{
10+
/// <summary>
11+
/// Символ, который импортирован из внешнего пакета
12+
/// </summary>
13+
public interface IPackageSymbol
14+
{
15+
IExternalPackage GetPackageInfo();
16+
}
17+
}

src/OneScript.Core/Compilation/ICompileTimeDependencyResolver.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8+
#nullable enable
9+
810
using OneScript.Execution;
911
using OneScript.Sources;
1012

@@ -18,6 +20,6 @@ public interface ICompileTimeDependencyResolver
1820
/// <param name="module">Модуль в котором объявлен импорт</param>
1921
/// <param name="libraryName">имя библиотеки</param>
2022
/// <param name="process"></param>
21-
void Resolve(SourceCode module, string libraryName, IBslProcess process);
23+
IExternalPackage? Resolve(SourceCode module, string libraryName, IBslProcess process);
2224
}
2325
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
namespace OneScript.Compilation
9+
{
10+
/// <summary>
11+
/// Внешний пакет (библиотека), который может быть загружен в текущий контекст.
12+
/// </summary>
13+
public interface IExternalPackage
14+
{
15+
/// <summary>
16+
/// Короткое представление, имя пакета.
17+
/// </summary>
18+
string GetShortName();
19+
20+
/// <summary>
21+
/// Идентификатор пакета. Два экземпляра считаются одним, если у них совпадают идентификаторы
22+
/// </summary>
23+
string GetIdentifier();
24+
}
25+
}

src/OneScript.Core/Contexts/IRuntimeEnvironment.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ This Source Code Form is subject to the terms of the
55
at http://mozilla.org/MPL/2.0/.
66
----------------------------------------------------------*/
77

8+
using System;
89
using System.Collections.Generic;
10+
using OneScript.Compilation;
911
using OneScript.Compilation.Binding;
1012
using ScriptEngine.Machine;
1113

@@ -40,6 +42,14 @@ public interface IRuntimeEnvironment
4042
/// <param name="readOnly">Флаг доступности свойства только для чтения</param>
4143
void InjectGlobalProperty(IValue value, string identifier, bool readOnly);
4244

45+
/// <summary>
46+
/// Инжект глобального свойства
47+
/// </summary>
48+
/// <param name="value">Значение свойства</param>
49+
/// <param name="identifier">Идентификатор</param>
50+
/// <param name="ownerPackage">Пакет, предоставивший это свойство</param>
51+
void InjectGlobalProperty(IValue value, string identifier, IExternalPackage ownerPackage);
52+
4353
/// <summary>
4454
/// Установить новое значение глобального свойства
4555
/// </summary>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*----------------------------------------------------------
2+
This Source Code Form is subject to the terms of the
3+
Mozilla Public License, v.2.0. If a copy of the MPL
4+
was not distributed with this file, You can obtain one
5+
at http://mozilla.org/MPL/2.0/.
6+
----------------------------------------------------------*/
7+
8+
namespace ScriptEngine.HostedScript
9+
{
10+
public enum ExplicitImportsBehavior
11+
{
12+
Enabled,
13+
Warn,
14+
Disabled
15+
}
16+
}

src/ScriptEngine.HostedScript/FileSystemDependencyResolver.cs

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private class Library
3333
public string id;
3434
public ProcessingState state;
3535
public LibraryLoader customLoader;
36+
public IExternalPackage loadingResult;
3637
}
3738

3839
private enum ProcessingState
@@ -77,39 +78,38 @@ public void Initialize(ScriptingEngine engine)
7778
Engine = engine;
7879
}
7980

80-
public void Resolve(SourceCode module, string libraryName, IBslProcess process)
81+
public IExternalPackage Resolve(SourceCode module, string libraryName, IBslProcess process)
8182
{
8283
bool quoted = PrepareQuoted(ref libraryName);
83-
bool loaded;
84-
if (quoted)
85-
loaded = LoadByRelativePath(module, libraryName, process);
86-
else
87-
loaded = LoadByName(libraryName, process);
8884

89-
if(!loaded)
90-
throw new CompilerException(String.Format("Библиотека не найдена: '{0}'", libraryName));
85+
var lib = quoted ?
86+
LoadByRelativePath(module, libraryName, process) :
87+
LoadByName(libraryName, process);
88+
89+
if (lib == null)
90+
throw new CompilerException($"Библиотека не найдена: '{libraryName}'");
91+
92+
return lib;
9193
}
9294

93-
private bool LoadByName(string libraryName, IBslProcess process)
95+
private IExternalPackage LoadByName(string libraryName, IBslProcess process)
9496
{
9597
foreach (var path in SearchDirectories)
9698
{
9799
if(!Directory.Exists(path))
98100
continue;
99101

100102
var libraryPath = Path.Combine(path, libraryName);
101-
if (LoadByPath(libraryPath, process))
102-
return true;
103+
var loadAttempt = LoadByPath(libraryPath, process);
104+
if (loadAttempt != null)
105+
return loadAttempt;
103106
}
104107

105108
var rootPath = Path.Combine(LibraryRoot, libraryName);
106-
if (LoadByPath(rootPath, process))
107-
return true;
108-
109-
return false;
109+
return LoadByPath(rootPath, process);
110110
}
111111

112-
private bool LoadByRelativePath(SourceCode module, string libraryPath, IBslProcess process)
112+
private IExternalPackage LoadByRelativePath(SourceCode module, string libraryPath, IBslProcess process)
113113
{
114114
string realPath;
115115

@@ -197,17 +197,14 @@ private bool PrepareQuoted(ref string value)
197197
return quoted;
198198
}
199199

200-
private bool LoadByPath(string libraryPath, IBslProcess process)
200+
private IExternalPackage LoadByPath(string libraryPath, IBslProcess process)
201201
{
202-
if (Directory.Exists(libraryPath))
203-
{
204-
return LoadLibraryInternal(libraryPath, process);
205-
}
206-
207-
return false;
202+
return Directory.Exists(libraryPath) ?
203+
LoadLibraryInternal(libraryPath, process) :
204+
null;
208205
}
209206

210-
private bool LoadLibraryInternal(string libraryPath, IBslProcess process)
207+
private IExternalPackage LoadLibraryInternal(string libraryPath, IBslProcess process)
211208
{
212209
var id = GetLibraryId(libraryPath);
213210
var existedLib = _libs.FirstOrDefault(x => x.id == id);
@@ -222,41 +219,42 @@ private bool LoadLibraryInternal(string libraryPath, IBslProcess process)
222219
$"Error loading library {id}. Circular dependencies found.\n") + libStack);
223220
}
224221

225-
return true;
222+
return existedLib.loadingResult;
226223
}
227224

228225
var newLib = new Library() { id = id, state = ProcessingState.Discovered };
229-
bool hasFiles;
230226
int newLibIndex = _libs.Count;
231227

232228
var customLoaderFile = Path.Combine(libraryPath, PREDEFINED_LOADER_FILE);
233229
if (File.Exists(customLoaderFile))
234230
newLib.customLoader = LibraryLoader.Create(Engine, customLoaderFile, process);
235231

232+
IExternalPackage package;
236233
try
237234
{
238235
_libs.Add(newLib);
239-
hasFiles = ProcessLibrary(newLib, process);
236+
package = ProcessLibrary(newLib, process);
240237
newLib.state = ProcessingState.Processed;
238+
newLib.loadingResult = package;
241239
}
242240
catch (Exception)
243241
{
244242
_libs.RemoveAt(newLibIndex);
245243
throw;
246244
}
247245

248-
return hasFiles;
246+
return package;
249247
}
250248

251-
private bool ProcessLibrary(Library lib, IBslProcess process)
249+
private IExternalPackage ProcessLibrary(Library lib, IBslProcess process)
252250
{
253251
LibraryLoader loader;
254252
if (lib.customLoader != null)
255253
loader = lib.customLoader;
256254
else
257255
loader = GetDefaultLoader(process);
258256

259-
return loader.ProcessLibrary(lib.id, process) != default;
257+
return loader.ProcessLibrary(lib.id, process);
260258
}
261259

262260
private static string ListToStringStack(IEnumerable<Library> libs, string stopToken)

0 commit comments

Comments
 (0)