123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- using System;
- using Jint.Native.Function;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Descriptors.Specialized;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Object
- {
- public sealed class ObjectConstructor : FunctionInstance, IConstructor
- {
- private readonly Engine _engine;
- public ObjectConstructor(Engine engine) : base(engine, engine.RootFunction, null, null)
- {
- _engine = engine;
- engine.RootFunction.DefineOwnProperty("hasOwnProperty", new ClrDataDescriptor<ObjectInstance>(engine, HasOwnProperty), false);
- engine.RootFunction.DefineOwnProperty("toString", new ClrDataDescriptor<ObjectInstance>(engine, ToString), false);
- }
- public override object Call(object thisObject, object[] arguments)
- {
- return Undefined.Instance;
- }
- public ObjectInstance Construct(object[] arguments)
- {
- var instance = new ObjectInstance(this.Prototype);
- // the constructor is the function constructor of an object
- instance.DefineOwnProperty("constructor", new DataDescriptor(this) { Writable = true, Enumerable = false, Configurable = false }, false);
- instance.DefineOwnProperty("prototype", new DataDescriptor(this.Prototype) { Writable = true, Enumerable = false, Configurable = false }, false);
- return instance;
- }
- private static object HasOwnProperty(ObjectInstance thisObject, object[] arguments)
- {
- var propertyName = TypeConverter.ToString(arguments[0]);
- var desc = thisObject.GetOwnProperty(propertyName);
- return desc != PropertyDescriptor.Undefined;
- }
- private static object ToString(ObjectInstance thisObject, object[] arguments)
- {
- if (thisObject == null || thisObject == Undefined.Instance)
- {
- return "[object Undefined]";
- }
- if (thisObject == Null.Instance)
- {
- return "[object Null]";
- }
- return string.Format("[object {0}]", thisObject.Class);
-
- }
- }
- }
|