Skip to content

Commit

Permalink
Use private constructors in type definitions of structs without an ex…
Browse files Browse the repository at this point in the history
…ported constructor (#4282)
  • Loading branch information
RunDevelopment authored Nov 28, 2024
1 parent 9e78347 commit 2fbee99
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 8 deletions.
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

0 comments on commit 2fbee99

Please sign in to comment.