public override JsInstance this[string index] { get { if (index == THIS && thisDescriptor != null) { return(thisDescriptor.Get(this)); } if (index == ARGUMENTS && argumentsDescriptor != null) { return(argumentsDescriptor.Get(this)); } return(base[index]); // will use overriden GetDescriptor } set { if (index == THIS) { if (thisDescriptor != null) { thisDescriptor.Set(this, value); } else { DefineOwnProperty(thisDescriptor = new ValueDescriptor(this, index, value)); } } else if (index == ARGUMENTS) { if (argumentsDescriptor != null) { argumentsDescriptor.Set(this, value); } else { DefineOwnProperty(argumentsDescriptor = new ValueDescriptor(this, index, value)); } } else { Descriptor d = GetDescriptor(index); if (d != null) { d.Set(this, value); } else if (globalScope != null) { // TODO: move to Execution visitor // define missing property in the global scope globalScope.DefineOwnProperty(index, value); } else { // this scope is a global scope DefineOwnProperty(index, value); } } } }
public override JsInstance this[string index] { get { if (index == THIS && thisDescriptor != null) { return(thisDescriptor.Get(this)); } if (index == ARGUMENTS && argumentsDescriptor != null) { return(argumentsDescriptor.Get(this)); } return(base[index]); // will use overriden GetDescriptor } set { if (index == THIS) { if (thisDescriptor != null) { thisDescriptor.Set(this, value); } else { DefineOwnProperty(index, thisDescriptor = new ValueDescriptor(this, index, value)); } } else if (index == ARGUMENTS) { if (argumentsDescriptor != null) { argumentsDescriptor.Set(this, value); } else { DefineOwnProperty(index, argumentsDescriptor = new ValueDescriptor(this, index, value)); } } else { Descriptor d = GetDescriptor(index); if (d != null) { // we have a property in the scopes hierarchy or in the bag if (d.Owner == bag || d.Owner == this || d.Owner.IsPrototypeOf(this)) { // if this is an own property of the bag or in scopes d.Set(d.Owner, value); } else { // this property should be from one of prototypes of the bag if (bag != null) { bag[index] = value; } } } else if (globalScope != null) { // define missing property in the global scope globalScope.DefineOwnProperty(index, value); } else { // this scope is a global scope DefineOwnProperty(index, value); } } } }
public void Visit(JsonExpression json) { JsObject instance = Global.ObjectClass.New(); JsScope scope = new JsScope() { Prototype = CurrentScope }; scope.DefineOwnProperty(JsInstance.THIS, instance); EnterScope(scope); try { foreach (var item in json.Values) { item.Value.Accept(this); instance.DefineOwnProperty(item.Key, Result); } } finally { ExitScope(); } Result = instance; }
public void ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters, Type[] genericParameters) { if (function == null) { return; } if (recursionLevel++ > MaxRecursions) { throw new JsException(Global.ErrorClass.New("Too many recursions in the script.")); } // ecma chapter 10. // TODO: move creation of the activation object to the JsFunction // create new argument object and instantinate arguments into it JsArguments args = new JsArguments(Global, function, parameters); // create new activation object and copy instantinated arguments to it // Activation should be before the function.Scope hierarchy JsScope functionScope = new JsScope(function.Scope ?? GlobalScope); for (int i = 0; i < function.Arguments.Count; i++) if (i < parameters.Length) functionScope.DefineOwnProperty( new LinkedDescriptor( functionScope, function.Arguments[i], args.GetDescriptor(i.ToString()), args ) ); else functionScope.DefineOwnProperty( new ValueDescriptor( functionScope, function.Arguments[i], JsUndefined.Instance ) ); // define arguments variable if (HasOption(Options.Strict)) functionScope.DefineOwnProperty(JsScope.ARGUMENTS, args); else args.DefineOwnProperty(JsScope.ARGUMENTS, args); // set this variable if (that != null) functionScope.DefineOwnProperty(JsScope.THIS, that); else functionScope.DefineOwnProperty(JsScope.THIS, that = Global as JsObject); // enter activation object EnterScope(functionScope); try { PermissionSet.PermitOnly(); if (genericParameters != null && genericParameters.Length > 0) Result = function.Execute(this, that, parameters, genericParameters); else Result = function.Execute(this, that, parameters); // Resets the return flag if (exit) { exit = false; } } finally { // return to previous execution state ExitScope(); CodeAccessPermission.RevertPermitOnly(); recursionLevel--; } }
public void ExecuteFunction(JsFunction function, JsDictionaryObject that, JsInstance[] parameters) { if (function == null) { return; } JsScope functionScope = new JsScope(); JsArguments args = new JsArguments(Global, function, parameters); functionScope.Prototype = args; if (HasOption(Options.Strict)) functionScope.DefineOwnProperty(JsInstance.ARGUMENTS, args); else functionScope.Prototype.DefineOwnProperty(JsInstance.ARGUMENTS, args); if (that != null) functionScope.DefineOwnProperty(JsInstance.THIS, that); functionScope.Extensible = false; //for (int i = function.DeclaringScopes.Count - 1; i >= 0; i--) //{ // EnterScope(function.DeclaringScopes[i]); //} EnterScope(function); EnterScope(functionScope); try { PermissionSet.PermitOnly(); Result = function.Execute(this, that, parameters); // Resets the return flag if (exit) { exit = false; } } finally { ExitScope(); ExitScope(); CodeAccessPermission.RevertPermitOnly(); } }