refactor boolean, bugfix for string
This commit is contained in:
parent
f4bde93974
commit
60a550834e
|
@ -16,7 +16,12 @@ class Login {
|
||||||
* @param {string} v sets the value of Email
|
* @param {string} v sets the value of Email
|
||||||
*/
|
*/
|
||||||
set Email(v) {
|
set Email(v) {
|
||||||
this._data.set(this._encoder.encode(v), 0);
|
if (v.length > 256) {
|
||||||
|
throw new Error("input is larger than buffer size of 256");
|
||||||
|
}
|
||||||
|
const tmp = new Uint8Array(new ArrayBuffer(256));
|
||||||
|
tmp.set(this._encoder.encode(v))
|
||||||
|
this._ptr.set(tmp.buffer, 0);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Password of the user
|
* Password of the user
|
||||||
|
@ -30,25 +35,23 @@ class Login {
|
||||||
* @param {string} v sets the value of Password
|
* @param {string} v sets the value of Password
|
||||||
*/
|
*/
|
||||||
set Password(v) {
|
set Password(v) {
|
||||||
this._data.set(this._encoder.encode(v), 256);
|
if (v.length > 64) {
|
||||||
|
throw new Error("input is larger than buffer size of 64");
|
||||||
}
|
}
|
||||||
/**
|
const tmp = new Uint8Array(new ArrayBuffer(64));
|
||||||
* Get the struct representation of the object
|
tmp.set(this._encoder.encode(v))
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
this._ptr.set(tmp.buffer, 256);
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Constructs a new Login
|
* Constructs a new Login
|
||||||
*
|
*
|
||||||
* @param {{Email: string, Password: string, }} init The arguments to construct the object.
|
* @param {{Email: string, Password: string, }} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = 320;
|
this._size = 320;
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
|
|
||||||
this._encoder = new TextEncoder();
|
this._encoder = new TextEncoder();
|
||||||
this._decoder = new TextDecoder();
|
this._decoder = new TextDecoder();
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import Login from "./Login"
|
import Login from "./Login"
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} LoginRequest a request for a login
|
* @typedef {Object} LoginRequest a request for a login
|
||||||
* @property Login Login user login info
|
* @property {Login} Login user login info
|
||||||
*/
|
*/
|
||||||
class LoginRequest {
|
class LoginRequest {
|
||||||
/**
|
/**
|
||||||
|
@ -16,25 +16,18 @@ class LoginRequest {
|
||||||
* @param {Login} sets the value of Login
|
* @param {Login} sets the value of Login
|
||||||
*/
|
*/
|
||||||
set Login(v) {
|
set Login(v) {
|
||||||
this._data.set(v.bytes(), 0);
|
this._ptr.set(v._ptr, 0);
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Get the struct representation of the object
|
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Constructs a new LoginRequest
|
* Constructs a new LoginRequest
|
||||||
*
|
*
|
||||||
* @param {{Login: Login, }} init The arguments to construct the object.
|
* @param {{Login: Login, }} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = 320;
|
this._size = 320;
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
|
|
||||||
for (const key of Object.keys(init)) {
|
for (const key of Object.keys(init)) {
|
||||||
this[key] = init[key];
|
this[key] = init[key];
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} LoginResponse the response from a login request
|
* @typedef {Object} LoginResponse the response from a login request
|
||||||
* @property boolean logical login was successful or not
|
* @property {boolean} logical login was successful or not
|
||||||
*/
|
*/
|
||||||
class LoginResponse {
|
class LoginResponse {
|
||||||
/**
|
/**
|
||||||
|
@ -8,32 +8,25 @@ class LoginResponse {
|
||||||
* @return {boolean} gets the value of success
|
* @return {boolean} gets the value of success
|
||||||
*/
|
*/
|
||||||
get success() {
|
get success() {
|
||||||
return this._data.getboolean(0, true);
|
return Boolean(this._data.getInt8(0, true));
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* login was successful or not
|
* login was successful or not
|
||||||
* @param {boolean} sets the value of success
|
* @param {boolean} sets the value of success
|
||||||
*/
|
*/
|
||||||
set success(v) {
|
set success(v) {
|
||||||
return this._data.setboolean(0, v, true);
|
return this._data.setInt8(0, v ? 1 : 0, true);
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Get the struct representation of the object
|
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Constructs a new LoginResponse
|
* Constructs a new LoginResponse
|
||||||
*
|
*
|
||||||
* @param {{success: boolean, }} init The arguments to construct the object.
|
* @param {{success: boolean, }} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = 1;
|
this._size = 1;
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
|
|
||||||
for (const key of Object.keys(init)) {
|
for (const key of Object.keys(init)) {
|
||||||
this[key] = init[key];
|
this[key] = init[key];
|
||||||
|
|
|
@ -3,8 +3,8 @@ import Vector3 from "./Vector3"
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} User The user object
|
* @typedef {Object} User The user object
|
||||||
* @property {string} Name Name of the user
|
* @property {string} Name Name of the user
|
||||||
* @property Login Login user login info
|
* @property {Login} Login user login info
|
||||||
* @property Vector3 Vector3 last logout position of user
|
* @property {Vector3} Vector3 last logout position of user
|
||||||
*/
|
*/
|
||||||
class User {
|
class User {
|
||||||
/**
|
/**
|
||||||
|
@ -19,7 +19,12 @@ class User {
|
||||||
* @param {string} v sets the value of Name
|
* @param {string} v sets the value of Name
|
||||||
*/
|
*/
|
||||||
set Name(v) {
|
set Name(v) {
|
||||||
this._data.set(this._encoder.encode(v), 0);
|
if (v.length > 24) {
|
||||||
|
throw new Error("input is larger than buffer size of 24");
|
||||||
|
}
|
||||||
|
const tmp = new Uint8Array(new ArrayBuffer(24));
|
||||||
|
tmp.set(this._encoder.encode(v))
|
||||||
|
this._ptr.set(tmp.buffer, 0);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* user login info
|
* user login info
|
||||||
|
@ -33,7 +38,7 @@ class User {
|
||||||
* @param {Login} sets the value of Login
|
* @param {Login} sets the value of Login
|
||||||
*/
|
*/
|
||||||
set Login(v) {
|
set Login(v) {
|
||||||
this._data.set(v.bytes(), 24);
|
this._ptr.set(v._ptr, 24);
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* last logout position of user
|
* last logout position of user
|
||||||
|
@ -47,25 +52,18 @@ class User {
|
||||||
* @param {Vector3} sets the value of Position
|
* @param {Vector3} sets the value of Position
|
||||||
*/
|
*/
|
||||||
set Position(v) {
|
set Position(v) {
|
||||||
this._data.set(v.bytes(), 344);
|
this._ptr.set(v._ptr, 344);
|
||||||
}
|
|
||||||
/**
|
|
||||||
* Get the struct representation of the object
|
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* Constructs a new User
|
* Constructs a new User
|
||||||
*
|
*
|
||||||
* @param {{Name: string, Login: Login, Position: Vector3, }} init The arguments to construct the object.
|
* @param {{Name: string, Login: Login, Position: Vector3, }} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = 356;
|
this._size = 356;
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
|
|
||||||
this._encoder = new TextEncoder();
|
this._encoder = new TextEncoder();
|
||||||
this._decoder = new TextDecoder();
|
this._decoder = new TextDecoder();
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
/**
|
/**
|
||||||
* @typedef {Object} Vector3 A representation of a position in 3D space
|
* @typedef {Object} Vector3 A representation of a position in 3D space
|
||||||
* @property Float32 f32 x coordinate
|
* @property {Float32} f32 x coordinate
|
||||||
* @property Float32 f32 y coordinate
|
* @property {Float32} f32 y coordinate
|
||||||
* @property Float32 f32 z coordinate
|
* @property {Float32} f32 z coordinate
|
||||||
*/
|
*/
|
||||||
class Vector3 {
|
class Vector3 {
|
||||||
/**
|
/**
|
||||||
|
@ -47,23 +47,16 @@ class Vector3 {
|
||||||
set z(v) {
|
set z(v) {
|
||||||
return this._data.setFloat32(8, v, true);
|
return this._data.setFloat32(8, v, true);
|
||||||
}
|
}
|
||||||
/**
|
|
||||||
* Get the struct representation of the object
|
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new Vector3
|
* Constructs a new Vector3
|
||||||
*
|
*
|
||||||
* @param {{x: Float32, y: Float32, z: Float32, }} init The arguments to construct the object.
|
* @param {{x: Float32, y: Float32, z: Float32, }} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = 12;
|
this._size = 12;
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
|
|
||||||
for (const key of Object.keys(init)) {
|
for (const key of Object.keys(init)) {
|
||||||
this[key] = init[key];
|
this[key] = init[key];
|
||||||
|
|
|
@ -72,7 +72,7 @@
|
||||||
"members": {
|
"members": {
|
||||||
"success": {
|
"success": {
|
||||||
"type": "logical",
|
"type": "logical",
|
||||||
"kind": "scalar",
|
"kind": "logical",
|
||||||
"comment": "login was successful or not"
|
"comment": "login was successful or not"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
import LoginRequest from "./build/LoginRequest"
|
import LoginRequest from "./build/LoginRequest";
|
||||||
import LoginResponse from "./build/LoginResponse"
|
import LoginResponse from "./build/LoginResponse";
|
||||||
|
import User from "./build/User";
|
||||||
|
import Vector3 from "./build/Vector3";
|
||||||
|
|
||||||
var decoder = new TextDecoder("utf-8");
|
var decoder = new TextDecoder("utf-8");
|
||||||
var port = 8089;
|
var port = 8089;
|
||||||
|
@ -18,8 +20,15 @@ wss.on("connection", function (ws) {
|
||||||
console.log(entity.Login.Email);
|
console.log(entity.Login.Email);
|
||||||
console.log(entity.Login.Password);
|
console.log(entity.Login.Password);
|
||||||
|
|
||||||
|
const user = new User({
|
||||||
|
Name: "Test User",
|
||||||
|
Login: entity.Login,
|
||||||
|
Position: new Vector3({ x: 0, y: 0, z: 0 }),
|
||||||
|
});
|
||||||
|
console.log(user.Name);
|
||||||
|
|
||||||
const response = new LoginResponse({ success: true });
|
const response = new LoginResponse({ success: true });
|
||||||
ws.send(response.bytes, { binary: true }); // Echo back the received message
|
ws.send(response._ptr, { binary: true }); // Echo back the received message
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
import { parseArgs } from "util";
|
import { parseArgs } from "util";
|
||||||
|
|
||||||
let types = {
|
let types = {
|
||||||
logical: {
|
|
||||||
c: "char",
|
|
||||||
js: "boolean",
|
|
||||||
sql: "INTEGER",
|
|
||||||
size: 1,
|
|
||||||
},
|
|
||||||
u8: {
|
u8: {
|
||||||
c: "unsigned char",
|
c: "unsigned char",
|
||||||
js: "Uint8",
|
js: "Uint8",
|
||||||
|
@ -93,23 +87,16 @@ const { values } = parseArgs({
|
||||||
|
|
||||||
function jsStructConstructor(size, containsString, type, args) {
|
function jsStructConstructor(size, containsString, type, args) {
|
||||||
return `
|
return `
|
||||||
/**
|
|
||||||
* Get the struct representation of the object
|
|
||||||
* @return {Uint8Array} u8 array of the C struct.
|
|
||||||
*/
|
|
||||||
get bytes() {
|
|
||||||
return new Uint8Array(this._ptr);
|
|
||||||
}
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new ${type}
|
* Constructs a new ${type}
|
||||||
*
|
*
|
||||||
* @param ${args} init The arguments to construct the object.
|
* @param ${args} init The arguments to construct the object.
|
||||||
* @param {ArrayBuffer} ptr The pointer to the C struct.
|
* @param {Uint8Array} ptr The pointer to the C struct.
|
||||||
*/
|
*/
|
||||||
constructor(init = {}, ptr = undefined) {
|
constructor(init = {}, ptr = undefined) {
|
||||||
this._size = ${size};
|
this._size = ${size};
|
||||||
this._ptr = ptr?.buffer || new ArrayBuffer(this._size);
|
this._ptr = ptr || new Uint8Array(this._size);
|
||||||
this._data = new DataView(this._ptr);
|
this._data = new DataView(this._ptr.buffer);
|
||||||
${
|
${
|
||||||
containsString
|
containsString
|
||||||
? `
|
? `
|
||||||
|
@ -148,6 +135,31 @@ for (const type of Object.keys(schema)) {
|
||||||
let typeSize = parseInt(types[propType]?.size);
|
let typeSize = parseInt(types[propType]?.size);
|
||||||
|
|
||||||
switch (kind) {
|
switch (kind) {
|
||||||
|
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;
|
||||||
case "string":
|
case "string":
|
||||||
containsString = true;
|
containsString = true;
|
||||||
typeSize = props[prop].size;
|
typeSize = props[prop].size;
|
||||||
|
@ -167,7 +179,12 @@ for (const type of Object.keys(schema)) {
|
||||||
* @param {string} v sets the value of ${prop}
|
* @param {string} v sets the value of ${prop}
|
||||||
*/
|
*/
|
||||||
set ${prop}(v) {
|
set ${prop}(v) {
|
||||||
this._data.set(this._encoder.encode(v), ${parseInt(offset)});
|
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)});
|
||||||
}`;
|
}`;
|
||||||
sqlData += `, ${prop} TEXT`;
|
sqlData += `, ${prop} TEXT`;
|
||||||
cData += `
|
cData += `
|
||||||
|
@ -195,7 +212,7 @@ for (const type of Object.keys(schema)) {
|
||||||
)}, v, true);
|
)}, v, true);
|
||||||
}`;
|
}`;
|
||||||
args += `${prop}: ${types[propType].js}, `;
|
args += `${prop}: ${types[propType].js}, `;
|
||||||
typeDef += ` * @property ${types[propType].js} ${propType} ${comment}\n`;
|
typeDef += ` * @property {${types[propType].js}} ${propType} ${comment}\n`;
|
||||||
sqlData += `, ${prop} ${types[propType].sql}`;
|
sqlData += `, ${prop} ${types[propType].sql}`;
|
||||||
cData += `
|
cData += `
|
||||||
${types[propType].c} ${prop}; // ${comment}`;
|
${types[propType].c} ${prop}; // ${comment}`;
|
||||||
|
@ -216,7 +233,7 @@ for (const type of Object.keys(schema)) {
|
||||||
* @param {${types[propType].js}} sets the value of ${prop}
|
* @param {${types[propType].js}} sets the value of ${prop}
|
||||||
*/
|
*/
|
||||||
set ${prop}(v) {
|
set ${prop}(v) {
|
||||||
this._data.set(v.bytes(), ${offset});
|
this._ptr.set(v._ptr, ${offset});
|
||||||
}`;
|
}`;
|
||||||
const importS = `import ${propType} from "./${propType}"\n`;
|
const importS = `import ${propType} from "./${propType}"\n`;
|
||||||
if (!importStatements.includes(importS)) {
|
if (!importStatements.includes(importS)) {
|
||||||
|
@ -224,7 +241,7 @@ for (const type of Object.keys(schema)) {
|
||||||
}
|
}
|
||||||
const localKey = `${prop.toLowerCase()}_id`;
|
const localKey = `${prop.toLowerCase()}_id`;
|
||||||
args += `${prop}: ${types[propType].js}, `;
|
args += `${prop}: ${types[propType].js}, `;
|
||||||
typeDef += ` * @property ${types[propType].js} ${propType} ${comment}\n`;
|
typeDef += ` * @property {${types[propType].js}} ${propType} ${comment}\n`;
|
||||||
sqlData += `, ${localKey} INTEGER`;
|
sqlData += `, ${localKey} INTEGER`;
|
||||||
foreignKeys += `\n, FOREIGN KEY(${localKey}) REFERENCES ${propType}(id)`
|
foreignKeys += `\n, FOREIGN KEY(${localKey}) REFERENCES ${propType}(id)`
|
||||||
cData += `\n\t\t${types[propType].c} ${prop}; // ${comment}`;
|
cData += `\n\t\t${types[propType].c} ${prop}; // ${comment}`;
|
||||||
|
|
Loading…
Reference in New Issue