forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJsrtSourceHolder.h
More file actions
131 lines (112 loc) · 5.17 KB
/
JsrtSourceHolder.h
File metadata and controls
131 lines (112 loc) · 5.17 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#pragma once
class ISourceHolder;
namespace Js
{
template <typename TLoadCallback, typename TUnloadCallback>
class JsrtSourceHolder sealed : public ISourceHolder
{
private:
enum MapRequestFor { Source = 1, Length = 2 };
FieldNoBarrier(TLoadCallback) scriptLoadCallback;
FieldNoBarrier(TUnloadCallback) scriptUnloadCallback;
Field(JsSourceContext) sourceContext;
#ifndef NTBUILD
Field(JsValueRef) mappedScriptValue;
Field(DetachedStateBase*) mappedSerializedScriptValue;
#endif
Field(utf8char_t const *) mappedSource;
Field(size_t) mappedSourceByteLength;
Field(size_t) mappedAllocLength;
// Wrapper methods with Asserts to ensure that we aren't trying to access unmapped source
utf8char_t const * GetMappedSource()
{
AssertMsg(mappedSource != nullptr, "Our mapped source is nullptr, isSourceMapped (Assert above) should be false.");
AssertMsg(scriptUnloadCallback != nullptr, "scriptUnloadCallback is null, this means that this object has been finalized.");
return mappedSource;
};
size_t GetMappedSourceLength()
{
AssertMsg(mappedSource != nullptr, "Our mapped source is nullptr, isSourceMapped (Assert above) should be false.");
AssertMsg(scriptUnloadCallback != nullptr, "scriptUnloadCallback is null, this means that this object has been finalized.");
return mappedSourceByteLength;
};
void EnsureSource(MapRequestFor requestedFor, const WCHAR* reasonString);
public:
JsrtSourceHolder(_In_ TLoadCallback scriptLoadCallback,
_In_ TUnloadCallback scriptUnloadCallback,
_In_ JsSourceContext sourceContext,
Js::ArrayBuffer* serializedScriptValue = nullptr) :
scriptLoadCallback(scriptLoadCallback),
scriptUnloadCallback(scriptUnloadCallback),
sourceContext(sourceContext),
#ifndef NTBUILD
mappedScriptValue(nullptr),
mappedSerializedScriptValue(serializedScriptValue == nullptr ? nullptr : serializedScriptValue->DetachAndGetState(false /*queueForDelayFree*/)),
#endif
mappedSourceByteLength(0),
mappedSource(nullptr)
{
AssertMsg(scriptLoadCallback != nullptr, "script load callback given is null.");
AssertMsg(scriptUnloadCallback != nullptr, "script unload callback given is null.");
};
virtual bool IsEmpty() override
{
return false;
}
// Following two methods do not attempt any source mapping
LPCUTF8 GetSourceUnchecked()
{
return this->GetMappedSource();
}
// Following two methods are calls to EnsureSource before attempting to get the source
virtual LPCUTF8 GetSource(const WCHAR* reasonString) override
{
this->EnsureSource(MapRequestFor::Source, reasonString);
return this->GetMappedSource();
}
virtual size_t GetByteLength(const WCHAR* reasonString) override
{
this->EnsureSource(MapRequestFor::Length, reasonString);
return this->GetMappedSourceLength();
}
virtual void Finalize(bool isShutdown) override;
virtual void Dispose(bool isShutdown) override
{
#ifndef NTBUILD
if (this->mappedSerializedScriptValue != nullptr)
{
// We have to extend the buffer data's lifetime until Dispose because
// it might be used during finalization of other objects, such as
// FunctionEntryPointInfo which wants to log its name.
this->mappedSerializedScriptValue->CleanUp();
}
#endif
}
virtual void Mark(Recycler * recycler) override
{
}
virtual void Unload() override;
virtual bool Equals(ISourceHolder* other) override
{
return this == other ||
(this->GetByteLength(_u("Equal Comparison")) == other->GetByteLength(_u("Equal Comparison"))
&& (this->GetSource(_u("Equal Comparison")) == other->GetSource(_u("Equal Comparison"))
|| memcmp(this->GetSource(_u("Equal Comparison")), other->GetSource(_u("Equal Comparison")), this->GetByteLength(_u("Equal Comparison"))) == 0));
}
virtual hash_t GetHashCode() override
{
LPCUTF8 source = GetSource(_u("Hash Code Calculation"));
size_t byteLength = GetByteLength(_u("Hash Code Calculation"));
Assert(byteLength < MAXUINT32);
return JsUtil::CharacterBuffer<utf8char_t>::StaticGetHashCode(source, (charcount_t)byteLength);
}
virtual bool IsDeferrable() override
{
return !PHASE_OFF1(Js::DeferSourceLoadPhase);
}
};
}