// Call this after creating the class and adding members to it. public bool FinalizeClass(ExecContext context) { Pb.Assert(0 == context.control.flags); // Only initializing the members that this class has added is a cool idea, but // leaves us f****d when it comes to overrridden functions. // So, I'm being lazy here and initializing them all. for (int ii = 0; ii < _memberFuncs.Count; ++ii) { ClassMember member = _memberFuncs.Get(ii); if (null != member.initializer && (member.initializer is Expr_Literal || member.initializer is Expr_Value)) { // Make sure vftableVars has a slot for this function. while (vftableVars.Count < ii + 1) { vftableVars.Add(null); } object initValue = member.initializer.Evaluate(context); if (context.IsRuntimeErrorSet()) { return(false); } // Create the variable for the function. vftableVars[ii] = new Variable(member.name, member.typeDef, initValue); } } // Initialize the static members. Populates the staticVars list. for (int ii = 0; ii < _statics.Count; ++ii) { ClassMember member = _statics.Get(ii); object initValue = null; if (null != member.initializer) { initValue = member.initializer.Evaluate(context); if (context.IsRuntimeErrorSet()) { context.engine.LogCompileError(ParseErrorType.StaticMemberEvaluationError, name + "::" + member.name + " - " + context.GetRuntimeErrorString()); return(false); } } staticVars[ii] = new Variable(member.name, member.typeDef, initValue); } return(true); }
public object EvaluateValues(ExecContext context) { bool isNum = valueType.CanStoreValue(context, IntrinsicTypeDefs.NUMBER); double nextInteger = 0; bool isString = valueType.CanStoreValue(context, IntrinsicTypeDefs.STRING); for (int ii = 0; ii < _values.Count; ++ii) { ClassValue_Enum val = (ClassValue_Enum)_classDef.Allocate(context); val.Get(mrName).value = _values[ii].name; _classDef.staticVars[ii].value = val; if (null == _values[ii].initializer) { if (isNum) { // If number, use next consecutive integer. val.Get(mrValue).value = nextInteger; nextInteger = Math.Floor(nextInteger + 1); } else if (isString) { // If string, just use name. val.Get(mrValue).value = _values[ii].name; } else { // Use the default value. val.Get(mrValue).value = valueType.GetDefaultValue(context); } } else { object init = _values[ii].initializer.Evaluate(context); if (context.IsRuntimeErrorSet()) { return(null); } val.Get(mrValue).value = init; if (isNum) { nextInteger = Math.Floor((double)init + 1); } } } return(false); }
private ScriptResult _EvaluateExpression(IExpr expr, bool createTempScope, bool hardTerminal = false) { if (null == expr) { LogError("Cannot evaluate null expression."); return(null); } StackState stackState = defaultContext.stack.GetState(); if (createTempScope) { if (!defaultContext.stack.PushTerminalScope("<Evaluate>", defaultContext, hardTerminal)) { LogError("_EvaluateExpression: stack overflow"); return(null); } } ScriptResult result = new ScriptResult(); result.value = expr.Evaluate(defaultContext); if (defaultContext.IsRuntimeErrorSet()) { defaultContext.stack.RestoreState(stackState); if (logCompileErrors) { LogError(defaultContext.GetRuntimeErrorString()); } result.runtimeError = defaultContext.control.runtimeError; result.success = false; defaultContext.control.Clear(); } else { result.success = true; if (createTempScope) { defaultContext.stack.RestoreState(stackState); defaultContext.control.Clear(); } } return(result); }
// Create a new instance of the class. public ClassValue Allocate(ExecContext context) { ClassValue result = childAllocator(); result.classDef = this; result.debugName = name + " Inst"; bool scopePushed = false; for (int ii = 0; ii < _fields.Count; ++ii) { ClassMember member = _fields.Get(ii); object value = null; if (null != member.initializer) { if (!scopePushed) { scopePushed = true; if (!context.stack.PushClassScope(result, context)) { context.SetRuntimeError(RuntimeErrorType.StackOverflow, "ClassValue.Allocate - stack overflow."); return(null); } } value = member.initializer.Evaluate(context); } else if (!member.typeDef.IsReference()) // don't instantiate class refs automatically { value = member.typeDef.GetDefaultValue(context); } Variable newVar = new Variable(member.name, member.typeDef, value); result.fieldVars.Add(newVar); } if (null != constructor) { if (context.IsRuntimeErrorSet()) { return(null); } if (!scopePushed) { scopePushed = true; if (!context.stack.PushClassScope(result, context)) { context.SetRuntimeError(RuntimeErrorType.StackOverflow, "ClassValue.Allocate - stack overflow."); return(null); } } constructor.Evaluate(context); if (context.IsRuntimeErrorSet()) { return(null); } } if (scopePushed) { context.stack.PopScope(); } return(result); }
public bool Write(ExecContext context, PebbleStreamHelper stream, object value) { Pb.Assert(!(value is Variable)); if (null != textWriter) { string s = CoreLib.ValueToString(context, value, false); textWriter.Write(s); return(true); } if (null == value) { writer.Write("null"); } else if (value is FunctionValue) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Cannot serialize functions."); return(false); } else if (value is bool) { writer.Write((bool)value); } else if (value is double) { writer.Write((double)value); } else if (value is string) { writer.Write((string)value); } else if (value is PebbleList) { PebbleList plist = value as PebbleList; // - Serialize full type, ie "List<string>". writer.Write(plist.classDef.name); // - Serialize count. writer.Write(plist.list.Count); // - Finally, serialize each object. foreach (Variable listvar in plist.list) { if (!Write(context, stream, listvar.value)) { return(false); } } } else if (value is PebbleDictionary) { PebbleDictionary dic = value as PebbleDictionary; // - class name writer.Write(dic.classDef.name); // - count writer.Write((Int32)dic.dictionary.Count); // - each key, value foreach (var kvp in dic.dictionary) { if (!Write(context, stream, kvp.Key)) { return(false); } if (!Write(context, stream, kvp.Value.value)) { return(false); } } } else if (value is ClassValue_Enum) { ClassValue_Enum enumVal = value as ClassValue_Enum; writer.Write(enumVal.classDef.name); writer.Write(enumVal.GetName()); } else if (value is ClassValue) { ClassValue classVal = value as ClassValue; MemberRef serMemRef = classVal.classDef.GetMemberRef(null, "Serialize", ClassDef.SEARCH.NORMAL); if (serMemRef.isInvalid) { context.SetRuntimeError(RuntimeErrorType.SerializeInvalidClass, "Class '" + classVal.classDef.name + "' cannot be serialized because it doesn't implement a serialization function."); return(false); } writer.Write(classVal.classDef.name); Variable serVar = classVal.Get(serMemRef); FunctionValue serFunc = serVar.value as FunctionValue; object result = serFunc.Evaluate(context, new List <object> { stream }, classVal); if (context.IsRuntimeErrorSet()) { return(false); } if (result is bool && false == (bool)result) { context.SetRuntimeError(RuntimeErrorType.SerializeFailed, "Serialize function of class '" + classVal.classDef.name + "' returned false."); return(false); } } else { throw new Exception("Internal error: Unexpected type of value in stream Write."); } return(true); }
public bool Read(ExecContext context, PebbleStreamHelper stream, Variable variable) { if (variable.type.IsConst()) { context.SetRuntimeError(RuntimeErrorType.SerializeIntoConst, "Cannot serialize into const variables."); return(false); } if (variable.type.CanStoreValue(context, IntrinsicTypeDefs.BOOL)) { variable.value = reader.ReadBoolean(); } else if (variable.type == IntrinsicTypeDefs.NUMBER) { variable.value = reader.ReadDouble(); } else if (variable.type == IntrinsicTypeDefs.STRING) { variable.value = reader.ReadString(); } else if (variable.type.GetName().StartsWith("List<")) { string listTypeName = reader.ReadString(); if ("null" == listTypeName) { variable.value = null; return(true); } // Is it possible that the specific generic class isn't registered yet. if (!EnsureGenericIsRegistered(context, listTypeName)) { return(false); } ClassDef listDef = context.GetClass(listTypeName); if (null == listDef) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Cannot deserialize list type '" + listTypeName + "' because it is unknown."); return(false); } ClassValue listValue = listDef.Allocate(context); PebbleList newlist = listValue as PebbleList; variable.value = listValue; ITypeDef elementType = listDef.typeDef.genericTypes[0]; int count = reader.ReadInt32(); for (int ii = 0; ii < count; ++ii) { Variable newelem = new Variable(null, elementType); if (!Read(context, stream, newelem)) { return(false); } newlist.list.Add(newelem); } } else if (variable.type.GetName().StartsWith("Dictionary<")) { string listTypeName = reader.ReadString(); if ("null" == listTypeName) { variable.value = null; return(true); } // Is it possible that the specific generic class isn't registered yet. if (!EnsureGenericIsRegistered(context, listTypeName)) { return(false); } ClassDef listDef = context.GetClass(listTypeName); if (null == listDef) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Cannot deserialize list type '" + listTypeName + "' because it is unknown."); return(false); } ClassValue listValue = listDef.Allocate(context); PebbleDictionary newlist = listValue as PebbleDictionary; variable.value = listValue; ITypeDef keyType = listDef.typeDef.genericTypes[0]; ITypeDef valueType = listDef.typeDef.genericTypes[1]; int count = reader.ReadInt32(); Variable tempKeyVar = new Variable("tempKeyVar", keyType); for (int ii = 0; ii < count; ++ii) { if (!Read(context, stream, tempKeyVar)) { return(false); } Variable newelem = new Variable(null, valueType); if (!Read(context, stream, newelem)) { return(false); } newlist.dictionary.Add(tempKeyVar.value, newelem); } } else if (variable.type is TypeDef_Enum) { string enumName = reader.ReadString(); string valueName = reader.ReadString(); // This happens. ITypeDef streamedType = context.GetTypeByName(enumName); if (null == streamedType) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Attempt to load saved enum of unknown type '" + enumName + "'."); return(false); } // I can't get this to happen. if (!(streamedType is TypeDef_Enum)) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Type '" + enumName + "' saved as something other than an enum, but attempted to stream into an enum variable."); return(false); } ClassDef enumClassDef = context.GetClass(enumName); Pb.Assert(null != enumClassDef, "Somehow we got a type for an enum but not the def."); ClassDef_Enum enumDef = enumClassDef as ClassDef_Enum; Pb.Assert(null != enumClassDef, "Registered type is enum but def is classdef."); // This happens. ClassValue_Enum cve = enumDef.enumDef.GetValue(valueName); if (null == cve) { context.SetRuntimeError(RuntimeErrorType.EnumValueNotFound, "Enum '" + enumName + "' does not have saved value '" + valueName + "'."); return(false); } variable.value = cve; } else if (variable.type is TypeDef_Class) { TypeDef_Class varType = variable.type as TypeDef_Class; // Get class name. string streamClassName = reader.ReadString(); if ("null" == streamClassName) { variable.value = null; return(true); } ITypeDef streamedType = context.GetTypeByName(streamClassName); if (null == streamedType) { context.SetRuntimeError(RuntimeErrorType.SerializeUnknownType, "Serialized type '" + streamClassName + "' not found."); return(false); } if (!varType.CanStoreValue(context, streamedType)) { context.SetRuntimeError(RuntimeErrorType.SerializeTypeMismatch, "Cannot deserialize a '" + streamClassName + "' into a variable of type '" + varType.GetName() + "'."); return(false); } TypeDef_Class streamedClassType = streamedType as TypeDef_Class; Pb.Assert(null != streamedClassType, "Somehow a streamed type is not a class but *can* be stored in a class type?!"); ClassDef streamedClassDef = context.GetClass(streamClassName); Pb.Assert(null != streamedClassDef, "Somehow we got a type for a class but not the def."); MemberRef serMemRef = streamedClassDef.GetMemberRef(null, "Serialize", ClassDef.SEARCH.NORMAL); if (serMemRef.isInvalid) { context.SetRuntimeError(RuntimeErrorType.SerializeInvalidClass, "Serialize function of class '" + streamClassName + "' not found."); return(false); } ClassValue streamedClassValue = streamedClassDef.Allocate(context); Variable serFuncVar = streamedClassValue.Get(serMemRef); FunctionValue serFuncVal = serFuncVar.value as FunctionValue; serFuncVal.Evaluate(context, new List <object>() { stream }, streamedClassValue); if (context.IsRuntimeErrorSet()) { return(false); } variable.value = streamedClassValue; } else { throw new Exception("Internal error: Unexpected type of value in stream Read."); } return(true); }