Skip to content

Commit

Permalink
fix(MeshoptDecoder): rewrite in TypeScript for types export (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodyJasonBennett authored May 25, 2023
1 parent 10a5ddb commit 0ec4ef9
Showing 1 changed file with 38 additions and 6 deletions.
44 changes: 38 additions & 6 deletions src/libs/MeshoptDecoder.js → src/libs/MeshoptDecoder.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
// This file is part of meshoptimizer library and is distributed under the terms of MIT License.
// Copyright (C) 2016-2020, by Arseny Kapoulkine ([email protected])

let generated
/* eslint-disable @typescript-eslint/explicit-function-return-type */

type API = {
ready: Promise<void>
supported: boolean
decodeVertexBuffer: (target: Uint8Array, count: number, size: number, source: Uint8Array, filter?: string) => void
decodeIndexBuffer: (target: Uint8Array, count: number, size: number, source: Uint8Array) => void
decodeIndexSequence: (target: Uint8Array, count: number, size: number, source: Uint8Array) => void
decodeGltfBuffer: (
target: Uint8Array,
count: number,
size: number,
source: Uint8Array,
mode: string,
filter?: string,
) => void
}

let generated: API

const MeshoptDecoder = () => {
if (generated) return generated
Expand Down Expand Up @@ -145,14 +163,14 @@ const MeshoptDecoder = () => {
wasm = wasm_simd
}

let instance
let instance: any // WebAssembly.Instance

const promise = WebAssembly.instantiate(unpack(wasm), {}).then((result) => {
instance = result.instance
instance.exports.__wasm_call_ctors()
})

function unpack(data) {
function unpack(data: string) {
const result = new Uint8Array(data.length)
for (let i = 0; i < data.length; ++i) {
const ch = data.charCodeAt(i)
Expand All @@ -165,7 +183,14 @@ const MeshoptDecoder = () => {
return result.buffer.slice(0, write)
}

function decode(fun, target, count, size, source, filter) {
function decode(
fun: Function,
target: Uint8Array,
count: number,
size: number,
source: Uint8Array,
filter?: Function,
) {
const sbrk = instance.exports.sbrk
const count4 = (count + 3) & ~3 // pad for SIMD filter
const tp = sbrk(count4 * size)
Expand Down Expand Up @@ -217,7 +242,7 @@ const MeshoptDecoder = () => {
count,
size,
source,
instance.exports[filters[filter]],
instance.exports[filters[filter as keyof typeof filters]],
)
},
decodeIndexBuffer(target, count, size, source) {
Expand All @@ -227,7 +252,14 @@ const MeshoptDecoder = () => {
decode(instance.exports.meshopt_decodeIndexSequence, target, count, size, source)
},
decodeGltfBuffer(target, count, size, source, mode, filter) {
decode(instance.exports[decoders[mode]], target, count, size, source, instance.exports[filters[filter]])
decode(
instance.exports[decoders[mode as keyof typeof decoders]],
target,
count,
size,
source,
instance.exports[filters[filter as keyof typeof filters]],
)
},
}

Expand Down

1 comment on commit 0ec4ef9

@vercel
Copy link

@vercel vercel bot commented on 0ec4ef9 May 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.