Skip to content

Commit

Permalink
add to webplayground
Browse files Browse the repository at this point in the history
  • Loading branch information
on-keyday committed Mar 18, 2024
1 parent f1877b6 commit 59269ac
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
7 changes: 7 additions & 0 deletions web/dev/src/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,11 @@ const handleTypeScript = async (ui :UIModel, s :JobResult) => {
isJavascript?"javascript" : "typescript",tsOption);
}

const handleKaitaiStruct = async (ui :UIModel, s :JobResult) => {
const option :CallOption= {};
return handleLanguage(ui,s,caller.getKaitaiStructCode,Language.KAITAI_STRUCT,"yaml",option);
}

const handleJSONOutput = async (ui :UIModel,id :TraceID,value :string,generator:(id :TraceID,srcCode :string,option:any)=>Promise<JobResult>) => {
const s = await generator(id,value,
{filename: "editor.bgn"}).catch((e) => {
Expand Down Expand Up @@ -235,5 +240,7 @@ export const updateGenerated = async (ui :UIModel) => {
return handleRust(ui,s);
case Language.TYPESCRIPT:
return handleTypeScript(ui,s);
case Language.KAITAI_STRUCT:
return handleKaitaiStruct(ui,s);
}
}
9 changes: 9 additions & 0 deletions web/dev/src/s2j/caller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const workerMap = Object.freeze({
[WorkerType.JSON2C]:()=> new Worker(new URL("./worker/json2c_worker.js",import.meta.url),{type:"module"}),
[WorkerType.JSON2RUST]:()=> new Worker(new URL("./worker/json2rust_worker.js",import.meta.url),{type:"module"}),
[WorkerType.JSON2TS]:()=> new Worker(new URL("./worker/json2ts_worker.js",import.meta.url),{type:"module"}),
[WorkerType.JSON2KAITAI]:()=> new Worker(new URL("./worker/json2kaitai_worker.js",import.meta.url),{type:"module"}),
});


Expand Down Expand Up @@ -154,6 +155,10 @@ const argConverter = Object.freeze({
}
return args;
},
[RequestLanguage.KAITAI_STRUCT] : (opt :CallOption) => {
const args :string[] = [];
return args;
}
})


Expand Down Expand Up @@ -207,3 +212,7 @@ export const getRustCode = (id :TraceID,sourceCode :string,options :RustOption)
export const getTSCode = (id :TraceID,sourceCode :string,options :TSOption) => {
return getLanguage(id,sourceCode,RequestLanguage.TYPESCRIPT,options)
}

export const getKaitaiStructCode = (id :TraceID,sourceCode :string,options :CallOption) => {
return getLanguage(id,sourceCode,RequestLanguage.KAITAI_STRUCT,options)
}
14 changes: 10 additions & 4 deletions web/dev/src/s2j/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ export const enum RequestLanguage {
GO = "go",
C = "c",
RUST = "rust",
TYPESCRIPT="typescript"
TYPESCRIPT="typescript",
KAITAI_STRUCT = "kaitai struct",
}

export const LanguageList = [
Expand All @@ -21,7 +22,8 @@ export const LanguageList = [
RequestLanguage.GO,
RequestLanguage.C,
RequestLanguage.RUST,
RequestLanguage.TYPESCRIPT
RequestLanguage.TYPESCRIPT,
RequestLanguage.KAITAI_STRUCT
];

export const enum WorkerType {
Expand All @@ -31,7 +33,8 @@ export const enum WorkerType {
JSON2GO,
JSON2C,
JSON2RUST,
JSON2TS
JSON2TS,
JSON2KAITAI,
}

export const WorkerList = Object.freeze([
Expand All @@ -41,7 +44,8 @@ export const WorkerList = Object.freeze([
WorkerType.JSON2GO,
WorkerType.JSON2C,
WorkerType.JSON2RUST,
WorkerType.JSON2TS
WorkerType.JSON2TS,
WorkerType.JSON2KAITAI,
]);


Expand Down Expand Up @@ -83,6 +87,7 @@ export type LanguageToOptionType = {
[RequestLanguage.C]:COption
[RequestLanguage.RUST]:RustOption
[RequestLanguage.TYPESCRIPT]:TSOption
[RequestLanguage.KAITAI_STRUCT]:CallOption
}

export const LanguageToWorkerType = Object.freeze({
Expand All @@ -95,6 +100,7 @@ export const LanguageToWorkerType = Object.freeze({
[RequestLanguage.C]: WorkerType.JSON2C,
[RequestLanguage.RUST]: WorkerType.JSON2RUST,
[RequestLanguage.TYPESCRIPT]: WorkerType.JSON2TS,
[RequestLanguage.KAITAI_STRUCT]: WorkerType.JSON2KAITAI,
})


Expand Down
25 changes: 25 additions & 0 deletions web/dev/src/s2j/worker/json2kaitai_worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/// <reference types="emscripten" />

export { };

import { JobRequest, RequestLanguage } from "../msg.js";
import { GoWorkContext} from "../go_work_ctx.js";




const requestCallback = (e:JobRequest) => {
switch (e.lang) {
case RequestLanguage.KAITAI_STRUCT:
return e.sourceCode;
default:
return new Error("unknown message type");
}
}


const j2go_ctx = new GoWorkContext(
fetch(new URL("../../lib/json2kaitai.wasm",import.meta.url)).then((r) => r.arrayBuffer()),
requestCallback, () => {
console.log("json2go worker is ready");
});
1 change: 1 addition & 0 deletions web/dev/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ copyWasm("json2cpp2.wasm");
copyWasm("json2go.wasm");
copyWasm("json2c.wasm");
copyWasm("json2ts.wasm");
copyWasm("json2kaitai.wasm");

let mode = "development";
if (process.env.WEB_PRODUCTION === "production") {
Expand Down

0 comments on commit 59269ac

Please sign in to comment.