本文整理汇总了C#中Microsoft.Scripting.Runtime.CodeContext类的典型用法代码示例。如果您正苦于以下问题:C# CodeContext类的具体用法?C# CodeContext怎么用?C# CodeContext使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
CodeContext类属于Microsoft.Scripting.Runtime命名空间,在下文中一共展示了CodeContext类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: __new__
public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
if (cls == TypeCache.Double) {
if (x is string) {
return ParseFloat((string)x);
} else if (x is Extensible<string>) {
object res;
if (PythonTypeOps.TryInvokeUnaryOperator(context, x, Symbols.ConvertToFloat, out res)) {
return res;
}
return ParseFloat(((Extensible<string>)x).Value);
} else if (x is char) {
return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
}
double doubleVal;
if (Converter.TryConvertToDouble(x, out doubleVal)) return doubleVal;
if (x is Complex64) throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, Symbols.ConvertToFloat));
if (d is double) return d;
throw PythonOps.TypeError("__float__ returned non-float (type %s)", DynamicHelpers.GetPythonType(d));
} else {
return cls.CreateInstance(context, x);
}
}
示例2: DynamicStackFrame
public DynamicStackFrame(CodeContext context, MethodBase method, string funcName, string filename, int line) {
_context = context;
_funcName = funcName;
_filename = filename;
_lineNo = line;
_method = method;
}
示例3: __new__
public static object __new__(CodeContext context, PythonType cls, object x) {
Extensible<string> es;
if (x is string) {
return ReturnBigInteger(context, cls, ParseBigIntegerSign((string)x, 10));
} else if ((es = x as Extensible<string>) != null) {
object value;
if (PythonTypeOps.TryInvokeUnaryOperator(context, x, Symbols.ConvertToLong, out value)) {
return ReturnBigInteger(context, cls, (BigInteger)value);
}
return ReturnBigInteger(context, cls, ParseBigIntegerSign(es.Value, 10));
}
BigInteger intVal;
if (Converter.TryConvertToBigInteger(x, out intVal)) {
if (Object.Equals(intVal, null)) throw PythonOps.TypeError("can't convert {0} to long", PythonTypeOps.GetName(x));
return ReturnBigInteger(context, cls, intVal);
}
if (x is Complex64) throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))");
throw PythonOps.ValueError("long argument must be convertible to long (string, number, or type that defines __long__, got {0})",
StringOps.Quote(PythonOps.GetPythonTypeName(x)));
}
示例4: CodeContext
public CodeContext(Scope scope, LanguageContext languageContext, CodeContext parent) {
Assert.NotNull(languageContext);
_languageContext = languageContext;
_scope = scope;
_parent = parent;
}
示例5: TrySetValue
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
IWeakReferenceable reference;
if (context.GetPythonContext().TryConvertToWeakReferenceable(instance, out reference)) {
return reference.SetWeakRef(new WeakRefTracker(value, instance));
}
return false;
}
示例6: __new__
public static object __new__(CodeContext/*!*/ context, PythonType cls, object x) {
if (cls == TypeCache.Double) {
if (x is string) {
return ParseFloat((string)x);
} else if (x is Extensible<string>) {
object res;
if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__float__", out res)) {
return res;
}
return ParseFloat(((Extensible<string>)x).Value);
} else if (x is char) {
return ParseFloat(ScriptingRuntimeHelpers.CharToString((char)x));
}
if (x is Complex) {
throw PythonOps.TypeError("can't convert complex to float; use abs(z)");
}
object d = PythonOps.CallWithContext(context, PythonOps.GetBoundAttr(context, x, "__float__"));
if (d is double) {
return d;
} else if (d is Extensible<double>) {
return ((Extensible<double>)d).Value;
}
throw PythonOps.TypeError("__float__ returned non-float (type {0})", PythonTypeOps.GetName(d));
} else {
return cls.CreateInstance(context, x);
}
}
示例7: TrySetValue
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
IWeakReferenceable reference = instance as IWeakReferenceable;
if (reference != null) {
return reference.SetWeakRef(new WeakRefTracker(value, instance));
}
return false;
}
示例8: TryDeleteValue
internal override bool TryDeleteValue(CodeContext context, object instance, PythonType owner) {
if (_deleter == null || instance == null) {
return base.TryDeleteValue(context, instance, owner);
}
CallTarget(context, null, new MethodInfo[] { _deleter }, instance);
return true;
}
示例9: TrySetValue
internal override bool TrySetValue(CodeContext context, object instance, PythonType owner, object value) {
if (instance != null) {
Setter(instance, value);
return true;
}
return false;
}
示例10: TryGetValue
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
try {
value = PythonOps.GetUserDescriptor(Value, instance, owner);
return true;
} catch (MissingMemberException) {
value = null;
return false;
}
}
示例11: TryGetValue
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (Getter.Length == 0 || instance == null) {
value = null;
return false;
}
value = CallGetter(context, null, instance, ArrayUtils.EmptyObjects);
return true;
}
示例12: TryGetValue
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (Getter.Length == 0 || (instance == null && Getter[0].IsDefined(typeof(WrapperDescriptorAttribute), false))) {
value = null;
return false;
}
value = CallGetter(context, null, instance, ArrayUtils.EmptyObjects);
return true;
}
示例13: __new__
public static object __new__(CodeContext context, PythonType cls, object x) {
Extensible<string> es;
if (x is string) {
return ReturnObject(context, cls, ParseBigIntegerSign((string)x, 10));
} else if ((es = x as Extensible<string>) != null) {
object value;
if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out value)) {
return ReturnObject(context, cls, (BigInteger)value);
}
return ReturnObject(context, cls, ParseBigIntegerSign(es.Value, 10));
}
if (x is double) return ReturnObject(context, cls, DoubleOps.__long__((double)x));
if (x is int) return ReturnObject(context, cls, (BigInteger)(int)x);
if (x is BigInteger) return ReturnObject(context, cls, x);
if (x is Complex) throw PythonOps.TypeError("can't convert complex to long; use long(abs(z))");
if (x is decimal) {
return ReturnObject(context, cls, (BigInteger)(decimal)x);
}
object result;
int intRes;
BigInteger bigintRes;
if (PythonTypeOps.TryInvokeUnaryOperator(context, x, "__long__", out result) &&
!Object.ReferenceEquals(result, NotImplementedType.Value) ||
x is OldInstance &&
PythonTypeOps.TryInvokeUnaryOperator(context, x, "__int__", out result) &&
!Object.ReferenceEquals(result, NotImplementedType.Value)) {
if (result is int || result is BigInteger ||
result is Extensible<int> || result is Extensible<BigInteger>) {
return ReturnObject(context, cls, result);
} else {
throw PythonOps.TypeError("__long__ returned non-long (type {0})", PythonTypeOps.GetOldName(result));
}
} else if (PythonOps.TryGetBoundAttr(context, x, "__trunc__", out result)) {
result = PythonOps.CallWithContext(context, result);
if (Converter.TryConvertToInt32(result, out intRes)) {
return ReturnObject(context, cls, (BigInteger)intRes);
} else if (Converter.TryConvertToBigInteger(result, out bigintRes)) {
return ReturnObject(context, cls, bigintRes);
} else {
throw PythonOps.TypeError("__trunc__ returned non-Integral (type {0})", PythonTypeOps.GetOldName(result));
}
}
if (x is OldInstance) {
throw PythonOps.AttributeError("{0} instance has no attribute '__trunc__'",
((OldInstance)x)._class.Name);
} else {
throw PythonOps.TypeError("long() argument must be a string or a number, not '{0}'",
DynamicHelpers.GetPythonType(x).Name);
}
}
示例14: MakeIsCallableRule
// This can produce a IsCallable rule that returns the immutable constant isCallable.
// Beware that objects can have a mutable callable property. Eg, in Python, assign or delete the __call__ attribute.
public static bool MakeIsCallableRule(CodeContext context, object self, bool isCallable, RuleBuilder rule) {
rule.MakeTest(CompilerHelpers.GetType(self));
rule.Target =
rule.MakeReturn(
context.LanguageContext.Binder,
Ast.Constant(isCallable)
);
return true;
}
示例15: TryGetValue
internal override bool TryGetValue(CodeContext context, object instance, PythonType owner, out object value) {
if (instance != null) {
value = Getter(instance);
PythonOps.CheckInitializedAttribute(value, instance, _name);
return true;
}
value = this;
return true;
}