Skip to content

Commit

Permalink
change way of passing the SpannedSourceText
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita-str committed Jan 9, 2025
1 parent 1f2893c commit cbb2d79
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 78 deletions.
8 changes: 5 additions & 3 deletions core/engine/src/builtins/eval/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use crate::{
spanned_source_text::SourceText,
string::StaticJsStrings,
vm::{CallFrame, CallFrameFlags, Constant, Opcode},
Context, JsArgs, JsResult, JsString, JsValue,
Context, JsArgs, JsResult, JsString, JsValue, SpannedSourceText,
};
use boa_ast::{
operations::{contains, contains_arguments, ContainsSymbol},
Expand Down Expand Up @@ -262,6 +262,9 @@ impl Eval {

let in_with = context.vm.environments.has_object_environment();

let source_text = SourceText::new(source);
let spanned_source_text = SpannedSourceText::new_source_only(source_text);

let mut compiler = ByteCompiler::new(
js_string!("<main>"),
body.strict(),
Expand All @@ -272,9 +275,8 @@ impl Eval {
false,
context.interner_mut(),
in_with,
spanned_source_text,
);
let source_text = SourceText::new(source);
compiler.set_source_text(source_text);

compiler.current_open_environments_count += 1;

Expand Down
12 changes: 6 additions & 6 deletions core/engine/src/builtins/function/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use crate::{
symbol::JsSymbol,
value::IntegerOrInfinity,
vm::{ActiveRunnable, CallFrame, CallFrameFlags, CodeBlock},
Context, JsArgs, JsResult, JsStr, JsString, JsValue,
Context, JsArgs, JsResult, JsStr, JsString, JsValue, SpannedSourceText,
};
use boa_ast::{
function::{FormalParameterList, FunctionBody},
Expand Down Expand Up @@ -646,7 +646,9 @@ impl BuiltInFunctionObject {
}

let in_with = context.vm.environments.has_object_environment();
let code = FunctionCompiler::new()
let spanned_source_text = SpannedSourceText::new_pseudo();

let code = FunctionCompiler::new(spanned_source_text)
.name(js_string!("anonymous"))
.generator(generator)
.r#async(r#async)
Expand Down Expand Up @@ -868,10 +870,8 @@ impl BuiltInFunctionObject {
.ok_or_else(|| JsNativeError::typ().with_message("not a function"))?;

let code = function.codeblock();
if let Some(source) = &code.source_text_spanned {
if !source.is_empty() {
return Ok(JsString::from(source.to_code_points()).into());
}
if let Some(code_points) = code.source_text_spanned.to_code_points() {
return Ok(JsString::from(code_points).into());
}

Ok(js_string!(
Expand Down
7 changes: 6 additions & 1 deletion core/engine/src/builtins/json/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use crate::{
symbol::JsSymbol,
value::IntegerOrInfinity,
vm::{CallFrame, CallFrameFlags},
Context, JsArgs, JsBigInt, JsResult, JsString, JsValue,
Context, JsArgs, JsBigInt, JsResult, JsString, JsValue, SpannedSourceText,
};
use boa_gc::Gc;
use boa_parser::{Parser, Source};
Expand Down Expand Up @@ -112,9 +112,13 @@ impl Json {
// 10. Assert: unfiltered is either a String, Number, Boolean, Null, or an Object that is defined by either an ArrayLiteral or an ObjectLiteral.
let mut parser = Parser::new(Source::from_bytes(&script_string));
parser.set_json_parse();
// In json we don't need the source: there no way to pass an object that needs a source text
// But if it's incorrect, just call `parser.parse_script_with_source` here
let script = parser.parse_script(&Scope::new_global(), context.interner_mut())?;
let code_block = {
let in_with = context.vm.environments.has_object_environment();
// If the source is needed then call `parser.parse_script_with_source` and pass `source_text` here.
let spanned_source_text = SpannedSourceText::new_pseudo();
let mut compiler = ByteCompiler::new(
js_string!("<main>"),
script.strict(),
Expand All @@ -125,6 +129,7 @@ impl Json {
false,
context.interner_mut(),
in_with,
spanned_source_text,
);
compiler.compile_statement_list(script.statements(), true, false);
Gc::new(compiler.finish())
Expand Down
16 changes: 12 additions & 4 deletions core/engine/src/bytecompiler/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use super::{ByteCompiler, Literal, Operand, ToJsString};
use crate::{
js_string,
vm::{BindingOpcode, CodeBlock, CodeBlockFlags, Opcode},
SpannedSourceText,
};
use boa_ast::{
expression::Identifier,
Expand Down Expand Up @@ -88,6 +89,8 @@ impl ByteCompiler<'_> {
None
};

// The new span is not the same as the parent `ByteCompiler` have.
let spanned_source_text = self.spanned_source_text.clone_only_source();
let mut compiler = ByteCompiler::new(
class_name.clone(),
true,
Expand All @@ -98,12 +101,9 @@ impl ByteCompiler<'_> {
false,
self.interner,
self.in_with,
spanned_source_text,
);

if let Some(source_text) = &self.source_text {
compiler.set_source_text(source_text.clone());
}

compiler.code_block_flags |= CodeBlockFlags::IS_CLASS_CONSTRUCTOR;

if let Some(expr) = &class.constructor {
Expand Down Expand Up @@ -294,6 +294,8 @@ impl ByteCompiler<'_> {
false,
self.interner,
self.in_with,
// if you need the source text then pass `self.spanned_source_text.clone_only_source()` here
SpannedSourceText::new_pseudo(),
);

// Function environment
Expand Down Expand Up @@ -330,6 +332,8 @@ impl ByteCompiler<'_> {
false,
self.interner,
self.in_with,
// if you need the source text then pass `self.spanned_source_text.clone_only_source()` here
SpannedSourceText::new_pseudo(),
);
let _ = field_compiler.push_scope(field.scope());
if let Some(node) = field.field() {
Expand Down Expand Up @@ -371,6 +375,8 @@ impl ByteCompiler<'_> {
false,
self.interner,
self.in_with,
// if you need the source text then pass `self.spanned_source_text.clone_only_source()` here
SpannedSourceText::new_pseudo(),
);
let _ = field_compiler.push_scope(field.scope());
let is_anonymous_function = if let Some(node) = &field.field() {
Expand Down Expand Up @@ -414,6 +420,8 @@ impl ByteCompiler<'_> {
false,
self.interner,
self.in_with,
// if you need the source text then pass `self.spanned_source_text.clone_only_source()` here
SpannedSourceText::new_pseudo(),
);
let _ = compiler.push_scope(block.scopes().function_scope());

Expand Down
10 changes: 5 additions & 5 deletions core/engine/src/bytecompiler/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
bytecompiler::{ByteCompiler, FunctionCompiler, FunctionSpec, NodeKind},
vm::{BindingOpcode, Opcode},
Context, JsNativeError, JsResult,
Context, JsNativeError, JsResult, SpannedSourceText,
};
use boa_ast::{
declaration::Binding,
Expand Down Expand Up @@ -516,14 +516,14 @@ impl ByteCompiler<'_> {
};

let func_span = function.linear_span();
let spanned_source_text = SpannedSourceText::new(self.source_text(), func_span);

let code = FunctionCompiler::new()
let code = FunctionCompiler::new(spanned_source_text)
.name(name.sym().to_js_string(self.interner()))
.generator(generator)
.r#async(r#async)
.strict(self.strict())
.in_with(self.in_with)
.linear_span(func_span, self.source_text.clone())
.compile(
parameters,
body,
Expand Down Expand Up @@ -779,14 +779,14 @@ impl ByteCompiler<'_> {
};

let func_span = function.linear_span();
let spanned_source_text = SpannedSourceText::new(self.source_text(), func_span);

let code = FunctionCompiler::new()
let code = FunctionCompiler::new(spanned_source_text)
.name(name.sym().to_js_string(self.interner()))
.generator(generator)
.r#async(r#async)
.strict(self.strict())
.in_with(self.in_with)
.linear_span(func_span, self.source_text.clone())
.name_scope(None)
.compile(
parameters,
Expand Down
31 changes: 5 additions & 26 deletions core/engine/src/bytecompiler/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@ use crate::{
bytecompiler::ByteCompiler,
js_string,
vm::{CodeBlock, CodeBlockFlags, Opcode},
JsString, SourceText, SpannedSourceText,
JsString, SpannedSourceText,
};
use boa_ast::{
function::{FormalParameterList, FunctionBody},
scope::{FunctionScopes, Scope},
LinearSpan,
};
use boa_gc::Gc;
use boa_interner::Interner;
Expand All @@ -25,13 +24,12 @@ pub(crate) struct FunctionCompiler {
method: bool,
in_with: bool,
name_scope: Option<Scope>,
source_text: Option<SourceText>,
source_text_span: Option<LinearSpan>,
spanned_source_text: SpannedSourceText,
}

impl FunctionCompiler {
/// Create a new `FunctionCompiler`.
pub(crate) fn new() -> Self {
pub(crate) fn new(spanned_source_text: SpannedSourceText) -> Self {
Self {
name: js_string!(),
generator: false,
Expand All @@ -41,8 +39,7 @@ impl FunctionCompiler {
method: false,
in_with: false,
name_scope: None,
source_text: None,
source_text_span: None,
spanned_source_text,
}
}

Expand Down Expand Up @@ -98,17 +95,6 @@ impl FunctionCompiler {
self
}

/// Indicate if the function is in a `with` statement.
pub(crate) fn linear_span(
mut self,
linear_span: Option<LinearSpan>,
source_text: Option<SourceText>,
) -> Self {
self.source_text = source_text;
self.source_text_span = linear_span;
self
}

/// Compile a function statement list and it's parameters into bytecode.
pub(crate) fn compile(
mut self,
Expand All @@ -133,16 +119,9 @@ impl FunctionCompiler {
self.generator,
interner,
self.in_with,
self.spanned_source_text,
);

if let Some(source_text) = self.source_text {
compiler.set_source_text(source_text.clone());
if let Some(span) = self.source_text_span {
let source_text_spanned = SpannedSourceText::new(source_text, span);
compiler.set_source_text_spanned(source_text_spanned);
}
}

compiler.length = length;
compiler.code_block_flags.set(
CodeBlockFlags::HAS_PROTOTYPE_PROPERTY,
Expand Down
32 changes: 15 additions & 17 deletions core/engine/src/bytecompiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,7 @@ pub struct ByteCompiler<'ctx> {
pub(crate) emitted_mapped_arguments_object_opcode: bool,

pub(crate) interner: &'ctx mut Interner,
pub(crate) source_text: Option<SourceText>,
pub(crate) source_text_spanned: Option<SpannedSourceText>,
spanned_source_text: SpannedSourceText,

#[cfg(feature = "annex-b")]
pub(crate) annex_b_function_names: Vec<Identifier>,
Expand Down Expand Up @@ -483,6 +482,7 @@ impl<'ctx> ByteCompiler<'ctx> {
is_generator: bool,
interner: &'ctx mut Interner,
in_with: bool,
spanned_source_text: SpannedSourceText,
) -> ByteCompiler<'ctx> {
let mut code_block_flags = CodeBlockFlags::empty();
code_block_flags.set(CodeBlockFlags::STRICT, strict);
Expand Down Expand Up @@ -544,8 +544,7 @@ impl<'ctx> ByteCompiler<'ctx> {
variable_scope,
lexical_scope,
interner,
source_text: None,
source_text_spanned: None,
spanned_source_text,

#[cfg(feature = "annex-b")]
annex_b_function_names: Vec::new(),
Expand All @@ -554,12 +553,8 @@ impl<'ctx> ByteCompiler<'ctx> {
}
}

pub(crate) fn set_source_text(&mut self, source_text: SourceText) {
self.source_text = Some(source_text);
}

pub(crate) fn set_source_text_spanned(&mut self, source_text_spanned: SpannedSourceText) {
self.source_text_spanned = Some(source_text_spanned);
pub(crate) fn source_text(&self) -> SourceText {
self.spanned_source_text.source_text()
}

pub(crate) const fn strict(&self) -> bool {
Expand Down Expand Up @@ -1543,15 +1538,16 @@ impl<'ctx> ByteCompiler<'ctx> {
Some(js_string!())
};

let code = FunctionCompiler::new()
let spanned_source_text = SpannedSourceText::new(self.source_text(), linear_span);

let code = FunctionCompiler::new(spanned_source_text)
.name(name)
.generator(generator)
.r#async(r#async)
.strict(self.strict())
.arrow(arrow)
.in_with(self.in_with)
.name_scope(name_scope.cloned())
.linear_span(linear_span, self.source_text.clone())
.compile(
parameters,
body,
Expand Down Expand Up @@ -1621,7 +1617,9 @@ impl<'ctx> ByteCompiler<'ctx> {
Some(js_string!())
};

let code = FunctionCompiler::new()
let spanned_source_text = SpannedSourceText::new(self.source_text(), linear_span);

let code = FunctionCompiler::new(spanned_source_text)
.name(name)
.generator(generator)
.r#async(r#async)
Expand All @@ -1630,7 +1628,6 @@ impl<'ctx> ByteCompiler<'ctx> {
.method(true)
.in_with(self.in_with)
.name_scope(name_scope.cloned())
.linear_span(linear_span, self.source_text.clone())
.compile(
parameters,
body,
Expand Down Expand Up @@ -1666,7 +1663,9 @@ impl<'ctx> ByteCompiler<'ctx> {
Some(js_string!())
};

let code = FunctionCompiler::new()
let spanned_source_text = SpannedSourceText::new(self.source_text(), linear_span);

let code = FunctionCompiler::new(spanned_source_text)
.name(name)
.generator(generator)
.r#async(r#async)
Expand All @@ -1675,7 +1674,6 @@ impl<'ctx> ByteCompiler<'ctx> {
.method(true)
.in_with(self.in_with)
.name_scope(function.name_scope.cloned())
.linear_span(linear_span, self.source_text.clone())
.compile(
parameters,
body,
Expand Down Expand Up @@ -1830,7 +1828,7 @@ impl<'ctx> ByteCompiler<'ctx> {
handlers: self.handlers,
flags: Cell::new(self.code_block_flags),
ic: self.ic.into_boxed_slice(),
source_text_spanned: self.source_text_spanned,
source_text_spanned: self.spanned_source_text,
}
}

Expand Down
Loading

0 comments on commit cbb2d79

Please sign in to comment.