summaryrefslogtreecommitdiffstats
path: root/third_party/rust/jsparagus-stencil/src/script.rs
blob: ab97b88177444f30b539529a2e0bcf64dc5153fd (plain)
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! The result of emitter

use crate::frame_slot::FrameSlot;
use crate::function::FunctionFlags;
use crate::gcthings::GCThing;
use crate::scope::ScopeIndex;
use crate::scope_notes::ScopeNote;
use ast::source_atom_set::SourceAtomSetIndex;

// WARNING
// The following section is generated by update_stencil.py.
// Do mot modify manually.
//
// @@@@ BEGIN TYPES @@@@
#[derive(Debug, Clone, Copy)]
pub enum ImmutableScriptFlagsEnum {
    #[allow(dead_code)]
    IsForEval = 1 << 0,
    #[allow(dead_code)]
    IsModule = 1 << 1,
    #[allow(dead_code)]
    IsFunction = 1 << 2,
    #[allow(dead_code)]
    SelfHosted = 1 << 3,
    #[allow(dead_code)]
    ForceStrict = 1 << 4,
    #[allow(dead_code)]
    HasNonSyntacticScope = 1 << 5,
    #[allow(dead_code)]
    NoScriptRval = 1 << 6,
    #[allow(dead_code)]
    TreatAsRunOnce = 1 << 7,
    #[allow(dead_code)]
    Strict = 1 << 8,
    #[allow(dead_code)]
    HasModuleGoal = 1 << 9,
    #[allow(dead_code)]
    HasInnerFunctions = 1 << 10,
    #[allow(dead_code)]
    HasDirectEval = 1 << 11,
    #[allow(dead_code)]
    BindingsAccessedDynamically = 1 << 12,
    #[allow(dead_code)]
    HasCallSiteObj = 1 << 13,
    #[allow(dead_code)]
    IsAsync = 1 << 14,
    #[allow(dead_code)]
    IsGenerator = 1 << 15,
    #[allow(dead_code)]
    FunHasExtensibleScope = 1 << 16,
    #[allow(dead_code)]
    FunctionHasThisBinding = 1 << 17,
    #[allow(dead_code)]
    NeedsHomeObject = 1 << 18,
    #[allow(dead_code)]
    IsDerivedClassConstructor = 1 << 19,
    #[allow(dead_code)]
    IsSyntheticFunction = 1 << 20,
    #[allow(dead_code)]
    UseMemberInitializers = 1 << 21,
    #[allow(dead_code)]
    HasRest = 1 << 22,
    #[allow(dead_code)]
    NeedsFunctionEnvironmentObjects = 1 << 23,
    #[allow(dead_code)]
    FunctionHasExtraBodyVarScope = 1 << 24,
    #[allow(dead_code)]
    ShouldDeclareArguments = 1 << 25,
    #[allow(dead_code)]
    NeedsArgsObj = 1 << 26,
    #[allow(dead_code)]
    HasMappedArgsObj = 1 << 27,
    #[allow(dead_code)]
    IsInlinableLargeFunction = 1 << 28,
    #[allow(dead_code)]
    FunctionHasNewTargetBinding = 1 << 29,
}

#[derive(Debug, Clone, Copy)]
pub enum MutableScriptFlagsEnum {
    #[allow(dead_code)]
    HasRunOnce = 1 << 8,
    #[allow(dead_code)]
    HasBeenCloned = 1 << 9,
    #[allow(dead_code)]
    HasScriptCounts = 1 << 10,
    #[allow(dead_code)]
    HasDebugScript = 1 << 11,
    #[allow(dead_code)]
    AllowRelazify = 1 << 14,
    #[allow(dead_code)]
    SpewEnabled = 1 << 15,
    #[allow(dead_code)]
    NeedsFinalWarmUpCount = 1 << 16,
    #[allow(dead_code)]
    BaselineDisabled = 1 << 17,
    #[allow(dead_code)]
    IonDisabled = 1 << 18,
    #[allow(dead_code)]
    Uninlineable = 1 << 19,
    #[allow(dead_code)]
    FailedBoundsCheck = 1 << 21,
    #[allow(dead_code)]
    HadLICMInvalidation = 1 << 22,
    #[allow(dead_code)]
    HadReorderingBailout = 1 << 23,
    #[allow(dead_code)]
    HadEagerTruncationBailout = 1 << 24,
    #[allow(dead_code)]
    FailedLexicalCheck = 1 << 25,
    #[allow(dead_code)]
    HadSpeculativePhiBailout = 1 << 26,
    #[allow(dead_code)]
    HadUnboxFoldingBailout = 1 << 27,
}

// @@@@ END TYPES @@@@

#[derive(Debug)]
pub struct ImmutableScriptFlags {
    value: u32,
}

impl ImmutableScriptFlags {
    pub fn new() -> Self {
        Self { value: 0 }
    }

    pub fn from_raw(bits: u32) -> Self {
        Self { value: bits }
    }

    pub fn set(&mut self, bit: ImmutableScriptFlagsEnum) {
        self.value |= bit as u32;
    }

    pub fn has(&self, bit: ImmutableScriptFlagsEnum) -> bool {
        (self.value & bit as u32) != 0
    }

    pub fn reset(&mut self, bit: ImmutableScriptFlagsEnum) {
        self.value &= !(bit as u32)
    }
}

impl From<ImmutableScriptFlags> for u32 {
    fn from(flags: ImmutableScriptFlags) -> u32 {
        flags.value
    }
}

/// Maps to js::ImmutableScriptData in m-c/js/src/vm/SharedStencil.h.
#[derive(Debug)]
pub struct ImmutableScriptData {
    pub main_offset: u32,
    pub nfixed: FrameSlot,
    pub nslots: u32,
    pub body_scope_index: u32,
    pub num_ic_entries: u32,
    pub fun_length: u16,
    pub bytecode: Vec<u8>,
    pub scope_notes: Vec<ScopeNote>,
    // TODO: Add resume_offsets and try_notes.
}

/// Index into ImmutableScriptDataList.scripts.
#[derive(Debug, Clone, Copy)]
pub struct ImmutableScriptDataIndex {
    index: usize,
}

impl ImmutableScriptDataIndex {
    fn new(index: usize) -> Self {
        Self { index }
    }
}

impl From<ImmutableScriptDataIndex> for usize {
    fn from(index: ImmutableScriptDataIndex) -> usize {
        index.index
    }
}

/// List of ImmutableScriptData.
#[derive(Debug)]
pub struct ImmutableScriptDataList {
    /// Uses Option to allow `allocate()` and `populate()` to be called
    /// separately.
    items: Vec<Option<ImmutableScriptData>>,
}

impl ImmutableScriptDataList {
    pub fn new() -> Self {
        Self { items: Vec::new() }
    }

    pub fn push(&mut self, script: ImmutableScriptData) -> ImmutableScriptDataIndex {
        let index = self.items.len();
        self.items.push(Some(script));
        ImmutableScriptDataIndex::new(index)
    }

    pub fn allocate(&mut self) -> ImmutableScriptDataIndex {
        let index = self.items.len();
        self.items.push(None);
        ImmutableScriptDataIndex::new(index)
    }

    pub fn populate(&mut self, index: ImmutableScriptDataIndex, script: ImmutableScriptData) {
        self.items[usize::from(index)].replace(script);
    }
}

impl From<ImmutableScriptDataList> for Vec<ImmutableScriptData> {
    fn from(list: ImmutableScriptDataList) -> Vec<ImmutableScriptData> {
        list.items
            .into_iter()
            .map(|g| g.expect("Should be populated"))
            .collect()
    }
}

#[derive(Debug, Clone)]
pub struct SourceExtent {
    pub source_start: u32,
    pub source_end: u32,
    pub to_string_start: u32,
    pub to_string_end: u32,

    pub lineno: u32,
    pub column: u32,
}

impl SourceExtent {
    fn new() -> Self {
        Self {
            source_start: 0,
            source_end: 0,
            to_string_start: 0,
            to_string_end: 0,

            lineno: 0,
            column: 0,
        }
    }

    pub fn top_level_script(length: u32, lineno: u32, column: u32) -> Self {
        Self {
            source_start: 0,
            source_end: length,
            to_string_start: 0,
            to_string_end: length,

            lineno,
            column,
        }
    }
}

/// Maps to js::frontend::ScriptStencil in m-c/js/src/frontend/Stencil.h.
#[derive(Debug)]
pub struct ScriptStencil {
    // Fields for BaseScript.
    // Used by:
    //   * Global script
    //   * Eval
    //   * Module
    //   * non-lazy Function (except asm.js module)
    //   * lazy Function (cannot be asm.js module)
    /// See `BaseScript::immutableFlags_`.
    pub immutable_flags: ImmutableScriptFlags,

    /// For top level script and non-lazy function script,
    /// this is a list of GC things referred by bytecode and scope.
    ///
    /// For lazy function script, this contains the list of inner functions,
    /// followed by the list of names defined and closed over by inner script.
    /// The list of names are delimited by GCThing::Null for each scope.
    ///
    /// The order of scopes are depth-first post-order, and names inside each
    /// scope is in not defined.
    ///
    /// Trailing scopes without any names are omitted for space efficiency.
    pub gcthings: Vec<GCThing>,

    /// See `BaseScript::sharedData_`.
    pub immutable_script_data: Option<ImmutableScriptDataIndex>,

    /// The location of this script in the source.
    pub extent: SourceExtent,

    // Fields for JSFunction.
    // Used by:
    //   * non-lazy Function
    //   * lazy Function
    //   * asm.js module
    /// The explicit or implicit name of the function. The FunctionFlags
    /// indicate the kind of name.
    pub fun_name: Option<SourceAtomSetIndex>,

    /// See `JSFunction::nargs_`.
    pub fun_nargs: u16,

    /// See: `FunctionFlags`.
    pub fun_flags: FunctionFlags,

    /// If this ScriptStencil refers to a lazy child of the function being
    /// compiled, this field holds the child's immediately enclosing scope's
    /// index. Once compilation succeeds, we will store the scope pointed by
    /// this in the child's BaseScript.  (Debugger may become confused if lazy
    /// scripts refer to partially initialized enclosing scopes, so we must
    /// avoid storing the scope in the BaseScript until compilation has
    /// completed successfully.)
    pub lazy_function_enclosing_scope_index: Option<ScopeIndex>,

    /// This function is a standalone function that is not syntactically part of
    /// another script. Eg. Created by `new Function("")`.
    pub is_standalone_function: bool,

    /// This is set by the emitter of the enclosing script when a
    /// reference to this function is generated.
    pub was_function_emitted: bool,

    /// This function should be marked as a singleton. It is expected to be
    /// defined at most once. This is a heuristic only and does not affect
    /// correctness.
    pub is_singleton_function: bool,
}

impl ScriptStencil {
    fn empty_top_level_script() -> Self {
        Self {
            immutable_flags: ImmutableScriptFlags::new(),
            gcthings: Vec::new(),
            immutable_script_data: None,
            extent: SourceExtent::new(),
            fun_name: None,
            fun_nargs: 0,
            fun_flags: FunctionFlags::empty(),
            lazy_function_enclosing_scope_index: None,
            is_standalone_function: false,
            was_function_emitted: false,
            is_singleton_function: false,
        }
    }

    pub fn top_level_script(
        gcthings: Vec<GCThing>,
        immutable_script_data: ImmutableScriptDataIndex,
        extent: SourceExtent,
    ) -> Self {
        Self {
            immutable_flags: ImmutableScriptFlags::new(),
            gcthings,
            immutable_script_data: Some(immutable_script_data),
            extent,
            fun_name: None,
            fun_nargs: 0,
            fun_flags: FunctionFlags::empty(),
            lazy_function_enclosing_scope_index: None,
            is_standalone_function: false,
            was_function_emitted: false,
            is_singleton_function: false,
        }
    }

    pub fn lazy_function(
        extent: SourceExtent,
        fun_name: Option<SourceAtomSetIndex>,
        is_generator: bool,
        is_async: bool,
        fun_flags: FunctionFlags,
        lazy_function_enclosing_scope_index: Option<ScopeIndex>,
    ) -> Self {
        let mut flags = ImmutableScriptFlagsEnum::IsFunction as u32;
        if is_generator {
            flags |= ImmutableScriptFlagsEnum::IsGenerator as u32;
        }
        if is_async {
            flags |= ImmutableScriptFlagsEnum::IsAsync as u32;
        }

        Self {
            immutable_flags: ImmutableScriptFlags::from_raw(flags),
            gcthings: Vec::new(),
            immutable_script_data: None,
            extent,
            fun_name,
            fun_nargs: 0,
            fun_flags,
            lazy_function_enclosing_scope_index,
            is_standalone_function: false,
            was_function_emitted: false,
            is_singleton_function: false,
        }
    }

    pub fn is_function(&self) -> bool {
        self.immutable_flags
            .has(ImmutableScriptFlagsEnum::IsFunction)
    }

    pub fn is_non_lazy_function(&self) -> bool {
        self.is_function() && self.immutable_script_data.is_some()
    }

    pub fn is_lazy_function(&self) -> bool {
        self.is_function() && self.immutable_script_data.is_none()
    }

    pub fn set_function_has_this_binding(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::FunctionHasThisBinding);
    }

    pub fn set_has_rest(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags.set(ImmutableScriptFlagsEnum::HasRest);
    }

    pub fn set_needs_function_environment_objects(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::NeedsFunctionEnvironmentObjects);
    }

    pub fn set_function_has_extra_body_var_scope(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::FunctionHasExtraBodyVarScope);
    }

    pub fn set_should_declare_arguments(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::ShouldDeclareArguments);
    }

    pub fn set_needs_args_obj(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::NeedsArgsObj);
    }

    pub fn set_has_mapped_args_obj(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::HasMappedArgsObj);
    }

    pub fn set_function_has_new_target_binding(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::FunctionHasNewTargetBinding);
    }

    pub fn is_arrow_function(&self) -> bool {
        self.is_function() && self.fun_flags.is_arrow()
    }

    pub fn set_fun_name(&mut self, name: SourceAtomSetIndex) {
        debug_assert!(self.is_function());
        self.fun_name = Some(name);
    }

    pub fn fun_name<'a>(&'a self) -> &'a Option<SourceAtomSetIndex> {
        debug_assert!(self.is_function());
        &self.fun_name
    }

    pub fn add_fun_nargs(&mut self) {
        debug_assert!(self.is_function());
        self.fun_nargs += 1;
    }

    /// source_start should point the start of parameter for functions.
    pub fn set_source_starts(&mut self, source_start: usize) {
        self.extent.source_start = source_start as u32;
    }

    /// to_string_end should point the end of function body for function,
    /// and the end of class for constructor.
    pub fn set_to_string_end(&mut self, to_string_end: usize) {
        self.extent.to_string_end = to_string_end as u32;
    }

    /// source_end should point the end of function body.
    pub fn set_source_end(&mut self, source_end: usize) {
        self.extent.source_end = source_end as u32;
    }

    pub fn set_function_emitted(&mut self) {
        self.was_function_emitted = true;
    }

    pub fn push_inner_function(&mut self, fun: ScriptStencilIndex) {
        self.immutable_flags
            .set(ImmutableScriptFlagsEnum::HasInnerFunctions);
        self.gcthings.push(GCThing::Function(fun));
    }

    pub fn push_closed_over_bindings(&mut self, name: SourceAtomSetIndex) {
        debug_assert!(self.is_lazy_function());
        self.gcthings.push(GCThing::Atom(name));
    }

    pub fn push_closed_over_bindings_delimiter(&mut self) {
        debug_assert!(self.is_lazy_function());
        self.gcthings.push(GCThing::Null);
    }
}

/// Index into ScriptStencilList.scripts.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ScriptStencilIndex {
    index: usize,
}

impl ScriptStencilIndex {
    fn new(index: usize) -> Self {
        Self { index }
    }
}

impl From<ScriptStencilIndex> for usize {
    fn from(index: ScriptStencilIndex) -> usize {
        index.index
    }
}

/// List of stencil scripts.
#[derive(Debug)]
pub struct ScriptStencilList {
    scripts: Vec<ScriptStencil>,
}

impl ScriptStencilList {
    pub fn new() -> Self {
        Self {
            scripts: Vec::new(),
        }
    }

    pub fn new_with_empty_top_level() -> Self {
        Self {
            scripts: vec![ScriptStencil::empty_top_level_script()],
        }
    }

    pub fn push(&mut self, script: ScriptStencil) -> ScriptStencilIndex {
        let index = self.scripts.len();
        self.scripts.push(script);
        ScriptStencilIndex::new(index)
    }

    pub fn get<'a>(&'a self, index: ScriptStencilIndex) -> &'a ScriptStencil {
        &self.scripts[usize::from(index)]
    }

    pub fn get_mut<'a>(&'a mut self, index: ScriptStencilIndex) -> &'a mut ScriptStencil {
        &mut self.scripts[usize::from(index)]
    }

    pub fn set_top_level(&mut self, top_level: ScriptStencil) {
        self.scripts[0] = top_level;
    }
}

impl From<ScriptStencilList> for Vec<ScriptStencil> {
    fn from(list: ScriptStencilList) -> Vec<ScriptStencil> {
        list.scripts.into_iter().collect()
    }
}