Skip to content

Commit b6ee787

Browse files
committed
kernel string escapes
1 parent 4360bad commit b6ee787

File tree

2 files changed

+255
-35
lines changed

2 files changed

+255
-35
lines changed

api/kernel.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -924,9 +924,12 @@ export class JuliaKernel extends CodePodKernel {
924924
lang = "julia";
925925
image = "julia_kernel";
926926
mapEval({ code, namespace }) {
927-
return `CODEPOD_EVAL("""${code
927+
return `CODEPOD_EVAL("""
928+
${code
928929
.replaceAll("\\", "\\\\")
929-
.replaceAll("$", "\\$")}""", "${namespace}")`;
930+
.replaceAll('"', '\\"')
931+
.replaceAll("$", "\\$")}
932+
""", "${namespace}")`;
930933
}
931934
mapAddImport({ from, to, name }) {
932935
return `CODEPOD_ADD_IMPORT("${from}", "${to}", "${name}")`;
@@ -1001,10 +1004,10 @@ export class JavascriptKernel extends CodePodKernel {
10011004
if (midports) {
10021005
names = midports.map((name) => `"${name}"`);
10031006
}
1004-
let code1 = `CODEPOD.eval(\`${code.replaceAll(
1005-
"\\",
1006-
"\\\\"
1007-
)}\`, "${namespace}", [${names.join(",")}])`;
1007+
let code1 = `CODEPOD.eval(\`${code
1008+
.replaceAll("\\", "\\\\")
1009+
.replaceAll("`", "\\`")
1010+
.replaceAll('"', '\\"')}\`, "${namespace}", [${names.join(",")}])`;
10081011
return code1;
10091012
}
10101013
mapAddImport({ from, to, name }) {

api/resolvers-pg.js

Lines changed: 246 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,47 @@ async function gitExport({ username, reponame, pods }) {
9999
return true;
100100
}
101101

102+
function normalize_cp_config(d) {
103+
// extract and remove config deck, return the config
104+
let configs = d["ROOT"].children.filter(({ name }) => name === "CP_CONFIG");
105+
106+
if (configs.length == 0) {
107+
// use default config
108+
return {};
109+
}
110+
let res = {};
111+
if (configs.length > 1) {
112+
console.log("WARNING: more than 1 config deck found");
113+
}
114+
// use first config
115+
let { id } = configs[0];
116+
let deck = d[id];
117+
deck.children.forEach(({ id }) => {
118+
let content = d[id].content;
119+
let jobj = JSON.parse(content);
120+
if (jobj && jobj["lang"]) {
121+
res[jobj["lang"]] = jobj;
122+
}
123+
});
124+
// remove from d
125+
configs.forEach(({ id }) => {
126+
let children = d["ROOT"].children;
127+
children.splice(
128+
children.findIndex(({ id: id2 }) => id === id2),
129+
1
130+
);
131+
});
132+
return res;
133+
}
134+
102135
async function exportFS({ username, reponame, pods }) {
103136
let path = `/srv/git/${username}/${reponame}`;
104137
if (fs.existsSync(`${path}/src`)) {
105138
await fs.promises.rm(`${path}/src`, { recursive: true });
106139
}
107140
await fs.promises.mkdir(`${path}/src`);
108141
let d = normalize(pods);
142+
let config = normalize_cp_config(d);
109143

110144
// export
111145
// start from ROOT, do dfs
@@ -120,7 +154,7 @@ async function exportFS({ username, reponame, pods }) {
120154
// console.log("deck", deck);
121155
for (const [name, suffix, gen] of [
122156
["racket", "rkt", gen_racket],
123-
["julia", "jl", gen_default("julia")],
157+
["julia", "jl", gen_julia],
124158
["javascript", "js", gen_default("javascript")],
125159
["python", "py", gen_default("python")],
126160
]) {
@@ -129,14 +163,14 @@ async function exportFS({ username, reponame, pods }) {
129163

130164
// gen with different generators
131165
// DEBUG use the deck's lang
132-
// if (deck.lang === name) {
133-
let content = gen(deck, d);
134-
if (content) {
135-
console.log("writing to", `${dir}/main.${suffix}`);
136-
// console.log(name, "content", content);
137-
await fs.promises.writeFile(`${dir}/main.${suffix}`, content);
166+
if (deck.lang === name) {
167+
let content = gen(deck, d);
168+
if (content) {
169+
console.log("writing to", `${dir}/main.${suffix}`);
170+
// console.log(name, "content", content);
171+
await fs.promises.writeFile(`${dir}/main.${suffix}`, content);
172+
}
138173
}
139-
// }
140174
}
141175
for (const { id } of deck.children.filter(({ type }) => type === "DECK")) {
142176
console.log("DFS on ", id);
@@ -145,29 +179,95 @@ async function exportFS({ username, reponame, pods }) {
145179
}
146180
// let decks = pods.filter((pod) => pod.type === "DECK");
147181
await dfs("ROOT", `${path}/src`);
148-
// export info.rkt
149-
let inforkt = `
182+
183+
if (config.racket) {
184+
// export info.rkt
185+
//
186+
// let defaultconfig = {
187+
// name: "bhdl",
188+
// root: "ROOT",
189+
// deps: ["base", "graph", "rebellion", "uuid"],
190+
// "build-deps": ["rackunit-lib"],
191+
// "pkg-desc": "BHDL: A Programming Language for making PCBs",
192+
// version: "0.1",
193+
// };
194+
config.racket.root = config.racket.root
195+
? config.racket.root.match(/^\/*(.*)/)[1]
196+
: "ROOT";
197+
config.racket.deps = config.racket.deps || [];
198+
config.racket["build-deps"] = config.racket["build-deps"] || [];
199+
200+
let inforkt = `
150201
#lang info
151-
(define collection "bhdl")
152-
(define deps '("base" "graph" "rebellion" "uuid"))
153-
(define build-deps '("rackunit-lib"))
154-
(define pkg-desc "CodePod export")
155-
(define pkg-authors '(codepod))
156-
(define version "0.1")
202+
(define collection "${config.racket.name}")
203+
(define deps '(${config.racket.deps.map((s) => `"${s}"`).join(" ")}))
204+
(define build-deps '(${config.racket["build-deps"]
205+
.map((s) => `"${s}"`)
206+
.join(" ")}))
207+
(define pkg-desc "${config.racket["pkg-desc"]}")
208+
(define pkg-authors '())
209+
(define version "${config.racket.version}")
157210
`;
158-
console.log("writing to", `${path}/info.rkt`);
159-
await fs.promises.writeFile(`${path}/src/info.rkt`, inforkt);
160-
await fs.promises.writeFile(
161-
`${path}/src/main.rkt`,
162-
`#lang racket
163-
(require "ROOT/main.rkt")
164-
(provide (all-from-out "ROOT/main.rkt"))`
165-
);
166-
// write codepod.rkt
167-
await fs.promises.copyFile(
168-
"./kernels/racket/codepod.rkt",
169-
`${path}/src/codepod.rkt`
170-
);
211+
console.log("writing to", `${path}/info.rkt`);
212+
await fs.promises.writeFile(`${path}/src/info.rkt`, inforkt);
213+
await fs.promises.writeFile(
214+
`${path}/src/main.rkt`,
215+
`#lang racket
216+
(require "${config.racket.root || "ROOT"}/main.rkt")
217+
(provide (all-from-out "${config.racket.root || "ROOT"}/main.rkt"))`
218+
);
219+
// write codepod.rkt
220+
await fs.promises.copyFile(
221+
"./kernels/racket/codepod.rkt",
222+
`${path}/src/codepod.rkt`
223+
);
224+
}
225+
if (config.julia) {
226+
// let defaultconfig = {
227+
// lang: "julia",
228+
// root: "ROOT/placer",
229+
// name: "BHDL",
230+
// uuid: "b4cd1eb8-1e24-11e8-3319-93036a3eb9f3",
231+
// pkgs: ["ProgressMeter", "CUDA"],
232+
// version: "0.1.0",
233+
// };
234+
// console.log("Julia config:", config.julia);
235+
config.julia.root = config.julia.root
236+
? config.julia.root.match(/^\/*(.*)/)[1]
237+
: "ROOT";
238+
// console.log(config.julia);
239+
config.julia.pkgs = config.julia.pkgs || [];
240+
// How to insert deps?
241+
// I could probably read from the runtime system?
242+
// await fs.promises.writeFile(
243+
// `${path}/Project.toml`,
244+
// `
245+
// name = "${config.julia.name}"
246+
// uuid = "${config.julia.uuid}"
247+
// version = "${config.julia.version}"
248+
// authors = ["Some One <[email protected]>"]
249+
250+
// [deps]
251+
// ${config.julia.pkgs.join("\n")}
252+
// `
253+
// );
254+
function shortns(ns) {
255+
let arr = ns.split("/");
256+
return arr[arr.length - 1];
257+
}
258+
await fs.promises.writeFile(
259+
`${path}/src/${config.julia.name}.jl`,
260+
`
261+
module ${config.julia.name}
262+
using Reexport
263+
264+
include("${config.julia.root || "ROOT"}/main.jl")
265+
266+
@reexport using .${shortns(config.julia.root)}
267+
end
268+
`
269+
);
270+
}
171271
}
172272

173273
function normalize(pods) {
@@ -194,6 +294,7 @@ function normalize(pods) {
194294
id: pod.id,
195295
type: pod.type,
196296
lang: pod.lang,
297+
name: pod.name,
197298
});
198299
}
199300
// sort
@@ -232,6 +333,122 @@ function gen_default(name) {
232333
};
233334
}
234335

336+
function gen_julia(pod, pods) {
337+
let ids = pod.children
338+
.filter(({ type }) => type !== "DECK")
339+
.filter(({ lang }) => lang === "julia")
340+
.map(({ id }) => id);
341+
342+
let content = ids.map((id) => pods[id].content).join("\n\n");
343+
let level = pod.ns.split("/").length;
344+
let names = pod.children
345+
.filter(({ lang, type }) => type !== "DECK" && lang === "julia")
346+
.filter(({ id }) => pods[id].exports)
347+
.map(({ id }) =>
348+
Object.entries(pods[id].exports)
349+
.filter(([k, v]) => v)
350+
.map(([k, v]) => k)
351+
);
352+
names = [].concat(...names);
353+
let nses = getUtilNs({ id: pod.id, pods });
354+
console.log("utils nses", nses);
355+
// child deck's
356+
// console.log("111");
357+
const child_deck_nses = pod.children
358+
.filter(
359+
({ id }) =>
360+
pods[id].type === "DECK" && !pods[id].thundar && !pods[id].utility
361+
)
362+
.map(({ id, type }) => pods[id].ns);
363+
// console.log("222");
364+
// console.log("child_deck_nses", child_deck_nses);
365+
366+
// FIXME the child might be utils, which will be duplicate with utilsNS
367+
nses = nses.concat(child_deck_nses);
368+
369+
let exported_decks = pod.children
370+
.filter(
371+
({ id }) =>
372+
pods[id].type === "DECK" && pods[id].exports && pods[id].exports["self"]
373+
)
374+
.map(({ id, type }) => pods[id].ns);
375+
376+
function shortns(ns) {
377+
let arr = ns.split("/");
378+
return arr[arr.length - 1];
379+
}
380+
381+
let code = `
382+
module ${shortns(pod.ns)}
383+
384+
using Reexport
385+
386+
${nses
387+
.map(
388+
(ns) => `
389+
include("${"../".repeat(level)}${ns}/main.jl")
390+
`
391+
)
392+
.join("\n")}
393+
394+
${nses
395+
.map(
396+
(ns) => `
397+
using .${shortns(ns)}`
398+
)
399+
.join("\n")}
400+
401+
${exported_decks
402+
.map(
403+
(ns) => `
404+
@reexport using .${shortns(ns)}`
405+
)
406+
.join("\n")}
407+
408+
${names.length > 0 ? `export ${names.join(",")}` : ""}
409+
410+
${content}
411+
412+
end
413+
`;
414+
415+
return code;
416+
417+
let code1 = `
418+
${nses
419+
.map(
420+
(ns) => `
421+
include("${"../".repeat(level)}${ns}/main.jl")
422+
`
423+
)
424+
.join("\n")}
425+
eval(:(module $(Symbol("${pod.ns}"))
426+
using Reexport
427+
428+
${nses
429+
.map(
430+
(ns) => `
431+
eval(:(using $(:Main).$(Symbol("${ns}"))))`
432+
)
433+
.join("\n")}
434+
${exported_decks
435+
.map(
436+
(ns) => `
437+
eval(:(@reexport using $(:Main).$(Symbol("${ns}"))))`
438+
)
439+
.join("\n")}
440+
441+
${names.length > 0 ? `export ${names.join(",")}` : ""}
442+
443+
${content}
444+
445+
end))
446+
447+
448+
`;
449+
return code;
450+
}
451+
235452
function getUtilNs({ id, pods, exclude }) {
236453
// get all utils for id
237454
// get children utils nodes

0 commit comments

Comments
 (0)