public virtual JSValue Construct(JSContext Scope, JSArgs Args)
        {
            JSObject o = new JSObject(this["prototype"], this);
            JSValue  r = this.Call(Scope, o, Args);

            if (r is JSObject)
            {
                return(r);
            }
            return(o);
        }
 public override JSValue Call(JSContext Scope, JSValue ThisObj, JSArgs Args)
 {
     if (ThisObj.CheckCoercible())
     {
         ThisObj = ThisObj.ToJSObject();
     }
     else
     {
         ThisObj = JSContext.CurrentGlobalContext.Global;
     }
     return(this.FunctionDef.Call(Scope, ThisObj, this, Args));
 }
Example #3
0
        public override JSValue Call(JSContext Scope, JSValue ThisObj, JSArgs Args)
        {
            if (ThisObj.CheckCoercible())
            {
                ThisObj = ThisObj.ToJSObject();
            }
            else
            {
                ThisObj = JSContext.CurrentGlobalContext.Global;
            }
            JSFunctionContext ctx = new JSFunctionContext(ThisObj, Args, new ParameterList(this.def.param_names), Scope, this.Scope);

            return(this.def.del(ctx));
        }
Example #4
0
        internal static DateTime UTC(JSArgs args)
        {
            double y       = args[0].NumberValue();
            double m       = args[1].NumberValue();
            double date    = (args.Count >= 3) ? args[2].NumberValue() : 1.0;
            double hours   = (args.Count >= 4) ? args[2].NumberValue() : 0.0;
            double minutes = (args.Count >= 5) ? args[2].NumberValue() : 0.0;
            double seconds = (args.Count >= 6) ? args[2].NumberValue() : 0.0;
            double ms      = (args.Count >= 7) ? args[2].NumberValue() : 0.0;

            if ((!double.IsNaN(y) && (y >= 0.0)) && (y <= 99.0))
            {
                y = 1900.0 + Math.Floor(y);
            }
            else
            {
                y = Math.Floor(y);
            }
            return((new DateTime((int)y, ((int)m) + 1, (int)date, (int)hours, (int)minutes, (int)seconds, (int)ms, DateTimeKind.Local)).ToUniversalTime());
        }
Example #5
0
        public JSFunctionContext(JSValue ThisObj, JSArgs Args, ParameterList parameterList, JSContext context, JSEnvRec Scope) : base(context)
        {
            if (!((ThisObj is JSNull) || (ThisObj is JSUndefined)))
            {
                this.ThisObj = ThisObj;
            }
            this.Args          = Args;
            this.parameterList = parameterList;
            base.LexicalEnv    = new JSDeclScope(Scope);
            base.VariableEnv   = base.LexicalEnv;
            base.CreateMutableBinding("arguments");
            base.SetMutableBinding("arguments", Args);
            int m = parameterList.Names.Length;

            for (int i = 0; i < m; i++)
            {
                base.CreateMutableBinding(parameterList.Names[i]);
                base.SetMutableBinding(parameterList.Names[i], Args[i]);
            }
        }
Example #6
0
 public JSDate(JSValue Prototype, JSValue Constructor, JSArgs args) : base(Prototype, Constructor)
 {
     if (args.Count == 0)
     {
         this.CLRDate = DateTime.Now;
     }
     else if (args.Count == 1)
     {
         JSValue v = args[0].ToPrimitive();
         if (v is JSString)
         {
             this.CLRDate = DateTime.Parse(v.StringValue());
         }
         else
         {
             this.CLRDate = FromNumber(TimeClip(v.NumberValue()));
         }
     }
     else
     {
         this.CLRDate = UTC(args);
     }
 }
Example #7
0
        internal static JSValue _Eval(JSContext Scope, JSArgs args, bool AsConstructor)
        {
            if (AsConstructor)
            {
                throw new JSRuntimeException("TypeError", "eval called as constructor");
            }
            JSValue arg = args[0];

            if (!(arg is JSString))
            {
                return(arg);
            }
            JSContext EvalContext = new JSContext(Scope);

            JSContext.PushContext(EvalContext);
            try
            {
                return(CompiledScript.Compile(arg.StringValue(), false).Run());
            }
            finally
            {
                JSContext.PopContext();
            }
        }
Example #8
0
 internal static JSValue DirectEvalCall(JSFunctionBase fnc, JSContext Scope, JSValue ThisObj, JSArgs Args)
 {
     if (fnc == JSContext.CurrentGlobalContext.GlobalEval)
     {
         return(_Eval(Scope, Args, false));
     }
     return(fnc.Call(JSContext.CurrentGlobalContext, ThisObj, Args));
 }
 private void AddFunction()
 {
     this.FunctionCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor)
     {
         string argNames;
         string sBody;
         if (args.Count > 1)
         {
             string[] ar = new string[args.Count - 1];
             for (int i = 0; i < (args.Count - 1); i++)
             {
                 ar[i] = args[i].StringValue();
             }
             argNames = string.Join(",", ar);
         }
         else
         {
             argNames = "";
         }
         if (args.Count == 0)
         {
             sBody = "";
         }
         else
         {
             sBody = args[args.Count - 1].StringValue();
         }
         JSFunctionObject r = CompiledScript.Compile("(function (" + argNames + ") {" + sBody + "})", false).Run() as JSFunctionObject;
         r.Scope            = JSContext.CurrentGlobalContext.LexicalEnv;
         return(r);
     }, 1);
     this.FunctionPrototype = new JSFunctionBase();
     this.FunctionPrototype.SetDataProp("constructor", this.FunctionCtor, true, false, true);
     this.FunctionPrototype.SetDataProp("call", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor)
     {
         JSArgs newArgs;
         if (AsConstructor)
         {
             throw new JSRuntimeException("TypeError", "Function.call called as constructor");
         }
         JSFunctionBase fnc = InternalUtilities.JSFunctionCast(ThisObj);
         if (args.Count == 0)
         {
             newArgs = args;
         }
         else
         {
             JSValue[] newAr = new JSValue[args.Count - 1];
             Array.Copy(args.ArgValues, 1, newAr, 0, args.Count - 1);
             newArgs = new JSArgs(ThisObj, newAr);
         }
         return(fnc.Call(Scope, args[0], newArgs));
     }, 1), true, false, true);
     this.FunctionPrototype.SetDataProp("apply", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor)
     {
         if (AsConstructor)
         {
             throw new JSRuntimeException("TypeError", "Function.apply called as constructor");
         }
         JSFunctionBase fnc = InternalUtilities.JSFunctionCast(ThisObj);
         JSValue argArray   = args[1];
         if ((argArray is JSUndefined) || (argArray is JSNull))
         {
             return(fnc.Call(Scope, args[0], new JSArgs(ThisObj, new JSValue[0])));
         }
         if (!(argArray is JSObject))
         {
             throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply");
         }
         JSValue olen = argArray["length"];
         if (!olen.CheckCoercible())
         {
             throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply");
         }
         double nlen = olen.NumberValue();
         uint len    = nlen.JSToUInt32();
         if (len != nlen)
         {
             throw new JSRuntimeException("TypeError", "Invalid argument to Function.apply");
         }
         JSValue[] newAr = new JSValue[len];
         for (int i = 0; i < len; i++)
         {
             newAr[i] = argArray[i.ToString()];
         }
         return(fnc.Call(Scope, args[0], new JSArgs(ThisObj, newAr)));
     }, 2), true, false, true);
     this.FunctionPrototype.SetDataProp("toString", new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor)
     {
         return(InternalUtilities.JSFunctionCast(ThisObj).GenerateString());
     }), true, false, true);
     this.FunctionPrototype.SetDataProp("length", 0.0, true, false, true);
     this.FunctionCtor.SetDataProp("prototype", this.FunctionPrototype, false, false, false);
 }
Example #10
0
 public virtual JSValue Call(JSContext Scope, JSValue ThisObj, JSArgs Args)
 {
     return(JSUndefined.Instance);
 }