protected internal virtual JSValue GetFunctionReference(int key, JSEnvRec r) { JSContext ctx = this; if (ctx.Source == null) { return(this.Parent.GetFunctionReference(key, r)); } functionExpressionNode fncNode = ctx.Source.FunctionList[key]; JSFunctionObject fnc = new JSFunctionObject(fncNode, r); fnc.SetDataProp("length", (double)fncNode.ParameterList.Names.Length, false, false, false); return(fnc); }
protected internal virtual JSValue GetFunctionReference(int key, JSEnvRec r) { JSContext ctx = this; if (ctx.Source == null) { return this.Parent.GetFunctionReference(key, r); } functionExpressionNode fncNode = ctx.Source.FunctionList[key]; JSFunctionObject fnc = new JSFunctionObject(fncNode, r); fnc.SetDataProp("length", (double) fncNode.ParameterList.Names.Length, false, false, false); return fnc; }
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); }