Nicolas Cannasse il y a 19 ans
Parent
commit
a83f7b89e2
11 fichiers modifiés avec 318 ajouts et 11 suppressions
  1. 24 1
      std/Hash.hx
  2. 26 4
      std/Reflect.hx
  3. 26 6
      std/Std.hx
  4. 10 0
      std/StringBuf.hx
  5. 4 0
      std/StringTools.hx
  6. 9 0
      std/XmlParser.hx
  7. 6 0
      std/all.hxml
  8. 7 0
      std/haxe/ImportAll.hx
  9. 4 0
      std/haxe/Log.hx
  10. 158 0
      std/js/Boot.hx
  11. 44 0
      std/js/Lib.hx

+ 24 - 1
std/Hash.hx

@@ -30,40 +30,55 @@ class Hash<T> {
 		h = untyped __new__(_global["Object"]);
 		#else neko
 		h = untyped __dollar__hnew(0);
+		#else js
+		h = untyped __new__("Object");
+		#else error
 		#end
 	}
 
 	public function set( key : String, value : T ) : Void {
 		#if flash
 		untyped h[key] = value;
+		#else js
+		untyped h[key] = value;
 		#else neko
 		untyped __dollar__hset(h,key.__s,value,null);
+		#else error
 		#end
 	}
 
 	public function get( key : String ) : T {
 		#if flash
 		return untyped h[key];
+		#else js
+		return untyped h[key];
 		#else neko
 		return untyped __dollar__hget(h,key.__s,null);
+		#else error
 		#end
 	}
 
 	public function exists( key : String ) : Bool {
 		#if flash
 		return untyped h.hasOwnProperty(key);
+		#else js
+		return untyped h.hasOwnProperty(key);
 		#else neko
 		return untyped __dollar__hmem(h,key.__s,null);
+		#else error
 		#end
 	}
 
 	public function keys() : Iterator<String> {
 		#if flash
 		return untyped (__keys__(h)).iterator();
+		#else js
+		return untyped js.Boot.__keys(h).iterator();
 		#else neko
 		var l = new List<String>();
 		untyped __dollar__hiter(h,function(k,_) { l.push(new String(k)); });
 		return l.iterator();
+		#else error
 		#end
 	}
 
@@ -75,10 +90,18 @@ class Hash<T> {
 			hasNext : function() { return this.it.hasNext(); },
 			next : function() { var i = this.it.next(); return this.ref[i]; }
 		});
+		#else js
+		return untyped({
+			ref : h,
+			it : keys(),
+			hasNext : function() { return this.it.hasNext(); },
+			next : function() { var i = this.it.next(); return this.ref[i]; }
+		});
 		#else neko
 		var l = new List<T>();
 		untyped __dollar__hiter(h,function(_,v) { l.push(v); });
 		return l.iterator();
+		#else error
 		#end
 	}
 
@@ -99,4 +122,4 @@ class Hash<T> {
 
 	private var h : Dynamic;
 
-}
+}

+ 26 - 4
std/Reflect.hx

@@ -31,6 +31,8 @@ class Reflect {
 			__new__(_global["Object"])
 		#else neko
 			__dollar__new(null)
+		#else js
+			__new__("Object")
 		#else error
 		#end
 			;
@@ -57,6 +59,8 @@ class Reflect {
 			}
 		#else neko
 			__dollar__call(__dollar__objget(cl,__dollar__hash("new".__s)),cl,args.__a)
+		#else js
+			__new__(cl,args[0],args[1],args[2],args[3],args[4])
 		#else error
 		#end
 			;
@@ -66,6 +70,8 @@ class Reflect {
 		return untyped
 		#if flash
 			this.hasOwnProperty.call(o,field)
+		#else js
+			o.hasOwnProperty(field)
 		#else neko
 			__dollar__typeof(o) == __dollar__tobject && __dollar__objfield(o,__dollar__hash(field.__s))
 		#else error
@@ -74,11 +80,18 @@ class Reflect {
 	}
 
 	public static function field( o : Dynamic, field : String ) : Dynamic {
-		return untyped
+		untyped
 		#if flash
 			{
 				var f = o[field];
-				if( f == null && !this.hasOwnProperty(o,f) )
+				if( f == null && !this.hasOwnProperty.call(o,f) )
+					throw ("No such field : " + field);
+				return f;
+			}
+		#else js
+			{
+				var f = o[field];
+				if( f == null && !o.hasOwnProperty(f) )
 					throw ("No such field : " + field);
 				return f;
 			}
@@ -98,21 +111,24 @@ class Reflect {
 	}
 
 	public static function setField( o : Dynamic, field : String, value : Dynamic ) : Void {
-		untyped {
+		untyped
 		#if flash
 			o[field] = value;
+		#else js
+			o[field] = value;
 		#else neko
 			if( __dollar__typeof(o) == __dollar__tobject )
 				__dollar__objset(o,__dollar__hash(field.__s),value);
 		#else error
 		#end
-		}
 	}
 
 	public static function callMethod( o : Dynamic, func : Dynamic, args : Array<Dynamic> ) : Dynamic {
 		return untyped
 		#if flash
 			func.apply(o,args)
+		#else js
+			func.apply(o,args)
 		#else neko
 			__dollar__call(func,o,args.__a)
 		#else error
@@ -124,6 +140,8 @@ class Reflect {
 		return untyped
 		#if flash
 			__keys__(o)
+		#else js
+			js.Boot.__keys(o)
 		#else neko
 			if( __dollar__typeof(o) != __dollar__tobject )
 				new Array<String>();
@@ -146,6 +164,8 @@ class Reflect {
 		return untyped
 		#if flash
 			f.call == _global["Function"].call
+		#else js
+			f.call == isFunction.call
 		#else neko
 			__dollar__typeof(f) == __dollar__tfunction
 		#else error
@@ -156,6 +176,8 @@ class Reflect {
 	public static function deleteField( o : Dynamic, f : String ) {
 		#if flash
 			untyped __delete__(o,f)
+		#else js
+			untyped delete(o[f])
 		#else neko
 			untyped __dollar__objremove(o,f.__s)
 		#else error

+ 26 - 6
std/Std.hx

@@ -34,6 +34,8 @@ class Std {
 		flash.Boot.__instanceof(obj,vclass);
 		#else neko
 		neko.Boot.__instanceof(obj,vclass);
+		#else js
+		js.Boot.__instanceof(obj,vclass);
 		#else error
 		#end
 	}
@@ -44,6 +46,8 @@ class Std {
 		flash.Boot.__string_rec(s,"");
 		#else neko
 		__dollar__string(s);
+		#else js
+		js.Boot.__string_rec(s,"");
 		#else error
 		#end
 	}
@@ -62,6 +66,8 @@ class Std {
 		_global.parseInt(x);
 		#else neko
 		__dollar__int(x.__s);
+		#else js
+		__top__parseInt(x);
 		#else error
 		#end
 	}
@@ -72,6 +78,8 @@ class Std {
 		_global.parseFloat(x);
 		#else neko
 		__dollar__float(x.__s);
+		#else js
+		__top__parseFloat(x);
 		#else error
 		#end
 	}
@@ -86,25 +94,31 @@ class Std {
 			__dollar__sset(s,0,x);
 			new String(s);
 		}
+		#else js
+		String.fromCharCode(x);
 		#else error
 		#end
 	}
 
 	public static function ord( x : String ) : Int {
-		return untyped
 		#if flash
 		if( x == "" )
-			null;
+			return null;
 		else
-			x.charCodeAt(0);
+			return x.charCodeAt(0);
 		#else neko
-		{
+		untyped {
 			var s = __dollar__ssize(x.__s);
 			if( s == 0 )
-				null;
+				return null;
 			else
-				__dollar__sget(s,0);
+				return __dollar__sget(s,0);
 		}
+		#else js
+		if( x == "" )
+			return null;
+		else
+			return x.charCodeAt(0);
 		#else error
 		#end
 	}
@@ -115,6 +129,8 @@ class Std {
 		_global.isFinite(i);
 		#else neko
 		!__dollar__isinfinite(i);
+		#else js
+		__top__isFinite(i);
 		#else error
 		#end
 	}
@@ -125,6 +141,8 @@ class Std {
 		_global.isNaN(i);
 		#else neko
 		__dollar__isnan(i);
+		#else js
+		__top__isNaN(i);
 		#else error
 		#end
 	}
@@ -135,6 +153,8 @@ class Std {
 		__random__(x);
 		#else neko
 		Math._rand_int(Math._rnd,x);
+		#else js
+		Math.floor(Math.random()*x);
 		#else error
 		#end
 	}

+ 10 - 0
std/StringBuf.hx

@@ -30,6 +30,8 @@ class StringBuf {
 		b = __make();
 		#else flash
 		b = "";
+		#else js
+		b = "";
 		#else error
 		#end
 	}
@@ -39,6 +41,8 @@ class StringBuf {
 		__add(b,x);
 		#else flash
 		b += untyped flash.Boot.__string_rec(x,"");
+		#else js
+		b += untyped js.Boot.__string_rec(x,"");
 		#else error
 		#end
 	}
@@ -48,6 +52,8 @@ class StringBuf {
 		__add_sub(b,untyped s.__s,pos,len);
 		#else flash
 		b += s.substr(pos,len);
+		#else js
+		b += s.substr(pos,len);
 		#else error
 		#end
 	}
@@ -57,6 +63,8 @@ class StringBuf {
 		__add_char(b,c);
 		#else flash
 		b += untyped String.fromCharCode(c);
+		#else js
+		b += untyped String.fromCharCode(c);
 		#else error
 		#end
 	}
@@ -66,6 +74,8 @@ class StringBuf {
 		return new String(__string(b));
 		#else flash
 		return b;
+		#else js
+		return b;
 		#else error
 		#end
 	}

+ 4 - 0
std/StringTools.hx

@@ -30,6 +30,8 @@ class StringTools {
 		return untyped _global.escape(s);
 		#else neko
 		return new String(_urlEncode(untyped s.__s));
+		#else js
+		return untyped __top__encodeURI(s);
 		#else error
 		#end
 	}
@@ -39,6 +41,8 @@ class StringTools {
 		return untyped _global.unescape(s);
 		#else neko
 		return new String(_urlDecode(untyped s.__s));
+		#else js
+		return untyped __top__decodeURI(s);
 		#else error
 		#end
 	}

+ 9 - 0
std/XmlParser.hx

@@ -99,6 +99,15 @@ class XmlParser {
 			untyped _parse(xmlData.__s,parser);
 			hierarchy(x);
 			return x;
+		#else js
+		if( js.Lib.isIE ) untyped {
+			var x = __new__("ActiveXObject","Microsoft.XMLDOM");
+			x.async="false";
+			x.loadXML(xmlData);
+			return x;
+		} else
+			return untyped __new__("DOMParser").parseFromString(xmlData, "text/xml");
+		#else error
 		#end
 	}
 

+ 6 - 0
std/all.hxml

@@ -7,3 +7,9 @@ ImportAll
 -swf all.swf
 -xml flash.xml
 ImportAll
+
+--next
+
+-js all.js
+-xml js.xml
+ImportAll

+ 7 - 0
std/haxe/ImportAll.hx

@@ -120,3 +120,10 @@ import neko.db.ResultSet;
 import tools.DocView;
 
 #end
+
+#if js
+
+import js.Boot;
+import js.Lib;
+
+#end

+ 4 - 0
std/haxe/Log.hx

@@ -39,6 +39,8 @@ class Log {
 		untyped flash.Boot.__trace(v,infos);
 		#else neko
 		untyped __dollar__print(infos.fileName+":"+infos.lineNumber+": ",v,"\n");
+		#else js
+		untyped js.Boot.__trace(v,infos);
 		#else error
 		#end
 	}
@@ -46,6 +48,8 @@ class Log {
 	public static function clear() : Void {
 		#if flash
 		untyped flash.Boot.__clear_trace();
+		#else js
+		untyped js.Boot.__clear_trace();
 		#else neko
 		// nothing
 		#else error

+ 158 - 0
std/js/Boot.hx

@@ -0,0 +1,158 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package js;
+
+class Boot {
+
+	private static function __keys(o) {
+		return []; // TODO
+	}
+
+	private static function __unhtml(s : String) {
+		return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
+	}
+
+	private static function __trace(v,i) {
+		untyped {
+			var msg = i.fileName+":"+i.lineNumber+": "+__unhtml(__string_rec(v,""))+"<br/>";
+			var d = document.getElementById("haxe:trace");
+			if( d == null )
+				alert("No haxe:trace element defined\n"+msg);
+			else
+				d.innerHTML += msg;
+		}
+	}
+
+	private static function __clear_trace() {
+		untyped {
+			var d = document.getElementById("haxe:trace");
+			if( d != null )
+				d.innerHTML = "";
+		}
+	}
+
+	private static function __string_rec(o,s) {
+		return untyped __top__String(o); // TODO
+	}
+
+	private static function __instanceof(o,cl) {
+		return false; // TODO
+	}
+
+	private static function __init() {
+		untyped {
+			Node.element_node = 1;
+			Node.text_node = 3;
+			Node.prototype.nodes = function() {
+				return untyped {
+					p : 0,
+					a : childNodes,
+					next : function() {
+						while true {
+							var x = this.a[this.p];
+							if( x == null )
+								return null;
+							this.p++;
+							if( x.nodeType == 1 )
+								return x;
+						}
+						return null;
+					},
+					hasNext : function() {
+						var x = this.next();
+						if( x != null ) {
+							this.p--;
+							return true;
+						}
+						return false;
+					}
+				};
+			};
+			Array.prototype.copy = Array.prototype.slice;
+			Array.prototype.insert = function(i,x) {
+				this.splice(i,0,x);
+			};
+			Array.prototype.remove = function(obj) {
+				var i = 0;
+				var l = this.length;
+				while( i < l ) {
+					if( this[i] == obj ) {
+						this.splice(i,1);
+						return true;
+					}
+					i++;
+				}
+				return false;
+			}
+			Array.prototype.iterator = function() {
+				return {
+					cur : 0,
+					max : this.length,
+					arr : this,
+					hasNext : function() {
+						return this.cur < this.max;
+					},
+					next : function() {
+						return this.arr[this.cur++];
+					}
+				}
+			};
+			Array.prototype.indexes = function() {
+				return {
+					cur : 0,
+					max : this.length,
+					hasNext : function() {
+						return this.cur < this.max;
+					},
+					next : function() {
+						return this.cur++;
+					}
+				}
+			};
+			Date.now = function() {
+				return __new__(Date);
+			};
+			Date.prototype.toString = function() {
+				var m = this.getMonth() + 1;
+				var d = this.getDate();
+				var h = this.getHours();
+				var mi = this.getMinutes();
+				var s = this.getSeconds();
+				if( d < 10 )
+					d = "0" + d;
+				if( m < 10 )
+					m = "0" + m;
+				if( h < 10 )
+					h = "0" + h;
+				if( mi < 10 )
+					mi = "0" + mi;
+				if( s < 10 )
+					s = "0" + s;
+				return this.getFullYear()+"-"+m+"-"+d+" "+h+":"+mi+":"+s;
+			};
+		}
+	}
+
+}

+ 44 - 0
std/js/Lib.hx

@@ -0,0 +1,44 @@
+/*
+ * Copyright (c) 2005, The haXe Project Contributors
+ * All rights reserved.
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   - Redistributions of source code must retain the above copyright
+ *     notice, this list of conditions and the following disclaimer.
+ *   - Redistributions in binary form must reproduce the above copyright
+ *     notice, this list of conditions and the following disclaimer in the
+ *     documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
+ * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+package js;
+
+class Lib {
+
+	public static var isIE = untyped (document.all != null);
+
+	public static function alert( v : Dynamic ) {
+		untyped __top__alert(js.Boot.__string_rec(v,""));
+	}
+
+	public static function setErrorHandler( f : String -> String -> Int -> Bool ) {
+		untyped __top__onerror = f;
+	}
+
+	public static function defaultHandler( msg : String, url : String, line : Int ) {
+		alert("Error "+url+" ("+line+")\n\n"+msg);
+		return true;
+	}
+
+}