Skip to main content

Context

Struct Context 

Source
pub struct Context<'ast> {
    pub atom_table: AtomTable,
    pub warn_undefined: bool,
    /* private fields */
}
Expand description

The storage for AST nodes.

Can be used to allocate and free nodes. Nodes allocated in one Context must not be referenced by another Context’s AST.

Fields§

§atom_table: AtomTable

All identifiers are kept here.

§warn_undefined: bool

Whether to warn about undefined variables in strict mode functions.

Implementations§

Source§

impl<'ast> Context<'ast>

Source

pub fn new() -> Self

Allocate a new Context with a new ID.

Source

pub fn lock<'ctx>(&'ctx mut self) -> GCLock<'ast, 'ctx>

Acquire a GCLock on this Context. This is just a more ergonomic way to call GCLock::new.

Source

pub fn atom_table(&self) -> &AtomTable

Return the atom table.

Source

pub fn atom_bytes<V: Into<Vec<u8>> + AsRef<[u8]>>(&self, value: V) -> AtomBytes

Add a byte-string to the identifier table.

Source

pub fn bytes(&self, ident: AtomBytes) -> &[u8]

Obtain the contents of an atom from the atom table.

Source

pub fn bytes_str_lossy(&self, ident: AtomBytes) -> &str

Obtain the contents of an atom as a string, substituting U+FFFD for anything unrepresentable. See AtomTable::bytes_str_lossy.

Source

pub fn try_bytes_str(&self, ident: AtomBytes) -> Option<&str>

Obtain the contents of an atom as a string, or None if it holds an unpaired surrogate, which has no UTF-8 form. See AtomTable::try_bytes_str.

Source

pub fn strict_mode(&self) -> bool

Return true if strict mode has been forced globally.

Source

pub fn enable_strict_mode(&mut self)

Enable strict mode. Note that it cannot be unset.

Source

pub fn enable_eval(&self) -> bool

Return true if eval() is enabled. Mirrors C++ Context::getEnableEval() (Context.h:407-409).

Source

pub fn set_enable_eval(&mut self, v: bool)

Enable or disable eval(). Mirrors C++ Context::setEnableEval() (Context.h:410-412).

Source

pub fn parse_flow(&self) -> bool

Return true if Flow type parsing is enabled. Mirrors C++ Context::getParseFlow().

Source

pub fn set_parse_flow(&mut self, v: bool)

Enable or disable Flow type parsing. Mirrors C++ Context::setParseFlow().

Source

pub fn parse_flow_ambiguous(&self) -> bool

Return true if the Flow ambiguous-expression grammar is enabled. Mirrors C++ Context::getParseFlowAmbiguous().

Source

pub fn set_parse_flow_ambiguous(&mut self, v: bool)

Enable or disable the Flow ambiguous-expression grammar.

Source

pub fn parse_flow_component_syntax(&self) -> bool

Return true if Flow component/hook syntax is enabled. Mirrors C++ Context::getParseFlowComponentSyntax().

Source

pub fn set_parse_flow_component_syntax(&mut self, v: bool)

Enable or disable Flow component/hook syntax.

Source

pub fn parse_flow_records(&self) -> bool

Return true if Flow record declarations/expressions are enabled. Mirrors C++ Context::getParseFlowRecords().

Source

pub fn set_parse_flow_records(&mut self, v: bool)

Enable or disable Flow record declarations/expressions.

Source

pub fn parse_flow_match(&self) -> bool

Return true if Flow match expressions/statements are enabled. Mirrors C++ Context::getParseFlowMatch().

Source

pub fn set_parse_flow_match(&mut self, v: bool)

Enable or disable Flow match expressions/statements.

Source

pub fn parse_ts(&self) -> bool

Return true if TypeScript type parsing is enabled. Mirrors C++ Context::getParseTS().

Source

pub fn set_parse_ts(&mut self, v: bool)

Enable or disable TypeScript type parsing. Mirrors C++ Context::setParseTS().

Source

pub fn parse_jsx(&self) -> bool

Return true if JSX parsing is enabled. Mirrors C++ Context::getParseJSX().

Source

pub fn set_parse_jsx(&mut self, v: bool)

Enable or disable JSX parsing. Mirrors C++ Context::setParseJSX(). Currently only read by the TS <Type> cast gate; the setter is wired when the JSX phase lands.

Source

pub fn preemptive_function_compilation_threshold(&self) -> u32

Return the preemptive-function-compilation threshold (bytes). Port of Context::getPreemptiveFunctionCompilationThreshold() (Context.h:516-518).

Source

pub fn set_preemptive_function_compilation_threshold(&mut self, byte_count: u32)

Set the preemptive-function-compilation threshold (bytes). Port of Context::setPreemptiveFunctionCompilationThreshold() (Context.h:520-522).

Source

pub fn gc(&mut self)

Mark and sweep the arena: everything reachable from a live NodeRc survives, the rest is returned to the free lists. Requires &mut self, so no GCLock — and therefore no &Node — can be outstanding.

Source

pub fn take_freed_node_ids(&mut self) -> Vec<NodeId>

Drain and return the ids of every node freed (by gc() or by an AllocationScope truncation) since the last call. Consumers use this to prune dead entries out of side tables keyed by NodeId.

Source

pub fn num_nodes(&self) -> usize

Returns the number of node slots which have been allocated. Includes nodes currently in use as well as nodes in the free list.

Source

pub fn num_list_elements(&self) -> usize

Returns the number of list-element slots which have been allocated. Includes elements currently in use as well as elements in the free list.

Source

pub fn num_free_nodes(&self) -> usize

Returns the number of node slots currently in the free list (i.e. allocated but unused, reclaimed by GC).

Source

pub fn storage_size(&self) -> usize

Returns the approximate size of just the AST storages in bytes. Includes the allocated nodes, lists, as well as free lists for both.

Trait Implementations§

Source§

impl<'ast> Debug for Context<'ast>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Context<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Context<'_>

Source§

fn drop(&mut self)

Ensure that there are no outstanding NodeRcs into this Context which will be invalidated once it is dropped.

§Panics

Will panic if there are any NodeRcs stored when this Context is dropped.

The panic is the only effect: before panicking, the node storage and the NodeRc counter are leaked (Context::leak_noderc_targets), so the outstanding handles — which are dropped during the ensuing unwind, or later still if the panic is caught — decrement refcounts in memory that is still valid. Leaking the arena is the price of keeping a caller’s bug a panic instead of a use-after-free.

Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl HeapSize for Context<'_>

Source§

fn heap_size(&self) -> usize

Returns the heap size of the AST storages only. Atom-table memory is intentionally excluded: the AtomTable is externally owned and accounted for separately.

Auto Trait Implementations§

§

impl<'ast> !Freeze for Context<'ast>

§

impl<'ast> !RefUnwindSafe for Context<'ast>

§

impl<'ast> !Send for Context<'ast>

§

impl<'ast> !Sync for Context<'ast>

§

impl<'ast> !UnwindSafe for Context<'ast>

§

impl<'ast> Unpin for Context<'ast>

§

impl<'ast> UnsafeUnpin for Context<'ast>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.