Skip to content

Commit

Permalink
Adding reserved class names (#623)
Browse files Browse the repository at this point in the history
  • Loading branch information
ambiguousname authored Aug 15, 2024
1 parent df193f1 commit 7318c98
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 10 deletions.
2 changes: 1 addition & 1 deletion feature_tests/js/api/MyStruct.mjs

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

12 changes: 6 additions & 6 deletions feature_tests/js/api/ResultOpaque.mjs

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

14 changes: 12 additions & 2 deletions tool/src/js/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,16 @@ const RESERVED: &[&str] = &[
"true",
"try",
"typeof",
"undefined",
"var",
"void",
"while",
"with",
];

/// From https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
const RESERVED_TYPES: &[&str] = &["Infinity", "NaN"];

/// Helper class for us to format JS identifiers from the HIR.
pub(crate) struct JSFormatter<'tcx> {
/// Per [`CFormatter`]'s documentation we use it for support.
Expand All @@ -60,10 +64,16 @@ impl<'tcx> JSFormatter<'tcx> {
pub fn fmt_type_name(&self, id: TypeId) -> Cow<'tcx, str> {
let type_def = self.tcx.resolve_type(id);

type_def
let name = type_def
.attrs()
.rename
.apply(type_def.name().as_str().into())
.apply(type_def.name().as_str().into());

if RESERVED_TYPES.contains(&&*name) || RESERVED.contains(&&*name) {
panic!("{name} is not an allowed type in JS. Please rename.")
}

name
}

pub fn fmt_file_name_extensionless(&self, type_name: &str) -> String {
Expand Down
2 changes: 1 addition & 1 deletion tool/src/js/type_generation/converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
let cause =
self.gen_c_to_js_for_type(e, receive_deref, &method.lifetime_env);
format!(
"const cause = {cause};\n throw new Error({message}, {{ cause }})",
"const cause = {cause};\n throw new globalThis.Error({message}, {{ cause }})",
message = match e {
Type::Enum(..) => format!("'{type_name}: ' + cause.value"),
Type::Struct(s) if match s.resolve(self.tcx) {
Expand Down

0 comments on commit 7318c98

Please sign in to comment.