本文整理汇总了C#中JsValue类的典型用法代码示例。如果您正苦于以下问题:C# JsValue类的具体用法?C# JsValue怎么用?C# JsValue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
JsValue类属于命名空间,在下文中一共展示了JsValue类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Construct
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-15.2.2.1
/// </summary>
/// <param name="arguments"></param>
/// <returns></returns>
public ObjectInstance Construct(JsValue[] arguments)
{
if (arguments.Length > 0)
{
var value = arguments[0];
var valueObj = value.TryCast<ObjectInstance>();
if (valueObj != null)
{
return valueObj;
}
var type = value.Type;
if (type == Types.String || type == Types.Number || type == Types.Boolean)
{
return TypeConverter.ToObject(_engine, value);
}
}
var obj = new ObjectInstance(_engine)
{
Extensible = true,
Prototype = Engine.Object.PrototypeObject
};
return obj;
}
示例2: Invoke
public object Invoke(DispatchIdentifier id, DispatchType dispType, JsValue[] args, out Type returnType)
{
if (id.Tag == DispatchIdentifierType.String) {
if (id.AsString == "length") {
return GetLength(dispType, out returnType);
}
else {
returnType = typeof(void);
return null;
}
}
else {
int intId = id.AsInt;
if (intId == this.target.Length) {
return GetLength(dispType, out returnType);
}
if (intId >= 0 && intId < this.target.Length) {
Debug.WriteLine(string.Format("{0}, {1}[{2}]", dispType, this.target, id));
if (dispType == DispatchType.PropertyGet) {
returnType = this.elementType;
return this.target.GetValue(intId);
}
if (dispType == DispatchType.PropertySet) {
object value = this.bridge.UnwrapValue(args.First(), this.elementType);
this.target.SetValue(value, intId);
returnType = typeof(void);
return null;
}
}
}
throw new NotSupportedException();
}
示例3: Call
public JsValue Call(JsValue thisObject, JsValue[] arguments)
{
// direct calls on a NamespaceReference constructor object is creating a generic type
var genericTypes = new Type[arguments.Length];
for (int i = 0; i < arguments.Length; i++)
{
var genericTypeReference = arguments.At(i);
if (genericTypeReference == Undefined.Instance || !genericTypeReference.IsObject() || genericTypeReference.AsObject().Class != "TypeReference")
{
throw new JavaScriptException(Engine.TypeError, "Invalid generic type parameter");
}
genericTypes[i] = arguments.At(0).As<TypeReference>().Type;
}
var typeReference = GetPath(_path + "`" + arguments.Length.ToString(CultureInfo.InvariantCulture)).As<TypeReference>();
if (typeReference == null)
{
return Undefined.Instance;
}
var genericType = typeReference.Type.MakeGenericType(genericTypes);
return TypeReference.CreateTypeReference(Engine, genericType);
}
示例4: Stringify
public JsValue Stringify(JsValue thisObject, JsValue[] arguments)
{
JsValue
value = Undefined.Instance,
replacer = Undefined.Instance,
space = Undefined.Instance;
if (arguments.Length > 2)
{
space = arguments[2];
}
if (arguments.Length > 1)
{
replacer = arguments[1];
}
if (arguments.Length > 0)
{
value = arguments[0];
}
var serializer = new JsonSerializer(_engine);
if (value == Undefined.Instance && replacer == Undefined.Instance) {
return Undefined.Instance;
}
else {
return serializer.Serialize(value, replacer, space);
}
}
示例5: IsPrototypeOf
private JsValue IsPrototypeOf(JsValue thisObject, JsValue[] arguments)
{
var arg = arguments[0];
if (!arg.IsObject())
{
return false;
}
var v = arg.AsObject();
var o = TypeConverter.ToObject(Engine, thisObject);
while (true)
{
v = v.Prototype;
if (v == null)
{
return false;
}
if (o == v)
{
return true;
}
}
}
示例6: Bind
private JsValue Bind(JsValue thisObj, JsValue[] arguments)
{
var target = thisObj.TryCast<ICallable>(x =>
{
throw new JavaScriptException(Engine.TypeError);
});
var thisArg = arguments.At(0);
var f = new BindFunctionInstance(Engine) {Extensible = true};
f.TargetFunction = thisObj;
f.BoundThis = thisArg;
f.BoundArgs = arguments.Skip(1).ToArray();
f.Prototype = Engine.Function.PrototypeObject;
var o = target as FunctionInstance;
if (o != null)
{
var l = TypeConverter.ToNumber(o.Get("length")) - (arguments.Length - 1);
f.FastAddProperty("length", System.Math.Max(l, 0), false, false, false);
}
else
{
f.FastAddProperty("length", 0, false, false, false);
}
var thrower = Engine.Function.ThrowTypeError;
f.DefineOwnProperty("caller", new PropertyDescriptor(thrower, thrower, false, false), false);
f.DefineOwnProperty("arguments", new PropertyDescriptor(thrower, thrower, false, false), false);
return f;
}
示例7: Put
public override void Put(string propertyName, JsValue value, bool throwOnError)
{
if (!CanPut(propertyName))
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError);
}
return;
}
var ownDesc = GetOwnProperty(propertyName);
if (ownDesc == null)
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError, "Unknown member: " + propertyName);
}
else
{
return;
}
}
ownDesc.Value = value;
}
示例8: ToString
public JsValue ToString(JsValue thisObject, JsValue[] arguments)
{
var o = thisObject.TryCast<ObjectInstance>();
if (o == null)
{
throw new JavaScriptException(Engine.TypeError);
}
var name = TypeConverter.ToString(o.Get("name"));
var msgProp = o.Get("message");
string msg;
if (msgProp == Undefined.Instance)
{
msg = "";
}
else
{
msg = TypeConverter.ToString(msgProp);
}
if (name == "")
{
return msg;
}
if (msg == "")
{
return name;
}
return name + ": " + msg;
}
示例9: Put
/// Implementation from ObjectInstance official specs as the one
/// in ObjectInstance is optimized for the general case and wouldn't work
/// for arrays
public override void Put(string propertyName, JsValue value, bool throwOnError)
{
if (!CanPut(propertyName))
{
if (throwOnError)
{
throw new JavaScriptException(Engine.TypeError);
}
return;
}
var ownDesc = GetOwnProperty(propertyName);
if (ownDesc.IsDataDescriptor())
{
var valueDesc = new PropertyDescriptor(value: value, writable: null, enumerable: null, configurable: null);
DefineOwnProperty(propertyName, valueDesc, throwOnError);
return;
}
// property is an accessor or inherited
var desc = GetProperty(propertyName);
if (desc.IsAccessorDescriptor())
{
var setter = desc.Set.Value.TryCast<ICallable>();
setter.Call(new JsValue(this), new[] { value });
}
else
{
var newDesc = new PropertyDescriptor(value, true, true, true);
DefineOwnProperty(propertyName, newDesc, throwOnError);
}
}
示例10: HasInstance
public virtual bool HasInstance(JsValue v)
{
var vObj = v.TryCast<ObjectInstance>();
if (vObj == null)
{
return false;
}
var po = Get("prototype");
if (!po.IsObject())
{
throw new JavaScriptException(_engine.TypeError, string.Format("Function has non-object prototype '{0}' in instanceof check", TypeConverter.ToString(po)));
}
var o = po.AsObject();
if (o == null)
{
throw new JavaScriptException(_engine.TypeError);
}
while (true)
{
vObj = vObj.Prototype;
if (vObj == null)
{
return false;
}
if (vObj == o)
{
return true;
}
}
}
示例11: Invoke
public object Invoke(DispatchIdentifier id, DispatchType dispType, JsValue[] jsArgs, out Type returnType)
{
MemberInfo member = GetMember(id.AsString);
if (member == null) {
returnType = typeof(void);
return null;
}
Debug.WriteLine(string.Format("{0}, {1}.{2}", dispType, this.target, member.Name));
if (member is MethodInfo && dispType == DispatchType.PropertyGet) {
var method = (MethodInfo)member;
return GetMethodAsProperty((MethodInfo)member, this.target, out returnType);
}
var args = this.bridge.UnwrapParameters(jsArgs, dispType, member);
if (dispType.IsMethod()) {
var method = (MethodBase)member;
returnType = method.IsConstructor ? method.DeclaringType : ((MethodInfo)method).ReturnType;
return method.Invoke(this.target, args);
}
if (dispType.IsPropertyGet()) {
var pi = (PropertyInfo)member;
returnType = pi.PropertyType;
return pi.GetValue(this.target, null);
}
if (dispType.IsPropertyPut()) {
var pi = (PropertyInfo)member;
pi.SetValue(this.target, args.First(), null);
returnType = typeof(void);
return null;
}
throw new NotSupportedException();
}
示例12: Parse
private JsValue Parse(JsValue thisObj, JsValue[] arguments)
{
DateTime result;
var date = TypeConverter.ToString(arguments.At(0));
if (!DateTime.TryParseExact(date, new[]
{
"yyyy-MM-ddTHH:mm:ss.FFF",
"yyyy-MM-ddTHH:mm:ss",
"yyyy-MM-ddTHH:mm",
"yyyy-MM-dd",
"yyyy-MM",
"yyyy"
}, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal | DateTimeStyles.AssumeUniversal, out result))
{
if (!DateTime.TryParseExact(date, new[]
{
// Formats used in DatePrototype toString methods
"ddd MMM dd yyyy HH:mm:ss 'GMT'K",
"ddd MMM dd yyyy",
"HH:mm:ss 'GMT'K",
// standard formats
"yyyy-M-dTH:m:s.FFFK",
"yyyy/M/dTH:m:s.FFFK",
"yyyy-M-dTH:m:sK",
"yyyy/M/dTH:m:sK",
"yyyy-M-dTH:mK",
"yyyy/M/dTH:mK",
"yyyy-M-d H:m:s.FFFK",
"yyyy/M/d H:m:s.FFFK",
"yyyy-M-d H:m:sK",
"yyyy/M/d H:m:sK",
"yyyy-M-d H:mK",
"yyyy/M/d H:mK",
"yyyy-M-dK",
"yyyy/M/dK",
"yyyy-MK",
"yyyy/MK",
"yyyyK",
"THH:mm:ss.FFFK",
"THH:mm:ssK",
"THH:mmK",
"THHK"
}, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
{
if (!DateTime.TryParse(date, Engine.Options.GetCulture(), DateTimeStyles.AdjustToUniversal, out result))
{
if (!DateTime.TryParse(date, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out result))
{
// unrecognized dates should return NaN (15.9.4.2)
return double.NaN;
}
}
}
}
return FromDateTime(result);
}
示例13: Call
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-13.2.1
/// </summary>
/// <param name="thisArg"></param>
/// <param name="arguments"></param>
/// <returns></returns>
public override JsValue Call(JsValue thisArg, JsValue[] arguments)
{
using (new StrictModeScope(Strict, true))
{
// setup new execution context http://www.ecma-international.org/ecma-262/5.1/#sec-10.4.3
JsValue thisBinding;
if (StrictModeScope.IsStrictModeCode)
{
thisBinding = thisArg;
}
else if (thisArg == Undefined.Instance || thisArg == Null.Instance)
{
thisBinding = Engine.Global;
}
else if (!thisArg.IsObject())
{
thisBinding = TypeConverter.ToObject(Engine, thisArg);
}
else
{
thisBinding = thisArg;
}
var localEnv = LexicalEnvironment.NewDeclarativeEnvironment(Engine, Scope);
Engine.EnterExecutionContext(localEnv, localEnv, thisBinding);
try
{
Engine.DeclarationBindingInstantiation(
DeclarationBindingType.FunctionCode,
_functionDeclaration.FunctionDeclarations,
_functionDeclaration.VariableDeclarations,
this,
arguments);
var result = Engine.ExecuteStatement(_functionDeclaration.Body);
if (result.Type == Completion.Throw)
{
JavaScriptException ex = new JavaScriptException(result.GetValueOrDefault());
ex.Location = result.Location;
throw ex;
}
if (result.Type == Completion.Return)
{
return result.GetValueOrDefault();
}
}
finally
{
Engine.LeaveExecutionContext();
}
return Undefined.Instance;
}
}
示例14: ParseInt
/// <summary>
/// http://www.ecma-international.org/ecma-262/5.1/#sec-15.1.2.2
/// </summary>
public static JsValue ParseInt(JsValue thisObject, JsValue[] arguments)
{
string inputString = TypeConverter.ToString(arguments.At(0));
var s = inputString.Trim();
var sign = 1;
if (!System.String.IsNullOrEmpty(s))
{
if (s[0] == '-')
{
sign = -1;
}
if (s[0] == '-' || s[0] == '+')
{
s = s.Substring(1);
}
}
var stripPrefix = true;
int radix = arguments.Length > 1 ? TypeConverter.ToInt32(arguments[1]) : 0;
if (radix == 0)
{
if (s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
{
radix = 16;
}
else
{
radix = 10;
}
}
else if (radix < 2 || radix > 36)
{
return double.NaN;
}
else if(radix != 16)
{
stripPrefix = false;
}
if (stripPrefix && s.Length >= 2 && s.StartsWith("0x") || s.StartsWith("0X"))
{
s = s.Substring(2);
}
try
{
return sign * Parse(s, radix).AsNumber();
}
catch
{
return double.NaN;
}
}
示例15: CreateArgumentsObject
public static ArgumentsInstance CreateArgumentsObject(Engine engine, FunctionInstance func, string[] names, JsValue[] args, EnvironmentRecord env, bool strict)
{
var obj = new ArgumentsInstance(engine, self =>
{
var len = args.Length;
self.FastAddProperty("length", len, true, false, true);
var map = engine.Object.Construct(Arguments.Empty);
var mappedNamed = new List<string>();
var indx = 0;
while (indx <= len - 1)
{
var indxStr = TypeConverter.ToString(indx);
var val = args[indx];
self.FastAddProperty(indxStr, val, true, true, true);
if (indx < names.Length)
{
var name = names[indx];
if (!strict && !mappedNamed.Contains(name))
{
mappedNamed.Add(name);
Func<JsValue, JsValue> g = n => env.GetBindingValue(name, false);
var p = new Action<JsValue, JsValue>((n, o) => env.SetMutableBinding(name, o, true));
map.DefineOwnProperty(indxStr, new ClrAccessDescriptor(engine, g, p) { Configurable = true }, false);
}
}
indx++;
}
// step 12
if (mappedNamed.Count > 0)
{
self.ParameterMap = map;
}
// step 13
if (!strict)
{
self.FastAddProperty("callee", func, true, false, true);
}
// step 14
else
{
var thrower = engine.Function.ThrowTypeError;
self.DefineOwnProperty("caller", new PropertyDescriptor(get: thrower, set: thrower, enumerable: false, configurable: false), false);
self.DefineOwnProperty("callee", new PropertyDescriptor(get: thrower, set: thrower, enumerable: false, configurable: false), false);
}
});
// These properties are pre-initialized as their don't trigger
// the EnsureInitialized() event and are cheap
obj.Prototype = engine.Object.PrototypeObject;
obj.Extensible = true;
obj.Strict = strict;
return obj;
}