123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- using Jint.Collections;
- using Jint.Native.Object;
- using Jint.Runtime;
- using Jint.Runtime.Descriptors;
- using Jint.Runtime.Interop;
- namespace Jint.Native.Symbol
- {
- /// <summary>
- /// http://www.ecma-international.org/ecma-262/5.1/#sec-15.5.4
- /// </summary>
- public sealed class SymbolPrototype : ObjectInstance
- {
- private SymbolConstructor _symbolConstructor;
- private SymbolPrototype(Engine engine)
- : base(engine)
- {
- }
- public static SymbolPrototype CreatePrototypeObject(Engine engine, SymbolConstructor symbolConstructor)
- {
- var obj = new SymbolPrototype(engine)
- {
- _prototype = engine.Object.PrototypeObject,
- _symbolConstructor = symbolConstructor
- };
- return obj;
- }
- protected override void Initialize()
- {
- _properties = new StringDictionarySlim<PropertyDescriptor>(8)
- {
- ["length"] = PropertyDescriptor.AllForbiddenDescriptor.NumberZero,
- ["constructor"] = new PropertyDescriptor(_symbolConstructor, true, false, true),
- ["toString"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toString", ToSymbolString), true, false, true),
- ["valueOf"] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "valueOf", ValueOf), true, false, true),
- ["toStringTag"] = new PropertyDescriptor(new JsString("Symbol"), false, false, true),
- [GlobalSymbolRegistry.ToPrimitive] = new PropertyDescriptor(new ClrFunctionInstance(Engine, "toPrimitive", ToPrimitive), false, false, true),
- [GlobalSymbolRegistry.ToStringTag] = new PropertyDescriptor(new JsString("Symbol"), false, false, true)
- };
- }
- private string SymbolDescriptiveString(JsSymbol sym)
- {
- return $"Symbol({sym.AsSymbol()})";
- }
- private JsValue ToSymbolString(JsValue thisObject, JsValue[] arguments)
- {
- if (!thisObject.IsSymbol())
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return SymbolDescriptiveString((JsSymbol)thisObject);
- }
- private JsValue ValueOf(JsValue thisObject, JsValue[] arguments)
- {
- var sym = thisObject.TryCast<SymbolInstance>();
- if (ReferenceEquals(sym, null))
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return sym.SymbolData;
- }
- private JsValue ToPrimitive(JsValue thisObject, JsValue[] arguments)
- {
- if (thisObject.IsSymbol())
- {
- return thisObject;
- }
- // Steps 3. and 4.
- var o = thisObject.AsInstance<SymbolInstance>();
- if (ReferenceEquals(o, null))
- {
- ExceptionHelper.ThrowTypeError(Engine);
- }
- return o.SymbolData;
- }
- }
- }
|