Esempio n. 1
0
 static void Main(string[] args)
 {
     Context = new Context();
     Context.Fs.Chroot = Context.Process.Instance.Cwd();
     Module main = new JavaScriptModule(Context, "tests/test.js");
     Context.Run(main);
     Thread t = new Thread(new ThreadStart(Run));
     t.Start();
 }
Esempio n. 2
0
 public override object CallLateBound(object thisObject, params object[] args)
 {
     if (args.Length == 0)
         throw new JavaScriptException(Module.Context.Engine, "TypeError", "null");
     if (!(args[0] is string))
         throw new JavaScriptException(Module.Context.Engine, "TypeError", "illegal module name");
     string filename = args[0] as string;
     // Lookup global module, like "fs"
     object exports = Module.Context.TryRequire(filename);
     if (exports != null)
         return exports;
     // Lookup local module
     if (Modules.HasProperty(filename))
         return Modules[filename];
     // Else try to load a local JavaScript module
     string dirname = Module.Context.Path.Dirname(Module.Source.Path);
     filename = Module.Context.Fs.CheckPath(Module.Context.Path.Join(dirname, filename));
     try {
         if (!File.Exists(filename))
             filename = filename + ".js";
         StreamReader sr = File.OpenText(filename);
         try {
             string code = sr.ReadToEnd();
             JavaScriptModule module = new JavaScriptModule(Module.Context, code, filename);
             Modules.SetPropertyValue(filename, exports = module.Run(), true);
             return exports;
         } finally {
             sr.Close();
         }
     } catch (Exception ex) {
         throw new JavaScriptException(Module.Context.Engine, "Error", ex.Message);
     }
 }
Esempio n. 3
0
 public RequireFunction(JavaScriptModule module)
     : base(module.Context.Engine.Function.InstancePrototype)
 {
     Module = module;
     Modules = Module.Engine.Object.Construct();
     SetPropertyValue("cache", Modules, false);
 }