Browse Source

Enable ES5 strict mode with --js-modern.

Since deleting __proto__ is not allowed in strict mode, and __proto__ is
non-standard and being phased out anyways, I switched (Int)Hash over to use
Object.create where available. Where Object.create is not supported (mostly old
IE), neither was __proto__, so this should have no impact.

Fixes issue #675.
Bruno Garcia 13 years ago
parent
commit
40d8b09a76
6 changed files with 13 additions and 20 deletions
  1. 1 1
      doc/CHANGES.txt
  2. 6 1
      genjs.ml
  3. 1 1
      std/js/Boot.hx
  4. 1 7
      std/js/_std/Hash.hx
  5. 2 6
      std/js/_std/IntHash.hx
  6. 2 4
      std/js/_std/Reflect.hx

+ 1 - 1
doc/CHANGES.txt

@@ -31,7 +31,7 @@
 	js : make difference between values and statements expressions in JSGenApi
 	js : added source mapping with -debug (replace previous stack emulation)
 	flash : added @:file("a.dat") class File extends flash.utils.ByteArray
-	js : added --js-modern for wrapping output in a closure
+	js : added --js-modern for wrapping output in a closure and ES5 strict mode
 
 2011-09-25: 2.08
 	js : added js.JQuery

+ 6 - 1
genjs.ml

@@ -999,9 +999,14 @@ let generate com =
 	| None ->
 	let ctx = alloc_ctx com in
 	if ctx.js_modern then begin
-		print ctx "(function () {";
+		(* Additional ES5 strict mode keywords. *)
+		List.iter (fun s -> Hashtbl.add kwds s ()) [ "arguments"; "eval" ];
+
+		(* Wrap output in a closure. *)
+		print ctx "(function () { \"use strict\"";
 		newline ctx;
 	end;
+
 	let hxClasses = if ctx.js_modern then "{}" else "$hxClasses || {}" in
 	print ctx "var $_, $hxClasses = %s, $estr = function() { return js.Boot.__string_rec(this,''); }
 function $extend(from, fields) {

+ 1 - 1
std/js/Boot.hx

@@ -238,7 +238,7 @@ class Boot {
 			};
 			Function.prototype["$bind"] = function(o){
 				var f = function(){
-					return f.method.apply(f.scope, arguments);
+					return f.method.apply(f.scope, untyped __js__("arguments"));
 				}
 				f.scope = o;
 				f.method = __this__;

+ 1 - 7
std/js/_std/Hash.hx

@@ -28,13 +28,7 @@
 	private var h : Dynamic;
 
 	public function new() : Void {
-		untyped {
-			h = __js__("{}");
-			if( h.__proto__ != null ) {
-				h.__proto__ = null;
-				__js__("delete")(h.__proto__);
-			}
-		}
+		h = untyped ( Object.create != null ) ? Object.create(null) : {};
 	}
 
 	public function set( key : String, value : T ) : Void {

+ 2 - 6
std/js/_std/IntHash.hx

@@ -28,11 +28,7 @@
 	private var h : Dynamic;
 
 	public function new() : Void {
-		h = untyped __js__("{}");
-		untyped if( h.__proto__ != null ) {
-			h.__proto__ = null;
-			__js__("delete")(h.__proto__);
-		};
+		h = untyped ( Object.create != null ) ? Object.create(null) : {};
 	}
 
 	public function set( key : Int, value : T ) : Void {
@@ -55,7 +51,7 @@
 
 	public function keys() : Iterator<Int> {
 		var a = new Array();
-		untyped __js__("for( x in this.h ) a.push(x|0)");
+		untyped __js__("for( var x in this.h ) a.push(x|0)");
 		return a.iterator();
 	}
 

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

@@ -115,10 +115,8 @@
 	}
 
 	public static function makeVarArgs( f : Array<Dynamic> -> Dynamic ) : Dynamic {
-		return function() untyped {
-			var a = new Array();
-			for( i in 0...arguments.length )
-				a.push(arguments[i]);
+		return function() {
+			var a = untyped Array.prototype.slice.call(__js__("arguments"));
 			return f(a);
 		};
 	}