raylib-wasm-transpiler/tools/js-class-c-struct-transpiler/transpile.js

284 lines
7.0 KiB
JavaScript
Raw Normal View History

2024-05-27 18:12:34 -04:00
import { parseArgs } from "util";
let types = {
u8: {
c: "unsigned char",
2024-05-27 18:12:34 -04:00
js: "Uint8",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 1,
},
i8: {
c: "char",
2024-05-27 18:12:34 -04:00
js: "Int8",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 1,
},
u16: {
c: "unsigned short",
2024-05-27 18:12:34 -04:00
js: "Uint16",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 2,
},
i16: {
c: "short",
2024-05-27 18:12:34 -04:00
js: "Int16",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 2,
},
u32: {
c: "unisgned int",
2024-05-27 18:12:34 -04:00
js: "Uint32",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 4,
},
i32: {
c: "int",
2024-05-27 18:12:34 -04:00
js: "Int32",
2024-05-27 20:19:37 -04:00
sql: "INTEGER",
2024-05-27 18:12:34 -04:00
size: 4,
},
2024-05-27 20:19:37 -04:00
i64: {
c: "long",
js: "BigInt64",
sql: "INTEGER"
},
u64: {
c: "unsigned long",
js: "BigUint64",
sql: "INTEGER"
},
2024-05-27 18:12:34 -04:00
f32: {
c: "float",
js: "Float32",
2024-05-27 20:19:37 -04:00
sql: "REAL",
2024-05-27 18:12:34 -04:00
size: 4,
},
f64: {
c: "double",
js: "Float64",
2024-05-27 20:19:37 -04:00
sql: "REAL",
2024-05-27 18:12:34 -04:00
size: 8,
},
};
const { values } = parseArgs({
args: Bun.argv,
options: {
schema: {
type: "string",
short: "S",
},
2024-05-27 20:19:37 -04:00
javascript_out: {
2024-05-27 19:13:13 -04:00
type: "string",
short: "j",
},
2024-05-27 20:19:37 -04:00
c_out: {
2024-05-27 19:13:13 -04:00
type: "string",
short: "c",
},
2024-05-27 20:19:37 -04:00
sqlite_out: {
type: "string",
short: "s"
}
2024-05-27 18:12:34 -04:00
},
strict: true,
allowPositionals: true,
});
2024-06-12 23:05:45 -04:00
function jsStructConstructor(size, containsString, type, args) {
2024-05-27 18:12:34 -04:00
return `
/**
2024-06-12 23:05:45 -04:00
* Constructs a new ${type}
*
* @param ${args} init The arguments to construct the object.
2024-06-13 01:08:44 -04:00
* @param {Uint8Array} ptr The pointer to the C struct.
*/
2024-05-27 18:12:34 -04:00
constructor(init = {}, ptr = undefined) {
this._size = ${size};
2024-06-13 01:08:44 -04:00
this._ptr = ptr || new Uint8Array(this._size);
this._data = new DataView(this._ptr.buffer);
${
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 = "";
2024-05-27 20:19:37 -04:00
let sqlData = "";
2024-05-27 18:12:34 -04:00
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 = "";
2024-05-27 20:19:37 -04:00
let foreignKeys = "";
2024-06-12 23:05:45 -04:00
const props = schema[type].members;
let typeDef = `/**\n * @typedef {Object} ${type} ${schema[type].comment}\n`;
let args = `{{`;
2024-05-27 22:56:25 -04:00
sqlData += `CREATE TABLE ${type} (id INTEGER PRIMARY KEY AUTOINCREMENT`;
2024-05-27 18:12:34 -04:00
cData += `typedef struct ${type} {`;
jsData += `class ${type} {`;
for (const prop of Object.keys(props)) {
const propType = props[prop].type;
const kind = props[prop].kind;
const comment = props[prop].comment;
2024-05-27 18:12:34 -04:00
let typeSize = parseInt(types[propType]?.size);
switch (kind) {
2024-06-13 01:08:44 -04:00
case "logical":
typeSize = 1;
jsData += `
/**
* ${comment}
* @return {boolean} gets the value of ${prop}
*/
get ${prop}() {
return Boolean(this._data.getInt8(${parseInt(offset)}, true));
}
/**
* ${comment}
* @param {boolean} sets the value of ${prop}
*/
set ${prop}(v) {
return this._data.setInt8(${parseInt(
offset
)}, v ? 1 : 0, true);
}`;
args += `${prop}: boolean, `;
typeDef += ` * @property {boolean} ${propType} ${comment}\n`;
sqlData += `, ${prop} INTEGER`;
cData += `
char ${prop}; // ${comment}`;
break;
2024-05-27 18:12:34 -04:00
case "string":
containsString = true;
typeSize = props[prop].size;
const iSize = parseInt(offset) + parseInt(typeSize);
jsData += `
2024-06-12 23:05:45 -04:00
/**
* ${comment}
2024-06-12 23:05:45 -04:00
* @return {string} gets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
get ${prop}() {
return this._decoder.decode(new Uint8Array(this._ptr.slice(${parseInt(
offset
)}, ${iSize})));
}
2024-06-12 23:05:45 -04:00
/**
* ${comment}
* @param {string} v sets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
set ${prop}(v) {
2024-06-13 01:08:44 -04:00
if (v.length > ${parseInt(typeSize)}) {
throw new Error("input is larger than buffer size of ${parseInt(typeSize)}");
}
const tmp = new Uint8Array(new ArrayBuffer(${parseInt(typeSize)}));
tmp.set(this._encoder.encode(v))
this._ptr.set(tmp.buffer, ${parseInt(offset)});
2024-05-27 18:28:27 -04:00
}`;
2024-05-27 21:21:21 -04:00
sqlData += `, ${prop} TEXT`;
2024-05-27 18:12:34 -04:00
cData += `
char ${prop}[${iSize}]; // ${comment}`;
2024-06-12 23:05:45 -04:00
args += `${prop}: string, `;
typeDef += ` * @property {string} ${prop} ${comment}\n`;
2024-05-27 18:12:34 -04:00
break;
case "scalar":
typeSize = types[propType].size;
jsData += `
2024-06-12 23:05:45 -04:00
/**
* ${comment}
2024-06-12 23:05:45 -04:00
* @return {${types[propType].js}} gets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
get ${prop}() {
return this._data.get${types[propType].js}(${parseInt(offset)}, true);
}
2024-06-12 23:05:45 -04:00
/**
* ${comment}
* @param {${types[propType].js}} sets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
set ${prop}(v) {
return this._data.set${types[propType].js}(${parseInt(
offset
)}, v, true);
}`;
2024-06-12 23:05:45 -04:00
args += `${prop}: ${types[propType].js}, `;
2024-06-13 01:08:44 -04:00
typeDef += ` * @property {${types[propType].js}} ${propType} ${comment}\n`;
2024-05-27 20:19:37 -04:00
sqlData += `, ${prop} ${types[propType].sql}`;
2024-05-27 18:12:34 -04:00
cData += `
${types[propType].c} ${prop}; // ${comment}`;
2024-05-27 18:12:34 -04:00
break;
case "struct":
const jsSize = parseInt(offset) + parseInt(types[propType].size);
jsData += `
2024-06-12 23:05:45 -04:00
/**
* ${comment}
2024-06-12 23:05:45 -04:00
* @return {${types[propType].js}} gets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
get ${prop}() {
return new ${propType}({}, new Uint8Array(this._ptr.slice(${offset}, ${jsSize})));
}
2024-06-12 23:05:45 -04:00
/**
* ${comment}
* @param {${types[propType].js}} sets the value of ${prop}
*/
2024-05-27 18:12:34 -04:00
set ${prop}(v) {
2024-06-13 01:08:44 -04:00
this._ptr.set(v._ptr, ${offset});
}`;
const importS = `import ${propType} from "./${propType}"\n`;
2024-05-27 19:13:13 -04:00
if (!importStatements.includes(importS)) {
importStatements += importS;
}
2024-05-27 22:56:25 -04:00
const localKey = `${prop.toLowerCase()}_id`;
2024-06-12 23:05:45 -04:00
args += `${prop}: ${types[propType].js}, `;
2024-06-13 01:08:44 -04:00
typeDef += ` * @property {${types[propType].js}} ${propType} ${comment}\n`;
2024-05-27 20:29:39 -04:00
sqlData += `, ${localKey} INTEGER`;
2024-05-27 22:56:25 -04:00
foreignKeys += `\n, FOREIGN KEY(${localKey}) REFERENCES ${propType}(id)`
cData += `\n\t\t${types[propType].c} ${prop}; // ${comment}`;
2024-05-27 18:12:34 -04:00
break;
case "array":
throw new Error("Not Implemented!");
2024-05-27 18:12:34 -04:00
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),
};
2024-06-12 23:05:45 -04:00
typeDef += " */\n"
args += `}}`
jsData = typeDef + jsData;
jsData += jsStructConstructor(size, containsString, type, args);
2024-05-27 20:19:37 -04:00
jsData += `\n}\n\nexport default ${type}`;
cData += `\n} ${type};\n\n`;
2024-05-27 20:29:39 -04:00
sqlData += `${foreignKeys});\n\n`;
2024-05-27 18:12:34 -04:00
2024-05-27 19:13:13 -04:00
await Bun.write(
2024-05-27 20:19:37 -04:00
Bun.file(values.javascript_out + type + ".js"),
2024-05-27 19:13:13 -04:00
importStatements + jsData
);
2024-05-27 18:12:34 -04:00
}
2024-05-27 20:19:37 -04:00
await Bun.write(Bun.file(values.c_out + "types.h"), cData);
await Bun.write(Bun.file(values.sqlite_out + "types.sql"), sqlData);