Переглянути джерело

errors for \0 in strings.

Nicolas Cannasse 19 роки тому
батько
коміт
54d92ef694
2 змінених файлів з 12 додано та 11 видалено
  1. 8 6
      genjs.ml
  2. 4 5
      genswf8.ml

+ 8 - 6
genjs.ml

@@ -52,8 +52,7 @@ let ident s = if Hashtbl.mem kwds s then "$" ^ s else s
 let spr ctx s = Buffer.add_string ctx.buf s
 let print ctx = Printf.kprintf (fun s -> Buffer.add_string ctx.buf s)			
 
-let unsupported p = 
-	raise (Typer.Error (Typer.Custom "This expression cannot be compiled to Javascript",p))
+let unsupported = Typer.error "This expression cannot be compiled to Javascript"
 
 let newline ctx = 
 	match Buffer.nth ctx.buf (Buffer.length ctx.buf - 1) with
@@ -111,10 +110,12 @@ let handle_break ctx e =
 
 let this ctx = if ctx.in_value then "$this" else "this"
 
-let gen_constant ctx = function
+let gen_constant ctx p = function
 	| TInt s
 	| TFloat s -> spr ctx s
-	| TString s -> print ctx "\"%s\"" (Ast.s_escape s)
+	| TString s -> 
+		if String.contains s '\000' then Typer.error "A String cannot contain \\0 characters" p;
+		print ctx "\"%s\"" (Ast.s_escape s)
 	| TBool b -> spr ctx (if b then "true" else "false")
 	| TNull -> spr ctx "null"
 	| TThis -> spr ctx (this ctx)
@@ -164,7 +165,7 @@ let rec gen_call ctx e el =
 
 and gen_expr ctx e =
 	match e.eexpr with
-	| TConst c -> gen_constant ctx c
+	| TConst c -> gen_constant ctx e.epos c
 	| TLocal s -> spr ctx (ident s)
 	| TEnumField (e,s) ->
 		print ctx "%s%s" (s_path e.e_path) (field s)
@@ -188,7 +189,7 @@ and gen_expr ctx e =
 			spr ctx "$closure(";
 			gen_value ctx x;
 			spr ctx ",";
-			gen_constant ctx (TString s);
+			gen_constant ctx e.epos (TString s);
 			spr ctx ")";
 		| _ -> 
 			gen_value ctx x;
@@ -651,6 +652,7 @@ let generate file types hres =
 	print ctx "js.Boot.__res = {}";
 	newline ctx;
 	Hashtbl.iter (fun name data ->
+		if String.contains data '\000' then failwith ("Resource " ^ name ^ " contains \\0 characters that can't be used in JavaScript");
 		print ctx "js.Boot.__res[\"%s\"] = \"%s\"" (Ast.s_escape name) (Ast.s_escape data);
 		newline ctx;
 	) hres;

+ 4 - 5
genswf8.ml

@@ -464,7 +464,9 @@ let rec gen_constant ctx c p =
 	match c with
 	| TInt s -> (try push ctx [VInt32 (Int32.of_string s)] with _ -> gen_constant ctx (TFloat s) p)
 	| TFloat s -> push ctx [VFloat (try float_of_string s with _ -> error p)]
-	| TString s -> push ctx [VStr s]
+	| TString s -> 
+		if String.contains s '\000' then Typer.error "A String cannot contain \\0 characters" p;
+		push ctx [VStr s]
 	| TBool b -> write ctx (APush [PBool b])
 	| TNull -> push ctx [VNull]
 	| TThis
@@ -1201,10 +1203,7 @@ let gen_boot ctx hres =
 	push ctx [VReg 0; VStr "__res"];
 	let count = ref 0 in
 	Hashtbl.iter (fun name data ->
-		(try 
-			ignore(String.index data '\000');
-			failwith ("Resource " ^ name ^ " does contain \\0 character than can't be used in Flash");
-		with Not_found -> ());
+		if String.contains data '\000' then failwith ("Resource " ^ name ^ " contains \\0 character than can't be used in Flash");		
 		push ctx [VStr name];
 		gen_big_string ctx data;
 		incr count;