本文整理汇总了C#中Rakudo.Metamodel.RakudoObject类的典型用法代码示例。如果您正苦于以下问题:C# RakudoObject类的具体用法?C# RakudoObject怎么用?C# RakudoObject使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RakudoObject类属于Rakudo.Metamodel命名空间,在下文中一共展示了RakudoObject类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: box_str
/// <summary>
/// Boxes a native string into its matching value type.
/// </summary>
/// <param name="Value"></param>
/// <returns></returns>
public static RakudoObject box_str(ThreadContext TC, string Value, RakudoObject To)
{
var REPR = To.STable.REPR;
var Result = REPR.instance_of(TC, To);
REPR.set_str(TC, Result, Value);
return Result;
}
示例2: lllist_bind_at_pos
/// <summary>
/// Binds a value at a given positional index from a low level list
/// (something that uses the P6list representation).
/// </summary>
/// <param name="TC"></param>
/// <param name="LLList"></param>
/// <param name="Index"></param>
/// <returns></returns>
public static RakudoObject lllist_bind_at_pos(ThreadContext TC, RakudoObject LLList, RakudoObject IndexObj, RakudoObject Value)
{
if (LLList is P6list.Instance)
{
var Storage = ((P6list.Instance)LLList).Storage;
var Index = Ops.unbox_int(TC, IndexObj);
if (Index < Storage.Count)
{
Storage[Index] = Value;
}
else
{
// XXX Need some more efficient resizable array approach...
// Also this is no way thread safe.
while (Index > Storage.Count)
Storage.Add(null);
Storage.Add(Value);
}
return Value;
}
else
{
throw new Exception("Cannot use lllist_bind_at_pos if representation is not P6list");
}
}
示例3: FormWith
/// <summary>
/// Forms a capture from the provided positional and named arguments.
/// </summary>
/// <param name="PosArgs"></param>
/// <param name="NamedArgs"></param>
/// <returns></returns>
public static RakudoObject FormWith(RakudoObject[] PosArgs, Dictionary<string, RakudoObject> NamedArgs)
{
var C = (P6capture.Instance)CaptureTypeObject.STable.REPR.instance_of(null, CaptureTypeObject);
C.Positionals = PosArgs;
C.Nameds = NamedArgs;
return C;
}
示例4: create_dispatch_and_add_candidates
/// <summary>
/// Creates an instantiation of the dispatch routine (or proto, which may
/// serve as one) supplied and augments it with the provided candidates.
/// It relies on being passed the instantiation of the dispatcher from the
/// last outer scope that had an instantiation, and we thus take its
/// candidates. This may or may not hold up in the long run; it works out
/// in the Perl 6-y "you can make a new instance from any object" sense
/// though, and seems more likely to get the closure semantics right than
/// any of the other approaches I've considered so far.
/// </summary>
/// <param name="TC"></param>
/// <param name="ToInstantiate"></param>
/// <param name="ExtraDispatchees"></param>
/// <returns></returns>
public static RakudoObject create_dispatch_and_add_candidates(ThreadContext TC, RakudoObject ToInstantiate, RakudoObject ExtraDispatchees)
{
// Make sure we got the right things.
var Source = ToInstantiate as RakudoCodeRef.Instance;
var AdditionalDispatchList = ExtraDispatchees as P6list.Instance;
if (Source == null || AdditionalDispatchList == null)
throw new Exception("create_dispatch_and_add_candidates expects a RakudoCodeRef and a P6list");
// Clone all but SC (since it's a new object and doesn't live in any
// SC yet) and dispatchees (which we want to munge).
var NewDispatch = new RakudoCodeRef.Instance(Source.STable);
NewDispatch.Body = Source.Body;
NewDispatch.CurrentContext = Source.CurrentContext;
NewDispatch.Handlers = Source.Handlers;
NewDispatch.OuterBlock = Source.OuterBlock;
NewDispatch.OuterForNextInvocation = Source.OuterForNextInvocation;
NewDispatch.Sig = Source.Sig;
NewDispatch.StaticLexPad = Source.StaticLexPad;
// Take existing candidates and add new ones.
NewDispatch.Dispatchees = new RakudoObject[Source.Dispatchees.Length + AdditionalDispatchList.Storage.Count];
var i = 0;
for (int j = 0; j < Source.Dispatchees.Length; j++)
NewDispatch.Dispatchees[i++] = Source.Dispatchees[j];
for (int j = 0; j < AdditionalDispatchList.Storage.Count; j++)
NewDispatch.Dispatchees[i++] = AdditionalDispatchList.Storage[j];
return NewDispatch;
}
示例5: Add
/// <summary>
/// Adds the given result to the dispatch cache for the provided
/// positional arguments.
/// </summary>
/// <param name="Positionals"></param>
/// <param name="Result"></param>
public void Add(RakudoObject[] Positionals, RakudoObject Result)
{
// Don't cache things with excessive arity.
if (Positionals.Length <= MAX_ARITY)
{
// Compute the type cache ID tuple.
var ToAdd = PositionalsToTypeCacheIDs(Positionals);
// Snapshot the previous arity cache.
var Previous = ArityCaches[Positionals.Length];
// Build a new one.
var New = new ArityCache();
if (Previous == null)
{
// First time. We go in slot 0.
New.NumEntries = 1;
New.Results = new RakudoObject[MAX_ENTRIES + 1];
New.TypeIDs = new long[MAX_ENTRIES * Positionals.Length];
for (int i = 0; i < ToAdd.Length; i++)
New.TypeIDs[i] = ToAdd[i];
New.Results[0] = Result;
}
else
{
// Copy existing entries.
New.NumEntries = Previous.NumEntries;
New.TypeIDs = (long[])Previous.TypeIDs.Clone();
New.Results = (RakudoObject[])Previous.Results.Clone();
// Space for the new one?
if (New.NumEntries <= MAX_ENTRIES)
{
// We can go on the end.
int i, j;
for (i = 0, j = New.NumEntries * ToAdd.Length; i < ToAdd.Length; i++, j++)
New.TypeIDs[j] = ToAdd[i];
New.Results[New.NumEntries] = Result;
New.NumEntries++;
}
else
{
// Pick a victim.
var Evictee = new Random().Next(MAX_ENTRIES + 1);
int i, j;
for (i = 0, j = Evictee * ToAdd.Length; i < ToAdd.Length; i++, j++)
New.TypeIDs[j] = ToAdd[i];
New.Results[Evictee] = Result;
}
}
// Pop it in place, if nothing beat us to it. Otherwise,
// we let whatever slipped in first win. (We may find it is
// also beneficial to loop here and try to add this entry
// again, but may be too much churn, and if it a given combination
// is called a lot, it'll make it in at some point.)
Interlocked.CompareExchange<ArityCache>(ref ArityCaches[ToAdd.Length], New, Previous);
}
}
示例6: is_dispatcher
/// <summary>
/// Checks if a routine is considered a dispatcher (that is, if it has a
/// candidate list).
/// </summary>
/// <param name="TC"></param>
/// <param name="Check"></param>
/// <returns></returns>
public static RakudoObject is_dispatcher(ThreadContext TC, RakudoObject Check)
{
var Checkee = Check as RakudoCodeRef.Instance;
if (Checkee != null && Checkee.Dispatchees != null)
return Ops.box_int(TC, 1, TC.DefaultBoolBoxType);
else
return Ops.box_int(TC, 0, TC.DefaultBoolBoxType);
}
示例7: Context
/// <summary>
/// Initializes the context.
/// </summary>
/// <param name="StaticCodeObject"></param>
/// <param name="Caller"></param>
public Context(RakudoObject StaticCodeObject_Uncast, Context Caller, RakudoObject Capture)
{
// Set up static code object and caller pointers.
var StaticCodeObject = (RakudoCodeRef.Instance)StaticCodeObject_Uncast;
this.StaticCodeObject = StaticCodeObject;
this.Caller = Caller;
this.Capture = Capture;
// Static sub object should have this as the current
// context.
StaticCodeObject.CurrentContext = this;
// Lex pad should be an "instantiation" of the static one.
// Instantiating a lexpad creates a new dynamic instance of it
// from a static one, copying over the slot storage entries
// from the static one but sharing the slot mapping.
this.LexPad.SlotMapping = StaticCodeObject.StaticLexPad.SlotMapping;
this.LexPad.Storage = (RakudoObject[])StaticCodeObject.StaticLexPad.Storage.Clone();
// Set outer context.
if (StaticCodeObject.OuterForNextInvocation != null)
{
this.Outer = StaticCodeObject.OuterForNextInvocation;
}
else if (StaticCodeObject.OuterBlock.CurrentContext != null)
{
this.Outer = StaticCodeObject.OuterBlock.CurrentContext;
}
else
{
// Auto-close. In this we go setting up fake contexts
// that use the static lexpad until we find a real one.
var CurContext = this;
var OuterBlock = StaticCodeObject.OuterBlock;
while (OuterBlock != null)
{
// If we found a block with a context, we're done.
if (OuterBlock.CurrentContext != null)
{
CurContext.Outer = OuterBlock.CurrentContext;
break;
}
// Build the fake context.
var OuterContext = new Context();
OuterContext.StaticCodeObject = OuterBlock;
OuterContext.LexPad = OuterBlock.StaticLexPad;
// Link it.
CurContext.Outer = OuterContext;
// Step back one level.
CurContext = OuterContext;
OuterBlock = OuterBlock.OuterBlock;
}
}
}
示例8: lllist_get_at_pos
/// <summary>
/// Gets a value at a given positional index from a low level list
/// (something that uses the P6list representation).
/// </summary>
/// <param name="TC"></param>
/// <param name="LLList"></param>
/// <param name="Index"></param>
/// <returns></returns>
public static RakudoObject lllist_get_at_pos(ThreadContext TC, RakudoObject LLList, RakudoObject Index)
{
if (LLList is P6list.Instance)
{
return ((P6list.Instance)LLList).Storage[Ops.unbox_int(TC, Index)];
}
else
{
throw new Exception("Cannot use lllist_get_at_pos if representation is not P6list");
}
}
示例9: lllist_elems
/// <summary>
/// Gets the number of elements in a low level list (something that
/// uses the P6list representation).
/// </summary>
/// <param name="TC"></param>
/// <param name="LLList"></param>
/// <returns></returns>
public static RakudoObject lllist_elems(ThreadContext TC, RakudoObject LLList)
{
if (LLList is P6list.Instance)
{
return Ops.box_int(TC, ((P6list.Instance)LLList).Storage.Count, TC.DefaultIntBoxType);
}
else
{
throw new Exception("Cannot use lllist_elems if representation is not P6list");
}
}
示例10: load_module
/// <summary>
/// Loads a module (that is, some pre-compiled compilation unit that
/// was compiled using NQP). Expects the path minus an extension
/// (that is, the .dll will be added). Returns what the body of the
/// compilation unit evaluated to.
/// </summary>
/// <param name="TC"></param>
/// <param name="Path"></param>
/// <returns></returns>
public static RakudoObject load_module(ThreadContext TC, RakudoObject Path)
{
// Load the assembly and grab the first type in it.
var Assembly = AppDomain.CurrentDomain.Load(Ops.unbox_str(TC, Path));
var Class = Assembly.GetTypes()[0];
// Call the Load method, passing along the current thread context
// and the setting to use with it. What's returned is what the main
// body of the compilation unit evaluates to.
var Method = Class.GetMethod("Load", BindingFlags.NonPublic | BindingFlags.Static);
return (RakudoObject)Method.Invoke(null, new object[] { TC, TC.Domain.Setting });
}
示例11: LoadSetting
/// <summary>
/// Loads the setting with the given name.
/// </summary>
/// <param name="Name"></param>
/// <param name="KnowHOW"></param>
/// <returns></returns>
public static Context LoadSetting(string Name, RakudoObject KnowHOW, RakudoObject KnowHOWAttribute)
{
// Load the assembly.
var SettingAssembly = AppDomain.CurrentDomain.Load(Name);
// Find the setting type and its LoadSetting method.
var Class = SettingAssembly.GetType("NQPSetting");
var Method = Class.GetMethod("LoadSetting", BindingFlags.NonPublic | BindingFlags.Static);
// Run it to get the context we want.
var SettingContext = (Context)Method.Invoke(null, new object[] { });
// Fudge a few more things in.
// XXX Should be able to toss all of these but KnowHOW.
SettingContext.LexPad.Extend(new string[]
{ "KnowHOW", "KnowHOWAttribute", "print", "say", "capture" });
SettingContext.LexPad.SetByName("KnowHOW", KnowHOW);
SettingContext.LexPad.SetByName("KnowHOWAttribute", KnowHOWAttribute);
SettingContext.LexPad.SetByName("print",
CodeObjectUtility.WrapNativeMethod((TC, self, C) =>
{
for (int i = 0; i < CaptureHelper.NumPositionals(C); i++)
{
var Value = CaptureHelper.GetPositional(C, i);
var StrMeth = self.STable.FindMethod(TC, Value, "Str", 0);
var StrVal = StrMeth.STable.Invoke(TC, StrMeth,
CaptureHelper.FormWith( new RakudoObject[] { Value }));
Console.Write(Ops.unbox_str(null, StrVal));
}
return CaptureHelper.Nil();
}));
SettingContext.LexPad.SetByName("say",
CodeObjectUtility.WrapNativeMethod((TC, self, C) =>
{
for (int i = 0; i < CaptureHelper.NumPositionals(C); i++)
{
var Value = CaptureHelper.GetPositional(C, i);
var StrMeth = self.STable.FindMethod(TC, Value, "Str", 0);
var StrVal = StrMeth.STable.Invoke(TC, StrMeth,
CaptureHelper.FormWith( new RakudoObject[] { Value }));
Console.Write(Ops.unbox_str(null, StrVal));
}
Console.WriteLine();
return CaptureHelper.Nil();
}));
SettingContext.LexPad.SetByName("capture", REPRRegistry.get_REPR_by_name("P6capture").type_object_for(null, null));
return SettingContext;
}
示例12: bind_lex
/// <summary>
/// Binds the given value to a lexical variable of the given name.
/// </summary>
/// <param name="TC"></param>
/// <param name="name"></param>
/// <returns></returns>
public static RakudoObject bind_lex(ThreadContext TC, string Name, RakudoObject Value)
{
var CurContext = TC.CurrentContext;
while (CurContext != null)
{
int Index;
if (CurContext.LexPad.SlotMapping.TryGetValue(Name, out Index))
{
CurContext.LexPad.Storage[Index] = Value;
return Value;
}
CurContext = CurContext.Outer;
}
throw new InvalidOperationException("No variable " + Name + " found in the lexical scope");
}
示例13: llmapping_get_at_key
/// <summary>
/// Gets a value at a given key from a low level mapping (something that
/// uses the P6mapping representation).
/// </summary>
/// <param name="TC"></param>
/// <param name="LLMapping"></param>
/// <param name="Key"></param>
/// <returns></returns>
public static RakudoObject llmapping_get_at_key(ThreadContext TC, RakudoObject LLMapping, RakudoObject Key)
{
if (LLMapping is P6mapping.Instance)
{
var Storage = ((P6mapping.Instance)LLMapping).Storage;
var StrKey = Ops.unbox_str(TC, Key);
if (Storage.ContainsKey(StrKey))
return Storage[StrKey];
else
return null;
}
else
{
throw new Exception("Cannot use llmapping_get_at_key if representation is not P6mapping");
}
}
示例14: DieFromUnhandledException
/// <summary>
/// Dies from an unhandled exception.
/// </summary>
/// <param name="Exception"></param>
public static void DieFromUnhandledException(ThreadContext TC, RakudoObject Exception)
{
// Try to stringify the exception object.
try
{
var StrMeth = Exception.STable.FindMethod(TC, Exception, "Str", Hints.NO_HINT);
var Stringified = StrMeth.STable.Invoke(TC, StrMeth, CaptureHelper.FormWith(new RakudoObject[] { Exception }));
Console.WriteLine(Ops.unbox_str(TC, Stringified));
}
catch
{
Console.Error.WriteLine("Died from an exception, and died trying to stringify it too.");
}
// Exit with an error code.
Environment.Exit(1);
}
示例15: llmapping_bind_at_key
/// <summary>
/// Binds a value at a given key from a low level mapping (something that
/// uses the P6mapping representation).
/// </summary>
/// <param name="TC"></param>
/// <param name="LLMapping"></param>
/// <param name="Key"></param>
/// <param name="Value"></param>
/// <returns></returns>
public static RakudoObject llmapping_bind_at_key(ThreadContext TC, RakudoObject LLMapping, RakudoObject Key, RakudoObject Value)
{
if (LLMapping is P6mapping.Instance)
{
var Storage = ((P6mapping.Instance)LLMapping).Storage;
var StrKey = Ops.unbox_str(TC, Key);
if (Storage.ContainsKey(StrKey))
Storage[StrKey] = Value;
else
Storage.Add(StrKey, Value);
return Value;
}
else
{
throw new Exception("Cannot use llmapping_bind_at_key if representation is not P6mapping");
}
}