Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SourceText collection & toString() for fns and methods #4038

Merged
merged 18 commits into from
Feb 3, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
struct naming & fn form
Nikita-str committed Jan 8, 2025
commit a199c9deda5ae18a7e754ff4a5c54943c6b2d319
6 changes: 6 additions & 0 deletions core/ast/src/source_text.rs
Original file line number Diff line number Diff line change
@@ -16,26 +16,31 @@ impl SourceText {
source_text: Vec::with_capacity(capacity),
}
}

/// Get current `LinearPosition`.
#[must_use]
pub fn cur_linear_position(&self) -> LinearPosition {
LinearPosition::new(self.source_text.len())
}

/// Get code points from `pos` to the current end.
#[must_use]
pub fn get_code_points_from_pos(&self, pos: LinearPosition) -> &[u16] {
&self.source_text[pos.pos()..]
}

/// Get code points within `span`.
#[must_use]
pub fn get_code_points_from_span(&self, span: LinearSpan) -> &[u16] {
&self.source_text[span.start().pos()..span.end().pos()]
}

/// Remove last code point.
#[inline]
pub fn remove_last_code_point(&mut self) {
self.source_text.pop();
}

/// Collect code point.
///
/// # Panics
@@ -57,6 +62,7 @@ impl SourceText {
self.push(cu1);
self.push(cu2);
}

#[inline]
fn push(&mut self, cp: u16) {
self.source_text.push(cp);
6 changes: 3 additions & 3 deletions core/engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@ use crate::{
bytecompiler::ByteCompiler,
js_string,
vm::{CodeBlock, CodeBlockFlags, Opcode},
JsString, SourceTextInner, SpannedSourceText,
JsString, SourceText, SpannedSourceText,
};
use boa_ast::{
function::{FormalParameterList, FunctionBody},
@@ -25,7 +25,7 @@ pub(crate) struct FunctionCompiler {
method: bool,
in_with: bool,
name_scope: Option<Scope>,
source_text_inner: Option<Gc<SourceTextInner>>,
source_text_inner: Option<SourceText>,
source_text_span: Option<LinearSpan>,
}

@@ -102,7 +102,7 @@ impl FunctionCompiler {
pub(crate) fn linear_span(
mut self,
linear_span: Option<LinearSpan>,
source_text: Option<Gc<SourceTextInner>>,
source_text: Option<SourceText>,
) -> Self {
self.source_text_inner = source_text;
self.source_text_span = linear_span;
6 changes: 3 additions & 3 deletions core/engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
@@ -21,7 +21,7 @@ use crate::{
BindingOpcode, CallFrame, CodeBlock, CodeBlockFlags, Constant, GeneratorResumeKind,
Handler, InlineCache, Opcode, VaryingOperandKind,
},
JsBigInt, JsStr, JsString, SourceTextInner, SpannedSourceText,
JsBigInt, JsStr, JsString, SourceText, SpannedSourceText,
};
use boa_ast::{
declaration::{Binding, LexicalDeclaration, VarDeclaration},
@@ -452,7 +452,7 @@ pub struct ByteCompiler<'ctx> {
pub(crate) emitted_mapped_arguments_object_opcode: bool,

pub(crate) interner: &'ctx mut Interner,
pub(crate) source_text_inner: Option<Gc<SourceTextInner>>,
pub(crate) source_text_inner: Option<SourceText>,
pub(crate) source_text_spanned: Option<SpannedSourceText>,

#[cfg(feature = "annex-b")]
@@ -554,7 +554,7 @@ impl<'ctx> ByteCompiler<'ctx> {
}
}

pub(crate) fn set_source_text_inner(&mut self, source_text_inner: Option<Gc<SourceTextInner>>) {
pub(crate) fn set_source_text_inner(&mut self, source_text_inner: Option<SourceText>) {
self.source_text_inner = source_text_inner;
}

2 changes: 1 addition & 1 deletion core/engine/src/lib.rs
Original file line number Diff line number Diff line change
@@ -110,7 +110,7 @@ mod small_map;
mod sys;

mod spanned_source_text;
use spanned_source_text::SourceTextInner;
use spanned_source_text::SourceText;
pub use spanned_source_text::SpannedSourceText;

#[cfg(test)]
10 changes: 5 additions & 5 deletions core/engine/src/script.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,7 @@ use crate::{
bytecompiler::{global_declaration_instantiation_context, ByteCompiler},
js_string,
realm::Realm,
spanned_source_text::SourceTextInner,
spanned_source_text::SourceText,
vm::{ActiveRunnable, CallFrame, CallFrameFlags, CodeBlock},
Context, HostDefined, JsResult, JsString, JsValue, Module,
};
@@ -48,7 +48,7 @@ struct Inner {
realm: Realm,
#[unsafe_ignore_trace]
source: boa_ast::Script,
source_text: Option<Gc<SourceTextInner>>,
source_text: Option<SourceText>,
codeblock: GcRefCell<Option<Gc<CodeBlock>>>,
loaded_modules: GcRefCell<FxHashMap<JsString, Module>>,
host_defined: HostDefined,
@@ -98,13 +98,13 @@ impl Script {
context.optimize_statement_list(code.statements_mut());
}

let source_text_inner = SourceTextInner::new(&mut code);
let source_text = code.take_source().map(SourceText::new);

Ok(Self {
inner: Gc::new(Inner {
realm: realm.unwrap_or_else(|| context.realm().clone()),
source: code,
source_text: source_text_inner,
source_text,
codeblock: GcRefCell::default(),
loaded_modules: GcRefCell::default(),
host_defined: HostDefined::default(),
@@ -245,7 +245,7 @@ impl Script {
self.inner.path.as_deref()
}

pub(super) fn get_source(&self) -> Option<Gc<SourceTextInner>> {
pub(super) fn get_source(&self) -> Option<SourceText> {
self.inner.source_text.clone()
}
}
42 changes: 30 additions & 12 deletions core/engine/src/spanned_source_text.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,45 @@
use boa_ast::{LinearSpan, SourceText};
use boa_ast::LinearSpan;
use boa_gc::{Finalize, Gc, Trace};

#[derive(Trace, Finalize)]
pub(crate) struct SourceTextInner {
struct Inner {
#[unsafe_ignore_trace]
source_text: SourceText,
source_text: boa_ast::SourceText,
}
impl Inner {
fn new(source_text: boa_ast::SourceText) -> Self {
Self { source_text }
}
}

#[derive(Trace, Finalize, Clone)]
pub(crate) struct SourceText {
source_text: Gc<Inner>,
}
impl SourceTextInner {

impl SourceText {
#[must_use]
pub(crate) fn new(code: &mut boa_ast::Script) -> Option<Gc<Self>> {
code.take_source()
.map(|source_text| Gc::new(Self { source_text }))
pub(crate) fn new(source_text: boa_ast::SourceText) -> Self {
Self {
source_text: Gc::new(Inner::new(source_text)),
}
}

fn inner(&self) -> &boa_ast::SourceText {
&self.source_text.source_text
}
}

/// Contains pointer to source code and span of the object.
#[derive(Trace, Finalize, Clone)]
pub struct SpannedSourceText {
gc: Gc<SourceTextInner>,
source_text: SourceText,
#[unsafe_ignore_trace]
span: LinearSpan,
}
impl SpannedSourceText {
pub(crate) fn new(gc: Gc<SourceTextInner>, span: LinearSpan) -> Self {
Self { gc, span }
pub(crate) fn new(source_text: SourceText, span: LinearSpan) -> Self {
Self { source_text, span }
}

/// Test if the span is empty.
@@ -36,7 +52,9 @@ impl SpannedSourceText {
/// Gets inner code points.
#[must_use]
pub fn to_code_points(&self) -> &[u16] {
self.gc.source_text.get_code_points_from_span(self.span)
self.source_text
.inner()
.get_code_points_from_span(self.span)
}
}

@@ -45,7 +63,7 @@ impl std::fmt::Debug for SpannedSourceText {
f.debug_struct("SourceTextSpanned").finish()
}
}
impl std::fmt::Debug for SourceTextInner {
impl std::fmt::Debug for SourceText {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SourceTextInner").finish()
}