private void AddProperty(ObjectInstance instance, string name, DelegateWrapper delegateWrapper) { JsValue getter = JsValue.FromObject(_engine.JintEngine, delegateWrapper); JsValue setter = JsValue.Null; instance.DefineOwnProperty(name, new PropertyDescriptor(getter, setter, true, false), true); }
private CliApiBuilder AddProperty(string name, DelegateWrapper delegateWrapper) { JsValue getter = JsValue.FromObject(_engine, delegateWrapper); JsValue setter = JsValue.Null; _instance.DefineOwnProperty(name, new PropertyDescriptor(getter, setter, true, false), true); return(this); }
public void DefineProperties(ObjectInstance jsObject, Type type) { var bindingFlags = BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly; //register properties foreach (var property in type.GetProperties(bindingFlags) .Where(p => p.GetCustomAttribute <JsHiddenAttribute>() == null)) { var name = property.GetName(); var jsProperty = new ClrProperty(jsObject.Engine, this, property); jsObject.DefineOwnProperty(name, jsProperty, false); } //register methods foreach (var methods in type .GetMethods(bindingFlags) .Where(p => p.GetCustomAttribute <JsHiddenAttribute>() == null) .GroupBy(p => p.GetName())) { var methodsArray = methods.ToArray(); var name = methods.Key; if (name == "toString") { continue; } var jsProperty = ClrMethodInfoFunc.Create(jsObject.Engine, this, name, methodsArray, jsObject); jsObject.DefineOwnProperty(name, jsProperty, false); } //register events foreach (var evt in type.GetEvents(bindingFlags) .Where(p => p.GetCustomAttribute <JsHiddenAttribute>() == null)) { var name = evt.GetName().ToLowerInvariant(); var jsProperty = new ClrEvent(jsObject.Engine, this, evt); jsObject.DefineOwnProperty(name, jsProperty, false); } DefineStatic(jsObject, type); }
/// <summary> /// https://tc39.es/ecma262/#sec-proxy-object-internal-methods-and-internal-slots-defineownproperty-p-desc /// </summary> public override bool DefineOwnProperty(JsValue property, PropertyDescriptor desc) { var arguments = new[] { _target, TypeConverter.ToPropertyKey(property), PropertyDescriptor.FromPropertyDescriptor(_engine, desc, strictUndefined: true) }; if (!TryCallHandler(TrapDefineProperty, arguments, out var result)) { return(_target.DefineOwnProperty(property, desc)); } var success = TypeConverter.ToBoolean(result); if (!success) { return(false); } var targetDesc = _target.GetOwnProperty(property); var extensibleTarget = _target.Extensible; var settingConfigFalse = desc.ConfigurableSet && !desc.Configurable; if (targetDesc == PropertyDescriptor.Undefined) { if (!extensibleTarget || settingConfigFalse) { ExceptionHelper.ThrowTypeError(_engine.Realm); } } else { if (!IsCompatiblePropertyDescriptor(extensibleTarget, desc, targetDesc)) { ExceptionHelper.ThrowTypeError(_engine.Realm); } if (targetDesc.Configurable && settingConfigFalse) { ExceptionHelper.ThrowTypeError(_engine.Realm); } if (targetDesc.IsDataDescriptor() && !targetDesc.Configurable && targetDesc.Writable) { if (desc.WritableSet && !desc.Writable) { ExceptionHelper.ThrowTypeError(_engine.Realm); } } } return(true); }
protected void AddInstanceProperty(ObjectInstance target, string name, Func <T, JsValue> get, Action <T, JsValue> set, bool enumerable, bool configurable) { GetterFunction getter = null; if (get != null) { getter = new GetterFunction(Engine, get); } SetterFunction setter = null; if (set != null) { setter = new SetterFunction(Engine, set); } target.DefineOwnProperty(name, new PropertyDescriptor(getter, setter, enumerable, configurable), false); }
internal SourceTextModuleRecord(Engine engine, Realm realm, Module source, string location, bool async) : base(engine, realm, source, location, async) { _source = source; // https://tc39.es/ecma262/#sec-parsemodule _importMeta = _realm.Intrinsics.Object.Construct(1); _importMeta.DefineOwnProperty("url", new PropertyDescriptor(location, PropertyFlag.ConfigurableEnumerableWritable)); HoistingScope.GetImportsAndExports( _source, out _requestedModules, out _importEntries, out _localExportEntries, out _indirectExportEntries, out _starExportEntries); //ToDo async modules }
public JsValue Parse(JsValue thisObject, JsValue[] arguments) { var parser = new JsonParser(_engine); var res = parser.Parse(TypeConverter.ToString(arguments[0])); if (arguments.Length > 1) { this._reviver = arguments[1]; ObjectInstance revRes = ObjectConstructor.CreateObjectConstructor(_engine).Construct(Arguments.Empty); revRes.DefineOwnProperty( "", new PropertyDescriptor( value: res, flags: PropertyFlag.ConfigurableEnumerableWritable ), false ); return(AbstractWalkOperation(revRes, "")); } return(res); }
/// <summary> /// http://www.ecma-international.org/ecma-262/5.1/#sec-10.2.1.2.2 /// </summary> /// <param name="name"></param> /// <param name="configurable"></param> public override void CreateMutableBinding(string name, bool configurable = true) { _bindingObject.DefineOwnProperty(name, new PropertyDescriptor(Undefined.Instance, true, true, configurable), true); }