forked from chakra-core/ChakraCore
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSONStack.cpp
More file actions
135 lines (120 loc) · 3.25 KB
/
Copy pathJSONStack.cpp
File metadata and controls
135 lines (120 loc) · 3.25 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
//-------------------------------------------------------------------------------------------------------
// Copyright (C) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.txt file in the project root for full license information.
//-------------------------------------------------------------------------------------------------------
#include "RuntimeLibraryPch.h"
#include "JSONStack.h"
namespace JSON
{
#if defined(_JS_VALUE) || (_DOM_VALUE)
#error "Something went wrong!"
#endif
#define _JS_VALUE tempValues[0]
#define _DOM_VALUE tempValues[1]
bool StrictEqualsObjectComparer::Equals(Js::Var x, Js::Var y)
{
return JSONStack::Equals(x,y);
}
JSONStack::JSONStack(ArenaAllocator *allocator, Js::ScriptContext *context)
: jsObjectStack(allocator), domObjectStack(nullptr),
alloc(allocator), scriptContext(context)
{
// most of the time, the size of the stack is 1 object.
// use direct member instead of expensive Push / Pop / Has
_JS_VALUE = nullptr;
_DOM_VALUE = nullptr;
}
bool JSONStack::Equals(Js::Var x, Js::Var y)
{
return Js::JavascriptOperators::StrictEqual(x, y, ((Js::RecyclableObject *)x)->GetScriptContext()) == TRUE;
}
bool JSONStack::Has(Js::Var data, bool bJsObject) const
{
if (bJsObject)
{
if (_JS_VALUE)
{
return (data == _JS_VALUE);
}
return jsObjectStack.Has(data);
}
else if (domObjectStack)
{
if (_DOM_VALUE)
{
return (data == _DOM_VALUE);
}
return domObjectStack->Contains(data);
}
return false;
}
bool JSONStack::Push(Js::Var data, bool bJsObject)
{
if (bJsObject)
{
bool result = true;
if (_JS_VALUE)
{
jsObjectStack.Push(_JS_VALUE);
_JS_VALUE = nullptr;
}
if (jsObjectStack.Count())
{
result = jsObjectStack.Push(data);
}
else
{
_JS_VALUE = data;
}
return result;
}
EnsuresDomObjectStack();
if (_DOM_VALUE)
{
domObjectStack->Add(_DOM_VALUE);
_DOM_VALUE = nullptr;
}
if (domObjectStack->Count())
{
domObjectStack->Add(data);
}
else
{
_DOM_VALUE = data;
}
return true;
}
void JSONStack::Pop(bool bJsObject)
{
if (bJsObject)
{
if (_JS_VALUE)
{
_JS_VALUE = nullptr;
}
else
{
jsObjectStack.Pop();
}
return;
}
AssertMsg(domObjectStack != NULL, "Misaligned pop");
if (_DOM_VALUE)
{
_DOM_VALUE = nullptr;
}
else
{
domObjectStack->RemoveAtEnd();
}
}
void JSONStack::EnsuresDomObjectStack(void)
{
if (!domObjectStack)
{
domObjectStack = DOMObjectStack::New(alloc);
}
}
} // namespace JSON
#undef _JS_VALUE
#undef _DOM_VALUE