Ejemplo n.º 1
0
        public static Script Compile(Code code, Context context, string source, IDebugger debugger)
        {
            if (debugger != null) debugger.SetSourceCode(source);

            context.SetFunction("eval", (string c) =>
                                            {
                                                var bkCode = source;
                                                var scope = context.Scope;
                                                context.Scope = new Scope {Obj = scope.Obj};
                                                var result = Compile(code, context, c, debugger).Run();

                                                CloneProperties(context, context.Scope.Obj, scope.Obj);

                                                context.Scope = scope;

                                                if (debugger != null) debugger.SetSourceCode(bkCode);

                                                return result;
                                            });

            context.SetFunction("isNaN", (object value) => value == JsObject.NaN);

            JsObject instance = null;

            if (!_cache.ContainsKey(source))
            {
                var csCode = GetCsCode(code, Parse(source, debugger != null, context));
                Assembly asm = CSScript.LoadCode(csCode);
                var type = asm.GetType("C0");
                var args = new List<object>();
                context.Actions.ToList().ForEach(a => args.Add(a.Value));
                args.Add(debugger);
                instance = (JsObject) Activator.CreateInstance(type, args.ToArray());

                _cache[source] = instance;
            }
            else
            {
                instance = _cache[source];
            }

            CloneProperties(context, context.Scope.Obj, instance);

            context.Scope.Obj = instance;

            return new Script(context);
        }
Ejemplo n.º 2
0
 public static Script Compile(Code code, Context context, string source)
 {
     return Compile(code, context, source, null);
 }
Ejemplo n.º 3
0
 private static string GetCsCode(Code code, dynamic tree)
 {
     return code.GetCsCode(tree);
 }