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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! Super-global environment — the bottom of the scope chain.
//!
//! This environment sits below the global scope and lazily resolves
//! built-in and plugin-provided objects on first access. Objects are
//! cached after first resolution so each name is materialized at most once.
//!
//! ## How It Works
//!
//! When JavaScript code references a name that isn't found in the lexical
//! environment chain or global scope, the super-global environment is consulted:
//!
//! ```text
//! JavaScript: Math.abs(-5)
//! ↓
//! 1. Check local scope → not found
//! 2. Check outer scopes → not found
//! 3. Check global scope → not found
//! 4. Check super-global → "Math" found!
//! ↓
//! 5. Query resolvers: Does anyone provide "Math"?
//! 6. CorePluginResolver says "yes"
//! 7. Cache the result
//! 8. Dispatch Math.abs() via resolver
//! ```
//!
//! ## Caching Strategy
//!
//! - **First lookup**: Query all resolvers in order, cache which one owns the name
//! - **Subsequent lookups**: Use cached resolver index directly
//! - **Method calls**: Can bypass object materialization entirely
//!
//! ## Example
//!
//! ```
//! use just::runner::plugin::super_global::SuperGlobalEnvironment;
//! use just::runner::plugin::resolver::PluginResolver;
//! use just::runner::plugin::types::EvalContext;
//! use just::runner::ds::value::{JsValue, JsNumberType};
//! use just::runner::ds::error::JErrorType;
//!
//! struct MyPlugin;
//!
//! impl PluginResolver for MyPlugin {
//! fn has_binding(&self, name: &str) -> bool {
//! name == "MyObject"
//! }
//!
//! fn resolve(&self, _name: &str, _ctx: &mut EvalContext) -> Result<JsValue, JErrorType> {
//! Ok(JsValue::Number(JsNumberType::Integer(42)))
//! }
//!
//! fn call_method(&self, _obj: &str, _method: &str, _ctx: &mut EvalContext,
//! _this: JsValue, _args: Vec<JsValue>) -> Option<Result<JsValue, JErrorType>> {
//! None
//! }
//!
//! fn name(&self) -> &str { "my_plugin" }
//! }
//!
//! let mut sg = SuperGlobalEnvironment::new();
//! sg.add_resolver(Box::new(MyPlugin));
//!
//! // Now "MyObject" is available in the super-global scope
//! ```
use HashMap;
use crateJErrorType;
use crateJsValue;
use cratePluginResolver;
use crateEvalContext;
/// The super-global environment for lazy resolution of built-in objects.
///
/// This is the outermost scope in the resolution chain, sitting below even
/// the global scope. It provides built-in objects (Math, console, etc.) and
/// plugin-provided objects through a lazy resolution mechanism.
///
/// ## Key Features
///
/// - **Lazy Resolution**: Objects are only materialized when first accessed
/// - **Caching**: Once resolved, values are cached for fast subsequent access
/// - **Read-Only**: JavaScript code cannot create or modify super-global bindings
/// - **Plugin System**: Multiple resolvers can be registered, queried in order
/// - **Method Dispatch**: Efficient method calls without object materialization
///
/// ## Resolution Order
///
/// When a name is looked up:
/// 1. Check if it's in the cache → return cached value
/// 2. Query each resolver's `has_binding()` in registration order
/// 3. First resolver that claims the name wins
/// 4. Call resolver's `resolve()` to materialize the value
/// 5. Cache the value and resolver index
/// 6. Return the value
///
/// ## Method Call Optimization
///
/// For method calls like `Math.abs(-5)`, the super-global can dispatch directly
/// to the resolver's `call_method()` without materializing the Math object,
/// providing better performance for built-in method calls.
///
/// ## Thread Safety
///
/// This struct is typically wrapped in `Rc<RefCell<>>` (see [`SharedSuperGlobal`](super::types::SharedSuperGlobal))
/// to allow shared mutable access across the evaluation context.