Skip to content

Commit

Permalink
Remove EmitResult.top_level_script and use EmitResult.scripts for bot…
Browse files Browse the repository at this point in the history
…h top level script and functions (fixes mozilla-spidermonkey#629)
  • Loading branch information
arai-a committed Sep 1, 2020
1 parent b4bcfed commit af9f930
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 44 deletions.
20 changes: 5 additions & 15 deletions crates/driver/src/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,25 +160,15 @@ fn handle_script<'alloc>(
}

if verbosity.bytecode {
let script_data_index: usize = result
.top_level_script
.immutable_script_data
.expect("Top level script should have ImmutableScriptData")
.into();
let script_data = &result.script_data_list[script_data_index];
println!("\n# Bytecode\n{}", emitter::dis(&script_data.bytecode));
}

for (i, fun) in result.functions.iter().enumerate() {
if verbosity.bytecode {
if fun.is_non_lazy_function() {
let script_data_index: usize = fun
for (i, script) in result.scripts.iter().enumerate() {
if !script.is_function() || script.is_non_lazy_function() {
let script_data_index: usize = script
.immutable_script_data
.expect("Non-lazy function should have ImmutableScriptData")
.expect("Non-lazy script should have ImmutableScriptData")
.into();
let script_data = &result.script_data_list[script_data_index];
println!(
"\n# bytecode for functions[{}]\n{}",
"\n# bytecode for script[{}]\n{}",
i,
emitter::dis(&script_data.bytecode)
);
Expand Down
7 changes: 4 additions & 3 deletions crates/emitter/src/ast_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ pub fn emit_program<'alloc>(
}
};

compilation_info.scripts.set_top_level(script);

Ok(EmitResult::new(
compilation_info.atoms.into(),
compilation_info.slices.into(),
compilation_info.scope_data_map.into(),
compilation_info.regexps.into(),
script,
compilation_info.functions.into(),
compilation_info.scripts.into(),
compilation_info.script_data_list.into(),
))
}
Expand Down Expand Up @@ -163,7 +164,7 @@ impl<'alloc, 'opt> AstEmitter<'alloc, 'opt> {

let name = self
.compilation_info
.functions
.scripts
.get(stencil_index)
.fun_name()
.expect("Function declaration should have name");
Expand Down
6 changes: 3 additions & 3 deletions crates/emitter/src/compilation_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct CompilationInfo<'alloc> {
pub function_declarations: HashMap<ScriptStencilIndex, &'alloc Function<'alloc>>,
pub function_stencil_indices: AssociatedData<ScriptStencilIndex>,
pub function_declaration_properties: FunctionDeclarationPropertyMap,
pub functions: ScriptStencilList,
pub scripts: ScriptStencilList,
pub script_data_list: ImmutableScriptDataList,
}

Expand All @@ -28,7 +28,7 @@ impl<'alloc> CompilationInfo<'alloc> {
function_declarations: HashMap<ScriptStencilIndex, &'alloc Function<'alloc>>,
function_stencil_indices: AssociatedData<ScriptStencilIndex>,
function_declaration_properties: FunctionDeclarationPropertyMap,
functions: ScriptStencilList,
scripts: ScriptStencilList,
) -> Self {
Self {
atoms,
Expand All @@ -38,7 +38,7 @@ impl<'alloc> CompilationInfo<'alloc> {
function_declarations,
function_stencil_indices,
function_declaration_properties,
functions,
scripts,
script_data_list: ImmutableScriptDataList::new(),
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/emitter/src/function_declaration_emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl LazyFunctionEmitter {
pub fn emit(self, emitter: &mut AstEmitter) -> GCThingIndex {
emitter
.compilation_info
.functions
.scripts
.get_mut(self.stencil_index)
.set_function_emitted();
emitter.emit.get_function_gcthing_index(self.stencil_index)
Expand Down
7 changes: 3 additions & 4 deletions crates/emitter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn emit<'alloc>(
function_declarations,
function_stencil_indices,
function_declaration_properties,
functions,
scripts,
error,
} = scope::generate_scope_data(ast);

Expand All @@ -57,7 +57,7 @@ pub fn emit<'alloc>(
function_declarations,
function_stencil_indices,
function_declaration_properties,
functions,
scripts,
);
ast_emitter::emit_program(ast, options, compilation_info)
}
Expand Down Expand Up @@ -100,8 +100,7 @@ mod tests {
)
.expect("Should work!");

let script_data_index: usize = result
.top_level_script
let script_data_index: usize = result.scripts[0]
.immutable_script_data
.expect("Top level script should have ImmutableScriptData")
.into();
Expand Down
2 changes: 1 addition & 1 deletion crates/interpreter/src/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ pub fn evaluate(result: &EmitResult, global: Rc<RefCell<Object>>) -> Result<JSVa
let mut stack = Vec::new();
let mut rval = JSValue::Undefined;

let script = &result.top_level_script;
let script = &result.scripts[0];
let script_data_index: usize = script
.immutable_script_data
.expect("Top level scropt should have ImmutableScriptData")
Expand Down
16 changes: 9 additions & 7 deletions crates/scope/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2430,7 +2430,7 @@ pub struct FunctionScriptStencilBuilder {
/// * map from Function AST node (`function_stencil_indices`)
/// * enclosing script/function, to list inner functions
function_stencil_indices: AssociatedData<ScriptStencilIndex>,
functions: ScriptStencilList,
scripts: ScriptStencilList,

/// The stack of functions that the current context is in.
///
Expand All @@ -2441,9 +2441,11 @@ pub struct FunctionScriptStencilBuilder {

impl FunctionScriptStencilBuilder {
fn new() -> Self {
let scripts = ScriptStencilList::new_with_empty_top_level();

Self {
function_stencil_indices: AssociatedData::new(),
functions: ScriptStencilList::new(),
scripts,
function_stack: Vec::new(),
}
}
Expand Down Expand Up @@ -2483,7 +2485,7 @@ impl FunctionScriptStencilBuilder {
FunctionFlags::interpreted(syntax_kind),
enclosing_scope_index,
);
let index = self.functions.push(function_stencil);
let index = self.scripts.push(function_stencil);
self.function_stencil_indices.insert(fun, index);

match self.maybe_current_mut() {
Expand Down Expand Up @@ -2524,7 +2526,7 @@ impl FunctionScriptStencilBuilder {
/// Returns a immutable reference to the innermost function. None otherwise.
fn maybe_current<'a>(&'a self) -> Option<&'a ScriptStencil> {
let maybe_index = self.function_stack.last();
maybe_index.map(move |index| self.functions.get(*index))
maybe_index.map(move |index| self.scripts.get(*index))
}

/// Returns a immutable reference to the current function.
Expand All @@ -2536,7 +2538,7 @@ impl FunctionScriptStencilBuilder {
/// Returns a mutable reference to the innermost function. None otherwise.
fn maybe_current_mut<'a>(&'a mut self) -> Option<&'a mut ScriptStencil> {
let maybe_index = self.function_stack.last().cloned();
maybe_index.map(move |index| self.functions.get_mut(index))
maybe_index.map(move |index| self.scripts.get_mut(index))
}

/// Returns a mutable reference to the current function.
Expand Down Expand Up @@ -3460,7 +3462,7 @@ pub struct ScopeDataMapAndScriptStencilList {
pub scope_data_map: ScopeDataMap,
pub function_stencil_indices: AssociatedData<ScriptStencilIndex>,
pub function_declaration_properties: FunctionDeclarationPropertyMap,
pub functions: ScriptStencilList,
pub scripts: ScriptStencilList,
pub error: Option<ScopeBuildError>,
}

Expand All @@ -3474,7 +3476,7 @@ impl From<ScopeDataMapBuilder> for ScopeDataMapAndScriptStencilList {
),
function_stencil_indices: builder.function_stencil_builder.function_stencil_indices,
function_declaration_properties: builder.function_declaration_properties,
functions: builder.function_stencil_builder.functions,
scripts: builder.function_stencil_builder.scripts,
error: builder.error,
}
}
Expand Down
6 changes: 3 additions & 3 deletions crates/scope/src/pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub struct ScopePassResult<'alloc> {
pub function_declarations: HashMap<ScriptStencilIndex, &'alloc Function<'alloc>>,
pub function_stencil_indices: AssociatedData<ScriptStencilIndex>,
pub function_declaration_properties: FunctionDeclarationPropertyMap,
pub functions: ScriptStencilList,
pub scripts: ScriptStencilList,
pub error: Option<ScopeBuildError>,
}

Expand Down Expand Up @@ -53,15 +53,15 @@ impl<'alloc> From<ScopePass<'alloc>> for ScopePassResult<'alloc> {
scope_data_map,
function_stencil_indices,
function_declaration_properties,
functions,
scripts,
error,
} = pass.builder.into();
ScopePassResult {
scope_data_map,
function_declarations: pass.function_declarations,
function_stencil_indices,
function_declaration_properties,
functions,
scripts,
error,
}
}
Expand Down
9 changes: 3 additions & 6 deletions crates/stencil/src/result.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub struct EmitResult<'alloc> {
pub scopes: Vec<ScopeData>,
pub regexps: Vec<RegExpItem>,

pub top_level_script: ScriptStencil,
pub functions: Vec<ScriptStencil>,
pub scripts: Vec<ScriptStencil>,
pub script_data_list: Vec<ImmutableScriptData>,
}

Expand All @@ -21,17 +20,15 @@ impl<'alloc> EmitResult<'alloc> {
slices: Vec<&'alloc str>,
scopes: Vec<ScopeData>,
regexps: Vec<RegExpItem>,
top_level_script: ScriptStencil,
functions: Vec<ScriptStencil>,
scripts: Vec<ScriptStencil>,
script_data_list: Vec<ImmutableScriptData>,
) -> Self {
Self {
atoms,
slices,
scopes,
regexps,
top_level_script,
functions,
scripts,
script_data_list,
}
}
Expand Down
38 changes: 38 additions & 0 deletions crates/stencil/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,18 @@ pub struct SourceExtent {
}

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,
Expand Down Expand Up @@ -315,6 +327,22 @@ pub struct ScriptStencil {
}

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,
Expand Down Expand Up @@ -513,6 +541,12 @@ impl ScriptStencilList {
}
}

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);
Expand All @@ -526,6 +560,10 @@ impl ScriptStencilList {
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> {
Expand Down
2 changes: 1 addition & 1 deletion gecko-patches.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
D85363:1648574
D88970:1662383

0 comments on commit af9f930

Please sign in to comment.