本文整理汇总了C#中IEnvironment.CreateString方法的典型用法代码示例。如果您正苦于以下问题:C# IEnvironment.CreateString方法的具体用法?C# IEnvironment.CreateString怎么用?C# IEnvironment.CreateString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnvironment
的用法示例。
在下文中一共展示了IEnvironment.CreateString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Call
protected override sealed IDynamic Call(IEnvironment environment, IArgs args)
{
if (args.Count > 0)
{
return args[0].ConvertToString();
}
return environment.CreateString("");
}
示例2: ToString
private static IDynamic ToString(IEnvironment environment, IArgs args)
{
var v = environment.Context.ThisBinding;
switch (v.TypeCode)
{
case LanguageTypeCode.Boolean:
return environment.CreateString(((IBoolean)v).BaseValue ? "true" : "false");
case LanguageTypeCode.Object:
var o = (IObject)v;
if (o.Class == "Boolean")
{
return environment.CreateString(((IBoolean)((IPrimitiveWrapper)o).PrimitiveValue).BaseValue ? "true" : "false");
}
break;
}
throw environment.CreateTypeError("");
}
示例3: Add
public static IJsValue Add(IEnvironment environment, IJsValue lhs, IJsValue rhs)
{
if ((lhs.ToPrimitive() is string) || (rhs.ToPrimitive() is string))
{
return environment.CreateString(lhs.ToString() + rhs.ToString());
}
return ArithEval(environment, lhs, rhs, (l, r) => l + r);
}
示例4: FromCharCode
internal static IDynamic FromCharCode(IEnvironment environment, IArgs args)
{
var sb = new StringBuilder(args.Count);
foreach (var arg in args)
{
sb.Append((char)arg.ConvertToUInt16().BaseValue);
}
return environment.CreateString(sb.ToString());
}
示例5: Construct
public override sealed IObject Construct(IEnvironment environment, IArgs args)
{
var patternArg = args[0];
var flagsArg = args[1];
var pattern = "";
var flags = "";
bool global, ignoreCase, multiline;
var patternObject = patternArg as IObject;
if (patternObject != null && patternObject.Class == "RegExp")
{
if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
{
throw environment.CreateTypeError("");
}
global = ((IBoolean)patternObject.Get("global")).BaseValue;
ignoreCase = ((IBoolean)patternObject.Get("ignoreCase")).BaseValue;
multiline = ((IBoolean)patternObject.Get("multiline")).BaseValue;
pattern = ((IString)patternObject.Get("source")).BaseValue;
if (global) flags += "g";
if (ignoreCase) flags += "i";
if (multiline) flags += "m";
}
else
{
if (patternArg.TypeCode != LanguageTypeCode.Undefined)
{
pattern = patternArg.ConvertToString().BaseValue;
}
if (flagsArg.TypeCode != LanguageTypeCode.Undefined)
{
flags = flagsArg.ConvertToString().BaseValue;
}
global = flags.Contains("g");
ignoreCase = flags.Contains("i");
multiline = flags.Contains("m");
}
var regExpObj = new NRegExp(environment);
regExpObj.Class = "RegExp";
regExpObj.Extensible = true;
regExpObj.Prototype = environment.RegExpPrototype;
regExpObj.Body = pattern;
regExpObj.Flags = flags;
regExpObj.RegExpMatcher = Machete.Compiler.RegExpParser.Parse(Environment, pattern, flags);
regExpObj.DefineOwnProperty("source", environment.CreateDataDescriptor(environment.CreateString(pattern), false, false, false), false);
regExpObj.DefineOwnProperty("global", environment.CreateDataDescriptor(environment.CreateBoolean(global), false, false, false), false);
regExpObj.DefineOwnProperty("ignoreCase", environment.CreateDataDescriptor(environment.CreateBoolean(ignoreCase), false, false, false), false);
regExpObj.DefineOwnProperty("multiline", environment.CreateDataDescriptor(environment.CreateBoolean(multiline), false, false, false), false);
regExpObj.DefineOwnProperty("lastIndex", environment.CreateDataDescriptor(environment.CreateNumber(0.0), true, false, false), false);
return regExpObj;
}
示例6: ToString
internal static IDynamic ToString(IEnvironment environment, IArgs args)
{
var obj = environment.Context.ThisBinding.ConvertToObject();
var name = obj.Get("name").ConvertToString().BaseValue;
var message = obj.Get("message").ConvertToString().BaseValue;
if (name == "undefined")
{
if (message == "undefined")
{
return environment.CreateString("Error");
}
return environment.CreateString("Error: " + message);
}
else
{
if (message == "undefined")
{
return environment.CreateString(name);
}
return environment.CreateString(name + ": " + message);
}
}
示例7: Construct
public override IObject Construct(IEnvironment environment, IArgs args)
{
var message = args[0].ConvertToString();
if (message.BaseValue == "undefined")
{
message = environment.CreateString("");
}
var error = new NError(environment);
error.Class = "Error";
error.Extensible = true;
error.Prototype = environment.ErrorPrototype;
error.Put("message", message, false);
return error;
}
示例8: Construct
public override sealed IObject Construct(IEnvironment environment, IArgs args)
{
var obj = new NString(environment);
{
var str = args.Count > 0 ? args[0].ConvertToString() : environment.CreateString("");
var len = environment.CreateNumber(str.BaseValue.Length);
var lenDesc = environment.CreateDataDescriptor(len, false, false, false);
obj.Class = "String";
obj.Extensible = true;
obj.Prototype = environment.StringPrototype;
obj.PrimitiveValue = str;
obj.DefineOwnProperty("length", lenDesc, false);
}
return obj;
}
示例9: Evaluate
public IJsValue Evaluate(IEnvironment environment)
{
// TODO: Throw on cast failure
var inspected = Inspected.Evaluate(environment) as IJsObject;
if (inspected == null)
{
// Note: 'for (var m in undefined)' and 'for (var m in null)' is allowed...
return JsUndefined.Value;
}
var propertyNames = (inspected.GetProperties()
.Where(pd => pd.Enumerable)
.Select(pd => pd.Name)
.Distinct())
.ToList();
var bindingName = ((IAstWithName) Binding).Name;
var bindingEnvironment = environment;
if (DeclareBinding)
{
bindingEnvironment = new Environment(environment);
bindingEnvironment.CreateReference(bindingName, JsUndefined.Value);
}
foreach (var propertyName in propertyNames)
{
bindingEnvironment.GetReference(bindingName).SetValue(null, environment.CreateString(propertyName));
Block.Evaluate(bindingEnvironment);
if (bindingEnvironment.ControlFlow.Break)
{
break;
}
if (bindingEnvironment.ControlFlow.Return)
{
break;
}
}
return JsUndefined.Value;
}
示例10: CharAt
IDynamic CharAt(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding;
environment.CheckObjectCoercible(o);
var s = o.ConvertToString().BaseValue;
var position = (int)args[0].ConvertToInteger().BaseValue;
if (position < 0 || position >= s.Length)
{
return environment.CreateString(string.Empty);
}
return environment.CreateString(s.Substring(position, 1));
}
示例11: Evaluate
public IJsValue Evaluate(IEnvironment environment)
{
return environment.CreateString(Name);
}
示例12: Exec
IDynamic Exec(IEnvironment environment, IArgs args)
{
var r = (NRegExp)environment.Context.ThisBinding;
var s = args[0].ConvertToString().BaseValue;
var length = s.Length;
var global = r.Get("global").ConvertToBoolean().BaseValue;
var i = !global ? 0 : (int)r.Get("lastIndex").ConvertToInteger().BaseValue;
int beginIndex = 0;
MatchResult result;
while (true)
{
beginIndex = i;
if (i < 0 || i > length)
{
r.Put("length", environment.CreateNumber(0.0), true);
return environment.Null;
}
result = r.RegExpMatcher(s, i);
if (result.success) break;
i++;
}
if (global)
{
r.Put("length", environment.CreateNumber(result.matchState.endIndex), true);
}
var captures = result.matchState.captures;
var n = captures.Length;
var array = ((IConstructable)environment.ArrayConstructor).Construct(environment, environment.EmptyArgs);
array.DefineOwnProperty("index", environment.CreateDataDescriptor(environment.CreateNumber(beginIndex), true, true, true), true);
array.DefineOwnProperty("input", environment.CreateDataDescriptor(environment.CreateString(s), true, true, true), true);
array.DefineOwnProperty("length", environment.CreateDataDescriptor(environment.CreateNumber(n + 1), null, null, null), true);
IDynamic value = environment.CreateString(result.matchState.input.Substring(beginIndex, result.matchState.endIndex - beginIndex));
var desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty("0", desc, true);
for (int index = 0; index < n; index++)
{
var v = captures[index];
value = v == null ? (IDynamic)environment.Undefined : (IDynamic)environment.CreateString(v);
desc = environment.CreateDataDescriptor(value, true, true, true);
array.DefineOwnProperty((index + 1).ToString(), desc, true);
}
return array;
}
示例13: ToLocaleString
private static IDynamic ToLocaleString(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding.ConvertToObject();
var arrayLen = o.Get("length");
var len = (uint)arrayLen.ConvertToUInt32().BaseValue;
var separator = CultureInfo.CurrentCulture.TextInfo.ListSeparator;
if (len == 0)
{
return environment.CreateString(string.Empty);
}
var sb = new StringBuilder();
{
for (uint i = 0; i < len; i++)
{
if (i > 0)
{
sb.Append(separator);
}
var nextElement = o.Get(i.ToString());
switch (nextElement.TypeCode)
{
case LanguageTypeCode.Boolean:
case LanguageTypeCode.String:
case LanguageTypeCode.Number:
case LanguageTypeCode.Object:
var elementObj = nextElement.ConvertToObject();
var func = elementObj.Get("toLocaleString") as ICallable;
if (func == null)
{
throw environment.CreateTypeError("");
}
sb.Append(func.Call(environment, elementObj, environment.EmptyArgs).ConvertToString().BaseValue);
break;
}
}
}
return environment.CreateString(sb.ToString());
}
示例14: Substring
IDynamic Substring(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding;
environment.CheckObjectCoercible(o);
var s = o.ConvertToString().BaseValue;
var start = args[0];
var end = args[1];
var startIndex = args[0].ConvertToInteger().BaseValue;
var endIndex = end is IUndefined ? (double)s.Length : end.ConvertToInteger().BaseValue;
var finalStart = Math.Min(Math.Max(startIndex, 0), s.Length);
var finalEnd = Math.Min(Math.Max(endIndex, 0), s.Length);
var from = (int)Math.Min(finalStart, finalEnd);
var to = (int)Math.Max(finalStart, finalEnd);
return environment.CreateString(s.Substring(from, to - from));
}
示例15: Slice
IDynamic Slice(IEnvironment environment, IArgs args)
{
var o = environment.Context.ThisBinding;
environment.CheckObjectCoercible(o);
var s = o.ConvertToString().BaseValue;
var start = args[0];
var end = args[1];
var intStart = (int)start.ConvertToInteger().BaseValue;
var intEnd = end is IUndefined ? s.Length : (int)end.ConvertToInteger().BaseValue;
var from = intStart < 0 ? Math.Max(s.Length + intStart, 0) : Math.Min(intStart, s.Length);
var to = intStart < 0 ? Math.Max(s.Length + intEnd, 0) : Math.Min(intEnd, s.Length);
var span = Math.Max(to - from, 0);
return environment.CreateString(s.Substring(from, span));
}