Object.hx 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Copyright (C)2005-2019 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. package js.lib;
  23. import haxe.extern.Rest;
  24. import haxe.DynamicAccess;
  25. /**
  26. The `js.lib.Object` constructor creates an object wrapper.
  27. Documentation [Object](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) by [Mozilla Contributors](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object$history), licensed under [CC-BY-SA 2.5](https://creativecommons.org/licenses/by-sa/2.5/).
  28. **/
  29. @:native("Object")
  30. extern class Object {
  31. /**
  32. The Object.assign() method is used to copy the values of all enumerable
  33. own properties from one or more source objects to a target object. It
  34. will return the target object.
  35. Note: this is an ES2015 feature
  36. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
  37. **/
  38. static function assign<TSource:{}, TDest:{}>(target:TSource, sources:Rest<{}>):TDest;
  39. /**
  40. The Object.create() method create a new object, using an existing object
  41. to provide the newly created object's __proto__ . (see browser console
  42. for visual evidence.)
  43. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create
  44. **/
  45. @:pure static function create<T>(proto:Null<{}>, ?propertiesObject:DynamicAccess<ObjectPropertyDescriptor<Any>>):T;
  46. /**
  47. The Object.defineProperties() method defines new or modifies existing
  48. properties directly on an object, returning the object.
  49. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties
  50. **/
  51. static function defineProperties<T:{}>(obj:T, props:DynamicAccess<ObjectPropertyDescriptor<Any>>):T;
  52. /**
  53. The static method Object.defineProperty() defines a new property directly
  54. on an object, or modifies an existing property on an object, and returns
  55. the object.
  56. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
  57. **/
  58. @:overload(function<T:{}, TProp>(obj:T, prop:Symbol, descriptor:ObjectPropertyDescriptor<TProp>):T {})
  59. static function defineProperty<T:{}, TProp>(obj:T, prop:String, descriptor:ObjectPropertyDescriptor<TProp>):T;
  60. /**
  61. The Object.entries() method returns an array of a given object's own
  62. enumerable property [key, value] pairs, in the same order as that
  63. provided by a for...in loop (the difference being that a for-in loop
  64. enumerates properties in the prototype chain as well).
  65. Note: this is an ES2017 feature
  66. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
  67. **/
  68. @:pure static function entries<T:{}>(obj:T):Array<ObjectEntry>;
  69. /**
  70. The Object.freeze() method freezes an object: that is, prevents new
  71. properties from being added to it; prevents existing properties from
  72. being removed; and prevents existing properties, or their enumerability,
  73. configurability, or writability, from being changed, it also prevents the
  74. prototype from being changed.
  75. The method returns the passed object.
  76. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze
  77. **/
  78. static function freeze<T:{}>(obj:T):T;
  79. /**
  80. Returns a new object from an iterable of key-value pairs
  81. (reverses Object.entries).
  82. **/
  83. @:pure static function fromEntries<T:{}>(iterable:Any):T;
  84. /**
  85. The Object.getOwnPropertyDescriptor() method returns a property
  86. descriptor for an own property (that is, one directly present on an
  87. object and not in the object's prototype chain) of a given object.
  88. In ES5, if the first argument to this method is not an object (a
  89. primitive), then it will cause a TypeError. In ES2015, a non-object
  90. first argument will be coerced to an object at first.
  91. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptor
  92. **/
  93. @:overload(function(obj:String, prop:Symbol):Null<ObjectPropertyDescriptor<String>> {})
  94. @:overload(function(obj:String, prop:String):Null<ObjectPropertyDescriptor<String>> {})
  95. @:overload(function<T>(target:Array<T>, propertyKey:Int):Null<ObjectPropertyDescriptor<T>> {})
  96. @:overload(function<T, TProp>(obj:T, prop:Symbol):Null<ObjectPropertyDescriptor<TProp>> {})
  97. @:pure static function getOwnPropertyDescriptor<T, TProp>(obj:T, prop:String):Null<ObjectPropertyDescriptor<TProp>>;
  98. /**
  99. The Object.getOwnPropertyDescriptors() method returns all own property
  100. descriptors of a given object.
  101. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyDescriptors
  102. **/
  103. @:overload(function(target:String):DynamicAccess<ObjectPropertyDescriptor<String>> {})
  104. @:overload(function<T>(target:Array<T>):DynamicAccess<ObjectPropertyDescriptor<T>> {})
  105. @:pure static function getOwnPropertyDescriptors<T>(obj:T):DynamicAccess<ObjectPropertyDescriptor<Any>>;
  106. /**
  107. The Object.getOwnPropertyNames() method returns an array of all
  108. properties (including non-enumerable properties except for those which
  109. use Symbol) found directly upon a given object.
  110. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
  111. **/
  112. @:pure static function getOwnPropertyNames<T:{}>(obj:T):Array<String>;
  113. /**
  114. The Object.getOwnPropertySymbols() method returns an array of all symbol
  115. properties found directly upon a given object.
  116. Note: this is an ES2015 feature
  117. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertySymbols
  118. **/
  119. @:pure static function getOwnPropertySymbols<T:{}>(obj:T):Array<Symbol>;
  120. /**
  121. The Object.getPrototypeOf() method returns the prototype (i.e. the value
  122. of the internal [[Prototype]] property) of the specified object.
  123. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getPrototypeOf
  124. **/
  125. @:pure static function getPrototypeOf<T:{}, TProto>(obj:T):TProto;
  126. /**
  127. The Object.is() method determines whether two values are the same value.
  128. Note: this is an ES2015 feature
  129. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  130. **/
  131. @:native("is") @:pure static function isSame<T:{}>(obj1:T, obj2:T):Bool;
  132. /**
  133. The Object.is() method determines whether two values are the same value.
  134. Note: this is an ES2015 feature
  135. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is
  136. **/
  137. @:deprecated("Use Object.isSame()")
  138. @:pure static function is<T:{}>(obj1:T, obj2:T):Bool;
  139. /**
  140. The Object.isExtensible() method determines if an object is extensible
  141. (whether it can have new properties added to it).
  142. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible
  143. **/
  144. @:pure static function isExtensible<T:{}>(obj:T):Bool;
  145. /**
  146. The Object.isFrozen() determines if an object is frozen.
  147. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen
  148. **/
  149. @:pure static function isFrozen<T:{}>(obj:T):Bool;
  150. /**
  151. The Object.isSealed() method determines if an object is sealed.
  152. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed
  153. **/
  154. @:pure static function isSealed<T:{}>(obj:T):Bool;
  155. /**
  156. The Object.keys() method returns an array of a given object's own
  157. enumerable properties, in the same order as that provided by a for...in
  158. loop (the difference being that a for-in loop enumerates properties in
  159. the prototype chain as well).
  160. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys
  161. **/
  162. @:pure static function keys<T:{}>(obj:T):Array<String>;
  163. /**
  164. The Object.preventExtensions() method prevents new properties from ever
  165. being added to an object (i.e. prevents future extensions to the object).
  166. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions
  167. **/
  168. static function preventExtensions<T:{}>(obj:T):T;
  169. /**
  170. The Object.seal() method seals an object, preventing new properties from
  171. being added to it and marking all existing properties as
  172. non-configurable. Values of present properties can still be changed as
  173. long as they are writable.
  174. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal
  175. **/
  176. static function seal<T:{}>(obj:T):T;
  177. /**
  178. The Object.setPrototypeOf() method sets the prototype (i.e., the internal
  179. [[Prototype]] property) of a specified object to another object or null.
  180. Note: this is an ES2015 feature
  181. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/setPrototypeOf
  182. **/
  183. static function setPrototypeOf<T:{}, TProto:{}>(obj:T, proto:Null<TProto>):T;
  184. /**
  185. The Object.values() method returns an array of a given object's own
  186. enumerable property values, in the same order as that provided by a
  187. for...in loop (the difference being that a for-in loop enumerates
  188. properties in the prototype chain as well).
  189. Note: this is an ES2017 feature
  190. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values
  191. **/
  192. @:pure static function values<T:{}>(obj:T):Array<Any>;
  193. /**
  194. Allows the addition of properties to all objects of type Object.
  195. **/
  196. static var prototype(default, never):ObjectPrototype;
  197. /**
  198. The Object constructor creates an object wrapper.
  199. **/
  200. @:pure function new(?value:Any);
  201. }
  202. /**
  203. Type for
  204. @see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object>
  205. **/
  206. typedef ObjectPrototype = {
  207. /**
  208. Returns a boolean indicating whether an object contains the specified
  209. property as a direct property of that object and not inherited through
  210. the prototype chain.
  211. **/
  212. var hasOwnProperty(default, never):Function;
  213. /**
  214. Returns a boolean indicating whether the object this method is called
  215. upon is in the prototype chain of the specified object.
  216. **/
  217. var isPrototypeOf(default, never):Function;
  218. /**
  219. Returns a boolean indicating if the internal enumerable attribute is set.
  220. **/
  221. var propertyIsEnumerable(default, never):Function;
  222. /**
  223. Calls `toString()`.
  224. **/
  225. var toLocaleString(default, never):Function;
  226. /**
  227. Returns a string representation of the object.
  228. **/
  229. var toString(default, never):Function;
  230. /**
  231. Returns the primitive value of the specified object.
  232. **/
  233. var valueOf(default, never):Function;
  234. }
  235. /**
  236. @see <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty>
  237. **/
  238. typedef ObjectPropertyDescriptor<TProp> = {
  239. /**
  240. `true` if and only if the type of this property descriptor may be
  241. changed and if the property may be deleted from the corresponding object.
  242. Defaults to `false`.
  243. **/
  244. var ?configurable:Bool;
  245. /**
  246. `true` if and only if this property shows up during enumeration of the
  247. properties on the corresponding object.
  248. Defaults to `false`.
  249. **/
  250. var ?enumerable:Bool;
  251. /**
  252. The value associated with the property.
  253. Can be any valid JavaScript value (number, object, function, etc).
  254. **/
  255. var ?value:TProp;
  256. /**
  257. `true` if and only if the value associated with the property may be
  258. changed with an assignment operator.
  259. Defaults to `false`.
  260. **/
  261. var ?writable:Bool;
  262. /**
  263. A function which serves as a getter for the property, or `undefined` if
  264. there is no getter. When the property is accessed, this function is
  265. called without arguments and with `this` set to the object through which
  266. the property is accessed (this may not be the object on which the
  267. property is defined due to inheritance).
  268. The return value will be used as the value of the property.
  269. **/
  270. var ?get:Void->TProp;
  271. /**
  272. A function which serves as a setter for the property, or undefined if
  273. there is no setter. When the property is assigned to, this function
  274. is called with one argument (the value being assigned to the property)
  275. and with `this` set to the object through which the property is assigned.
  276. **/
  277. var ?set:TProp->Void;
  278. }
  279. /**
  280. Key/value access helper for `js.lib.Object.entries()`.
  281. **/
  282. abstract ObjectEntry(Array<Any>) {
  283. public var key(get, never):String;
  284. public var value(get, never):Any;
  285. inline function get_key():String
  286. return this[0];
  287. inline function get_value():Any
  288. return this[1];
  289. }