Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

34
LINES

< > BotCompany Repo | #1022318 // dm_processEmbeddedSafeEvals

JavaX fragment (include)

sS dm_processEmbeddedSafeEvals(S s, O... _) {
  Map<S> evalCache = cast optPar evalCache(_);
  
  if (!contains(s, "{=")) ret s;
  LS tok = javaTok(s);
  Map<Int> bracketMap = curlyBracketMap(tok);
  for (int i = 1; i < l(tok)-4; i += 2) {
    continue unless eq(tok.get(i), "{") && eqGet(tok, i+1, "") && eqGet(tok, i+2, "=");
    // eval found
    
    int j = or(bracketMap.get(i), l(tok));
    S code = joinSubList(tok, i+4, j-1);
    
    if (!isSafeCodeFragment(code))
      replaceTokens(tok, i+2, i+4, "UNSAFE: ");
    else {
      S result = mapGet(evalCache, code);
      if (result == null) {
        print("Evaluating: " + code);
        try {
          result = strOrEmpty(dm_javaEvalOrInterpret(code));
        } catch e {
          printStackTrace(e);
          result = "";
        }
        mapPut(evalCache, code, result);
      }
      replaceTokens(tok, i, j+1, result);
    }
      
    i = j-2;
  }
  ret trim(join(tok));
}

download  show line numbers  debug dex  old transpilations   

Travelled to 7 computer(s): bhatertpkbcr, cfunsshuasjs, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1022318
Snippet name: dm_processEmbeddedSafeEvals
Eternal ID of this version: #1022318/10
Text MD5: a7cfa43f0029b84b5edd52dbc750fe9c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2019-03-23 21:30:02
Source code size: 978 bytes / 34 lines
Pitched / IR pitched: No / No
Views / Downloads: 286 / 346
Version history: 9 change(s)
Referenced in: [show references]