-
Notifications
You must be signed in to change notification settings - Fork 160
Expand file tree
/
Copy pathDocumentInfo.cs
More file actions
140 lines (122 loc) · 4.96 KB
/
DocumentInfo.cs
File metadata and controls
140 lines (122 loc) · 4.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.IO;
using System.Threading;
using Microsoft.ClearScript.JavaScript;
using Microsoft.ClearScript.Util;
namespace Microsoft.ClearScript
{
/// <summary>
/// Contains meta-information for a document.
/// </summary>
public struct DocumentInfo
{
private static long lastUniqueId;
private readonly string name;
private DocumentCategory category;
private ulong uniqueId;
/// <summary>
/// Initializes a new <c><see cref="DocumentInfo"/></c> structure with the specified document name.
/// </summary>
/// <param name="name">The document name.</param>
public DocumentInfo(string name)
: this()
{
this.name = name;
uniqueId = Interlocked.Increment(ref lastUniqueId).ToUnsigned();
}
/// <summary>
/// Initializes a new <c><see cref="DocumentInfo"/></c> structure with the specified document URI.
/// </summary>
/// <param name="uri">The document URI.</param>
public DocumentInfo(Uri uri)
: this()
{
MiscHelpers.VerifyNonNullArgument(uri, nameof(uri));
Uri = uri.IsAbsoluteUri ? uri : new Uri(Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar + uri);
name = Path.GetFileName(Uri.AbsolutePath);
uniqueId = Interlocked.Increment(ref lastUniqueId).ToUnsigned();
}
/// <summary>
/// Gets the document's name.
/// </summary>
/// <remarks>
/// This property always returns a non-blank string. If a null or blank document name was
/// specified at instantiation time, this property returns a default document name.
/// </remarks>
public string Name => name.ToNonBlank(Category.DefaultName);
/// <summary>
/// Gets the document's URI.
/// </summary>
/// <remarks>
/// This property returns <c>null</c> if a URI was not specified at instantiation time.
/// </remarks>
public Uri Uri { get; }
/// <summary>
/// Gets or sets an optional source map URI for the document.
/// </summary>
public Uri SourceMapUri { get; set; }
/// <summary>
/// Gets or sets the document's category.
/// </summary>
public DocumentCategory Category
{
get => category ?? DocumentCategory.Script;
set => category = value;
}
/// <summary>
/// Gets or sets optional document attributes.
/// </summary>
public DocumentFlags? Flags { get; set; }
/// <summary>
/// Gets or sets an optional context callback for the document.
/// </summary>
/// <remarks>
/// <para>
/// This property currently applies only to modules. If specified, the callback is invoked
/// the first time the module attempts to retrieve its context information. The properties
/// it returns are made available to the module implementation. This mechanism can be used
/// to expose host resources selectively, securely, and without polluting the script
/// engine's global namespace.
/// </para>
/// <para>
/// Use
/// <c><see href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta">import.meta</see></c>
/// to access the context information of a <c><see cref="ModuleCategory.Standard"/></c> JavaScript
/// module. In a <c><see cref="ModuleCategory.CommonJS"/></c> module, use <c>module.meta</c>.
/// </para>
/// </remarks>
public DocumentContextCallback ContextCallback { get; set; }
internal UniqueDocumentInfo MakeUnique(ScriptEngine engine)
{
return MakeUnique(engine.DocumentNameManager);
}
internal UniqueDocumentInfo MakeUnique(ScriptEngine engine, DocumentFlags? defaultFlags)
{
return MakeUnique(engine.DocumentNameManager, defaultFlags);
}
internal UniqueDocumentInfo MakeUnique(IUniqueNameManager manager)
{
return MakeUnique(manager, null);
}
internal UniqueDocumentInfo MakeUnique(IUniqueNameManager manager, DocumentFlags? defaultFlags)
{
var info = this;
if (!info.Flags.HasValue && defaultFlags.HasValue)
{
info.Flags = defaultFlags;
}
if (uniqueId < 1)
{
uniqueId = Interlocked.Increment(ref lastUniqueId).ToUnsigned();
}
var uniqueName = manager.GetUniqueName(Name, Category.DefaultName);
if (info.Flags.GetValueOrDefault().HasAllFlags(DocumentFlags.IsTransient))
{
uniqueName += " [temp]";
}
return new UniqueDocumentInfo(info, uniqueId, uniqueName);
}
}
}