| 1 |
// cat src/mupdf.c | sed '/#include/d' | cpp | node gen-wasm-type.js |
| 2 |
|
| 3 |
"use strict" |
| 4 |
|
| 5 |
import fs from "fs" |
| 6 |
|
| 7 |
console.log(`export default libmupdf_wasm |
| 8 |
declare function libmupdf_wasm(Module: any): Promise<Libmupdf> |
| 9 |
declare const _brand: unique symbol |
| 10 |
export type Pointer<B> = number & { readonly [_brand]: B } |
| 11 |
export { libmupdf_wasm } |
| 12 |
interface Libmupdf { |
| 13 |
UTF8ToString(ptr: Pointer<"char">): string, |
| 14 |
stringToUTF8(str: string, outPtr: Pointer<"char">, maxBytesToWrite: number): number, |
| 15 |
lengthBytesUTF8(str: string): number, |
| 16 |
HEAP8: Int8Array, |
| 17 |
HEAP16: Int16Array, |
| 18 |
HEAPU8: Uint8Array, |
| 19 |
HEAPU16: Uint16Array, |
| 20 |
HEAP32: Int32Array, |
| 21 |
HEAPU32: Uint32Array, |
| 22 |
HEAPF32: Float32Array, |
| 23 |
HEAPF64: Float64Array, |
| 24 |
`) |
| 25 |
|
| 26 |
const TYPE = { |
| 27 |
size_t: "number", |
| 28 |
int: "number", |
| 29 |
float: "number", |
| 30 |
double: "number", |
| 31 |
fz_document: "any_document", |
| 32 |
pdf_document: "any_document", |
| 33 |
fz_page: "any_page", |
| 34 |
pdf_page: "any_page", |
| 35 |
} |
| 36 |
|
| 37 |
const PTR_TYPE = { |
| 38 |
size_t: "int", |
| 39 |
int: "int", |
| 40 |
float: "float", |
| 41 |
double: "double", |
| 42 |
fz_document: "any_document", |
| 43 |
pdf_document: "any_document", |
| 44 |
fz_page: "any_page", |
| 45 |
pdf_page: "any_page", |
| 46 |
} |
| 47 |
|
| 48 |
let lines = fs.readFileSync(0, "utf-8").split("\n") |
| 49 |
let dump = false |
| 50 |
for (let line of lines) { |
| 51 |
if (dump) { |
| 52 |
line.split(";").map(parse_function_signature) |
| 53 |
dump = false |
| 54 |
} |
| 55 |
else if (line === "EMSCRIPTEN_KEEPALIVE") |
| 56 |
dump = true |
| 57 |
else if (line.startsWith("EMSCRIPTEN_KEEPALIVE")) { |
| 58 |
line.split("EMSCRIPTEN_KEEPALIVE").map(chunk => chunk.replace(/{.*}/, "").replaceAll(";", "").trim()).map(parse_function_signature) |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
function map_ptr_type(raw) { |
| 63 |
return "Pointer<\"" + (PTR_TYPE[raw] || raw) + "\">" |
| 64 |
} |
| 65 |
|
| 66 |
function map_type(raw) { |
| 67 |
return TYPE[raw] || raw |
| 68 |
} |
| 69 |
|
| 70 |
function parse_type_name(str) { |
| 71 |
let is_ptr = str.includes("*") |
| 72 |
let list = str.replaceAll("*", "").split(/ +/g) |
| 73 |
let name = list.pop() |
| 74 |
let type |
| 75 |
if (is_ptr) |
| 76 |
type = map_ptr_type(list.join(" ")) |
| 77 |
else |
| 78 |
type = map_type(list.join(" ")) |
| 79 |
return [ type, name ] |
| 80 |
} |
| 81 |
|
| 82 |
function parse_function_signature(line) { |
| 83 |
if (!line.includes("(")) |
| 84 |
return |
| 85 |
|
| 86 |
line = line.replaceAll("const ", " ") |
| 87 |
line = line.replaceAll("unsigned ", " ") |
| 88 |
line = line.replaceAll("signed ", " ") |
| 89 |
line = line.trim() |
| 90 |
|
| 91 |
let [ret, args] = line.split("(") |
| 92 |
args = args.replace(")", "").split(",").map(x => x.trim()) |
| 93 |
|
| 94 |
let [ ret_type, ret_name ] = parse_type_name(ret) |
| 95 |
|
| 96 |
let outarg = [] |
| 97 |
for (let arg of args) { |
| 98 |
if (arg !== "void") { |
| 99 |
let [ arg_type, arg_name ] = parse_type_name(arg) |
| 100 |
outarg.push(arg_name + ": " + arg_type) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
console.log(`\t_${ret_name}(${outarg.join(", ")}): ${ret_type},`) |
| 105 |
} |
| 106 |
|
| 107 |
console.log("}") |