Browse Source

added js.Boot.__cast (fixed issue #841)

Simon Krajewski 13 years ago
parent
commit
cf679c8afb
2 changed files with 19 additions and 3 deletions
  1. 14 3
      genjs.ml
  2. 5 0
      std/js/Boot.hx

+ 14 - 3
genjs.ml

@@ -691,7 +691,12 @@ and gen_expr ctx e =
 	| TCast (e,None) ->
 		gen_expr ctx e
 	| TCast (e1,Some t) ->
-		gen_expr ctx (Codegen.default_cast ctx.com e1 t e.etype e.epos)
+		spr ctx "js.Boot.__cast(";
+		gen_expr ctx e1;
+		spr ctx " , ";
+		spr ctx (ctx.type_accessor t);
+		spr ctx ")"
+
 
 and gen_block ctx e =
 	match e.eexpr with
@@ -750,8 +755,14 @@ and gen_value ctx e =
 	| TBreak
 	| TContinue ->
 		unsupported e.epos
-	| TCast (e1,t) ->
-		gen_value ctx (match t with None -> e1 | Some t -> Codegen.default_cast ctx.com e1 t e.etype e.epos)
+	| TCast (e1, None) ->
+		gen_value ctx e1
+	| TCast (e1, Some t) ->
+		spr ctx "js.Boot.__cast(";
+		gen_value ctx e1;
+		spr ctx " , ";
+		spr ctx (ctx.type_accessor t);
+		spr ctx ")"	
 	| TVars _
 	| TFor _
 	| TWhile _

+ 5 - 0
std/js/Boot.hx

@@ -179,6 +179,11 @@ class Boot {
 		}
 	}
 
+	private static function __cast(o : Dynamic, t : Dynamic) {
+		if (__instanceof(o, t)) return o;
+		else throw "Cannot cast " +Std.string(o) + " to " +Std.string(t);
+	}
+	
 	private static function __init() {
 		untyped {
 			Array.prototype.copy = Array.prototype.slice;