public static object CheckConvert(object result) { // Finally, convert to fluentscript types. // Case 1: Aleady an LObject if (result is LObject) { return(result); } // Case 2: C# type so wrap inside of fluentscript type. return(LangTypeHelper.ConvertToLangValue(result)); }
/// <summary> /// Evaluate the result of indexing an object e.g. users[0] or users["admins"] /// </summary> /// <param name="regmethods"></param> /// <param name="node"></param> /// <param name="target"></param> /// <param name="ndxObj"></param> /// <returns></returns> public static LObject AccessIndex(RegisteredMethods regmethods, AstNode node, LObject target, LObject ndxObj) { object result = LObjects.Null; // Case 1: Array access users[0]; if (target.Type == LTypes.Array) { var ndx = ((LNumber)ndxObj).Value; var methods = regmethods.Get(LTypes.Array); // TODO: Make this generic. var length = Convert.ToInt32(methods.ExecuteMethod(target, "length", null)); if (ndx >= length) { throw ExceptionHelper.BuildRunTimeException(node, "Index out of bounds : '" + ndx + "'"); } result = methods.GetByNumericIndex(target, (int)ndx); } // Case 2: Map access. users["kishore"]; else if (target.Type == LTypes.Map) { var memberName = ((LString)ndxObj).Value; var methods = regmethods.Get(LTypes.Map); if (!methods.HasProperty(target, memberName)) { throw ExceptionHelper.BuildRunTimeException(node, "Property does not exist : '" + memberName + "'"); } result = methods.GetByStringMember(target, memberName); } // Conver to lang type. if (result != LObjects.Null && !(result is LObject)) { result = LangTypeHelper.ConvertToLangValue(result); } return((LObject)result); }