private static dynamic BooleanConstructor(this JS js, dynamic value) { dynamic b = new JSObject(); b.JSTypeName = "Boolean"; // Set up prototype dynamic p = new JSObject(); b.Prototype = b.GetPrototype(p); // Calculate the primitive value bool _primitiveValue = true; //default to true and only set to false when needed dynamic v = (value is JSObject && !(value is JSUndefined || value is JSNaN)) ? value.valueOf() : value; if (v == null || (v is String && (v == "" || v == "false")) || (v is Boolean && v == false) || ((v is Int32 || v is Int64 || v is Int16) && v == 0) || v is JSNaN || v is JSUndefined) { _primitiveValue = false; } // Set up instance items b.valueOf = new Func<bool>(() => _primitiveValue); b.toString = new Func<string>(() => _primitiveValue ? "true" : "false"); //Consider using String() here return b; }
private static dynamic StringConstructor(this JS js, dynamic value) { dynamic s = new JSObject(); s.JSTypeName = "String"; // Set up the prototype first dynamic p = new JSObject(); s.Prototype = s.GetPrototype(p); // Set up the instance behavior var _primitiveValue = (value == null) ? null : (value is JSObject && JS.cs.Boolean(value.toString as string)) ? value.toString() : value.ToString(); s.toString = s.valueOf = new Func<string>(() => _primitiveValue); return s; }