Skip to content

Commit

Permalink
Implement draft of module execution
Browse files Browse the repository at this point in the history
  • Loading branch information
jedel1043 committed May 9, 2023
1 parent 8926dba commit 7e1fce4
Show file tree
Hide file tree
Showing 31 changed files with 3,350 additions and 250 deletions.
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
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 `OrdinaryExportEntry`.
#[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 `ReExportEntry`.
#[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

0 comments on commit 7e1fce4

Please sign in to comment.