12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using Jint.Native.Function;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Interop;
- namespace Jint.Native.String
- {
- public sealed class StringConstructor : FunctionInstance, IConstructor
- {
- public StringConstructor(Engine engine)
- : base(engine, null, null, false)
- {
- }
- public static StringConstructor CreateStringConstructor(Engine engine)
- {
- var obj = new StringConstructor(engine);
- obj.Extensible = true;
- // The value of the [[Prototype]] internal property of the String constructor is the Function prototype object
- obj.Prototype = engine.Function.PrototypeObject;
- obj.PrototypeObject = StringPrototype.CreatePrototypeObject(engine, obj);
- obj.FastAddProperty("length", 1, false, false, false);
- // The initial value of String.prototype is the String prototype object
- obj.FastAddProperty("prototype", obj.PrototypeObject, false, false, false);
- return obj;
- }
- public void Configure()
- {
- FastAddProperty("fromCharCode", new ClrFunctionInstance<object, string>(Engine, FromCharCode), false, false, false);
- }
- private static string FromCharCode(object thisObj, object[] arguments)
- {
- var chars = new char[arguments.Length];
- for (var i = 0; i < chars.Length; i++ )
- {
- chars[i] = (char)TypeConverter.ToUint16(arguments[i]);
- }
-
- return new System.String(chars);
- }
- public override object Call(object thisObject, object[] arguments)
- {
- if (arguments.Length == 0)
- {
- return "";
- }
- return TypeConverter.ToString(arguments[0]);
- }
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.7.2.1
- /// </summary>
- /// <param name="arguments"></param>
- /// <returns></returns>
- public ObjectInstance Construct(object[] arguments)
- {
- return Construct(arguments.Length > 0 ? TypeConverter.ToString(arguments[0]) : "");
- }
- public StringPrototype PrototypeObject { get; private set; }
- public StringInstance Construct(string value)
- {
- var instance = new StringInstance(Engine);
- instance.Prototype = PrototypeObject;
- instance.PrimitiveValue = value;
- instance.Extensible = true;
- instance.FastAddProperty("length", value.Length, false, false, false);
- return instance;
- }
- }
- }
|