2024-05-27 18:12:34 -04:00
|
|
|
import { parseArgs } from "util";
|
|
|
|
|
|
|
|
let types = {
|
|
|
|
u8: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "unsigned char",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Uint8",
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
i8: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "char",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Int8",
|
|
|
|
size: 1,
|
|
|
|
},
|
|
|
|
u16: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "unsigned short",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Uint16",
|
|
|
|
size: 2,
|
|
|
|
},
|
|
|
|
i16: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "short",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Int16",
|
|
|
|
size: 2,
|
|
|
|
},
|
|
|
|
u32: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "unisgned int",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Uint32",
|
|
|
|
size: 4,
|
|
|
|
},
|
|
|
|
i32: {
|
2024-05-27 18:26:44 -04:00
|
|
|
c: "int",
|
2024-05-27 18:12:34 -04:00
|
|
|
js: "Int32",
|
|
|
|
size: 4,
|
|
|
|
},
|
|
|
|
f32: {
|
|
|
|
c: "float",
|
|
|
|
js: "Float32",
|
|
|
|
size: 4,
|
|
|
|
},
|
|
|
|
f64: {
|
|
|
|
c: "double",
|
|
|
|
js: "Float64",
|
|
|
|
size: 8,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
const { values } = parseArgs({
|
|
|
|
args: Bun.argv,
|
|
|
|
options: {
|
|
|
|
schema: {
|
|
|
|
type: "string",
|
|
|
|
short: "S",
|
|
|
|
},
|
2024-05-27 18:26:44 -04:00
|
|
|
jsout: {
|
2024-05-27 19:13:13 -04:00
|
|
|
type: "string",
|
|
|
|
short: "j",
|
2024-05-27 18:26:44 -04:00
|
|
|
},
|
|
|
|
cout: {
|
2024-05-27 19:13:13 -04:00
|
|
|
type: "string",
|
|
|
|
short: "c",
|
2024-05-27 18:26:44 -04:00
|
|
|
},
|
2024-05-27 18:12:34 -04:00
|
|
|
},
|
|
|
|
strict: true,
|
|
|
|
allowPositionals: true,
|
|
|
|
});
|
|
|
|
|
|
|
|
function jsStructConstructor(size, containsString) {
|
|
|
|
return `
|
|
|
|
get bytes() {
|
|
|
|
return new Uint8Array(this._ptr);
|
|
|
|
}
|
|
|
|
constructor(init = {}, ptr = undefined) {
|
2024-05-27 18:26:44 -04:00
|
|
|
this._size = ${size};
|
|
|
|
this._ptr = ptr.buffer || new ArrayBuffer(this._size);
|
|
|
|
this._data = new DataView(this._ptr);
|
|
|
|
${
|
|
|
|
containsString
|
|
|
|
? `
|
|
|
|
this._encoder = new TextEncoder();
|
|
|
|
this._decoder = new TextDecoder();`
|
|
|
|
: ""
|
|
|
|
}
|
|
|
|
for (const key of Object.keys(init)) {
|
|
|
|
this[key] = init[key];
|
|
|
|
}
|
2024-05-27 18:12:34 -04:00
|
|
|
}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
const sFile = Bun.file(values.schema);
|
|
|
|
const schema = await sFile.json();
|
|
|
|
let cData = "";
|
|
|
|
|
|
|
|
for (const type of Object.keys(schema)) {
|
|
|
|
let containsString = false;
|
|
|
|
let offset = 0;
|
|
|
|
let size = 0;
|
2024-05-27 19:08:52 -04:00
|
|
|
let importStatements = "";
|
2024-05-27 18:12:34 -04:00
|
|
|
let jsData = "";
|
|
|
|
const props = schema[type];
|
|
|
|
cData += `typedef struct ${type} {`;
|
|
|
|
jsData += `class ${type} {`;
|
|
|
|
for (const prop of Object.keys(props)) {
|
|
|
|
const propType = props[prop].type;
|
|
|
|
const kind = props[prop].kind;
|
|
|
|
let typeSize = parseInt(types[propType]?.size);
|
|
|
|
|
|
|
|
switch (kind) {
|
|
|
|
case "string":
|
|
|
|
containsString = true;
|
|
|
|
typeSize = props[prop].size;
|
|
|
|
const iSize = parseInt(offset) + parseInt(typeSize);
|
|
|
|
jsData += `
|
|
|
|
get ${prop}() {
|
|
|
|
return this._decoder.decode(new Uint8Array(this._ptr.slice(${parseInt(
|
|
|
|
offset
|
|
|
|
)}, ${iSize})));
|
|
|
|
}
|
|
|
|
set ${prop}(v) {
|
|
|
|
this._data.set(this._encoder.encode(v), ${parseInt(offset)});
|
2024-05-27 18:28:27 -04:00
|
|
|
}`;
|
2024-05-27 18:12:34 -04:00
|
|
|
|
|
|
|
cData += `
|
|
|
|
char ${prop}[${iSize}];`;
|
|
|
|
break;
|
|
|
|
case "scalar":
|
|
|
|
typeSize = types[propType].size;
|
|
|
|
jsData += `
|
|
|
|
get ${prop}() {
|
|
|
|
return this._data.get${types[propType].js}(${parseInt(offset)}, true);
|
|
|
|
}
|
|
|
|
set ${prop}(v) {
|
|
|
|
return this._data.set${types[propType].js}(${parseInt(
|
|
|
|
offset
|
|
|
|
)}, v, true);
|
2024-05-27 18:26:44 -04:00
|
|
|
}`;
|
2024-05-27 18:12:34 -04:00
|
|
|
|
|
|
|
cData += `
|
|
|
|
${types[propType].c} ${prop};`;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case "struct":
|
|
|
|
const jsSize = parseInt(offset) + parseInt(types[propType].size);
|
|
|
|
jsData += `
|
|
|
|
get ${prop}() {
|
|
|
|
return new ${propType}({}, new Uint8Array(this._ptr.slice(${offset}, ${jsSize})));
|
|
|
|
}
|
|
|
|
set ${prop}(v) {
|
|
|
|
this._data.set(v.bytes(), ${offset});
|
2024-05-27 18:26:44 -04:00
|
|
|
}`;
|
2024-05-27 19:13:13 -04:00
|
|
|
const importS = `import ${propType} from "./${propType}"\n\n`;
|
|
|
|
if (!importStatements.includes(importS)) {
|
|
|
|
importStatements += importS;
|
|
|
|
}
|
2024-05-27 18:12:34 -04:00
|
|
|
|
2024-05-27 19:08:52 -04:00
|
|
|
cData += `\n\t\t${types[propType].c} ${prop};`;
|
2024-05-27 18:12:34 -04:00
|
|
|
break;
|
|
|
|
case "array":
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += parseInt(typeSize);
|
|
|
|
offset += parseInt(typeSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* add the new type to the list so we can use it for structs later.
|
|
|
|
*/
|
|
|
|
types[type] = {
|
|
|
|
c: type,
|
|
|
|
js: type,
|
|
|
|
size: parseInt(size),
|
|
|
|
};
|
|
|
|
|
|
|
|
jsData += jsStructConstructor(size, containsString);
|
|
|
|
jsData += `
|
2024-05-27 18:54:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ${type}`;
|
2024-05-27 18:12:34 -04:00
|
|
|
cData += `
|
|
|
|
} ${type};
|
|
|
|
|
|
|
|
`;
|
|
|
|
|
2024-05-27 19:13:13 -04:00
|
|
|
await Bun.write(
|
|
|
|
Bun.file(values.jsout + type + ".js"),
|
|
|
|
importStatements + jsData
|
|
|
|
);
|
2024-05-27 18:12:34 -04:00
|
|
|
}
|
2024-05-27 18:26:44 -04:00
|
|
|
await Bun.write(Bun.file(values.cout + "types.h"), cData);
|