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

Implement module execution #2922

Merged
merged 18 commits into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ node_modules
yarn-error.log
.vscode/settings.json

# tests/js/test.js is used for testing changes locally
tests/js/test.js
# tests/js is used for testing changes locally
tests/js
.boa_history

# Profiling
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 114 additions & 0 deletions boa_ast/src/declaration/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use std::ops::ControlFlow;

use super::{ModuleSpecifier, VarDeclaration};
use crate::{
expression::Identifier,
function::{AsyncFunction, AsyncGenerator, Class, Function, Generator},
try_break,
visitor::{VisitWith, Visitor, VisitorMut},
Expand Down Expand Up @@ -213,3 +214,116 @@ impl VisitWith for ExportSpecifier {
visitor.visit_sym_mut(&mut self.private_name)
}
}

/// The name under which a reexported binding is exported by a module.
///
/// This differs slightly from the spec, since `[[ImportName]]` can be either a name, `all-but-default`
/// or `all`, but the last two exports can be identified with the `export_name` field from
/// [`ExportEntry`], which joins both variants into a single `Star` variant.
#[derive(Debug, Clone, Copy)]
pub enum ReExportImportName {
/// A binding of the imported module.
Name(Sym),
/// All exports of the module.
Star,
}

/// [`ExportEntry`][spec] record.
///
/// [spec]: https://tc39.es/ecma262/#table-exportentry-records
#[derive(Debug, Clone, Copy)]
pub enum ExportEntry {
/// An ordinary export entry
Ordinary(LocalExportEntry),
/// A star reexport entry.
StarReExport {
/// The module from where this reexport will import.
module_request: Sym,
},
/// A reexport entry with an export name.
ReExport(IndirectExportEntry),
}

impl From<IndirectExportEntry> for ExportEntry {
fn from(v: IndirectExportEntry) -> Self {
Self::ReExport(v)
}
}

impl From<LocalExportEntry> for ExportEntry {
fn from(v: LocalExportEntry) -> Self {
Self::Ordinary(v)
}
}

/// A local export entry
#[derive(Debug, Clone, Copy)]
pub struct LocalExportEntry {
local_name: Identifier,
export_name: Sym,
}

impl LocalExportEntry {
/// Creates a new `LocalExportEntry`.
#[must_use]
pub const fn new(local_name: Identifier, export_name: Sym) -> Self {
Self {
local_name,
export_name,
}
}

/// Gets the local name of this export entry.
#[must_use]
pub const fn local_name(&self) -> Identifier {
self.local_name
}

/// Gets the export name of this export entry.
#[must_use]
pub const fn export_name(&self) -> Sym {
self.export_name
}
}

/// A reexported export entry.
#[derive(Debug, Clone, Copy)]
pub struct IndirectExportEntry {
module_request: Sym,
import_name: ReExportImportName,
export_name: Sym,
}

impl IndirectExportEntry {
/// Creates a new `IndirectExportEntry`.
#[must_use]
pub const fn new(
module_request: Sym,
import_name: ReExportImportName,
export_name: Sym,
) -> Self {
Self {
module_request,
import_name,
export_name,
}
}

/// Gets the module from where this entry reexports.
#[must_use]
pub const fn module_request(&self) -> Sym {
self.module_request
}

/// Gets the import name of the reexport.
#[must_use]
pub const fn import_name(&self) -> ReExportImportName {
self.import_name
}

/// Gets the public alias of the reexport.
#[must_use]
pub const fn export_name(&self) -> Sym {
self.export_name
}
}
49 changes: 49 additions & 0 deletions boa_ast/src/declaration/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,52 @@ impl VisitWith for ImportSpecifier {
visitor.visit_sym_mut(&mut self.export_name)
}
}

/// The name under which the imported binding is exported by a module.
#[derive(Debug, Clone, Copy)]
pub enum ImportName {
/// The namespace object of the imported module.
Namespace,
/// A binding of the imported module.
Name(Sym),
}

/// [`ImportEntry`][spec] record.
///
/// [spec]: https://tc39.es/ecma262/#table-importentry-record-fields
#[derive(Debug, Clone, Copy)]
pub struct ImportEntry {
module_request: Sym,
import_name: ImportName,
local_name: Identifier,
}

impl ImportEntry {
/// Creates a new `ImportEntry`.
#[must_use]
pub const fn new(module_request: Sym, import_name: ImportName, local_name: Identifier) -> Self {
Self {
module_request,
import_name,
local_name,
}
}

/// Gets the module from where the binding must be imported.
#[must_use]
pub const fn module_request(&self) -> Sym {
self.module_request
}

/// Gets the import name of the imported binding.
#[must_use]
pub const fn import_name(&self) -> ImportName {
self.import_name
}

/// Gets the local name of the imported binding.
#[must_use]
pub const fn local_name(&self) -> Identifier {
self.local_name
}
}
Loading