sanket3280/code-execution
0
1module.exports = function primitivesWrapper({ sourceCode, language }) {2 let prefix = '';3 switch (language) {4 case 'JAVASCRIPT_NODE':5 prefix = `6// Primitive helpers (JS)7function __toBool(v){if(typeof v==='boolean')return v;const s=String(v).trim().toLowerCase();return s==='true'||s==='1'}8function __toInt(v){const n=Number(v);return Number.isNaN(n)?0:Math.trunc(n)}9function __toFloat(v){const n=Number(v);return Number.isNaN(n)?0:n}10`;11 break;12 case 'PYTHON3':13 case 'PYTHON3_8':14 prefix = `15# Primitive helpers (Python)16def __to_bool(v):17 if isinstance(v, bool):18 return v19 s = str(v).strip().lower()20 return s in ('true','1')21def __to_int(v):22 try:23 return int(float(v))24 except Exception:25 return 026def __to_float(v):27 try:28 return float(v)29 except Exception:30 return 0.031`;32 break;33 case 'JAVA':34 prefix = `35// Primitive helpers (Java)36class __Prim { static boolean toBool(Object v){ if(v instanceof Boolean) return (Boolean)v; String s=String.valueOf(v).trim().toLowerCase(); return s.equals("true")||s.equals("1"); } static int toInt(Object v){ try{return (int)Double.parseDouble(String.valueOf(v));}catch(Exception e){return 0;}} static double toFloat(Object v){ try{return Double.parseDouble(String.valueOf(v));}catch(Exception e){return 0.0;}} }37`;38 break;39 case 'CPP14':40 case 'CPP':41 prefix = `42// Primitive helpers (C++)43namespace __prim { inline bool toBool(const std::string& s){ auto t=s; std::string l; l.resize(t.size()); std::transform(t.begin(), t.end(), l.begin(), ::tolower); return l=="true"||l=="1"; } inline int toInt(const std::string& s){ try{ return (int)std::stod(s);}catch(...){return 0;}} inline double toFloat(const std::string& s){ try{ return std::stod(s);}catch(...){return 0.0;}} }44`;45 break;46 case 'C':47 prefix = `48/* Primitive helpers (C) */49int __to_bool(const char* s){ if(!s) return 0; return strcmp(s,"true")==0 || strcmp(s,"1")==0; }50int __to_int(const char* s){ if(!s) return 0; return (int)atof(s); }51double __to_float(const char* s){ if(!s) return 0.0; return atof(s); }52`;53 break;54 case 'CSHARP':55 prefix = `56// Primitive helpers (C#)57static class __Prim { public static bool ToBool(object v){ if(v is bool b) return b; var s=(v==null?"":v.ToString()).Trim().ToLower(); return s=="true"||s=="1"; } public static int ToInt(object v){ if(v==null) return 0; if(int.TryParse(v.ToString(), out var n)) return n; if(double.TryParse(v.ToString(), out var d)) return (int)d; return 0; } public static double ToFloat(object v){ if(v==null) return 0; if(double.TryParse(v.ToString(), out var d)) return d; return 0; } }58`;59 break;60 default:61 return { sourceCode, usedRunner: false };62 }63 return { sourceCode: prefix + sourceCode, usedRunner: false };64};65 66 67 