private void AddDate() { this.DateCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { if (!AsConstructor) { return(DateTime.UtcNow.ToString("F")); } return(new JSDate(this.DatePrototype, this.DateCtor, args)); }); this.DatePrototype = new JSObject(this.ObjectPrototype, this.DateCtor); this.DateCtor.SetDataProp("prototype", this.DatePrototype, false, false, false); }
private void AddObject() { this.ObjectCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { JSValue arg = args[0]; if ((arg is JSUndefined) || (arg is JSNull)) { return(new JSObject(this.ObjectPrototype, this.ObjectCtor)); } return(arg.ToJSObject()); }, 1); this.ObjectPrototype = new JSObject(null, this.ObjectCtor); this.ObjectCtor.SetDataProp("prototype", this.ObjectPrototype, false, false, false); }
private void AddArray() { this.ArrayCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs Args, bool AsConstructor) { if ((Args.Count == 1) && (Args[0] is JSNumber)) { double n = Args[0].NumberValue(); uint i = n.JSToUInt32(); if (n != i) { throw new JSRuntimeException("RangeError", "invalid array length"); } return(new JSArray(i)); } return(new JSArray(Args.ArgValues)); }, 1); this.ArrayPrototype = new JSArray(this.ObjectPrototype, this.ArrayCtor); this.ArrayCtor.SetDataProp("prototype", this.ArrayPrototype, false, false, false); }
private void AddRegExp() { this.RegExpCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { string pattern; string flags; JSValue v = args[0]; if (AsConstructor && (v is JSRegExp)) { return(v); } if (v is JSRegExp) { pattern = v["source"].StringValue(); } else if (v is JSUndefined) { pattern = ""; } else { pattern = v.StringValue(); } v = args[1]; if (v is JSUndefined) { flags = ""; } else { flags = v.StringValue(); } return(new JSRegExp(this.RegExpPrototype, this.RegExpCtor, pattern, flags)); }); this.RegExpPrototype = new JSObject(this.ObjectPrototype, this.RegExpCtor); this.RegExpCtor.SetDataProp("prototype", this.RegExpPrototype, false, false, false); }
private void AddRegExp() { this.RegExpCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { string pattern; string flags; JSValue v = args[0]; if (AsConstructor && (v is JSRegExp)) { return v; } if (v is JSRegExp) { pattern = v["source"].StringValue(); } else if (v is JSUndefined) { pattern = ""; } else { pattern = v.StringValue(); } v = args[1]; if (v is JSUndefined) { flags = ""; } else { flags = v.StringValue(); } return new JSRegExp(this.RegExpPrototype, this.RegExpCtor, pattern, flags); }); this.RegExpPrototype = new JSObject(this.ObjectPrototype, this.RegExpCtor); this.RegExpCtor.SetDataProp("prototype", this.RegExpPrototype, false, false, false); }
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); }
private void AddArray() { this.ArrayCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs Args, bool AsConstructor) { if ((Args.Count == 1) && (Args[0] is JSNumber)) { double n = Args[0].NumberValue(); uint i = n.JSToUInt32(); if (n != i) { throw new JSRuntimeException("RangeError", "invalid array length"); } return new JSArray(i); } return new JSArray(Args.ArgValues); }, 1); this.ArrayPrototype = new JSArray(this.ObjectPrototype, this.ArrayCtor); this.ArrayCtor.SetDataProp("prototype", this.ArrayPrototype, false, false, false); }
private void AddObject() { this.ObjectCtor = new JSDelegateWrapper(delegate(JSContext Scope, JSValue ThisObj, JSArgs args, bool AsConstructor) { JSValue arg = args[0]; if ((arg is JSUndefined) || (arg is JSNull)) { return new JSObject(this.ObjectPrototype, this.ObjectCtor); } return arg.ToJSObject(); }, 1); this.ObjectPrototype = new JSObject(null, this.ObjectCtor); this.ObjectCtor.SetDataProp("prototype", this.ObjectPrototype, false, false, false); }
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); }