@@ -4,6 +4,7 @@ import { createStore, StateCreator, StoreApi } from "zustand";
44
55// FIXME cyclic import
66import { RepoSlice } from "./store" ;
7+ import { rewriteCode } from "./parser" ;
78
89function getChildExports ( { id, pods } ) {
910 // get all the exports and reexports. The return would be:
@@ -288,6 +289,58 @@ function getExports(content) {
288289 return { exports, reexports, content } ;
289290}
290291
292+ function doRun ( { id, socket, set, get } ) {
293+ // 1. rewrite the code
294+ let pods = get ( ) . pods ;
295+ let pod = pods [ id ] ;
296+ let code = pod . content ;
297+ // get symbol tables
298+ //
299+ // TODO currently, I'm using only the symbol table from the current pod. I'll
300+ // need to get the symbol table from all the children as well.
301+
302+ // I should get symbol table of:
303+ // - all sibling nodes
304+ console . log ( "rewriting code .." ) ;
305+ // console.log("my id", id, pod.symbolTable);
306+ // console.log("=== children", pods[pod.parent].children);
307+ // FIXME what if there are conflicts?
308+ let allSymbolTables = pods [ pod . parent ] . children . map ( ( { id, type } ) => {
309+ // FIXME make this consistent, CODE, POD, DECK, SCOPE; use enums
310+ if ( pods [ id ] . type === "CODE" ) {
311+ return pods [ id ] . symbolTable || { } ;
312+ } else {
313+ let tables = pods [ id ] . children
314+ . filter ( ( { id } ) => pods [ id ] . ispublic )
315+ . map ( ( { id } ) => pods [ id ] . symbolTable || { } ) ;
316+ return Object . assign ( { } , ...tables ) ;
317+ }
318+ } ) ;
319+ // console.log("=== allSymbolTables", allSymbolTables);
320+ let combinedSymbolTable = Object . assign ( { } , ...allSymbolTables ) ;
321+ // console.log("=== combinedSymbolTable", combinedSymbolTable);
322+ let { ispublic, newcode } = rewriteCode ( code , combinedSymbolTable ) ;
323+ get ( ) . setPodVisibility ( id , ispublic ) ;
324+ console . log ( "new code:\n" , newcode ) ;
325+
326+ get ( ) . setRunning ( pod . id ) ;
327+ socket . send (
328+ JSON . stringify ( {
329+ type : "runCode" ,
330+ payload : {
331+ lang : pod . lang ,
332+ code : newcode ,
333+ namespace : pod . ns ,
334+ raw : true ,
335+ podId : pod . id ,
336+ sessionId : get ( ) . sessionId ,
337+ } ,
338+ } )
339+ ) ;
340+
341+ // 2. send for evaluation
342+ }
343+
291344function handleRunTree ( { id, socket, set, get } ) {
292345 // get all pods
293346 function helper ( id ) {
@@ -540,6 +593,7 @@ export interface RuntimeSlice {
540593 clearResults : ( id ) => void ;
541594 clearAllResults : ( ) => void ;
542595 setRunning : ( id ) => void ;
596+ setSymbolTable : ( id : string , names : string [ ] ) => void ;
543597 addPodExport : ( id , exports , reexports ) => void ;
544598 clearAllExports : ( ) => void ;
545599 setPodExport : ( { id, exports, reexports } ) => void ;
@@ -657,7 +711,7 @@ export const createRuntimeSlice: StateCreator<
657711 } ) ;
658712 return ;
659713 }
660- handleRunTree ( {
714+ doRun ( {
661715 id,
662716 socket : {
663717 send : ( payload ) => {
@@ -670,6 +724,7 @@ export const createRuntimeSlice: StateCreator<
670724 } ) ;
671725 } ,
672726 wsPowerRun : ( { id, doEval } ) => {
727+ throw Error ( "Depcrecated" ) ;
673728 if ( ! get ( ) . socket ) {
674729 get ( ) . addError ( {
675730 type : "error" ,
@@ -721,6 +776,19 @@ export const createRuntimeSlice: StateCreator<
721776 } ,
722777 // ==========
723778 // exports
779+ setSymbolTable : ( id : string , names : string [ ] ) => {
780+ set (
781+ produce ( ( state ) => {
782+ // a symbol table is foo->foo_<podid>
783+ state . pods [ id ] . symbolTable = Object . assign (
784+ { } ,
785+ ...names . map ( ( name ) => ( {
786+ [ name ] : `${ name } _${ id } ` ,
787+ } ) )
788+ ) ;
789+ } )
790+ ) ;
791+ } ,
724792 addPodExport : ( { id, name } ) => {
725793 set (
726794 produce ( ( state ) => {
0 commit comments