/** * See E4X 13.1.2.1. */ public bool IsXMLName(Context cx, object value) { string name; try { name = ScriptConvert.ToString(value); } catch (EcmaScriptError ee) { if ("TypeError".Equals(ee.Name)) { return(false); } throw ee; } // See http://w3.org/TR/xml-names11/#NT-NCName int length = name.Length; if (length != 0) { if (IsNCNameStartChar(name [0])) { for (int i = 1; i != length; ++i) { if (!IsNCNameChar(name [i])) { return(false); } } return(true); } } return(false); }
internal static int GetPrecision(object arg) { int precision = ScriptConvert.ToInt32(arg); AssertValidPrecision(precision); return(precision); }
protected internal override void SetInstanceIdValue(int id, System.Object value_Renamed) { switch (id - base.MaxInstanceId) { case Id_ignoreComments: lib.ignoreComments = ScriptConvert.ToBoolean(value_Renamed); return; case Id_ignoreProcessingInstructions: lib.ignoreProcessingInstructions = ScriptConvert.ToBoolean(value_Renamed); return; case Id_ignoreWhitespace: lib.ignoreWhitespace = ScriptConvert.ToBoolean(value_Renamed); return; case Id_prettyIndent: lib.prettyIndent = ScriptConvert.ToInt32(value_Renamed); return; case Id_prettyPrinting: lib.prettyPrinting = ScriptConvert.ToBoolean(value_Renamed); return; } base.SetInstanceIdValue(id, value_Renamed); }
internal static Namespace Parse(XMLLib lib, Context cx, Object uriValue) { String prefix; String uri; if (uriValue is Namespace) { Namespace ns = (Namespace)uriValue; prefix = ns.Prefix; uri = ns.Uri; } else if (uriValue is QName) { QName qname = (QName)uriValue; uri = qname.Uri; if (uri != null) { prefix = qname.Prefix; } else { uri = qname.ToString(); prefix = null; } } else { uri = ScriptConvert.ToString(uriValue); prefix = (uri.Length == 0) ? "" : null; } return(new Namespace(lib, prefix, uri)); }
private static long toInteger(System.Object value, System.Type type, double min, double max) { double d = toDouble(value); if (System.Double.IsInfinity(d) || System.Double.IsNaN(d)) { // Convert to string first, for more readable message reportConversionError(ScriptConvert.ToString(value), type); } if (d > 0.0) { d = Math.Floor(d); } else { d = Math.Ceiling(d); } if (d < min || d > max) { // Convert to string first, for more readable message reportConversionError(ScriptConvert.ToString(value), type); } return((long)d); }
/* * Python-esque sequence operations. */ private static string js_concat(string target, object [] args) { int N = args.Length; if (N == 0) { return(target); } else if (N == 1) { string arg = ScriptConvert.ToString(args [0]); return(string.Concat(target, arg)); } // Find total capacity for the final string to avoid unnecessary // re-allocations in StringBuffer int size = target.Length; string [] argsAsStrings = new string [N]; for (int i = 0; i != N; ++i) { string s = ScriptConvert.ToString(args [i]); argsAsStrings [i] = s; size += s.Length; } System.Text.StringBuilder result = new System.Text.StringBuilder(size); result.Append(target); for (int i = 0; i != N; ++i) { result.Append(argsAsStrings [i]); } return(result.ToString()); }
public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args) { if (!f.HasTag(NUMBER_TAG)) { return(base.ExecIdCall(f, cx, scope, thisObj, args)); } int id = f.MethodId; if (id == Id_constructor) { double val = (args.Length >= 1) ? ScriptConvert.ToNumber(args [0]) : 0.0; if (thisObj == null) { // new Number(val) creates a new Number object. return(new BuiltinNumber(val)); } // Number(val) converts val to a number value. return(val); } // The rest of Number.prototype methods require thisObj to be Number BuiltinNumber nativeNumber = (thisObj as BuiltinNumber); if (nativeNumber == null) { throw IncompatibleCallError(f); } double value = nativeNumber.doubleValue; switch (id) { case Id_toLocaleString: case Id_toString: return(ImplToString(value, args)); case Id_toSource: return("(new Number(" + ScriptConvert.ToString(value) + "))"); case Id_valueOf: return(value); case Id_toFixed: return(ImplToFixed(value, args)); case Id_toExponential: return(ImplToExponential(value, args)); case Id_toPrecision: return(ImplToPrecision(value, args)); default: throw new ArgumentException(Convert.ToString(id)); } }
internal static int AssertValidPrecision(int precision) { if (precision < 0 || precision > MAX_PRECISION) { string msg = ScriptRuntime.GetMessage("msg.bad.precision", ScriptConvert.ToString(precision)); throw ScriptRuntime.ConstructError("RangeError", msg); } return(precision); }
internal XMLName toQualifiedName(Context cx, Object namespaceValue, Object nameValue) { // This is duplication of constructQName(cx, namespaceValue, nameValue) // but for XMLName String uri; String localName; if (nameValue is QName) { QName qname = (QName)nameValue; localName = qname.LocalName; } else { localName = ScriptConvert.ToString(nameValue); } Namespace ns; if (namespaceValue == Undefined.Value) { if ("*".Equals(localName)) { ns = null; } else { ns = GetDefaultNamespace(cx); } } else if (namespaceValue == null) { ns = null; } else if (namespaceValue is Namespace) { ns = (Namespace)namespaceValue; } else { ns = Namespace.Parse(this, cx, namespaceValue); } if (ns == null) { uri = null; } else { uri = ns.Uri; } return(XMLName.FormProperty(uri, localName)); }
private static string getString(IScriptable obj, string id) { object value = ScriptableObject.GetProperty(obj, id); if (value == UniqueTag.NotFound) { return(""); } return(ScriptConvert.ToString(value)); }
internal static QName Parse(XMLLib lib, Context cx, object namespaceValue, object nameValue) { String uri; String localName; String prefix; if (nameValue is QName) { QName qname = (QName)nameValue; localName = qname.LocalName; } else { localName = ScriptConvert.ToString(nameValue); } Namespace ns; if (namespaceValue == Undefined.Value) { if ("*".Equals(localName)) { ns = null; } else { ns = lib.GetDefaultNamespace(cx); } } else if (namespaceValue == null) { ns = null; } else if (namespaceValue is Namespace) { ns = (Namespace)namespaceValue; } else { ns = Namespace.Parse(lib, cx, namespaceValue); } if (ns == null) { uri = null; prefix = null; } else { uri = ns.Uri; prefix = ns.Prefix; } return(new QName(lib, uri, localName, prefix)); }
public static Namespace Parse(XMLLib lib, Context cx, Object prefixValue, Object uriValue) { String prefix; String uri; if (uriValue is QName) { QName qname = (QName)uriValue; uri = qname.Uri; if (uri == null) { uri = qname.ToString(); } } else { uri = ScriptConvert.ToString(uriValue); } if (uri.Length == 0) { if (prefixValue == Undefined.Value) { prefix = ""; } else { prefix = ScriptConvert.ToString(prefixValue); if (prefix.Length != 0) { throw ScriptRuntime.TypeError( "Illegal prefix '" + prefix + "' for 'no namespace'."); } } } else if (prefixValue == Undefined.Value) { prefix = ""; } else if (!lib.IsXMLName(cx, prefixValue)) { prefix = ""; } else { prefix = ScriptConvert.ToString(prefixValue); } return(new Namespace(lib, prefix, uri)); }
private static double toDouble(System.Object value) { if (value is System.ValueType) { return(Convert.ToDouble(value)); } else if (value is System.String) { return(ScriptConvert.ToNumber((System.String)value)); } else if (value is IScriptable) { if (value is Wrapper) { // TODO: optimize tail-recursion? return(toDouble(((Wrapper)value).Unwrap())); } else { return(ScriptConvert.ToNumber(value)); } } else { System.Reflection.MethodInfo meth; try { meth = value.GetType().GetMethod("doubleValue", new System.Type [0]); } catch (System.MethodAccessException) { meth = null; } catch (System.Security.SecurityException) { meth = null; } if (meth != null) { try { return(Convert.ToDouble(meth.Invoke(value, (System.Object [])null))); } catch (System.UnauthorizedAccessException) { // TODO: ignore, or error message? reportConversionError(value, typeof(double)); } catch (System.Reflection.TargetInvocationException) { // TODO: ignore, or error message? reportConversionError(value, typeof(double)); } } return(ScriptConvert.ToNumber(value.ToString())); } }
public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args) { if (!f.HasTag(SCRIPT_TAG)) { return(base.ExecIdCall(f, cx, scope, thisObj, args)); } int id = f.MethodId; switch (id) { case Id_constructor: { string source = (args.Length == 0) ? "" : ScriptConvert.ToString(args [0]); IScript script = compile(cx, source); BuiltinScript nscript = new BuiltinScript(script); ScriptRuntime.setObjectProtoAndParent(nscript, scope); return(nscript); } case Id_toString: { if (thisObj is BuiltinFunction) { return(((BuiltinFunction)thisObj).Decompile(0, 0)); } BuiltinScript real = realThis(thisObj, f); IScript realScript = real.script; if (realScript == null) { return(""); } return(cx.DecompileScript(realScript, 0)); } case Id_exec: { throw Context.ReportRuntimeErrorById("msg.cant.call.indirect", "exec"); } case Id_compile: { BuiltinScript real = realThis(thisObj, f); string source = ScriptConvert.ToString(args, 0); real.script = compile(cx, source); return(real); } } throw new ArgumentException(Convert.ToString(id)); }
/* * See ECMA 15.5.4.15 */ private static string js_substring(Context cx, string target, object [] args) { int length = target.Length; double start = ScriptConvert.ToInteger(args, 0); double end; if (start < 0) { start = 0; } else if (start > length) { start = length; } if (args.Length <= 1 || args [1] == Undefined.Value) { end = length; } else { end = ScriptConvert.ToInteger(args [1]); if (end < 0) { end = 0; } else if (end > length) { end = length; } // swap if end < start if (end < start) { if (cx.Version != Context.Versions.JS1_2) { double temp = start; start = end; end = temp; } else { // Emulate old JDK1.0 java.lang.String.substring() end = start; } } } return(target.Substring((int)start, ((int)end) - ((int)start))); }
private static string js_slice(string target, object [] args) { if (args.Length != 0) { double begin = ScriptConvert.ToInteger(args [0]); double end; int length = target.Length; if (begin < 0) { begin += length; if (begin < 0) { begin = 0; } } else if (begin > length) { begin = length; } if (args.Length == 1) { end = length; } else { end = ScriptConvert.ToInteger(args [1]); if (end < 0) { end += length; if (end < 0) { end = 0; } } else if (end > length) { end = length; } if (end < begin) { end = begin; } } return(target.Substring((int)begin, ((int)end) - ((int)begin))); } return(target); }
/// <summary> The global unescape method, as per ECMA-262 15.1.2.5.</summary> private object js_unescape(object [] args) { string s = ScriptConvert.ToString(args, 0); int firstEscapePos = s.IndexOf((char)'%'); if (firstEscapePos >= 0) { int L = s.Length; char [] buf = s.ToCharArray(); int destination = firstEscapePos; for (int k = firstEscapePos; k != L;) { char c = buf [k]; ++k; if (c == '%' && k != L) { int end, start; if (buf [k] == 'u') { start = k + 1; end = k + 5; } else { start = k; end = k + 2; } if (end <= L) { int x = 0; for (int i = start; i != end; ++i) { x = ScriptConvert.XDigitToInt(buf [i], x); } if (x >= 0) { c = (char)x; k = end; } } } buf [destination] = c; ++destination; } s = new string (buf, 0, destination); } return(s); }
/* * * See ECMA 15.5.4.7 * */ private static int js_lastIndexOf(string target, object [] args) { string search = ScriptConvert.ToString(args, 0); double end = ScriptConvert.ToNumber(args, 1); if (double.IsNaN(end) || end > target.Length) { end = target.Length; } else if (end < 0) { end = 0; } return(lastIndexOf( target.ToCharArray(), 0, target.Length, search.ToCharArray(), 0, search.Length, (int)end)); }
internal static QName Parse(XMLLib lib, Context cx, object value) { QName result; if (value is QName) { QName qname = (QName)value; result = new QName(lib, qname.Uri, qname.LocalName, qname.Prefix); } else { result = Parse(lib, cx, ScriptConvert.ToString(value)); } return(result); }
public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args) { int id = f.MethodId; switch (id) { case Id_print: for (int i = 0; i < args.Length; i++) { if (i > 0) { Console.Out.Write(" "); } Console.Out.Write(ScriptConvert.ToString(args [i])); } Console.Out.WriteLine(); return(Undefined.Value); case Id_version: if (args.Length > 0) { if (CliHelper.IsNumber(args [0])) { int newVer = (int)ScriptConvert.ToNumber(args [0]); if (Context.IsValidLanguageVersion(newVer)) { cx.Version = Context.ToValidLanguageVersion(newVer); } } } return((int)cx.Version); case Id_options: StringBuilder sb = new StringBuilder(); if (cx.HasFeature(Context.Features.Strict)) { sb.Append("strict"); } return(sb.ToString()); case Id_gc: GC.Collect(); return(Undefined.Value); } throw f.Unknown(); }
private static string js_toSource(Context cx, IScriptable scope, IScriptable thisObj) { // Emulation of SpiderMonkey behavior object name = ScriptableObject.GetProperty(thisObj, "name"); object message = ScriptableObject.GetProperty(thisObj, "message"); object fileName = ScriptableObject.GetProperty(thisObj, "fileName"); object lineNumber = ScriptableObject.GetProperty(thisObj, "lineNumber"); System.Text.StringBuilder sb = new System.Text.StringBuilder(); sb.Append("(new "); if (name == UniqueTag.NotFound) { name = Undefined.Value; } sb.Append(ScriptConvert.ToString(name)); sb.Append("("); if (message != UniqueTag.NotFound || fileName != UniqueTag.NotFound || lineNumber != UniqueTag.NotFound) { if (message == UniqueTag.NotFound) { message = ""; } sb.Append(ScriptRuntime.uneval(cx, scope, message)); if (fileName != UniqueTag.NotFound || lineNumber != UniqueTag.NotFound) { sb.Append(", "); if (fileName == UniqueTag.NotFound) { fileName = ""; } sb.Append(ScriptRuntime.uneval(cx, scope, fileName)); if (lineNumber != UniqueTag.NotFound) { int line = ScriptConvert.ToInt32(lineNumber); if (line != 0) { sb.Append(", "); sb.Append(ScriptConvert.ToString(line)); } } } } sb.Append("))"); return(sb.ToString()); }
/* * * See ECMA 15.5.4.6. Uses Java String.indexOf() * OPT to add - BMH searching from jsstr.c. */ private static int js_indexOf(string target, object [] args) { string search = ScriptConvert.ToString(args, 0); double begin = ScriptConvert.ToInteger(args, 1); if (begin > target.Length) { return(-1); } else { if (begin < 0) { begin = 0; } return(target.IndexOf(search, (int)begin)); } }
private static string num_to(double val, object [] args, int zeroArgMode, int oneArgMode, int precisionMin, int precisionOffset) { int precision; if (args.Length == 0) { precision = 0; oneArgMode = zeroArgMode; } else { /* We allow a larger range of precision than * ECMA requires; this is permitted by ECMA. */ precision = ScriptConvert.ToInt32(args [0]); if (precision < precisionMin || precision > MAX_PRECISION) { string msg = ScriptRuntime.GetMessage("msg.bad.precision", ScriptConvert.ToString(args [0])); throw ScriptRuntime.ConstructError("RangeError", msg); } } switch (zeroArgMode) { case DTOSTR_FIXED: return(val.ToString("F" + (precision + precisionOffset), NumberFormatter)); case DTOSTR_STANDARD_EXPONENTIAL: return(val.ToString("e" + (precision + precisionOffset), NumberFormatter)); case DTOSTR_STANDARD: if (oneArgMode == DTOSTR_PRECISION) { return(val.ToString(precision.ToString(), NumberFormatter)); } else { return(val.ToString(NumberFormatter)); } } Context.CodeBug(); return(string.Empty); // Not reached }
protected internal override void SetInstanceIdValue(int id, object value) { int shifted = id - base.MaxInstanceId; switch (shifted) { case Id_multiline: case Id_STAR: Impl.multiline = ScriptConvert.ToBoolean(value); return; case Id_input: case Id_UNDERSCORE: Impl.input = ScriptConvert.ToString(value); return; } base.SetInstanceIdValue(id, value); }
public override object ExecIdCall(IdFunctionObject f, Context cx, IScriptable scope, IScriptable thisObj, object [] args) { if (!f.HasTag(BOOLEAN_TAG)) { return(base.ExecIdCall(f, cx, scope, thisObj, args)); } int id = f.MethodId; if (id == Id_constructor) { bool b = ScriptConvert.ToBoolean(args, 0); if (thisObj == null) { return(new BuiltinBoolean(b)); } return(b); } // The rest of Boolean.prototype methods require thisObj to be Boolean if (!(thisObj is BuiltinBoolean)) { throw IncompatibleCallError(f); } bool value = ((BuiltinBoolean)thisObj).booleanValue; switch (id) { case Id_toString: return(value ? "true" : "false"); case Id_toSource: return(value ? "(new Boolean(true))" : "(new Boolean(false))"); case Id_valueOf: return(value); } throw new ArgumentException(Convert.ToString(id)); }
/// <summary> /// HTML composition aids. /// </summary> private static string Tagify(object thisObj, string tag, string attribute, object [] args) { string str = ScriptConvert.ToString(thisObj); System.Text.StringBuilder result = new System.Text.StringBuilder(); result.Append('<'); result.Append(tag); if (attribute != null) { result.Append(' '); result.Append(attribute); result.Append("=\""); result.Append(ScriptConvert.ToString(args, 0)); result.Append('"'); } result.Append('>'); result.Append(str); result.Append("</"); result.Append(tag); result.Append('>'); return(result.ToString()); }
internal static EcmaScriptError BadXMLName(object value) { String msg; if (CliHelper.IsNumber(value)) { msg = "Can not construct XML name from number: "; } else if (value is Boolean) { msg = "Can not construct XML name from boolean: "; } else if (value == Undefined.Value || value == null) { msg = "Can not construct XML name from "; } else { throw new ArgumentException(value.ToString()); } return(ScriptRuntime.TypeError(msg + ScriptConvert.ToString(value))); }
public XMLList(XMLLib lib, object inputObject) : this(lib) { string frag; if (inputObject == null || inputObject is Undefined) { frag = ""; } else if (inputObject is XML) { XML xml = (XML)inputObject; Add(xml); } else if (inputObject is XMLList) { XMLList xmll = (XMLList)inputObject; AddRange(xmll); } else { frag = ScriptConvert.ToString(inputObject).Trim(); if (!frag.StartsWith("<>")) { frag = "<>" + frag + "</>"; } frag = "<fragment>" + frag.Substring(2); if (!frag.EndsWith("</>")) { throw ScriptRuntime.TypeError("XML with anonymous tag missing end anonymous tag"); } frag = frag.Substring(0, frag.Length - 3) + "</fragment>"; XML orgXML = XML.CreateFromJS(lib, frag); // Now orphan the children and add them to our XMLList. XMLList children = (XMLList)orgXML.Children(); AddRange(children); } }
internal static BuiltinError make(Context cx, IScriptable scope, IdFunctionObject ctorObj, object [] args) { IScriptable proto = (IScriptable)(ctorObj.Get("prototype", ctorObj)); BuiltinError obj = new BuiltinError(); obj.SetPrototype(proto); obj.ParentScope = scope; if (args.Length >= 1) { ScriptableObject.PutProperty(obj, "message", ScriptConvert.ToString(args [0])); if (args.Length >= 2) { ScriptableObject.PutProperty(obj, "fileName", args [1]); if (args.Length >= 3) { int line = ScriptConvert.ToInt32(args [2]); ScriptableObject.PutProperty(obj, "lineNumber", (object)line); } } } return(obj); }
public override object Call(Context cx, IScriptable scope, IScriptable thisObj, object [] args) { if (m_MethodInfos.Length == 0) { throw new ApplicationException("No methods defined for call"); } int index = FindFunction(cx, m_MethodInfos, args, paramsParameters); if (index < 0) { Type c = m_MethodInfos [0].DeclaringType; string sig = c.FullName + '.' + FunctionName + '(' + ScriptSignature(args) + ')'; throw Context.ReportRuntimeErrorById("msg.java.no_such_method", sig); } MethodBase meth = (MethodBase)m_MethodInfos [index]; ParameterInfo [] pis = meth.GetParameters(); // First, we marshall the args. object [] origArgs = args; for (int i = 0; i < args.Length; i++) { object arg = args [i]; if (paramsParameters [index] && i >= pis.Length - 1) { // params[] arg is always an array type Type arrayType = pis [pis.Length - 1].ParameterType; Type arrayElementType = arrayType.GetElementType(); object [] dummyArg = new object [args.Length - i]; for (int e = i; e < args.Length; e++) { dummyArg [e - i] = Context.JsToCli(arg, arrayElementType); } args = new object [i + 1]; args.CopyTo(args, 0); args [i] = dummyArg; } else { Type argType = pis [i].ParameterType; object coerced = Context.JsToCli(arg, argType); if (coerced != arg) { if (origArgs == args) { args = new object [args.Length]; args.CopyTo(args, 0); } args [i] = coerced; } } } object cliObject; if (meth.IsStatic) { cliObject = null; // don't need an object } else { IScriptable o = thisObj; Type c = meth.DeclaringType; for (; ;) { if (o == null) { throw Context.ReportRuntimeErrorById("msg.nonjava.method", FunctionName, ScriptConvert.ToString(thisObj), c.FullName); } if (o is Wrapper) { cliObject = ((Wrapper)o).Unwrap(); if (c.IsInstanceOfType(cliObject)) { break; } } o = o.GetPrototype(); } } object retval = null; try { retval = (meth as MethodBase).Invoke(cliObject, args); } catch (Exception ex) { Context.ThrowAsScriptRuntimeEx(ex); } Type staticType = meth.DeclaringType; if (meth is MethodInfo) { staticType = ((MethodInfo)meth).ReturnType; } object wrapped = cx.Wrap(scope, retval, staticType); if (wrapped == null && staticType == Type.GetType("System.Void")) { wrapped = Undefined.Value; } return(wrapped); }