PageRenderTime 53ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/src/sys/dotnet/fan/sys/Sys.cs

https://bitbucket.org/bedlaczech/fan-1.0
C# | 1019 lines | 346 code | 70 blank | 603 comment | 26 complexity | 20ba51fc42114fdad7da145bbae5db2f MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. //
  2. // Copyright (c) 2006, Brian Frank and Andy Frank
  3. // Licensed under the Academic Free License version 3.0
  4. //
  5. // History:
  6. // 14 Sep 06 Andy Frank Creation
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Reflection;
  11. using System.IO;
  12. using Fanx.Fcode;
  13. using Fanx.Util;
  14. namespace Fan.Sys
  15. {
  16. ///
  17. /// Sys provides static access to the system's environment.
  18. ///
  19. public sealed class Sys
  20. {
  21. //////////////////////////////////////////////////////////////////////////
  22. // Fields (loaded before type constants)
  23. //////////////////////////////////////////////////////////////////////////
  24. /** Env.os constant */
  25. public static readonly string m_os = initOs();
  26. /** Env.arch constant */
  27. public static readonly string m_arch = initArch();
  28. /** Env.platform constant */
  29. public static readonly string m_platform = m_os + "-" + m_arch;
  30. /** BootEnv.homeDir */
  31. public static readonly string m_homeDir = initHomeDir();
  32. /** {BootEnv.homeDir}/lib/fan/ */
  33. public static readonly string m_podsDir = initPodsDir();
  34. /** {BootEnv.homeDir}/lib/fan/sys.pod */
  35. public static Pod m_sysPod = initSysPod();
  36. //////////////////////////////////////////////////////////////////////////
  37. // Fields (type constants)
  38. //////////////////////////////////////////////////////////////////////////
  39. // the Eve of all types
  40. public static readonly Type ObjType = initType("Obj");
  41. // basic primitives
  42. public static readonly Type NumType = initType("Num");
  43. public static readonly Type EnumType = initType("Enum");
  44. public static readonly Type FacetType = initType("Facet");
  45. public static readonly Type BoolType = initType("Bool");
  46. public static readonly Type DurationType = initType("Duration");
  47. public static readonly Type FuncType = initType("Func");
  48. public static readonly Type IntType = initType("Int");
  49. public static readonly Type DecimalType = initType("Decimal");
  50. public static readonly Type FloatType = initType("Float");
  51. public static readonly Type ListType = initType("List");
  52. public static readonly Type MapType = initType("Map");
  53. public static readonly Type MonthType = initType("Month");
  54. public static readonly Type PodType = initType("Pod");
  55. public static readonly Type RangeType = initType("Range");
  56. public static readonly Type StrType = initType("Str");
  57. public static readonly Type StrBufType = initType("StrBuf");
  58. public static readonly Type TestType = initType("Test");
  59. public static readonly Type DateTimeType = initType("DateTime");
  60. public static readonly Type DateType = initType("Date");
  61. public static readonly Type TimeType = initType("Time");
  62. public static readonly Type TimeZoneType = initType("TimeZone");
  63. public static readonly Type TypeType = initType("Type");
  64. public static readonly Type WeekdayType = initType("Weekday");
  65. public static readonly Type ThisType = initType("This");
  66. public static readonly Type VoidType = initType("Void");
  67. public static readonly Type EnvType = initType("Env");
  68. public static readonly Type BootEnvType = initType("BootEnv");
  69. public static readonly Type JarDistEnvType = initType("JarDistEnv");
  70. // reflection
  71. public static readonly Type SlotType = initType("Slot");
  72. public static readonly Type FieldType = initType("Field");
  73. public static readonly Type MethodType = initType("Method");
  74. public static readonly Type ParamType = initType("Param");
  75. // IO
  76. public static readonly Type CharsetType = initType("Charset");
  77. public static readonly Type EndianType = initType("Endian");
  78. public static readonly Type InStreamType = initType("InStream");
  79. public static readonly Type SysInStreamType = initType("SysInStream");
  80. public static readonly Type OutStreamType = initType("OutStream");
  81. public static readonly Type SysOutStreamType = initType("SysOutStream");
  82. public static readonly Type FileType = initType("File");
  83. public static readonly Type LocalFileType = initType("LocalFile");
  84. public static readonly Type ZipEntryFileType = initType("ZipEntryFile");
  85. public static readonly Type BufType = initType("Buf");
  86. public static readonly Type MemBufType = initType("MemBuf");
  87. public static readonly Type FileBufType = initType("FileBuf");
  88. public static readonly Type MmapBufType = initType("MmapBuf");
  89. public static readonly Type UriType = initType("Uri");
  90. public static readonly Type ZipType = initType("Zip");
  91. // utils
  92. public static readonly Type DependType = initType("Depend");
  93. public static readonly Type LogType = initType("Log");
  94. public static readonly Type LogLevelType = initType("LogLevel");
  95. public static readonly Type LogRecType = initType("LogRec");
  96. public static readonly Type LocaleType = initType("Locale");
  97. public static readonly Type MimeTypeType = initType("MimeType");
  98. public static readonly Type ProcessType = initType("Process");
  99. public static readonly Type RegexType = initType("Regex");
  100. public static readonly Type RegexMatcherType = initType("RegexMatcher");
  101. public static readonly Type ServiceType = initType("Service");
  102. public static readonly Type VersionType = initType("Version");
  103. public static readonly Type UnitType = initType("Unit");
  104. public static readonly Type UnsafeType = initType("Unsafe");
  105. public static readonly Type UuidType = initType("Uuid");
  106. // uri schemes
  107. public static readonly Type UriSchemeType = initType("UriScheme");
  108. public static readonly Type FanSchemeType = initType("FanScheme");
  109. public static readonly Type FileSchemeType = initType("FileScheme");
  110. // facets
  111. public static readonly Type TransientType = initType("Transient");
  112. public static readonly Type SerializableType = initType("Serializable");
  113. public static readonly Type JsType = initType("Js");
  114. public static readonly Type NoDocType = initType("NoDoc");
  115. public static readonly Type DeprecatedType = initType("Deprecated");
  116. public static readonly Type OperatorType = initType("Operator");
  117. public static readonly Type FacetMetaType = initType("FacetMeta");
  118. // exceptions
  119. public static readonly Type ErrType = initType("Err");
  120. public static readonly Type ArgErrType = initType("ArgErr");
  121. public static readonly Type CancelledErrType = initType("CancelledErr");
  122. public static readonly Type CastErrType = initType("CastErr");
  123. public static readonly Type ConstErrType = initType("ConstErr");
  124. public static readonly Type FieldNotSetErrType = initType("FieldNotSetErr");
  125. public static readonly Type IOErrType = initType("IOErr");
  126. public static readonly Type IndexErrType = initType("IndexErr");
  127. public static readonly Type InterruptedErrType = initType("InterruptedErr");
  128. public static readonly Type NameErrType = initType("NameErr");
  129. public static readonly Type NotImmutableErrType = initType("NotImmutableErr");
  130. public static readonly Type NullErrType = initType("NullErr");
  131. public static readonly Type ParseErrType = initType("ParseErr");
  132. public static readonly Type ReadonlyErrType = initType("ReadonlyErr");
  133. public static readonly Type TestErrType = initType("TestErr");
  134. public static readonly Type TimeoutErrType = initType("TimeoutErr");
  135. public static readonly Type UnknownKeyErrType = initType("UnknownKeyErr");
  136. public static readonly Type UnknownPodErrType = initType("UnknownPodErr");
  137. public static readonly Type UnknownServiceErrType = initType("UnknownServiceErr");
  138. public static readonly Type UnknownSlotErrType = initType("UnknownSlotErr");
  139. public static readonly Type UnknownFacetErrType = initType("UnknownFacetErr");
  140. public static readonly Type UnknownTypeErrType = initType("UnknownTypeErr");
  141. public static readonly Type UnresolvedErrType = initType("UnresolvedErr");
  142. public static readonly Type UnsupportedErrType = initType("UnsupportedErr");
  143. // generic parameter types used with generic types List, Map, and Method
  144. static ClassType[] m_genericParamTypes = new ClassType[256];
  145. public static readonly ClassType AType = initGeneric('A');
  146. public static readonly ClassType BType = initGeneric('B');
  147. public static readonly ClassType CType = initGeneric('C');
  148. public static readonly ClassType DType = initGeneric('D');
  149. public static readonly ClassType EType = initGeneric('E');
  150. public static readonly ClassType FType = initGeneric('F');
  151. public static readonly ClassType GType = initGeneric('G');
  152. public static readonly ClassType HType = initGeneric('H');
  153. public static readonly ClassType KType = initGeneric('K');
  154. public static readonly ClassType LType = initGeneric('L');
  155. public static readonly ClassType MType = initGeneric('M');
  156. public static readonly ClassType RType = initGeneric('R');
  157. public static readonly ClassType VType = initGeneric('V');
  158. private static bool dummy1 = initGenericParamTypes();
  159. //////////////////////////////////////////////////////////////////////////
  160. // Fields (loaded after type constants)
  161. //////////////////////////////////////////////////////////////////////////
  162. /** Empty Str:Obj? map */
  163. public static readonly Map m_emptyStrObjMap = initEmptyStrMap(ObjType.toNullable());
  164. /** Empty Str:Str map */
  165. public static readonly Map m_emptyStrStrMap = initEmptyStrMap(StrType);
  166. /** Empty Str:Type map */
  167. public static readonly Map m_emptyStrTypeMap = initEmptyStrMap(TypeType);
  168. /** Bootstrap environment */
  169. public static readonly BootEnv m_bootEnv = new BootEnv();
  170. internal static Env m_curEnv = m_bootEnv;
  171. /** {BootEnv.homeDir}/etc/sys/config.props */
  172. public static readonly Map m_sysConfig = initSysConfig();
  173. /** "fan.debug" env var used to generating debug attributes in bytecode */
  174. public static readonly bool m_debug = sysConfigBool("debug", false);
  175. /** Absolute boot time */
  176. public static readonly DateTime m_bootDateTime = initBootDateTime();
  177. /** Relative boot time */
  178. public static readonly Duration m_bootDuration = initBootDuration();
  179. /** Current environment - do this after sys fully booted */
  180. private static bool dummy2 = initEnv();
  181. //////////////////////////////////////////////////////////////////////////
  182. // Platform Init
  183. //////////////////////////////////////////////////////////////////////////
  184. // TODO: need to query 32-bit versus 64-bit, looks like you need WMI for this
  185. private static string initOs() { return "win32"; }
  186. private static string initArch() { return "x86"; }
  187. //////////////////////////////////////////////////////////////////////////
  188. // Dir Init
  189. //////////////////////////////////////////////////////////////////////////
  190. private static string initHomeDir()
  191. {
  192. try
  193. {
  194. return sysPropToDir("fan.home", "FAN_HOME", null);
  195. }
  196. catch (Exception e)
  197. {
  198. throw initFail("homeDir", e);
  199. }
  200. }
  201. private static string initPodsDir()
  202. {
  203. try
  204. {
  205. return FileUtil.combine(m_homeDir, "lib", "fan");
  206. }
  207. catch (Exception e)
  208. {
  209. throw initFail("podsDir", e);
  210. }
  211. }
  212. private static string sysPropToDir(string propKey, string envKey, string def)
  213. {
  214. // lookup system property
  215. string val = SysProps.getProperty(propKey);
  216. // fallback to environment variable
  217. if (val == null)
  218. val = Environment.GetEnvironmentVariable(envKey);
  219. // fallback to def if provides
  220. if (val == null && def != null)
  221. val = def;
  222. // if still not found then we're toast
  223. if (val == null)
  224. throw new Exception("Missing " + propKey + " system property or " + envKey + " env var");
  225. // check if relative to home directory (fand, fant)
  226. bool checkExists = true;
  227. if (val.StartsWith("$home"))
  228. {
  229. val = FileUtil.combine(m_homeDir, val.Substring(6));
  230. checkExists = false;
  231. }
  232. // check that it is a valid directory
  233. if (checkExists && !Directory.Exists(val))
  234. throw new Exception("Invalid " + propKey + " dir: " + val);
  235. // make sure path gets normalized
  236. return new DirectoryInfo(val).FullName;
  237. }
  238. //////////////////////////////////////////////////////////////////////////
  239. // Init Sys Pod
  240. //////////////////////////////////////////////////////////////////////////
  241. static Pod initSysPod()
  242. {
  243. try
  244. {
  245. return Pod.doFind("sys", true, null);
  246. }
  247. catch (Exception e)
  248. {
  249. throw initFail("sysPod", e);
  250. }
  251. }
  252. //////////////////////////////////////////////////////////////////////////
  253. // Init Types
  254. //////////////////////////////////////////////////////////////////////////
  255. static Type initType(string name)
  256. {
  257. try
  258. {
  259. return m_sysPod.type(name, true);
  260. }
  261. catch (Exception e)
  262. {
  263. throw initFail("type " + name, e);
  264. }
  265. }
  266. private static ClassType initGeneric(int ch)
  267. {
  268. string name = ""+(char)ch;
  269. try
  270. {
  271. return m_genericParamTypes[ch] = new ClassType(m_sysPod, name, 0, null);
  272. }
  273. catch (Exception e)
  274. {
  275. throw initFail("generic " + name, e);
  276. }
  277. }
  278. private static bool initGenericParamTypes()
  279. {
  280. List noMixins = new List(TypeType, 0).ro();
  281. for (int i=0; i<m_genericParamTypes.Length; ++i)
  282. {
  283. ClassType gp = m_genericParamTypes[i];
  284. if (gp == null) continue;
  285. gp.m_base = ObjType;
  286. gp.m_mixins = noMixins;
  287. }
  288. return true;
  289. }
  290. public static Type genericParamType(string name)
  291. {
  292. if (name.Length == 1 && name[0] < m_genericParamTypes.Length)
  293. return m_genericParamTypes[name[0]];
  294. else
  295. return null;
  296. }
  297. //////////////////////////////////////////////////////////////////////////
  298. // Init Env
  299. //////////////////////////////////////////////////////////////////////////
  300. private static bool initEnv()
  301. {
  302. try
  303. {
  304. string var = (string)Env.cur().vars().get("FAN_ENV");
  305. if (var == null) return true;
  306. m_curEnv = (Env)Type.find(var).make();
  307. }
  308. catch (Exception e)
  309. {
  310. initWarn("curEnv", e);
  311. }
  312. return true;
  313. }
  314. //////////////////////////////////////////////////////////////////////////
  315. // Empty Maps
  316. //////////////////////////////////////////////////////////////////////////
  317. private static Map initEmptyStrMap(Type v)
  318. {
  319. try
  320. {
  321. return (Map)new Map(StrType, v).toImmutable();
  322. }
  323. catch (Exception e)
  324. {
  325. throw initFail("emptyStrMap", e);
  326. }
  327. }
  328. //////////////////////////////////////////////////////////////////////////
  329. // Sys Config
  330. //////////////////////////////////////////////////////////////////////////
  331. private static Map initSysConfig()
  332. {
  333. try
  334. {
  335. string path = FileUtil.combine(m_homeDir, "etc", "sys", "config.props");
  336. LocalFile f = new LocalFile(new FileInfo(path));
  337. if (f.exists())
  338. {
  339. try
  340. {
  341. return f.readProps();
  342. }
  343. catch (Exception e)
  344. {
  345. Console.WriteLine("ERROR: Invalid props file: " + f);
  346. Console.WriteLine(" " + e);
  347. }
  348. }
  349. }
  350. catch (Exception e)
  351. {
  352. throw initFail("sysConfig", e);
  353. }
  354. return m_emptyStrStrMap;
  355. }
  356. static string sysConfig(string name)
  357. {
  358. return (string)m_sysConfig.get(name);
  359. }
  360. static bool sysConfigBool(string name, bool def)
  361. {
  362. string val = sysConfig(name);
  363. if (val != null) return val == "true";
  364. return def;
  365. }
  366. //////////////////////////////////////////////////////////////////////////
  367. // Boot Times
  368. //////////////////////////////////////////////////////////////////////////
  369. private static Duration initBootDuration()
  370. {
  371. try
  372. {
  373. return Duration.now();
  374. }
  375. catch (Exception e)
  376. {
  377. throw initFail("bootDuration", e);
  378. }
  379. }
  380. private static DateTime initBootDateTime()
  381. {
  382. try
  383. {
  384. return DateTime.now();
  385. }
  386. catch (Exception e)
  387. {
  388. throw initFail("bootDuration", e);
  389. }
  390. }
  391. //////////////////////////////////////////////////////////////////////////
  392. // Utils
  393. //////////////////////////////////////////////////////////////////////////
  394. private static void initWarn(string field, Exception e)
  395. {
  396. Console.WriteLine("WARN: cannot init Sys." + field);
  397. Err.dumpStack(e);
  398. }
  399. private static Exception initFail(string field, Exception e)
  400. {
  401. Console.WriteLine("ERROR: cannot init Sys." + field);
  402. Err.dumpStack(e);
  403. throw new Exception("Cannot boot fan: " + e.ToString());
  404. }
  405. /**
  406. * Make a thread-safe copy of the specified object.
  407. * If it is immutable, then just return it; otherwise
  408. * we make a serialized copy.
  409. */
  410. public static object safe(object obj)
  411. {
  412. if (obj == null) return null;
  413. if (FanObj.isImmutable(obj)) return obj;
  414. Buf buf = new MemBuf(512);
  415. buf.writeObj(obj);
  416. buf.flip();
  417. return buf.readObj();
  418. }
  419. public static long nanoTime()
  420. {
  421. return System.DateTime.Now.Ticks * DateTime.nsPerTick;
  422. }
  423. public static void dumpStack()
  424. {
  425. System.Console.WriteLine(new System.Diagnostics.StackTrace(true));
  426. }
  427. /** Force sys class to load */
  428. public static void boot() {}
  429. }
  430. }
  431. /*
  432. //////////////////////////////////////////////////////////////////////////
  433. // Constructor
  434. //////////////////////////////////////////////////////////////////////////
  435. private Sys() {}
  436. //////////////////////////////////////////////////////////////////////////
  437. // Environment
  438. //////////////////////////////////////////////////////////////////////////
  439. public static List args() { return m_args.ro(); }
  440. public static Map env() { return m_env; }
  441. internal static Env m_curEnv;
  442. public static Fan.Sys.File homeDir() { return m_homeDir; }
  443. public static string hostName() { return m_hostName; }
  444. public static string userName() { return m_userName; }
  445. public static void exit() { exit(0); }
  446. public static void exit(long status) { System.Environment.Exit((int)status); }
  447. public static InStream @in() { return StdIn; }
  448. public static OutStream @out() { return StdOut; }
  449. public static OutStream err() { return StdErr; }
  450. public static void gc()
  451. {
  452. GC.Collect();
  453. }
  454. public static long idHash(object obj)
  455. {
  456. return System.Runtime.CompilerServices.RuntimeHelpers.GetHashCode(obj);
  457. }
  458. public static long nanoTime()
  459. {
  460. return System.DateTime.Now.Ticks * DateTime.nsPerTick;
  461. }
  462. public static Map diagnostics()
  463. {
  464. Map d = new Map(StrType, ObjType);
  465. // TODO - return empty map for now
  466. return d;
  467. }
  468. public static void dumpStack()
  469. {
  470. System.Console.WriteLine(new System.Diagnostics.StackTrace(true));
  471. }
  472. //////////////////////////////////////////////////////////////////////////
  473. // Platform
  474. //////////////////////////////////////////////////////////////////////////
  475. // TODO: need to query 32-bit versus 64-bit, looks like you need WMI for this
  476. public static string os() { return "win32"; }
  477. public static string arch() { return "x86"; }
  478. public static string platform() { return "win32-x86"; }
  479. //////////////////////////////////////////////////////////////////////////
  480. // Fields
  481. //////////////////////////////////////////////////////////////////////////
  482. private static string m_HomeDir;
  483. private static string m_PodsDir;
  484. public static string HomeDir { get { return m_HomeDir; } }
  485. public static string PodsDir { get { return m_PodsDir; } }
  486. public static readonly Pod SysPod;
  487. public static readonly InStream StdIn;
  488. public static readonly OutStream StdOut;
  489. public static readonly OutStream StdErr;
  490. // are we using the bootstrap stub versions of sys types
  491. public static bool isStub;
  492. // if true then we disable FanClassLoader and all classes
  493. // must be available precompiled into bytecode from the
  494. // system's classloader (or webapp, etc); default is false
  495. public static bool usePrecompiledOnly = false; // TODO
  496. //////////////////////////////////////////////////////////////////////////
  497. // Boot
  498. //////////////////////////////////////////////////////////////////////////
  499. static Sys()
  500. {
  501. try
  502. {
  503. // get fan home directory
  504. // HomeDir = Environment.GetEnvironmentVariable("fan_home");
  505. // if (HomeDir == null)
  506. // throw new Exception("Missing fan_home environment variable.");
  507. // if (!Directory.Exists(HomeDir))
  508. // throw new Exception("Invalid fan_home dir: " + HomeDir);
  509. // map key directories
  510. m_HomeDir = sysPropToDir("fan.home", "fan_home", null);
  511. m_PodsDir = FileUtil.combine(HomeDir, "lib", "fan");
  512. // load sys pod
  513. SysPod = new Pod("sys");
  514. Pod.m_podsByName["sys"] = SysPod;
  515. try
  516. {
  517. SysPod.load(Pod.readFPod("sys"));
  518. isStub = false;
  519. }
  520. catch (Exception e)
  521. {
  522. Console.WriteLine("WARNING: Using stub for 'sys' pod");
  523. Err.dumpStack(e);
  524. Sys.isStub = true;
  525. }
  526. // types
  527. ObjType = builtin("Obj", null);
  528. // basic primitives
  529. NumType = builtin("Num", ObjType);
  530. BoolType = builtin("Bool", ObjType);
  531. CharsetType = builtin("Charset", ObjType);
  532. DurationType = builtin("Duration", ObjType);
  533. FuncType = builtin("Func", ObjType);
  534. IntType = builtin("Int", NumType);
  535. EnumType = builtin("Enum", ObjType);
  536. DecimalType = builtin("Decimal", NumType);
  537. FloatType = builtin("Float", NumType);
  538. ListType = builtin("List", ObjType);
  539. MapType = builtin("Map", ObjType);
  540. MonthType = builtin("Month", EnumType);
  541. PodType = builtin("Pod", ObjType);
  542. RangeType = builtin("Range", ObjType);
  543. StrType = builtin("Str", ObjType);
  544. StrBufType = builtin("StrBuf", ObjType);
  545. TestType = builtin("Test", ObjType);
  546. DateTimeType = builtin("DateTime", ObjType);
  547. DateType = builtin("Date", ObjType);
  548. TimeType = builtin("Time", ObjType);
  549. TimeZoneType = builtin("TimeZone", ObjType);
  550. TypeType = builtin("Type", ObjType);
  551. UriType = builtin("Uri", ObjType);
  552. ThisType = builtin("This", ObjType);
  553. VoidType = builtin("Void", ObjType);
  554. WeekdayType = builtin("Weekday", EnumType);
  555. EnvType = builtin("Env", ObjType);
  556. BootEnvType = builtin("BootEnv", ObjType);
  557. // reflection
  558. SlotType = builtin("Slot", ObjType);
  559. FieldType = builtin("Field", SlotType);
  560. MethodType = builtin("Method", SlotType);
  561. ParamType = builtin("Param", ObjType);
  562. SymbolType = builtin("Symbol", ObjType);
  563. // IO
  564. InStreamType = builtin("InStream", ObjType);
  565. SysInStreamType = builtin("SysInStream", ObjType);
  566. OutStreamType = builtin("OutStream", ObjType);
  567. SysOutStreamType = builtin("SysOutStream", ObjType);
  568. BufType = builtin("Buf", ObjType);
  569. MemBufType = builtin("MemBuf", BufType);
  570. MmapBufType = builtin("MmapBuf", BufType);
  571. FileBufType = builtin("FileBuf", BufType);
  572. FileType = builtin("File", ObjType);
  573. LocalFileType = builtin("LocalFile", FileType);
  574. ZipEntryFileType = builtin("ZipEntryFile", FileType);
  575. ZipType = builtin("Zip", ObjType);
  576. EndianType = builtin("Endian", EnumType);
  577. // utils
  578. DependType = builtin("Depend", ObjType);
  579. LogType = builtin("Log", ObjType);
  580. LogLevelType = builtin("LogLevel", EnumType);
  581. LogRecType = builtin("LogRec", ObjType);
  582. LocaleType = builtin("Locale", ObjType);
  583. MimeTypeType = builtin("MimeType", ObjType);
  584. ProcessType = builtin("Process", ObjType);
  585. RegexType = builtin("Regex", ObjType);
  586. RegexMatcherType = builtin("RegexMatcher", ObjType);
  587. ServiceType = builtin("Service", ObjType);
  588. VersionType = builtin("Version", ObjType);
  589. UnitType = builtin("Unit", ObjType);
  590. UnsafeType = builtin("Unsafe", ObjType);
  591. UuidType = builtin("Uuid", ObjType);
  592. // scheme
  593. UriSchemeType = builtin("UriScheme", ObjType);
  594. FanSchemeType = builtin("FanScheme", UriSchemeType);
  595. FileSchemeType = builtin("FileScheme", UriSchemeType);
  596. // exceptions
  597. ErrType = builtin("Err", ObjType);
  598. ArgErrType = builtin("ArgErr", ErrType);
  599. CancelledErrType = builtin("CancelledErr", ErrType);
  600. CastErrType = builtin("CastErr", ErrType);
  601. ConstErrType = builtin("ConstErr", ErrType);
  602. IndexErrType = builtin("IndexErr", ErrType);
  603. InterruptedErrType = builtin("InterruptedErr", ErrType);
  604. IOErrType = builtin("IOErr", ErrType);
  605. NameErrType = builtin("NameErr", ErrType);
  606. NotImmutableErrType = builtin("NotImmutableErr", ErrType);
  607. NullErrType = builtin("NullErr", ErrType);
  608. ParseErrType = builtin("ParseErr", ErrType);
  609. ReadonlyErrType = builtin("ReadonlyErr", ErrType);
  610. TestErrType = builtin("TestErr", ErrType);
  611. TimeoutErrType = builtin("TimeoutErr", ErrType);
  612. UnknownPodErrType = builtin("UnknownPodErr", ErrType);
  613. UnknownSlotErrType = builtin("UnknownSlotErr", ErrType);
  614. UnknownTypeErrType = builtin("UnknownTypeErr", ErrType);
  615. UnknownServiceErrType = builtin("UnknownServiceErr", ErrType);
  616. UnknownFacetErrType = builtin("UnknownFacetErr", ErrType);
  617. UnresolvedErrType = builtin("UnresolvedErr", ErrType);
  618. UnsupportedErrType = builtin("UnsupportedErr", ErrType);
  619. // generic types
  620. GenericParameterTypes['A'] = AType = new ClassType(SysPod, "A", 0, null); // A-H Params
  621. GenericParameterTypes['B'] = BType = new ClassType(SysPod, "B", 0, null);
  622. GenericParameterTypes['C'] = CType = new ClassType(SysPod, "C", 0, null);
  623. GenericParameterTypes['D'] = DType = new ClassType(SysPod, "D", 0, null);
  624. GenericParameterTypes['E'] = EType = new ClassType(SysPod, "E", 0, null);
  625. GenericParameterTypes['F'] = FType = new ClassType(SysPod, "F", 0, null);
  626. GenericParameterTypes['G'] = GType = new ClassType(SysPod, "G", 0, null);
  627. GenericParameterTypes['H'] = HType = new ClassType(SysPod, "H", 0, null);
  628. GenericParameterTypes['K'] = KType = new ClassType(SysPod, "K", 0, null); // Key
  629. GenericParameterTypes['L'] = LType = new ClassType(SysPod, "L", 0, null); // Parameterized List
  630. GenericParameterTypes['M'] = MType = new ClassType(SysPod, "M", 0, null); // Parameterized Map
  631. GenericParameterTypes['R'] = RType = new ClassType(SysPod, "R", 0, null); // Return
  632. GenericParameterTypes['V'] = VType = new ClassType(SysPod, "V", 0, null); // Value
  633. List noMixins = new List(TypeType, 0).ro();
  634. for (int i=0; i<GenericParameterTypes.Length; i++)
  635. {
  636. ClassType gp = GenericParameterTypes[i];
  637. if (gp == null) continue;
  638. gp.m_base = ObjType;
  639. gp.m_mixins = noMixins;
  640. }
  641. m_args = new List(StrType);
  642. m_homeDir = new LocalFile(new DirectoryInfo(HomeDir));
  643. m_hostName = Environment.MachineName;
  644. m_userName = Environment.UserName;
  645. m_env = new Map(StrType, StrType);
  646. m_env.caseInsensitive(true);
  647. try
  648. {
  649. // predefined
  650. m_env.set("os.name", Environment.OSVersion.Platform.Tostring());
  651. m_env.set("os.version", Environment.OSVersion.Version.Tostring());
  652. // environment variables
  653. IDictionary getenv = Environment.GetEnvironmentVariables();
  654. foreach (DictionaryEntry de in getenv)
  655. {
  656. string key = (string)de.Key;
  657. string val = (string)de.Value;
  658. m_env.set(key, val);
  659. }
  660. // TODO - is there an equiv in C# for Java sys props?
  661. // TODO - is it System.Configuration.ConfigurationSettings?
  662. // Java system properties
  663. it = System.getProperties().keySet().iterator();
  664. while (it.hasNext())
  665. {
  666. string key = (string)it.next();
  667. string val = System.getProperty(key);
  668. env.set(string.make(key), string.make(val));
  669. }
  670. // sys.properties
  671. LocalFile f = new LocalFile(new FileInfo(FileUtil.combine(HomeDir, "lib", "sys.props")));
  672. if (f.exists())
  673. {
  674. try
  675. {
  676. Map props = f.readProps();
  677. m_env.setAll(props);
  678. }
  679. catch (Exception e)
  680. {
  681. System.Console.WriteLine("ERROR: Invalid props file: " + f);
  682. System.Console.WriteLine(" " + e);
  683. }
  684. }
  685. }
  686. catch (Exception e)
  687. {
  688. Err.dumpStack(e);
  689. }
  690. m_env = m_env.ro();
  691. //
  692. // Standard streams
  693. //
  694. StdIn = new SysInStream(Console.OpenStandardInput());
  695. StdOut = new SysOutStream(Console.OpenStandardOutput());
  696. StdErr = new SysOutStream(Console.OpenStandardError());
  697. //
  698. // Touch
  699. //
  700. DateTime.boot();
  701. Duration.boot();
  702. }
  703. catch(Exception e)
  704. {
  705. Console.WriteLine("ERROR: Sys.static");
  706. Err.dumpStack(e);
  707. throw e;
  708. }
  709. }
  710. /// <summary>
  711. /// Map a system property or environment variable to a local directory.
  712. /// </summary>
  713. static string sysPropToDir(string propKey, string envKey, string def)
  714. {
  715. // lookup system property
  716. string val = SysProps.getProperty(propKey);
  717. // fallback to environment variable
  718. if (val == null)
  719. val = Environment.GetEnvironmentVariable(envKey);
  720. // fallback to def if provides
  721. if (val == null && def != null)
  722. val = def;
  723. // if still not found then we're toast
  724. if (val == null)
  725. throw new Exception("Missing " + propKey + " system property or " + envKey + " env var");
  726. // check that val ends in trailing newline
  727. //if (!val.EndsWith("/")) val += "/";
  728. // check if relative to home directory (fand, fant)
  729. bool checkExists = true;
  730. if (val.StartsWith("$home"))
  731. {
  732. val = FileUtil.combine(HomeDir, val.Substring(6));
  733. checkExists = false;
  734. }
  735. // check that it is a valid directory
  736. if (checkExists && !Directory.Exists(val))
  737. throw new Exception("Invalid " + propKey + " dir: " + val);
  738. // make sure path gets normalized
  739. return new DirectoryInfo(val).FullName;
  740. }
  741. //////////////////////////////////////////////////////////////////////////
  742. // Utils
  743. //////////////////////////////////////////////////////////////////////////
  744. /// <summary>
  745. /// Make a thread-safe copy of the specified object.
  746. /// If it is immutable, then just return it; otherwise
  747. /// we make a serialized copy.
  748. /// </summary>
  749. public static object safe(object obj)
  750. {
  751. if (obj == null) return null;
  752. if (FanObj.isImmutable(obj)) return obj;
  753. Buf buf = new MemBuf(512);
  754. buf.m_out.writeObj(obj);
  755. buf.flip();
  756. return buf.m_in.readObj();
  757. }
  758. //////////////////////////////////////////////////////////////////////////
  759. // Built-in Types
  760. //////////////////////////////////////////////////////////////////////////
  761. // the Eve of all types
  762. public static readonly Type ObjType;
  763. // basic primitives
  764. public static readonly Type NumType;
  765. public static readonly Type BoolType;
  766. public static readonly Type CharsetType;
  767. public static readonly Type DurationType;
  768. public static readonly Type FuncType;
  769. public static readonly Type IntType;
  770. public static readonly Type EnumType;
  771. public static readonly Type DecimalType;
  772. public static readonly Type FloatType;
  773. public static readonly Type ListType;
  774. public static readonly Type MapType;
  775. public static readonly Type MonthType;
  776. public static readonly Type PodType;
  777. public static readonly Type RangeType;
  778. public static readonly Type StrType;
  779. public static readonly Type StrBufType;
  780. public static readonly Type TestType;
  781. public static readonly Type DateTimeType;
  782. public static readonly Type TimeZoneType;
  783. public static readonly Type DateType;
  784. public static readonly Type TimeType;
  785. public static readonly Type TypeType;
  786. public static readonly Type UriType;
  787. public static readonly Type ThisType;
  788. public static readonly Type VoidType;
  789. public static readonly Type WeekdayType;
  790. public static readonly Type EnvType;
  791. public static readonly Type BootEnvType;
  792. // reflection
  793. public static readonly Type SlotType;
  794. public static readonly Type FieldType;
  795. public static readonly Type MethodType;
  796. public static readonly Type ParamType;
  797. public static readonly Type SymbolType;
  798. // IO
  799. public static readonly Type InStreamType;
  800. public static readonly Type SysInStreamType;
  801. public static readonly Type OutStreamType;
  802. public static readonly Type SysOutStreamType;
  803. public static readonly Type BufType;
  804. public static readonly Type MemBufType;
  805. public static readonly Type MmapBufType;
  806. public static readonly Type FileBufType;
  807. public static readonly Type FileType;
  808. public static readonly Type LocalFileType;
  809. public static readonly Type ZipEntryFileType;
  810. public static readonly Type ZipType;
  811. public static readonly Type EndianType;
  812. // actos
  813. public static readonly Type ActorType;
  814. public static readonly Type ActorPoolType;
  815. public static readonly Type FutureType;
  816. // utils
  817. public static readonly Type DependType;
  818. public static readonly Type LogType;
  819. public static readonly Type LogLevelType;
  820. public static readonly Type LogRecType;
  821. public static readonly Type LocaleType;
  822. public static readonly Type MimeTypeType;
  823. public static readonly Type ProcessType;
  824. public static readonly Type RegexType;
  825. public static readonly Type RegexMatcherType;
  826. public static readonly Type ServiceType;
  827. public static readonly Type VersionType;
  828. public static readonly Type UnitType;
  829. public static readonly Type UnsafeType;
  830. public static readonly Type UuidType;
  831. // uri schemes
  832. public static readonly Type UriSchemeType;
  833. public static readonly Type FanSchemeType;
  834. public static readonly Type FileSchemeType;
  835. // exceptions
  836. public static readonly Type ErrType;
  837. public static readonly Type ArgErrType;
  838. public static readonly Type CancelledErrType;
  839. public static readonly Type CastErrType;
  840. public static readonly Type ConstErrType;
  841. public static readonly Type IndexErrType;
  842. public static readonly Type InterruptedErrType;
  843. public static readonly Type IOErrType;
  844. public static readonly Type NameErrType;
  845. public static readonly Type NotImmutableErrType;
  846. public static readonly Type NullErrType;
  847. public static readonly Type ParseErrType;
  848. public static readonly Type ReadonlyErrType;
  849. public static readonly Type TestErrType;
  850. public static readonly Type TimeoutErrType;
  851. public static readonly Type UnknownPodErrType;
  852. public static readonly Type UnknownSlotErrType;
  853. public static readonly Type UnknownTypeErrType;
  854. public static readonly Type UnknownServiceErrType;
  855. public static readonly Type UnknownFacetErrType;
  856. public static readonly Type UnresolvedErrType;
  857. public static readonly Type UnsupportedErrType;
  858. // generic parameter types used with generic types List, Map, and Method
  859. public static readonly ClassType[] GenericParameterTypes = new ClassType[256];
  860. public static readonly ClassType AType, BType, CType, DType, EType, FType, GType,
  861. HType, KType, LType, MType, RType, VType;
  862. public static Type genericParameterType(string name)
  863. {
  864. if (name.Length == 1 && name[0] < GenericParameterTypes.Length)
  865. return GenericParameterTypes[name[0]];
  866. else
  867. return null;
  868. }
  869. static Type builtin(string name, Type baseType)
  870. {
  871. return SysPod.findType(name, true);
  872. }
  873. public static readonly List m_args;
  874. public static readonly LocalFile m_homeDir;
  875. public static readonly string m_hostName;
  876. public static readonly string m_userName;
  877. private static Map m_env;
  878. }
  879. */