} // WrapException // Create a JObject with one property. The property's name // is "id" and its value is the given exception object. public static JObject CreateCatchScope(Exception exception, string id) { object unwrapped; if (exception is WrappedException) unwrapped = ((WrappedException)exception).obj; else unwrapped = exception; JObject obj = new JObject(null, null, "CatchScope"); obj.Put(id, unwrapped); return obj; } // CreateCatchScope
} // WithGet // Set an identifier value, searching one or more "with" scopes. // We first search for id in each object in the withs array, starting // with the last entry in the array. If any object in the array has // a property of the given name, we update that property value. Otherwise, // we call lhs.Put(id, value). public static object WithPut( JObject lhs, string id, object value, params object[] withs ) { for (int i=0; i<withs.Length; i++) if (HasProperty(withs[i], id)) return AssignProperty(withs[i], id, value); return lhs.Put(id, value); } // WithPut
} // DefineBuiltinGlobals // For each public static method of the specified type, create a // function in the given prototype object. private static void RegisterBuiltins(Type impType, JObject proto) { CachedTypeInfo typeInfo = CachedTypeInfo.GetInfoForType(impType, true); IDictionaryEnumerator enumerator = typeInfo.members.GetEnumerator(); while (enumerator.MoveNext()) if (enumerator.Value is MethodInfo) { // Non-overloaded method. Register a function glue // object. JFunctionObject func = new JFunctionStaticGlue( typeInfo, (string) enumerator.Key, enumerator.Value ); proto.Put((string)enumerator.Key, func); } else if (enumerator.Value is MethodBase[]) { // Overloaded method. Register a function glue // object. JFunctionObject func = new JFunctionStaticGlue( typeInfo, (string) enumerator.Key, enumerator.Value ); proto.Put((string)enumerator.Key, func); } } // RegisterBuiltins
} // BoolTest // Create a new object from a literal expression. There should be // an even number of arguments, consisting alternately of property // names and property values for the object. public static JObject LiteralObject(params object[] args) { JObject obj = new JObject(JObject.ObjectPrototype, null, "Object"); for (int i=0; i<args.Length; i += 2) obj.Put((string)(args[i]), args[i+1]); return obj; } // LiteralObject
} // CreateTypeInstance_ // Add a property to the given object for each built-in identifier // that is defined in the global scope. public static void DefineBuiltinGlobals(JObject globals) { // HACK snewman 8/6/01: need to finish the ECMAScript spec. Make // sure that the "length" property is defined for all functions // created here. // HACK snewman 10/3/01: document the "extra" functions we define // that aren't in ECMAScript, such as writeln, GetType, and // CreateTypeInstance. globals.Put("writeln", new JFunctionObject(new JFunctionObject.JFunctionImp(writeln_), null)); globals.Put("eval", new JFunctionObject(new JFunctionObject.JFunctionImp(eval_ ), null)); globals.Put("GetType", new JFunctionObject(new JFunctionObject.JFunctionImp(GetType_), null)); globals.Put("CreateTypeInstance", new JFunctionObject(new JFunctionObject.JFunctionImp(CreateTypeInstance_), null)); // HACK snewman 10/3/01: all of these properties should be dontEnum, // dontDelete. globals.Put("NaN", Double.NaN); globals.Put("Infinity", Double.PositiveInfinity); globals.Put("undefined", JUndefinedObject.instance); RegisterBuiltins(typeof(GlobalMethods), globals); globals.Put("Object", JObject.ObjectConstructorObj); RegisterBuiltins(typeof(ObjectPrototypeMethods), JObject.ObjectPrototype); globals.Put("Boolean", JBooleanObject.BooleanConstructorObj); RegisterBuiltins(typeof(BooleanPrototypeMethods), JBooleanObject.BooleanPrototype); globals.Put("Number", JNumberObject.NumberConstructorObj); RegisterBuiltins(typeof(NumberPrototypeMethods), JNumberObject.NumberPrototype); globals.Put("Array", JArrayObject.ArrayConstructorObj); RegisterBuiltins(typeof(ArrayPrototypeMethods), JArrayObject.ArrayPrototype); globals.Put("String", JStringObject.StringConstructorObj); RegisterBuiltins(typeof(StringConstructorMethods), JStringObject.StringConstructorObj); RegisterBuiltins(typeof(StringPrototypeMethods), JStringObject.StringPrototype); JObject mathObj = new JObject(JObject.ObjectPrototype, null, "Math"); globals.Put("Math", mathObj); RegisterBuiltins(typeof(MathMethods), mathObj); // HACK snewman 10/3/01: all of these properties should be dontEnum, // dontDelete, readOnly. mathObj.Put("E", Math.E ); mathObj.Put("LN10", Math.Log(10) ); mathObj.Put("LN2", Math.Log(2) ); mathObj.Put("LOG2E", 1/Math.Log(2) ); mathObj.Put("LOG10E", 1/Math.Log(10)); mathObj.Put("PI", Math.PI ); mathObj.Put("SQRT1_2", Math.Sqrt(0.5)); mathObj.Put("SQRT2", Math.Sqrt(2) ); } // DefineBuiltinGlobals