/// <summary> /// Converts a string expression into a script object. /// </summary> private SObject ToScriptObject(string exp) { exp = exp.Trim(); // This means it's either an indexer or an array if (exp.EndsWith("]")) { if (!(exp.StartsWith("[") && !exp.Remove(0, 1).Contains("["))) // When there's no "[" besides the start, and it starts with [, then it is an array. Otherwise, do real check. { // It is possible that we are having a simple array declaration here. // We check that by looking if we can find a "[" before the expression ends: var depth = 0; var index = exp.Length - 2; var indexerStartIndex = 0; var foundIndexer = false; StringEscapeHelper escaper = new RightToLeftStringEscapeHelper(exp, index); while (index > 0 && !foundIndexer) { var t = exp[index]; escaper.CheckStartAt(index); if (!escaper.IsString) { if (t == ')' || t == '}' || t == ']') { depth++; } else if (t == '(' || t == '{') { depth--; } else if (t == '[') { if (depth == 0) { if (index > 0) { indexerStartIndex = index; foundIndexer = true; } } else { depth--; } } } index--; } if (foundIndexer) { var indexerCode = exp.Substring(indexerStartIndex + 1, exp.Length - indexerStartIndex - 2); var identifier = exp.Remove(indexerStartIndex); var statementResult = ExecuteStatement(new ScriptStatement(indexerCode)); return ToScriptObject(identifier).GetMember(this, statementResult, true); } } } // Normal object return procedure: // Negative number: var isNegative = false; if (exp.StartsWith("-")) { exp = exp.Remove(0, 1); isNegative = true; } double dblResult; SObject returnObject; if (exp == SObject.LITERAL_NULL) { returnObject = Null; } else if (exp == SObject.LITERAL_UNDEFINED) { returnObject = Undefined; } else if (exp == SObject.LITERAL_BOOL_FALSE) { returnObject = CreateBool(false); } else if (exp == SObject.LITERAL_BOOL_TRUE) { returnObject = CreateBool(true); } else if (exp == SObject.LITERAL_NAN) { returnObject = CreateNumber(double.NaN); } else if (exp == SObject.LITERAL_THIS) { returnObject = Context.This; } else if (SNumber.TryParse(exp, out dblResult)) { returnObject = CreateNumber(dblResult); } else if (exp.StartsWith("\"") && exp.EndsWith("\"") || exp.StartsWith("\'") && exp.EndsWith("\'")) { returnObject = CreateString(exp.Remove(exp.Length - 1, 1).Remove(0, 1), true, false); } else if (exp.StartsWith("$\"") && exp.EndsWith("\"") || exp.StartsWith("$\'") && exp.EndsWith("\'")) { returnObject = CreateString(exp.Remove(exp.Length - 1, 1).Remove(0, 2), true, true); } else if (exp.StartsWith("@\"") && exp.EndsWith("\"") || exp.StartsWith("@\'") && exp.EndsWith("\'")) { returnObject = CreateString(exp.Remove(exp.Length - 1, 1).Remove(0, 2), false, false); } else if (exp.StartsWith("{") && exp.EndsWith("}")) { returnObject = SProtoObject.Parse(this, exp); } else if (exp.StartsWith("[") && exp.EndsWith("]")) { returnObject = SArray.Parse(this, exp); } else if (exp.StartsWith("function") && Regex.IsMatch(exp, REGEX_FUNCTION)) { returnObject = new SFunction(this, exp); } else if (Context.IsAPIUsing(exp)) { returnObject = Context.GetAPIUsing(exp); } else if (Context.IsVariable(exp)) { returnObject = Context.GetVariable(exp); } else if (Context.This.HasMember(this, exp)) { returnObject = Context.This.GetMember(this, CreateString(exp), false); } else if (Context.IsPrototype(exp)) { returnObject = Context.GetPrototype(exp); } else if (exp.StartsWith("new ")) { returnObject = Context.CreateInstance(exp); } else if (exp.StartsWith(ObjectBuffer.OBJ_PREFIX)) { var strId = exp.Remove(0, ObjectBuffer.OBJ_PREFIX.Length); var id = 0; if (int.TryParse(strId, out id) && ObjectBuffer.HasObject(id)) { returnObject = (SObject)ObjectBuffer.GetObject(id); } else { returnObject = ErrorHandler.ThrowError(ErrorType.SyntaxError, ErrorHandler.MESSAGE_SYNTAX_INVALID_TOKEN, exp); } } else { returnObject = ErrorHandler.ThrowError(ErrorType.ReferenceError, ErrorHandler.MESSAGE_REFERENCE_NOT_DEFINED, exp); } if (isNegative) returnObject = ObjectOperators.NegateNumber(this, returnObject); return returnObject; }
private static object TranslateFunction(SFunction obj) { if (obj.Method != null) return obj.Method; else if (obj.DotNetMethod != null) return obj.DotNetMethod; else return obj.ToScriptSource(); }
private SObject ExecuteFunction(ScriptStatement statement) { // function <name> () var exp = statement.Code; exp = exp.Remove(0, "function".Length).Trim(); var functionName = exp.Remove(exp.IndexOf("(")); if (!IsValidIdentifier(functionName)) return ErrorHandler.ThrowError(ErrorType.SyntaxError, ErrorHandler.MESSAGE_SYNTAX_MISSING_VAR_NAME); var functionExpression = "function " + exp.Remove(0, exp.IndexOf("(")); // The function's body is the next statement: _index++; if (_index < _statements.Length) { var functionBodyStatement = _statements[_index]; var functionBody = functionBodyStatement.Code; if (!functionBodyStatement.IsCompoundStatement) functionBody = "{" + functionBody + "}"; functionExpression += functionBody; var function = new SFunction(this, functionExpression); Context.AddVariable(functionName, function); return function; } else { return ErrorHandler.ThrowError(ErrorType.SyntaxError, ErrorHandler.MESSAGE_SYNTAX_EXPECTED_EXPRESSION, "end of script"); } }