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 [[HostDefined]] for Module and Script #3381

Merged
merged 2 commits into from
Oct 14, 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
16 changes: 12 additions & 4 deletions boa_engine/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ use crate::{
realm::Realm,
Context, JsError, JsResult, JsString, JsValue,
};
use crate::{js_string, JsNativeError, NativeFunction};
use crate::{js_string, HostDefined, JsNativeError, NativeFunction};

/// The referrer from which a load request of a module originates.
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -276,7 +276,6 @@ impl std::fmt::Debug for Module {
.field("environment", &self.inner.environment)
.field("namespace", &self.inner.namespace)
.field("kind", &self.inner.kind)
.field("host_defined", &self.inner.host_defined)
.finish()
}
}
Expand All @@ -287,7 +286,7 @@ struct Inner {
environment: GcRefCell<Option<Gc<DeclarativeEnvironment>>>,
namespace: GcRefCell<Option<JsObject>>,
kind: ModuleKind,
host_defined: (),
host_defined: HostDefined,
}

/// The kind of a [`Module`].
Expand Down Expand Up @@ -372,7 +371,7 @@ impl Module {
environment: GcRefCell::default(),
namespace: GcRefCell::default(),
kind: ModuleKind::SourceText(src.clone()),
host_defined: (),
host_defined: HostDefined::default(),
}),
};

Expand All @@ -388,6 +387,15 @@ impl Module {
&self.inner.realm
}

/// Returns the [`ECMAScript specification`][spec] defined [`\[\[HostDefined\]\]`][`HostDefined`] field of the [`Module`].
///
/// [spec]: https://tc39.es/ecma262/#sec-abstract-module-records
#[inline]
#[must_use]
pub fn host_defined(&self) -> &HostDefined {
&self.inner.host_defined
}

/// Gets the kind of this `Module`.
pub(crate) fn kind(&self) -> &ModuleKind {
&self.inner.kind
Expand Down
15 changes: 11 additions & 4 deletions boa_engine/src/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::{
bytecompiler::ByteCompiler,
realm::Realm,
vm::{ActiveRunnable, CallFrame, CodeBlock},
Context, JsResult, JsString, JsValue, Module,
Context, HostDefined, JsResult, JsString, JsValue, Module,
};

/// ECMAScript's [**Script Record**][spec].
Expand All @@ -37,7 +37,6 @@ impl std::fmt::Debug for Script {
.field("realm", &self.inner.realm.addr())
.field("code", &self.inner.source)
.field("loaded_modules", &self.inner.loaded_modules)
.field("host_defined", &self.inner.host_defined)
.finish()
}
}
Expand All @@ -49,7 +48,7 @@ struct Inner {
source: boa_ast::Script,
codeblock: GcRefCell<Option<Gc<CodeBlock>>>,
loaded_modules: GcRefCell<FxHashMap<JsString, Module>>,
host_defined: (),
host_defined: HostDefined,
}

impl Script {
Expand All @@ -59,6 +58,14 @@ impl Script {
&self.inner.realm
}

/// Returns the [`ECMAScript specification`][spec] defined [`\[\[HostDefined\]\]`][`HostDefined`] field of the [`Module`].
///
/// [spec]: https://tc39.es/ecma262/#script-record
#[must_use]
pub fn host_defined(&self) -> &HostDefined {
&self.inner.host_defined
}

/// Gets the loaded modules of this script.
pub(crate) fn loaded_modules(&self) -> &GcRefCell<FxHashMap<JsString, Module>> {
&self.inner.loaded_modules
Expand Down Expand Up @@ -91,7 +98,7 @@ impl Script {
source: code,
codeblock: GcRefCell::default(),
loaded_modules: GcRefCell::default(),
host_defined: (),
host_defined: HostDefined::default(),
}),
})
}
Expand Down