Skip to content

Commit 9ef7b18

Browse files
committed
fixes EvilBeaver#1540 Предупреждение о неявных импортах
1 parent 458a529 commit 9ef7b18

35 files changed

+774
-217
lines changed
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+
PackageInfo GetPackageInfo();
16+
}
17+
}

src/OneScript.Core/Compilation/ICompileTimeDependencyResolver.cs

Lines changed: 5 additions & 2 deletions
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,7 @@ 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+
/// <returns>Информация о загруженном пакете или null</returns>
24+
PackageInfo? Resolve(SourceCode module, string libraryName, IBslProcess process);
2225
}
23-
}
26+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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 sealed class PackageInfo
14+
{
15+
public PackageInfo(string id, string shortName)
16+
{
17+
Id = id;
18+
ShortName = shortName;
19+
}
20+
21+
/// <summary>
22+
/// Уникальный идентификатор пакета (обычно путь к каталогу)
23+
/// </summary>
24+
public string Id { get; }
25+
26+
/// <summary>
27+
/// Короткое имя для отображения в сообщениях
28+
/// </summary>
29+
public string ShortName { get; }
30+
}
31+
}

src/OneScript.Core/Contexts/IRuntimeEnvironment.cs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using System.Collections.Generic;
9+
using OneScript.Compilation;
910
using OneScript.Compilation.Binding;
1011
using ScriptEngine.Machine;
1112

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

44+
/// <summary>
45+
/// Инжект глобального свойства
46+
/// </summary>
47+
/// <param name="value">Значение свойства</param>
48+
/// <param name="identifier">Идентификатор</param>
49+
/// <param name="ownerPackage">Пакет, предоставивший это свойство</param>
50+
void InjectGlobalProperty(IValue value, string identifier, PackageInfo ownerPackage);
51+
4352
/// <summary>
4453
/// Инжект глобального свойства с помощью описания свойства.
4554
/// </summary>
@@ -71,4 +80,4 @@ public interface IRuntimeEnvironment
7180
/// </summary>
7281
IReadOnlyList<IAttachableContext> AttachedContexts { get; }
7382
}
74-
}
83+
}

src/OneScript.Core/Sources/FileCodeSource.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ This Source Code Form is subject to the terms of the
99
using System.Text;
1010
using OneScript.Commons;
1111
using System;
12+
using OneScript.Language.Sources;
1213

1314
namespace OneScript.Sources
1415
{

src/OneScript.Core/Sources/StringCodeSource.cs

Lines changed: 2 additions & 0 deletions
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+
using OneScript.Language.Sources;
9+
810
namespace OneScript.Sources
911
{
1012
public class StringCodeSource : ICodeSource

src/OneScript.Language/Sources/ICodeSource.cs

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

8-
namespace OneScript.Sources
8+
namespace OneScript.Language.Sources
99
{
1010
public interface ICodeSource
1111
{

src/OneScript.Language/Sources/SourceCode.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using OneScript.Language.LexicalAnalysis;
9+
using OneScript.Language.Sources;
910

1011
// ReSharper disable once CheckNamespace
1112
namespace OneScript.Sources
@@ -17,10 +18,11 @@ public class SourceCode : ISourceCodeIndexer
1718

1819
private string _code = null;
1920

20-
internal SourceCode(string sourceName, ICodeSource source)
21+
internal SourceCode(string sourceName, ICodeSource source, string ownerPackageId = null)
2122
{
2223
_source = source;
2324
Name = sourceName;
25+
OwnerPackageId = ownerPackageId;
2426
}
2527

2628
public SourceCodeIterator CreateIterator()
@@ -34,6 +36,11 @@ public SourceCodeIterator CreateIterator()
3436
public string Location => _source.Location;
3537

3638
public string Name { get; }
39+
40+
/// <summary>
41+
/// Идентификатор пакета-владельца. null если модуль не принадлежит библиотеке.
42+
/// </summary>
43+
public string OwnerPackageId { get; }
3744

3845
public string GetSourceCode()
3946
{
@@ -49,4 +56,4 @@ public string GetCodeLine(int lineNumber)
4956

5057
public override string ToString() => Name ?? Location;
5158
}
52-
}
59+
}

src/OneScript.Language/Sources/SourceCodeBuilder.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ This Source Code Form is subject to the terms of the
66
----------------------------------------------------------*/
77

88
using System;
9+
using OneScript.Language.Sources;
910

1011
// ReSharper disable once CheckNamespace
1112
namespace OneScript.Sources
@@ -14,6 +15,7 @@ public class SourceCodeBuilder
1415
{
1516
private ICodeSource _source;
1617
private string _moduleName;
18+
private string _ownerPackageId;
1719

1820
private SourceCodeBuilder()
1921
{
@@ -30,6 +32,15 @@ public SourceCodeBuilder WithName(string name)
3032
_moduleName = name;
3133
return this;
3234
}
35+
36+
/// <summary>
37+
/// Устанавливает идентификатор пакета-владельца для исходного кода.
38+
/// </summary>
39+
public SourceCodeBuilder WithOwnerPackage(string packageId)
40+
{
41+
_ownerPackageId = packageId;
42+
return this;
43+
}
3344

3445
public SourceCode Build()
3546
{
@@ -39,9 +50,9 @@ public SourceCode Build()
3950
if (_moduleName == default)
4051
_moduleName = _source.Location;
4152

42-
return new SourceCode(_moduleName, _source);
53+
return new SourceCode(_moduleName, _source, _ownerPackageId);
4354
}
4455

4556
public static SourceCodeBuilder Create() => new SourceCodeBuilder();
4657
}
47-
}
58+
}

src/ScriptEngine.HostedScript/FileSystemDependencyResolver.cs

Lines changed: 29 additions & 31 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 PackageInfo 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 PackageInfo 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 PackageInfo 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 PackageInfo 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 PackageInfo 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 PackageInfo 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+
PackageInfo 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 PackageInfo 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)
@@ -285,4 +283,4 @@ private static bool PathHasInvalidChars(string path)
285283
return (!string.IsNullOrEmpty(path) && path.IndexOfAny(Path.GetInvalidPathChars()) >= 0);
286284
}
287285
}
288-
}
286+
}

0 commit comments

Comments
 (0)