Skip to content

Commit

Permalink
Improved typescript bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikSom committed Jan 8, 2024
1 parent 77e8e35 commit 691b89a
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 337 deletions.
3 changes: 3 additions & 0 deletions README.dev.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,6 @@ sh clipper2-wasm/compile-wasm.sh prod

### Completion
After completing these steps, you will find the WASM builds in the `clipper2-wasm/dist` directory.

### Generating typescript files
Append `--embind-emit-tsd ../clipper2z.d.ts` to the ES6 export, manually at the Factory export and the glue-stub functions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ https://github.com/ErikSom/Clipper2-WASM/tree/main/CPP/clipper2-wasm/examples/um

## Typescript example:
```javascript
import { MainModule } from 'clipper2-wasm';
import { Clipper2ZFactoryFunction, MainModule } from 'clipper2-wasm/dist/clipper2z';
import * as _Clipper2ZFactory from 'clipper2-wasm/dist/umd/clipper2z';

const Clipper2ZFactory = _Clipper2ZFactory as any;
const Clipper2ZFactory: Clipper2ZFactoryFunction = _Clipper2ZFactory;

Clipper2ZFactory({
locateFile: () => {
return './clipper2z.wasm'
return 'path/to/your/clipper2z.wasm'
},
}).then((Clipper2Z: MainModule) => {
console.log('Clipper2Z', Clipper2Z);
});
})
```

## Benchmark vs Clipper1 WASM: (Apple M2 Pro)
Expand Down
8 changes: 4 additions & 4 deletions clipper2-wasm/compile-wasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ fi

# build ES6
echo "Building ES6"
em++ $FLAGS -s EXPORT_ES6=1 -s NO_FILESYSTEM=1 -s ENVIRONMENT='web' -s clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z.js -s EXPORT_NAME="Clipper2Z" --post-js clipper2-wasm/glue-stub-z.js --embind-emit-tsd clipper2z.d.ts
em++ $FLAGS -s EXPORT_ES6=1 -s NO_FILESYSTEM=1 -s ENVIRONMENT='web' -s clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z.js -s EXPORT_NAME="Clipper2Z" --post-js clipper2-wasm/glue-stub-z.js

# build Tools ES6
em++ $FLAGS -Iclipper2/CPP/Utils -s EXPORT_ES6=1 -s ENVIRONMENT='web' -s clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z-utils.js -s EXPORT_NAME="Clipper2ZUtils" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js --embind-emit-tsd clipper2z-utils.d.ts
em++ $FLAGS -Iclipper2/CPP/Utils -s EXPORT_ES6=1 -s ENVIRONMENT='web' -s clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/es/clipper2z-utils.js -s EXPORT_NAME="Clipper2ZUtils" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js

# build UMD
echo "Building UMD"
em++ $FLAGS -s NO_FILESYSTEM=1 clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z.js -s ENVIRONMENT='web' -s EXPORT_NAME="Clipper2ZFactory" --post-js clipper2-wasm/glue-stub-z.js --embind-emit-tsd clipper2z.d.ts
em++ $FLAGS -s NO_FILESYSTEM=1 clipper2-wasm/clipper.bindings.cpp clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z.js -s ENVIRONMENT='web' -s EXPORT_NAME="Clipper2ZFactory" --post-js clipper2-wasm/glue-stub-z.js

# build Tools UMD
em++ $FLAGS -Iclipper2/CPP/Utils clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z-utils.js -s ENVIRONMENT='web' -s EXPORT_NAME="Clipper2ZUtilsFactory" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js --embind-emit-tsd clipper2z-utils.d.ts
em++ $FLAGS -Iclipper2/CPP/Utils clipper2-wasm/clipper-tools.bindings.cpp clipper2/CPP/build/libClipper2Zutils.a clipper2/CPP/build/libClipper2Z.a -o clipper2-wasm/dist/umd/clipper2z-utils.js -s ENVIRONMENT='web' -s EXPORT_NAME="Clipper2ZUtilsFactory" -s EXPORTED_FUNCTIONS=[FS] --post-js clipper2-wasm/glue-stub-tools-z.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference types="emscripten" />
export interface Clipper2ZUtilsFactoryFunction extends EmscriptenModuleFactory<MainModule & EmscriptenModule> { }

export interface FillRuleValue<T extends number> {
value: T;
}
Expand Down Expand Up @@ -33,6 +36,22 @@ export interface PathD {
delete(): void;
}

export interface Path64 {
clear(): void;
push_back(_0: Point64): void;
size(): number;
get(_0: number): Point64;
delete(): void;
}

export interface Paths64 {
clear(): void;
push_back(_0: Path64): void;
size(): number;
get(_0: number): Path64;
delete(): void;
}

export interface PathsD {
clear(): void;
push_back(_0: PathD): void;
Expand All @@ -58,4 +77,8 @@ export interface MainModule {
PathsD: {new(): PathsD};
Point64: {new(_0: bigint, _1: bigint, _2: bigint): Point64};
SvgAddSolution(_0: SvgWriter, _1: PathsD, _2: FillRule, _3: boolean): void;
PathDToPath64(pathD: PathD): Path64;
Path64ToPathD(path64: Path64): PathD;
Paths64ToPathsD(paths64: Paths64): PathsD;
PathsDToPaths64(pathsD: PathsD): Paths64;
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/// <reference types="emscripten" />
export interface Clipper2ZFactoryFunction extends EmscriptenModuleFactory<MainModule & EmscriptenModule> { }

export interface ClipperBase {
Clear(): void;
Clear(): void;
Expand All @@ -10,32 +13,32 @@ export interface ClipperBase {
export interface FillRuleValue<T extends number> {
value: T;
}
export type FillRule = FillRuleValue<0>|FillRuleValue<1>|FillRuleValue<2>|FillRuleValue<3>;
export type FillRule = FillRuleValue<0> | FillRuleValue<1> | FillRuleValue<2> | FillRuleValue<3>;

export interface ClipTypeValue<T extends number> {
value: T;
}
export type ClipType = ClipTypeValue<1>|ClipTypeValue<2>|ClipTypeValue<3>|ClipTypeValue<4>;
export type ClipType = ClipTypeValue<1> | ClipTypeValue<2> | ClipTypeValue<3> | ClipTypeValue<4>;

export interface PathTypeValue<T extends number> {
value: T;
}
export type PathType = PathTypeValue<0>|PathTypeValue<1>;
export type PathType = PathTypeValue<0> | PathTypeValue<1>;

export interface JoinTypeValue<T extends number> {
value: T;
}
export type JoinType = JoinTypeValue<0>|JoinTypeValue<2>|JoinTypeValue<3>;
export type JoinType = JoinTypeValue<0> | JoinTypeValue<2> | JoinTypeValue<3>;

export interface EndTypeValue<T extends number> {
value: T;
}
export type EndType = EndTypeValue<0>|EndTypeValue<1>|EndTypeValue<2>|EndTypeValue<3>|EndTypeValue<4>;
export type EndType = EndTypeValue<0> | EndTypeValue<1> | EndTypeValue<2> | EndTypeValue<3> | EndTypeValue<4>;

export interface PointInPolygonResultValue<T extends number> {
value: T;
}
export type PointInPolygonResult = PointInPolygonResultValue<0>|PointInPolygonResultValue<1>|PointInPolygonResultValue<2>;
export type PointInPolygonResult = PointInPolygonResultValue<0> | PointInPolygonResultValue<1> | PointInPolygonResultValue<2>;

export interface Point64 {
x: bigint;
Expand Down Expand Up @@ -167,17 +170,17 @@ export interface ClipperD extends ClipperBase {

export interface MainModule {
ClipperBase: {};
FillRule: {EvenOdd: FillRuleValue<0>, NonZero: FillRuleValue<1>, Positive: FillRuleValue<2>, Negative: FillRuleValue<3>};
ClipType: {Intersection: ClipTypeValue<1>, Union: ClipTypeValue<2>, Difference: ClipTypeValue<3>, Xor: ClipTypeValue<4>};
PathType: {Subject: PathTypeValue<0>, Clip: PathTypeValue<1>};
JoinType: {Square: JoinTypeValue<0>, Round: JoinTypeValue<2>, Miter: JoinTypeValue<3>};
EndType: {Polygon: EndTypeValue<0>, Joined: EndTypeValue<1>, Butt: EndTypeValue<2>, Square: EndTypeValue<3>, Round: EndTypeValue<4>};
PointInPolygonResult: {IsOn: PointInPolygonResultValue<0>, IsInside: PointInPolygonResultValue<1>, IsOutside: PointInPolygonResultValue<2>};
Point64: {new(_0: bigint, _1: bigint, _2: bigint): Point64};
Path64: {new(): Path64};
Paths64: {new(): Paths64};
FillRule: { EvenOdd: FillRuleValue<0>, NonZero: FillRuleValue<1>, Positive: FillRuleValue<2>, Negative: FillRuleValue<3> };
ClipType: { Intersection: ClipTypeValue<1>, Union: ClipTypeValue<2>, Difference: ClipTypeValue<3>, Xor: ClipTypeValue<4> };
PathType: { Subject: PathTypeValue<0>, Clip: PathTypeValue<1> };
JoinType: { Square: JoinTypeValue<0>, Round: JoinTypeValue<2>, Miter: JoinTypeValue<3> };
EndType: { Polygon: EndTypeValue<0>, Joined: EndTypeValue<1>, Butt: EndTypeValue<2>, Square: EndTypeValue<3>, Round: EndTypeValue<4> };
PointInPolygonResult: { IsOn: PointInPolygonResultValue<0>, IsInside: PointInPolygonResultValue<1>, IsOutside: PointInPolygonResultValue<2> };
Point64: { new(_0: bigint, _1: bigint, _2: bigint): Point64 };
Path64: { new(): Path64 };
Paths64: { new(): Paths64 };
PointInPolygon64(_0: Point64, _1: Path64): PointInPolygonResult;
Rect64: {new(): Rect64; new(_0: bigint, _1: bigint, _2: bigint, _3: bigint): Rect64};
Rect64: { new(): Rect64; new(_0: bigint, _1: bigint, _2: bigint, _3: bigint): Rect64 };
RectClipPaths64(_0: Rect64, _1: Paths64): Paths64;
RectClipPath64(_0: Rect64, _1: Path64): Paths64;
RectClipLinesPaths64(_0: Rect64, _1: Paths64): Paths64;
Expand All @@ -188,15 +191,15 @@ export interface MainModule {
UnionSelf64(_0: Paths64, _1: FillRule): Paths64;
Difference64(_0: Paths64, _1: Paths64, _2: FillRule): Paths64;
Xor64(_0: Paths64, _1: Paths64, _2: FillRule): Paths64;
PolyPath64: {new(): PolyPath64};
Clipper64: {new(): Clipper64};
PointD: {new(_0: number, _1: number, _2: number): PointD};
PathD: {new(): PathD};
PathsD: {new(): PathsD};
PolyPath64: { new(): PolyPath64 };
Clipper64: { new(): Clipper64 };
PointD: { new(_0: number, _1: number, _2: number): PointD };
PathD: { new(): PathD };
PathsD: { new(): PathsD };
PointInPolygonD(_0: PointD, _1: PathD): PointInPolygonResult;
RectD: {new(): RectD; new(_0: number, _1: number, _2: number, _3: number): RectD};
PolyPathD: {new(): PolyPathD};
ClipperD: {new(): ClipperD};
RectD: { new(): RectD; new(_0: number, _1: number, _2: number, _3: number): RectD };
PolyPathD: { new(): PolyPathD };
ClipperD: { new(): ClipperD };
ReversePath64(_0: Path64): void;
BooleanOpOut64(_0: ClipType, _1: FillRule, _2: Paths64, _3: Paths64, _4: PolyPath64): void;
ReversePathD(_0: PathD): void;
Expand Down Expand Up @@ -239,4 +242,12 @@ export interface MainModule {
InflatePathsD(_0: PathsD, _1: number, _2: JoinType, _3: EndType, _4: number, _5: number, _6: number): PathsD;
SimplifyPathD(_0: PathD, _1: number, _2: boolean): PathD;
SimplifyPathsD(_0: PathsD, _1: number, _2: boolean): PathsD;
MakePath64(intArray: Array<number | bigint>): Path64;
MakePathZ64(intArray: Array<number | bigint>): Path64;
MakePathD(intArray: number[]): PathD;
MakePathZD(intArray: number[]): PathD;
PathDToPath64(pathD: PathD): Path64;
Path64ToPathD(path64: Path64): PathD;
Paths64ToPathsD(paths64: Paths64): PathsD;
PathsDToPaths64(pathsD: PathsD): Paths64;
}
61 changes: 0 additions & 61 deletions clipper2-wasm/dist/umd/clipper2z-utils.d.ts

This file was deleted.

Loading

0 comments on commit 691b89a

Please sign in to comment.