public static void InitClass() { foreach (ObjString s in Strings) { s.ClassObj = WrenVM.StringClass; } _initCompleted = true; Strings.Clear(); TrueString = new ObjString("true"); FalseString = new ObjString("false"); }
// Returns true if [a] and [b] are equivalent. Immutable values (null, bools, // numbers, ranges, and strings) are equal if they have the same data. All // other values are equal if they are identical objects. public static bool Equals(Obj a, Obj b) { if (a == b) { return(true); } if (a.Type != b.Type) { return(false); } if (a.Type == ObjType.Num) { return(a.Num == b.Num); } // If we get here, it's only possible for two heap-allocated immutable objects // to be equal. if (a.Type != ObjType.Obj) { return(true); } // Must be the same type. if (a.GetType() != b.GetType()) { return(false); } ObjString aString = a as ObjString; if (aString != null) { ObjString bString = (ObjString)b; return(aString.Str.Equals(bString.Str)); } ObjRange aRange = a as ObjRange; if (aRange != null) { ObjRange bRange = (ObjRange)b; return(ObjRange.Equals(aRange, bRange)); } // All other types are only equal if they are same, which they aren't if // we get here. return(false); }
public WrenVM() { MethodNames = new List<string>(); ObjString name = new ObjString("core"); // Implicitly create a "core" module for the built in libraries. ObjModule coreModule = new ObjModule(name); _modules = new ObjMap(); _modules.Set(Obj.Null, coreModule); CoreLibrary core = new CoreLibrary(this); core.InitializeCore(); // Load in System functions Meta.LoadLibrary(this); }
// Resets [fiber] back to an initial state where it is ready to invoke [fn]. private void ResetFiber(Obj fn) { Stack = new Value[InitialStackSize]; Capacity = InitialStackSize; Frames = new List<CallFrame>(); // Push the stack frame for the function. StackTop = 0; NumFrames = 1; OpenUpvalues = null; Caller = null; Error = null; CallerIsTrying = false; CallFrame frame = new CallFrame { fn = fn, StackStart = 0, ip = 0 }; Frames.Add(frame); }
public WrenVM() { MethodNames = new List<string>(); ObjString name = new ObjString("core"); // Implicitly create a "core" module for the built in libraries. ObjModule coreModule = new ObjModule(name); modules = new ObjMap(); modules.Set(new Value (ValueType.Null), new Value(coreModule)); CoreLibrary core = new CoreLibrary(this); core.InitializeCore(); // Load in System functions Library.IO.LoadIOLibrary(this); }
// Creates a new class object as well as its associated metaclass. public ObjClass(ObjClass superclass, int numFields, ObjString name) { Methods = new Method[InitialMethodSize]; Superclass = superclass; NumFields = numFields; Name = name; // Create the metaclass. ObjString metaclassName = new ObjString(name + " metaclass"); ObjClass metaclass = new ObjClass(0, metaclassName) { ClassObj = ClassClass }; // Metaclasses always inherit Class and do not parallel the non-metaclass // hierarchy. metaclass.BindSuperclass(ClassClass); ClassObj = metaclass; BindSuperclass(superclass); }
// TODO: The argument list here is getting a bit gratuitous. // Creates a new function object with the given code and constants. The new // function will take over ownership of [bytecode] and [sourceLines]. It will // copy [constants] into its own array. public ObjFn(ObjModule module, Obj[] constants, int numUpvalues, int arity, byte[] bytecode, ObjString debugSourcePath, string debugName, int[] sourceLines) { Bytecode = bytecode; Constants = constants; Module = module; NumUpvalues = numUpvalues; NumConstants = constants.Length; Arity = arity; /* Debug Information */ SourcePath = debugSourcePath; Name = debugName; SourceLines = sourceLines; ClassObj = WrenVM.FnClass; }
// Creates a new "raw" class. It has no metaclass or superclass whatsoever. // This is only used for bootstrapping the initial Object and Class classes, // which are a little special. public ObjClass(int numFields, ObjString name) { Methods = new Method[InitialMethodSize]; NumFields = numFields; Name = name; }
// Creates either the Object or Class class in the core library with [name]. static ObjClass DefineClass(WrenVM vm, string name) { ObjString nameString = new ObjString(name); ObjClass classObj = new ObjClass(0, nameString); vm.DefineVariable(null, name, classObj); return classObj; }
// The currently defined top-level variables. // The name of the module. // Creates a new module. public ObjModule(ObjString name) { Name = name; Variables = new List<ModuleVariable>(); }
// Puts [fiber] into a runtime failed state because of [error]. // // Returns the fiber that should receive the error or `NULL` if no fiber // caught it. public ObjFiber RuntimeError(Value error) { //ASSERT(fiber->error == NULL, "Can only fail once."); // Store the error in the fiber so it can be accessed later. Error = error.Obj as ObjString; // If the caller ran this fiber using "try", give it the error. if (CallerIsTrying) { // Make the caller's try method return the error message. Caller.SetReturnValue(error); return Caller; } // If we got here, nothing caught the error, so show the stack trace. // TODO: Fix me //DebugPrintStackTrace(fiber); return null; }
// Creates a new module. public ObjModule(ObjString name) { Name = name; Variables = new List <ModuleVariable>(); }