Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:コードチェック機能の実装 #362

Open
wants to merge 15 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion i18n/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"ソースコードを非表示": "Hide source code",
"エラーの詳細を見る": "Click to view error details",
"クリックして閉じる": "Click to close",
"コマンド": "Command"
"コマンド": "Command",
"検証": "Verify"
}
3 changes: 2 additions & 1 deletion i18n/locales/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@
"ソースコードを非表示": "ソースコードを非表示",
"エラーの詳細を見る": "エラーの詳細を見る",
"クリックして閉じる": "クリックして閉じる",
"コマンド": "コマンド"
"コマンド": "コマンド",
"検証": "検証"
}
6 changes: 6 additions & 0 deletions src/hooks/useCrc8.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { useMemo } from "react";
import { crc8Calculator } from "../utils/crc8Calculator";

export const useCrc8 = (data?: Uint8Array) => {
return useMemo(() => crc8Calculator(data), [data]);
};
20 changes: 17 additions & 3 deletions src/libs/mrubyWriterConnector.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { WritableStreamDefaultWriter } from "stream/web";
import { crc8Calculator } from "../utils/crc8Calculator";
import { Failure, Result, Success } from "./result";

export const targets = ["ESP32", "RBoard"] as const;
Expand All @@ -23,7 +24,7 @@ const enterWriteModeKeyword: Record<Target, RegExp> = {
} as const;

const exitWriteModeKeyword: Record<Target, RegExp> = {
ESP32: /mrubyc-esp32: End mrbwrite mode/,
ESP32: /mruby\/c v\d(.\d+)* start/,
Copy link
Member

Choose a reason for hiding this comment

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

意図が分かりづらいので何かしらのコメントが欲しいです。

RBoard: /\+OK Execute mruby\/c\./,
} as const;

Expand Down Expand Up @@ -238,7 +239,7 @@ export class MrubyWriterConnector {
async writeCode(
binary: Uint8Array,
option?: Partial<{ execute: boolean }>
): Promise<Result<null, Error>> {
): Promise<Result<string, Error>> {
if (!this.port) {
return Failure.error("No port.");
}
Expand All @@ -262,7 +263,11 @@ export class MrubyWriterConnector {
await this.sendCommand("execute");
}

return Success.value(null);
const crc = crc8Calculator(binary);
const crcRes = writeRes.value.split("+OK")[1];
Copy link
Member

Choose a reason for hiding this comment

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

意図が分かりづらいので、"".match(//)を使って正規表現で取得するのがよさそうです。

Suggested change
const crcRes = writeRes.value.split("+OK")[1];
const crcRes = writeRes.value.match(/^\+OK (?<hash>[0-9a-zA-Z]+)$/)?.groups?.hash;

のような感じでしょうか?(テキトーに書いたのでちゃんと検証していません)

if (crc !== undefined && crcRes !== undefined)
this.verify(crc, parseInt(crcRes, 16));
return Success.value(writeRes.value);
Comment on lines +268 to +270
Copy link
Member

Choose a reason for hiding this comment

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

crcResundefinedでないこともそうですが、parseInt(crcRes, 16)NaNでないことも検証するべきでしょう。crcResが常に数値に変換できるとは限らないからです。

}

private async sendData(
Expand Down Expand Up @@ -492,4 +497,13 @@ export class MrubyWriterConnector {

return Success.value(line);
}
async verify(culcHash: number, retHash: number) {
if (culcHash === retHash) {
this.handleText("\r\n Verify Success\r\n");
return true;
} else {
this.handleText("\r\n Verify Failed\r\n");
return false;
}
}
Comment on lines +500 to +508
Copy link
Member

Choose a reason for hiding this comment

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

verifyに持たせる機能が少なすぎると思います。バイナリからハッシュ値を作って、verifyコマンドを送信して結果を受けとって検証する一連の流れを抽象化すべきです。

}
19 changes: 18 additions & 1 deletion src/pages/home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
CheckCircleRounded as CheckCircleRoundedIcon,
Edit as EditIcon,
Flag as FlagIcon,
Plagiarism,
Copy link
Member

Choose a reason for hiding this comment

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

流石にPlagiarism(=盗作)はよくない気がします。Document SearchData Checkはどうでしょうか?

Usb as UsbIcon,
UsbOff as UsbOffIcon,
} from "@mui/icons-material";
Expand Down Expand Up @@ -30,6 +31,7 @@ import { isTarget } from "libs/utility";
import { useTranslation } from "react-i18next";
import ESP32 from "/images/ESP32.png";
import RBoard from "/images/Rboard.png";
import { useCrc8 } from "../hooks/useCrc8";

const targets = [
{
Expand All @@ -51,6 +53,7 @@ const commands = [
"reset",
"help",
"showprog",
"verify",
] as const;

export const Home = () => {
Expand Down Expand Up @@ -98,6 +101,7 @@ export const Home = () => {
}
}, [t, connector]);

const crc8 = useCrc8(code);
//1秒ごとに書き込みモードに入ることを試みる
const tryEntry = useCallback(async () => {
return new Promise<void>((resolve, reject) => {
Expand Down Expand Up @@ -159,8 +163,13 @@ export const Home = () => {
}`
);
}
if (text == "verify" && res.isSuccess() && res.value.includes("+OK")) {
const hash = parseInt(res.value.split("+OK")[1], 16);
if (crc8 !== undefined && hash !== undefined)
connector.verify(crc8, hash);
}
Suzune2741 marked this conversation as resolved.
Show resolved Hide resolved
},
[t, connector]
[t, connector, crc8]
);

const writeCode = useCallback(async () => {
Expand Down Expand Up @@ -453,6 +462,14 @@ export const Home = () => {
compileStatus.status !== "success" || !connector.isWriteMode
}
/>
<ControlButton
label={t("検証")}
icon={<Plagiarism />}
onClick={() => send("verify")}
Suzune2741 marked this conversation as resolved.
Show resolved Hide resolved
disabled={
compileStatus.status !== "success" || !connector.isWriteMode
}
/>
<ControlButton
label={t("実行")}
icon={<FlagIcon />}
Expand Down
16 changes: 16 additions & 0 deletions src/utils/crc8Calculator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export const crc8Calculator = (data?: Uint8Array) => {
Suzune2741 marked this conversation as resolved.
Show resolved Hide resolved
const hash = data?.reduce((crc, byte) => {
const poly = 0x31;
crc ^= byte;
for (let i = 0; i < 8; i++) {
if (crc & 0x80) {
crc = (crc << 1) ^ poly;
} else {
crc = crc << 1;
}
}
crc &= 0xff;
return crc;
}, 0xff);
return hash;
};
Loading