2
0
Эх сурвалжийг харах

[js] add externs for Object and Symbol (also use the Object extern instead of untyped in Reflect methods)

Dan Korostelev 8 жил өмнө
parent
commit
109fdc915a

+ 66 - 0
std/js/Object.hx

@@ -0,0 +1,66 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package js;
+
+import haxe.extern.Rest;
+import haxe.DynamicAccess;
+
+@:native("Object")
+extern class Object {
+	static function assign<T:{}>(target:T, sources:Rest<{}>):T;
+	@:pure static function create<T:{}>(proto:{}, ?propertiesObject:DynamicAccess<ObjectPropertyDescriptor>):T;
+	static function defineProperties<T:{}>(obj:T, props:DynamicAccess<ObjectPropertyDescriptor>):T;
+	static function defineProperty<T:{}>(obj:T, prop:String, descriptor:ObjectPropertyDescriptor):T;
+	static function freeze<T:{}>(obj:T):T;
+	@:pure static function getOwnPropertyDescriptor(obj:{}, prop:String):Null<ObjectPropertyDescriptor>;
+	@:pure static function getOwnPropertyNames(obj:{}):Array<String>;
+	@:pure static function getOwnPropertySymbols(obj:{}):Array<Symbol>;
+	@:pure static function getPrototypeOf<TProto:{}>(obj:{}):Null<TProto>;
+	@:pure static function is<T>(value1:T, value2:T):Bool;
+	@:pure static function isExtensible(obj:{}):Bool;
+	@:pure static function isFrozen(obj:{}):Bool;
+	@:pure static function isSealed(obj:{}):Bool;
+	@:pure static function keys(obj:{}):Array<String>;
+	static function preventExtensions<T:{}>(obj:T):T;
+	static function seal<T:{}>(obj:T):T;
+	static function setPrototypeOf<T:{}>(obj:T, prototype:Null<{}>):T;
+	static var prototype(default,never):ObjectPrototype;
+	@:pure function new(?value:Any);
+}
+
+typedef ObjectPrototype = {
+	var hasOwnProperty(default,never):Function;
+	var isPrototypeOf(default,never):Function;
+	var propertyIsEnumerable(default,never):Function;
+	var toLocaleString(default,never):Function;
+	var toString(default,never):Function;
+	var valueOf(default,never):Function;
+}
+
+typedef ObjectPropertyDescriptor = {
+	@:optional var configurable:Bool;
+	@:optional var enumerable:Bool;
+	@:optional var value:Any;
+	@:optional var writable:Bool;
+	@:optional var get:Void->Any;
+	@:optional var set:Any->Void;
+}

+ 30 - 0
std/js/Symbol.hx

@@ -0,0 +1,30 @@
+/*
+ * Copyright (C)2005-2017 Haxe Foundation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ */
+package js;
+
+@:native("Symbol")
+extern class Symbol {
+	@:pure @:selfCall function new(?description:String);
+	@:native("for") static function for_(key:String):Symbol;
+	@:pure static function keyFor(sym:Symbol):Null<String>;
+	@:pure function toString():String;
+}

+ 2 - 2
std/js/_std/Reflect.hx

@@ -23,7 +23,7 @@
 
 	@:pure
 	public inline static function hasField( o : Dynamic, field : String ) : Bool {
-		return untyped __js__('Object').prototype.hasOwnProperty.call(o, field);
+		return js.Object.prototype.hasOwnProperty.call(o, field);
 	}
 
 	public static function field( o : Dynamic, field : String ) : Dynamic {
@@ -51,7 +51,7 @@
 	public static function fields( o : Dynamic ) : Array<String> {
 		var a = [];
 		if (o != null) untyped {
-			var hasOwnProperty = __js__('Object').prototype.hasOwnProperty;
+			var hasOwnProperty = js.Object.prototype.hasOwnProperty;
 			__js__("for( var f in o ) {");
 			if( f != "__id__" && f != "hx__closures__" && hasOwnProperty.call(o, f) ) a.push(f);
 			__js__("}");