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

Use private constructors in type definitions of structs without an exported constructor #4282

Merged
merged 5 commits into from
Nov 28, 2024
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,9 @@
* Fixed imports for functions using `this` and late binding.
[#4225](https://github.com/rustwasm/wasm-bindgen/pull/4225)

* Don't expose non-functioning implicit constructors to classes when none are provided.
[#4282](https://github.com/rustwasm/wasm-bindgen/pull/4282)

--------------------------------------------------------------------------------

## [0.2.95](https://github.com/rustwasm/wasm-bindgen/compare/0.2.94...0.2.95)
Expand Down
21 changes: 13 additions & 8 deletions crates/cli-support/src/js/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,19 @@ __wbg_set_wasm(wasm);"
let mut dst = format!("class {} {{\n", name);
let mut ts_dst = format!("export {}", dst);

if self.config.debug && !class.has_constructor {
dst.push_str(
"
constructor() {
throw new Error('cannot invoke `new` directly');
}
",
);
if !class.has_constructor {
// declare the constructor as private to prevent direct instantiation
ts_dst.push_str(" private constructor();\n");

if self.config.debug {
dst.push_str(
"
constructor() {
throw new Error('cannot invoke `new` directly');
}
",
);
}
}

if class.wrap_needed {
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/builder.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export class ClassBuilder {
private constructor();
free(): void;
static builder(): ClassBuilder;
}
1 change: 1 addition & 0 deletions crates/cli/tests/reference/echo.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ export function echo_option_vec_string(a?: (string)[]): (string)[] | undefined;
export function echo_option_struct(a?: Foo): Foo | undefined;
export function echo_option_vec_struct(a?: (Foo)[]): (Foo)[] | undefined;
export class Foo {
private constructor();
free(): void;
}
1 change: 1 addition & 0 deletions crates/cli/tests/reference/getter-setter.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/* tslint:disable */
/* eslint-disable */
export class Foo {
private constructor();
free(): void;
x: number;
y?: number;
Expand Down
1 change: 1 addition & 0 deletions crates/cli/tests/reference/raw.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
/* eslint-disable */
export function test1(test: number): number;
export class Test {
private constructor();
free(): void;
static test1(test: number): Test;
test2(test: number): void;
Expand Down
8 changes: 8 additions & 0 deletions crates/typescript-tests/src/custom_section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ const _: &str = TS_INTERFACE_EXPORT2;
pub struct Person {
pub height: u32,
}

#[wasm_bindgen]
impl Person {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self { height: 170 }
}
}
30 changes: 30 additions & 0 deletions crates/typescript-tests/src/getters_setters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ pub struct ColorWithGetters {

#[wasm_bindgen]
impl ColorWithGetters {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
r: 0.0,
_g: 0.0,
_b: 0.0,
_a: 0,
}
}

#[wasm_bindgen(getter)]
pub fn r(&self) -> f64 {
self.r
Expand All @@ -31,6 +41,16 @@ pub struct ColorWithSetters {

#[wasm_bindgen]
impl ColorWithSetters {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
r: 0.0,
_g: 0.0,
_b: 0.0,
a: 0,
}
}

#[wasm_bindgen(setter)]
pub fn set_r(&mut self, r: f64) {
self.r = r;
Expand All @@ -57,6 +77,16 @@ pub struct ColorWithGetterAndSetter {

#[wasm_bindgen]
impl ColorWithGetterAndSetter {
#[wasm_bindgen(constructor)]
pub fn new() -> Self {
Self {
r: 0.0,
_g: 0.0,
_b: 0.0,
a: 0,
}
}

#[wasm_bindgen(getter)]
pub fn r(&self) -> f64 {
self.r
Expand Down